1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
49
50
51
52
55
56
57
58
59
60
61
62
63
64
65
66
67
74
75
83
100
101
102
103
104
105
106
107
108
109
110
111
112
113
123
124
125
126
127
128
129
130
131
132
133
134
135
136
146
147
148
149
150
151
152
153
154
155
166
167
168
169
170
171
172
173
174
175
176
187
188
189
190
191
192
193
194
195
196
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
234
235
236
237
238
239
240
241
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
263
264
268
269
272
273
274
275
276
277
278
279
280
281
282
283
284
285
290
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
327
328
329
333
334
336
337
338
339
340
341
342
347
348
349
372
373
374
375
376
377
378
379
380
381
384
387
390
393
396
399
402
405
408
411
414
417
420
423
426
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
466
467
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
493
494
495
500
501
502
503
504
505
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
535
536
541
542
543
553
554
562
563
572
573
576
577
578
579
580
581
582
583
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
626
627
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
687
688
689
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
724
725
726
727
728
729
730
731
732
733
738
739
740
749
750
751
752
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
790
791
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
834
835
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
880
881
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
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
962
963
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
1012
1013
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1043
1044
1045
1050
1051
1052
1053
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1083
1084
1085
1090
1091
1092
1093
1098
1099
1100
1118
1119
1120
1121
1122
1123
1126
1129
1132
1135
1138
1141
1144
1147
1150
1153
1156
1159
1162
1165
1166
1167
1168
1169
1184
1185
1186
1187
1188
1191
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1210
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1244
1245
1246
1251
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1344
1345
1349
1354
1355
1356
1357
1358
1362
1363
1364
1365
1366
1367
1368
1369
1370
1373
1377
1378
1379
1380
1381
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1406
1407
1413
1414
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1487
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1525
1526
1527
1528
1529
1530
1532
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1572
1573
1574
/* ... */
/* ... */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btc/btc_util.h"
#include "btc/btc_manage.h"
#include "device/bdaddr.h"
#include "btc/btc_storage.h"
#include "osi/allocator.h"
#include "bta/utl.h"
#include "bta/bta_hh_api.h"
#include "stack/l2c_api.h"11 includes
#if HID_HOST_INCLUDED == TRUE
#include "btc_hh.h"
/* ... */
btc_hh_cb_t btc_hh_cb;
static bdstr_t bdstr;
/* ... */
#define COD_MASK 0x07FF
#define COD_UNCLASSIFIED ((0x1F) << 8)
#define COD_HID_KEYBOARD 0x0540
#define COD_HID_POINTING 0x0580
#define COD_HID_COMBO 0x05C0
#define COD_HID_MAJOR 0x0500
#define COD_HID_MASK 0x0700
#define is_hidh_init() (btc_hh_cb.status > BTC_HH_DISABLED)
#define BTC_TIMEOUT_VUP_MS (3 * 1000)9 defines
static inline void btc_hh_cb_to_app(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param)
{
esp_hh_cb_t btc_hh_cb = (esp_hh_cb_t)btc_profile_cb_get(BTC_PID_HH);
if (btc_hh_cb) {
btc_hh_cb(event, param);
}{...}
}{...}
/* ... */
static inline tBTA_HH_PROTO_MODE proto_mode_change_to_lower_layer(esp_hidh_protocol_mode_t protocol_mode)
{
tBTA_HH_PROTO_MODE proto_mode = BTA_HH_PROTO_UNKNOWN;
switch (protocol_mode) {
case ESP_HIDH_REPORT_MODE:
proto_mode = BTA_HH_PROTO_RPT_MODE;
break;...
case ESP_HIDH_BOOT_MODE:
proto_mode = BTA_HH_PROTO_BOOT_MODE;
break;...
default:
break;...
}{...}
return proto_mode;
}{...}
/* ... */
static inline esp_hidh_protocol_mode_t proto_mode_change_to_upper_layer(tBTA_HH_PROTO_MODE proto_mode)
{
esp_hidh_protocol_mode_t protocol_mode = ESP_HIDH_UNSUPPORTED_MODE;
switch (proto_mode) {
case BTA_HH_PROTO_RPT_MODE:
protocol_mode = ESP_HIDH_REPORT_MODE;
break;...
case BTA_HH_PROTO_BOOT_MODE:
protocol_mode = ESP_HIDH_BOOT_MODE;
break;...
default:
break;...
}{...}
return protocol_mode;
}{...}
/* ... */
btc_hh_device_t *btc_hh_find_connected_dev_by_handle(uint8_t handle)
{
uint32_t i;
for (i = 0; i < BTC_HH_MAX_HID; i++) {
if (btc_hh_cb.devices[i].dev_status == ESP_HIDH_CONN_STATE_CONNECTED && btc_hh_cb.devices[i].dev_handle == handle) {
return &btc_hh_cb.devices[i];
}{...}
}{...}
return NULL;
}{...}
/* ... */
static btc_hh_device_t *btc_hh_find_dev_by_bda(BD_ADDR bd_addr)
{
uint8_t i;
for (i = 0; i < BTC_HH_MAX_HID; i++) {
if (btc_hh_cb.devices[i].dev_status != ESP_HIDH_CONN_STATE_UNKNOWN &&
memcmp(btc_hh_cb.devices[i].bd_addr, bd_addr, BD_ADDR_LEN) == 0) {
return &btc_hh_cb.devices[i];
}{...}
}{...}
return NULL;
}{...}
/* ... */
static btc_hh_device_t *btc_hh_find_connected_dev_by_bda(BD_ADDR bd_addr)
{
uint32_t i;
for (i = 0; i < BTC_HH_MAX_HID; i++) {
if (btc_hh_cb.devices[i].dev_status == ESP_HIDH_CONN_STATE_CONNECTED &&
memcmp(btc_hh_cb.devices[i].bd_addr, bd_addr, BD_ADDR_LEN) == 0) {
return &btc_hh_cb.devices[i];
}{...}
}{...}
return NULL;
}{...}
/* ... */
void btc_hh_stop_vup_timer(BD_ADDR bd_addr)
{
BTIF_TRACE_API("%s", __func__);
btc_hh_device_t *p_dev = btc_hh_find_connected_dev_by_bda(bd_addr);
if (p_dev != NULL) {
BTC_TRACE_DEBUG("stop VUP timer");
if (p_dev->vup_timer) {
osi_alarm_free(p_dev->vup_timer);
p_dev->vup_timer = NULL;
}{...}
}{...}
}{...}
/* ... */
void btc_hh_timer_timeout(void *data)
{
btc_hh_device_t *p_dev = (btc_hh_device_t *)data;
bt_status_t status;
tBTA_HH p_data;
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_HH;
msg.act = BTA_HH_VC_UNPLUG_EVT;
BTC_TRACE_API("%s", __func__);
if (p_dev->dev_status != ESP_HIDH_CONN_STATE_CONNECTED){
BTC_TRACE_ERROR("%s Device[%s] is not connected!", __func__,
bdaddr_to_string((const bt_bdaddr_t *)p_dev->bd_addr, bdstr, sizeof(bdstr)));
return;
}{...}
memset(&p_data, 0, sizeof(tBTA_HH));
p_data.dev_status.status = BTA_HH_ERR;
p_data.dev_status.handle = p_dev->dev_handle;
status = btc_transfer_context(&msg, &p_data, sizeof(tBTA_HH), NULL, NULL);
if (status != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("%s context transfer failed", __func__);
}{...}
}{...}
/* ... */
void btc_hh_start_vup_timer(BD_ADDR bd_addr)
{
BTC_TRACE_API("%s", __func__);
btc_hh_device_t *p_dev = btc_hh_find_connected_dev_by_bda(bd_addr);
if (p_dev == NULL) {
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
return;
}{...}
if (p_dev->vup_timer) {
osi_alarm_free(p_dev->vup_timer);
p_dev->vup_timer = NULL;
}{...}
if ((p_dev->vup_timer = osi_alarm_new("btc_hh.vup_timer", btc_hh_timer_timeout, p_dev, BTC_TIMEOUT_VUP_MS)) ==
NULL) {
BTC_TRACE_ERROR("%s unable to malloc vup_timer!", __func__);
}{...}
}{...}
/* ... */
bool btc_hh_add_added_dev(BD_ADDR bd_addr, tBTA_HH_ATTR_MASK attr_mask)
{
int i;
for (i = 0; i < BTC_HH_MAX_ADDED_DEV; i++) {
if (memcmp(btc_hh_cb.added_devices[i].bd_addr, bd_addr, BD_ADDR_LEN) == 0) {
return false;
}{...}
}{...}
for (i = 0; i < BTC_HH_MAX_ADDED_DEV; i++) {
if (memcmp(btc_hh_cb.added_devices[i].bd_addr, bd_addr_null, BD_ADDR_LEN) == 0) {
memcpy(btc_hh_cb.added_devices[i].bd_addr, bd_addr, BD_ADDR_LEN);
btc_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE;
btc_hh_cb.added_devices[i].attr_mask = attr_mask;
return true;
}{...}
}{...}
BTC_TRACE_ERROR("%s: Error, out of space to add device", __func__);
return false;
}{...}
/* ... */
void btc_hh_remove_device(BD_ADDR bd_addr)
{
int i;
btc_hh_device_t *p_dev;
btc_hh_added_device_t *p_added_dev;
for (i = 0; i < BTC_HH_MAX_ADDED_DEV; i++) {
p_added_dev = &btc_hh_cb.added_devices[i];
if (memcmp(p_added_dev->bd_addr, bd_addr, BD_ADDR_LEN) == 0) {
BTA_HhRemoveDev(p_added_dev->dev_handle);
btc_storage_remove_hid_info((bt_bdaddr_t *)p_added_dev->bd_addr);
memset(p_added_dev->bd_addr, 0, 6);
p_added_dev->dev_handle = BTA_HH_INVALID_HANDLE;
break;
}{...}
}{...}
p_dev = btc_hh_find_dev_by_bda(bd_addr);
if (p_dev == NULL) {
BTC_TRACE_ERROR("%s Oops, can't find device", __func__);
return;
}{...}
/* ... */
p_dev->dev_status = ESP_HIDH_CONN_STATE_UNKNOWN;
p_dev->dev_handle = BTA_HH_INVALID_HANDLE;
p_dev->ready_for_data = false;
if (btc_hh_cb.device_num > 0) {
btc_hh_cb.device_num--;
}{...} else {
BTC_TRACE_WARNING("%s: device_num = 0", __func__);
}{...}
}{...}
static void bte_hh_arg_deep_copy(btc_msg_t *msg, void *p_dst, void *p_src)
{
tBTA_HH *p_dst_data = (tBTA_HH *)p_dst;
tBTA_HH *p_src_data = (tBTA_HH *)p_src;
switch (msg->act)
{
case BTA_HH_GET_RPT_EVT: {
BT_HDR *src_hdr = p_src_data->hs_data.rsp_data.p_rpt_data;
if (src_hdr) {
p_dst_data->hs_data.rsp_data.p_rpt_data = osi_malloc(sizeof(BT_HDR) + src_hdr->offset + src_hdr->len);
if (p_dst_data->hs_data.rsp_data.p_rpt_data == NULL) {
BTC_TRACE_ERROR("%s malloc p_rpt_data failed!", __func__);
p_dst_data->hs_data.status = ESP_HIDH_ERR_NO_RES;
break;
}{...}
memcpy(p_dst_data->hs_data.rsp_data.p_rpt_data, src_hdr, sizeof(BT_HDR) + src_hdr->offset + src_hdr->len);
}{...}
break;
}{...}
... default:
break;...
}{...}
}{...}
static void bte_hh_evt(tBTA_HH_EVT event, tBTA_HH *p_data)
{
bt_status_t status;
int param_len = 0;
BTC_TRACE_API("%s event=%d", __func__, event);
switch (event) {
case BTA_HH_ENABLE_EVT:
param_len = sizeof(tBTA_HH_STATUS);
break;...
case BTA_HH_DISABLE_EVT:
param_len = sizeof(tBTA_HH_STATUS);
break;...
case BTA_HH_OPEN_EVT:
param_len = sizeof(tBTA_HH_CONN);
break;...
case BTA_HH_CLOSE_EVT:
param_len = sizeof(tBTA_HH_CBDATA);
break;...
case BTA_HH_GET_RPT_EVT:
param_len = sizeof(tBTA_HH_HSDATA);
break;...
case BTA_HH_SET_RPT_EVT:
param_len = sizeof(tBTA_HH_CBDATA);
break;...
case BTA_HH_GET_PROTO_EVT:
param_len = sizeof(tBTA_HH_HSDATA);
break;...
case BTA_HH_SET_PROTO_EVT:
param_len = sizeof(tBTA_HH_CBDATA);
break;...
case BTA_HH_GET_IDLE_EVT:
param_len = sizeof(tBTA_HH_HSDATA);
break;...
case BTA_HH_SET_IDLE_EVT:
param_len = sizeof(tBTA_HH_CBDATA);
break;...
case BTA_HH_GET_DSCP_EVT:
param_len = sizeof(tBTA_HH_DEV_DSCP_INFO);
break;...
case BTA_HH_ADD_DEV_EVT:
param_len = sizeof(tBTA_HH_DEV_INFO);
break;...
case BTA_HH_RMV_DEV_EVT:
param_len = sizeof(tBTA_HH_DEV_INFO);
break;...
case BTA_HH_VC_UNPLUG_EVT:
param_len = sizeof(tBTA_HH_CBDATA);
break;...
case BTA_HH_DATA_EVT:
param_len = sizeof(tBTA_HH_API_SENDDATA);
break;...
case BTA_HH_API_ERR_EVT:
param_len = 0;
break;...
}{...}
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_HH;
msg.act = event;
status = btc_transfer_context(&msg, p_data, param_len, bte_hh_arg_deep_copy, btc_hh_cb_arg_deep_free);
assert(status == BT_STATUS_SUCCESS);
}{...}
/* ... */
static void btc_hh_init(void)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
do {
if (is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has been initiated, shall uninit first!", __func__);
ret = ESP_HIDH_NEED_DEINIT;
break;
}{...}
memset(&btc_hh_cb, 0, sizeof(btc_hh_cb));
for (uint8_t i = 0; i < BTC_HH_MAX_HID; i++) {
btc_hh_cb.devices[i].dev_status = ESP_HIDH_CONN_STATE_UNKNOWN;
}{...}
BTA_HhEnable(BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT, bte_hh_evt);
}{...} while (0);
if (ret != ESP_HIDH_OK) {
esp_hidh_cb_param_t param;
param.init.status = ret;
btc_hh_cb_to_app(ESP_HIDH_INIT_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_deinit(void)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
for (uint8_t i = 0; i < BTC_HH_MAX_HID; i++) {
if(btc_hh_cb.devices[i].dev_status == ESP_HIDH_CONN_STATE_CONNECTED){
BTA_HhClose(btc_hh_cb.devices[i].dev_handle);
}{...}
}{...}
btc_hh_cb.service_dereg_active = TRUE;
btc_hh_cb.status = BTC_HH_DISABLING;
BTA_HhDisable();
}{...} while (0);
if (ret != ESP_HIDH_OK) {
esp_hidh_cb_param_t param;
param.deinit.status = ret;
btc_hh_cb_to_app(ESP_HIDH_DEINIT_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_connect(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
btc_hh_added_device_t* added_dev = NULL;
btc_hh_device_t* dev = NULL;
esp_hidh_cb_param_t param;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
if (btc_hh_cb.status == BTC_HH_DEV_CONNECTING) {
BTC_TRACE_ERROR("%s HH is connecting, ignore!", __func__);
ret = ESP_HIDH_BUSY;
break;
}{...}
dev = btc_hh_find_dev_by_bda(arg->connect.bd_addr);
if (!dev && btc_hh_cb.device_num >= BTC_HH_MAX_HID) {
BTC_TRACE_ERROR("%s exceeded the maximum supported HID device number %d!", __func__, BTC_HH_MAX_HID);
ret = ESP_HIDH_ERR_NO_RES;
break;
}{...} else if (dev && dev->dev_status == ESP_HIDH_CONN_STATE_CONNECTED) {
BTC_TRACE_WARNING("%s Device[%s] already connected", __func__,
bdaddr_to_string((const bt_bdaddr_t *)arg->connect.bd_addr, bdstr, sizeof(bdstr)));
param.open.conn_status = ESP_HIDH_CONN_STATE_CONNECTED;
break;
}{...}
for (int i = 0; i < BTC_HH_MAX_ADDED_DEV; i++) {
if (memcmp(btc_hh_cb.added_devices[i].bd_addr, arg->connect.bd_addr, BD_ADDR_LEN) == 0) {
added_dev = &btc_hh_cb.added_devices[i];
BTC_TRACE_WARNING("%s Device[%s] already added, attr_mask = 0x%x", __func__,
bdaddr_to_string((const bt_bdaddr_t *)arg->connect.bd_addr, bdstr, sizeof(bdstr)),
added_dev->attr_mask);
}{...}
}{...}
if (added_dev != NULL) {
if (added_dev->dev_handle == BTA_HH_INVALID_HANDLE) {
BTC_TRACE_ERROR("device added but addition failed");
memset(added_dev->bd_addr, 0, sizeof(added_dev->bd_addr));
ret = ESP_HIDH_ERR;
break;
}{...}
}{...}
/* ... */
btc_hh_cb.status = BTC_HH_DEV_CONNECTING;
memcpy(btc_hh_cb.pending_conn_address, arg->connect.bd_addr, BD_ADDR_LEN);
BTA_HhOpen(arg->connect.bd_addr, BTA_HH_PROTO_RPT_MODE, (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT));
param.open.conn_status = ESP_HIDH_CONN_STATE_CONNECTING;
ret = ESP_HIDH_OK;
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.open.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
}{...}
param.open.status = ret;
param.open.handle = BTA_HH_INVALID_HANDLE;
memcpy(param.open.bd_addr, arg->connect.bd_addr, BD_ADDR_LEN);
param.open.is_orig = true;
btc_hh_cb_to_app(ESP_HIDH_OPEN_EVT, ¶m);
}{...}
/* ... */
static void btc_hh_disconnect(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
btc_hh_device_t *p_dev;
esp_hidh_cb_param_t param;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->disconnect.bd_addr);
if (p_dev != NULL) {
BTA_HhClose(p_dev->dev_handle);
param.close.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTING;
param.close.handle = p_dev->dev_handle;
}{...} else {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
}{...}
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.close.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
param.close.handle = BTA_HH_INVALID_HANDLE;
}{...}
param.close.status = ret;
btc_hh_cb_to_app(ESP_HIDH_CLOSE_EVT, ¶m);
}{...}
/* ... */
static void btc_hh_virtual_unplug(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
btc_hh_device_t *p_dev;
esp_hidh_cb_param_t param;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_dev_by_bda(arg->unplug.bd_addr);
if ((p_dev != NULL) && (p_dev->dev_status == ESP_HIDH_CONN_STATE_CONNECTED) &&
(p_dev->attr_mask & HID_VIRTUAL_CABLE)) {
BTC_TRACE_DEBUG("%s: Sending BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG", __func__);
btc_hh_start_vup_timer(arg->unplug.bd_addr);
p_dev->local_vup = true;
BTA_HhSendCtrl(p_dev->dev_handle, BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG);
param.unplug.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTING;
param.unplug.handle = p_dev->dev_handle;
}{...} else if ((p_dev != NULL) && (p_dev->dev_status == ESP_HIDH_CONN_STATE_CONNECTED)) {
BTC_TRACE_WARNING("%s: Virtual unplug not supported, disconnecting device", __func__);
btc_hh_start_vup_timer(arg->unplug.bd_addr);
p_dev->local_vup = true;
BTA_HhClose(p_dev->dev_handle);
param.unplug.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTING;
param.unplug.handle = p_dev->dev_handle;
}{...} else {
BTC_TRACE_ERROR("%s: Error, device not opened, status = %d", __func__, btc_hh_cb.status);
ret = ESP_HIDH_NO_CONNECTION;
if (memcmp(btc_hh_cb.pending_conn_address, arg->unplug.bd_addr, BD_ADDR_LEN) == 0 &&
(btc_hh_cb.status == BTC_HH_DEV_CONNECTING)) {
btc_hh_cb.status = (BTC_HH_STATUS)BTC_HH_DEV_DISCONNECTED;
memset(btc_hh_cb.pending_conn_address, 0, BD_ADDR_LEN);
}{...}
}{...}
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.unplug.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
param.unplug.handle = BTA_HH_INVALID_HANDLE;
}{...}
param.unplug.status = ret;
btc_hh_cb_to_app(ESP_HIDH_VC_UNPLUG_EVT, ¶m);
}{...}
/* ... */
static void btc_hh_set_info(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
tBTA_HH_DEV_DSCP_INFO dscp_info;
BTC_TRACE_DEBUG("%s: sub_class = 0x%02x, app_id = %d, vendor_id = 0x%04x, "
"product_id = 0x%04x, version= 0x%04x",
__func__, arg->set_info.hid_info->sub_class, arg->set_info.hid_info->app_id,
arg->set_info.hid_info->vendor_id, arg->set_info.hid_info->product_id,
arg->set_info.hid_info->version);
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
memset(&dscp_info, 0, sizeof(dscp_info));
dscp_info.vendor_id = arg->set_info.hid_info->vendor_id;
dscp_info.product_id = arg->set_info.hid_info->product_id;
dscp_info.version = arg->set_info.hid_info->version;
dscp_info.ctry_code = arg->set_info.hid_info->ctry_code;
dscp_info.descriptor.dl_len = arg->set_info.hid_info->dl_len;
dscp_info.descriptor.dsc_list = (uint8_t *)osi_malloc(dscp_info.descriptor.dl_len);
if (dscp_info.descriptor.dsc_list == NULL) {
BTC_TRACE_ERROR("%s malloc dsc_list failed!", __func__);
ret = ESP_HIDH_ERR_NO_RES;
break;
}{...}
memcpy(dscp_info.descriptor.dsc_list, arg->set_info.hid_info->dsc_list, dscp_info.descriptor.dl_len);
if (btc_hh_add_added_dev(arg->set_info.bd_addr, arg->set_info.hid_info->attr_mask)) {
btc_hh_cb.add_event = BTC_HH_SET_INFO_EVT;
BTA_HhAddDev(arg->set_info.bd_addr, arg->set_info.hid_info->attr_mask, arg->set_info.hid_info->sub_class,
arg->set_info.hid_info->app_id, dscp_info);
}{...} else {
BTC_TRACE_ERROR("%s malloc dsc_list failed!", __func__);
ret = ESP_HIDH_ERR;
break;
}{...}
}{...} while(0);
utl_freebuf((void **)&dscp_info.descriptor.dsc_list);
if (ret != ESP_HIDH_OK) {
param.set_info.status = ret;
param.set_info.handle = BTA_HH_INVALID_HANDLE;
memcpy(param.set_info.bd_addr, arg->set_info.bd_addr, BD_ADDR_LEN);
btc_hh_cb_to_app(ESP_HIDH_SET_INFO_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_get_protocol(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->get_protocol.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...}
BTA_HhGetProtoMode(p_dev->dev_handle);
}{...} while(0);
if (ret != ESP_HIDH_OK) {
param.get_proto.proto_mode = ESP_HIDH_UNSUPPORTED_MODE;
param.get_proto.handle = BTA_HH_INVALID_HANDLE;
param.get_proto.status = ret;
btc_hh_cb_to_app(ESP_HIDH_GET_PROTO_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_set_protocol(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->set_protocol.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...} else if (arg->set_protocol.protocol_mode != ESP_HIDH_REPORT_MODE && arg->set_protocol.protocol_mode != ESP_HIDH_BOOT_MODE) {
BTC_TRACE_ERROR("%s: Error, device proto_mode = %d.", __func__, arg->set_protocol.protocol_mode);
ret = ESP_HIDH_HS_INVALID_PARAM;
break;
}{...} else {
BTA_HhSetProtoMode(p_dev->dev_handle, proto_mode_change_to_lower_layer(arg->set_protocol.protocol_mode));
}{...}
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.set_proto.handle = BTA_HH_INVALID_HANDLE;
param.set_proto.status = ret;
btc_hh_cb_to_app(ESP_HIDH_SET_PROTO_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_get_report(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->get_report.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...} else if (((int)arg->get_report.report_type) <= BTA_HH_RPTT_RESRV ||
((int)arg->get_report.report_type) > BTA_HH_RPTT_FEATURE) {
BTC_TRACE_ERROR("%s Error: report type=%d not supported!", __func__, arg->get_report.report_type);
ret = ESP_HIDH_HS_INVALID_PARAM;
break;
}{...} else {
BTA_HhGetReport(p_dev->dev_handle, arg->get_report.report_type, arg->get_report.report_id,
arg->get_report.buffer_size);
}{...}
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.get_rpt.handle = BTA_HH_INVALID_HANDLE;
param.get_rpt.status = ret;
param.get_rpt.len = 0;
param.get_rpt.data = NULL;
btc_hh_cb_to_app(ESP_HIDH_GET_RPT_EVT, ¶m);
}{...}
}{...}
/* ... */
static BT_HDR *create_pbuf(uint16_t len, uint8_t *data)
{
uint8_t *pbuf_data;
BT_HDR *p_buf = (BT_HDR *)osi_malloc(len + BTA_HH_MIN_OFFSET + sizeof(BT_HDR));
if (p_buf == NULL) {
BTC_TRACE_ERROR("%s failed!", __func__);
return NULL;
}{...}
p_buf->len = len;
p_buf->offset = BTA_HH_MIN_OFFSET;
pbuf_data = (uint8_t *)(p_buf + 1) + p_buf->offset;
memcpy(pbuf_data, data, len);
return p_buf;
}{...}
/* ... */
static void btc_hh_set_report(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->set_report.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...} else if (((int)arg->set_report.report_type) <= BTA_HH_RPTT_RESRV ||
((int)arg->set_report.report_type) > BTA_HH_RPTT_FEATURE) {
BTC_TRACE_ERROR("%s Error: report type=%d not supported!", __func__, arg->set_report.report_type);
ret = ESP_HIDH_HS_INVALID_PARAM;
break;
}{...} else if (arg->set_report.report == NULL || arg->set_report.len == 0) {
BTC_TRACE_ERROR("%s Error: report is empty!", __func__);
ret = ESP_HIDH_HS_INVALID_PARAM;
break;
}{...} else {
BT_HDR* p_buf = create_pbuf(arg->set_report.len, arg->set_report.report);
if (p_buf == NULL) {
ret = ESP_HIDH_ERR_NO_RES;
break;
}{...}
BTA_HhSetReport(p_dev->dev_handle, arg->set_report.report_type, p_buf);
}{...}
}{...} while(0);
if (ret != ESP_HIDH_OK) {
param.set_rpt.handle = BTA_HH_INVALID_HANDLE;
param.set_rpt.status = ret;
btc_hh_cb_to_app(ESP_HIDH_SET_RPT_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_send_data(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->send_data.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...} else if (arg->send_data.data == NULL || arg->send_data.len == 0) {
BTC_TRACE_ERROR("%s Error: send data is empty!", __func__);
ret = ESP_HIDH_HS_INVALID_PARAM;
break;
}{...} else {
BT_HDR *p_buf = create_pbuf(arg->send_data.len, arg->send_data.data);
if (p_buf == NULL) {
ret = ESP_HIDH_ERR_NO_RES;
break;
}{...}
p_buf->layer_specific = BTA_HH_RPTT_OUTPUT;
BTA_HhSendData(p_dev->dev_handle, arg->send_data.bd_addr, p_buf);
}{...}
}{...} while(0);
if (ret != ESP_HIDH_OK) {
param.send_data.handle = BTA_HH_INVALID_HANDLE;
param.send_data.status = ret;
param.send_data.reason = 0;
btc_hh_cb_to_app(ESP_HIDH_DATA_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_get_idle_time(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->get_idle.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...}
BTA_HhGetIdle(p_dev->dev_handle);
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.get_idle.handle = BTA_HH_INVALID_HANDLE;
param.get_idle.status = ret;
param.get_idle.idle_rate = 0;
btc_hh_cb_to_app(ESP_HIDH_GET_IDLE_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hh_set_idle_time(btc_hidh_args_t *arg)
{
BTC_TRACE_API("%s", __func__);
esp_hidh_status_t ret = ESP_HIDH_OK;
esp_hidh_cb_param_t param;
btc_hh_device_t *p_dev;
do {
if (!is_hidh_init()) {
BTC_TRACE_ERROR("%s HH has not been initiated, shall init first!", __func__);
ret = ESP_HIDH_NEED_INIT;
break;
}{...}
p_dev = btc_hh_find_connected_dev_by_bda(arg->set_idle.bd_addr);
if (p_dev == NULL) {
ret = ESP_HIDH_NO_CONNECTION;
BTC_TRACE_ERROR("%s Error: device not connected!", __func__);
break;
}{...}
BTA_HhSetIdle(p_dev->dev_handle, arg->set_idle.idle_time);
}{...} while (0);
if (ret != ESP_HIDH_OK) {
param.set_idle.handle = BTA_HH_INVALID_HANDLE;
param.set_idle.status = ret;
btc_hh_cb_to_app(ESP_HIDH_SET_IDLE_EVT, ¶m);
}{...}
}{...}
static void btc_hh_call_arg_deep_free(btc_msg_t *msg)
{
btc_hidh_args_t *arg = (btc_hidh_args_t *)msg->arg;
switch (msg->act) {
case BTC_HH_SET_INFO_EVT:
utl_freebuf((void **)&arg->set_info.hid_info);
break;...
case BTC_HH_SET_REPORT_EVT:
utl_freebuf((void **)&arg->set_report.report);
break;...
case BTC_HH_SEND_DATA_EVT:
utl_freebuf((void **)&arg->send_data.data);
break;...
default:
break;...
}{...}
}{...}
void btc_hh_call_handler(btc_msg_t *msg)
{
btc_hidh_args_t *arg = (btc_hidh_args_t *)(msg->arg);
switch (msg->act) {
case BTC_HH_INIT_EVT:
btc_hh_init();
break;...
case BTC_HH_CONNECT_EVT:
btc_hh_connect(arg);
break;...
case BTC_HH_DISCONNECT_EVT:
btc_hh_disconnect(arg);
break;...
case BTC_HH_UNPLUG_EVT:
btc_hh_virtual_unplug(arg);
break;...
case BTC_HH_SET_INFO_EVT:
btc_hh_set_info(arg);
break;...
case BTC_HH_GET_PROTO_EVT:
btc_hh_get_protocol(arg);
break;...
case BTC_HH_SET_PROTO_EVT:
btc_hh_set_protocol(arg);
break;...
case BTC_HH_GET_IDLE_EVT:
btc_hh_get_idle_time(arg);
break;...
case BTC_HH_SET_IDLE_EVT:
btc_hh_set_idle_time(arg);
break;...
case BTC_HH_GET_REPORT_EVT:
btc_hh_get_report(arg);
break;...
case BTC_HH_SET_REPORT_EVT:
btc_hh_set_report(arg);
break;...
case BTC_HH_SEND_DATA_EVT:
btc_hh_send_data(arg);
break;...
case BTC_HH_DEINIT_EVT:
btc_hh_deinit();
break;...
default:
BTC_TRACE_WARNING("unknown hidh action %d", msg->act);
break;...
}{...}
btc_hh_call_arg_deep_free(msg);
}{...}
void btc_hh_cb_arg_deep_free(btc_msg_t *msg)
{
tBTA_HH *arg = (tBTA_HH *)msg->arg;
switch (msg->act) {
case BTA_HH_GET_RPT_EVT:
utl_freebuf((void **)&arg->hs_data.rsp_data.p_rpt_data);
break;...
case BTA_HH_DATA_IND_EVT:
utl_freebuf((void **)&arg->int_data.p_data);
break;...
default:
break;...
}{...}
}{...}
bool btc_hh_copy_hid_info(tBTA_HH_DEV_DSCP_INFO *dest, tBTA_HH_DEV_DSCP_INFO *src)
{
dest->descriptor.dl_len = 0;
if (src->descriptor.dl_len > 0) {
dest->descriptor.dsc_list = (uint8_t *)osi_malloc(src->descriptor.dl_len);
}{...}
if (dest->descriptor.dsc_list) {
memcpy(dest->descriptor.dsc_list, src->descriptor.dsc_list, src->descriptor.dl_len);
dest->descriptor.dl_len = src->descriptor.dl_len;
}{...}
dest->vendor_id = src->vendor_id;
dest->product_id = src->product_id;
dest->version = src->version;
dest->ctry_code = src->ctry_code;
dest->ssr_max_latency = src->ssr_max_latency;
dest->ssr_min_tout = src->ssr_min_tout;
return true;
}{...}
bool btc_hh_cb_copy_hid_info(esp_hidh_cb_param_t *param, tBTA_HH_DEV_DSCP_INFO *src)
{
param->dscp.dl_len = 0;
if (src->descriptor.dl_len > 0) {
param->dscp.dsc_list = (uint8_t *)osi_malloc(src->descriptor.dl_len);
}{...}
if (param->dscp.dsc_list) {
memcpy(param->dscp.dsc_list, src->descriptor.dsc_list, src->descriptor.dl_len);
param->dscp.dl_len = src->descriptor.dl_len;
}{...}
param->dscp.vendor_id = src->vendor_id;
param->dscp.product_id = src->product_id;
param->dscp.version = src->version;
param->dscp.ctry_code = src->ctry_code;
param->dscp.ssr_max_latency = src->ssr_max_latency;
param->dscp.ssr_min_tout = src->ssr_min_tout;
return true;
}{...}
void btc_hh_cb_handler(btc_msg_t *msg)
{
esp_hidh_cb_param_t param = {0};
tBTA_HH *p_data = (tBTA_HH *)msg->arg;
btc_hh_device_t *p_dev = NULL;
int len, i;
BTC_TRACE_DEBUG("%s: event=%s dereg = %d", __func__, dump_hh_event(msg->act), btc_hh_cb.service_dereg_active);
switch (msg->act) {
case BTA_HH_ENABLE_EVT:
if (p_data->status == BTA_HH_OK) {
btc_hh_cb.status = BTC_HH_ENABLED;
BTC_TRACE_DEBUG("Loading added devices");
}{...} else {
btc_hh_cb.status = BTC_HH_DISABLED;
BTC_TRACE_ERROR("Error, HH enabling failed, status = %d", p_data->status);
}{...}
param.init.status = p_data->status;
btc_hh_cb_to_app(ESP_HIDH_INIT_EVT, ¶m);
break;...
case BTA_HH_DISABLE_EVT:
btc_hh_cb.status = BTC_HH_DISABLED;
if (btc_hh_cb.service_dereg_active) {
BTIF_TRACE_DEBUG("BTA_HH_DISABLE_EVT: enabling HID Device service");
btc_hh_cb.service_dereg_active = FALSE;
}{...}
if (p_data->status == BTA_HH_OK) {
for (uint8_t i = 0; i < BTC_HH_MAX_HID; i++) {
if (btc_hh_cb.devices[i].vup_timer) {
osi_alarm_free(btc_hh_cb.devices[i].vup_timer);
}{...}
}{...}
memset(&btc_hh_cb, 0, sizeof(btc_hh_cb));
for (i = 0; i < BTC_HH_MAX_HID; i++) {
btc_hh_cb.devices[i].dev_status = ESP_HIDH_CONN_STATE_UNKNOWN;
}{...}
}{...} else {
BTC_TRACE_ERROR("Error, HH disabling failed, status = %d", p_data->status);
}{...}
param.deinit.status = p_data->status;
btc_hh_cb_to_app(ESP_HIDH_DEINIT_EVT, ¶m);
break;...
case BTA_HH_OPEN_EVT:
BTC_TRACE_DEBUG("handle=%d, status =%d", p_data->conn.handle, p_data->conn.status);
memset(btc_hh_cb.pending_conn_address, 0, BD_ADDR_LEN);
if (p_data->conn.status == BTA_HH_OK) {
p_dev = btc_hh_find_connected_dev_by_handle(p_data->conn.handle);
if (p_dev == NULL) {
BTC_TRACE_ERROR("Error, cannot find device with handle %d", p_data->conn.handle);
btc_hh_cb.status = (BTC_HH_STATUS)BTC_HH_DEV_DISCONNECTED;
BTA_HhClose(p_data->conn.handle);
param.open.status = ESP_HIDH_ERR;
param.open.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
}{...} else {
BTC_TRACE_DEBUG("Found device...Getting dscp info for handle "
"... %d",
p_data->conn.handle);
memcpy(p_dev->bd_addr, p_data->conn.bda, BD_ADDR_LEN);
btc_hh_cb.status = (BTC_HH_STATUS)BTC_HH_DEV_CONNECTED;
btc_hh_cb.p_curr_dev = btc_hh_find_connected_dev_by_handle(p_data->conn.handle);
BTA_HhGetDscpInfo(p_data->conn.handle);
p_dev->dev_status = ESP_HIDH_CONN_STATE_CONNECTED;
param.open.status = ESP_HIDH_OK;
param.open.conn_status = ESP_HIDH_CONN_STATE_CONNECTED;
}{...}
}{...} else {
p_dev = btc_hh_find_dev_by_bda(p_data->conn.bda);
if (p_dev != NULL) {
btc_hh_stop_vup_timer(p_dev->bd_addr);
p_dev->dev_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
}{...}
btc_hh_cb.status = (BTC_HH_STATUS)BTC_HH_DEV_DISCONNECTED;
param.open.status = p_data->conn.status;
param.open.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
}{...}
param.open.handle = p_data->conn.handle;
param.open.is_orig = p_data->conn.is_orig;
memcpy(param.open.bd_addr, p_data->conn.bda, BD_ADDR_LEN);
btc_hh_cb_to_app(ESP_HIDH_OPEN_EVT, ¶m);
break;...
case BTA_HH_GET_DSCP_EVT:
len = p_data->dscp_info.descriptor.dl_len;
BTC_TRACE_DEBUG("len = %d", len);
do {
param.dscp.status = ESP_HIDH_OK;
param.dscp.handle = BTA_HH_INVALID_HANDLE;
param.dscp.added = false;
p_dev = btc_hh_cb.p_curr_dev;
if (p_dev == NULL) {
BTC_TRACE_ERROR("No HID device is currently connected");
param.dscp.status = ESP_HIDH_NO_CONNECTION;
break;
}{...}
if (btc_hh_add_added_dev(p_dev->bd_addr, p_dev->attr_mask)) {
tBTA_HH_DEV_DSCP_INFO dscp_info;
bt_status_t ret;
btc_hh_copy_hid_info(&dscp_info, &p_data->dscp_info);
btc_hh_cb.add_event = BTC_HH_CONNECT_EVT;
BTA_HhAddDev(p_dev->bd_addr, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id, dscp_info);
ret = btc_storage_add_hid_device_info((bt_bdaddr_t *)p_dev->bd_addr, p_dev->attr_mask, p_dev->sub_class, p_dev->app_id,
p_data->dscp_info.vendor_id, p_data->dscp_info.product_id,
p_data->dscp_info.version, p_data->dscp_info.ctry_code,
p_data->dscp_info.ssr_max_latency, p_data->dscp_info.ssr_min_tout,
len, p_data->dscp_info.descriptor.dsc_list);
if (ret != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("write hid info to nvs failed!");
}{...}
if (dscp_info.descriptor.dl_len > 0 && dscp_info.descriptor.dsc_list != NULL) {
utl_freebuf((void **)&dscp_info.descriptor.dsc_list);
dscp_info.descriptor.dl_len = 0;
}{...}
}{...} else {
BTC_TRACE_WARNING("%s: Device already added ", __func__);
param.dscp.added = true;
}{...}
btc_hh_cb_copy_hid_info(¶m, &p_data->dscp_info);
param.dscp.handle = p_dev->dev_handle;
}{...} while(0);
btc_hh_cb_to_app(ESP_HIDH_GET_DSCP_EVT, ¶m);
if (param.dscp.dl_len > 0 && param.dscp.dsc_list != NULL) {
utl_freebuf((void **)¶m.dscp.dsc_list);
param.dscp.dl_len = 0;
}{...}
break;...
case BTA_HH_CLOSE_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_status.status,
p_data->dev_status.handle);
p_dev = btc_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
if (p_dev != NULL) {
BTC_TRACE_DEBUG("uhid local_vup=%d", p_dev->local_vup);
btc_hh_stop_vup_timer(p_dev->bd_addr);
/* ... */
if (p_dev->local_vup) {
p_dev->local_vup = false;
BTA_DmRemoveDevice(p_dev->bd_addr, BT_TRANSPORT_BR_EDR);
}{...}
btc_hh_cb.status = (BTC_HH_STATUS)BTC_HH_DEV_DISCONNECTED;
p_dev->dev_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
param.close.status = p_data->dev_status.status;
}{...} else {
BTC_TRACE_ERROR("Error: cannot find device with handle %d", p_data->dev_status.handle);
param.close.status = ESP_HIDH_NO_CONNECTION;
}{...}
param.close.handle = p_data->dev_status.handle;
param.close.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
btc_hh_cb_to_app(ESP_HIDH_CLOSE_EVT, ¶m);
break;...
case BTA_HH_VC_UNPLUG_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_status.status,
p_data->dev_status.handle);
p_dev = btc_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
btc_hh_cb.status = (BTC_HH_STATUS)BTC_HH_DEV_DISCONNECTED;
if (p_dev != NULL) {
btc_hh_stop_vup_timer(p_dev->bd_addr);
p_dev->dev_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
BTC_TRACE_DEBUG("%s---Sending connection state change", __func__);
param.close.status = ESP_HIDH_OK;
param.close.handle = p_data->dev_status.handle;
param.close.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
btc_hh_cb_to_app(ESP_HIDH_CLOSE_EVT, ¶m);
BTC_TRACE_DEBUG("%s---Removing HID bond", __func__);
/* ... */
if (p_dev->local_vup) {
p_dev->local_vup = false;
BTA_DmRemoveDevice(p_dev->bd_addr, BT_TRANSPORT_BR_EDR);
}{...} else {
btc_hh_remove_device(p_dev->bd_addr);
}{...}
param.unplug.status = p_data->dev_status.status;
}{...} else {
BTC_TRACE_ERROR("Error: cannot find device with handle %d", p_data->dev_status.handle);
param.unplug.status = ESP_HIDH_NO_CONNECTION;
}{...}
param.unplug.handle = p_data->dev_status.handle;
param.unplug.conn_status = ESP_HIDH_CONN_STATE_DISCONNECTED;
btc_hh_cb_to_app(ESP_HIDH_VC_UNPLUG_EVT, ¶m);
break;...
case BTA_HH_DATA_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->send_data.status,
p_data->send_data.handle);
param.send_data.handle = p_data->send_data.handle;
param.send_data.status = p_data->send_data.status;
param.send_data.reason = p_data->send_data.reason;
btc_hh_cb_to_app(ESP_HIDH_DATA_EVT, ¶m);
break;...
case BTA_HH_GET_PROTO_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d, proto = [%d], %s", p_data->hs_data.status,
p_data->hs_data.handle, p_data->hs_data.rsp_data.proto_mode,
(p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE)
? "Report Mode"
: (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_BOOT_MODE) ? "Boot Mode"
: "Unsupported");
param.get_proto.proto_mode = proto_mode_change_to_upper_layer(p_data->hs_data.rsp_data.proto_mode);
param.get_proto.handle = p_data->hs_data.handle;
param.get_proto.status = p_data->hs_data.status;
btc_hh_cb_to_app(ESP_HIDH_GET_PROTO_EVT, ¶m);
break;...
case BTA_HH_SET_PROTO_EVT:
BTIF_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_status.status,
p_data->dev_status.handle);
param.set_proto.handle = p_data->dev_status.handle;
param.set_proto.status = p_data->dev_status.status;
btc_hh_cb_to_app(ESP_HIDH_SET_PROTO_EVT, ¶m);
break;...
case BTA_HH_GET_RPT_EVT: {
BT_HDR *hdr = p_data->hs_data.rsp_data.p_rpt_data;
uint8_t *data = NULL;
uint16_t len = 0;
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->hs_data.status, p_data->hs_data.handle);
if (hdr) {
data = (uint8_t *)(hdr + 1) + hdr->offset;
len = hdr->len;
}{...}
param.get_rpt.handle = p_data->hs_data.handle;
param.get_rpt.status = p_data->hs_data.status;
param.get_rpt.len = len;
param.get_rpt.data = data;
btc_hh_cb_to_app(ESP_HIDH_GET_RPT_EVT, ¶m);
break;
}{...}
... case BTA_HH_SET_RPT_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_status.status,
p_data->dev_status.handle);
param.set_rpt.handle = p_data->dev_status.handle;
param.set_rpt.status = p_data->dev_status.status;
btc_hh_cb_to_app(ESP_HIDH_SET_RPT_EVT, ¶m);
break;...
case BTA_HH_GET_IDLE_EVT:
BTC_TRACE_DEBUG("handle = %d, status = %d, rate = %d", p_data->hs_data.handle, p_data->hs_data.status,
p_data->hs_data.rsp_data.idle_rate);
param.get_idle.handle = p_data->hs_data.handle;
param.get_idle.status = p_data->hs_data.status;
param.get_idle.idle_rate = p_data->hs_data.rsp_data.idle_rate;
btc_hh_cb_to_app(ESP_HIDH_GET_IDLE_EVT, ¶m);
break;...
case BTA_HH_SET_IDLE_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_status.status, p_data->dev_status.handle);
param.set_idle.handle = p_data->dev_status.handle;
param.set_idle.status = p_data->dev_status.status;
btc_hh_cb_to_app(BTA_HH_SET_IDLE_EVT, ¶m);
break;...
case BTA_HH_ADD_DEV_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_info.status, p_data->dev_info.handle);
for (uint8_t i = 0; i < BTC_HH_MAX_ADDED_DEV; i++) {
if (memcmp(btc_hh_cb.added_devices[i].bd_addr, p_data->dev_info.bda, BD_ADDR_LEN) == 0) {
if (p_data->dev_info.status == BTA_HH_OK) {
btc_hh_cb.added_devices[i].dev_handle = p_data->dev_info.handle;
}{...} else {
memset(btc_hh_cb.added_devices[i].bd_addr, 0, BD_ADDR_LEN);
btc_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE;
}{...}
break;
}{...}
}{...}
if (btc_hh_cb.add_event == BTC_HH_SET_INFO_EVT) {
param.set_info.handle = p_data->dev_info.handle;
param.set_info.status = p_data->dev_info.status;
memcpy(param.set_info.bd_addr, p_data->dev_info.bda, BD_ADDR_LEN);
btc_hh_cb_to_app(ESP_HIDH_SET_INFO_EVT, ¶m);
}{...} else {
param.add_dev.handle = p_data->dev_info.handle;
param.add_dev.status = p_data->dev_info.status;
memcpy(param.add_dev.bd_addr, p_data->dev_info.bda, BD_ADDR_LEN);
btc_hh_cb_to_app(ESP_HIDH_ADD_DEV_EVT, ¶m);
}{...}
break;...
case BTA_HH_RMV_DEV_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->dev_info.status, p_data->dev_info.handle);
param.rmv_dev.handle = p_data->dev_info.status;
param.rmv_dev.status = p_data->dev_info.handle;
memcpy(param.rmv_dev.bd_addr, p_data->dev_info.bda, BD_ADDR_LEN);
btc_hh_cb_to_app(ESP_HIDH_RMV_DEV_EVT, ¶m);
break;...
case BTA_HH_DATA_IND_EVT:
BTC_TRACE_DEBUG("status = %d, handle = %d", p_data->int_data.status, p_data->int_data.handle);
if (p_data->int_data.status == BTA_HH_OK && p_data->int_data.p_data) {
param.data_ind.len = p_data->int_data.p_data->len;
param.data_ind.data = p_data->int_data.p_data->data + p_data->int_data.p_data->offset;
}{...}
param.data_ind.handle = p_data->int_data.handle;
param.data_ind.status = p_data->int_data.status;
param.data_ind.proto_mode = proto_mode_change_to_upper_layer(p_data->int_data.proto_mode);
btc_hh_cb_to_app(ESP_HIDH_DATA_IND_EVT, ¶m);
break;...
case BTA_HH_API_ERR_EVT:
break;...
default:
BTC_TRACE_WARNING("%s: Unhandled event: %d", __func__, msg->act);
break;...
}{...}
btc_hh_cb_arg_deep_free(msg);
}{...}
void btc_hh_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
btc_hidh_args_t *dst = (btc_hidh_args_t *)p_dest;
btc_hidh_args_t *src = (btc_hidh_args_t *)p_src;
switch (msg->act) {
case BTC_HH_SET_INFO_EVT:
dst->set_info.hid_info = (esp_hidh_hid_info_t *)osi_malloc(sizeof(esp_hidh_hid_info_t));
if (dst->set_info.hid_info) {
memcpy(dst->set_info.hid_info, src->set_info.hid_info, sizeof(esp_hidh_hid_info_t));
}{...} else {
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
}{...}
break;...
case BTC_HH_SET_REPORT_EVT:
dst->set_report.report = (uint8_t *)osi_malloc(src->set_report.len);
if (dst->set_report.report) {
memcpy(dst->set_report.report, src->set_report.report, src->set_report.len);
}{...} else {
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
}{...}
break;...
case BTC_HH_SEND_DATA_EVT:
dst->send_data.data = (uint8_t *)osi_malloc(src->send_data.len);
if (dst->send_data.data) {
memcpy(dst->send_data.data, src->send_data.data, src->send_data.len);
}{...} else {
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
}{...}
break;...
default:
break;...
}{...}
}{...}
/* ... */
#endif