Skip to content

Commit

Permalink
Allow not setting a string descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Oct 23, 2024
1 parent 341bd0f commit 458cc08
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,47 +83,51 @@ impl UsbDevice {
num_configurations: 1,
..Self::default()
};
res.string_configuration = res.new_string("Default Configuration");
res.string_manufacturer = res.new_string("Manufacturer");
res.string_product = res.new_string("Product");
res.string_serial = res.new_string("Serial");
res.string_configuration = 0;
res.string_manufacturer = 0;
res.string_product = 0;
res.string_serial = 0;
res
}

/// Returns the old value, if present.
pub fn set_configuration_name(&mut self, name: &str) -> Option<String> {
self.string_pool
.insert(self.string_configuration, name.to_string())
pub fn set_configuration_name(&mut self, name: Option<&str>) -> Option<String> {
let old = (self.string_configuration != 0).then(|| self.string_pool.remove(&self.string_configuration)).flatten();
self.string_configuration = name.map(|name| self.new_string(name)).unwrap_or(0);
old
}

/// Returns the old value, if present.
pub fn set_serial_number(&mut self, name: &str) -> Option<String> {
self.string_pool
.insert(self.string_serial, name.to_string())
pub fn set_serial_number(&mut self, name: Option<&str>) -> Option<String> {
let old = (self.string_serial != 0).then(|| self.string_pool.remove(&self.string_serial)).flatten();
self.string_serial = name.map(|name| self.new_string(name)).unwrap_or(0);
old
}

/// Returns the old value, if present.
pub fn set_product_name(&mut self, name: &str) -> Option<String> {
self.string_pool
.insert(self.string_product, name.to_string())
pub fn set_product_name(&mut self, name: Option<&str>) -> Option<String> {
let old = (self.string_product != 0).then(|| self.string_pool.remove(&self.string_product)).flatten();
self.string_product = name.map(|name| self.new_string(name)).unwrap_or(0);
old
}

/// Returns the old value, if present.
pub fn set_manufacturer_name(&mut self, name: &str) -> Option<String> {
self.string_pool
.insert(self.string_manufacturer, name.to_string())
pub fn set_manufacturer_name(&mut self, name: Option<&str>) -> Option<String> {
let old = (self.string_manufacturer != 0).then(|| self.string_pool.remove(&self.string_manufacturer)).flatten();
self.string_manufacturer = name.map(|name| self.new_string(name)).unwrap_or(0);
old
}

pub fn with_interface(
mut self,
interface_class: u8,
interface_subclass: u8,
interface_protocol: u8,
name: &str,
name: Option<&str>,
endpoints: Vec<UsbEndpoint>,
handler: Arc<Mutex<Box<dyn UsbInterfaceHandler + Send>>>,
) -> Self {
let string_interface = self.new_string(name);
let string_interface = name.map(|name| self.new_string(name)).unwrap_or(0);
let class_specific_descriptor = handler.lock().unwrap().get_class_specific_descriptor();
self.interfaces.push(UsbInterface {
interface_class,
Expand Down

0 comments on commit 458cc08

Please sign in to comment.