diff --git a/src/device_handle.rs b/src/device_handle.rs index eb9c360..ff2602d 100644 --- a/src/device_handle.rs +++ b/src/device_handle.rs @@ -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 { + let mut buf = Vec::::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). @@ -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 { + 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 { match device.manufacturer_string_index() { @@ -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 { + 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 { match device.product_string_index() { @@ -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 { + 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 { match device.serial_number_string_index() {