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

Add friendly_name field for usb device on windows #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub struct DeviceInfo {
#[cfg(target_os = "windows")]
pub(crate) driver: Option<String>,

#[cfg(target_os = "windows")]
pub(crate) friendly_name: Option<String>,

#[cfg(target_os = "macos")]
pub(crate) registry_id: u64,

Expand Down Expand Up @@ -143,6 +146,12 @@ impl DeviceInfo {
self.port_number
}

/// *(Windows-only)* Friendly name
#[cfg(target_os = "windows")]
pub fn friendly_name(&self) -> Option<&str> {
self.friendly_name.as_deref()
}

/// Path of port numbers identifying the port where the device is connected.
///
/// Together with the bus ID, it identifies a physical port. The path is
Expand Down Expand Up @@ -322,6 +331,7 @@ impl std::fmt::Debug for DeviceInfo {

#[cfg(target_os = "windows")]
{
s.field("friendly_name", &self.friendly_name);
s.field("instance_id", &self.instance_id);
s.field("parent_instance_id", &self.parent_instance_id);
s.field("location_paths", &self.location_paths);
Expand Down
10 changes: 8 additions & 2 deletions src/platform/windows_winusb/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use log::debug;
use windows_sys::Win32::Devices::{
Properties::{
DEVPKEY_Device_Address, DEVPKEY_Device_BusReportedDeviceDesc, DEVPKEY_Device_CompatibleIds,
DEVPKEY_Device_DeviceDesc, DEVPKEY_Device_HardwareIds, DEVPKEY_Device_InstanceId,
DEVPKEY_Device_LocationPaths, DEVPKEY_Device_Parent, DEVPKEY_Device_Service,
DEVPKEY_Device_DeviceDesc, DEVPKEY_Device_FriendlyName, DEVPKEY_Device_HardwareIds,
DEVPKEY_Device_InstanceId, DEVPKEY_Device_LocationPaths, DEVPKEY_Device_Parent,
DEVPKEY_Device_Service,
},
Usb::{GUID_DEVINTERFACE_USB_DEVICE, GUID_DEVINTERFACE_USB_HUB},
};
Expand Down Expand Up @@ -68,6 +69,10 @@ pub fn probe_device(devinst: DevInst) -> Option<DeviceInfo> {
.and_then(|s| s.into_string().ok());
// DEVPKEY_Device_Manufacturer exists but is often wrong and appears not to be read from the string descriptor but the .inf file

let friendly_name = devinst
.get_property::<OsString>(DEVPKEY_Device_FriendlyName)
.and_then(|s| s.into_string().ok());

let serial_number = if info.device_desc.iSerialNumber != 0 {
// Experimentally confirmed, the string descriptor is cached and this does
// not perform IO. However, the language ID list is not cached, so we
Expand Down Expand Up @@ -143,6 +148,7 @@ pub fn probe_device(devinst: DevInst) -> Option<DeviceInfo> {
speed: info.speed,
manufacturer_string: None,
product_string,
friendly_name,
serial_number,
interfaces,
})
Expand Down
Loading