Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow not setting a string descriptor #53

Merged
merged 1 commit into from
Oct 26, 2024
Merged
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
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> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is unclear, maybe we should use two apis for set & clear?

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