Skip to content

Commit 57b0112

Browse files
committed
Add some checks that the firmware looks like what we expect
1 parent 42f9003 commit 57b0112

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

canalystii/device.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import ctypes
2+
import logging
23
import usb.core
34
import time
45

56
from . import protocol
67

8+
logger = logging.getLogger(__name__)
9+
710
# "Fast" lookups to go from channel to USB endpoint number
811
CHANNEL_TO_COMMAND_EP = [2, 4] # Command EP for channels 0,1
912
CHANNEL_TO_MESSAGE_EP = [1, 3] # CAN Message EP for channels 0, 1
@@ -37,13 +40,29 @@ def __init__(
3740
self._dev = devices[device_index]
3841
self._dev.set_configuration(1)
3942

43+
# Check this looks like the firmware we expect: as this is an unofficial driver,
44+
# we don't know if other versions might are out there.
45+
if self._dev.product != "Chuangxin Tech USBCAN/CANalyst-II":
46+
logger.warning(
47+
f"Unexpected USB product string: {self._dev.product}. Firmware version may be unsupported."
48+
)
49+
interfaces = self._dev.get_active_configuration().interfaces()
50+
if len(interfaces) != 1:
51+
logger.warning(
52+
f"Unexpected interface count {len(interfaces)}. Firmware version may be unsupported."
53+
)
54+
endpoints = interfaces[0].endpoints()
55+
# For whatever reason FW has 6 bidirectional BULK endpoints!
56+
if len(endpoints) != 12:
57+
logger.warning(
58+
f"Unexpected endpoint count {len(endpoints)}. Firmware version mayb e unsupported."
59+
)
60+
4061
if bitrate is not None or timing0 is not None:
4162
# if not specified, don't initialize yet
4263
self.init(0, bitrate, timing0, timing1)
4364
self.init(1, bitrate, timing0, timing1)
4465

45-
# TODO: check _dev properties match what we expect
46-
4766
def clear_rx_buffer(self, channel):
4867
"""Clears the device's receive buffer for the specified channel.
4968

0 commit comments

Comments
 (0)