pub struct Device { /* private fields */ }Expand description
An opened USB device.
Obtain a Device by calling DeviceInfo::open:
use nusb::{self, MaybeFuture};
let device_info = nusb::list_devices().wait().unwrap()
.find(|dev| dev.vendor_id() == 0xAAAA && dev.product_id() == 0xBBBB)
.expect("device not connected");
let device = device_info.open().wait().expect("failed to open device");
let interface = device.claim_interface(0);This type is reference-counted with an Arc internally, and can be cloned cheaply for
use in multiple places in your program. The device is closed when all clones and all
associated Interfaces are dropped.
Use .claim_interface(i) to open an interface to submit
transfers.
Implementations§
Source§impl Device
impl Device
Sourcepub fn from_fd(fd: OwnedFd) -> impl MaybeFuture
pub fn from_fd(fd: OwnedFd) -> impl MaybeFuture
Wrap a usbdevfs file descriptor that is already open.
This opens a device from a file descriptor for a /dev/bus/usb/* device
provided externally, such as from
Android,
xdg-desktop-portal,
etc.
Supported on Linux and Android only.
Sourcepub fn claim_interface(&self, interface: u8) -> impl MaybeFuture
pub fn claim_interface(&self, interface: u8) -> impl MaybeFuture
Open an interface of the device and claim it for exclusive use.
Sourcepub fn detach_and_claim_interface(&self, interface: u8) -> impl MaybeFuture
pub fn detach_and_claim_interface(&self, interface: u8) -> impl MaybeFuture
Detach kernel drivers and open an interface of the device and claim it for exclusive use.
§Platform-specific details
This function can only detach kernel drivers on Linux. Calling on other platforms has
the same effect as claim_interface.
Sourcepub fn detach_kernel_driver(&self, interface: u8) -> Result<(), Error>
pub fn detach_kernel_driver(&self, interface: u8) -> Result<(), Error>
Detach kernel drivers for the specified interface.
§Platform-specific details
This function can only detach kernel drivers on Linux. Calling on other platforms has no effect.
Sourcepub fn attach_kernel_driver(&self, interface: u8) -> Result<(), Error>
pub fn attach_kernel_driver(&self, interface: u8) -> Result<(), Error>
Attach kernel drivers for the specified interface.
§Platform-specific details
This function can only attach kernel drivers on Linux. Calling on other platforms has no effect.
Sourcepub fn device_descriptor(&self) -> DeviceDescriptor
pub fn device_descriptor(&self) -> DeviceDescriptor
Get the device descriptor.
This returns cached data and does not perform IO.
Sourcepub fn active_configuration(
&self,
) -> Result<ConfigurationDescriptor<'_>, ActiveConfigurationError>
pub fn active_configuration( &self, ) -> Result<ConfigurationDescriptor<'_>, ActiveConfigurationError>
Get information about the active configuration.
This returns cached data and does not perform IO. However, it can fail if the device is unconfigured, or if it can’t find a configuration descriptor for the configuration reported as active by the OS.
Sourcepub fn configurations(
&self,
) -> impl Iterator<Item = ConfigurationDescriptor<'_>>
pub fn configurations( &self, ) -> impl Iterator<Item = ConfigurationDescriptor<'_>>
Get an iterator returning information about each configuration of the device.
This returns cached data and does not perform IO.
Sourcepub fn set_configuration(&self, configuration: u8) -> impl MaybeFuture
pub fn set_configuration(&self, configuration: u8) -> impl MaybeFuture
Set the device configuration.
The argument is the desired configuration’s bConfigurationValue
descriptor field from ConfigurationDescriptor::configuration_value or 0 to
unconfigure the device.
§Platform-specific details
- Not supported on Windows
Sourcepub fn get_descriptor(
&self,
desc_type: u8,
desc_index: u8,
language_id: u16,
timeout: Duration,
) -> impl MaybeFuture
pub fn get_descriptor( &self, desc_type: u8, desc_index: u8, language_id: u16, timeout: Duration, ) -> impl MaybeFuture
Request a descriptor from the device.
The language_id should be 0 unless you are requesting a string descriptor.
§Platform-specific details
- On Windows, the timeout argument is ignored, and an OS-defined timeout is used.
- On Windows, this does not wake suspended devices. Reading their descriptors will return an error.
Sourcepub fn get_string_descriptor_supported_languages(
&self,
timeout: Duration,
) -> impl MaybeFuture
pub fn get_string_descriptor_supported_languages( &self, timeout: Duration, ) -> impl MaybeFuture
Request the list of supported languages for string descriptors.
§Platform-specific details
See notes on get_descriptor.
Sourcepub fn get_string_descriptor(
&self,
desc_index: NonZero<u8>,
language_id: u16,
timeout: Duration,
) -> impl MaybeFuture
pub fn get_string_descriptor( &self, desc_index: NonZero<u8>, language_id: u16, timeout: Duration, ) -> impl MaybeFuture
Request a string descriptor from the device.
Almost all devices support only the language ID US_ENGLISH.
Unpaired UTF-16 surrogates will be replaced with �, like String::from_utf16_lossy.
§Platform-specific details
See notes on get_descriptor.
Sourcepub fn reset(&self) -> impl MaybeFuture
pub fn reset(&self) -> impl MaybeFuture
Reset the device, forcing it to re-enumerate.
This Device will no longer be usable, and you should drop it and call
list_devices to find and re-open it again.
§Platform-specific details
- Not supported on Windows
Sourcepub fn control_in(&self, data: ControlIn, timeout: Duration) -> impl MaybeFuture
pub fn control_in(&self, data: ControlIn, timeout: Duration) -> impl MaybeFuture
Submit a single IN (device-to-host) transfer on the default control endpoint.
§Example
use std::time::Duration;
use futures_lite::future::block_on;
use nusb::transfer::{ ControlIn, ControlType, Recipient };
let data: Vec<u8> = device.control_in(ControlIn {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x30,
value: 0x0,
index: 0x0,
length: 64,
}, Duration::from_millis(100)).wait()?;§Platform-specific details
- Not supported on Windows. You must claim an interface and use the interface handle to submit transfers.
Sourcepub fn control_out(
&self,
data: ControlOut<'_>,
timeout: Duration,
) -> impl MaybeFuture
pub fn control_out( &self, data: ControlOut<'_>, timeout: Duration, ) -> impl MaybeFuture
Submit a single OUT (host-to-device) transfer on the default control endpoint.
§Example
use std::time::Duration;
use futures_lite::future::block_on;
use nusb::transfer::{ ControlOut, ControlType, Recipient };
device.control_out(ControlOut {
control_type: ControlType::Vendor,
recipient: Recipient::Device,
request: 0x32,
value: 0x0,
index: 0x0,
data: &[0x01, 0x02, 0x03, 0x04],
}, Duration::from_millis(100)).wait()?;§Platform-specific details
- Not supported on Windows. You must claim an interface and use the interface handle to submit transfers.