usb_descriptor_decoder/descriptors/
desc_device.rs

1use num_derive::FromPrimitive;
2
3#[derive(Copy, Clone, Default, Debug)]
4#[repr(C, packed)]
5pub struct Device {
6    pub len: u8,
7    pub descriptor_type: u8,
8    pub cd_usb: u16,
9    pub class: u8,
10    pub subclass: u8,
11    pub protocol: u8,
12    pub max_packet_size0: u8,
13    pub vendor: u16,
14    pub product_id: u16,
15    pub device: u16,
16    pub manufacture: u8,
17    pub product: u8,
18    pub serial_number: u8,
19    pub num_configurations: u8,
20}
21impl Device {
22    pub fn max_packet_size(&self) -> u16 {
23        if let (3, _) = self.version() {
24            2_u16.pow(self.max_packet_size0.into())
25        } else {
26            self.max_packet_size0.into()
27        }
28    }
29
30    fn version(&self) -> (u8, u8) {
31        let cd_usb = self.cd_usb;
32
33        (
34            (cd_usb >> 8).try_into().unwrap(),
35            (cd_usb & 0xff).try_into().unwrap(),
36        )
37    }
38
39    pub fn is_refer_interface(&self) -> bool {
40        self.class == StandardUSBDeviceClassCode::ReferInterfaceDescriptor as u8
41            && self.subclass == StandardUSBDeviceClassCode::ReferInterfaceDescriptor as u8
42            && self.protocol == 0
43    }
44}
45
46#[derive(FromPrimitive, Copy, Clone, Debug)]
47#[repr(u8)]
48pub enum StandardUSBDeviceClassCode {
49    ReferInterfaceDescriptor = 0x00,
50    Audio = 0x01,
51    CommunicationsAndCDCControl = 0x02,
52    HID = 0x03,
53    Physical = 0x05,
54    Image = 0x06,
55    Printer = 0x07,
56    MassStorage = 0x08,
57    Hub = 0x09,
58    CDCData = 0x0A,
59    SmartCard = 0x0B,
60    ContentSecurity = 0x0D,
61    Video = 0x0E,
62    PersonalHealthcare = 0x0F,
63    AudioVideoDevices = 0x10,
64    BillboardDeviceClass = 0x11,
65    USBTypeCBridge = 0x12,
66    DiagnosticDevice = 0xDC,
67    WirelessController = 0xE0,
68    Miscellaneous = 0xEF,
69    ApplicationSpecific = 0xFE,
70    VendorSpecific = 0xFF,
71}
72
73// #[derive(Copy, Clone, Debug, FromPrimitive)]
74// #[repr(u8)]
75// pub enum StandardUSBDeviceSubClassCode {
76//     Common = 0x02,
77//     Any = 0x03,
78// }
79
80// #[derive(FromPrimitive, Copy, Clone, Debug)]
81// #[repr(u8)]
82// pub enum StandardUSBDeviceProtocol {
83//     ReferInterfaceAssociationDescriptor = 0x01,
84// }