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
80
83
86
89
92
95
98
100
102
103
104
105
106
116
117
127
128
129
130
131
139
140
141
142
143
144
145
149
150
151
152
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
194
195
196
197
201
202
203
213
214
224
225
226
227
235
236
245
246
255
256
265
266
267
268
269
270
271
272
273
282
283
286
287
288
289
290
291
292
293
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
315
316
317
318
319
320
321
322
323
324
325
326
327
331
332
333
343
344
352
353
362
363
371
372
380
381
382
383
384
385
395
396
397
398
399
407
408
417
418
419
420
421
422
439
440
441
442
443
451
452
453
454
455
456
457
458
462
463
464
465
473
474
475
476
477
478
479
480
484
485
486
487
488
489
493
494
495
496
497
498
499
500
501
502
503
504
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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
629
630
631
632
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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
792
793
794
795
796
799
800
801
802
803
804
805
806
807
808
809
821
822
823
824
825
826
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
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
887
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
913
914
915
919
920
921
922
926
927
928
929
930
931
932
933
934
935
936
937
938
939
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
983
984
985
986
987
988
989
990
991
992
997
998
999
1003
1007
1011
1015
1019
1024
1028
1032
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1065
1066
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1153
1154
1155
1156
1157
1158
1159
1160
1163
1164
1165
1166
1178
1179
1180
1181
1182
1183
1184
1188
1189
1190
1191
1192
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1210
1211
1216
1217
1218
1219
1220
1221
1222
1227
1228
1234
1235
1236
1237
1238
1239
1242
1243
1244
1247
1248
1249
1250
1251
1256
1257
1258
1259
1260
1261
1271
1272
1273
1276
1277
1278
1281
1282
1283
1284
1287
1288
1289
1290
1291
1292
1293
1294
1297
1298
1299
1300
1301
1302
1303
1304
1305
1308
1309
1310
1311
1312
1313
1314
1318
1319
1322
1323
1324
1325
1326
1327
1328
1331
1334
1335
1336
1337
1338
1346
1347
1350
1351
1352
1353
1354
1355
1360
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1378
1379
1380
1381
1383
1384
1385
1388
1389
1390
1391
1392
1393
1394
1397
1398
1399
1402
1403
1404
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1421
1422
1423
1424
1425
1426
1427
1428
1431
1432
1435
1436
1437
1438
1441
1442
1443
1444
1445
1448
1449
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1467
1468
1471
1472
1473
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1494
1495
1498
1499
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1516
1517
1520
1521
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1538
1539
1542
1543
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1562
1563
1566
1567
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1583
1584
1587
1588
1589
1590
1593
1594
1597
1598
1602
1603
1604
1605
1606
1607
1608
1611
1612
1615
1616
1617
1621
1622
1623
1624
1625
1629
1630
/* ... */
#define BTSTACK_FILE__ "hids_client.c"
#include "btstack_config.h"
#ifdef ENABLE_TESTING_SUPPORT
#include <stdio.h>
#endif
#include <stdint.h>
#include <string.h>
#include "ble/gatt-service/hids_client.h"
#include "btstack_memory.h"
#include "ble/core.h"
#include "ble/gatt_client.h"
#include "bluetooth_gatt.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_run_loop.h"
#include "gap.h"
11 includes
#define HID_REPORT_MODE_REPORT_ID 3
#define HID_REPORT_MODE_REPORT_MAP_ID 4
#define HID_REPORT_MODE_HID_INFORMATION_ID 5
#define HID_REPORT_MODE_HID_CONTROL_POINT_ID 6
static btstack_linked_list_t clients;
static uint16_t hids_cid_counter = 0;
static uint8_t * hids_client_descriptor_storage;
static uint16_t hids_client_descriptor_storage_len;
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void hids_client_handle_can_write_without_reponse(void * context);
#ifdef ENABLE_TESTING_SUPPORT
static char * hid_characteristic_name(uint16_t uuid){
switch (uuid){
case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE:
return "PROTOCOL_MODE";
case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE:
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT:
return "BOOT_KEYBOARD_INPUT_REPORT";
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT:
return "BOOT_MOUSE_INPUT_REPORT";
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT:
return "BOOT_KEYBOARD_OUTPUT_REPORT";
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT:
return "REPORT";
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP:
return "REPORT_MAP";
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP:
case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION:
return "HID_INFORMATION";
case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION:
case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT:
return "HID_CONTROL_POINT";case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT:
default:
return "UKNOWN";default
}switch (uuid) { ... }
}hid_characteristic_name (uint16_t uuid) { ... }
/* ... */#endif
static hids_client_t * hids_get_client_for_con_handle(hci_con_handle_t con_handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &clients);
while (btstack_linked_list_iterator_has_next(&it)){
hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
if (client->con_handle != con_handle) continue;
return client;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static hids_client_t * hids_get_client_for_cid(uint16_t hids_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &clients);
while (btstack_linked_list_iterator_has_next(&it)){
hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
if (client->cid != hids_cid) continue;
return client;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static uint16_t hids_client_descriptors_len(hids_client_t * client){
uint16_t descriptors_len = 0;
uint8_t service_index;
for (service_index = 0; service_index < client->num_instances; service_index++){
descriptors_len += client->services[service_index].hid_descriptor_len;
}for (service_index = 0; service_index < client->num_instances; service_index++) { ... }
return descriptors_len;
}{ ... }
static uint16_t hids_client_descriptor_storage_get_available_space(void){
uint16_t free_space = hids_client_descriptor_storage_len;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &clients);
while (btstack_linked_list_iterator_has_next(&it)){
hids_client_t * client = (hids_client_t *)btstack_linked_list_iterator_next(&it);
free_space -= hids_client_descriptors_len(client);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return free_space;
}{ ... }
static void hids_client_descriptor_storage_init(hids_client_t * client, uint8_t service_index){
uint16_t available_space = hids_client_descriptor_storage_get_available_space();
client->services[service_index].hid_descriptor_len = 0;
client->services[service_index].hid_descriptor_max_len = available_space;
client->services[service_index].hid_descriptor_offset = hids_client_descriptor_storage_len - available_space;
}{ ... }
static bool hids_client_descriptor_storage_store(hids_client_t * client, uint8_t service_index, uint8_t byte){
if (client->services[service_index].hid_descriptor_len >= client->services[service_index].hid_descriptor_max_len) return false;
hids_client_descriptor_storage[client->services[service_index].hid_descriptor_offset + client->services[service_index].hid_descriptor_len] = byte;
client->services[service_index].hid_descriptor_len++;
return true;
}{ ... }
static void hids_client_descriptor_storage_delete(hids_client_t * client){
uint8_t service_index;
uint16_t descriptors_len = hids_client_descriptors_len(client);
if (descriptors_len > 0){
uint16_t next_offset = client->services[0].hid_descriptor_offset + descriptors_len;
memmove(&hids_client_descriptor_storage[client->services[0].hid_descriptor_offset],
&hids_client_descriptor_storage[next_offset],
hids_client_descriptor_storage_len - next_offset);
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &clients);
while (btstack_linked_list_iterator_has_next(&it)){
hids_client_t * conn = (hids_client_t *)btstack_linked_list_iterator_next(&it);
if (conn == client) continue;
for (service_index = 0; service_index < client->num_instances; service_index++){
if (conn->services[service_index].hid_descriptor_offset >= next_offset){
conn->services[service_index].hid_descriptor_offset -= descriptors_len;
}if (conn->services[service_index].hid_descriptor_offset >= next_offset) { ... }
}for (service_index = 0; service_index < client->num_instances; service_index++) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}if (descriptors_len > 0) { ... }
for (service_index = 0; service_index < client->num_instances; service_index++){
client->services[service_index].hid_descriptor_len = 0;
client->services[service_index].hid_descriptor_offset = 0;
}for (service_index = 0; service_index < client->num_instances; service_index++) { ... }
}{ ... }
const uint8_t * hids_client_descriptor_storage_get_descriptor_data(uint16_t hids_cid, uint8_t service_index){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return NULL;
}if (client == NULL) { ... }
if (service_index >= client->num_instances){
return NULL;
}if (service_index >= client->num_instances) { ... }
return &hids_client_descriptor_storage[client->services[service_index].hid_descriptor_offset];
}{ ... }
uint16_t hids_client_descriptor_storage_get_descriptor_len(uint16_t hids_cid, uint8_t service_index){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return 0;
}if (client == NULL) { ... }
if (service_index >= client->num_instances){
return 0;
}if (service_index >= client->num_instances) { ... }
return client->services[service_index].hid_descriptor_len;
}{ ... }
static uint16_t hids_get_next_cid(void){
if (hids_cid_counter == 0xffff) {
hids_cid_counter = 1;
}if (hids_cid_counter == 0xffff) { ... } else {
hids_cid_counter++;
}else { ... }
return hids_cid_counter;
}{ ... }
static uint8_t find_report_index_for_value_handle(hids_client_t * client, uint16_t value_handle){
uint8_t i;
for (i = 0; i < client->num_reports; i++){
if (client->reports[i].value_handle == value_handle){
return i;
}if (client->reports[i].value_handle == value_handle) { ... }
}for (i = 0; i < client->num_reports; i++) { ... }
return HIDS_CLIENT_INVALID_REPORT_INDEX;
}{ ... }
static uint8_t find_external_report_index_for_value_handle(hids_client_t * client, uint16_t value_handle){
uint8_t i;
for (i = 0; i < client->num_external_reports; i++){
if (client->external_reports[i].value_handle == value_handle){
return i;
}if (client->external_reports[i].value_handle == value_handle) { ... }
}for (i = 0; i < client->num_external_reports; i++) { ... }
return HIDS_CLIENT_INVALID_REPORT_INDEX;
}{ ... }
static bool external_report_index_for_uuid_exists(hids_client_t * client, uint16_t uuid16){
uint8_t i;
for (i = 0; i < client->num_external_reports; i++){
if (client->external_reports[i].external_report_reference_uuid == uuid16){
return true;
}if (client->external_reports[i].external_report_reference_uuid == uuid16) { ... }
}for (i = 0; i < client->num_external_reports; i++) { ... }
return false;
}{ ... }
static uint8_t find_report_index_for_report_id_and_report_type(hids_client_t * client, uint8_t report_id, hid_report_type_t report_type){
uint8_t i;
for (i = 0; i < client->num_reports; i++){
hids_client_report_t report = client->reports[i];
hid_protocol_mode_t protocol_mode = client->services[report.service_index].protocol_mode;
if (protocol_mode == HID_PROTOCOL_MODE_BOOT){
if (!client->reports[i].boot_report){
continue;
}if (!client->reports[i].boot_report) { ... }
}if (protocol_mode == HID_PROTOCOL_MODE_BOOT) { ... } else if (protocol_mode == HID_PROTOCOL_MODE_REPORT){
if (client->reports[i].boot_report){
continue;
}if (client->reports[i].boot_report) { ... }
}else if (protocol_mode == HID_PROTOCOL_MODE_REPORT) { ... }
if (report.report_id == report_id && report.report_type == report_type){
return i;
}if (report.report_id == report_id && report.report_type == report_type) { ... }
}for (i = 0; i < client->num_reports; i++) { ... }
return HIDS_CLIENT_INVALID_REPORT_INDEX;
}{ ... }
static uint8_t hids_client_add_characteristic(hids_client_t * client, gatt_client_characteristic_t * characteristic, uint8_t report_id, hid_report_type_t report_type, bool boot_report){
uint8_t report_index = find_external_report_index_for_value_handle(client, characteristic->value_handle);
if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
return report_index;
}if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
report_index = client->num_reports;
if (report_index < HIDS_CLIENT_NUM_REPORTS) {
client->reports[report_index].value_handle = characteristic->value_handle;
client->reports[report_index].end_handle = characteristic->end_handle;
client->reports[report_index].properties = characteristic->properties;
client->reports[report_index].service_index = client->service_index;
client->reports[report_index].report_id = report_id;
client->reports[report_index].report_type = report_type;
client->reports[report_index].boot_report = boot_report;
log_info("add index %d, id %d, type %d, value handle 0x%02x, properties 0x%02x", report_index, report_id, report_type, characteristic->value_handle, characteristic->properties);
client->num_reports++;
return report_index;
}if (report_index < HIDS_CLIENT_NUM_REPORTS) { ... } else {
log_info("not enough storage, increase HIDS_CLIENT_NUM_REPORTS");
return HIDS_CLIENT_INVALID_REPORT_INDEX;
}else { ... }
}{ ... }
static uint8_t hids_client_add_external_report(hids_client_t * client, gatt_client_characteristic_descriptor_t * characteristic_descriptor){
uint8_t report_index = client->num_external_reports;
if (report_index < HIDS_CLIENT_NUM_REPORTS) {
client->external_reports[report_index].value_handle = characteristic_descriptor->handle;
client->external_reports[report_index].service_index = client->service_index;
client->num_external_reports++;
log_info("add external index %d [%d], value handle 0x%02x", report_index, client->num_external_reports, characteristic_descriptor->handle);
return report_index;
}if (report_index < HIDS_CLIENT_NUM_REPORTS) { ... } else {
log_info("not enough storage, increase HIDS_CLIENT_NUM_REPORTS");
return HIDS_CLIENT_INVALID_REPORT_INDEX;
}else { ... }
}{ ... }
static uint8_t hids_client_get_next_active_report_map_index(hids_client_t * client){
uint8_t i;
for (i = client->service_index; i < client->num_instances; i++){
if (client->services[i].report_map_value_handle != 0){
return i;
}if (client->services[i].report_map_value_handle != 0) { ... }
}for (i = client->service_index; i < client->num_instances; i++) { ... }
client->service_index = HIDS_CLIENT_INVALID_REPORT_INDEX;
return HIDS_CLIENT_INVALID_REPORT_INDEX;
}{ ... }
static bool hids_client_report_query_next_report_map(hids_client_t * client){
client->service_index++;
if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR;
return true;
}if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static bool hids_client_report_map_query_init(hids_client_t * client){
client->service_index = 0;
if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR;
return true;
}if (hids_client_get_next_active_report_map_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static bool hids_client_report_query_next_report_map_uuid(hids_client_t * client){
client->report_index++;
if (client->report_index < client->num_external_reports){
client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID;
return true;
}if (client->report_index < client->num_external_reports) { ... }
return false;
}{ ... }
static bool hids_client_report_map_uuid_query_init(hids_client_t * client){
client->report_index = 0;
if (client->num_external_reports > 0){
client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID;
return true;
}if (client->num_external_reports > 0) { ... }
return false;
}{ ... }
static uint8_t hids_client_get_next_report_index(hids_client_t * client){
uint8_t i;
uint8_t index = HIDS_CLIENT_INVALID_REPORT_INDEX;
for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++){
hids_client_report_t report = client->reports[i];
if (!report.boot_report){
if (report.report_type == HID_REPORT_TYPE_RESERVED && report.report_id == HID_REPORT_MODE_REPORT_ID){
index = i;
client->service_index = report.service_index;
break;
}if (report.report_type == HID_REPORT_TYPE_RESERVED && report.report_id == HID_REPORT_MODE_REPORT_ID) { ... }
}if (!report.boot_report) { ... }
}for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++) { ... }
client->report_index = index;
return index;
}{ ... }
static bool hids_client_report_query_next_report(hids_client_t * client){
client->report_index++;
if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_FIND_REPORT;
return true;
}if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static bool hids_client_report_query_init(hids_client_t * client){
client->report_index = 0;
if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_FIND_REPORT;
return true;
}if (hids_client_get_next_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static uint8_t hids_client_get_next_notification_report_index(hids_client_t * client){
uint8_t i;
uint8_t index = HIDS_CLIENT_INVALID_REPORT_INDEX;
for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++){
hids_client_report_t report = client->reports[i];
hid_protocol_mode_t protocol_mode = client->services[report.service_index].protocol_mode;
if (protocol_mode == HID_PROTOCOL_MODE_BOOT){
if (!client->reports[i].boot_report){
continue;
}if (!client->reports[i].boot_report) { ... }
}if (protocol_mode == HID_PROTOCOL_MODE_BOOT) { ... } else if (protocol_mode == HID_PROTOCOL_MODE_REPORT){
if (client->reports[i].boot_report){
continue;
}if (client->reports[i].boot_report) { ... }
}else if (protocol_mode == HID_PROTOCOL_MODE_REPORT) { ... }
if (report.report_type == HID_REPORT_TYPE_INPUT){
index = i;
}if (report.report_type == HID_REPORT_TYPE_INPUT) { ... }
}for (i = client->report_index; i < client->num_reports && (index == HIDS_CLIENT_INVALID_REPORT_INDEX); i++) { ... }
client->report_index = index;
return index;
}{ ... }
static bool hids_client_report_next_notification_report_index(hids_client_t * client){
client->report_index++;
if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS;
return true;
}if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static bool hids_client_report_notifications_init(hids_client_t * client){
#ifdef ENABLE_TESTING_SUPPORT
printf("\nRegister for Notifications: \n");
#endif
client->report_index = 0;
if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS;
return true;
}if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static bool hids_client_report_next_notifications_configuration_report_index(hids_client_t * client){
client->report_index++;
if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS;
return true;
}if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static bool hids_client_notifications_configuration_init(hids_client_t * client){
#ifdef ENABLE_TESTING_SUPPORT
printf("\nConfigure for Notifications: \n");
#endif
client->report_index = 0;
if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->state = HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS;
return true;
}if (hids_client_get_next_notification_report_index(client) != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
return false;
}{ ... }
static hids_client_t * hids_create_client(hci_con_handle_t con_handle, uint16_t cid){
hids_client_t * client = btstack_memory_hids_client_get();
if (!client){
log_error("Not enough memory to create client");
return NULL;
}if (!client) { ... }
client->state = HIDS_CLIENT_STATE_IDLE;
client->cid = cid;
client->con_handle = con_handle;
btstack_linked_list_add(&clients, (btstack_linked_item_t *) client);
return client;
}{ ... }
static void hids_finalize_client(hids_client_t * client){
uint8_t i;
for (i = 0; i < client->num_reports; i++){
gatt_client_stop_listening_for_characteristic_value_updates(&client->reports[i].notification_listener);
}for (i = 0; i < client->num_reports; i++) { ... }
hids_client_descriptor_storage_delete(client);
btstack_linked_list_remove(&clients, (btstack_linked_item_t *) client);
btstack_memory_hids_client_free(client);
}{ ... }
static void hids_emit_connection_established(hids_client_t * client, uint8_t status){
uint8_t event[8];
int pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED;
little_endian_store_16(event, pos, client->cid);
pos += 2;
event[pos++] = status;
event[pos++] = client->services[0].protocol_mode;
event[pos++] = client->num_instances;
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}{ ... }
static void hids_emit_notifications_configuration(hids_client_t * client){
uint8_t event[6];
int pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = GATTSERVICE_SUBEVENT_HID_SERVICE_REPORTS_NOTIFICATION;
little_endian_store_16(event, pos, client->cid);
pos += 2;
event[pos++] = client->value;
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}{ ... }
static uint16_t hids_client_setup_report_event(uint8_t subevent, hids_client_t *client, uint8_t report_index, uint8_t *buffer,
uint16_t report_len) {
uint16_t pos = 0;
buffer[pos++] = HCI_EVENT_GATTSERVICE_META;
pos++;
buffer[pos++] = subevent;
little_endian_store_16(buffer, pos, client->cid);
pos += 2;
buffer[pos++] = client->reports[report_index].service_index;
buffer[pos++] = client->reports[report_index].report_id;
little_endian_store_16(buffer, pos, report_len);
pos += 2;
buffer[1] = pos + report_len - 2;
return pos;
}{ ... }
static void hids_client_setup_report_event_with_report_id(hids_client_t * client, uint8_t report_index, uint8_t *buffer, uint16_t report_len){
uint16_t pos = hids_client_setup_report_event(GATTSERVICE_SUBEVENT_HID_REPORT, client, report_index, buffer,
report_len + 1);
buffer[pos] = client->reports[report_index].report_id;
}{ ... }
static void hids_client_emit_hid_information_event(hids_client_t * client, const uint8_t *value, uint16_t value_len){
if (value_len != 4) return;
uint8_t event[11];
int pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = GATTSERVICE_SUBEVENT_HID_INFORMATION;
little_endian_store_16(event, pos, client->cid);
pos += 2;
event[pos++] = client->service_index;
memcpy(event+pos, value, 3);
pos += 3;
event[pos++] = (value[3] & 0x02) >> 1;
event[pos++] = value[3] & 0x01;
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}{ ... }
static void hids_client_emit_protocol_mode_event(hids_client_t * client, const uint8_t *value, uint16_t value_len){
if (value_len != 1) return;
uint8_t event[11];
int pos = 0;
event[pos++] = HCI_EVENT_GATTSERVICE_META;
event[pos++] = sizeof(event) - 2;
event[pos++] = GATTSERVICE_SUBEVENT_HID_PROTOCOL_MODE;
little_endian_store_16(event, pos, client->cid);
pos += 2;
event[pos++] = client->service_index;
event[pos++] = value[0];
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}{ ... }
static void handle_notification_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
UNUSED(packet_type);
UNUSED(channel);
if (hci_event_packet_get_type(packet) != GATT_EVENT_NOTIFICATION) return;
hids_client_t * client = hids_get_client_for_con_handle(gatt_event_notification_get_handle(packet));
if (client == NULL) return;
uint8_t report_index = find_report_index_for_value_handle(client, gatt_event_notification_get_value_handle(packet));
if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
return;
}if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
uint8_t * in_place_event = &packet[-2];
hids_client_setup_report_event_with_report_id(client, report_index, in_place_event,
gatt_event_notification_get_value_length(packet));
(*client->client_handler)(HCI_EVENT_GATTSERVICE_META, client->cid, in_place_event, size + 2);
}{ ... }
static void handle_report_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
UNUSED(packet_type);
UNUSED(channel);
if (hci_event_packet_get_type(packet) != GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT) return;
hids_client_t * client = hids_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet));
if (client == NULL) return;
if (client->state != HIDS_CLIENT_W4_GET_REPORT_RESULT){
return;
}if (client->state != HIDS_CLIENT_W4_GET_REPORT_RESULT) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
uint8_t report_index = find_report_index_for_value_handle(client, gatt_event_characteristic_value_query_result_get_value_handle(packet));
if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
return;
}if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
uint8_t * in_place_event = &packet[-2];
hids_client_setup_report_event_with_report_id(client, report_index, in_place_event,
gatt_event_characteristic_value_query_result_get_value_length(packet));
(*client->client_handler)(HCI_EVENT_GATTSERVICE_META, client->cid, in_place_event, size + 2);
}{ ... }
static void hids_run_for_client(hids_client_t * client){
uint8_t att_status;
gatt_client_service_t service;
gatt_client_characteristic_t characteristic;
switch (client->state){
case HIDS_CLIENT_STATE_W2_QUERY_SERVICE:
#ifdef ENABLE_TESTING_SUPPORT
printf("\n\nQuery Services:\n");
#endif
client->state = HIDS_CLIENT_STATE_W4_SERVICE_RESULT;
att_status = gatt_client_discover_primary_services_by_uuid16(handle_gatt_client_event, client->con_handle, ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_QUERY_SERVICE:
case HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC:
#ifdef ENABLE_TESTING_SUPPORT
printf("\n\nQuery Characteristics of service %d:\n", client->service_index);
#endif
client->state = HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT;
service.start_group_handle = client->services[client->service_index].start_handle;
service.end_group_handle = client->services[client->service_index].end_handle;
att_status = gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, client->con_handle, &service);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC:
case HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR:
#ifdef ENABLE_TESTING_SUPPORT
printf("\n\nRead REPORT_MAP (Handle 0x%04X) HID Descriptor of service %d:\n", client->services[client->service_index].report_map_value_handle, client->service_index);
#endif
client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR;
att_status = gatt_client_read_long_value_of_characteristic_using_value_handle(&handle_gatt_client_event, client->con_handle, client->services[client->service_index].report_map_value_handle);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_READ_REPORT_MAP_HID_DESCRIPTOR:
case HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS:
#ifdef ENABLE_TESTING_SUPPORT
printf("\nDiscover REPORT_MAP (Handle 0x%04X) Characteristic Descriptors of service %d:\n", client->services[client->service_index].report_map_value_handle, client->service_index);
#endif
client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT;
characteristic.value_handle = client->services[client->service_index].report_map_value_handle;
characteristic.end_handle = client->services[client->service_index].report_map_end_handle;
att_status = gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS:
case HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID:
#ifdef ENABLE_TESTING_SUPPORT
printf("\nRead external chr UUID (Handle 0x%04X) Characteristic Descriptors, service index %d:\n", client->external_reports[client->report_index].value_handle, client->service_index);
#endif
client->state = HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID;
att_status = gatt_client_read_characteristic_descriptor_using_descriptor_handle(&handle_gatt_client_event, client->con_handle, client->external_reports[client->report_index].value_handle);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_REPORT_MAP_READ_EXTERNAL_REPORT_REFERENCE_UUID:
case HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC:
#ifdef ENABLE_TESTING_SUPPORT
printf("\nDiscover External Report Characteristic:\n");
#endif
client->state = HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT;
service.start_group_handle = 0x0001;
service.end_group_handle = 0xffff;
att_status = gatt_client_discover_characteristics_for_service(&handle_gatt_client_event, client->con_handle, &service);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC:
case HIDS_CLIENT_STATE_W2_FIND_REPORT:
#ifdef ENABLE_TESTING_SUPPORT
printf("\nQuery Report Characteristic Descriptors [%d, %d, 0x%04X]:\n",
client->report_index,
client->reports[client->report_index].service_index,
client->reports[client->report_index].value_handle);/* ... */
#endif
client->state = HIDS_CLIENT_STATE_W4_REPORT_FOUND;
client->handle = 0;
characteristic.value_handle = client->reports[client->report_index].value_handle;
characteristic.end_handle = client->reports[client->report_index].end_handle;
characteristic.properties = client->reports[client->report_index].properties;
att_status = gatt_client_discover_characteristic_descriptors(&handle_gatt_client_event, client->con_handle, &characteristic);
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_FIND_REPORT:
case HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE:
client->state = HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE;
att_status = gatt_client_read_characteristic_descriptor_using_descriptor_handle(&handle_gatt_client_event, client->con_handle, client->handle);
client->handle = 0;
UNUSED(att_status);
break;
case HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE:
case HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS:
#ifdef ENABLE_TESTING_SUPPORT
if (client->value > 0){
printf(" Notification configuration enable ");
}if (client->value > 0) { ... } else {
printf(" Notification configuration disable ");
}else { ... }
printf("[%d, %d, 0x%04X]:\n",
client->report_index,
client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);/* ... */
#endif
client->state = HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED;
characteristic.value_handle = client->reports[client->report_index].value_handle;
characteristic.end_handle = client->reports[client->report_index].end_handle;
characteristic.properties = client->reports[client->report_index].properties;
att_status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, client->con_handle, &characteristic, client->value);
if (att_status == ERROR_CODE_SUCCESS){
switch(client->value){
case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION:
gatt_client_listen_for_characteristic_value_updates(
&client->reports[client->report_index].notification_listener,
&handle_notification_event, client->con_handle, &characteristic);
break;case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION:
default:
gatt_client_stop_listening_for_characteristic_value_updates(&client->reports[client->report_index].notification_listener);
break;default
}switch (client->value) { ... }
}if (att_status == ERROR_CODE_SUCCESS) { ... } else {
if (hids_client_report_next_notifications_configuration_report_index(client)){
hids_run_for_client(client);
break;
}if (hids_client_report_next_notifications_configuration_report_index(client)) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
}else { ... }
break;
case HIDS_CLIENT_STATE_W2_CONFIGURE_NOTIFICATIONS:
case HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS:
#ifdef ENABLE_TESTING_SUPPORT
printf(" Notification enable [%d, %d, 0x%04X]:\n",
client->report_index,
client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);/* ... */
#endif
client->state = HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED;
characteristic.value_handle = client->reports[client->report_index].value_handle;
characteristic.end_handle = client->reports[client->report_index].end_handle;
characteristic.properties = client->reports[client->report_index].properties;
att_status = gatt_client_write_client_characteristic_configuration(&handle_gatt_client_event, client->con_handle, &characteristic, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION);
if (att_status == ERROR_CODE_SUCCESS){
gatt_client_listen_for_characteristic_value_updates(
&client->reports[client->report_index].notification_listener,
&handle_notification_event, client->con_handle, &characteristic);
}if (att_status == ERROR_CODE_SUCCESS) { ... } else {
if (hids_client_report_next_notification_report_index(client)){
hids_run_for_client(client);
break;
}if (hids_client_report_next_notification_report_index(client)) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
}else { ... }
break;
case HIDS_CLIENT_STATE_W2_ENABLE_INPUT_REPORTS:
case HIDS_CLIENT_W2_SEND_WRITE_REPORT:
#ifdef ENABLE_TESTING_SUPPORT
printf(" Write report [%d, %d, 0x%04X]:\n",
client->report_index,
client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);/* ... */
#endif
client->state = HIDS_CLIENT_W4_WRITE_REPORT_DONE;
att_status = gatt_client_write_value_of_characteristic(
&handle_gatt_client_event, client->con_handle,
client->reports[client->report_index].value_handle,
client->report_len, (uint8_t *)client->report);
UNUSED(att_status);
break;
case HIDS_CLIENT_W2_SEND_WRITE_REPORT:
case HIDS_CLIENT_W2_SEND_GET_REPORT:
#ifdef ENABLE_TESTING_SUPPORT
printf(" Get report [index %d, ID %d, Service %d, handle 0x%04X]:\n",
client->report_index,
client->reports[client->report_index].report_id,
client->reports[client->report_index].service_index, client->reports[client->report_index].value_handle);/* ... */
#endif
client->state = HIDS_CLIENT_W4_GET_REPORT_RESULT;
att_status = gatt_client_read_value_of_characteristic_using_value_handle(
&handle_report_event,
client->con_handle,
client->reports[client->report_index].value_handle);
UNUSED(att_status);
break;
#ifdef ENABLE_TESTING_SUPPORTcase HIDS_CLIENT_W2_SEND_GET_REPORT:
case HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION:
client->state = HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT;
att_status = gatt_client_read_value_of_characteristic_using_value_handle(
&handle_gatt_client_event,
client->con_handle,
client->reports[client->report_index].ccc_handle);
break;/* ... */
#endifcase HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION:
case HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC:
client->state = HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT;
att_status = gatt_client_read_value_of_characteristic_using_value_handle(
&handle_gatt_client_event,
client->con_handle,
client->handle);
break;
case HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC:
case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE:
case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
client->write_without_response_request.callback = &hids_client_handle_can_write_without_reponse;
client->write_without_response_request.context = client;
(void) gatt_client_request_to_write_without_response(&client->write_without_response_request, client->con_handle);
break;
case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
default:
break;default
}switch (client->state) { ... }
}{ ... }
static void hids_client_handle_can_write_without_reponse(void * context) {
hids_client_t *client = (hids_client_t *) context;
uint8_t att_status;
switch (client->state){
case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE:
att_status = gatt_client_write_value_of_characteristic_without_response(
client->con_handle,
client->services[client->service_index].protocol_mode_value_handle, 1, (uint8_t *)&client->required_protocol_mode);
#ifdef ENABLE_TESTING_SUPPORT
printf("\n\nSet report mode %d of service %d, status 0x%02x", client->required_protocol_mode, client->service_index, att_status);
#endif
if (att_status == ATT_ERROR_SUCCESS){
client->services[client->service_index].protocol_mode = client->required_protocol_mode;
if ((client->service_index + 1) < client->num_instances){
client->service_index++;
hids_run_for_client(client);
break;
}if ((client->service_index + 1) < client->num_instances) { ... }
}if (att_status == ATT_ERROR_SUCCESS) { ... }
if (hids_client_report_map_uuid_query_init(client)){
hids_run_for_client(client);
break;
}if (hids_client_report_map_uuid_query_init(client)) { ... }
if (hids_client_report_query_init(client)){
hids_run_for_client(client);
break;
}if (hids_client_report_query_init(client)) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
break;
case HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE:
case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
#ifdef ENABLE_TESTING_SUPPORT
printf(" Write characteristic [service %d, handle 0x%04X]:\n", client->service_index, client->handle);
#endif
client->state = HIDS_CLIENT_STATE_CONNECTED;
(void) gatt_client_write_value_of_characteristic_without_response(client->con_handle, client->handle, 1, (uint8_t *) &client->value);
break;
case HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE:
default:
break;default
}switch (client->state) { ... }
}{ ... }
static void handle_gatt_client_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(packet_type);
UNUSED(channel);
UNUSED(size);
hids_client_t * client = NULL;
uint8_t att_status;
gatt_client_service_t service;
gatt_client_characteristic_t characteristic;
gatt_client_characteristic_descriptor_t characteristic_descriptor;
const uint8_t * characteristic_descriptor_value;
uint8_t i;
uint8_t report_index;
const uint8_t * value;
uint16_t value_len;
switch(hci_event_packet_get_type(packet)){
case GATT_EVENT_SERVICE_QUERY_RESULT:
client = hids_get_client_for_con_handle(gatt_event_service_query_result_get_handle(packet));
if (client == NULL) break;
if (client->num_instances < MAX_NUM_HID_SERVICES){
uint8_t index = client->num_instances;
gatt_event_service_query_result_get_service(packet, &service);
client->services[index].start_handle = service.start_group_handle;
client->services[index].end_handle = service.end_group_handle;
client->num_instances++;
#ifdef ENABLE_TESTING_SUPPORT
printf("HID Service: start handle 0x%04X, end handle 0x%04X\n", client->services[index].start_handle, client->services[index].end_handle);
#endif
hids_client_descriptor_storage_init(client, index);
}if (client->num_instances < MAX_NUM_HID_SERVICES) { ... } else {
log_info("%d hid services found, only first %d can be stored, increase MAX_NUM_HID_SERVICES", client->num_instances + 1, MAX_NUM_HID_SERVICES);
}else { ... }
break;
case GATT_EVENT_SERVICE_QUERY_RESULT:
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
client = hids_get_client_for_con_handle(gatt_event_characteristic_query_result_get_handle(packet));
if (client == NULL) break;
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
report_index = HIDS_CLIENT_INVALID_REPORT_INDEX;
if (client->state == HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT){
if (!external_report_index_for_uuid_exists(client, characteristic.uuid16)){
break;
}if (!external_report_index_for_uuid_exists(client, characteristic.uuid16)) { ... }
}if (client->state == HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT) { ... }
switch (characteristic.uuid16){
case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE:
client->services[client->service_index].protocol_mode_value_handle = characteristic.value_handle;
break;
case ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE:
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT:
report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_KEYBOARD_ID, HID_REPORT_TYPE_INPUT, true);
break;
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT:
report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_MOUSE_ID, HID_REPORT_TYPE_INPUT, true);
break;
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT:
report_index = hids_client_add_characteristic(client, &characteristic, HID_BOOT_MODE_KEYBOARD_ID, HID_REPORT_TYPE_OUTPUT, true);
break;
case ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT:
report_index = hids_client_add_characteristic(client, &characteristic, HID_REPORT_MODE_REPORT_ID, HID_REPORT_TYPE_RESERVED, false);
break;
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT:
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP:
client->services[client->service_index].report_map_value_handle = characteristic.value_handle;
client->services[client->service_index].report_map_end_handle = characteristic.end_handle;
break;
case ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP:
case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION:
client->services[client->service_index].hid_information_value_handle = characteristic.value_handle;
break;
case ORG_BLUETOOTH_CHARACTERISTIC_HID_INFORMATION:
case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT:
client->services[client->service_index].control_point_value_handle = characteristic.value_handle;
break;
case ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT:
default:
#ifdef ENABLE_TESTING_SUPPORT
printf(" TODO: Found external characteristic 0x%04X\n", characteristic.uuid16);
#endif
return;default
}switch (characteristic.uuid16) { ... }
#ifdef ENABLE_TESTING_SUPPORT
printf("HID Characteristic %s: \n Attribute Handle 0x%04X, Properties 0x%02X, Handle 0x%04X, UUID 0x%04X, service %d",
hid_characteristic_name(characteristic.uuid16),
characteristic.start_handle,
characteristic.properties,
characteristic.value_handle, characteristic.uuid16,
client->service_index);
if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
printf(", report index 0x%02X", report_index);
}if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
printf("\n");/* ... */
#endif
break;
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
client = hids_get_client_for_con_handle(gatt_event_long_characteristic_value_query_result_get_handle(packet));
if (client == NULL) break;
value = gatt_event_long_characteristic_value_query_result_get_value(packet);
value_len = gatt_event_long_characteristic_value_query_result_get_value_length(packet);
#ifdef ENABLE_TESTING_SUPPORT
printf_hexdump(value, value_len);/* ... */
#endif
for (i = 0; i < value_len; i++){
bool stored = hids_client_descriptor_storage_store(client, client->service_index, value[i]);
if (!stored){
client->services[client->service_index].hid_descriptor_status = ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
break;
}if (!stored) { ... }
}for (i = 0; i < value_len; i++) { ... }
break;
case GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT:
case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
client = hids_get_client_for_con_handle(gatt_event_all_characteristic_descriptors_query_result_get_handle(packet));
if (client == NULL) break;
gatt_event_all_characteristic_descriptors_query_result_get_characteristic_descriptor(packet, &characteristic_descriptor);
switch (client->state) {
case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT:
if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE){
report_index = hids_client_add_external_report(client, &characteristic_descriptor);
#ifdef ENABLE_TESTING_SUPPORT
if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
printf(" External Report Reference Characteristic Descriptor: Handle 0x%04X, UUID 0x%04X, service %d, report index 0x%02X\n",
characteristic_descriptor.handle,
characteristic_descriptor.uuid16,
client->service_index, report_index);
}if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
/* ... */#endif
}if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_EXTERNAL_REPORT_REFERENCE) { ... }
break;case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT:
case HIDS_CLIENT_STATE_W4_REPORT_FOUND:
if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE){
client->handle = characteristic_descriptor.handle;
#ifdef ENABLE_TESTING_SUPPORT
printf(" Report Characteristic Report Reference Characteristic Descriptor: Handle 0x%04X, UUID 0x%04X\n",
characteristic_descriptor.handle,
characteristic_descriptor.uuid16);/* ... */
#endif
}if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE) { ... }
#ifdef ENABLE_TESTING_SUPPORT
if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION){
client->reports[client->report_index].ccc_handle = characteristic_descriptor.handle;
printf(" Report Client Characteristic Configuration Descriptor: Handle 0x%04X, UUID 0x%04X\n",
characteristic_descriptor.handle,
characteristic_descriptor.uuid16);
}if (characteristic_descriptor.uuid16 == ORG_BLUETOOTH_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION) { ... }
/* ... */#endif
break;
case HIDS_CLIENT_STATE_W4_REPORT_FOUND:
default:
break;default
}switch (client->state) { ... }
break;
case GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
client = hids_get_client_for_con_handle(gatt_event_characteristic_value_query_result_get_handle(packet));
if (client == NULL) break;
value = gatt_event_characteristic_value_query_result_get_value(packet);
value_len = gatt_event_characteristic_value_query_result_get_value_length(packet);
switch (client->state){
#ifdef ENABLE_TESTING_SUPPORT
case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
printf(" Received CCC value: ");
printf_hexdump(value, value_len);
break;/* ... */
#endif case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:{
uint16_t value_handle = gatt_event_characteristic_value_query_result_get_value_handle(packet);
if (value_handle == client->services[client->service_index].hid_information_value_handle){
hids_client_emit_hid_information_event(client, value, value_len);
break;
}if (value_handle == client->services[client->service_index].hid_information_value_handle) { ... }
if (value_handle == client->services[client->service_index].protocol_mode_value_handle){
hids_client_emit_protocol_mode_event(client, value, value_len);
break;
}if (value_handle == client->services[client->service_index].protocol_mode_value_handle) { ... }
break;
...}case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:
default:
break;default
}switch (client->state) { ... }
break;
case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
client = hids_get_client_for_con_handle(gatt_event_characteristic_descriptor_query_result_get_handle(packet));
if (client == NULL) break;
if (gatt_event_characteristic_descriptor_query_result_get_descriptor_length(packet) != 2){
break;
}if (gatt_event_characteristic_descriptor_query_result_get_descriptor_length(packet) != 2) { ... }
characteristic_descriptor_value = gatt_event_characteristic_descriptor_query_result_get_descriptor(packet);
switch (client->state) {
case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID:
report_index = find_external_report_index_for_value_handle(client, gatt_event_characteristic_descriptor_query_result_get_descriptor_handle(packet));
if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->external_reports[report_index].external_report_reference_uuid = little_endian_read_16(characteristic_descriptor_value, 0);
#ifdef ENABLE_TESTING_SUPPORT
printf(" Update external_report_reference_uuid of report index 0x%02X, service index 0x%02X, UUID 0x%02X\n",
report_index, client->service_index, client->external_reports[report_index].external_report_reference_uuid);/* ... */
#endif
}if (report_index != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
break;
case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID:
case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE:
if (client->report_index != HIDS_CLIENT_INVALID_REPORT_INDEX){
client->reports[client->report_index].report_id = characteristic_descriptor_value[0];
client->reports[client->report_index].report_type = (hid_report_type_t)characteristic_descriptor_value[1];
#ifdef ENABLE_TESTING_SUPPORT
printf(" Update report ID and type [%d, %d] of report index 0x%02X, service index 0x%02X\n",
client->reports[client->report_index].report_id,
client->reports[client->report_index].report_type,
client->report_index, client->service_index);/* ... */
#endif
}if (client->report_index != HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
break;
case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE:
default:
break;default
}switch (client->state) { ... }
break;
case GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT:
case GATT_EVENT_QUERY_COMPLETE:
client = hids_get_client_for_con_handle(gatt_event_query_complete_get_handle(packet));
if (client == NULL) break;
att_status = gatt_event_query_complete_get_att_status(packet);
switch (client->state){
case HIDS_CLIENT_STATE_W4_SERVICE_RESULT:
if (att_status != ATT_ERROR_SUCCESS){
hids_emit_connection_established(client, att_status);
hids_finalize_client(client);
return;
}if (att_status != ATT_ERROR_SUCCESS) { ... }
if (client->num_instances == 0){
hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
hids_finalize_client(client);
return;
}if (client->num_instances == 0) { ... }
client->service_index = 0;
client->state = HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC;
break;
case HIDS_CLIENT_STATE_W4_SERVICE_RESULT:
case HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT:
if (att_status != ATT_ERROR_SUCCESS){
hids_emit_connection_established(client, att_status);
hids_finalize_client(client);
return;
}if (att_status != ATT_ERROR_SUCCESS) { ... }
if ((client->service_index + 1) < client->num_instances){
client->service_index++;
client->state = HIDS_CLIENT_STATE_W2_QUERY_CHARACTERISTIC;
break;
}if ((client->service_index + 1) < client->num_instances) { ... }
btstack_assert(client->required_protocol_mode != HID_PROTOCOL_MODE_REPORT_WITH_FALLBACK_TO_BOOT);
switch (client->required_protocol_mode){
case HID_PROTOCOL_MODE_REPORT:
for (i = 0; i < client->num_instances; i++){
client->services[i].protocol_mode = HID_PROTOCOL_MODE_REPORT;
}for (i = 0; i < client->num_instances; i++) { ... }
if (hids_client_report_map_query_init(client)){
break;
}if (hids_client_report_map_query_init(client)) { ... }
hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
hids_finalize_client(client);
return;
case HID_PROTOCOL_MODE_REPORT:
default:
client->service_index = 0;
client->state = HIDS_CLIENT_STATE_W2_SET_PROTOCOL_MODE_WITHOUT_RESPONSE;
break;default
}switch (client->required_protocol_mode) { ... }
break;
case HIDS_CLIENT_STATE_W4_CHARACTERISTIC_RESULT:
case HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR:
if (att_status != ATT_ERROR_SUCCESS){
hids_emit_connection_established(client, att_status);
hids_finalize_client(client);
return;
}if (att_status != ATT_ERROR_SUCCESS) { ... }
client->state = HIDS_CLIENT_STATE_W2_REPORT_MAP_DISCOVER_CHARACTERISTIC_DESCRIPTORS;
break;
case HIDS_CLIENT_STATE_W4_REPORT_MAP_HID_DESCRIPTOR:
case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT:
if (hids_client_report_query_next_report_map(client)){
break;
}if (hids_client_report_query_next_report_map(client)) { ... }
if (hids_client_report_map_uuid_query_init(client)){
break;
}if (hids_client_report_map_uuid_query_init(client)) { ... }
if (hids_client_report_query_init(client)){
break;
}if (hids_client_report_query_init(client)) { ... }
hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
hids_finalize_client(client);
return;
case HIDS_CLIENT_STATE_W4_REPORT_MAP_CHARACTERISTIC_DESCRIPTORS_RESULT:
case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID:
if (hids_client_report_query_next_report_map_uuid(client)){
break;
}if (hids_client_report_query_next_report_map_uuid(client)) { ... }
client->state = HIDS_CLIENT_STATE_W2_DISCOVER_EXTERNAL_REPORT_CHARACTERISTIC;
break;
case HIDS_CLIENT_STATE_W4_REPORT_MAP_EXTERNAL_REPORT_REFERENCE_UUID:
case HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT:
if (hids_client_report_query_init(client)){
break;
}if (hids_client_report_query_init(client)) { ... }
hids_emit_connection_established(client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
hids_finalize_client(client);
return;
case HIDS_CLIENT_STATE_W4_EXTERNAL_REPORT_CHARACTERISTIC_RESULT:
case HIDS_CLIENT_STATE_W4_REPORT_FOUND:
if (client->handle != 0){
client->state = HIDS_CLIENT_STATE_W2_READ_REPORT_ID_AND_TYPE;
break;
}if (client->handle != 0) { ... }
if (hids_client_report_query_next_report(client)){
break;
}if (hids_client_report_query_next_report(client)) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
break;
case HIDS_CLIENT_STATE_W4_REPORT_FOUND:
case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE:
if (hids_client_report_query_next_report(client)){
break;
}if (hids_client_report_query_next_report(client)) { ... }
if (hids_client_report_notifications_init(client)){
break;
}if (hids_client_report_notifications_init(client)) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
break;
case HIDS_CLIENT_STATE_W4_REPORT_ID_AND_TYPE:
case HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED:
if (hids_client_report_next_notification_report_index(client)){
break;
}if (hids_client_report_next_notification_report_index(client)) { ... }
client->state = HIDS_CLIENT_STATE_CONNECTED;
hids_emit_connection_established(client, ERROR_CODE_SUCCESS);
break;
case HIDS_CLIENT_STATE_W4_INPUT_REPORTS_ENABLED:
case HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED:
if (hids_client_report_next_notifications_configuration_report_index(client)){
break;
}if (hids_client_report_next_notifications_configuration_report_index(client)) { ... }
hids_emit_notifications_configuration(client);
client->state = HIDS_CLIENT_STATE_CONNECTED;
break;
#ifdef ENABLE_TESTING_SUPPORT case HIDS_CLIENT_STATE_W4_NOTIFICATIONS_CONFIGURED:
case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
client->state = HIDS_CLIENT_W2_SEND_GET_REPORT;
break;/* ... */
#endif
case HIDS_CLIENT_W4_CHARACTERISTIC_CONFIGURATION_RESULT:
case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:
client->state = HIDS_CLIENT_STATE_CONNECTED;
break;
case HIDS_CLIENT_W4_VALUE_OF_CHARACTERISTIC_RESULT:
case HIDS_CLIENT_W4_WRITE_REPORT_DONE:
{
client->state = HIDS_CLIENT_STATE_CONNECTED;
uint8_t event[9];
hids_client_setup_report_event(GATTSERVICE_SUBEVENT_HID_REPORT_WRITTEN, client,
client->report_index, event, 0);
(*client->client_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
...}
break;
case HIDS_CLIENT_W4_WRITE_REPORT_DONE:
default:
break;default
}switch (client->state) { ... }
break;
case GATT_EVENT_QUERY_COMPLETE:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
if (client != NULL){
hids_run_for_client(client);
}if (client != NULL) { ... }
}{ ... }
uint8_t hids_client_connect(hci_con_handle_t con_handle, btstack_packet_handler_t packet_handler, hid_protocol_mode_t protocol_mode, uint16_t * hids_cid){
btstack_assert(packet_handler != NULL);
hids_client_t * client = hids_get_client_for_con_handle(con_handle);
if (client != NULL){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client != NULL) { ... }
uint16_t cid = hids_get_next_cid();
if (hids_cid != NULL) {
*hids_cid = cid;
}if (hids_cid != NULL) { ... }
client = hids_create_client(con_handle, cid);
if (client == NULL) {
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (client == NULL) { ... }
client->required_protocol_mode = protocol_mode;
client->client_handler = packet_handler;
client->state = HIDS_CLIENT_STATE_W2_QUERY_SERVICE;
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_disconnect(uint16_t hids_cid){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
hids_finalize_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_send_write_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type, const uint8_t * report, uint8_t report_len){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
uint8_t report_index = find_report_index_for_report_id_and_report_type(client, report_id, report_type);
if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
uint16_t mtu;
uint8_t status = gatt_client_get_mtu(client->con_handle, &mtu);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (mtu - 2 < report_len){
return ERROR_CODE_PARAMETER_OUT_OF_MANDATORY_RANGE;
}if (mtu - 2 < report_len) { ... }
client->state = HIDS_CLIENT_W2_SEND_WRITE_REPORT;
client->report_index = report_index;
client->report = report;
client->report_len = report_len;
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_send_get_report(uint16_t hids_cid, uint8_t report_id, hid_report_type_t report_type){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
uint8_t report_index = find_report_index_for_report_id_and_report_type(client, report_id, report_type);
if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (report_index == HIDS_CLIENT_INVALID_REPORT_INDEX) { ... }
client->report_index = report_index;
#ifdef ENABLE_TESTING_SUPPORT
client->state = HIDS_CLIENT_W2_READ_CHARACTERISTIC_CONFIGURATION;
#else
client->state = HIDS_CLIENT_W2_SEND_GET_REPORT;
#endif
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_get_hid_information(uint16_t hids_cid, uint8_t service_index){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
if (service_index >= client->num_instances){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (service_index >= client->num_instances) { ... }
client->service_index = service_index;
client->handle = client->services[client->service_index].hid_information_value_handle;
client->state = HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC;
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_get_protocol_mode(uint16_t hids_cid, uint8_t service_index){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
if (service_index >= client->num_instances){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (service_index >= client->num_instances) { ... }
client->service_index = service_index;
client->handle = client->services[client->service_index].protocol_mode_value_handle;
client->state = HIDS_CLIENT_W2_READ_VALUE_OF_CHARACTERISTIC;
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_send_set_protocol_mode(uint16_t hids_cid, uint8_t service_index, hid_protocol_mode_t protocol_mode){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
if (service_index >= client->num_instances){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (service_index >= client->num_instances) { ... }
client->service_index = service_index;
client->handle = client->services[client->service_index].protocol_mode_value_handle;
client->value = (uint8_t)protocol_mode;
client->state = HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE;
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
static uint8_t hids_client_send_control_point_cmd(uint16_t hids_cid, uint8_t service_index, uint8_t value){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
if (service_index >= client->num_instances){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (service_index >= client->num_instances) { ... }
client->service_index = service_index;
client->handle = client->services[client->service_index].control_point_value_handle;
client->value = value;
client->state = HIDS_CLIENT_W2_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE;
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_send_suspend(uint16_t hids_cid, uint8_t service_index){
return hids_client_send_control_point_cmd(hids_cid, service_index, 0);
}{ ... }
uint8_t hids_client_send_exit_suspend(uint16_t hids_cid, uint8_t service_index){
return hids_client_send_control_point_cmd(hids_cid, service_index, 1);
}{ ... }
uint8_t hids_client_enable_notifications(uint16_t hids_cid){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
client->value = GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION;
if (hids_client_notifications_configuration_init(client)){
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}if (hids_client_notifications_configuration_init(client)) { ... }
hids_emit_notifications_configuration(client);
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t hids_client_disable_notifications(uint16_t hids_cid){
hids_client_t * client = hids_get_client_for_cid(hids_cid);
if (client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (client == NULL) { ... }
if (client->state != HIDS_CLIENT_STATE_CONNECTED) {
return ERROR_CODE_COMMAND_DISALLOWED;
}if (client->state != HIDS_CLIENT_STATE_CONNECTED) { ... }
client->value = GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NONE;
if (hids_client_notifications_configuration_init(client)){
hids_run_for_client(client);
return ERROR_CODE_SUCCESS;
}if (hids_client_notifications_configuration_init(client)) { ... }
hids_emit_notifications_configuration(client);
return ERROR_CODE_SUCCESS;
}{ ... }
void hids_client_init(uint8_t * hid_descriptor_storage, uint16_t hid_descriptor_storage_len){
hids_client_descriptor_storage = hid_descriptor_storage;
hids_client_descriptor_storage_len = hid_descriptor_storage_len;
}{ ... }
void hids_client_deinit(void){}