Trait rusb::UsbContext

source ·
pub trait UsbContext: Clone + Sized + Send + Sync {
    // Required method
    fn as_raw(&self) -> *mut libusb_context;

    // Provided methods
    fn devices(&self) -> Result<DeviceList<Self>> { ... }
    fn open_device_with_vid_pid(
        &self,
        vendor_id: u16,
        product_id: u16
    ) -> Option<DeviceHandle<Self>> { ... }
    unsafe fn open_device_with_fd(
        &self,
        fd: RawFd
    ) -> Result<DeviceHandle<Self>> { ... }
    fn set_log_level(&mut self, level: LogLevel) { ... }
    fn set_log_callback(
        &mut self,
        log_callback: Box<dyn Fn(LogLevel, String)>,
        mode: LogCallbackMode
    ) { ... }
    fn register_callback(
        &self,
        vendor_id: Option<u16>,
        product_id: Option<u16>,
        class: Option<u8>,
        callback: Box<dyn Hotplug<Self>>
    ) -> Result<Registration<Self>> { ... }
    fn unregister_callback(&self, _reg: Registration<Self>) { ... }
    fn handle_events(&self, timeout: Option<Duration>) -> Result<()> { ... }
    fn interrupt_handle_events(&self) { ... }
    fn next_timeout(&self) -> Result<Option<Duration>> { ... }
}

Required Methods§

source

fn as_raw(&self) -> *mut libusb_context

Get the raw libusb_context pointer, for advanced use in unsafe code.

Provided Methods§

source

fn devices(&self) -> Result<DeviceList<Self>>

Returns a list of the current USB devices.

source

fn open_device_with_vid_pid( &self, vendor_id: u16, product_id: u16 ) -> Option<DeviceHandle<Self>>

Convenience function to open a device by its vendor ID and product ID.

This function is provided as a convenience for building prototypes without having to iterate a DeviceList. It is not meant for production applications.

Returns a device handle for the first device found matching vendor_id and product_id. On error, or if the device could not be found, it returns None.

source

unsafe fn open_device_with_fd(&self, fd: RawFd) -> Result<DeviceHandle<Self>>

Opens the device with a pre-opened file descriptor.

This is UNIX-only and platform-specific. It is currently working with Linux/Android, but might work with other systems in the future.

Note: This function does not take ownership of the specified file descriptor. The caller has the responsibility of keeping it opened for as long as the device handle.

source

fn set_log_level(&mut self, level: LogLevel)

Sets the log level of a libusb for context.

source

fn set_log_callback( &mut self, log_callback: Box<dyn Fn(LogLevel, String)>, mode: LogCallbackMode )

source

fn register_callback( &self, vendor_id: Option<u16>, product_id: Option<u16>, class: Option<u8>, callback: Box<dyn Hotplug<Self>> ) -> Result<Registration<Self>>

👎Deprecated since 0.9.0: Use HotplugBuilder

Register a callback to be called on hotplug events. The callback’s Hotplug::device_arrived method is called when a new device is added to the bus, and Hotplug::device_left is called when it is removed.

Devices can optionally be filtered by vendor (vendor_id) and device id (product_id).

The callback will remain registered until the returned Registration is dropped, which can be done explicitly with Context::unregister_callback.

When handling a Hotplug::device_arrived event it is considered safe to call any rusb function that takes a crate::Device. It also safe to open a device and submit asynchronous transfers. However, most other functions that take a DeviceHandle are not safe to call. Examples of such functions are any of the synchronous API functions or the blocking functions that retrieve various USB descriptors. These functions must be used outside of the context of the Hotplug functions.

source

fn unregister_callback(&self, _reg: Registration<Self>)

Unregisters the callback corresponding to the given registration. The same thing can be achieved by dropping the registration.

source

fn handle_events(&self, timeout: Option<Duration>) -> Result<()>

Handle any pending events. If timeout less then 1 microseconds then this function will handle any already-pending events and then immediately return in non-blocking style. If timeout is None then function will handle any pending events in blocking mode.

source

fn interrupt_handle_events(&self)

Interrupt any active thread that is handling events (for example with handle_events).

source

fn next_timeout(&self) -> Result<Option<Duration>>

Object Safety§

This trait is not object safe.

Implementors§