diff --git a/examples/cdc_acm_serial.rs b/examples/cdc_acm_serial.rs index 598c512..fce5a0a 100644 --- a/examples/cdc_acm_serial.rs +++ b/examples/cdc_acm_serial.rs @@ -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(), ), diff --git a/examples/hid_keyboard.rs b/examples/hid_keyboard.rs index e73034a..47bae3e 100644 --- a/examples/hid_keyboard.rs +++ b/examples/hid_keyboard.rs @@ -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 diff --git a/src/device.rs b/src/device.rs index 0007bd0..f1b1552 100644 --- a/src/device.rs +++ b/src/device.rs @@ -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 { - 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 { + 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 { + 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 { - 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 { + 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 { + 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 { - 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 { + 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 { + 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 { - 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 { + 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 { + let old = (self.string_manufacturer != 0) + .then(|| self.string_pool.remove(&self.string_manufacturer)) + .flatten(); + self.string_manufacturer = 0; old } diff --git a/src/lib.rs b/src/lib.rs index abb7ba3..b9afbc7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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