nusb/examples/detach_claim.rs
Kevin Mehall 14a8f171e5
Make blocking APIs optionally async (#100)
* Make blocking APIs optionally async

`DeviceInfo::open`, `Device::from_fd`, `Device::set_configuration`,
`Device::reset`, `Interface::set_alt_setting`, `Interface::clear_halt`
all perform IO but are currently blocking because the underlying OS APIs
are blocking.

`list_devices`,`list_buses`, `Device::claim_interface`
`Device::detach_and_claim_interface` theoretically don't perform IO, but
are also included here because they need to be async on WebUSB.

The `MaybeFuture` trait allows deferring these actions to the thread pool
from the `blocking` crate when used asynchronously with `.await` /
`IntoFuture`, or directly runs the blocking syscall synchronously with a
`.wait()` method.
2025-02-11 18:53:07 -07:00

18 lines
565 B
Rust

//! Detach the kernel driver for an FTDI device, claim the USB interface, and
//! then reattach it.
use std::{thread::sleep, time::Duration};
use nusb::MaybeFuture;
fn main() {
env_logger::init();
let di = nusb::list_devices()
.wait()
.unwrap()
.find(|d| d.vendor_id() == 0x0403 && d.product_id() == 0x6010)
.expect("device should be connected");
let device = di.open().wait().unwrap();
let interface = device.detach_and_claim_interface(0).wait().unwrap();
sleep(Duration::from_secs(1));
drop(interface);
}