-
Notifications
You must be signed in to change notification settings - Fork 151
/
WebUSB.cpp
358 lines (313 loc) · 9.6 KB
/
WebUSB.cpp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
Copyright (c) 2015, Arduino LLC
Original code (pre-library): Copyright (c) 2011, Peter Barrett
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
*/
#include "WebUSB.h"
WebUSB WebSerial;
const uint8_t BOS_DESCRIPTOR[57] PROGMEM = {
0x05, // Length
0x0F, // Binary Object Store descriptor
0x39, 0x00, // Total length
0x02, // Number of device capabilities
// WebUSB Platform Capability descriptor (bVendorCode == 0x01).
0x18, // Length
0x10, // Device Capability descriptor
0x05, // Platform Capability descriptor
0x00, // Reserved
0x38, 0xB6, 0x08, 0x34, 0xA9, 0x09, 0xA0, 0x47,
0x8B, 0xFD, 0xA0, 0x76, 0x88, 0x15, 0xB6, 0x65, // WebUSB GUID
0x00, 0x01, // Version 1.0
0x01, // Vendor request code
0x01, // Landing page
// Microsoft OS 2.0 Platform Capability Descriptor (MS_VendorCode == 0x02)
0x1C, // Length
0x10, // Device Capability descriptor
0x05, // Platform Capability descriptor
0x00, // Reserved
0xDF, 0x60, 0xDD, 0xD8, 0x89, 0x45, 0xC7, 0x4C,
0x9C, 0xD2, 0x65, 0x9D, 0x9E, 0x64, 0x8A, 0x9F, // MS OS 2.0 GUID
0x00, 0x00, 0x03, 0x06, // Windows version
0x2e, 0x00, // Descriptor set length
0x02, // Vendor request code
0x00 // Alternate enumeration code
};
// This descriptor is also used as the landing page which is why it is more than
// just an origin.
const uint8_t WEBUSB_ORIGIN_1[35] PROGMEM = {
0x23, // Length
0x03, // URL descriptor
0x01, // https://
'w', 'e', 'b', 'u', 's', 'b', '.', 'g', 'i', 't', 'h', 'u', 'b', '.', 'i', 'o',
'/', 'a', 'r', 'd', 'u', 'i', 'n', 'o', '/', 'd', 'e', 'm', 'o', 's', '/'
};
const uint8_t WEBUSB_ORIGIN_2[17] PROGMEM = {
0x11, // Length
0x03, // URL descriptor
0x00, // http://
'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't', ':', '8', '0', '0', '0'
};
const uint8_t MS_OS_20_DESCRIPTOR_SET[] PROGMEM = {
0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x06,
};
typedef struct
{
u32 dwDTERate;
u8 bCharFormat;
u8 bParityType;
u8 bDataBits;
u8 lineState;
} LineInfo;
static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
static volatile int32_t breakValue = -1;
int WebUSB::getInterface(uint8_t* interfaceCount)
{
*interfaceCount += 1; // uses 1 interface
WebUSBDescriptor webUSBInterface = {
D_INTERFACE(pluggedInterface, 2, 0xff, 0, 0),
D_ENDPOINT(USB_ENDPOINT_OUT(pluggedEndpoint),USB_ENDPOINT_TYPE_BULK,0x40,0),
D_ENDPOINT(USB_ENDPOINT_IN (pluggedEndpoint+1),USB_ENDPOINT_TYPE_BULK,0x40,0)
};
return USB_SendControl(0, &webUSBInterface, sizeof(webUSBInterface));
}
int WebUSB::getDescriptor(USBSetup& setup)
{
if (USB_BOS_DESCRIPTOR_TYPE == setup.wValueH)
{
if (setup.wValueL == 0 && setup.wIndex == 0) {
return USB_SendControl(TRANSFER_PGM, &BOS_DESCRIPTOR, sizeof(BOS_DESCRIPTOR));
}
}
return 0;
}
uint8_t WebUSB::getShortName(char* name)
{
memcpy(name, "WUART", 5);
return 5;
}
bool WebUSB::VendorControlRequest(USBSetup& setup)
{
if (setup.bmRequestType == (REQUEST_DEVICETOHOST | REQUEST_VENDOR | REQUEST_DEVICE)) {
if (setup.bRequest == 0x01 && setup.wIndex == WEBUSB_REQUEST_GET_ALLOWED_ORIGINS)
{
uint8_t allowedOrigins[] = {
// Allowed Origins Header, bNumConfigurations = 1
0x05, 0x00, 0x0e, 0x00, 0x01,
// Configuration Subset Header, bNumFunctions = 1
0x04, 0x01, 0x01, 0x01,
// Function Subset Header, bFirstInterface = pluggedInterface
0x05, 0x02, pluggedInterface, 0x01, 0x02
};
USB_SendControl(0, &allowedOrigins, sizeof(allowedOrigins));
return true;
}
else if (setup.bRequest == 0x01 && setup.wIndex == WEBUSB_REQUEST_GET_URL)
{
if (setup.wValueL == 1)
{
USB_SendControl(TRANSFER_PGM, &WEBUSB_ORIGIN_1, sizeof(WEBUSB_ORIGIN_1));
return true;
}
else if (setup.wValueL == 2)
{
USB_SendControl(TRANSFER_PGM, &WEBUSB_ORIGIN_2, sizeof(WEBUSB_ORIGIN_2));
return true;
}
}
else if (setup.bRequest == 0x02 && setup.wIndex == MS_OS_20_REQUEST_DESCRIPTOR)
{
uint8_t osDescriptor[] = {
// Microsoft OS 2.0 descriptor set header (table 10)
0x0A, 0x00, // Descriptor size (10 bytes)
0x00, 0x00, // MS OS 2.0 descriptor set header
0x00, 0x00, 0x03, 0x06, // Windows version (8.1) (0x06030000)
0x2e, 0x00, // Size, MS OS 2.0 descriptor set
// Microsoft OS 2.0 configuration subset header
0x08, 0x00, // Descriptor size (8 bytes)
0x01, 0x00, // MS OS 2.0 configuration subset header
0x00, // bConfigurationValue
0x00, // Reserved
0x24, 0x00, // Size, MS OS 2.0 configuration subset
// Microsoft OS 2.0 function subset header
0x08, 0x00, // Descriptor size (8 bytes)
0x02, 0x00, // MS OS 2.0 function subset header
pluggedInterface, // First interface
0x00, // Reserved
0x1c, 0x00, // Size, MS OS 2.0 function subset
// Microsoft OS 2.0 compatible ID descriptor (table 13)
0x14, 0x00, // wLength
0x03, 0x00, // MS_OS_20_FEATURE_COMPATIBLE_ID
'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
USB_SendControl(0, &osDescriptor, sizeof(osDescriptor));
return true;
}
}
return false;
}
bool WebUSB::setup(USBSetup& setup)
{
uint8_t r = setup.bRequest;
uint8_t requestType = setup.bmRequestType;
if (REQUEST_CLASS == (requestType & REQUEST_TYPE) && (pluggedInterface == setup.wIndex)) {
if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
{
if (CDC_GET_LINE_CODING == r)
{
USB_SendControl(0,(void*)&_usbLineInfo,7);
return true;
}
}
if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
{
if (CDC_SEND_BREAK == r)
{
breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL;
}
if (CDC_SET_LINE_CODING == r)
{
USB_RecvControl((void*)&_usbLineInfo,7);
}
if (CDC_SET_CONTROL_LINE_STATE == r)
{
_usbLineInfo.lineState = setup.wValueL;
}
return true;
}
} else if (REQUEST_VENDOR == (requestType & REQUEST_TYPE)) {
return VendorControlRequest(setup);
}
return false;
}
WebUSB::WebUSB(void) : PluggableUSBModule(2, 1, epType)
{
// one interface, 2 endpoints
epType[0] = EP_TYPE_BULK_OUT;
epType[1] = EP_TYPE_BULK_IN;
PluggableUSB().plug(this);
}
void WebUSB::begin(unsigned long /* baud_count */)
{
peek_buffer = -1;
}
void WebUSB::begin(unsigned long /* baud_count */, byte /* config */)
{
peek_buffer = -1;
}
void WebUSB::end(void)
{
}
int WebUSB::available(void)
{
if (peek_buffer >= 0) {
return 1 + USB_Available(pluggedEndpoint);
}
return USB_Available(pluggedEndpoint);
}
int WebUSB::peek(void)
{
if (peek_buffer < 0)
peek_buffer = USB_Recv(pluggedEndpoint);
return peek_buffer;
}
int WebUSB::read(void)
{
if (peek_buffer >= 0) {
int c = peek_buffer;
peek_buffer = -1;
return c;
}
return USB_Recv(pluggedEndpoint);
}
int WebUSB::availableForWrite(void)
{
return USB_SendSpace(pluggedEndpoint+1);
}
void WebUSB::flush(void)
{
USB_Flush(pluggedEndpoint+1);
}
size_t WebUSB::write(uint8_t c)
{
return write(&c, 1);
}
size_t WebUSB::write(const uint8_t *buffer, size_t size)
{
/* only try to send bytes if the high-level CDC connection itself
is open (not just the pipe) - the OS should set lineState when the port
is opened and clear lineState when the port is closed.
bytes sent before the user opens the connection or after
the connection is closed are lost - just like with a UART. */
// TODO - ZE - check behavior on different OSes and test what happens if an
// open connection isn't broken cleanly (cable is yanked out, host dies
// or locks up, or host virtual serial port hangs)
if (_usbLineInfo.lineState > 0) {
int r = USB_Send(pluggedEndpoint+1,buffer,size);
if (r > 0) {
return r;
} else {
setWriteError();
return 0;
}
}
setWriteError();
return 0;
}
// This operator is a convenient way for a sketch to check whether the
// port has actually been configured and opened by the host (as opposed
// to just being connected to the host). It can be used, for example, in
// setup() before printing to ensure that an application on the host is
// actually ready to receive and display the data.
// We add a short delay before returning to fix a bug observed by Federico
// where the port is configured (lineState != 0) but not quite opened.
WebUSB::operator bool() {
bool result = false;
if (_usbLineInfo.lineState > 0)
result = true;
delay(10);
return result;
}
unsigned long WebUSB::baud() {
// Disable interrupts while reading a multi-byte value
uint32_t baudrate;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
baudrate = _usbLineInfo.dwDTERate;
}
return baudrate;
}
uint8_t WebUSB::stopbits() {
return _usbLineInfo.bCharFormat;
}
uint8_t WebUSB::paritytype() {
return _usbLineInfo.bParityType;
}
uint8_t WebUSB::numbits() {
return _usbLineInfo.bDataBits;
}
bool WebUSB::dtr() {
return _usbLineInfo.lineState & 0x1;
}
bool WebUSB::rts() {
return _usbLineInfo.lineState & 0x2;
}
int32_t WebUSB::readBreak() {
int32_t ret;
// Disable IRQs while reading and clearing breakValue to make
// sure we don't overwrite a value just set by the ISR.
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
ret = breakValue;
breakValue = -1;
}
return ret;
}