Skip to content

Commit

Permalink
chore: osx doesn't have a consistent class name
Browse files Browse the repository at this point in the history
  • Loading branch information
dilawar committed Jul 8, 2024
1 parent 8a34330 commit 71a452e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ strict = []
crossbeam = "0.8"
num_enum = "0.7.2"
[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { version = "0.52.0", features = ["Win32_Devices_DeviceAndDriverInstallation", "Win32_Foundation"] }
windows = { version = "0.58", features = ["Win32_Devices_DeviceAndDriverInstallation", "Win32_Foundation"] }
[target.'cfg(target_os = "linux")'.dependencies]
udev = "0.8"
[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
33 changes: 25 additions & 8 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::common::*;
use core_foundation::{base::*, dictionary::*, number::*, string::*};
use io_kit_sys::{types::*, usb::lib::*, *};
use mach::kern_return::*;
use std::{error::Error, mem::MaybeUninit};
use std::convert::TryFrom;
use std::{convert::TryFrom, error::Error, mem::MaybeUninit};

pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice> {
let mut output = Vec::new();
Expand Down Expand Up @@ -91,20 +90,38 @@ pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice>
.map(|s| s.to_string());

let key = CFString::from_static_string("bDeviceClass");
let base_class = properties
let base_class = DeviceBaseClass::try_from(
properties
.find(&key)
.and_then(|value_ref| value_ref.downcast::<CFNumber>())
.ok_or(ParseError)?
.to_i32()
.ok_or(ParseError)? as u8,
)
.ok();

let key = CFString::from_static_string("USB Product Name");
let friendly_name = properties
.find(&key)
.and_then(|value_ref| value_ref.downcast::<CFNumber>())
.ok_or(ParseError)?
.to_i32()
.ok_or(ParseError)? as u8;
.and_then(|value_ref| value_ref.downcast::<CFString>())
.map(|s| s.to_string());

let key = CFString::from_static_string("USB Vendor Name");
let manufacturer = properties
.find(&key)
.and_then(|value_ref| value_ref.downcast::<CFString>())
.map(|s| s.to_string());

output.push(UsbDevice {
id: id.to_string(),
vendor_id,
product_id,
description,
serial_number,
base_class: DeviceBaseClass::try_from(base_class).ok(),
base_class,
class: None,
friendly_name,
manufacturer,
});

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
mem::{size_of, zeroed},
ptr::{null, null_mut},
};
use windows_sys::{
use windows::{
w,

Check failure on line 8 in src/windows.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, stable)

unresolved import `windows::w`

Check failure on line 8 in src/windows.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, nightly)

unresolved import `windows::w`

Check failure on line 8 in src/windows.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 1.51.0)

unresolved import `windows::w`

Check failure on line 8 in src/windows.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, beta)

unresolved import `windows::w`
Win32::Devices::DeviceAndDriverInstallation::{
SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInfo, SetupDiGetClassDevsW,
Expand Down

0 comments on commit 71a452e

Please sign in to comment.