Skip to content

usb2can-libusb driver for native 8devices CAN on OSX and other platforms #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

bri3d
Copy link

@bri3d bri3d commented Feb 12, 2021

This is a very primitive/basic pyusb based native driver for the 8Devices USB2CAN hardware. The current 8Devices USB2CAN driver uses their "Canal" abstraction layer and is Windows-only.

I tested this driver with a "Korlan USB2CAN" I purchased a few days ago, with firmware "2.3." My test harness is an M1 MacBook Air (OS X) performing ISO-TP communication (P2=0.05s) with an ECU CAN bus running at 500kbaud that spams around 300 messages/second, so the driver has been pretty well stress-tested and does seem viable for practical applications. Besides serial CAN, I think this is the first fully cross-platform (Linux, Windows, MacOS) driver, which is nice.

Unfortunately the 8Devices USB2CAN hardware is not well documented at all. So, this driver is based on the 8Devices Linux SocketCAN kernel module (https://github.com/8devices/usb2can/blob/master/usb_8dev.c) which, while it lives in the 8Devices GitHub organization, itself seems to have been based on a combination of private-thread email lore and reverse engineering.

I hope these tests are adequate. I tried to fence the USB part well enough to not require PyUSB for the test suite or on install.

The device does support a loopback mode that could be used to produce a hardware-in-the-loop integration test for owners of the hardware, although I didn't really see precedent for one in the codebase.

One thing I was uncertain of is the desired pattern for handling so-called "error frames." I just pass through the native device "error frames" as error frames directly to Python-CAN for now, which is what other devices seem to do as well.

@mergify mergify bot requested a review from hardbyte February 12, 2021 19:20
@codecov
Copy link

codecov bot commented Feb 12, 2021

Codecov Report

Merging #979 (394e7fb) into develop (71580d8) will decrease coverage by 0.24%.
The diff coverage is 63.50%.

@@             Coverage Diff             @@
##           develop     #979      +/-   ##
===========================================
- Coverage    70.49%   70.24%   -0.25%     
===========================================
  Files           79       83       +4     
  Lines         7679     7964     +285     
===========================================
+ Hits          5413     5594     +181     
- Misses        2266     2370     +104     

@bri3d bri3d marked this pull request as draft February 12, 2021 19:31
@bri3d
Copy link
Author

bri3d commented Feb 12, 2021

Working on lint and docs as well of course :)

@bri3d bri3d marked this pull request as ready for review February 12, 2021 22:28
Copy link
Owner

@hardbyte hardbyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good effort!

INSTALL
_______

Install `pyusb` and a working pyusb backend (on most platforms, this is `libusb1`).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add pyusb to the extras in setup.py

Comment on lines +41 to +44
raise ValueError(
"8Devices CAN interface not found! Serial number provided: %s"
% serial_number
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries with raising a value error here, but I'd like to give a backend specific exception to the user.

See #1046 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By now, a CanInitializationError seems more appropriate.

or self.data_rx_ep is None
or self.data_tx_ep is None
):
raise ValueError("Could not configure 8Devices CAN endpoints!")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By now, a CanInitializationError seems more appropriate.

@hartkopp
Copy link
Collaborator

Unfortunately the 8Devices USB2CAN hardware is not well documented at all. So, this driver is based on the 8Devices Linux SocketCAN kernel module (https://github.com/8devices/usb2can/blob/master/usb_8dev.c) which, while it lives in the 8Devices GitHub organization, itself seems to have been based on a combination of private-thread email lore and reverse engineering.

The USB2CAN from 8 devices https://www.8devices.com/products/usb2can originally has been built by Bernd Krumboeck here:
https://github.com/krumboeck/usb2can and I recently made some updates for a out-of-tree compilation on an Linux 5.5 RasPi https://github.com/hartkopp/usb2can.

But all these out-of-tree drivers are not really well maintained so I would suggest to look into the latest mainline Linux driver here:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/can/usb/usb_8dev.c

And you should make sure that the device has the latest v1.7 firmware as you might get problems when restarting the host with a powered USB-Hub otherwise.

@bri3d
Copy link
Author

bri3d commented Jul 19, 2021

I checked the kernel in-tree driver, thanks for the link - it is a lot cleaner than the one in the 8devices repository. The overall message structure of course looks the same, but there are a few more error masks I may be able to use for some more specific backend errors (to the PR comments). So thank you for that! Unfortunately several features which the device seems to support via CANAL on Windows are still not documented/supported in any open source way- namely and particularly, message filtering, so that's a bit of a downer.

That tangent aside, I will address the PR comments later this week. All seems good to address and perfectly reasonable to me - I see that after I asked about more specialized CAN errors, they were indeed added, so happy to support those. And I totally missed the setup.py extras!

@hardbyte hardbyte requested review from hardbyte and removed request for hardbyte September 25, 2021 21:03
@felixdivo
Copy link
Collaborator

Ping

As a note: Before merging this, the new exceptions should be used (see here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants