Skip to main content

Device

Struct Device 

Source
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

Source

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.

Source

pub fn claim_interface(&self, interface: u8) -> impl MaybeFuture

Open an interface of the device and claim it for exclusive use.

Source

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.

Source

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.

Source

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.

Source

pub fn device_descriptor(&self) -> DeviceDescriptor

Get the device descriptor.

This returns cached data and does not perform IO.

Source

pub fn speed(&self) -> Option<Speed>

Get the device’s connection speed.

Source

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.

Source

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.

Source

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
Source

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.
Source

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.

Source

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.

Source

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
Source

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.
Source

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.

Trait Implementations§

Source§

impl Clone for Device

Source§

fn clone(&self) -> Device

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Device

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more