forked from thevoidnn/esp8266-wifi-cmsis-dap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathesp8266-wifi-cmsis-dap.ino
906 lines (661 loc) · 24.3 KB
/
esp8266-wifi-cmsis-dap.ino
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/* CMSIS-DAP ported to run on the Pro Micro and Teensy 3.2
* Copyright (C) 2016 Phillip Pearson <[email protected]>
*
* CMSIS-DAP Interface Firmware
* Copyright (c) 2009-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* *******************************************************************
*
SWD PINOUT:
SWD_CLOCK: D3 (0)
SWD_DATA: D4 (2)
SWD_VDD: 3V
SWD_GND: GND
(see DAP_config.h line 127)
*/
#include <ESP8266WiFi.h>
#define htons(x) ( ((x)<< 8 & 0xFF00) | \
((x)>> 8 & 0x00FF) )
#define ntohs(x) htons(x)
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
((x)<< 8 & 0x00FF0000UL) | \
((x)>> 8 & 0x0000FF00UL) | \
((x)>>24 & 0x000000FFUL) )
#define ntohl(x) htonl(x)
#define USB_DEVICE_VENDOR_ID 0x16c0
#define USB_DEVICE_PRODUCT_ID 0x05df
#define USB_DEVICE_VERSION 0x0130
#define USB_HID_MAX_PACKET_SIZE 64
#include "DAP_config.h"
#include "DAP.h"
#include "usb_defs.h"
#include "usbip_defs.h"
// wifi setup:
const char* ssid = "<your SSID>";
const char* pass = "<your password>";
const char *strings[] = {
0, // reserved: available languages
"thevoidnn",
"esp8266 CMSIS-DAP",
"1234",
};
const uint8_t hid_report_descriptor[] = {
0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00)
0x09, 0x01, // Usage (0x01)
0xA1, 0x01, // Collection (Application)
0x15, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x00, // Logical Maximum (255)
0x75, 0x08, // Report Size (8)
0x95, 0x40, // Report Count (64)
0x09, 0x01, // Usage (0x01)
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
0x95, 0x40, // Report Count (64)
0x09, 0x01, // Usage (0x01)
0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0x95, 0x01, // Report Count (1)
0x09, 0x01, // Usage (0x01)
0xB1, 0x02, // Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
0xC0, // End Collection
// 33 bytes
};
#define STR_ILANGUAGES_SUPPORTED 0
#define STR_IMANUFACTURER 1
#define STR_IPRODUCT 2
#define STR_ISERIAL 3
#define USBIP_SERVER_PORT 3240
void dump_buff(void *data, int size) {
Serial.println("****************************");
uint8_t *buffer = (uint8_t *)data;
for (int i = 0; i < size; i++) {
if (i != 0 && i % 16 == 0) {
Serial.println();
}
unsigned int val = buffer[i];
Serial.print(val, HEX);
Serial.print(' ');
}
Serial.println();
Serial.println("****************************");
Serial.println();
}
class USBIPServer {
public:
enum state_t {
ACCEPTING,
ATTACHING,
EMULATING,
};
typedef int (*callback_t)(void *data_in, void *data_out);
USBIPServer(callback_t on_hid_data)
: respond(false)
, state(ACCEPTING)
, on_hid_data(on_hid_data)
, wifiServer(USBIP_SERVER_PORT)
{
memset(data_in, 0, USB_HID_MAX_PACKET_SIZE);
memset(data_out, 0, USB_HID_MAX_PACKET_SIZE);
}
void begin() {
wifiServer.begin();
wifiServer.setNoDelay(true);
}
void loop() {
//Serial.println("loop");
switch (state) {
case ACCEPTING:
accept();
break;
case ATTACHING:
attach();
break;
case EMULATING:
emulate();
break;
}
if (!client.connected()) {
state = ACCEPTING;
}
}
void accept() {
if (wifiServer.hasClient()) {
client = wifiServer.available();
state = ATTACHING;
}
}
void attach() {
int command = read_stage1_command();
if (command < 0) {
return;
}
switch (command) {
case USBIP_STAGE1_CMD_DEVICE_LIST:
handle_device_list();
break;
case USBIP_STAGE1_CMD_DEVICE_ATTACH:
handle_device_attach();
break;
default:
Serial.println("s1 unknown command:");
Serial.println(command);
break;
}
}
void emulate() {
usbip_stage2_header header;
int command = read_stage2_command(header);
if (command < 0) {
return;
}
switch (command) {
case USBIP_STAGE2_REQ_SUBMIT:
handle_submit(header);
break;
case USBIP_STAGE2_REQ_UNLINK:
handle_unlink(header);
break;
default:
Serial.println("s2 unknown command:");
Serial.println(command);
}
}
protected:
void write(void *data, int size) {
client.write((uint8_t *)data, size);
}
void send_stage2_submit(usbip_stage2_header &req_header, int32_t status, int32_t data_length) {
req_header.base.command = USBIP_STAGE2_RSP_SUBMIT;
req_header.base.direction = !req_header.base.direction;
memset(&req_header.u.ret_submit, 0, sizeof(usbip_stage2_header_ret_submit));
req_header.u.ret_submit.status = status;
req_header.u.ret_submit.data_length = data_length;
pack(&req_header, sizeof(usbip_stage2_header));
write(&req_header, sizeof(usbip_stage2_header));
}
void send_stage2_submit_data(usbip_stage2_header &req_header, int32_t status, void *data, int32_t data_length) {
send_stage2_submit(req_header, status, data_length);
if (data_length) {
write(data, data_length);
}
}
void send_stage2_unlink(usbip_stage2_header &req_header) {
req_header.base.command = USBIP_STAGE2_RSP_UNLINK;
req_header.base.direction = USBIP_DIR_OUT;
memset(&req_header.u.ret_unlink, 0, sizeof(usbip_stage2_header_ret_unlink));
// req_header.u.ret_unlink.status = 0;
pack(&req_header, sizeof(usbip_stage2_header));
write(&req_header, sizeof(usbip_stage2_header));
}
void send_stage1_header(uint16_t command, uint32_t status) {
Serial.println("s1 sending header...");
usbip_stage1_header header;
header.version = htons(273);
header.command = htons(command);
header.status = htonl(status);
write(&header, sizeof(usbip_stage1_header));
}
void send_device_list() {
Serial.println("s1 sending device list...");
// send device list size:
Serial.println("s1 sending device list size...");
usbip_stage1_response_devlist response_devlist;
// we have only 1 device, so:
response_devlist.list_size = htonl(1);
write(&response_devlist, sizeof(usbip_stage1_response_devlist));
// may be foreach:
{
// send device info:
send_device_info();
// send device interfaces: // (1)
send_interface_info();
}
}
void send_device_info() {
Serial.println("s1 sending device info...");
usbip_stage1_usb_device device;
strcpy(device.path, "/sys/devices/pci0000:00/0000:00:01.2/usb1/1-1");
strcpy(device.busid, "1-1");
device.busnum = htonl(1);
device.devnum = htonl(2);
device.speed = htonl(2);
// see: https://github.com/obdev/v-usb/blob/master/usbdrv/USB-IDs-for-free.txt
device.idVendor = htons(USB_DEVICE_VENDOR_ID);
device.idProduct = htons(USB_DEVICE_PRODUCT_ID);
// device version:
device.bcdDevice = htons(USB_DEVICE_VERSION);
device.bDeviceClass = 0x00; // <defined at interface level>
device.bDeviceSubClass = 0x00;
device.bDeviceProtocol = 0x00;
device.bConfigurationValue = 1;
device.bNumConfigurations = 1;
device.bNumInterfaces = 1;
write(&device, sizeof(usbip_stage1_usb_device));
}
void send_interface_info() {
Serial.println("sending interface info...");
usbip_stage1_usb_interface interface;
interface.bInterfaceClass = USB_CLASS_HID;
interface.bInterfaceSubClass = 0;
interface.bInterfaceProtocol = 0;
interface.padding = 0;
write(&interface, sizeof(usbip_stage1_usb_interface));
}
int read_stage1_command() {
if (client.available() < sizeof(usbip_stage1_header)) {
return -1;
}
usbip_stage1_header req;
client.readBytes((uint8_t *)&req, sizeof(usbip_stage1_header));
return ntohs(req.command) & 0xFF; // 0x80xx
}
int read_stage2_command(usbip_stage2_header &header) {
if (client.available() < sizeof(usbip_stage2_header)) {
return -1;
}
client.readBytes((uint8_t *)&header, sizeof(usbip_stage2_header));
unpack((uint32_t *) &header, sizeof(usbip_stage2_header));
return header.base.command;
}
void pack(void* data, int size) {
// don't fk with setup bytes
int sz = (size/sizeof(uint32_t)) - 2;
uint32_t *ptr = (uint32_t *)data;
for(int i = 0; i < sz; i++) {
//Serial.print(i);
ptr[i]=htonl(ptr[i]);
}
}
void unpack(void* data, int size) {
// don't fk with setup bytes
int sz = (size/sizeof(uint32_t)) - 2;
uint32_t *ptr = (uint32_t *)data;
for(int i = 0; i < sz; i++) {
ptr[i]=ntohl(ptr[i]);
}
}
// handlers:
// stage 1:
void handle_device_list() {
Serial.println("s1 handling dev list request...");
send_stage1_header(USBIP_STAGE1_CMD_DEVICE_LIST, 0);
send_device_list();
}
void handle_device_attach() {
Serial.println("s1 handling dev attach request...");
char bus[USBIP_BUSID_SIZE];
while (client.available() < USBIP_BUSID_SIZE) {
// nop
}
client.readBytes((uint8_t *)bus, USBIP_BUSID_SIZE);
send_stage1_header(USBIP_STAGE1_CMD_DEVICE_ATTACH, 0);
send_device_info();
state = EMULATING;
}
// stage 2:
void handle_submit(usbip_stage2_header &header) {
//dump_cmd_submit(header);
switch (header.base.ep) {
// control
case 0x00:
handle_control_request(header);
break;
// data
case 0x01:
if (header.base.direction == 0) {
// Serial.println("EP 01 DATA FROM HOST");
handle_data_request(header);
} else {
// Serial.println("EP 01 DATA TO HOST");
handle_data_response(header);
}
break;
// request to save data to device
case 0x81:
if (header.base.direction == 0) {
Serial.println("*** WARN! EP 81 DATA TX");
} else {
Serial.println("*** WARN! EP 81 DATA RX");
}
break;
default:
Serial.print("*** WARN ! UNKNOWN ENDPOINT: ");
Serial.println((int)header.base.ep);
//usleep(10000000);
}
}
void handle_unlink(usbip_stage2_header &header) {
Serial.println("s2 handling cmd unlink...");
send_stage2_unlink(header);
}
// stage 2 helpers:
void handle_control_request(usbip_stage2_header &header) {
switch (header.u.cmd_submit.request.bmRequestType) {
case 0x80: // : default
case 0x81: // : hid
switch (header.u.cmd_submit.request.bRequest) {
case 0x06: // GET DESCRIPTOR
handle_get_descriptor(header);
break;
default:
Serial.println("usb unknown request:");
Serial.println(header.u.cmd_submit.request.bRequest);
break;
}
break;
case 0x00:
switch (header.u.cmd_submit.request.bRequest) {
case 0x09: // SET CONFIGURATION
handle_set_configuration(header);
break;
default:
Serial.println("usb unknown request:");
Serial.println(header.u.cmd_submit.request.bRequest);
break;
}
break;
case 0x21:
switch (header.u.cmd_submit.request.bRequest) {
case 0x0a:
handle_set_idle(header);
break;
default:
Serial.println("usb unknown request:");
Serial.println(header.u.cmd_submit.request.bRequest);
break;
}
break;
default:
Serial.println("usb unknown request type:");
Serial.println(header.u.cmd_submit.request.bmRequestType);
break;
}
}
void handle_data_request(usbip_stage2_header &header) {
while (client.available() < USB_HID_MAX_PACKET_SIZE) {
// nop
}
client.readBytes(data_in, USB_HID_MAX_PACKET_SIZE);
respond = on_hid_data(data_in, data_out);
send_stage2_submit(header, 0, 0);
}
void handle_data_response(usbip_stage2_header &header) {
if (respond) {
respond = false;
//Serial.println("*** Will respond");
send_stage2_submit_data(header, 0, data_out, USB_HID_MAX_PACKET_SIZE);
//Serial.println("*** RESPONDED ***");
} else {
//Serial.println("*** Will NOT respond");
send_stage2_submit(header, 0, 0);
}
}
void handle_get_descriptor(usbip_stage2_header &header) {
switch (header.u.cmd_submit.request.wValue.u8hi) {
case USB_DT_DEVICE:
handle_get_device_descriptor(header);
break;
case USB_DT_CONFIGURATION:
handle_get_configuration_descriptor(header);
break;
case USB_DT_STRING:
handle_get_string_descriptor(header);
break;
case USB_DT_INTERFACE:
handle_get_interface_descriptor(header);
break;
case USB_DT_ENDPOINT:
handle_get_endpoint_descriptor(header);
break;
case USB_DT_DEVICE_QUALIFIER:
handle_get_device_qualifier_descriptor(header);
break;
case USB_DT_OTHER_SPEED_CONFIGURATION:
Serial.println("GET 0x07 [UNIMPLEMENTED] USB_DT_OTHER_SPEED_CONFIGURATION");
break;
case USB_DT_INTERFACE_POWER:
Serial.println("GET 0x08 [UNIMPLEMENTED] USB_DT_INTERFACE_POWER");
break;
case USB_DT_REPORT:
handle_get_hid_report_descriptor(header);
break;
default:
Serial.println("usb unknown descriptor requested:");
Serial.println(header.u.cmd_submit.request.wValue.u8lo);
break;
}
}
void handle_get_device_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x01 DEVICE DESCRIPTOR");
usb_device_descriptor desc;
desc.bLength = USB_DT_DEVICE_SIZE;
desc.bDescriptorType = USB_DT_DEVICE;
desc.bcdUSB = 0x0110;
// defined at interface level
desc.bDeviceClass = 0x0;
desc.bDeviceSubClass = 0x0;
desc.bDeviceProtocol = 0x0;
desc.bMaxPacketSize0 = USB_HID_MAX_PACKET_SIZE;
desc.idVendor = USB_DEVICE_VENDOR_ID;
desc.idProduct = USB_DEVICE_PRODUCT_ID;
desc.bcdDevice = USB_DEVICE_VERSION;
desc.iManufacturer = STR_IMANUFACTURER;
desc.iProduct = STR_IPRODUCT;
desc.iSerialNumber = STR_ISERIAL;
desc.bNumConfigurations = 1;
send_stage2_submit_data(header, 0, &desc, sizeof(usb_device_descriptor));
}
void handle_get_configuration_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x02 CONFIGURATION DESCRIPTOR");
if (header.u.cmd_submit.data_length == USB_DT_CONFIGURATION_SIZE) {
Serial.println("Sending only first part of CONFIG");
// USB_DT_CONFIGURATION_SIZE + USB_DT_INTERFACE_SIZE + USB_DT_HID_SIZE + USB_DT_REPORT_SIZE + (2 * USB_DT_ENDPOINT_SIZE)
send_stage2_submit(header, 0, header.u.cmd_submit.data_length);
send_stage2_configuration_descriptor();
} else {
Serial.println("Sending ALL CONFIG");
// USB_DT_CONFIGURATION_SIZE + USB_DT_INTERFACE_SIZE + USB_DT_HID_SIZE + USB_DT_REPORT_SIZE + (2 * USB_DT_ENDPOINT_SIZE)
send_stage2_submit(header, 0, header.u.cmd_submit.data_length);
send_stage2_configuration_descriptor();
send_stage2_interface_descriptor();
send_stage2_hid_descriptor();
send_stage2_endpoint_descriptors();
}
}
void handle_get_string_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x03 STRING DESCRIPTOR");
if (header.u.cmd_submit.request.wValue.u8lo == 0) {
Serial.println("** REQUESTED list of supported languages");
uint8_t lng[4];
usb_string_descriptor *lng_desc = (usb_string_descriptor *)lng;
lng_desc->bLength = 0x04;
lng_desc->bDescriptorType = USB_DT_STRING;
lng_desc->wData[0] = 0x0409;
send_stage2_submit_data(header, 0, lng, 0x04);
return;
}
Serial.println("** REQUESTED STRING ID:");
Serial.println(header.u.cmd_submit.request.wValue.u8lo);
Serial.println("** SENDING STRING:");
Serial.println(strings[header.u.cmd_submit.request.wValue.u8lo]);
int slen = strlen(strings[header.u.cmd_submit.request.wValue.u8lo]);
int wslen = slen * 2;
int buff_len = sizeof(usb_string_descriptor) + wslen;
uint8_t *buff = new uint8_t[buff_len];
usb_string_descriptor *desc = (usb_string_descriptor *)buff;
desc->bLength = buff_len;
desc->bDescriptorType = USB_DT_STRING;
for (int i = 0; i < slen; i++) {
desc->wData[i] = strings[header.u.cmd_submit.request.wValue.u8lo][i];
}
send_stage2_submit_data(header, 0, buff, buff_len);
delete[] buff;
}
void handle_get_interface_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x04 INTERFACE DESCRIPTOR (UNIMPLEMENTED)");
send_stage2_submit(header, 0, 0);
}
void handle_get_endpoint_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x05 ENDPOINT DESCRIPTOR (UNIMPLEMENTED)");
}
void handle_get_device_qualifier_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x06 DEVICE QUALIFIER DESCRIPTOR");
usb_device_qualifier_descriptor desc;
memset(&desc, 0, sizeof(usb_device_qualifier_descriptor));
send_stage2_submit_data(header, 0, &desc, sizeof(usb_device_qualifier_descriptor));
}
void handle_get_hid_report_descriptor(usbip_stage2_header &header) {
Serial.println("* GET 0x22 HID REPORT DESCRIPTOR");
send_stage2_submit_data(header, 0, (void *)hid_report_descriptor, sizeof(hid_report_descriptor));
}
void handle_set_configuration(usbip_stage2_header &header) {
Serial.println("* SET CONFIGURATION");
send_stage2_submit_data(header, 0, 0, 0);
}
void handle_set_idle(usbip_stage2_header &header) {
Serial.println("* SET IDLE");
send_stage2_submit(header, 0, 0);
}
void send_stage2_configuration_descriptor() {
usb_config_descriptor desc;
desc.bLength = USB_DT_CONFIGURATION_SIZE;
desc.bDescriptorType = USB_DT_CONFIGURATION;
desc.wTotalLength = USB_DT_CONFIGURATION_SIZE + USB_DT_INTERFACE_SIZE + USB_DT_HID_SIZE + USB_DT_REPORT_SIZE + (2 * USB_DT_ENDPOINT_SIZE);
desc.bNumInterfaces = 1;
desc.bConfigurationValue = 1;
desc.iConfiguration = 0;
desc.bmAttributes = 0x80;// 0xC0; ?
desc.bMaxPower = 0x50;
write(&desc, USB_DT_CONFIGURATION_SIZE);
}
void send_stage2_interface_descriptor() {
usb_interface_descriptor desc;
desc.bLength = USB_DT_INTERFACE_SIZE;
desc.bDescriptorType = USB_DT_INTERFACE;
desc.bInterfaceNumber = 0;
desc.bAlternateSetting = 0;
desc.bNumEndpoints = 2;
desc.bInterfaceClass = USB_CLASS_HID;
desc.bInterfaceSubClass = 0;
desc.bInterfaceProtocol = 0;
desc.iInterface = 0;
write(&desc, USB_DT_INTERFACE_SIZE);
}
void send_stage2_hid_descriptor() {
usb_hid_descriptor desc;
desc.bLength = USB_DT_HID_SIZE + USB_DT_REPORT_SIZE;
desc.bDescriptorType = USB_DT_HID;
desc.bcdHID = 0x001;// 0x0111; ?
desc.bCountryCode = 0;
desc.bNumDescriptors = 1;
write(&desc, USB_DT_HID_SIZE);
usb_hid_report_descriptor rep_desc;
rep_desc.bDescriptorType = USB_DT_REPORT;
rep_desc.wReportLength = sizeof(hid_report_descriptor);
write(&rep_desc, USB_DT_REPORT_SIZE);
}
void send_stage2_endpoint_descriptors() {
usb_endpoint_descriptor hid_endpoints[] = {
{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x81,
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
.wMaxPacketSize = USB_HID_MAX_PACKET_SIZE,
.bInterval = 0xff,
},
{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x01,
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
.wMaxPacketSize = USB_HID_MAX_PACKET_SIZE,
.bInterval = 0xff,
},
};
write(hid_endpoints, sizeof(hid_endpoints));
}
private:
uint8_t data_in[USB_HID_MAX_PACKET_SIZE];
uint8_t data_out[USB_HID_MAX_PACKET_SIZE];
bool respond;
state_t state;
callback_t on_hid_data;
WiFiServer wifiServer;
WiFiClient client;
};
int on_hid_data(void *data_in, void *data_out) {
//Serial.print('.');
int res = DAP_ProcessCommand((uint8_t *)data_in, (uint8_t *)data_out);
//dump_buff(data_in, USB_HID_MAX_PACKET_SIZE);
//dump_buff(data_out, USB_HID_MAX_PACKET_SIZE);
return res;
}
USBIPServer server(on_hid_data);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Starting WiFi...");
WiFi.begin(ssid, pass);
do {
delay(500);
} while (WiFi.status() != WL_CONNECTED);
Serial.println("Initializing DAP...");
DAP_Setup();
Serial.println("Starting USBIP server...");
server.begin();
}
void loop() {
server.loop();
}
/*
void setup() {
Serial.begin(115200);
DAP_Setup();
#ifdef HIDPROJECT_RAWHID
// Set the RawHID OUT report array.
// Feature reports are also (parallel) possible, see the other example for this.
RawHID.begin(rawhidRequest, DAP_PACKET_SIZE);
#endif
}
void loop() {
// Check if there is new data from the RawHID device
auto bytesAvailable =
#ifdef HIDPROJECT_RAWHID
RawHID.available();
#else
RawHID.recv(rawhidRequest, 0);
#endif
if (bytesAvailable > 0) {
Serial.print("cmd ");
Serial.print(rawhidRequest[0], HEX);
Serial.print(" ");
Serial.print(rawhidRequest[1], HEX);
Serial.print(" ");
auto sz = DAP_ProcessCommand(rawhidRequest, rawhidResponse);
Serial.print("rsp ");
Serial.print(sz);
Serial.println(" B");
#ifdef HIDPROJECT_RAWHID
RawHID.enable(); // signal that we're ready to receive another buffer
#endif
if (sz > 0) {
#ifdef HIDPROJECT_RAWHID
RawHID.write(rawhidResponse, DAP_PACKET_SIZE);
#else
RawHID.send(rawhidResponse, DAP_PACKET_SIZE);
#endif
}
}
}
*/