aboutsummaryrefslogtreecommitdiffstats
path: root/usbh/src/usbproto.rs
diff options
context:
space:
mode:
Diffstat (limited to 'usbh/src/usbproto.rs')
-rw-r--r--usbh/src/usbproto.rs85
1 files changed, 28 insertions, 57 deletions
diff --git a/usbh/src/usbproto.rs b/usbh/src/usbproto.rs
index 4fd97b0..be8ebfe 100644
--- a/usbh/src/usbproto.rs
+++ b/usbh/src/usbproto.rs
@@ -347,7 +347,7 @@ mod test {
#[test]
fn dev_desc_serialize() {
- let mut desc = USBDeviceDescriptor {
+ let desc = USBDeviceDescriptor {
b_length: 18,
b_descriptor_type: USBDescriptor::Device,
bcd_usb: 0x0110,
@@ -363,27 +363,17 @@ mod test {
i_serial_number: 0xa2,
b_num_configurations: 1,
};
- let db = DataBuf::from(&mut desc);
- assert_eq!(db.len, 18);
let want = [
0x12, 0x01, 0x10, 0x01, 0x02, 0x03, 0x04, 64, 0xad, 0xde, 0xef, 0xbe, 0x32, 0x54, 0xa0,
0xa1, 0xa2, 1,
];
- let got = unsafe { core::slice::from_raw_parts(db.ptr, db.len) };
- assert_eq!(got, want);
- let mut desc2: USBDeviceDescriptor = Default::default();
- let db = DataBuf::from(&mut desc2);
- let dbslice = unsafe { core::slice::from_raw_parts_mut(db.ptr, db.len) };
- for i in 0..db.len {
- dbslice[i] = want[i];
- }
- assert_eq!(desc2, desc);
+ serde_test(desc, &want);
}
#[test]
fn config_desc_serialize() {
- let mut desc = USBConfigurationDescriptor {
+ let desc = USBConfigurationDescriptor {
b_length: 18,
b_descriptor_type: USBDescriptor::Configuration,
w_total_length: 0x20,
@@ -393,24 +383,14 @@ mod test {
bm_attributes: 0x40,
b_max_power: 0xfa,
};
- let db = DataBuf::from(&mut desc);
- assert_eq!(db.len, 9);
let want = [0x12, 0x02, 0x20, 0x00, 0x05, 0x0a, 0x01, 0x40, 0xfa];
- let got = unsafe { core::slice::from_raw_parts(db.ptr, db.len) };
- assert_eq!(got, want);
- let mut desc2: USBConfigurationDescriptor = Default::default();
- let db = DataBuf::from(&mut desc2);
- let dbslice = unsafe { core::slice::from_raw_parts_mut(db.ptr, db.len) };
- for i in 0..db.len {
- dbslice[i] = want[i];
- }
- assert_eq!(desc2, desc);
+ serde_test(desc, &want);
}
#[test]
fn interface_desc_serialize() {
- let mut desc = USBInterfaceDescriptor {
+ let desc = USBInterfaceDescriptor {
b_length: 18,
b_descriptor_type: USBDescriptor::Interface,
b_interface_number: 2,
@@ -421,24 +401,14 @@ mod test {
b_interface_protocol: 0x33,
i_interface: 10,
};
- let db = DataBuf::from(&mut desc);
- assert_eq!(db.len, 9);
let want = [0x12, 0x04, 0x02, 0xaa, 0x05, 0x11, 0x22, 0x33, 0x0a];
- let got = unsafe { core::slice::from_raw_parts(db.ptr, db.len) };
- assert_eq!(got, want);
- let mut desc2: USBInterfaceDescriptor = Default::default();
- let db = DataBuf::from(&mut desc2);
- let dbslice = unsafe { core::slice::from_raw_parts_mut(db.ptr, db.len) };
- for i in 0..db.len {
- dbslice[i] = want[i];
- }
- assert_eq!(desc2, desc);
+ serde_test(desc, &want);
}
#[test]
fn endpoint_desc_serialize() {
- let mut desc = USBEndpointDescriptor {
+ let desc = USBEndpointDescriptor {
b_length: 18,
b_descriptor_type: USBDescriptor::Endpoint,
b_endpoint_address: 1,
@@ -446,42 +416,43 @@ mod test {
w_max_packet_size: 0xdead,
b_interval: 0x33,
};
- let db = DataBuf::from(&mut desc);
- assert_eq!(db.len, 7);
let want = [0x12, 0x05, 0x01, 0x22, 0xad, 0xde, 0x33];
- let got = unsafe { core::slice::from_raw_parts(db.ptr, db.len) };
- assert_eq!(got, want);
- let mut desc2: USBEndpointDescriptor = Default::default();
- let db = DataBuf::from(&mut desc2);
- let dbslice = unsafe { core::slice::from_raw_parts_mut(db.ptr, db.len) };
- for i in 0..db.len {
- dbslice[i] = want[i];
- }
- assert_eq!(desc2, desc);
+ serde_test(desc, &want);
}
#[test]
fn setup_packet_serialize() {
- let mut setup_packet = USBSetupPacket {
+ let setup_packet = USBSetupPacket {
bm_request_type: BMRequestType::get_descr(),
b_request: USBRequest::GetDescriptor,
w_value: WValue::from((0x00, 0x01)),
w_index: 0xbeef,
w_length: 18,
};
- let db = DataBuf::from(&mut setup_packet);
- assert_eq!(db.len, 8);
let want = [0x80, 0x06, 0x00, 0x01, 0xef, 0xbe, 0x012, 0x00];
- let got = unsafe { core::slice::from_raw_parts(db.ptr, db.len) };
- assert_eq!(got, want);
- let mut setup_packet2: USBSetupPacket = Default::default();
- let db = DataBuf::from(&mut setup_packet2);
+ serde_test(setup_packet, &want);
+ }
+
+ fn serde_test<T>(mut native: T, raw: &[u8])
+ where
+ T: Default + PartialEq + core::fmt::Debug,
+ {
+ let db = DataBuf::from(&mut native);
+ assert_eq!(db.len, raw.len());
+
+ // Serialization.
+ let got_raw = unsafe { core::slice::from_raw_parts(db.ptr, db.len) };
+ assert_eq!(got_raw, raw);
+
+ // Deserialization.
+ let mut got_native: T = Default::default();
+ let db = DataBuf::from(&mut got_native);
let dbslice = unsafe { core::slice::from_raw_parts_mut(db.ptr, db.len) };
for i in 0..db.len {
- dbslice[i] = want[i];
+ dbslice[i] = raw[i];
}
- assert_eq!(setup_packet2, setup_packet);
+ assert_eq!(got_native, native);
}
}