1use crate::descriptor::descriptor_type;
10use crate::endpoint::{EndpointAddress, EndpointType, IsoSynchronization, IsoUsage};
11use crate::host::error::{HostError, HostResult};
12
13#[inline]
14const fn le16(bytes: &[u8], offset: usize) -> u16 {
15 (bytes[offset] as u16) | ((bytes[offset + 1] as u16) << 8)
16}
17
18#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
22#[cfg_attr(feature = "defmt", derive(defmt::Format))]
23pub struct DeviceDescriptor {
24 pub usb_version: u16,
26 pub class: u8,
28 pub sub_class: u8,
30 pub protocol: u8,
32 pub max_packet0: u16,
34 pub vendor_id: u16,
36 pub product_id: u16,
38 pub device_version: u16,
40 pub manufacturer: u8,
42 pub product: u8,
44 pub serial_number: u8,
46 pub configurations: u8,
48}
49
50impl DeviceDescriptor {
51 pub const LEN: usize = 18;
53
54 pub fn parse(bytes: &[u8]) -> HostResult<Self> {
56 if bytes.len() < Self::LEN || bytes[0] < Self::LEN as u8 {
57 return Err(HostError::Malformed);
58 }
59 if bytes[1] != descriptor_type::DEVICE {
60 return Err(HostError::Malformed);
61 }
62 Ok(Self {
63 usb_version: le16(bytes, 2),
64 class: bytes[4],
65 sub_class: bytes[5],
66 protocol: bytes[6],
67 max_packet0: bytes[7] as u16,
68 vendor_id: le16(bytes, 8),
69 product_id: le16(bytes, 10),
70 device_version: le16(bytes, 12),
71 manufacturer: bytes[14],
72 product: bytes[15],
73 serial_number: bytes[16],
74 configurations: bytes[17],
75 })
76 }
77}
78
79#[derive(Clone, Copy, PartialEq, Eq, Debug)]
83pub struct Descriptor<'a> {
84 pub kind: u8,
86 pub bytes: &'a [u8],
88}
89
90#[derive(Clone, Copy, Debug)]
92pub struct Descriptors<'a> {
93 rest: &'a [u8],
94}
95
96impl<'a> Iterator for Descriptors<'a> {
97 type Item = Descriptor<'a>;
98
99 fn next(&mut self) -> Option<Self::Item> {
100 let length = *self.rest.first()? as usize;
101 if length < 2 || length > self.rest.len() {
102 self.rest = &[];
105 return None;
106 }
107 let (head, tail) = self.rest.split_at(length);
108 self.rest = tail;
109 Some(Descriptor {
110 kind: head[1],
111 bytes: head,
112 })
113 }
114}
115
116#[derive(Clone, Copy, PartialEq, Eq, Debug)]
120pub struct Configuration<'a> {
121 raw: &'a [u8],
122}
123
124impl<'a> Configuration<'a> {
125 pub const HEADER_LEN: usize = 9;
127
128 pub fn parse(raw: &'a [u8]) -> HostResult<Self> {
130 if raw.len() < Self::HEADER_LEN || raw[1] != descriptor_type::CONFIGURATION {
131 return Err(HostError::Malformed);
132 }
133 let total = le16(raw, 2) as usize;
134 if total < Self::HEADER_LEN || total > raw.len() {
135 return Err(HostError::Malformed);
136 }
137 Ok(Self { raw: &raw[..total] })
138 }
139
140 pub(crate) fn validated(raw: &'a [u8]) -> Self {
143 Self { raw }
144 }
145
146 #[inline]
148 pub const fn bytes(&self) -> &'a [u8] {
149 self.raw
150 }
151
152 #[inline]
154 pub fn total_length(&self) -> u16 {
155 le16(self.raw, 2)
156 }
157
158 #[inline]
160 pub fn value(&self) -> u8 {
161 self.raw[5]
162 }
163
164 #[inline]
166 pub fn attributes(&self) -> u8 {
167 self.raw[6]
168 }
169
170 #[inline]
172 pub fn max_power_ma(&self) -> u16 {
173 self.raw[8] as u16 * 2
174 }
175
176 #[inline]
178 pub fn descriptors(&self) -> Descriptors<'a> {
179 Descriptors { rest: self.raw }
180 }
181
182 #[inline]
184 pub fn interfaces(&self) -> Interfaces<'a> {
185 Interfaces {
186 raw: self.raw,
187 offset: Self::HEADER_LEN,
188 }
189 }
190
191 pub fn find_interface(&self, class: u8, sub_class: u8, protocol: u8) -> Option<Interface<'a>> {
193 self.interfaces()
194 .find(|i| i.class() == class && i.sub_class() == sub_class && i.protocol() == protocol)
195 }
196}
197
198#[derive(Clone, Copy, PartialEq, Eq, Debug)]
202pub struct Interface<'a> {
203 raw: &'a [u8],
205 length: usize,
207}
208
209impl<'a> Interface<'a> {
210 fn parse(bytes: &'a [u8]) -> Option<Self> {
211 if bytes.len() < 9 || bytes[1] != descriptor_type::INTERFACE {
212 return None;
213 }
214 Some(Self {
215 raw: bytes,
216 length: bytes[0] as usize,
217 })
218 }
219
220 #[inline]
222 pub fn number(&self) -> u8 {
223 self.raw[2]
224 }
225
226 #[inline]
228 pub fn alternate_setting(&self) -> u8 {
229 self.raw[3]
230 }
231
232 #[inline]
235 pub fn endpoint_count(&self) -> u8 {
236 self.raw[4]
237 }
238
239 #[inline]
241 pub fn class(&self) -> u8 {
242 self.raw[5]
243 }
244
245 #[inline]
247 pub fn sub_class(&self) -> u8 {
248 self.raw[6]
249 }
250
251 #[inline]
253 pub fn protocol(&self) -> u8 {
254 self.raw[7]
255 }
256
257 #[inline]
262 pub fn endpoints(&self) -> Endpoints<'a> {
263 Endpoints {
264 rest: &self.raw[self.length.min(self.raw.len())..],
265 }
266 }
267}
268
269#[derive(Clone, Copy, Debug)]
271pub struct Interfaces<'a> {
272 raw: &'a [u8],
273 offset: usize,
274}
275
276impl<'a> Iterator for Interfaces<'a> {
277 type Item = Interface<'a>;
278
279 fn next(&mut self) -> Option<Self::Item> {
280 while self.offset + 2 <= self.raw.len() {
281 let start = self.offset;
282 let length = self.raw[start] as usize;
283 if length < 2 || start + length > self.raw.len() {
284 self.offset = self.raw.len();
285 return None;
286 }
287 self.offset += length;
288 if self.raw[start + 1] == descriptor_type::INTERFACE {
289 return Interface::parse(&self.raw[start..]);
292 }
293 }
294 None
295 }
296}
297
298#[derive(Clone, Copy, PartialEq, Eq, Debug)]
302#[cfg_attr(feature = "defmt", derive(defmt::Format))]
303pub struct EndpointDescriptor {
304 pub address: EndpointAddress,
306 pub attributes: u8,
308 pub max_packet: u16,
310 pub interval: u8,
312}
313
314impl EndpointDescriptor {
315 pub const LEN: usize = 7;
317
318 pub fn parse(bytes: &[u8]) -> HostResult<Self> {
320 if bytes.len() < Self::LEN || bytes[0] < Self::LEN as u8 {
321 return Err(HostError::Malformed);
322 }
323 if bytes[1] != descriptor_type::ENDPOINT {
324 return Err(HostError::Malformed);
325 }
326 Ok(Self {
327 address: EndpointAddress::from(bytes[2]),
328 attributes: bytes[3],
329 max_packet: le16(bytes, 4) & 0x07ff,
330 interval: bytes[6],
331 })
332 }
333
334 pub fn transfer_type(&self) -> EndpointType {
336 match self.attributes & 0x03 {
337 0 => EndpointType::Control,
338 1 => EndpointType::Isochronous {
339 synchronization: IsoSynchronization::None,
340 usage: IsoUsage::Data,
341 },
342 2 => EndpointType::Bulk,
343 _ => EndpointType::Interrupt,
344 }
345 }
346}
347
348#[derive(Clone, Copy, Debug)]
350pub struct Endpoints<'a> {
351 rest: &'a [u8],
352}
353
354impl Iterator for Endpoints<'_> {
355 type Item = EndpointDescriptor;
356
357 fn next(&mut self) -> Option<Self::Item> {
358 let length = *self.rest.first()? as usize;
359 if length < 2 || length > self.rest.len() || self.rest[1] != descriptor_type::ENDPOINT {
360 self.rest = &[];
362 return None;
363 }
364 let (head, tail) = self.rest.split_at(length);
365 self.rest = tail;
366 EndpointDescriptor::parse(head).ok()
367 }
368}