| 1 |
use std::io::ErrorKind; |
| 2 |
|
| 3 |
use core_foundation::{ |
| 4 |
base::{CFType, TCFType}, |
| 5 |
number::CFNumber, |
| 6 |
string::CFString, |
| 7 |
ConcreteCFType, |
| 8 |
}; |
| 9 |
use io_kit_sys::{ |
| 10 |
kIOMasterPortDefault, kIORegistryIterateParents, kIORegistryIterateRecursively, |
| 11 |
keys::kIOServicePlane, ret::kIOReturnSuccess, usb::lib::kIOUSBDeviceClassName, |
| 12 |
IORegistryEntryGetChildIterator, IORegistryEntryGetRegistryEntryID, |
| 13 |
IORegistryEntrySearchCFProperty, IOServiceGetMatchingServices, IOServiceMatching, |
| 14 |
}; |
| 15 |
use log::debug; |
| 16 |
|
| 17 |
use crate::{DeviceInfo, Error, InterfaceInfo, Speed}; |
| 18 |
|
| 19 |
use super::iokit::{IoService, IoServiceIterator}; |
| 20 |
|
| 21 |
fn usb_service_iter() -> Result<IoServiceIterator, Error> { |
| 22 |
unsafe { |
| 23 |
let dictionary = IOServiceMatching(kIOUSBDeviceClassName); |
| 24 |
if dictionary.is_null() { |
| 25 |
return Err(Error::new(ErrorKind::Other, "IOServiceMatching failed")); |
| 26 |
} |
| 27 |
|
| 28 |
let mut iterator = 0; |
| 29 |
let r = IOServiceGetMatchingServices(kIOMasterPortDefault, dictionary, &mut iterator); |
| 30 |
if r != kIOReturnSuccess { |
| 31 |
return Err(Error::from_raw_os_error(r)); |
| 32 |
} |
| 33 |
|
| 34 |
Ok(IoServiceIterator::new(iterator)) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> { |
| 39 |
Ok(usb_service_iter()?.filter_map(probe_device)) |
| 40 |
} |
| 41 |
|
| 42 |
pub(crate) fn service_by_registry_id(registry_id: u64) -> Result<IoService, Error> { |
| 43 |
usb_service_iter()? |
| 44 |
.find(|dev| get_registry_id(dev) == Some(registry_id)) |
| 45 |
.ok_or(Error::new(ErrorKind::NotFound, "not found by registry id")) |
| 46 |
} |
| 47 |
|
| 48 |
pub(crate) fn probe_device(device: IoService) -> Option<DeviceInfo> { |
| 49 |
let registry_id = get_registry_id(&device)?; |
| 50 |
log::debug!("Probing device {registry_id:08x}"); |
| 51 |
|
| 52 |
|
| 53 |
Some(DeviceInfo { |
| 54 |
registry_id, |
| 55 |
location_id: get_integer_property(&device, "locationID")? as u32, |
| 56 |
bus_number: 0, |
| 57 |
device_address: get_integer_property(&device, "USB Address")? as u8, |
| 58 |
vendor_id: get_integer_property(&device, "idVendor")? as u16, |
| 59 |
product_id: get_integer_property(&device, "idProduct")? as u16, |
| 60 |
device_version: get_integer_property(&device, "bcdDevice")? as u16, |
| 61 |
class: get_integer_property(&device, "bDeviceClass")? as u8, |
| 62 |
subclass: get_integer_property(&device, "bDeviceSubClass")? as u8, |
| 63 |
protocol: get_integer_property(&device, "bDeviceProtocol")? as u8, |
| 64 |
speed: get_integer_property(&device, "Device Speed").and_then(map_speed), |
| 65 |
manufacturer_string: get_string_property(&device, "kUSBVendorString") |
| 66 |
.or_else(|| get_string_property(&device, "USB Vendor Name")), |
| 67 |
product_string: get_string_property(&device, "kUSBProductString") |
| 68 |
.or_else(|| get_string_property(&device, "USB Product Name")), |
| 69 |
serial_number: get_string_property(&device, "kUSBSerialNumberString") |
| 70 |
.or_else(|| get_string_property(&device, "USB Serial Number")), |
| 71 |
interfaces: get_children(&device).map_or(Vec::new(), |iter| { |
| 72 |
iter.flat_map(|child| { |
| 73 |
Some(InterfaceInfo { |
| 74 |
interface_number: get_integer_property(&child, "bInterfaceNumber")? as u8, |
| 75 |
class: get_integer_property(&child, "bInterfaceClass")? as u8, |
| 76 |
subclass: get_integer_property(&child, "bInterfaceSubClass")? as u8, |
| 77 |
protocol: get_integer_property(&child, "bInterfaceProtocol")? as u8, |
| 78 |
interface_string: get_string_property(&child, "kUSBString") |
| 79 |
.or_else(|| get_string_property(&child, "USB Interface Name")), |
| 80 |
}) |
| 81 |
}) |
| 82 |
.collect() |
| 83 |
}), |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
pub(crate) fn get_registry_id(device: &IoService) -> Option<u64> { |
| 88 |
unsafe { |
| 89 |
let mut out = 0; |
| 90 |
let r = IORegistryEntryGetRegistryEntryID(device.get(), &mut out); |
| 91 |
|
| 92 |
if r == kIOReturnSuccess { |
| 93 |
Some(out) |
| 94 |
} else { |
| 95 |
|
| 96 |
debug!("IORegistryEntryGetRegistryEntryID failed with {r}"); |
| 97 |
None |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
fn get_property<T: ConcreteCFType>(device: &IoService, property: &'static str) -> Option<T> { |
| 103 |
unsafe { |
| 104 |
let cf_property = CFString::from_static_string(property); |
| 105 |
|
| 106 |
let raw = IORegistryEntrySearchCFProperty( |
| 107 |
device.get(), |
| 108 |
kIOServicePlane as *mut i8, |
| 109 |
cf_property.as_CFTypeRef() as *const _, |
| 110 |
std::ptr::null(), |
| 111 |
kIORegistryIterateRecursively | kIORegistryIterateParents, |
| 112 |
); |
| 113 |
|
| 114 |
if raw.is_null() { |
| 115 |
debug!("Device does not have property `{property}`"); |
| 116 |
return None; |
| 117 |
} |
| 118 |
|
| 119 |
let res = CFType::wrap_under_create_rule(raw).downcast_into(); |
| 120 |
|
| 121 |
if res.is_none() { |
| 122 |
debug!("Failed to convert device property `{property}`"); |
| 123 |
} |
| 124 |
|
| 125 |
res |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
fn get_string_property(device: &IoService, property: &'static str) -> Option<String> { |
| 130 |
get_property::<CFString>(device, property).map(|s| s.to_string()) |
| 131 |
} |
| 132 |
|
| 133 |
fn get_integer_property(device: &IoService, property: &'static str) -> Option<i64> { |
| 134 |
let n = get_property::<CFNumber>(device, property)?; |
| 135 |
n.to_i64().or_else(|| { |
| 136 |
debug!("failed to convert {property} value {n:?} to i64"); |
| 137 |
None |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
fn get_children(device: &IoService) -> Result<IoServiceIterator, Error> { |
| 142 |
unsafe { |
| 143 |
let mut iterator = 0; |
| 144 |
let r = |
| 145 |
IORegistryEntryGetChildIterator(device.get(), kIOServicePlane as *mut _, &mut iterator); |
| 146 |
if r != kIOReturnSuccess { |
| 147 |
debug!("IORegistryEntryGetChildIterator failed: {r}"); |
| 148 |
return Err(Error::from_raw_os_error(r)); |
| 149 |
} |
| 150 |
|
| 151 |
Ok(IoServiceIterator::new(iterator)) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
fn map_speed(speed: i64) -> Option<Speed> { |
| 156 |
|
| 157 |
match speed { |
| 158 |
0 => Some(Speed::Low), |
| 159 |
1 => Some(Speed::Full), |
| 160 |
2 => Some(Speed::High), |
| 161 |
3 => Some(Speed::Super), |
| 162 |
4 | 5 => Some(Speed::SuperPlus), |
| 163 |
_ => None, |
| 164 |
} |
| 165 |
} |