aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb/src/descriptor_reader.rs
blob: abb4b379efaa1770226d25ac20419089508143d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::descriptor::descriptor_type;
use crate::driver::EndpointAddress;
use crate::types::InterfaceNumber;

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ReadError;

pub struct Reader<'a> {
    data: &'a [u8],
}

impl<'a> Reader<'a> {
    pub const fn new(data: &'a [u8]) -> Self {
        Self { data }
    }

    pub const fn eof(&self) -> bool {
        self.data.is_empty()
    }

    pub fn read<const N: usize>(&mut self) -> Result<[u8; N], ReadError> {
        let n = self.data.get(0..N).ok_or(ReadError)?;
        self.data = &self.data[N..];
        Ok(n.try_into().unwrap())
    }

    pub fn read_u8(&mut self) -> Result<u8, ReadError> {
        Ok(u8::from_le_bytes(self.read()?))
    }
    pub fn read_u16(&mut self) -> Result<u16, ReadError> {
        Ok(u16::from_le_bytes(self.read()?))
    }

    pub fn read_slice(&mut self, len: usize) -> Result<&'a [u8], ReadError> {
        let res = self.data.get(0..len).ok_or(ReadError)?;
        self.data = &self.data[len..];
        Ok(res)
    }

    pub fn read_descriptors(&mut self) -> DescriptorIter<'_, 'a> {
        DescriptorIter { r: self }
    }
}

pub struct DescriptorIter<'a, 'b> {
    r: &'a mut Reader<'b>,
}

impl<'a, 'b> Iterator for DescriptorIter<'a, 'b> {
    type Item = Result<(u8, Reader<'a>), ReadError>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.r.eof() {
            return None;
        }

        let len = match self.r.read_u8() {
            Ok(x) => x,
            Err(e) => return Some(Err(e)),
        };
        let type_ = match self.r.read_u8() {
            Ok(x) => x,
            Err(e) => return Some(Err(e)),
        };
        let data = match self.r.read_slice(len as usize - 2) {
            Ok(x) => x,
            Err(e) => return Some(Err(e)),
        };

        Some(Ok((type_, Reader::new(data))))
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EndpointInfo {
    pub configuration: u8,
    pub interface: InterfaceNumber,
    pub interface_alt: u8,
    pub ep_address: EndpointAddress,
}

pub fn foreach_endpoint(data: &[u8], mut f: impl FnMut(EndpointInfo)) -> Result<(), ReadError> {
    let mut ep = EndpointInfo {
        configuration: 0,
        interface: InterfaceNumber(0),
        interface_alt: 0,
        ep_address: EndpointAddress::from(0),
    };
    for res in Reader::new(data).read_descriptors() {
        let (kind, mut r) = res?;
        match kind {
            descriptor_type::CONFIGURATION => {
                let _total_length = r.read_u16()?;
                let _total_length = r.read_u8()?;
                ep.configuration = r.read_u8()?;
            }
            descriptor_type::INTERFACE => {
                ep.interface = InterfaceNumber(r.read_u8()?);
                ep.interface_alt = r.read_u8()?;
            }
            descriptor_type::ENDPOINT => {
                ep.ep_address = EndpointAddress::from(r.read_u8()?);
                f(ep);
            }
            _ => {}
        }
    }
    Ok(())
}