-
Notifications
You must be signed in to change notification settings - Fork 94
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
refactor(serial): replace rusb with nusb #180
base: main
Are you sure you want to change the base?
Conversation
nusb is a pure Rust library providing the same low level access to USB devices that rusb/libusb provide. This commit removes rusb (and thus the dependence on libusb) and replaces it with nusb in the serial utility. The only functional change is that nusb does not support timeouts for bulk data commands. nusb is async. This commit contains a naïve implementation that simply blocks on bulk reads and writes in send_command().
if let Err(e) = handle.write_control(0x40, 0xa0, 0, 0, &[], timeout) { | ||
|
||
if let Some(interface) = open_device(0x05c6, 0xf626) { | ||
let enable_command_mode = Control { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these could always be inlined
Probably the best call for implementing timeouts is to just use https://docs.rs/tokio/latest/tokio/time/fn.timeout.html Runtime memory on the Orbic is the key constraint, not disk size, so there's probably no reason not to use a small part of tokio here. The linker won't be including ALL of tokio anyways. <-- note to myself to get out of that 90s headspace |
no objections at all to using tokio, and reminder that |
Replace futures_lite::future::block_on (which will block indefinitely) with tokio::time::timeout to restore the original behaviour of this utility, where communication over USB interface bulk endpoints times out after 1 second.
Mmm true that, more of that late night logic creeping in :) I've tested this branch with the Orbic using the serial binary built with I haven't tried the binary on Linux/arm64 or macOS/arm64 but I can later. |
tokio::time::timeout(timeout, interface.bulk_out(0x2, data.as_bytes().to_vec())) | ||
.await | ||
.expect("Timed out writing command") | ||
.into_result() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's other ways to style this statement (the double expect isn't gorgeous, nor is tokio::time::timeout(timeout, ...
), but I went for the most minimal changes instead of putting my design fingerprints all over everything.
|
||
if let Some(interface) = open_device(0x05c6, 0xf626) { | ||
let enable_command_mode = Control { | ||
control_type: ControlType::Vendor, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really appreciated how instead of just writing the bmRequestType as a magic integer like rusb, this library uses enums to make the intent clearer.
Description
nusb is a pure Rust library providing the same low level access to USB devices as rusb/libusb.
This branch removes rusb (and thus the dependence on libusb) and replaces it with nusb in the serial utility.
Notes
Addresses #176