Struct nusb::Device

source ·
pub struct Device { /* private fields */ }
Expand description

An opened USB device.

Obtain a Device by calling DeviceInfo::open:

use nusb;
let device_info = nusb::list_devices().unwrap()
    .find(|dev| dev.vendor_id() == 0xAAAA && dev.product_id() == 0xBBBB)
    .expect("device not connected");

let device = device_info.open().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 claim_interface(&self, interface: u8) -> Result<Interface, Error>

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

source

pub fn detach_and_claim_interface( &self, interface: u8 ) -> Result<Interface, Error>

Detach kernel drivers and open an interface of the device and claim it for exclusive use.

§Platform notes

This function can only detach kernel drivers on Linux. Calling on other platforms has the same effect as claim_interface.

source

pub fn active_configuration( &self ) -> Result<Configuration<'_>, 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 = Configuration<'_>>

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) -> Result<(), Error>

Set the device configuration.

The argument is the desired configuration’s bConfigurationValue descriptor field from Configuration::configuration_value or 0 to unconfigure the device.

§Platform-specific notes
  • Not supported on Windows
source

pub fn get_descriptor( &self, desc_type: u8, desc_index: u8, language_id: u16, timeout: Duration ) -> Result<Vec<u8>, Error>

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 ) -> Result<impl Iterator<Item = u16>, Error>

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: u8, language_id: u16, timeout: Duration ) -> Result<String, Error>

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) -> Result<(), Error>

Reset the device, forcing it to re-enumerate.

This Device will no longer be usable, and you should drop it and call super::list_devices to find and re-open it again.

§Platform-specific notes
  • Not supported on Windows

Trait Implementations§

source§

impl Clone for Device

source§

fn clone(&self) -> Device

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl Send for Device

§

impl Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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,

§

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>,

§

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>,

§

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.