Add unset string fuctions

This commit is contained in:
Jiajie Chen 2024-10-26 09:35:32 +08:00
parent 458cc084c7
commit 7e3409d6d9
4 changed files with 63 additions and 19 deletions

View file

@ -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(),
),

View file

@ -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

View file

@ -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
}

View file

@ -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>