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
//! Device descriptors
use core::num::NonZeroU8;
use crate::desc;
/// Device descriptor
pub struct Descriptor {
// pub blength: u8,
// pub bDescriptorType: u8,
// pub bcdUSB: u16,
/// Device class
pub bDeviceClass: u8,
/// Device subclass
pub bDeviceSubClass: u8,
/// Device protocol
pub bDeviceProtocol: u8,
/// Maximum packet size
pub bMaxPacketSize0: bMaxPacketSize0,
/// Vendor ID
pub idVendor: u16,
/// Product ID
pub idProduct: u16,
/// Device release number
pub bcdDevice: u16,
/// Manufacturer string index
pub iManufacturer: Option<NonZeroU8>,
/// Product string index
pub iProduct: Option<NonZeroU8>,
/// Serial number string index
pub iSerialNumber: Option<NonZeroU8>,
/// Number of configurations
pub bNumConfigurations: NonZeroU8,
}
#[allow(non_upper_case_globals)]
const bcdUSB: u16 = 0x0200; // 2.0
/// Maximum packet size
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub enum bMaxPacketSize0 {
/// 8 bytes
B8 = 8,
/// 16 bytes
B16 = 16,
/// 32 bytes
B32 = 32,
/// 64 bytes
B64 = 64,
}
impl Descriptor {
/// The size of this descriptor on the wire
pub const SIZE: u8 = 18;
/// Returns the wire representation of this device endpoint
pub fn bytes(&self) -> [u8; Self::SIZE as usize] {
[
Self::SIZE,
desc::Type::Device as u8,
bcdUSB as u8,
(bcdUSB >> 8) as u8,
self.bDeviceClass,
self.bDeviceSubClass,
self.bDeviceProtocol,
self.bMaxPacketSize0 as u8,
self.idVendor as u8,
(self.idVendor >> 8) as u8,
self.idProduct as u8,
(self.idProduct >> 8) as u8,
self.bcdDevice as u8,
(self.bcdDevice >> 8) as u8,
self.iManufacturer.map(|nz| nz.get()).unwrap_or(0),
self.iProduct.map(|nz| nz.get()).unwrap_or(0),
self.iSerialNumber.map(|nz| nz.get()).unwrap_or(0),
self.bNumConfigurations.get(),
]
}
}