Skip to main content

viva_u3v/
discovery.rs

1//! USB3 Vision device enumeration.
2//!
3//! Lists the USB devices with a USB3 Vision interface, from what the
4//! operating system already knows about them: no device is opened, so a
5//! scan neither disturbs nor depends on a camera another handle — this
6//! process's own, say — holds (backlog UV-04).
7
8#[cfg(feature = "usb")]
9use crate::U3vError;
10use crate::bootstrap::UsbSpeed;
11
12/// Information about a discovered USB3 Vision device.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct U3vDeviceInfo {
15    /// The USB bus — the host controller — as the operating system names it.
16    pub bus: String,
17    /// USB device address on the bus. It changes when the device is plugged
18    /// in again; the serial does not.
19    pub address: u8,
20    /// USB vendor ID.
21    pub vendor_id: u16,
22    /// USB product ID.
23    pub product_id: u16,
24    /// Serial number, from the USB string descriptor the operating system
25    /// caches; `None` when the device reports none.
26    pub serial: Option<String>,
27    /// Manufacturer name, likewise. Windows caches none.
28    pub manufacturer: Option<String>,
29    /// Product/model name, likewise.
30    pub model: Option<String>,
31    /// The speed the device enumerated at, when the operating system says.
32    pub speed: Option<UsbSpeed>,
33}
34
35impl std::fmt::Display for U3vDeviceInfo {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(
38            f,
39            "USB3V {:04x}:{:04x} bus={} addr={} {}",
40            self.vendor_id,
41            self.product_id,
42            self.bus,
43            self.address,
44            self.model.as_deref().unwrap_or("(unknown)"),
45        )
46    }
47}
48
49/// Enumerate all USB3 Vision devices currently connected to the system.
50///
51/// A device is one when an interface — or, on Windows, the interface
52/// association grouping them — has class `0xEF` and subclass `0x05`. Its
53/// strings come from the operating system's cache; a device is opened only
54/// when it is connected to, which warns of a camera below SuperSpeed.
55#[cfg(feature = "usb")]
56pub fn discover() -> Result<Vec<U3vDeviceInfo>, U3vError> {
57    use nusb::MaybeFuture;
58
59    let devices = nusb::list_devices()
60        .wait()
61        .map_err(U3vError::usb)?
62        .filter(is_u3v)
63        .map(|d| U3vDeviceInfo {
64            bus: d.bus_id().to_string(),
65            address: d.device_address(),
66            vendor_id: d.vendor_id(),
67            product_id: d.product_id(),
68            serial: d.serial_number().map(str::to_string),
69            manufacturer: d.manufacturer_string().map(str::to_string),
70            model: d.product_string().map(str::to_string),
71            speed: d.speed().map(crate::native::speed),
72        })
73        .collect();
74    Ok(devices)
75}
76
77/// Whether `device` has a USB3 Vision interface (class `0xEF`, subclass
78/// `0x05`). A device bound whole to one driver lists no interfaces on
79/// Windows; it counts when it declares the interface association triple
80/// (`0xEF`/`0x02`/`0x01`), and opening it settles whether it is one.
81#[cfg(feature = "usb")]
82fn is_u3v(device: &nusb::DeviceInfo) -> bool {
83    use crate::descriptor::{U3V_INTERFACE_CLASS, U3V_INTERFACE_SUBCLASS};
84    let mut interfaces = device.interfaces().peekable();
85    if interfaces.peek().is_none() {
86        return (device.class(), device.subclass(), device.protocol()) == (0xEF, 0x02, 0x01);
87    }
88    interfaces.any(|i| i.class() == U3V_INTERFACE_CLASS && i.subclass() == U3V_INTERFACE_SUBCLASS)
89}