Skip to content

Commit

Permalink
Fix: Remove functions for getting a DeviceInfo from a opened device
Browse files Browse the repository at this point in the history
  • Loading branch information
kirisauce committed Jan 8, 2025
1 parent 088781f commit 3a17c2b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 94 deletions.
66 changes: 6 additions & 60 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
descriptors::{
decode_string_descriptor, language_id::US_ENGLISH, validate_device_descriptor,
validate_string_descriptor, ActiveConfigurationError, Configuration, DeviceDescriptor,
InterfaceAltSetting, DESCRIPTOR_TYPE_STRING,
decode_string_descriptor, validate_device_descriptor, validate_string_descriptor,
ActiveConfigurationError, Configuration, DeviceDescriptor, InterfaceAltSetting,
DESCRIPTOR_TYPE_STRING,
},
platform,
transfer::{
Expand Down Expand Up @@ -350,68 +350,14 @@ impl Device {
///
/// ### Platform-specific notes
///
/// * This is only supported on Linux.
/// * This is only supported on Linux at present.
/// * On Linux, this method uses descriptors cached in memory, instead
/// of sending a request to the device for a descriptor.
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn get_device_descriptor(&self) -> Option<DeviceDescriptor> {
let buf = self.backend.get_descriptors();
pub fn device_descriptor(&self) -> Option<DeviceDescriptor> {
let buf = self.backend.descriptors();
validate_device_descriptor(&buf).map(|len| DeviceDescriptor::new(&buf[0..len]))
}

/// Get [`DeviceInfo`] of this device.
/// This method calls [`Self::get_device_descriptor`] and [`Self::get_string_descriptor`]
/// for some information internally.
///
/// **NOTICE: This method always returns a [`DeviceInfo`] with empty
/// `interfaces`, `port_chain` and `path` fields.**
///
/// ### Platform-specific notes
///
/// * Only available on Linux.
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn get_device_info(&self) -> Result<DeviceInfo, Error> {
let Some(desc) = self.get_device_descriptor() else {
error!("No any valid device descriptor was found");
return Err(ErrorKind::Other.into());
};

let (busnum, device_address) = self.backend.get_busnum_and_devnum()?;
dbg!(&desc);
let manufacturer_string = self
.get_string_descriptor(desc.manufacturer_string_index(), US_ENGLISH, Duration::ZERO)
.ok();
let product_string = self
.get_string_descriptor(desc.product_string_index(), US_ENGLISH, Duration::ZERO)
.ok();
let serial_number = self
.get_string_descriptor(
desc.serial_number_string_index(),
US_ENGLISH,
Duration::ZERO,
)
.ok();

Ok(DeviceInfo {
path: platform::SysfsPath(std::path::PathBuf::default()),
busnum,
bus_id: format!("{busnum:03}"),
device_address,
port_chain: Vec::new(),
vendor_id: desc.vendor_id(),
product_id: desc.product_id(),
device_version: desc.device_version(),
class: desc.class(),
subclass: desc.subclass(),
protocol: desc.protocol(),
max_packet_size_0: desc.max_packet_size_0(),
speed: desc.speed(),
manufacturer_string,
product_string,
serial_number,
interfaces: Vec::new(),
})
}
}

/// An opened interface of a USB device.
Expand Down
36 changes: 2 additions & 34 deletions src/platform/linux_usbfs/device.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::{ErrorKind, Seek};
use std::{ffi::c_void, time::Duration};
use std::{
fs::{read_link, File},
fs::File,
io::Read,
mem::ManuallyDrop,
path::PathBuf,
Expand Down Expand Up @@ -36,8 +36,6 @@ use crate::{
DeviceInfo, Error,
};

const USBFS_DEVICE_PREFIX: &'static str = "/dev/bus/usb/";

pub(crate) struct LinuxDevice {
fd: OwnedFd,
events_id: usize,
Expand Down Expand Up @@ -408,39 +406,9 @@ impl LinuxDevice {
return Err(ErrorKind::Other.into());
}

pub(crate) fn get_descriptors(&self) -> &[u8] {
pub(crate) fn descriptors(&self) -> &[u8] {
&self.descriptors
}

pub(crate) fn get_busnum_and_devnum(&self) -> Result<(u8, u8), Error> {
let raw_fd = self.fd.as_raw_fd();
// The symbolic link points to the device file in `devfs` (usually mounted on /dev)
let fd_path = format!("/proc/self/fd/{}", raw_fd);

// Device path is like "/dev/bus/usb/002/003"
// The first number is the bus number, and the second is the device address on the bus
let device_path = read_link(&fd_path)?;

device_path
.as_os_str()
.to_str()
.and_then(|device_location| device_location.strip_prefix(USBFS_DEVICE_PREFIX))
.and_then(|device_location| device_location.split_once('/'))
.map(|(busnum, devnum)| {
(
busnum.trim_start_matches('0').parse::<u8>().unwrap_or(0),
devnum.trim_start_matches('0').parse::<u8>().unwrap_or(0),
)
})
.ok_or_else(|| {
warn!(
"Unable to parse path \"{}\" for device fd {}",
device_path.display(),
raw_fd
);
ErrorKind::Other.into()
})
}
}

impl Drop for LinuxDevice {
Expand Down

0 comments on commit 3a17c2b

Please sign in to comment.