Skip to content

Commit

Permalink
windows: just check starts with root_hub
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Sep 23, 2024
1 parent dbcc440 commit 53b5920
Showing 1 changed file with 4 additions and 40 deletions.
44 changes: 4 additions & 40 deletions src/platform/windows_winusb/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ pub fn probe_device(devinst: DevInst) -> Option<DeviceInfo> {

pub fn probe_bus(devinst: DevInst) -> Option<BusInfo> {
let instance_id = devinst.get_property::<OsString>(DEVPKEY_Device_InstanceId)?;
// Skip non-root hubs; buses - ID will not parse
let (_device_version, _serial_number) = parse_root_hub_id(&instance_id)?;
// Skip non-root hubs; buses which have instance IDs starting with "USB\\ROOT_HUB"
if !instance_id.to_string_lossy().starts_with("USB\\ROOT_HUB") {
return None;
}

debug!("Probing bus {instance_id:?}");

Expand Down Expand Up @@ -394,41 +396,3 @@ fn test_parse_location_path() {
None
);
}

/// Parse device version and ID from Root Hub instance ID
fn parse_root_hub_id(s: &OsStr) -> Option<(u16, Option<String>)> {
let s = s.to_str()?;
let s = s.strip_prefix("USB\\ROOT_HUB")?;
let (version, i) = u16::from_str_radix(s.get(0..2).unwrap_or("11"), 10)
.map(|v| ((v / 10) << 8 | v % 10 << 4, 2)) // convert to BCD
.unwrap_or((0x0110, 0)); // default USB 1.1
let id = s
.get(i..)
.and_then(|v| v.strip_prefix("\\").map(|s| s.to_owned()));
Some((version, id))
}

#[test]
fn test_parse_root_hub_id() {
assert_eq!(parse_root_hub_id(OsStr::new("")), None);
assert_eq!(
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB")),
Some((0x0110, None))
);
assert_eq!(
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB\\4&2FB9F669&0")),
Some((0x0110, Some("4&2FB9F669&0".to_string())))
);
assert_eq!(
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB20")),
Some((0x0200, None))
);
assert_eq!(
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB31")),
Some((0x0310, None))
);
assert_eq!(
parse_root_hub_id(OsStr::new("USB\\ROOT_HUB30\\4&2FB9F669&0")),
Some((0x0300, Some("4&2FB9F669&0".to_string())))
);
}

0 comments on commit 53b5920

Please sign in to comment.