Skip to content

Unify listen-only mode into can.Bus(listen_only=...) - #2149

Merged
zariiii9003 merged 1 commit into
hardbyte:mainfrom
zariiii9003:listen-only-split-v3
Nov 29, 2026
Merged

zariiii9003 merged 1 commit into
hardbyte:mainfrom
zariiii9003:listen-only-split-v3

Conversation

@zariiii9003

@zariiii9003 zariiii9003 commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary of Changes

This adds one portable listen_only option to can.Bus / BusABC. It replaces the different listen-only switches each interface had before.

 can.Bus(interface, channel, ..., listen_only=None, **kwargs)
   util.load_config()                # listen_only also comes from config file / env; must be a bool
+  if listen_only is False and the class cannot take it → drop it   # False always works
   BusABC.__new__
+    if listen_only is True and no constructor in the MRO declares it
+      raise CanInterfaceNotImplementedError    # before any driver/device is opened
   <Interface>.__init__(listen_only=...)
+    True  → apply listen-only mode, or raise CanInitializationError if it can't be applied
+    False → normal mode
+    None  → leave the mode unchanged (cantact, kvaser, nixnet, pcan, vector)
+  send() in listen-only mode → CanOperationError
  • New API
    • listen_only argument on can.Bus and BusABC.__init__.
    • Read-only BusABC.listen_only property. It can also be set at runtime on pcan and systec, and ThreadSafeBus forwards it.
    • can.cli.add_bus_arguments(listen_only_arg=...).
    • --listen-only / --no-listen-only on can.logger and can.viewer.
  • Interfaces with support: cantact, kvaser, nixnet, pcan, seeedstudio, slcan, systec, vector, virtual.
  • Third-party plugins: the listen_only=True check inspects the constructor signatures, so plugins need no changes.
    • A plugin that declares listen_only is accepted.
    • A plugin that does not declare it raises CanInterfaceNotImplementedError instead of silently opening in normal mode.
  • Deprecated (each emits a DeprecationWarning attributed to the caller, and raises ValueError if it contradicts listen_only):
    • cantact: monitor
    • kvaser: driver_mode
    • seeedstudio: the "silent" and "loopback_and_silent" operation modes
    • pcan / systec: the state argument and setting bus.state
    • can.logger: --active / --passive
  • Fixed
    • Requests that previously fell back to normal mode without notice now raise CanInitializationError: vector/kvaser without init access, kvaser controllers without silent mode, and PCAN-LAN devices.
    • kvaser's silent driver_mode now takes effect with single_handle=False, because it is applied to the handle that has init access.
    • can.logger --active no longer raises NotImplementedError. can.logger --passive now works on every interface that supports listen-only mode, not only pcan and systec.
    • An invalid seeedstudio operation_mode now raises ValueError before the serial port is opened, instead of a KeyError afterwards.
  • Docs
    • New "listen-only mode" section in doc/bus.rst.
    • Updated pcan, seeedstudio and systec interface pages.
    • Updated doc/internal-api.rst.

Before / after (the same script run on main and on this branch, default warning filters):

Snippet main this PR
Bus("virtual", listen_only=True).send(msg) frame is sent; the peer receives it CanOperationError: Cannot send in listen-only mode
bus.listen_only AttributeError True
Bus("udp_multicast", listen_only=True) opens in normal mode without notice CanInterfaceNotImplementedError: UdpMulticastBus does not support listen-only mode
Bus("udp_multicast", listen_only=False) opens opens
Bus("cantact", monitor=True) no warning demo.py:9: DeprecationWarning: The 'monitor' argument is deprecated …, and bus.listen_only is True

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe): deprecates the interface-specific listen-only options (monitor, driver_mode, seeedstudio silent modes, state / bus.state, --active / --passive)

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

  • Behaviour changes to note:
    • listen_only=True on an interface without support now raises instead of being ignored.
    • On vector, an explicit listen_only=False now requests init access and sets normal mode. It raises if a channel without init access is not already in normal mode. The default None leaves the channel as it was.
  • Backward compatibility:
    • Every deprecated option keeps working until v5.0.0.
    • listen_only=False (from an argument, a config file or the environment) is accepted by every interface, including plugins that don't know the argument.
  • News fragments: doc/changelog.d/2149.{added,changed,deprecated,fixed}.rst.
  • Tests: added to the existing test file of each affected interface and script (test_bus, test_cli, test_load_config, test_logger, test_viewer, back2back_test, plus cantact, kvaser, pcan, seeedstudio, slcan, systec, vector and virtual).

🤖 Generated with AI

@zariiii9003
zariiii9003 force-pushed the listen-only-split-v3 branch 4 times, most recently from 81084ba to ae4f810 Compare November 29, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant