Skip to content
Open
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
52 changes: 52 additions & 0 deletions src/device_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@ impl<'a> DeviceHandle<'a> {
}).collect())
}

/// Reads a ascii string descriptor from the device.
///
pub fn read_string_descriptor_ascii(&self, index: u8) -> ::Result<String> {
let mut buf = Vec::<u8>::with_capacity(256);

let buf_slice = unsafe {
slice::from_raw_parts_mut((&mut buf[..]).as_mut_ptr(), buf.capacity())
};

let ptr = buf_slice.as_mut_ptr() as *mut c_uchar;
let len = buf_slice.len() as i32;

let res = unsafe {
libusb_get_string_descriptor_ascii(self.handle, index, ptr, len)
};

if res < 0 {
return Err(error::from_libusb(res))
}

unsafe {
buf.set_len(res as usize);
}

String::from_utf8(buf).map_err(|_| Error::Other)
}

/// Reads a string descriptor from the device.
///
/// `language` should be one of the languages returned from [`read_languages`](#method.read_languages).
Expand Down Expand Up @@ -452,6 +479,15 @@ impl<'a> DeviceHandle<'a> {
String::from_utf16(&utf16[..]).map_err(|_| Error::Other)
}


/// Reads the device's manufacturer string descriptor (ascii).
pub fn read_manufacturer_string_ascii(&self, device: &DeviceDescriptor) -> ::Result<String> {
match device.manufacturer_string_index() {
None => Err(Error::InvalidParam),
Some(n) => self.read_string_descriptor_ascii(n)
}
}

/// Reads the device's manufacturer string descriptor.
pub fn read_manufacturer_string(&self, language: Language, device: &DeviceDescriptor, timeout: Duration) -> ::Result<String> {
match device.manufacturer_string_index() {
Expand All @@ -460,6 +496,14 @@ impl<'a> DeviceHandle<'a> {
}
}

/// Reads the device's product string descriptor (ascii).
pub fn read_product_string_ascii(&self, device: &DeviceDescriptor) -> ::Result<String> {
match device.product_string_index() {
None => Err(Error::InvalidParam),
Some(n) => self.read_string_descriptor_ascii(n)
}
}

/// Reads the device's product string descriptor.
pub fn read_product_string(&self, language: Language, device: &DeviceDescriptor, timeout: Duration) -> ::Result<String> {
match device.product_string_index() {
Expand All @@ -468,6 +512,14 @@ impl<'a> DeviceHandle<'a> {
}
}

/// Reads the device's serial number string descriptor (ascii).
pub fn read_serial_number_string_ascii(&self, device: &DeviceDescriptor) -> ::Result<String> {
match device.serial_number_string_index() {
None => Err(Error::InvalidParam),
Some(n) => self.read_string_descriptor_ascii(n)
}
}

/// Reads the device's serial number string descriptor.
pub fn read_serial_number_string(&self, language: Language, device: &DeviceDescriptor, timeout: Duration) -> ::Result<String> {
match device.serial_number_string_index() {
Expand Down