aboutsummaryrefslogtreecommitdiffstats
path: root/src/setup.rs
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-08-11 12:37:21 -0400
committerBrian Cully <bjc@kublai.com>2019-08-11 12:48:06 -0400
commite8739872b482872adf9b18ef2b5419098f8fadb3 (patch)
tree3956b680f26c495cdddf7b71ad656e2ed14c2cc0 /src/setup.rs
parentb0be48424e81384de3280b5a5ac521d694b82e15 (diff)
downloadusb-host-e8739872b482872adf9b18ef2b5419098f8fadb3.tar.gz
usb-host-e8739872b482872adf9b18ef2b5419098f8fadb3.zip
Add documentation and tests.
Diffstat (limited to 'src/setup.rs')
-rw-r--r--src/setup.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/setup.rs b/src/setup.rs
index 8fa6414..1451cb6 100644
--- a/src/setup.rs
+++ b/src/setup.rs
@@ -1,3 +1,12 @@
+//! A collection of structures for use in setting up devices during
+//! enumeration.
+//!
+//! These types are all defined in ยง9.3 of the USB 2.0 specification.
+//!
+//! The structures defined herein are `repr(C)` and `repr(packed)`
+//! when necessary to ensure that they are able to be directly
+//! marshalled to the bus.
+
use core::convert::{TryFrom, TryInto};
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -194,3 +203,43 @@ pub struct SetupPacket {
pub w_index: u16,
pub w_length: u16,
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ use core::mem;
+ use core::slice;
+
+ #[test]
+ fn setup_packet_layout() {
+ let len = mem::size_of::<SetupPacket>();
+ assert_eq!(len, 8);
+ let sp = SetupPacket {
+ bm_request_type: RequestType::from((
+ RequestDirection::HostToDevice,
+ RequestKind::Class,
+ RequestRecipient::Endpoint,
+ )),
+ b_request: RequestCode::GetInterface,
+ w_value: WValue::from((0xf0, 0x0d)),
+ w_index: 0xadde,
+ w_length: 0xefbe,
+ };
+ let base = &sp as *const _ as usize;
+ assert_offset("bm_request_type", &sp.bm_request_type, base, 0x00);
+ assert_offset("b_request", &sp.b_request, base, 0x01);
+ assert_offset("w_value", &sp.w_value, base, 0x02);
+ assert_offset("w_index", &sp.w_index, base, 0x04);
+ assert_offset("w_length", &sp.w_length, base, 0x06);
+
+ let got = unsafe { slice::from_raw_parts(&sp as *const _ as *const u8, len) };
+ let want = &[0x22, 0x0a, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef];
+ assert_eq!(got, want);
+ }
+
+ fn assert_offset<T>(name: &str, field: &T, base: usize, offset: usize) {
+ let ptr = field as *const _ as usize;
+ assert_eq!(ptr - base, offset, "{} register offset.", name);
+ }
+}