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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use super::*;
#[derive(Clone)]
enum UsbHidKeyboardHandlerState {
Idle,
KeyDown,
}
#[derive(Clone)]
pub struct UsbHidKeyboardHandler {
pub report_descriptor: Vec<u8>,
pub pending_key_events: VecDeque<UsbHidKeyboardReport>,
state: UsbHidKeyboardHandlerState,
}
#[derive(Clone)]
pub struct UsbHidKeyboardReport {
pub modifier: u8,
pub keys: [u8; 6],
}
impl UsbHidKeyboardReport {
pub fn from_ascii(ascii: u8) -> UsbHidKeyboardReport {
let key = match ascii {
b'a'..=b'z' => ascii - b'a' + 4,
b'1'..=b'9' => ascii - b'1' + 30,
b'0' => 39,
b'\r' | b'\n' => 40,
_ => unimplemented!("Unrecognized ascii {}", ascii),
};
UsbHidKeyboardReport {
modifier: 0,
keys: [key, 0, 0, 0, 0, 0],
}
}
}
impl UsbHidKeyboardHandler {
pub fn new_keyboard() -> Self {
Self {
pending_key_events: VecDeque::new(),
state: UsbHidKeyboardHandlerState::Idle,
report_descriptor: vec![
0x05, 0x01,
0x09, 0x06,
0xA1, 0x01,
0x05, 0x07,
0x19, 0xE0,
0x29, 0xE7,
0x15, 0x00,
0x25, 0x01,
0x75, 0x01,
0x95, 0x08,
0x81, 0x02,
0x95, 0x01,
0x75, 0x08,
0x81, 0x01,
0x95, 0x06,
0x75, 0x08,
0x15, 0x00,
0x25, 0x65,
0x05, 0x07,
0x19, 0x00,
0x29, 0x65,
0x81, 0x00,
0xC0,
],
}
}
}
impl UsbInterfaceHandler for UsbHidKeyboardHandler {
fn handle_urb(
&mut self,
_interface: &UsbInterface,
ep: UsbEndpoint,
setup: SetupPacket,
_req: &[u8],
) -> Result<Vec<u8>> {
use StandardRequest::*;
if ep.is_ep0() {
match (setup.request_type, FromPrimitive::from_u8(setup.request)) {
(0b10000001, Some(GetDescriptor)) => {
match FromPrimitive::from_u16(setup.value >> 8) {
Some(HidDescriptorType::Report) => {
return Ok(self.report_descriptor.clone());
}
_ => unimplemented!("hid descriptor {:?}", setup),
}
}
_ => unimplemented!("hid request {:?}", setup),
}
} else {
if let Direction::In = ep.direction() {
match self.state {
UsbHidKeyboardHandlerState::Idle => {
if let Some(report) = self.pending_key_events.pop_front() {
let mut resp = vec![report.modifier, 0];
resp.extend_from_slice(&report.keys);
info!("HID key down");
self.state = UsbHidKeyboardHandlerState::KeyDown;
return Ok(resp);
}
}
UsbHidKeyboardHandlerState::KeyDown => {
let resp = vec![0; 6];
info!("HID key up");
self.state = UsbHidKeyboardHandlerState::Idle;
return Ok(resp);
}
}
}
}
Ok(vec![])
}
fn get_class_specific_descriptor(&self) -> Vec<u8> {
return vec![
0x09,
HidDescriptorType::Hid as u8,
0x11,
0x01,
0x00,
0x01,
HidDescriptorType::Report as u8,
self.report_descriptor.len() as u8,
(self.report_descriptor.len() >> 8) as u8,
];
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
}
#[derive(Copy, Clone, Debug, FromPrimitive)]
pub enum HidDescriptorType {
Hid = 0x21,
Report = 0x22,
Physical = 0x23,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn desc_verify() {
let handler = UsbHidKeyboardHandler::new_keyboard();
verify_descriptor(&handler.get_class_specific_descriptor());
}
}