diff --git a/src/platform/pnp_detect_libusb.rs b/src/platform/pnp_detect_libusb.rs index ec559d3..32822dc 100644 --- a/src/platform/pnp_detect_libusb.rs +++ b/src/platform/pnp_detect_libusb.rs @@ -3,32 +3,31 @@ // This code is licensed under MIT license (see LICENSE.txt for details) // -use crate::usb::{device2str, UsbCallback}; use anyhow::{anyhow, Result}; use rusb::{Context, Device, HotplugBuilder, Registration, UsbContext}; /// Detection of plugged in / removed USB devices: uses "libusb" and should work on Linux /// and MacOS, but not on Windows: libusb does not support hotplug on Windows. pub struct PnPDetectLibusb { - callback: Box, + callback: Box, } impl rusb::Hotplug for PnPDetectLibusb { fn device_arrived(&mut self, device: Device) { - if let Some(str) = device2str(device) { + if let Some(str) = crate::usb::device_id(&device) { self.callback.device_added(&str) } } fn device_left(&mut self, device: Device) { - if let Some(str) = device2str(device) { + if let Some(str) = crate::usb::device_id(&device) { self.callback.device_removed(&str) } } } impl PnPDetectLibusb { - pub fn new(callback: Box) -> Box { + pub fn new(callback: Box) -> Box { Box::new(PnPDetectLibusb { callback }) } diff --git a/src/platform/pnp_detect_windows.rs b/src/platform/pnp_detect_windows.rs index 0e5dd2e..4636804 100644 --- a/src/platform/pnp_detect_windows.rs +++ b/src/platform/pnp_detect_windows.rs @@ -8,7 +8,6 @@ use std::ffi::OsStr; use std::iter::once; use std::os::windows::ffi::OsStrExt; -use crate::usb::{device2str, UsbCallback}; use anyhow::{anyhow, Result}; use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM}; use winapi::shared::ntdef::LPCWSTR; @@ -24,12 +23,12 @@ use winapi::um::winuser::{ /// https://github.com/libusb/libusb/issues/86 pub struct PnPDetectWindows { hwnd: HWND, - callback: Box, + callback: Box, current_devices: HashSet, } impl PnPDetectWindows { - pub fn new(callback: Box) -> Box { + pub fn new(callback: Box) -> Box { let mut pnp_detect = Box::new(Self { callback, current_devices: Self::read_device_list().unwrap_or_default(), @@ -62,7 +61,7 @@ impl PnPDetectWindows { fn read_device_list() -> Result> { Ok(rusb::devices()? .iter() - .map(|device| device2str(device).ok_or(anyhow!("Cannot get device Ids"))) + .map(|device| crate::usb::device_id(&device).ok_or(anyhow!("Cannot get device Ids"))) .collect::>()?) } diff --git a/src/usb.rs b/src/usb.rs index 9e606ee..2b97804 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -4,13 +4,25 @@ // use rusb::UsbContext; -pub fn device2str(device: rusb::Device) -> Option { +pub fn device_id(device: &rusb::Device) -> Option { device .device_descriptor() .map(|device_desc| format!("{:04x}:{:04x}", device_desc.vendor_id(), device_desc.product_id())) .ok() } +// pub fn device_product_name(device: &rusb::Device) -> Option { +// let descriptor = device.device_descriptor().ok()?; +// let handle = device.open().ok()?; +// handle.read_product_string_ascii(&descriptor).ok() +// } +// +// pub fn device_manufacturer_name(device: &rusb::Device) -> Option { +// let descriptor = device.device_descriptor().ok()?; +// let handle = device.open().ok()?; +// handle.read_manufacturer_string_ascii(&descriptor).ok() +// } + pub trait UsbCallback: Send { fn device_added(&self, device_id: &str); fn device_removed(&self, device_id: &str);