| 1 | use std::fs; |
| 2 | use std::io; |
| 3 | use std::num::ParseIntError; |
| 4 | use std::path::PathBuf; |
| 5 | use std::str::FromStr; |
| 6 | |
| 7 | use log::debug; |
| 8 | |
| 9 | use crate::enumeration::InterfaceInfo; |
| 10 | use crate::DeviceInfo; |
| 11 | use crate::Error; |
| 12 | use crate::Speed; |
| 13 | |
| 14 | #[derive(Debug, Clone)] |
| 15 | pub struct SysfsPath(pub(crate) PathBuf); |
| 16 | |
| 17 | impl SysfsPath { |
| 18 | pub(crate) fn read_attr<T: FromStr>(&self, attr: &str) -> Result<T, io::Error> |
| 19 | where |
| 20 | T: FromStr, |
| 21 | T::Err: std::error::Error + Send + Sync + 'static, |
| 22 | { |
| 23 | let attr_path = self.0.join(attr); |
| 24 | let read_res = fs::read_to_string(&attr_path); |
| 25 | debug!("sysfs read {attr_path:?}: {read_res:?}"); |
| 26 | |
| 27 | read_res? |
| 28 | .trim() |
| 29 | .parse() |
| 30 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) |
| 31 | } |
| 32 | |
| 33 | fn read_attr_hex<T: FromHexStr>(&self, attr: &str) -> Result<T, io::Error> { |
| 34 | let s = self.read_attr::<String>(attr)?; |
| 35 | T::from_hex_str(&s) |
| 36 | .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid hex str")) |
| 37 | } |
| 38 | |
| 39 | fn children(&self) -> impl Iterator<Item = SysfsPath> { |
| 40 | fs::read_dir(&self.0) |
| 41 | .ok() |
| 42 | .into_iter() |
| 43 | .flat_map(|x| x) |
| 44 | .filter_map(|f| f.ok()) |
| 45 | .filter(|f| f.file_type().ok().is_some_and(|t| t.is_dir())) |
| 46 | .map(|f| SysfsPath(f.path())) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | trait FromHexStr: Sized { |
| 51 | fn from_hex_str(s: &str) -> Result<Self, ParseIntError>; |
| 52 | } |
| 53 | |
| 54 | impl FromHexStr for u8 { |
| 55 | fn from_hex_str(s: &str) -> Result<Self, ParseIntError> { |
| 56 | u8::from_str_radix(s, 16) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl FromHexStr for u16 { |
| 61 | fn from_hex_str(s: &str) -> Result<Self, ParseIntError> { |
| 62 | u16::from_str_radix(s, 16) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const SYSFS_PREFIX: &'static str = "/sys/bus/usb/devices/"; |
| 67 | |
| 68 | pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> { |
| 69 | Ok(fs::read_dir(SYSFS_PREFIX)?.flat_map(|entry| { |
| 70 | let res = probe_device(SysfsPath(entry.ok()?.path())); |
| 71 | if let Err(x) = &res { |
| 72 | debug!("failed to probe, skipping: {x}") |
| 73 | } |
| 74 | res.ok() |
| 75 | })) |
| 76 | } |
| 77 | |
| 78 | pub fn probe_device(path: SysfsPath) -> Result<DeviceInfo, Error> { |
| 79 | debug!("probe device {path:?}"); |
| 80 | Ok(DeviceInfo { |
| 81 | bus_number: path.read_attr("busnum")?, |
| 82 | device_address: path.read_attr("devnum")?, |
| 83 | vendor_id: path.read_attr_hex("idVendor")?, |
| 84 | product_id: path.read_attr_hex("idProduct")?, |
| 85 | device_version: path.read_attr_hex("bcdDevice")?, |
| 86 | class: path.read_attr_hex("bDeviceClass")?, |
| 87 | subclass: path.read_attr_hex("bDeviceSubClass")?, |
| 88 | protocol: path.read_attr_hex("bDeviceProtocol")?, |
| 89 | speed: path |
| 90 | .read_attr::<String>("speed") |
| 91 | .ok() |
| 92 | .as_deref() |
| 93 | .and_then(Speed::from_str), |
| 94 | manufacturer_string: path.read_attr("manufacturer").ok(), |
| 95 | product_string: path.read_attr("product").ok(), |
| 96 | serial_number: path.read_attr("serial").ok(), |
| 97 | interfaces: { |
| 98 | let mut interfaces: Vec<_> = path |
| 99 | .children() |
| 100 | .filter(|i| { |
| 101 | // Skip subdirectories like `power` that aren't interfaces |
| 102 | // (they would be skipped when missing required properties, |
| 103 | // but might as well not open them) |
| 104 | i.0.file_name() |
| 105 | .unwrap_or_default() |
| 106 | .as_encoded_bytes() |
| 107 | .contains(&b':') |
| 108 | }) |
| 109 | .flat_map(|i| { |
| 110 | Some(InterfaceInfo { |
| 111 | interface_number: i.read_attr_hex("bInterfaceNumber").ok()?, |
| 112 | class: i.read_attr_hex("bInterfaceClass").ok()?, |
| 113 | subclass: i.read_attr_hex("bInterfaceSubClass").ok()?, |
| 114 | protocol: i.read_attr_hex("bInterfaceProtocol").ok()?, |
| 115 | interface_string: i.read_attr("interface").ok(), |
| 116 | }) |
| 117 | }) |
| 118 | .collect(); |
| 119 | interfaces.sort_unstable_by_key(|i| i.interface_number); |
| 120 | interfaces |
| 121 | }, |
| 122 | path, |
| 123 | }) |
| 124 | } |