-
Notifications
You must be signed in to change notification settings - Fork 87
/
BluetoothConnection.cpp
2222 lines (2054 loc) · 92.3 KB
/
BluetoothConnection.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
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
/* USB EHCI Host for Teensy 3.6
* Copyright 2017 Paul Stoffregen ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* information about the BlueTooth HCI comes from logic analyzer captures
* plus... http://affon.narod.ru/BT/bluetooth_app_c10.pdf
*/
//=============================================================================
// Bluetooth connections code
//=============================================================================
#include <Arduino.h>
#include "USBHost_t36.h" // Read this header first for key info
#include "utility/bt_defines.h"
#define print USBHost::print_
#define println USBHost::println_//#define DEBUG_BT
//#define DEBUG_BT
//#define DEBUG_BT_VERBOSE
#ifndef DEBUG_BT
#undef DEBUG_BT_VERBOSE
void inline DBGPrintf(...) {};
#else
#define DBGPrintf USBHDBGSerial.printf
#endif
#ifndef DEBUG_BT_VERBOSE
void inline VDBGPrintf(...) {};
#else
#define VDBGPrintf USBHDBGSerial.printf
#endif
// This is a list of all the drivers inherited from the BTHIDInput class.
// Unlike the list of USBDriver (managed in enumeration.cpp), drivers stay
// on this list even when they have claimed a top level collection.
BluetoothConnection *BluetoothConnection::s_first_ = NULL;
// When a new top level collection is found, this function asks drivers
// if they wish to claim it. The driver taking ownership of the
// collection is returned, or NULL if no driver wants it.
//=============================================================================
// initialize the connection data
//=============================================================================
void BluetoothConnection::initializeConnection(BluetoothController *btController, uint8_t bdaddr[6], uint32_t class_of_device,
bool inquire_mode)
{
device_driver_ = nullptr;
btController_ = btController; // back pointer to main object
// lets setup a connection for this timer
bt_connection_timer_.init(btController_); // so it will use the main device
bt_connection_timer_.pointer = (void*)this; // but rememember us.
connection_rxid_ = 0;
control_dcid_ = 0x70;
interrupt_dcid_ = 0x71;
sdp_dcid_ = 0x40;
connection_complete_ = 0;
connection_started_ = false;
connection_started_by_timer_ = false;
//if (!connection_started_) {
// connection_started_ = true;
// btController_->setTimer(nullptr, 0); // clear out timer
//}
use_hid_protocol_ = false;
sdp_connected_ = false;
supports_SSP_ = false;
pending_control_tx_ = 0;
inquire_mode_ = inquire_mode;
find_driver_type_1_called_ = false; // bugbug should combine:
// We need to save away the BDADDR and class link type?
for (uint8_t i = 0; i < 6; i++) device_bdaddr_[i] = bdaddr[i];
device_class_ = class_of_device;
}
void BluetoothConnection::remoteNameComplete(const uint8_t *device_name)
{
// hack for now remember the device_name if we have one
if (device_name)strcpy((char*)remote_name_, (const char *)device_name);
else remote_name_[0] = 0; // null terminated string.
device_driver_ = find_driver(device_name, 0);
}
//=============================================================================
// Find a driver
//=============================================================================
BTHIDInput * BluetoothConnection::find_driver(const uint8_t *remoteName, int type)
{
if (type == 1) find_driver_type_1_called_ = true;
DBGPrintf("BluetoothController::find_driver(%x) type: %d\n", device_class_, type);
if (device_class_ & 0x2000) DBGPrintf(" (0x2000)Limited Discoverable Mode\n");
DBGPrintf(" (0x500)Peripheral device\n");
if (device_class_ & 0x80) DBGPrintf(" Mouse\n");
if (device_class_ & 0x40) DBGPrintf(" Keyboard\n");
switch (device_class_ & 0x3c) {
case 4: DBGPrintf(" Joystick\n"); break;
case 8: DBGPrintf(" Gamepad\n"); break;
case 0xc: DBGPrintf(" Remote Control\n"); break;
}
check_for_hid_descriptor_ = false;
BTHIDInput *driver = BluetoothController::available_bthid_drivers_list;
while (driver) {
DBGPrintf(" driver %x\n", (uint32_t)driver);
// should make const all the way through
hidclaim_t claim_type = driver->claim_bluetooth(this, device_class_, (uint8_t*)remoteName, type);
if (claim_type == CLAIM_INTERFACE) {
check_for_hid_descriptor_ = false;
DBGPrintf(" *** Claimed ***\n");
return driver;
} else if (claim_type == CLAIM_REPORT) {
DBGPrintf(" *** Check for HID ***\n");
check_for_hid_descriptor_ = true;
}
driver = driver->next;
}
return NULL;
}
//=============================================================================
// Handle the rx2_
//=============================================================================
void BluetoothConnection::rx2_data(uint8_t *rx2buf) // called from rx2_data of BluetoothController
{
// need to detect if these are L2CAP or SDP or ...
uint16_t data_len = rx2buf[4] + ((uint16_t)rx2buf[5] << 8);
uint16_t dcid = rx2buf[6] + ((uint16_t)rx2buf[7] << 8);
//DBGPrintf("@@@@@@ SDP MSG? %x %x %x @@@@@", dcid, sdp_dcid_, rx2buf[8]);
if (dcid == sdp_dcid_) {
switch (rx2buf[8]) {
case SDP_SERVICE_SEARCH_REQUEST:
process_sdp_service_search_request(&rx2buf[8]);
break;
case SDP_SERVICE_SEARCH_RESPONSE:
process_sdp_service_search_response(&rx2buf[8]);
break;
case SDP_SERVICE_ATTRIBUTE_REQUEST:
process_sdp_service_attribute_request(&rx2buf[8]);
break;
case SDP_SERVICE_ATTRIBUTE_RESPONSE:
process_sdp_service_attribute_response(&rx2buf[8]);
break;
case SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
process_sdp_service_search_attribute_request(&rx2buf[8]);
break;
case SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE:
process_sdp_service_search_attribute_response(&rx2buf[8]);
break;
}
} else {
switch (rx2buf[8]) {
case L2CAP_CMD_CONNECTION_REQUEST:
process_l2cap_connection_request(&rx2buf[8], data_len);
break;
case L2CAP_CMD_CONNECTION_RESPONSE:
process_l2cap_connection_response(&rx2buf[8], data_len);
break;
case L2CAP_CMD_CONFIG_REQUEST:
process_l2cap_config_request(&rx2buf[8], data_len);
break;
case L2CAP_CMD_CONFIG_RESPONSE:
process_l2cap_config_response(&rx2buf[8], data_len);
break;
case HID_THDR_DATA_INPUT:
handleHIDTHDRData(rx2buf); // Pass the whole buffer...
break;
case L2CAP_CMD_COMMAND_REJECT:
process_l2cap_command_reject(&rx2buf[8], data_len);
break;
case L2CAP_CMD_DISCONNECT_REQUEST:
process_l2cap_disconnect_request(&rx2buf[8], data_len);
break;
}
}
}
//=============================================================================
// Process tx_data and state
//=============================================================================
void BluetoothConnection::tx_data(uint8_t *data, uint16_t length)
{
#ifdef DEBUG_BT_VERBOSE
DBGPrintf("tx_data callback (bluetooth): %d : ", pending_control_tx_);
for (uint8_t i = 0; i < length; i++) DBGPrintf("%02x ", data[i]);
DBGPrintf("\n");
#endif
switch (pending_control_tx_) {
case STATE_TX_SEND_CONNECT_INT:
delay(1);
connection_rxid_++;
sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, interrupt_dcid_, HID_INTR_PSM);
pending_control_tx_ = 0;
break;
case STATE_TX_SEND_CONECT_RSP_SUCCESS:
delay(1);
// Tell the device we are ready
sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_++, control_dcid_, control_scid_, SUCCESSFUL);
pending_control_tx_ = STATE_TX_SEND_CONFIG_REQ;
break;
case STATE_TX_SEND_CONFIG_REQ:
delay(1);
sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, control_scid_);
pending_control_tx_ = 0;
break;
case STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS:
delay(1);
// Tell the device we are ready
sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_++, interrupt_dcid_, interrupt_scid_, SUCCESSFUL);
pending_control_tx_ = STATE_TX_SEND_CONFIG_ISR_REQ;
break;
case STATE_TX_SEND_CONFIG_ISR_REQ:
delay(1);
sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, interrupt_scid_);
pending_control_tx_ = 0;
break;
case STATE_TX_SEND_CONECT_SDP_RSP_SUCCESS:
delay(1);
sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, sdp_dcid_, sdp_scid_, SUCCESSFUL);
pending_control_tx_ = STATE_TX_SEND_CONFIG_SDP_REQ;
break;
case STATE_TX_SEND_CONFIG_SDP_REQ:
delay(1);
sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, sdp_scid_);
pending_control_tx_ = 0;
break;
}
}
//=============================================================================
// Moved handling of L2CAP and HID messages
//=============================================================================
// Experiment to see if we can get SDP connectin setup
void BluetoothConnection::connectToSDP() {
DBGPrintf("$$$ BluetoothController::connectToSDP() Called\n");
connection_rxid_++;
sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_,
sdp_dcid_, SDP_PSM);
pending_control_tx_ = 0;
}
void BluetoothConnection::process_l2cap_connection_request(uint8_t *data, uint16_t length) {
// ID LEN LEN PSM PSM SCID SCID
// 0x02 0x02 0x04 0x00 0x11 0x00 0x43 0x00
uint16_t psm = data[4] + ((uint16_t)data[5] << 8);
uint16_t scid = data[6] + ((uint16_t)data[7] << 8);
connection_started_ = true;
btController_->setTimer(nullptr, 0); // clear out timer
connection_rxid_ = data[1];
DBGPrintf(" recv L2CAP Connection Request: ID: %d, PSM: %x, SCID: %x\n", connection_rxid_, psm, scid);
// Assuming not pair mode Send response like:
// RXID Len LEN DCID DCID SCID SCID RES 0 0 0
// 0x03 0x02 0x08 0x00 0x70 0x00 0x43 0x00 0x01 0x00 0x00 0x00
if (psm == HID_CTRL_PSM) {
control_scid_ = scid;
sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, control_dcid_, control_scid_, PENDING);
pending_control_tx_ = STATE_TX_SEND_CONECT_RSP_SUCCESS;
} else if (psm == HID_INTR_PSM) {
interrupt_scid_ = scid;
sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, interrupt_dcid_, interrupt_scid_, PENDING);
pending_control_tx_ = STATE_TX_SEND_CONECT_ISR_RSP_SUCCESS;
} else if (psm == SDP_PSM) {
DBGPrintf(" <<< SDP PSM >>>\n");
sdp_scid_ = scid;
sendl2cap_ConnectionResponse(device_connection_handle_, connection_rxid_, sdp_dcid_, sdp_scid_, PENDING);
pending_control_tx_ = STATE_TX_SEND_CONECT_SDP_RSP_SUCCESS;
}
}
// Process the l2cap_connection_response...
void BluetoothConnection::process_l2cap_connection_response(uint8_t *data, uint16_t length) {
uint16_t scid = data[4] + ((uint16_t)data[5] << 8);
uint16_t dcid = data[6] + ((uint16_t)data[7] << 8);
uint16_t result = data[8] + ((uint16_t)data[9] << 8);
DBGPrintf(" recv L2CAP Connection Response: ID: %d, Dest:%x, Source:%x, Result:%x, Status: %x pending control: %x %x\n",
data[1], scid, dcid,
result, data[10] + ((uint16_t)data[11] << 8), btController_->pending_control_, pending_control_tx_);
// Experiment ignore if: pending_control_tx_ = STATE_TX_SEND_CONFIG_REQ;
if ((pending_control_tx_ == STATE_TX_SEND_CONFIG_REQ) || (pending_control_tx_ == STATE_TX_SEND_CONECT_RSP_SUCCESS)) {
DBGPrintf(" *** State == STATE_TX_SEND_CONFIG_REQ - try ignoring\n");
return;
}
//48 20 10 0 | c 0 1 0 | 3 0 8 0 44 0 70 0 0 0 0 0
if (dcid == interrupt_dcid_) {
interrupt_scid_ = scid;
DBGPrintf(" Interrupt Response\n");
connection_rxid_++;
sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
} else if (dcid == control_dcid_) {
control_scid_ = scid;
DBGPrintf(" Control Response\n");
sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
} else if (dcid == sdp_dcid_) {
// Check for failure!
DBGPrintf(" SDP Response\n");
if (result != 0) {
DBGPrintf(" Failed - No SDP!\n");
// Enable SCan to page mode
sdp_connected_ = false;
connection_complete_ |= CCON_SDP;
if (connection_complete_ == CCON_ALL)
btController_->sendHCIWriteScanEnable(2);
} else {
sdp_scid_ = scid;
sendl2cap_ConfigRequest(device_connection_handle_, connection_rxid_, scid);
}
} else {
DBGPrintf(" Unknown Response\n");
// Unknown dcid... Guess
if (((connection_complete_ | CCON_SDP) == CCON_ALL) && (result !=0)) {
DBGPrintf(" Guess SDP Response failure\n");
connection_complete_ |= CCON_SDP;
btController_->sendHCIWriteScanEnable(2);
}
}
}
void BluetoothConnection::process_l2cap_config_request(uint8_t *data, uint16_t length) {
//48 20 10 00 0c 00 01 00 * 04 02 08 00 70 00 00 00 01 02 30 00
//48 20 10 00 0C 00 01 00 * 04 03 08 00 70 00 00 00 01 02 48 00
uint16_t dcid = data[4] + ((uint16_t)data[5] << 8);
DBGPrintf(" recv L2CAP config Request: ID: %d, Dest:%x, Flags:%x, Options:",
data[1], dcid, data[6] + ((uint16_t)data[7] << 8));
for (uint16_t i = 8; i < length; i++) DBGPrintf(" %02x", data[i]);
uint16_t mtu = 0x2A0; // hard coded what we were returning....
for (uint16_t i = 8; i < length; i++) {
if ((data[i] == 1) && (data[i+1] == 2)) {
mtu = data[i+2] + (data[i+3] << 8);
DBGPrintf(" MTU:%u", mtu);
}
i += data[i+1] + 2;
}
DBGPrintf("\n");
// Now see which dest was specified
if (dcid == control_dcid_) {
DBGPrintf(" Control Configuration request\n");
sendl2cap_ConfigResponse(device_connection_handle_, data[1], control_scid_, mtu);
connection_complete_ |= CCON_CONT;
} else if (dcid == interrupt_dcid_) {
DBGPrintf(" Interrupt Configuration request\n");
sendl2cap_ConfigResponse(device_connection_handle_, data[1], interrupt_scid_, mtu);
connection_complete_ |= CCON_INT;
} else if (dcid == sdp_dcid_) {
DBGPrintf(" SDP Configuration request\n");
sendl2cap_ConfigResponse(device_connection_handle_, data[1], sdp_scid_, mtu);
connection_complete_ |= CCON_SDP;
sdp_connected_ = true;
} else if (dcid == sdp_scid_) {
DBGPrintf(" SDP Configuration request (But our SCID?)\n");
// maybe we should change the ids around?
sendl2cap_ConfigResponse(device_connection_handle_, data[1], sdp_scid_, mtu);
// We see this with some PS4?
// Enable SCan to page mode
connection_complete_ |= CCON_SDP;
sdp_connected_ = true;
}
}
void BluetoothConnection::process_l2cap_config_response(uint8_t *data, uint16_t length) {
// 48 20 12 0 e 0 1 0 5 0 a 0 70 0 0 0 0 0 1 2 30 0
uint16_t scid = data[4] + ((uint16_t)data[5] << 8);
uint16_t result = data[8] + ((uint16_t)data[9] << 8);
DBGPrintf(" recv L2CAP config Response: ID: %d, Source:%x, Flags:%x, Result:%x",
data[1], scid, data[6] + ((uint16_t)data[7] << 8), result );
switch (result) {
case 0x0000: DBGPrintf("(Success)"); break;
case 0x0001: DBGPrintf("(Failure – unacceptable parameters)"); break;
case 0x0002: DBGPrintf("(Failure – rejected (no reason provided))"); break;
case 0x0003: DBGPrintf("(Failure – unknown options)"); break;
case 0x0004: DBGPrintf("(Pending)"); break;
case 0x0005: DBGPrintf("(Failure - flow spec rejected)"); break;
}
if (length > 10) {
DBGPrintf(", Config: ");
for (uint16_t i = 10; i < length; i++) DBGPrintf(" %02x", data[i]);
for (uint16_t i = 10; i < length; i++) {
if ((data[i] == 1) && (data[i+1] == 2)) {
uint16_t mtu = data[i+2] + (data[i+3] << 8);
DBGPrintf(" MTU:%u", mtu);
}
i += data[i+1] + 2;
}
}
DBGPrintf("\n");
if (scid == control_dcid_) {
// Maybe we should avoid setting HID mode and only do
// so if the class of device is either/or MOUSE or Keyboard.
// Set HID Boot mode
// Don't do if PS3... Or if class told us not to
if (device_class_ & 0xC0) {
if (use_hid_protocol_) {
// see what happens if I tell it to
btController_->setHIDProtocol(HID_RPT_PROTOCOL);
} else {
// don't call through Null pointer
if ((device_driver_ == nullptr) ||
!(device_driver_->special_process_required & BTHIDInput::SP_PS3_IDS)) {
btController_->setHIDProtocol(HID_BOOT_PROTOCOL); //
}
}
}
//setHIDProtocol(HID_RPT_PROTOCOL); //HID_RPT_PROTOCOL
if ((btController_->do_pair_device_ || btController_->do_pair_ssp_) && !(device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_DONT_NEED_CONNECT))) {
pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
} else if (device_driver_ && (device_driver_->special_process_required & BTHIDInput::SP_NEED_CONNECT)) {
DBGPrintf(" Needs connect to device INT(PS4?)\n");
// The PS4 requires a connection request to it.
pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
} else if (connection_started_by_timer_) {
DBGPrintf(" Connection started by timeout continue to interrupt\n");
// The PS4 requires a connection request to it.
pending_control_tx_ = STATE_TX_SEND_CONNECT_INT;
} else {
btController_->pending_control_ = 0;
}
// Could be cleaner
if (((device_class_ & 0xC0) == 0) && (pending_control_tx_ == STATE_TX_SEND_CONNECT_INT)) {
// if we are not mouse and keybboard and we need to send the connect int
// do it now.
delay(1);
connection_rxid_++;
sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, interrupt_dcid_, HID_INTR_PSM);
pending_control_tx_ = 0;
}
DBGPrintf("\tNew Pending control pair:%u driver:%p HID:%u TX: %x\n", btController_->do_pair_device_, device_driver_, check_for_hid_descriptor_, pending_control_tx_);
connection_complete_ |= CCON_CONT;
} else if (scid == interrupt_dcid_) {
// Lets try SDP connect if we are not already connected.
if (connection_started_by_timer_) {
DBGPrintf(" Connection started by timeout experiment continue to SDP\n");
} else
if (!check_for_hid_descriptor_) connection_complete_ |= CCON_SDP; // Don't force connect if no is asking for HID
if ((connection_complete_ & CCON_SDP) == 0) connectToSDP(); // temp to see if we can do this later...
// Enable SCan to page mode
//connection_complete_ = true;
//sendHCIWriteScanEnable(2);
connection_complete_ |= CCON_INT;
} else if (scid == sdp_dcid_) {
// Enable SCan to page mode
DBGPrintf(" SDP configuration complete\n");
// Enable SCan to page mode
connection_complete_ |= CCON_SDP;
sdp_connected_ = true;
}
if (connection_complete_ == CCON_ALL) {
btController_->sendHCIWriteScanEnable(2);
}
}
void BluetoothConnection::process_l2cap_command_reject(uint8_t *data, uint16_t length) {
// 48 20 b 0 7 0 70 0 *1 0 0 0 2 0 4
DBGPrintf(" recv L2CAP command reject: ID: %d, length:%x, Reason:%x, Data: %x %x \n",
data[1], data[2] + ((uint16_t)data[3] << 8), data[4], data[5], data[6]);
}
void BluetoothConnection::process_l2cap_disconnect_request(uint8_t *data, uint16_t length) {
uint16_t dcid = data[4] + ((uint16_t)data[5] << 8);
uint16_t scid = data[6] + ((uint16_t)data[7] << 8);
DBGPrintf(" recv L2CAP disconnect request: ID: %d, Length:%x, Dest:%x, Source:%x\n",
data[1], data[2] + ((uint16_t)data[3] << 8), dcid, scid);
// May need to respond in some cases...
if (dcid == control_dcid_) {
DBGPrintf(" Control disconnect request\n");
} else if (dcid == interrupt_dcid_) {
DBGPrintf(" Interrupt disconnect request\n");
} else if (dcid == sdp_dcid_) {
DBGPrintf(" SDP disconnect request\n");
sdp_connected_ = false; // say we are not connected
sendl2cap_DisconnectResponse(device_connection_handle_, data[1],
sdp_dcid_,
sdp_scid_);
}
}
void BluetoothConnection::handleHIDTHDRData(uint8_t *data) {
// Example
// T HID data
//48 20 d 0 9 0 71 0 a1 3 8a cc c5 a 23 22 79
uint16_t len = data[4] + ((uint16_t)data[5] << 8);
DBGPrintf("HID HDR Data: len: %d, Type: %d Con:%p\n", len, data[9], this);
// ??? How to parse??? Use HID object???
#if 0
if (!find_driver_type_1_called_ && !device_driver_ && !have_hid_descriptor_) {
// If we got to here and don't have driver or ... try once to get one
DBGPrintf("\t $$$ No HID or Driver: See if one wants it now\n");
// BUGBUG we initialize descriptor with name...
device_driver_ = find_driver(descriptor_, 1);
}
#endif
if (device_driver_) {
device_driver_->process_bluetooth_HID_data(&data[9], len - 1); // We skip the first byte...
} else if (have_hid_descriptor_) {
// nead to bias to location within data.
parse(0x0100 | data[9], &data[10], len - 2);
} else {
switch (data[9]) {
case 1:
DBGPrintf(" Keyboard report type\n");
break;
case 2:
DBGPrintf(" Mouse report type\n");
break;
case 3:
DBGPrintf(" Combo keyboard/pointing\n");
break;
default:
DBGPrintf(" Unknown report\n");
}
}
}
void BluetoothConnection::handle_HCI_WRITE_SCAN_ENABLE_complete(uint8_t *rxbuf)
{
// See if we have driver and a remote
DBGPrintf("Write_Scan_enable Completed - connection state: %x\n", connection_complete_);
if (connection_complete_ == CCON_ALL) { // We have a driver call their
if (device_driver_) {
device_driver_->connectionComplete();
} else if (check_for_hid_descriptor_) {
have_hid_descriptor_ = false;
sdp_buffer_ = descriptor_;
sdp_buffer_len_ = sizeof(descriptor_);
if (!startSDP_ServiceSearchAttributeRequest(0x206, 0x206, sdp_buffer_, sdp_buffer_len_)) {
// Maybe try to claim_driver as we won't get a HID report.
DBGPrintf("Failed to start SDP attribute request - so lets try again to find a driver");
device_driver_ = find_driver(descriptor_, 1);
}
}
connection_complete_ = 0; // only call once
}
}
void BluetoothConnection::handle_HCI_OP_ROLE_DISCOVERY_complete(uint8_t *rxbuf)
{
// PS4 looks something like: 0E 07 01 09 08 00 47 00 01
// Others look like : 0E 07 01 09 08 00 48 00 00
// last byte is the interesting one says what role.
DBGPrintf("ROLE_DISCOVERY Completed - Role: %x ", rxbuf[8]);
if (rxbuf[8] == 0) {
DBGPrintf("(Central)\n");
// Set a timeout
btController_->setTimer(this, CONNECTION_TIMEOUT_US);
} else {
DBGPrintf("(Peripheral)\n");
// Should maybe do some state checking before doing this, but...
DBGPrintf("\t** We will issue connection requsts **\n ");
connection_started_ = true;
sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
}
}
void BluetoothConnection::timer_event(USBDriverTimer *whichTimer)
{
DBGPrintf("BluetoothConnection::timer_event(%p) : %u\n", this, connection_started_);
if (whichTimer == &bt_connection_timer_) {
if (device_driver_) {
device_driver_->bt_hid_timer_event(whichTimer);
}
} else if (!connection_started_) {
DBGPrintf("\t** Timed out now try issue connection requsts **\n ");
connection_started_ = true;
connection_started_by_timer_ = true;
connection_rxid_++;
sendl2cap_ConnectionRequest(device_connection_handle_, connection_rxid_, control_dcid_, HID_CTRL_PSM);
}
}
// l2cap support functions.
void BluetoothConnection::sendl2cap_ConnectionResponse(uint16_t handle, uint8_t rxid, uint16_t dcid, uint16_t scid, uint8_t result) {
uint8_t l2capbuf[12];
l2capbuf[0] = L2CAP_CMD_CONNECTION_RESPONSE; // Code
l2capbuf[1] = rxid; // Identifier
l2capbuf[2] = 0x08; // Length
l2capbuf[3] = 0x00;
l2capbuf[4] = dcid & 0xff; // Destination CID
l2capbuf[5] = dcid >> 8;
l2capbuf[6] = scid & 0xff; // Source CID
l2capbuf[7] = scid >> 8;
l2capbuf[8] = result; // Result: Pending or Success
l2capbuf[9] = 0x00;
l2capbuf[10] = 0x00; // No further information
l2capbuf[11] = 0x00;
DBGPrintf("\nSend L2CAP_CMD_CONNECTION_RESPONSE(RXID:%x, DCID:%x, SCID:%x RES:%x)\n", rxid, dcid, scid, result);
btController_->sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
}
void BluetoothConnection::sendl2cap_ConnectionRequest(uint16_t handle, uint8_t rxid, uint16_t scid, uint16_t psm) {
uint8_t l2capbuf[8];
l2capbuf[0] = L2CAP_CMD_CONNECTION_REQUEST; // Code
l2capbuf[1] = rxid; // Identifier
l2capbuf[2] = 0x04; // Length
l2capbuf[3] = 0x00;
l2capbuf[4] = (uint8_t)(psm & 0xff); // PSM
l2capbuf[5] = (uint8_t)(psm >> 8);
l2capbuf[6] = scid & 0xff; // Source CID
l2capbuf[7] = (scid >> 8) & 0xff;
DBGPrintf("\nSend ConnectionRequest (RXID:%x, SCID:%x, PSM:%x)\n", rxid, scid, psm);
btController_->sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
}
void BluetoothConnection::sendl2cap_ConfigRequest(uint16_t handle, uint8_t rxid, uint16_t dcid) {
uint8_t l2capbuf[12];
l2capbuf[0] = L2CAP_CMD_CONFIG_REQUEST; // Code
l2capbuf[1] = rxid; // Identifier
l2capbuf[2] = 0x08; // Length
l2capbuf[3] = 0x00;
l2capbuf[4] = dcid & 0xff; // Destination CID
l2capbuf[5] = (dcid >> 8) & 0xff;
l2capbuf[6] = 0x00; // Flags
l2capbuf[7] = 0x00;
l2capbuf[8] = 0x01; // Config Opt: type = MTU (Maximum Transmission Unit) - Hint
l2capbuf[9] = 0x02; // Config Opt: length
l2capbuf[10] = 0xFF; // MTU
l2capbuf[11] = 0xFF;
DBGPrintf("\nsend L2CAP_ConfigRequest(RXID:%x, DCID:%x)\n", rxid, dcid);
btController_->sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
}
void BluetoothConnection::sendl2cap_ConfigResponse(uint16_t handle, uint8_t rxid, uint16_t scid, uint16_t mtu) {
uint8_t l2capbuf[10];
l2capbuf[0] = L2CAP_CMD_CONFIG_RESPONSE; // Code
l2capbuf[1] = rxid; // Identifier
l2capbuf[2] = 0x06; // try not sending mtu 0x0A; // Length
l2capbuf[3] = 0x00;
l2capbuf[4] = scid & 0xff; // Source CID
l2capbuf[5] = (scid >> 8) & 0xff;
l2capbuf[6] = 0x00; // Flag
l2capbuf[7] = 0x00;
l2capbuf[8] = 0x00; // Result
l2capbuf[9] = 0x00;
//l2capbuf[10] = 0x01; // Config
//l2capbuf[11] = 0x02;
//l2capbuf[12] = mtu & 0xff; //0xA0;
//l2capbuf[13] = mtu >> 8; //0x02;
DBGPrintf("\nsend L2CAP_ConfigResponse(RXID:%x, SCID:%x)\n", rxid, scid);
btController_->sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
}
void BluetoothConnection::sendl2cap_DisconnectResponse(uint16_t handle, uint8_t rxid, uint16_t dcid, uint16_t scid) {
uint8_t l2capbuf[8];
l2capbuf[0] = L2CAP_CMD_DISCONNECT_RESPONSE; // Code
l2capbuf[1] = rxid; // Identifier
l2capbuf[2] = 0x04; // Length
l2capbuf[3] = 0x00;
l2capbuf[4] = dcid & 0xff; // dcid CID
l2capbuf[5] = (dcid >> 8) & 0xff;
l2capbuf[6] = scid & 0xff; // SCID CID
l2capbuf[7] = (scid >> 8) & 0xff;
DBGPrintf("\nsend L2CAP_DisconnectResponse(RXID:%x, DCID:%x, SCID:%x)\n", rxid, dcid, scid);
btController_->sendL2CapCommand(handle, l2capbuf, sizeof(l2capbuf));
}
//=============================================================================
// Process the SDP stuff.
//=============================================================================
bool BluetoothConnection::startSDP_ServiceSearchAttributeRequest(uint16_t range_low, uint16_t range_high, uint8_t *buffer, uint32_t cb)
{
if (!sdp_connected_) return false;
sdp_request_range_low_ = range_low;
sdp_reqest_range_high_ = range_high;
sdp_request_buffer_ = buffer;
sdp_request_buffer_cb_ = cb;
sdp_request_buffer_used_cnt_ = 0; // cnt in bytes used.
sdp_request_completed_ = false;
// So start it up
send_SDP_ServiceSearchAttributeRequest(nullptr, 0);
return true;
}
void BluetoothConnection::send_SDP_ServiceSearchRequest(uint8_t *continue_state, uint8_t cb)
{
// Example message: first part l2cap. Which will let them fill that in
// l2cap: 0x47 0x0 0x18 0x0 0x14 0x0 0x42 0x0
// PSM header: 0x6 0x0 0x0 0x0 0xf
// req: 0x35 0x3 0x19 0x1 0x0 0xff 0xff 0x35 0x5 0xa 0x0 0x0 0xff 0xff 0x0
uint8_t sdpcmdbuf[30]; // 20 + up to 10 continue
seq_number_++;
uint16_t packet_size = (13 - 5) + cb;
sdpcmdbuf[0] = 0x2; // SDP_ServiceSearchRequest
sdpcmdbuf[1] = seq_number_ >> 8; // Sequence number
sdpcmdbuf[2] = seq_number_ & 0xff; //
sdpcmdbuf[3] = packet_size >> 8; // Data in the rest...
sdpcmdbuf[4] = packet_size & 0xff; //
// Now lets build the attribute request data.
// 0x35 0x3 0x19 0x1 0x0 0xff 0xff 0x35 0x5 0xa 0x0 0x0 0xff 0xff 0x2 0x0 0x26
sdpcmdbuf[5] = 0x35; // type sequence
sdpcmdbuf[6] = 0x03; // 3 bytes
sdpcmdbuf[7] = 0x19; // UUID 2 bytes
sdpcmdbuf[8] = 0x01; // UUID
sdpcmdbuf[9] = 0x00; // UUID
sdpcmdbuf[10] = 0xff; // MAX attributes
sdpcmdbuf[11] = 0xff; //
sdpcmdbuf[12] = cb; // Count of continuation bytes
for (uint8_t i = 0; i < cb; i++) sdpcmdbuf[13 + i] = continue_state[i];
// Now lets try to send the packet
btController_->sendL2CapCommand(sdpcmdbuf, 13 + cb, sdp_scid_);
}
void BluetoothConnection::send_SDP_ServiceSearchAttributeRequest(uint8_t *continue_state, uint8_t cb)
{
// Example message: first part l2cap. Which will let them fill that in
// l2cap: 0x47 0x0 0x18 0x0 0x14 0x0 0x42 0x0
// PSM header: 0x6 0x0 0x0 0x0 0xf
// req: 0x35 0x3 0x19 0x1 0x0 0xff 0xff 0x35 0x5 0xa 0x0 0x0 0xff 0xff 0x0
uint16_t continue_data_offset = 17;
uint8_t sdpcmdbuf[37]; // 20 + up to 17 continue
seq_number_++;
sdpcmdbuf[0] = 0x6; // SDP_ServiceSearchAttributeReques
sdpcmdbuf[1] = seq_number_ >> 8; // Sequence number
sdpcmdbuf[2] = seq_number_ & 0xff; //
//sdpcmdbuf[3] = packet_size >> 8; // Data in the rest...
//sdpcmdbuf[4] = packet_size & 0xff; //
// Now lets build the attribute request data.
// 0x35 0x3 0x19 0x1 0x0 0xff 0xff 0x35 0x5 0xa 0x0 0x0 0xff 0xff 0x2 0x0 0x26
sdpcmdbuf[5] = 0x35; // type sequence
sdpcmdbuf[6] = 0x03; // 3 bytes
sdpcmdbuf[7] = 0x19; // UUID 2 bytes
sdpcmdbuf[8] = 0x01; // UUID
sdpcmdbuf[9] = 0x00; // UUID
sdpcmdbuf[10] = 0xff; // MAX size
sdpcmdbuf[11] = 0xff; //
sdpcmdbuf[12] = 0x35; // Sequence
if (sdp_request_range_low_ == sdp_reqest_range_high_) {
// doing specific
sdpcmdbuf[13] = 0x03; // 3 bytes
sdpcmdbuf[14] = 0x09; // 2 byte integer
sdpcmdbuf[15] = sdp_request_range_low_ >> 8; // Attribute low
sdpcmdbuf[16] = sdp_request_range_low_ & 0xff; //
} else {
// doing range
sdpcmdbuf[13] = 0x05; // 5 bytes
sdpcmdbuf[14] = 0x0A; // 4 byte integer
sdpcmdbuf[15] = sdp_request_range_low_ >> 8; // Attribute low
sdpcmdbuf[16] = sdp_request_range_low_ & 0xff; //
sdpcmdbuf[17] = sdp_reqest_range_high_ & 0xff; // high
sdpcmdbuf[18] = sdp_reqest_range_high_ & 0xff; // high
continue_data_offset = 19;
}
sdpcmdbuf[continue_data_offset++] = cb; // Count of continuation bytes
for (uint8_t i = 0; i < cb; i++) sdpcmdbuf[continue_data_offset + i] = continue_state[i];
uint16_t packet_size = (continue_data_offset - 5) + cb;
sdpcmdbuf[3] = packet_size >> 8; // Data in the rest...
sdpcmdbuf[4] = packet_size & 0xff; //
// Now lets try to send the packet
btController_->sendL2CapCommand(sdpcmdbuf, continue_data_offset + cb, sdp_scid_);
}
//----------------------------------------------------------------
// Some SDP stuff.
void BluetoothConnection::process_sdp_service_search_request(uint8_t *data) {
DBGPrintf("process_sdp_service_search_request\n");
}
void BluetoothConnection::process_sdp_service_search_response(uint8_t *data) {
DBGPrintf("process_sdp_service_search_response\n");
// (processed before us)(0) 48 20 1A 00 16 00 40 00
// (0) 03 00 01 00 11
// (5) 00 03 00 03 00 00 00 00 00 01 00 00 00 01 00 01 00
uint16_t total_count = (data[5] << 8) + data[6];
uint16_t current_count = (data[7] << 8) + data[8];
uint8_t offset = 9;
DBGPrintf("\tTotal Count:%u Current:%u", total_count, current_count);
for (uint8_t i = 0; i < current_count; i++) {
uint32_t srh = ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (data[offset + 3]);
DBGPrintf(" %08X", srh);
offset += 4;
}
uint8_t continue_state_count = data[offset++];
DBGPrintf(" Cont CB:%u", continue_state_count);
for (uint8_t i = 0; i < continue_state_count; i++) DBGPrintf(" %02X", data[offset++]);
DBGPrintf("\n");
}
void BluetoothConnection::process_sdp_service_attribute_request(uint8_t *data) {
DBGPrintf("process_sdp_service_attribute_request\n");
}
void BluetoothConnection::process_sdp_service_attribute_response(uint8_t *data) {
DBGPrintf("process_sdp_service_attribute_response:\n");
}
void BluetoothConnection::process_sdp_service_search_attribute_request(uint8_t *data) {
DBGPrintf("\n### process_sdp_service_search_attribute_request ###\n");
// Print out the data like UHS2
#ifdef DEBUG_BT
uint16_t service = data[1] << 8 | data[2];
uint16_t length = data[3] << 8 | data[4];
uint16_t uuid = (data[8] << 8 | data[9]);
if (uuid == 0x0000) // Check if it's sending the UUID as a 128-bit UUID
uuid = (data[10] << 8 | data[11]);
DBGPrintf(" Service:%x UUID:%x Length:%u\n", service, uuid, length);
DBGPrintf(" Data:");
for (uint8_t i = 0; i < length; i++) {
DBGPrintf("%02x ", data[5 + i]);
}
DBGPrintf("\n");
#endif
// lets respond saying we don't support the service
uint8_t l2capbuf[10];
l2capbuf[0] = SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE;
l2capbuf[1] = data[1];
l2capbuf[2] = data[2];
l2capbuf[3] = 0x00; // MSB Parameter Length
l2capbuf[4] = 0x05; // LSB Parameter Length = 5
l2capbuf[5] = 0x00; // MSB AttributeListsByteCount
l2capbuf[6] = 0x02; // LSB AttributeListsByteCount = 2
/* Attribute ID/Value Sequence: */
l2capbuf[7] = 0x35; // Data element sequence - length in next byte
l2capbuf[8] = 0x00; // Length = 0
l2capbuf[9] = 0x00; // No continuation state
DBGPrintf("Send SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE not supported\n");
btController_->sendL2CapCommand(device_connection_handle_, l2capbuf, sizeof(l2capbuf),
sdp_scid_ & 0xff, sdp_scid_ >> 8);
}
void BluetoothConnection::process_sdp_service_search_attribute_response(uint8_t *data) {
//before :0B 20 34 00 30 00 40 00
// (00) 07 00 01 00 2B
// (5) 00 26 // cb_data
// (7) 36 03 A2 36 00 8E 09 00 00 0A 00 00 00 00 09 00 01 35 03 19 10 00 09 00 04 35 0D 35 06 19 01 00 09 00 01 35 03 19 // data
// (7 + cb_data) 02 00 26 //
uint16_t cb_data = (data[5] << 8) + data[6];
uint8_t cb_cont = data[7 + cb_data];
uint8_t *pb_cont = &data[8 + cb_data];
DBGPrintf("process_sdp_service_search_attribute_response: cb dat:%u cont:%u ", cb_data, cb_cont);
for (uint8_t i = 0; i < cb_cont; i++) DBGPrintf(" %02X", pb_cont[i]);
DBGPrintf("\n");
// lets copy the data in...
uint16_t cb_space = sdp_request_buffer_cb_ - sdp_request_buffer_used_cnt_;
if (cb_data > cb_space) cb_data = cb_space;
memcpy(&sdp_request_buffer_[sdp_request_buffer_used_cnt_], &data[7], cb_data);
sdp_request_buffer_used_cnt_ += cb_data;
// Now see if we are done or not, if not start up next request
if ((cb_cont == 0) || (sdp_request_buffer_used_cnt_ == sdp_request_buffer_cb_)) {
sdp_request_completed_ = true;
if (device_driver_) {
device_driver_->sdp_command_completed(true); // We skip the first byte...
} else {
// Must be our own... we should now try to proces it.
completeSDPRequest(true);
}
} else {
send_SDP_ServiceSearchAttributeRequest(pb_cont, cb_cont);
}
}
//=============================================================================
// HID SUPPORT - maybe split out
//=============================================================================
// lets use the SDP Support functions to try to retrieve the
// HID Descriptor using SDP
//=============================================================================
bool BluetoothConnection::completeSDPRequest(bool success)
{
if (!success) return false;
// Now real hack:
// Lets see if we can now print out the report descriptor.
uint32_t cb_left = sdp_request_buffer_used_cnt_;
uint8_t *pb = sdp_buffer_; // start at second byte;
sdp_element_t sdpe;
bool found_report_attribute = false;
while (cb_left > 0) {
int cb = extract_next_SDP_Token(pb, cb_left, sdpe);
if (cb <= 0 ) break;
// Should do a lot more validation, but ...
// At least check that we see a value with our attribute number
if ((sdpe.dtype == DU32) && (sdpe.data.uw == 0x206)) found_report_attribute = true;
if (found_report_attribute && (sdpe.element_type == 4) && (sdpe.dtype == DPB)) {
descsize_ = sdpe.element_size;
memcpy(descriptor_, sdpe.data.pb, descsize_);
dumpHIDReportDescriptor();
have_hid_descriptor_ = true;
parse();
return true;
}
cb_left -= cb;
pb += cb;
}
return false;
}
//BUGBUG: move to class or ...
int BluetoothConnection::extract_next_SDP_Token(uint8_t *pbElement, int cb_left, sdp_element_t &sdpe) {
uint8_t element = *pbElement; // first byte is type of element
sdpe.element_type = element >> 3;
sdpe.element_size = element & 7;
sdpe.data.luw = 0; // start off 0
int size_of_element = -1; // should never use this initialized value
switch (sdpe.element_size) {
case 0: size_of_element = 2; break;
case 1: size_of_element = 3; break;
case 2: size_of_element = 5; break;
case 3: size_of_element = 9; break;
case 4: size_of_element = 17; break;
case 5: size_of_element = pbElement[1] + 2; break;
case 6: size_of_element = (pbElement[1] << 8) + pbElement[2] + 3; break;
case 7: size_of_element = (uint32_t)(pbElement[1] << 24) + (uint32_t)(pbElement[2] << 16) + (pbElement[3] << 8) + pbElement[4] + 5; break;