Skip to content
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

Reading individual USB packets. #250

Open
elpiel opened this issue Mar 9, 2025 · 4 comments
Open

Reading individual USB packets. #250

elpiel opened this issue Mar 9, 2025 · 4 comments

Comments

@elpiel
Copy link

elpiel commented Mar 9, 2025

USB data packets are usually up to 64 bytes and I would like to be able to read those packets instead of reading just raw bytes when they come (Read).

This could greatly improve building protocols for embedded firmware as you can use the same exact encoding on the device side and on the host side.

@sirhcel
Copy link
Contributor

sirhcel commented Mar 9, 2025

This is definitely something useful when it comes to communicating with embedded devices. Could you flesh out your idea and the goal bit more?

Maybe there are already tailored solution to this readily available: For example postcard-rpc which supports raw USB transport and could be even used on top of existing USB serial protocols without interfering directly with the latter. And it allows to share the exact same protocol/interface definition code between the devices communicating.

The serialport library operates on top of the device drivers which abstract the USB communication with the actual device away. Directly speaking to a USB serial device from the serialport crate is effectively incorporating a userspace driver for a certain USB serial protocol into the crate.

@elpiel
Copy link
Author

elpiel commented Mar 9, 2025

My idea was simple: Instead of providing only Read/Write to be able to read a USB frame through methods.
I took a look at postcard-rpc, at least on the host side, it does a buffering loop to make sure and read bytes but process 0, 1 or more messages.
I need to read a bit more and understand the rpc part of it though. Underneath it uses nusb which is low-level usb crate I've taken a look at.

@sirhcel
Copy link
Contributor

sirhcel commented Mar 9, 2025

My idea was simple: Instead of providing only Read/Write to be able to read a USB frame through methods.

Do you know more about how to achieve this? I'm asking because I don't know of an interface where serialport could get/inspect the USB traffic from a USB serial device. The device driver abstracts this completely away an just exposes data and device control though the serial device serialport-rs operates on.

@elpiel
Copy link
Author

elpiel commented Mar 10, 2025

As far as I saw, it can't. I thought that serialport uses something like nusb to operate on the usb device and not the OS io.

I took a look at postcard-rpc and indeed there, at least for the cobs encoder/decoder, the crate itself allows you to find the frames' ends and decode each (see https://docs.rs/cobs/latest/cobs/fn.decode_in_place.html). I'm still looking around to get a general feeling this can be achieved (in serialport or not).
nusb has methods for bulk in/out but it also has a much lower-level api and I need to get a bit more familiar with the usb protocol.

I'm open to ideas too.

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

No branches or pull requests

2 participants