Skip to content

Commit 14cba14

Browse files
committed
Implement enum to represent CAN protocol
1 parent b2ec42c commit 14cba14

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

can/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .util import set_logging_level
2828

2929
from .message import Message
30-
from .bus import BusABC, BusState
30+
from .bus import BusABC, BusState, CANProtocol
3131
from .thread_safe_bus import ThreadSafeBus
3232
from .notifier import Notifier
3333
from .interfaces import VALID_INTERFACES

can/bus.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class BusState(Enum):
2727
ERROR = auto()
2828

2929

30+
class CANProtocol(Enum):
31+
"""The CAN protocol type supported by a :clas:`can.BusABC` instance"""
32+
33+
CAN_20 = auto()
34+
CAN_FD = auto()
35+
CAN_XL = auto()
36+
37+
3038
class BusABC(metaclass=ABCMeta):
3139
"""The CAN Bus Abstract Base Class that serves as the basis
3240
for all concrete interfaces.
@@ -48,7 +56,7 @@ class BusABC(metaclass=ABCMeta):
4856
def __init__(
4957
self,
5058
channel: Any,
51-
is_fd: bool = False,
59+
protocol: CANProtocol = CANProtocol.CAN_20,
5260
can_filters: Optional[can.typechecking.CanFilters] = None,
5361
**kwargs: object
5462
):
@@ -60,8 +68,12 @@ def __init__(
6068
:param channel:
6169
The can interface identifier. Expected type is backend dependent.
6270
63-
:param is_fd:
64-
Indicates that this bus supports CAN-FD.
71+
:param protocol:
72+
The CAN protocol currently used by this bus instance. This value
73+
is determined at initialization time (based on the initialization
74+
parameters or because the bus interface only supports a specific
75+
protocol) and does not change during the lifetime of a bus
76+
instance.
6577
6678
:param can_filters:
6779
See :meth:`~can.BusABC.set_filters` for details.
@@ -75,7 +87,7 @@ def __init__(
7587
:raises ~can.exceptions.CanInitializationError:
7688
If the bus cannot be initialized
7789
"""
78-
self._is_fd = is_fd
90+
self._can_protocol = protocol
7991
self._periodic_tasks: List[_SelfRemovingCyclicTask] = []
8092
self.set_filters(can_filters)
8193

@@ -448,8 +460,13 @@ def state(self, new_state: BusState) -> None:
448460
raise NotImplementedError("Property is not implemented.")
449461

450462
@property
451-
def is_fd(self) -> bool:
452-
return self._is_fd
463+
def protocol(self) -> CANProtocol:
464+
"""Return the CAN protocol used by this bus instance.
465+
466+
This value is set at initialization time and does not change
467+
during the lifetime of a bus instance.
468+
"""
469+
return self._can_protocol
453470

454471
@staticmethod
455472
def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]:

can/interfaces/pcan/pcan.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from can import (
1313
BusABC,
1414
BusState,
15+
CANProtocol,
1516
BitTiming,
1617
BitTimingFd,
1718
Message,
@@ -339,7 +340,11 @@ def __init__(
339340
raise PcanCanInitializationError(self._get_formatted_error(result))
340341

341342
super().__init__(
342-
channel=channel, is_fd=is_fd, state=state, bitrate=bitrate, **kwargs
343+
channel=channel,
344+
protocol=CANProtocol.CAN_FD if is_fd else CANProtocol.CAN_20,
345+
state=state,
346+
bitrate=bitrate,
347+
**kwargs,
343348
)
344349

345350
def _find_channel_by_dev_id(self, device_id):
@@ -486,7 +491,7 @@ def _recv_internal(
486491
end_time = time.time() + timeout if timeout is not None else None
487492

488493
while True:
489-
if self.is_fd:
494+
if self.protocol == CANProtocol.CAN_FD:
490495
result, pcan_msg, pcan_timestamp = self.m_objPCANBasic.ReadFD(
491496
self.m_PcanHandle
492497
)
@@ -548,7 +553,7 @@ def _recv_internal(
548553
error_state_indicator = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ESI.value)
549554
is_error_frame = bool(pcan_msg.MSGTYPE & PCAN_MESSAGE_ERRFRAME.value)
550555

551-
if self.is_fd:
556+
if self.protocol == CANProtocol.CAN_FD:
552557
dlc = dlc2len(pcan_msg.DLC)
553558
timestamp = boottimeEpoch + (pcan_timestamp.value / (1000.0 * 1000.0))
554559
else:
@@ -594,7 +599,7 @@ def send(self, msg, timeout=None):
594599
if msg.error_state_indicator:
595600
msgType |= PCAN_MESSAGE_ESI.value
596601

597-
if self.is_fd:
602+
if self.protocol == CANProtocol.CAN_FD:
598603
# create a TPCANMsg message structure
599604
CANMsg = TPCANMsgFD()
600605

test/test_pcan.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from parameterized import parameterized
1212

1313
import can
14-
from can.bus import BusState
14+
from can import BusState, CANProtocol
1515
from can.exceptions import CanInitializationError
1616
from can.interfaces.pcan import PcanBus, PcanError
1717
from can.interfaces.pcan.basic import *
@@ -53,7 +53,7 @@ def test_bus_creation(self) -> None:
5353
self.bus = can.Bus(interface="pcan")
5454

5555
self.assertIsInstance(self.bus, PcanBus)
56-
self.assertFalse(self.bus.is_fd)
56+
self.assertEqual(self.bus.protocol, CANProtocol.CAN_20)
5757

5858
self.MockPCANBasic.assert_called_once()
5959
self.mock_pcan.Initialize.assert_called_once()
@@ -81,7 +81,7 @@ def test_bus_creation_fd(self, clock_param: str, clock_val: int) -> None:
8181
)
8282

8383
self.assertIsInstance(self.bus, PcanBus)
84-
self.assertTrue(self.bus.is_fd)
84+
self.assertEqual(self.bus.protocol, CANProtocol.CAN_FD)
8585

8686
self.MockPCANBasic.assert_called_once()
8787
self.mock_pcan.Initialize.assert_not_called()
@@ -459,7 +459,7 @@ def test_constructor_bit_timing(self):
459459

460460
bitrate_arg = self.mock_pcan.Initialize.call_args[0][1]
461461
self.assertEqual(bitrate_arg.value, 0x472F)
462-
self.assertFalse(bus.is_fd)
462+
self.assertEqual(bus.protocol, CANProtocol.CAN_20)
463463

464464
def test_constructor_bit_timing_fd(self):
465465
timing = can.BitTimingFd(
@@ -474,7 +474,7 @@ def test_constructor_bit_timing_fd(self):
474474
data_sjw=1,
475475
)
476476
bus = can.Bus(interface="pcan", channel="PCAN_USBBUS1", timing=timing)
477-
self.assertTrue(bus.is_fd)
477+
self.assertEqual(bus.protocol, CANProtocol.CAN_FD)
478478

479479
bitrate_arg = self.mock_pcan.InitializeFD.call_args[0][-1]
480480

0 commit comments

Comments
 (0)