From 6b6de045821c5ba9c42c77598a9b346a8f94fae9 Mon Sep 17 00:00:00 2001 From: Brent Gardner Date: Sun, 1 Dec 2024 17:44:15 -0500 Subject: [PATCH] New BusState members per #736 --- CONTRIBUTORS.txt | 1 + can/bus.py | 10 ++++++---- can/interfaces/etas/__init__.py | 6 +++--- can/interfaces/ixxat/canlib_vcinpl.py | 8 ++++---- can/interfaces/pcan/pcan.py | 8 ++++---- can/interfaces/systec/ucanbus.py | 12 ++++++------ can/logger.py | 4 ++-- doc/interfaces/pcan.rst | 4 ++-- doc/interfaces/systec.rst | 2 +- examples/receive_all.py | 2 +- test/test_logger.py | 4 ++-- test/test_pcan.py | 6 +++--- test/test_systec.py | 2 +- 13 files changed, 36 insertions(+), 33 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 524908dfc..92061d36c 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -92,3 +92,4 @@ Felix Nieuwenhuizen @SWolfSchunk @belliriccardo @cssedev +@bggardner diff --git a/can/bus.py b/can/bus.py index a12808ab6..269ce7dfc 100644 --- a/can/bus.py +++ b/can/bus.py @@ -35,9 +35,11 @@ class BusState(Enum): """The state in which a :class:`can.BusABC` can be.""" - ACTIVE = auto() - PASSIVE = auto() - ERROR = auto() + ERROR_ACTIVE = auto() + ERROR_PASSIVE = auto() + BUS_OFF = auto() + STOPPED = auto() + UNKNOWN = auto() class CanProtocol(Enum): @@ -510,7 +512,7 @@ def state(self) -> BusState: """ Return the current state of the hardware """ - return BusState.ACTIVE + return BusState.ERROR_ACTIVE @state.setter def state(self, new_state: BusState) -> None: diff --git a/can/interfaces/etas/__init__.py b/can/interfaces/etas/__init__.py index 2bfcbf427..1edec96ed 100644 --- a/can/interfaces/etas/__init__.py +++ b/can/interfaces/etas/__init__.py @@ -278,14 +278,14 @@ def state(self) -> can.BusState: status = OCI_CANControllerStatus() OCI_GetCANControllerStatus(self.ctrl, ctypes.byref(status)) if status.stateCode & OCI_CAN_STATE_ACTIVE: - return can.BusState.ACTIVE + return can.BusState.ERROR_ACTIVE elif status.stateCode & OCI_CAN_STATE_PASSIVE: - return can.BusState.PASSIVE + return can.BusState.ERROR_PASSIVE @state.setter def state(self, new_state: can.BusState) -> None: # disabled, OCI_AdaptCANConfiguration does not allow changing the bus mode - # if new_state == can.BusState.ACTIVE: + # if new_state == can.BusState.ERROR_ACTIVE: # self.ctrlConf.busParticipationMode = OCI_BUSMODE_ACTIVE # else: # self.ctrlConf.busParticipationMode = OCI_BUSMODE_PASSIVE diff --git a/can/interfaces/ixxat/canlib_vcinpl.py b/can/interfaces/ixxat/canlib_vcinpl.py index 0579d0942..87e950d74 100644 --- a/can/interfaces/ixxat/canlib_vcinpl.py +++ b/can/interfaces/ixxat/canlib_vcinpl.py @@ -849,19 +849,19 @@ def state(self) -> BusState: status = structures.CANLINESTATUS() _canlib.canControlGetStatus(self._control_handle, ctypes.byref(status)) if status.bOpMode == constants.CAN_OPMODE_LISTONLY: - return BusState.PASSIVE + return BusState.ERROR_PASSIVE error_byte_1 = status.dwStatus & 0x0F # CAN_STATUS_BUSOFF = 0x08 # bus off status if error_byte_1 & constants.CAN_STATUS_BUSOFF: - return BusState.ERROR + return BusState.BUS_OFF error_byte_2 = status.dwStatus & 0xF0 # CAN_STATUS_BUSCERR = 0x20 # bus coupling error if error_byte_2 & constants.CAN_STATUS_BUSCERR: - return BusState.ERROR + return BusState.STOPPED - return BusState.ACTIVE + return BusState.ERROR_ACTIVE # ~class IXXATBus(BusABC): --------------------------------------------------- diff --git a/can/interfaces/pcan/pcan.py b/can/interfaces/pcan/pcan.py index d0372a83c..41d9dc5cd 100644 --- a/can/interfaces/pcan/pcan.py +++ b/can/interfaces/pcan/pcan.py @@ -121,7 +121,7 @@ def __init__( self, channel: str = "PCAN_USBBUS1", device_id: Optional[int] = None, - state: BusState = BusState.ACTIVE, + state: BusState = BusState.ERROR_ACTIVE, timing: Optional[Union[BitTiming, BitTimingFd]] = None, bitrate: int = 500000, receive_own_messages: bool = False, @@ -268,7 +268,7 @@ def __init__( self.check_api_version() - if state in [BusState.ACTIVE, BusState.PASSIVE]: + if state in [BusState.ERROR_ACTIVE, BusState.ERROR_PASSIVE]: self.state = state else: raise ValueError("BusState must be Active or Passive") @@ -699,12 +699,12 @@ def state(self, new_state): # declare here, which is called by __init__() self._state = new_state # pylint: disable=attribute-defined-outside-init - if new_state is BusState.ACTIVE: + if new_state is BusState.ERROR_ACTIVE: self.m_objPCANBasic.SetValue( self.m_PcanHandle, PCAN_LISTEN_ONLY, PCAN_PARAMETER_OFF ) - elif new_state is BusState.PASSIVE: + elif new_state is BusState.ERROR_PASSIVE: # When this mode is set, the CAN controller does not take part on active events (eg. transmit CAN messages) # but stays in a passive mode (CAN monitor), in which it can analyse the traffic on the CAN bus used by a # PCAN channel. See also the Philips Data Sheet "SJA1000 Stand-alone CAN controller". diff --git a/can/interfaces/systec/ucanbus.py b/can/interfaces/systec/ucanbus.py index 00f101e4e..d279f4a4b 100644 --- a/can/interfaces/systec/ucanbus.py +++ b/can/interfaces/systec/ucanbus.py @@ -120,8 +120,8 @@ def __init__(self, channel, can_filters=None, **kwargs): if bitrate not in self.BITRATES: raise ValueError(f"Invalid bitrate {bitrate}") - state = kwargs.get("state", BusState.ACTIVE) - if state is BusState.ACTIVE or state is BusState.PASSIVE: + state = kwargs.get("state", BusState.ERROR_ACTIVE) + if state is BusState.ERROR_ACTIVE or state is BusState.ERROR_PASSIVE: self._state = state else: raise ValueError("BusState must be Active or Passive") @@ -130,7 +130,7 @@ def __init__(self, channel, can_filters=None, **kwargs): self._params = { "mode": Mode.MODE_NORMAL | (Mode.MODE_TX_ECHO if kwargs.get("receive_own_messages") else 0) - | (Mode.MODE_LISTEN_ONLY if state is BusState.PASSIVE else 0), + | (Mode.MODE_LISTEN_ONLY if state is BusState.ERROR_PASSIVE else 0), "BTR": self.BITRATES[bitrate], } # get extra parameters @@ -306,14 +306,14 @@ def state(self): @state.setter def state(self, new_state): - if self._state is not BusState.ERROR and ( - new_state is BusState.ACTIVE or new_state is BusState.PASSIVE + if self._state is not BusState.STOPPED and ( + new_state is BusState.ERROR_ACTIVE or new_state is BusState.ERROR_PASSIVE ): try: # close the CAN channel self._ucan.shutdown(self.channel, False) # set mode - if new_state is BusState.ACTIVE: + if new_state is BusState.ERROR_ACTIVE: self._params["mode"] &= ~Mode.MODE_LISTEN_ONLY else: self._params["mode"] |= Mode.MODE_LISTEN_ONLY diff --git a/can/logger.py b/can/logger.py index 4167558d8..e1d2358d7 100644 --- a/can/logger.py +++ b/can/logger.py @@ -281,9 +281,9 @@ def main() -> None: bus = _create_bus(results, **additional_config) if results.active: - bus.state = BusState.ACTIVE + bus.state = BusState.ERROR_ACTIVE elif results.passive: - bus.state = BusState.PASSIVE + bus.state = BusState.ERROR_PASSIVE print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}") print(f"Can Logger (Started on {datetime.now()})") diff --git a/doc/interfaces/pcan.rst b/doc/interfaces/pcan.rst index 2f73dd3a7..de9a8d864 100644 --- a/doc/interfaces/pcan.rst +++ b/doc/interfaces/pcan.rst @@ -15,7 +15,7 @@ Here is an example configuration file for using `PCAN-USB None: @parameterized.expand( [ - ("active", BusState.ACTIVE, PCAN_PARAMETER_OFF), - ("passive", BusState.PASSIVE, PCAN_PARAMETER_ON), + ("active", BusState.ERROR_ACTIVE, PCAN_PARAMETER_OFF), + ("passive", BusState.ERROR_PASSIVE, PCAN_PARAMETER_ON), ] ) def test_state(self, name, bus_state: BusState, expected_parameter) -> None: @@ -384,7 +384,7 @@ def test_state(self, name, bus_state: BusState, expected_parameter) -> None: ) def test_state_constructor(self): - for state in [BusState.ACTIVE, BusState.PASSIVE]: + for state in [BusState.ERROR_ACTIVE, BusState.ERROR_PASSIVE]: bus = can.Bus(interface="pcan", state=state) assert bus.state == state diff --git a/test/test_systec.py b/test/test_systec.py index 86ed31362..3bc19988b 100644 --- a/test/test_systec.py +++ b/test/test_systec.py @@ -218,7 +218,7 @@ def test_receive_own_messages(): @staticmethod def test_bus_passive_state(): ucan.UcanInitCanEx2.reset_mock() - bus = can.Bus(interface="systec", channel=0, state=can.BusState.PASSIVE) + bus = can.Bus(interface="systec", channel=0, state=can.BusState.ERROR_PASSIVE) ucan.UcanInitCanEx2.assert_called_once_with( bus._ucan._handle, 0,