aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/descriptor_reader.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-04-16 04:47:27 +0200
committerDario Nieuwenhuis <[email protected]>2022-04-23 01:11:10 +0200
commit092c2b7dfea146681cea01fd4e4105c21c62b61f (patch)
treebe32635e53177b36ef36939bcdd5eadcf3be3ab8 /embassy-usb/src/descriptor_reader.rs
parentea0a701ebd142ea42e05e51e844bd53cc9bdf354 (diff)
usb: builtin handling of interface alternate settings
The stack reads its own descriptors to figure out which endpoints are used in which alt settings, and enables/disables them as needed. The ControlHandler has a callback so it can get notified of alternate setting changes, which is purely informative (it doesn't have to do anything).
Diffstat (limited to 'embassy-usb/src/descriptor_reader.rs')
-rw-r--r--embassy-usb/src/descriptor_reader.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/embassy-usb/src/descriptor_reader.rs b/embassy-usb/src/descriptor_reader.rs
new file mode 100644
index 000000000..0a12b566c
--- /dev/null
+++ b/embassy-usb/src/descriptor_reader.rs
@@ -0,0 +1,110 @@
1use crate::descriptor::descriptor_type;
2use crate::types::EndpointAddress;
3
4#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6pub struct ReadError;
7
8pub struct Reader<'a> {
9 data: &'a [u8],
10}
11
12impl<'a> Reader<'a> {
13 pub fn new(data: &'a [u8]) -> Self {
14 Self { data }
15 }
16
17 pub fn eof(&self) -> bool {
18 self.data.is_empty()
19 }
20
21 pub fn read<const N: usize>(&mut self) -> Result<[u8; N], ReadError> {
22 let n = self.data.get(0..N).ok_or(ReadError)?;
23 self.data = &self.data[N..];
24 Ok(n.try_into().unwrap())
25 }
26
27 pub fn read_u8(&mut self) -> Result<u8, ReadError> {
28 Ok(u8::from_le_bytes(self.read()?))
29 }
30 pub fn read_u16(&mut self) -> Result<u16, ReadError> {
31 Ok(u16::from_le_bytes(self.read()?))
32 }
33
34 pub fn read_slice(&mut self, len: usize) -> Result<&'a [u8], ReadError> {
35 let res = self.data.get(0..len).ok_or(ReadError)?;
36 self.data = &self.data[len..];
37 Ok(res)
38 }
39
40 pub fn read_descriptors(&mut self) -> DescriptorIter<'_, 'a> {
41 DescriptorIter { r: self }
42 }
43}
44
45pub struct DescriptorIter<'a, 'b> {
46 r: &'a mut Reader<'b>,
47}
48
49impl<'a, 'b> Iterator for DescriptorIter<'a, 'b> {
50 type Item = Result<(u8, Reader<'a>), ReadError>;
51
52 fn next(&mut self) -> Option<Self::Item> {
53 if self.r.eof() {
54 return None;
55 }
56
57 let len = match self.r.read_u8() {
58 Ok(x) => x,
59 Err(e) => return Some(Err(e)),
60 };
61 let type_ = match self.r.read_u8() {
62 Ok(x) => x,
63 Err(e) => return Some(Err(e)),
64 };
65 let data = match self.r.read_slice(len as usize - 2) {
66 Ok(x) => x,
67 Err(e) => return Some(Err(e)),
68 };
69
70 Some(Ok((type_, Reader::new(data))))
71 }
72}
73
74#[derive(Copy, Clone, PartialEq, Eq, Debug)]
75#[cfg_attr(feature = "defmt", derive(defmt::Format))]
76pub struct EndpointInfo {
77 pub configuration: u8,
78 pub interface: u8,
79 pub interface_alt: u8,
80 pub ep_address: EndpointAddress,
81}
82
83pub fn foreach_endpoint(data: &[u8], mut f: impl FnMut(EndpointInfo)) -> Result<(), ReadError> {
84 let mut ep = EndpointInfo {
85 configuration: 0,
86 interface: 0,
87 interface_alt: 0,
88 ep_address: EndpointAddress::from(0),
89 };
90 for res in Reader::new(data).read_descriptors() {
91 let (kind, mut r) = res?;
92 match kind {
93 descriptor_type::CONFIGURATION => {
94 let _total_length = r.read_u16()?;
95 let _total_length = r.read_u8()?;
96 ep.configuration = r.read_u8()?;
97 }
98 descriptor_type::INTERFACE => {
99 ep.interface = r.read_u8()?;
100 ep.interface_alt = r.read_u8()?;
101 }
102 descriptor_type::ENDPOINT => {
103 ep.ep_address = EndpointAddress::from(r.read_u8()?);
104 f(ep)
105 }
106 _ => {}
107 }
108 }
109 Ok(())
110}