-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathBleGamepad.cpp
More file actions
2666 lines (2246 loc) · 82.1 KB
/
Copy pathBleGamepad.cpp
File metadata and controls
2666 lines (2246 loc) · 82.1 KB
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <NimBLEDevice.h>
#include <NimBLEUtils.h>
#include <NimBLEServer.h>
#include "NimBLEHIDDevice.h"
#include "HIDTypes.h"
#include "HIDKeyboardTypes.h"
#include "sdkconfig.h"
#include "BleConnectionStatus.h"
#include "BleGamepad.h"
#include "NimBLELog.h"
#include "BleGamepadConfiguration.h"
#include <stdexcept>
#include "BleXInputDescriptors.h"
#include <type_traits>
// Verify wire-format sizes of packed report structs match the protocol spec.
// If these fail, the struct padding or packing is wrong on this platform.
static_assert(sizeof(XInputInputReport) == XINPUT_REPORT_LEN_INPUT,
"XInputInputReport must be 16 bytes on wire");
static_assert(sizeof(XInputOutputReport) == XINPUT_REPORT_LEN_OUTPUT,
"XInputOutputReport must be 8 bytes on wire");
// Clamp INT16_MIN (-32768) to INT16_MIN+1 (-32767) because some hosts treat
// -32768 as an invalid/uninitialized value. All axis setters run through this.
static inline int16_t clampAxis(int16_t v)
{
return v;
}
#if defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG "BLEGamepad"
#else
#include "esp_log.h"
static const char *LOG_TAG = "BLEGamepad";
#endif
#define SERVICE_UUID_DEVICE_INFORMATION "180A" // Service - Device information
#define CHARACTERISTIC_UUID_MODEL_NUMBER "2A24" // Characteristic - Model Number String - 0x2A24
#define CHARACTERISTIC_UUID_SOFTWARE_REVISION "2A28" // Characteristic - Software Revision String - 0x2A28
#define CHARACTERISTIC_UUID_SERIAL_NUMBER "2A25" // Characteristic - Serial Number String - 0x2A25
#define CHARACTERISTIC_UUID_FIRMWARE_REVISION "2A26" // Characteristic - Firmware Revision String - 0x2A26
#define CHARACTERISTIC_UUID_HARDWARE_REVISION "2A27" // Characteristic - Hardware Revision String - 0x2A27
#define CHARACTERISTIC_UUID_BATTERY_POWER_STATE "2A1A" // Characteristic - Battery Power State - 0x2A1A
#if BLE_GAMEPAD_DEBUG == 1
static void dumpHIDReport(const uint8_t* report, size_t len);
#endif
BleGamepad::BleGamepad(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel, bool delayAdvertising) : _buttons(),
_specialButtons(0),
_x(0),
_y(0),
_z(0),
_rX(0),
_rY(0),
_rZ(0),
_slider1(0),
_slider2(0),
_rudder(0),
_throttle(0),
_accelerator(0),
_brake(0),
_steering(0),
_hat1(0),
_hat2(0),
_hat3(0),
_hat4(0),
_gX(0),
_gY(0),
_gZ(0),
_aX(0),
_aY(0),
_aZ(0),
_touch1X(0),
_touch1Y(0),
_touch1Pressure(0),
_touch2X(0),
_touch2Y(0),
_touch2Pressure(0),
_batteryPowerInformation(0),
_dischargingState(0),
_chargingState(0),
_powerLevel(0),
xInputReceiver(nullptr),
xInputConsumer(nullptr),
xInputBattery(nullptr),
pServer(nullptr),
hid(0),
pCharacteristic_Power_State(0),
configuration()
{
this->resetButtons();
this->deviceName = deviceName;
this->deviceManufacturer = deviceManufacturer;
this->batteryLevel = batteryLevel;
this->delayAdvertising = delayAdvertising;
this->connectionStatus = new BleConnectionStatus();
hidReportDescriptorSize = 0;
hidReportSize = 0;
numOfButtonBytes = 0;
enableOutputReport = false;
outputReportLength = 64;
enableFeatureReport = false;
featureReportLength = 64;
}
void BleGamepad::resetButtons()
{
memset(&_buttons, 0, sizeof(_buttons));
}
// Bounds-check macro for HID descriptor construction. tempHidReportDescriptor
// is a fixed-size buffer; this catches overflows at runtime before they corrupt
// adjacent memory.
#define HID_DESC_CHECK(n) do { \
if (hidReportDescriptorSize + (n) > (int)sizeof(tempHidReportDescriptor)) { \
NIMBLE_LOGE(LOG_TAG, "HID descriptor overflow at byte %d (need %d more)", \
hidReportDescriptorSize, (n)); \
return; \
} \
} while (0)
void BleGamepad::buildSInputDescriptor()
{
// Verbatim 139-byte report descriptor from HandHeldLegend/SINPUT-LIB-HID
// (sinput_lib_hid.c:k_sinput_hid_report_descriptor). Using it 1:1 ensures
// wDescriptorLength 0x8B matches and Windows' HID parser sees the exact
// Vendor usages (0xFF00:0x20/0x21/0x22) the official library ships.
static const uint8_t k_sinput_hid_report_descriptor[139] = {
0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, 0x85, 0x01, 0x06, 0x00, 0xFF, 0x09, 0x01, 0x15, 0x00, 0x25,
0xFF, 0x75, 0x08, 0x95, 0x02, 0x81, 0x02, 0x05, 0x09, 0x19, 0x01, 0x29, 0x20, 0x15, 0x00, 0x25,
0x01, 0x75, 0x01, 0x95, 0x20, 0x81, 0x02, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x32, 0x09,
0x35, 0x09, 0x33, 0x09, 0x34, 0x16, 0x00, 0x80, 0x26, 0xFF, 0x7F, 0x75, 0x10, 0x95, 0x06, 0x81,
0x02, 0x06, 0x00, 0xFF, 0x09, 0x20, 0x15, 0x00, 0x26, 0xFF, 0xFF, 0x75, 0x20, 0x95, 0x01, 0x81,
0x02, 0x09, 0x21, 0x16, 0x00, 0x80, 0x26, 0xFF, 0x7F, 0x75, 0x10, 0x95, 0x06, 0x81, 0x02, 0x09,
0x22, 0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x1D, 0x81, 0x02, 0x85, 0x02, 0x09, 0x23,
0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x3F, 0x81, 0x02, 0x85, 0x03, 0x09, 0x24, 0x15,
0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x2F, 0x91, 0x02, 0xC0};
memcpy(tempHidReportDescriptor, k_sinput_hid_report_descriptor, sizeof(k_sinput_hid_report_descriptor));
hidReportDescriptorSize = sizeof(k_sinput_hid_report_descriptor);
}
void BleGamepad::buildGenericDescriptor()
{
// USAGE_PAGE (Generic Desktop)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// USAGE (Joystick - 0x04; Gamepad - 0x05; Multi-axis Controller - 0x08)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getControllerType();
// COLLECTION (Application)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xa1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// REPORT_ID (Default: 3)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x85;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getHidReportId();
if (configuration.getButtonCount() > 0)
{
// USAGE_PAGE (Button)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
// LOGICAL_MINIMUM (0)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// LOGICAL_MAXIMUM (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// REPORT_SIZE (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// USAGE_MINIMUM (Button 1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x19;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// USAGE_MAXIMUM (Up to 128 buttons possible)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x29;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getButtonCount();
// REPORT_COUNT (# of buttons)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getButtonCount();
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
if (genericButtonPaddingBits > 0)
{
// REPORT_SIZE (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// REPORT_COUNT (# of padding bits)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = genericButtonPaddingBits;
// INPUT (Const,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03;
}
}
if (configuration.getTotalSpecialButtonCount() > 0)
{
// LOGICAL_MINIMUM (0)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// LOGICAL_MAXIMUM (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// REPORT_SIZE (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
if (configuration.getDesktopSpecialButtonCount() > 0)
{
// USAGE_PAGE (Generic Desktop)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// REPORT_COUNT
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getDesktopSpecialButtonCount();
if (configuration.getIncludeStart())
{
// USAGE (Start)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3D;
}
if (configuration.getIncludeSelect())
{
// USAGE (Select)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3E;
}
if (configuration.getIncludeMenu())
{
// USAGE (App Menu)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x86;
}
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
}
if (configuration.getConsumerSpecialButtonCount() > 0)
{
// USAGE_PAGE (Consumer Page)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x0C;
// REPORT_COUNT
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getConsumerSpecialButtonCount();
if (configuration.getIncludeHome())
{
// USAGE (Home)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x0A;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x23;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
}
if (configuration.getIncludeBack())
{
// USAGE (Back)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x0A;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x24;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
}
if (configuration.getIncludeVolumeInc())
{
// USAGE (Volume Increment)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xE9;
}
if (configuration.getIncludeVolumeDec())
{
// USAGE (Volume Decrement)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xEA;
}
if (configuration.getIncludeVolumeMute())
{
// USAGE (Mute)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xE2;
}
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
}
if (genericSpecialButtonPaddingBits > 0)
{
// REPORT_SIZE (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// REPORT_COUNT (# of padding bits)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = genericSpecialButtonPaddingBits;
// INPUT (Const,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03;
}
}
if (configuration.getAxisCount() > 0)
{
// USAGE_PAGE (Generic Desktop)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// LOGICAL_MINIMUM (-32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getAxesMin());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getAxesMin());
// LOGICAL_MAXIMUM (+32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getAxesMax());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getAxesMax());
// REPORT_SIZE (16)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
// REPORT_COUNT (configuration.getAxisCount() - sliders)
uint8_t sliders = (configuration.getIncludeSlider1() ? 1 : 0) + (configuration.getIncludeSlider2() ? 1 : 0);
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getAxisCount() - sliders;
// COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
if (configuration.getIncludeXAxis())
{
// USAGE (X)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x30;
}
if (configuration.getIncludeYAxis())
{
// USAGE (Y)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x31;
}
if (configuration.getIncludeZAxis())
{
// USAGE (Z)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x32;
}
if (configuration.getIncludeRzAxis())
{
// USAGE (Rz)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
}
if (configuration.getIncludeRxAxis())
{
// USAGE (Rx)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x33;
}
if (configuration.getIncludeRyAxis())
{
// USAGE (Ry)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x34;
}
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
if (configuration.getIncludeSlider1())
{
// REPORT_COUNT (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// COLLECTION (Logical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// USAGE (Slider)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x36;
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// END_COLLECTION (Logical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC0;
}
if (configuration.getIncludeSlider2())
{
// REPORT_COUNT (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// COLLECTION (Logical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// USAGE (Slider)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x36;
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// END_COLLECTION (Logical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC0;
}
// END_COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
}
if (configuration.getSimulationCount() > 0)
{
// USAGE_PAGE (Simulation Controls)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// LOGICAL_MINIMUM (-32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getSimulationMin());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getSimulationMin());
// LOGICAL_MAXIMUM (+32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getSimulationMax());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getSimulationMax());
// REPORT_SIZE (16)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
// REPORT_COUNT (configuration.getSimulationCount())
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getSimulationCount();
// COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
if (configuration.getIncludeRudder())
{
// USAGE (Rudder)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBA;
}
if (configuration.getIncludeThrottle())
{
// USAGE (Throttle)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xBB;
}
if (configuration.getIncludeAccelerator())
{
// USAGE (Accelerator)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC4;
}
if (configuration.getIncludeBrake())
{
// USAGE (Brake)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC5;
}
if (configuration.getIncludeSteering())
{
// USAGE (Steering)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xC8;
}
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// END_COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
}
// Gyroscope
if (configuration.getIncludeGyroscope())
{
// COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// USAGE_PAGE (Generic Desktop)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// USAGE (Gyroscope - Rotational X - Rx)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x33;
// USAGE (Rotational - Rotational Y - Ry)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x34;
// USAGE (Rotational - Rotational Z - Rz)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
// LOGICAL_MINIMUM (-32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getMotionMin());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getMotionMin());
// LOGICAL_MAXIMUM (+32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getMotionMax());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getMotionMax());
// REPORT_SIZE (16)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
// REPORT_COUNT (3)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03;
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// END_COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
}
// Accelerometer
if (configuration.getIncludeAccelerometer())
{
// COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// USAGE_PAGE (Generic Desktop)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x05;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// USAGE (Accelerometer - Vector X - Vx)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x40;
// USAGE (Accelerometer - Vector Y - Vy)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x41;
// USAGE (Accelerometer - Vector Z - Vz)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x42;
// LOGICAL_MINIMUM (-32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x16;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getMotionMin());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getMotionMin());
// LOGICAL_MAXIMUM (+32767)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getMotionMax());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getMotionMax());
// REPORT_SIZE (16)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x10;
// REPORT_COUNT (3)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x03;
// INPUT (Data,Var,Abs)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
// END_COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
}
if (configuration.getHatSwitchCount() > 0)
{
// COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xA1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// USAGE_PAGE (Generic Desktop)
tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE_PAGE(1);
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// USAGE (Hat Switch)
for (int currentHatIndex = 0; currentHatIndex < configuration.getHatSwitchCount(); currentHatIndex++)
{
tempHidReportDescriptor[hidReportDescriptorSize++] = USAGE(1);
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x39;
}
// Logical Min (1)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// Logical Max (8)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x25;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x08;
// Physical Min (0)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x35;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// Physical Max (315)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x46;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x3B;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// Unit (SI Rot : Ang Pos)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x65;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x12;
// Report Size (8)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x08;
// Report Count (4)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getHatSwitchCount();
// Input (Data, Variable, Absolute)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x81;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x42;
// END_COLLECTION (Physical)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
}
if (configuration.getEnableOutputReport())
{
// Usage Page (Vendor Defined 0xFF00)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x06;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
// Usage (Vendor Usage 0x01)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// Usage (0x01)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// Logical Minimum (0)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// Logical Maximum (255)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// Report Size (8 bits)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x08;
if (configuration.getOutputReportLength() <= 0xFF)
{
// Report Count (0~255 bytes)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getOutputReportLength();
}
else
{
// Report Count (0~65535 bytes)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x96;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getOutputReportLength());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getOutputReportLength());
}
// Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x91;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
}
if (configuration.getEnableFeatureReport())
{
// Vendor blob feature report
// Usage Page (Vendor Defined 0xFF00)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x06;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
// Usage (Vendor Usage 0x01)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// Usage (0x01)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x09;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x01;
// Logical Minimum (0)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x15;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// Logical Maximum (255)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x26;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xFF;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x00;
// Report Size (8 bits)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x75;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x08;
if (configuration.getFeatureReportLength() <= 0xFF)
{
// Report Count (0~255 bytes)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x95;
tempHidReportDescriptor[hidReportDescriptorSize++] = configuration.getFeatureReportLength();
}
else
{
// Report Count (0~65535 bytes)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x96;
tempHidReportDescriptor[hidReportDescriptorSize++] = lowByte(configuration.getFeatureReportLength());
tempHidReportDescriptor[hidReportDescriptorSize++] = highByte(configuration.getFeatureReportLength());
}
// Feature (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xB1;
tempHidReportDescriptor[hidReportDescriptorSize++] = 0x02;
}
// END_COLLECTION (Application)
tempHidReportDescriptor[hidReportDescriptorSize++] = 0xc0;
}
void BleGamepad::buildXInputDescriptor()
{
// Xbox descriptors taken verbatim from Mystfit pr-74 (570d6da):
// - XInputOneS -> 1708 (AC Back 0x0A,0x24,0x02, 0x02/0x04 present) — Generic on Win11 22H2+ WGI, XInput on linux<6.5 xpad
// - XInputSeriesX -> 1914 (Record/Share 0x0A,0xB2,0x00, 0x01+0x03 only) — XInput on Win11 WGI (Series X ok)
GamepadMode mode = configuration.getGamepadMode();
if (mode == GamepadMode::XInputSeriesX)
{
memcpy(tempHidReportDescriptor, XboxOneS_1914_HIDDescriptor, XboxOneS_1914_DescriptorSize);
hidReportDescriptorSize = XboxOneS_1914_DescriptorSize;
}
else
{
memcpy(tempHidReportDescriptor, XboxOneS_1708_HIDDescriptor, XboxOneS_1708_DescriptorSize);
hidReportDescriptorSize = XboxOneS_1708_DescriptorSize;
}
}
void BleGamepad::begin(BleGamepadConfiguration *config)
{
BleGamepadConfiguration defaultConfig;
configuration = config ? *config : defaultConfig;
// begin() may be called again to re-apply a changed configuration. Reset the
// sizes so they are recomputed from scratch: hidReportDescriptorSize indexes
// tempHidReportDescriptor as the descriptor is assembled below, so a stale
// non-zero value would append past the previous descriptor and overflow the
// fixed buffer. (They start at 0 from the constructor on the first call.)
hidReportDescriptorSize = 0;
hidReportSize = 0;
GamepadMode mode = configuration.getGamepadMode();
if (mode == GamepadMode::SInput || mode == GamepadMode::XInputOneS || mode == GamepadMode::XInputSeriesX)
{
configuration.setIncludeSlider1(false);
configuration.setIncludeSlider2(false);
configuration.setIncludeRudder(false);
configuration.setIncludeThrottle(false);
configuration.setIncludeAccelerator(false);
configuration.setIncludeBrake(false);
configuration.setIncludeSteering(false);
configuration.setIncludeGyroscope(false);
configuration.setIncludeAccelerometer(false);
configuration.setEnableOutputReport(false);
configuration.setEnableFeatureReport(false);
configuration.setHatSwitchCount(1);
if (mode == GamepadMode::SInput)
configuration.setButtonCount(25);
else if (mode == GamepadMode::XInputOneS || mode == GamepadMode::XInputSeriesX)
{
configuration.setButtonCount(11);
deviceName = "Xbox Wireless Controller";
deviceManufacturer = "Microsoft";
// Mystfit's XBOX_1708_SERIAL / XBOX_1914_SERIAL — ASCII hex serials
// from real controller BLE captures, used by the Xbox driver for matching.
if (mode == GamepadMode::XInputSeriesX)
configuration.setSerialNumber("3039373130303637313034303231");
else
configuration.setSerialNumber("3033363030343037323136373239");
}
if (mode == GamepadMode::SInput)
{
if (configuration.getVid() != SINPUT_USB_VID || configuration.getPid() != SINPUT_USB_PID_GENERIC)
Serial.println("WARNING: SInput mode requires VID 0x2E8A / PID 0x10C6 for host recognition. Do not override setVid()/setPid().");
}
else if (mode == GamepadMode::XInputOneS || mode == GamepadMode::XInputSeriesX)
{
if (configuration.getVid() != XINPUT_USB_VID)
Serial.println("WARNING: XInputOneS/XInputSeriesX mode requires VID 0x045E for Xbox driver recognition. Do not override setVid()/setPid().");
}
}
enableOutputReport = configuration.getEnableOutputReport();
outputReportLength = configuration.getOutputReportLength();
enableFeatureReport = configuration.getEnableFeatureReport();
featureReportLength = configuration.getFeatureReportLength();
enableSInput = configuration.getEnableSInput();
genericButtonPaddingBits = 8 - (configuration.getButtonCount() % 8);
if (genericButtonPaddingBits == 8)
{
genericButtonPaddingBits = 0;
}
genericSpecialButtonPaddingBits = 8 - (configuration.getTotalSpecialButtonCount() % 8);
if (genericSpecialButtonPaddingBits == 8)
{
genericSpecialButtonPaddingBits = 0;
}
// Precompute bit positions for each special button so press/release don't
// need to iterate the configuration array on every call.
uint8_t pos = 0;
for (int i = 0; i < POSSIBLESPECIALBUTTONS; i++)
{
_specialButtonPositions[i] = pos;
if (configuration.getWhichSpecialButtons()[i]) pos++;
}
uint8_t numOfAxisBytes = configuration.getAxisCount() * 2;
uint8_t numOfSimulationBytes = configuration.getSimulationCount() * 2;
numOfButtonBytes = configuration.getButtonCount() / 8;
if (genericButtonPaddingBits > 0)
{
numOfButtonBytes++;
}
uint8_t numOfSpecialButtonBytes = configuration.getTotalSpecialButtonCount() / 8;
if (genericSpecialButtonPaddingBits > 0)
{
numOfSpecialButtonBytes++;
}
uint8_t numOfMotionBytes = 0;
if (configuration.getIncludeAccelerometer())
{
numOfMotionBytes += 6;
}
if (configuration.getIncludeGyroscope())
{
numOfMotionBytes += 6;
}
hidReportSize = numOfButtonBytes + numOfSpecialButtonBytes + numOfAxisBytes + numOfSimulationBytes + numOfMotionBytes + configuration.getHatSwitchCount();
if (mode == GamepadMode::SInput)
{
enableSInput = true;
buildSInputDescriptor();
}
else if (mode == GamepadMode::XInputOneS || mode == GamepadMode::XInputSeriesX)
{
enableSInput = false;
buildXInputDescriptor();
}
else
{
enableSInput = false;
buildGenericDescriptor();
}
// tempHidReportDescriptor is a fixed buffer. A very large configuration (max
// buttons + every axis/simulation control + motion + hats + feature/output
// reports) can approach its capacity; if it is ever exceeded the writes above
// have already run past the end of the array, so fail loud here rather than
// ship a silently corrupt descriptor. getHidReportDescriptorSize() lets a
// caller watch the headroom.
if (hidReportDescriptorSize > (int)sizeof(tempHidReportDescriptor))
{
NIMBLE_LOGE(LOG_TAG, "HID report descriptor (%d bytes) overflowed its %u-byte buffer", hidReportDescriptorSize, (unsigned)sizeof(tempHidReportDescriptor));
}
// Set task priority from 5 to 1 in order to get ESP32-C3 working
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 1, NULL);
}
void BleGamepad::end(void)
{
delete outputReceiver;
outputReceiver = nullptr;
delete featureReceiver;
featureReceiver = nullptr;
delete sInputReceiver;
sInputReceiver = nullptr;
delete xInputReceiver;
xInputReceiver = nullptr;
delete connectionStatus;
connectionStatus = nullptr;
delete[] outputBackupBuffer;
outputBackupBuffer = nullptr;
delete[] featureBackupBuffer;
featureBackupBuffer = nullptr;
}
void BleGamepad::setAxes(int16_t x, int16_t y, int16_t z, int16_t rX, int16_t rY, int16_t rZ, int16_t slider1, int16_t slider2)
{
x = clampAxis(x);
y = clampAxis(y);
z = clampAxis(z);
rZ = clampAxis(rZ);
rX = clampAxis(rX);
rY = clampAxis(rY);
slider1 = clampAxis(slider1);
slider2 = clampAxis(slider2);
_x = x;
_y = y;
_z = z;
_rZ = rZ;
_rX = rX;
_rY = rY;
_slider1 = slider1;
_slider2 = slider2;
if (configuration.getAutoReport())
{
sendReport();
}
}
void BleGamepad::setHIDAxes(int16_t x, int16_t y, int16_t z, int16_t rZ, int16_t rX, int16_t rY, int16_t slider1, int16_t slider2)
{
x = clampAxis(x);
y = clampAxis(y);
z = clampAxis(z);
rZ = clampAxis(rZ);
rX = clampAxis(rX);
rY = clampAxis(rY);
slider1 = clampAxis(slider1);
slider2 = clampAxis(slider2);
_x = x;
_y = y;
_z = z;
_rZ = rZ;
_rX = rX;
_rY = rY;
_slider1 = slider1;
_slider2 = slider2;