| 1 | #[cfg(target_os = "windows")] |
| 2 | use std::ffi::{OsStr, OsString}; |
| 3 | |
| 4 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 5 | use crate::platform::SysfsPath; |
| 6 | |
| 7 | use crate::{Device, Error}; |
| 8 | |
| 9 | /// Opaque device identifier |
| 10 | #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] |
| 11 | pub struct DeviceId(pub(crate) crate::platform::DeviceId); |
| 12 | |
| 13 | /// Information about a device that can be obtained without opening it. |
| 14 | /// |
| 15 | /// Found in the results of [`crate::list_devices`]. |
| 16 | /// |
| 17 | /// ### Platform-specific notes |
| 18 | /// |
| 19 | /// * Some fields are platform-specific |
| 20 | /// * Linux: `sysfs_path` |
| 21 | /// * Windows: `instance_id`, `parent_instance_id`, `port_number`, `driver` |
| 22 | /// * macOS: `registry_id`, `location_id` |
| 23 | #[derive(Clone)] |
| 24 | pub struct DeviceInfo { |
| 25 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 26 | pub(crate) path: SysfsPath, |
| 27 | |
| 28 | #[cfg(target_os = "windows")] |
| 29 | pub(crate) instance_id: OsString, |
| 30 | |
| 31 | #[cfg(target_os = "windows")] |
| 32 | pub(crate) parent_instance_id: OsString, |
| 33 | |
| 34 | #[cfg(target_os = "windows")] |
| 35 | pub(crate) port_number: u32, |
| 36 | |
| 37 | #[cfg(target_os = "windows")] |
| 38 | pub(crate) devinst: crate::platform::DevInst, |
| 39 | |
| 40 | #[cfg(target_os = "windows")] |
| 41 | pub(crate) driver: Option<String>, |
| 42 | |
| 43 | #[cfg(target_os = "macos")] |
| 44 | pub(crate) registry_id: u64, |
| 45 | |
| 46 | #[cfg(target_os = "macos")] |
| 47 | pub(crate) location_id: u32, |
| 48 | |
| 49 | pub(crate) bus_number: u8, |
| 50 | pub(crate) device_address: u8, |
| 51 | |
| 52 | pub(crate) vendor_id: u16, |
| 53 | pub(crate) product_id: u16, |
| 54 | pub(crate) device_version: u16, |
| 55 | |
| 56 | pub(crate) class: u8, |
| 57 | pub(crate) subclass: u8, |
| 58 | pub(crate) protocol: u8, |
| 59 | |
| 60 | pub(crate) speed: Option<Speed>, |
| 61 | |
| 62 | pub(crate) manufacturer_string: Option<String>, |
| 63 | pub(crate) product_string: Option<String>, |
| 64 | pub(crate) serial_number: Option<String>, |
| 65 | |
| 66 | pub(crate) interfaces: Vec<InterfaceInfo>, |
| 67 | } |
| 68 | |
| 69 | impl DeviceInfo { |
| 70 | /// Opaque identifier for the device. |
| 71 | pub fn id(&self) -> DeviceId { |
| 72 | #[cfg(target_os = "windows")] |
| 73 | { |
| 74 | DeviceId(self.devinst) |
| 75 | } |
| 76 | |
| 77 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 78 | { |
| 79 | DeviceId(crate::platform::DeviceId { |
| 80 | bus: self.bus_number, |
| 81 | addr: self.device_address, |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | #[cfg(target_os = "macos")] |
| 86 | { |
| 87 | DeviceId(self.registry_id) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// *(Linux-only)* Sysfs path for the device. |
| 92 | #[doc(hidden)] |
| 93 | #[deprecated = "use `sysfs_path()` instead"] |
| 94 | #[cfg(target_os = "linux")] |
| 95 | pub fn path(&self) -> &SysfsPath { |
| 96 | &self.path |
| 97 | } |
| 98 | |
| 99 | /// *(Linux-only)* Sysfs path for the device. |
| 100 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 101 | pub fn sysfs_path(&self) -> &std::path::Path { |
| 102 | &self.path.0 |
| 103 | } |
| 104 | |
| 105 | /// *(Windows-only)* Instance ID path of this device |
| 106 | #[cfg(target_os = "windows")] |
| 107 | pub fn instance_id(&self) -> &OsStr { |
| 108 | &self.instance_id |
| 109 | } |
| 110 | |
| 111 | /// *(Windows-only)* Instance ID path of the parent hub |
| 112 | #[cfg(target_os = "windows")] |
| 113 | pub fn parent_instance_id(&self) -> &OsStr { |
| 114 | &self.parent_instance_id |
| 115 | } |
| 116 | |
| 117 | /// *(Windows-only)* Port number |
| 118 | #[cfg(target_os = "windows")] |
| 119 | pub fn port_number(&self) -> u32 { |
| 120 | self.port_number |
| 121 | } |
| 122 | |
| 123 | /// *(Windows-only)* Driver associated with the device as a whole |
| 124 | #[cfg(target_os = "windows")] |
| 125 | pub fn driver(&self) -> Option<&str> { |
| 126 | self.driver.as_deref() |
| 127 | } |
| 128 | |
| 129 | /// *(macOS-only)* IOKit Location ID |
| 130 | #[cfg(target_os = "macos")] |
| 131 | pub fn location_id(&self) -> u32 { |
| 132 | self.location_id |
| 133 | } |
| 134 | |
| 135 | /// *(macOS-only)* IOKit [Registry Entry ID](https://developer.apple.com/documentation/iokit/1514719-ioregistryentrygetregistryentryi?language=objc) |
| 136 | #[cfg(target_os = "macos")] |
| 137 | pub fn registry_entry_id(&self) -> u64 { |
| 138 | self.registry_id |
| 139 | } |
| 140 | |
| 141 | /// Number identifying the bus / host controller where the device is connected. |
| 142 | pub fn bus_number(&self) -> u8 { |
| 143 | self.bus_number |
| 144 | } |
| 145 | |
| 146 | /// Number identifying the device within the bus. |
| 147 | pub fn device_address(&self) -> u8 { |
| 148 | self.device_address |
| 149 | } |
| 150 | |
| 151 | /// The 16-bit number identifying the device's vendor, from the `idVendor` device descriptor field. |
| 152 | #[doc(alias = "idVendor")] |
| 153 | pub fn vendor_id(&self) -> u16 { |
| 154 | self.vendor_id |
| 155 | } |
| 156 | |
| 157 | /// The 16-bit number identifying the product, from the `idProduct` device descriptor field. |
| 158 | #[doc(alias = "idProduct")] |
| 159 | pub fn product_id(&self) -> u16 { |
| 160 | self.product_id |
| 161 | } |
| 162 | |
| 163 | /// The device version, normally encoded as BCD, from the `bcdDevice` device descriptor field. |
| 164 | #[doc(alias = "bcdDevice")] |
| 165 | pub fn device_version(&self) -> u16 { |
| 166 | self.device_version |
| 167 | } |
| 168 | |
| 169 | /// Code identifying the standard device class, from the `bDeviceClass` device descriptor field. |
| 170 | /// |
| 171 | /// `0x00`: specified at the interface level.\ |
| 172 | /// `0xFF`: vendor-defined. |
| 173 | #[doc(alias = "bDeviceClass")] |
| 174 | pub fn class(&self) -> u8 { |
| 175 | self.class |
| 176 | } |
| 177 | |
| 178 | /// Standard subclass, from the `bDeviceSubClass` device descriptor field. |
| 179 | #[doc(alias = "bDeviceSubClass")] |
| 180 | pub fn subclass(&self) -> u8 { |
| 181 | self.subclass |
| 182 | } |
| 183 | |
| 184 | /// Standard protocol, from the `bDeviceProtocol` device descriptor field. |
| 185 | #[doc(alias = "bDeviceProtocol")] |
| 186 | pub fn protocol(&self) -> u8 { |
| 187 | self.protocol |
| 188 | } |
| 189 | |
| 190 | /// Connection speed |
| 191 | pub fn speed(&self) -> Option<Speed> { |
| 192 | self.speed |
| 193 | } |
| 194 | |
| 195 | /// Manufacturer string, if available without device IO. |
| 196 | /// |
| 197 | /// ### Platform-specific notes |
| 198 | /// * Windows: Windows does not cache the manufacturer string, and |
| 199 | /// this will return `None` regardless of whether a descriptor exists. |
| 200 | #[doc(alias = "iManufacturer")] |
| 201 | pub fn manufacturer_string(&self) -> Option<&str> { |
| 202 | self.manufacturer_string.as_deref() |
| 203 | } |
| 204 | |
| 205 | /// Product string, if available without device IO. |
| 206 | #[doc(alias = "iProduct")] |
| 207 | pub fn product_string(&self) -> Option<&str> { |
| 208 | self.product_string.as_deref() |
| 209 | } |
| 210 | |
| 211 | /// Serial number string, if available without device IO. |
| 212 | #[doc(alias = "iSerial")] |
| 213 | pub fn serial_number(&self) -> Option<&str> { |
| 214 | self.serial_number.as_deref() |
| 215 | } |
| 216 | |
| 217 | /// Iterator over the device's interfaces. |
| 218 | /// |
| 219 | /// This returns summary information about the interfaces in the device's |
| 220 | /// active configuration for the purposes of matching devices prior to |
| 221 | /// opening them. |
| 222 | /// |
| 223 | /// Additional information about interfaces can be found in the |
| 224 | /// configuration descriptor after opening the device by calling |
| 225 | /// [`Device::active_configuration`]. |
| 226 | /// |
| 227 | /// ### Platform-specific notes: |
| 228 | /// * Windows: this is only available for composite devices bound to the |
| 229 | /// `usbccgp` driver, and will be empty if the entire device is bound to |
| 230 | /// a specific driver. |
| 231 | /// * Windows: When interfaces are grouped by an interface |
| 232 | /// association descriptor, this returns details from the interface |
| 233 | /// association descriptor and does not include each of the associated |
| 234 | /// interfaces. |
| 235 | pub fn interfaces(&self) -> impl Iterator<Item = &InterfaceInfo> { |
| 236 | self.interfaces.iter() |
| 237 | } |
| 238 | |
| 239 | /// Open the device |
| 240 | pub fn open(&self) -> Result<Device, Error> { |
| 241 | Device::open(self) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Not derived so that we can format some fields in hex |
| 246 | impl std::fmt::Debug for DeviceInfo { |
| 247 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 248 | let mut s = f.debug_struct("DeviceInfo"); |
| 249 | |
| 250 | s.field("bus_number", &self.bus_number) |
| 251 | .field("device_address", &self.device_address) |
| 252 | .field("vendor_id", &format_args!("0x{:04X}", self.vendor_id)) |
| 253 | .field("product_id", &format_args!("0x{:04X}", self.product_id)) |
| 254 | .field( |
| 255 | "device_version", |
| 256 | &format_args!("0x{:04X}", self.device_version), |
| 257 | ) |
| 258 | .field("class", &format_args!("0x{:02X}", self.class)) |
| 259 | .field("subclass", &format_args!("0x{:02X}", self.subclass)) |
| 260 | .field("protocol", &format_args!("0x{:02X}", self.protocol)) |
| 261 | .field("speed", &self.speed) |
| 262 | .field("manufacturer_string", &self.manufacturer_string) |
| 263 | .field("product_string", &self.product_string) |
| 264 | .field("serial_number", &self.serial_number); |
| 265 | |
| 266 | #[cfg(any(target_os = "linux", target_os = "android"))] |
| 267 | { |
| 268 | s.field("sysfs_path", &self.path); |
| 269 | } |
| 270 | |
| 271 | #[cfg(target_os = "windows")] |
| 272 | { |
| 273 | s.field("instance_id", &self.instance_id); |
| 274 | s.field("parent_instance_id", &self.parent_instance_id); |
| 275 | s.field("port_number", &self.port_number); |
| 276 | s.field("driver", &self.driver); |
| 277 | } |
| 278 | |
| 279 | #[cfg(target_os = "macos")] |
| 280 | { |
| 281 | s.field("location_id", &format_args!("0x{:08X}", self.location_id)); |
| 282 | s.field( |
| 283 | "registry_entry_id", |
| 284 | &format_args!("0x{:08X}", self.registry_id), |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | s.field("interfaces", &self.interfaces); |
| 289 | |
| 290 | s.finish() |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /// USB connection speed |
| 295 | #[derive(Copy, Clone, Eq, PartialOrd, Ord, PartialEq, Hash, Debug)] |
| 296 | #[non_exhaustive] |
| 297 | pub enum Speed { |
| 298 | /// Low speed (1.5 Mbit) |
| 299 | Low, |
| 300 | |
| 301 | /// Full speed (12 Mbit) |
| 302 | Full, |
| 303 | |
| 304 | /// High speed (480 Mbit) |
| 305 | High, |
| 306 | |
| 307 | /// Super speed (5000 Mbit) |
| 308 | Super, |
| 309 | |
| 310 | /// Super speed (10000 Mbit) |
| 311 | SuperPlus, |
| 312 | } |
| 313 | |
| 314 | impl Speed { |
| 315 | #[allow(dead_code)] // not used on all platforms |
| 316 | pub(crate) fn from_str(s: &str) -> Option<Self> { |
| 317 | match s { |
| 318 | "low" | "1.5" => Some(Speed::Low), |
| 319 | "full" | "12" => Some(Speed::Full), |
| 320 | "high" | "480" => Some(Speed::High), |
| 321 | "super" | "5000" => Some(Speed::Super), |
| 322 | "super+" | "10000" => Some(Speed::SuperPlus), |
| 323 | _ => None, |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /// Summary information about a device's interface, available before opening a device. |
| 329 | #[derive(Clone)] |
| 330 | pub struct InterfaceInfo { |
| 331 | pub(crate) interface_number: u8, |
| 332 | pub(crate) class: u8, |
| 333 | pub(crate) subclass: u8, |
| 334 | pub(crate) protocol: u8, |
| 335 | pub(crate) interface_string: Option<String>, |
| 336 | } |
| 337 | |
| 338 | impl InterfaceInfo { |
| 339 | /// Identifier for the interface from the `bInterfaceNumber` descriptor field. |
| 340 | pub fn interface_number(&self) -> u8 { |
| 341 | self.interface_number |
| 342 | } |
| 343 | |
| 344 | /// Code identifying the standard interface class, from the `bInterfaceClass` interface descriptor field. |
| 345 | pub fn class(&self) -> u8 { |
| 346 | self.class |
| 347 | } |
| 348 | |
| 349 | /// Standard subclass, from the `bInterfaceSubClass` interface descriptor field. |
| 350 | pub fn subclass(&self) -> u8 { |
| 351 | self.subclass |
| 352 | } |
| 353 | |
| 354 | /// Standard protocol, from the `bInterfaceProtocol` interface descriptor field. |
| 355 | pub fn protocol(&self) -> u8 { |
| 356 | self.protocol |
| 357 | } |
| 358 | |
| 359 | /// Interface string descriptor value as cached by the OS. |
| 360 | pub fn interface_string(&self) -> Option<&str> { |
| 361 | self.interface_string.as_deref() |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Not derived so that we can format some fields in hex |
| 366 | impl std::fmt::Debug for InterfaceInfo { |
| 367 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 368 | f.debug_struct("InterfaceInfo") |
| 369 | .field("interface_number", &self.interface_number) |
| 370 | .field("class", &format_args!("0x{:02X}", self.class)) |
| 371 | .field("subclass", &format_args!("0x{:02X}", self.subclass)) |
| 372 | .field("protocol", &format_args!("0x{:02X}", self.protocol)) |
| 373 | .field("interface_string", &self.interface_string) |
| 374 | .finish() |
| 375 | } |
| 376 | } |