Skip to content

Commit

Permalink
Add unset string fuctions
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Oct 26, 2024
1 parent 458cc08 commit 7e3409d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/cdc_acm_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async fn main() {
usbip::ClassCode::CDC as u8,
usbip::cdc::CDC_ACM_SUBCLASS,
0x00,
"Test CDC ACM",
Some("Test CDC ACM"),
usbip::cdc::UsbCdcAcmHandler::endpoints(),
handler.clone(),
),
Expand Down
2 changes: 1 addition & 1 deletion examples/hid_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() {
usbip::ClassCode::HID as u8,
0x00,
0x00,
"Test HID",
Some("Test HID"),
vec![usbip::UsbEndpoint {
address: 0x81, // IN
attributes: 0x03, // Interrupt
Expand Down
76 changes: 60 additions & 16 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,82 @@ impl UsbDevice {
num_configurations: 1,
..Self::default()
};
res.string_configuration = 0;
res.string_manufacturer = 0;
res.string_product = 0;
res.string_serial = 0;
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
}

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

/// Unset configuration name and returns the old value, if present.
pub fn unset_configuration_name(&mut self) -> Option<String> {
let old = (self.string_configuration != 0)
.then(|| self.string_pool.remove(&self.string_configuration))
.flatten();
self.string_configuration = 0;
old
}

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

/// Unset serial number and returns the old value, if present.
pub fn unset_serial_number(&mut self) -> Option<String> {
let old = (self.string_serial != 0)
.then(|| self.string_pool.remove(&self.string_serial))
.flatten();
self.string_serial = 0;
old
}

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

/// Unset product name and returns the old value, if present.
pub fn unset_product_name(&mut self) -> Option<String> {
let old = (self.string_product != 0)
.then(|| self.string_pool.remove(&self.string_product))
.flatten();
self.string_product = 0;
old
}

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

/// Unset manufacturer name and returns the old value, if present.
pub fn unset_manufacturer_name(&mut self) -> Option<String> {
let old = (self.string_manufacturer != 0)
.then(|| self.string_pool.remove(&self.string_manufacturer))
.flatten();
self.string_manufacturer = 0;
old
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ mod tests {
ClassCode::CDC as u8,
cdc::CDC_ACM_SUBCLASS,
0x00,
"Test CDC ACM",
Some("Test CDC ACM"),
cdc::UsbCdcAcmHandler::endpoints(),
Arc::new(Mutex::new(
Box::new(cdc::UsbCdcAcmHandler::new()) as Box<dyn UsbInterfaceHandler + Send>
Expand Down

0 comments on commit 7e3409d

Please sign in to comment.