Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds friendly name and manufactorer #5

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-sys = { version = "0.52", features = ["Win32_Devices_DeviceAndDriverInstallation", "Win32_Foundation"] }
[target.'cfg(target_os = "linux")'.dependencies]
udev = "0.8"
[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
7 changes: 7 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ pub struct UsbDevice {
pub vendor_id: u16,
/// Product ID
pub product_id: u16,
/// Friendly name.
pub friendly_name: Option<String>,
/// Optional device description
pub description: Option<String>,
/// Optional serial number
pub serial_number: Option<String>,
/// Class (bDeviceBaseClass) of device.
pub base_class: Option<DeviceBaseClass>,
/// Class as string
pub class: Option<String>,
/// Manufacturer
pub manufacturer: Option<String>,
}

/// See <https://www.usb.org/defined-class-codes>
#[repr(u8)]
#[derive(Hash, Eq, Debug, Clone, PartialEq, TryFromPrimitive)]
#[allow(dead_code)]
pub enum DeviceBaseClass {
UseClassCodeFromInterfaceDescriptors = 0x00,
Audio = 0x01,
Expand Down
16 changes: 16 additions & 0 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,29 @@ pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice>
.attribute_value("bDeviceClass")
.and_then(|x| x.to_str()?.parse::<u8>().ok());

let friendly_name = device
.property_value("ID_MODEL_FROM_DATABASE")
.and_then(|s| s.to_str())
.map(|x| x.to_string());
let manufacturer = device
.property_value("ID_VENDOR_FROM_DATABASE")
.and_then(|s| s.to_str())
.map(|x| x.to_string());
let class = device
.property_value("ID_PCI_CLASS_FROM_DATABASE")
.and_then(|s| s.to_str())
.map(|x| x.to_string());

output.push(UsbDevice {
id,
vendor_id,
product_id,
description,
serial_number,
base_class: bclass.and_then(|bc| DeviceBaseClass::try_from(bc).ok()),
class,
friendly_name,
manufacturer,
});

Ok(())
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
43 changes: 41 additions & 2 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use windows_sys::{
Win32::Devices::DeviceAndDriverInstallation::{
SetupDiDestroyDeviceInfoList, SetupDiEnumDeviceInfo, SetupDiGetClassDevsW,
SetupDiGetDeviceInstanceIdW, SetupDiGetDeviceRegistryPropertyW, DIGCF_ALLCLASSES,
DIGCF_PRESENT, SPDRP_CLASS, SPDRP_DEVICEDESC, SPDRP_HARDWAREID, SP_DEVINFO_DATA,
DIGCF_PRESENT, SPDRP_CLASS, SPDRP_DEVICEDESC, SPDRP_FRIENDLYNAME, SPDRP_HARDWAREID,
SPDRP_MFG, SP_DEVINFO_DATA,
},
};

pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice> {
let mut output: Vec<UsbDevice> = Vec::new();
// let usb: Vec<u16> = OsStr::new("USB\0").encode_wide().collect();
let usb = w!("USB\0");
let dev_info =
unsafe { SetupDiGetClassDevsW(null(), usb, -1, DIGCF_ALLCLASSES | DIGCF_PRESENT) };
Expand Down Expand Up @@ -77,6 +77,42 @@ pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice>
class = Some(string_from_buf_u8(buf));
}

// manufactor
buf = vec![0; 1000];
let mut manufacturer = None;
if unsafe {
SetupDiGetDeviceRegistryPropertyW(
dev_info,
&mut dev_info_data,
SPDRP_MFG,
null_mut(),
buf.as_mut_ptr(),
buf.len() as u32,
null_mut(),
)
} > 0
{
manufacturer = Some(string_from_buf_u8(buf));
}

// friendly name
buf = vec![0; 1000];
let mut friendly_name = None;
if unsafe {
SetupDiGetDeviceRegistryPropertyW(
dev_info,
&mut dev_info_data,
SPDRP_FRIENDLYNAME,
null_mut(),
buf.as_mut_ptr(),
buf.len() as u32,
null_mut(),
)
} > 0
{
friendly_name = Some(string_from_buf_u8(buf));
}

buf = vec![0; 1000];

if unsafe {
Expand Down Expand Up @@ -111,8 +147,11 @@ pub fn enumerate_platform(vid: Option<u16>, pid: Option<u16>) -> Vec<UsbDevice>
id,
vendor_id,
product_id,
friendly_name,
manufacturer,
description: Some(description),
serial_number,
class: class.clone(),
base_class: class.map(|cls| cls.into()),
});
}
Expand Down
Loading