Skip to content

Commit 39c9d16

Browse files
author
ferris
committed
Add iter_descriptors as proposed by @AlexCharlton & deprecate parse_custom_descriptor in favour of iter_descriptors
1 parent c463b80 commit 39c9d16

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

embassy-usb/src/host/descriptor.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@ impl<'a> ConfigurationDescriptor<'a> {
224224
})
225225
}
226226

227+
/// Iterate over all (raw) descriptors contained in this Configuration
228+
pub fn iter_descriptors(&self) -> RawDescriptorIterator<'_> {
229+
RawDescriptorIterator {
230+
buf: self.buffer,
231+
offset: 0,
232+
}
233+
}
234+
227235
/// Iterate over all interface descriptors of this Configuration
228236
pub fn iter_interface(&self) -> InterfaceIterator<'_> {
229237
InterfaceIterator {
@@ -398,10 +406,19 @@ impl<'a> InterfaceDescriptor<'a> {
398406
}
399407

400408
/// Try to parse a class descriptor of a given type
409+
#[deprecated(note = "Use `iter_descriptors()` with `filter_map`/`find_map` instead")]
401410
pub fn parse_class_descriptor<T: USBDescriptor>(&self) -> Option<T> {
402411
Self::identify_descriptor::<T>(self.buffer).and_then(|i| T::try_from_bytes(&self.buffer[i..]).ok())
403412
}
404413

414+
/// Iterate over (raw) descriptors in this Interface
415+
pub fn iter_descriptors(&self) -> RawDescriptorIterator<'_> {
416+
RawDescriptorIterator {
417+
buf: self.buffer,
418+
offset: 0,
419+
}
420+
}
421+
405422
/// Iterate over endpoints
406423
pub fn iter_endpoints(&'a self) -> EndpointIterator<'a> {
407424
EndpointIterator {
@@ -433,6 +450,7 @@ impl<'a> InterfaceDescriptor<'a> {
433450
}
434451

435452
// Returns the offset to the first matching descriptor in the slice
453+
#[deprecated(note = "Use `iter_endpoints()` or `iter_descriptors()` instead")]
436454
fn identify_descriptor<T: USBDescriptor>(slice: &[u8]) -> Option<usize> {
437455
let mut offset = 0;
438456
let mut desc_len = slice[offset] as usize;
@@ -695,7 +713,10 @@ mod test {
695713
let interface0 = cfg.iter_interface().next().unwrap();
696714
assert_eq!(interface0.interface_number, 0);
697715

698-
let hid_desc: HIDDescriptor = interface0.parse_class_descriptor().unwrap();
716+
let hid_desc: HIDDescriptor = interface0
717+
.iter_descriptors()
718+
.find_map(|v| HIDDescriptor::try_from_bytes(v.1).ok())
719+
.unwrap();
699720

700721
assert_eq!(hid_desc.len, 9);
701722
assert_eq!(hid_desc.descriptor_type, 33);

0 commit comments

Comments
 (0)