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
47
48
49
50
51
52
55
56
57
58
59
60
61
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
85
86
93
94
128
129
130
131
132
133
134
135
136
137
138
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
166
169
170
171
174
177
180
183
186
189
192
193
194
195
196
197
198
199
200
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
228
229
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
256
257
262
263
264
265
266
273
274
275
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
306
307
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
380
381
386
387
392
393
394
397
398
399
400
401
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
428
432
433
437
441
443
444
445
448
449
454
455
456
457
458
459
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
488
492
493
497
498
502
504
505
506
509
510
515
516
517
518
519
520
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
549
553
554
558
559
568
570
571
572
575
576
581
582
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
626
627
632
633
638
639
640
641
642
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
682
683
686
687
692
693
694
695
698
699
700
706
707
708
720
721
722
723
724
725
728
731
734
737
740
743
746
749
752
755
756
757
758
759
774
775
776
777
778
779
780
781
782
783
795
810
823
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
850
851
852
853
854
855
856
872
878
885
899
905
906
907
914
915
916
922
923
924
925
926
927
928
935
940
943
944
945
946
947
965
966
/* ... */
/* ... */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bta/bta_api.h"
#include "bta/bta_hd_api.h"
#include "bta/bta_hh_api.h"
#include "bta/utl.h"
#include "btc/btc_storage.h"
#include "btc/btc_util.h"
#include "btc/btc_manage.h"
#include "btc_hd.h"
#include "osi/allocator.h"
#include "esp_hidd_api.h"13 includes
#if HID_DEV_INCLUDED == TRUE
#include "bta_dm_int.h"
typedef enum { BTC_HD_DUMMY_REQ_EVT = 0 } btc_hd_req_evt_t;
/* ... */
btc_hd_cb_t btc_hd_cb = {0};
/* ... */
#define BTC_HD_APP_NAME_LEN 50
#define BTC_HD_APP_DESCRIPTION_LEN 50
#define BTC_HD_APP_PROVIDER_LEN 50
#define BTC_HD_APP_DESCRIPTOR_LEN 2048
#define COD_HID_KEYBOARD 0x0540
#define COD_HID_POINTING 0x0580
#define COD_HID_COMBO 0x05C0
#define COD_HID_MAJOR 0x0500
#define is_hidd_init() (btc_hd_cb.status > BTC_HD_DISABLED)
#define is_hidd_app_register() (btc_hd_cb.app_registered)10 defines
typedef void (bt_hid_copy_cb_t)(btc_msg_t *msg, void *p_dest, void *p_src);
static inline void btc_hd_cb_to_app(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param)
{
esp_hd_cb_t btc_hd_cb = (esp_hd_cb_t)btc_profile_cb_get(BTC_PID_HD);
if (btc_hd_cb) {
btc_hd_cb(event, param);
}{...}
}{...}
static void free_app_info_param(void)
{
utl_freebuf((void **)&btc_hd_cb.app_info.descriptor.dsc_list);
utl_freebuf((void **)&btc_hd_cb.app_info.p_provider);
utl_freebuf((void **)&btc_hd_cb.app_info.p_description);
utl_freebuf((void **)&btc_hd_cb.app_info.p_name);
}{...}
static void bte_hd_arg_deep_copy(btc_msg_t *msg, void *p_dst, void *p_src)
{
tBTA_HD *p_dst_data = (tBTA_HD *)p_dst;
tBTA_HD *p_src_data = (tBTA_HD *)p_src;
switch (msg->act)
{
case BTA_HD_SET_REPORT_EVT: {
uint8_t *src_data = p_src_data->set_report.p_data;
if (src_data) {
p_dst_data->set_report.p_data = osi_malloc(p_src_data->set_report.len);
if (p_dst_data->set_report.p_data == NULL) {
BTC_TRACE_ERROR("%s malloc set_report data failed!", __func__);
break;
}{...}
memcpy(p_dst_data->set_report.p_data, src_data, p_src_data->set_report.len);
}{...}
break;
}{...}
... case BTA_HD_INTR_DATA_EVT: {
uint8_t *src_data = p_src_data->intr_data.p_data;
if (src_data) {
p_dst_data->intr_data.p_data = osi_malloc(p_src_data->intr_data.len);
if (p_dst_data->intr_data.p_data == NULL) {
BTC_TRACE_ERROR("%s malloc intr_data data failed!", __func__);
break;
}{...}
memcpy(p_dst_data->intr_data.p_data, src_data, p_src_data->intr_data.len);
}{...}
break;
}{...}
... default:
break;...
}{...}
}{...}
/* ... */
void btc_hd_remove_device(bt_bdaddr_t bd_addr)
{
BTA_HdRemoveDevice((uint8_t *)&bd_addr);
}{...}
/* ... */
static void bte_hd_evt(tBTA_HD_EVT event, tBTA_HD *p_data)
{
bt_status_t status;
int param_len = 0;
BTC_TRACE_API("%s event=%d", __func__, event);
switch (event) {
case BTA_HD_ENABLE_EVT:
case BTA_HD_DISABLE_EVT:
case BTA_HD_UNREGISTER_APP_EVT:
param_len = sizeof(tBTA_HD_STATUS);
break;...
case BTA_HD_REGISTER_APP_EVT:
param_len = sizeof(tBTA_HD_REG_STATUS);
break;...
case BTA_HD_OPEN_EVT:
case BTA_HD_CLOSE_EVT:
case BTA_HD_VC_UNPLUG_EVT:
param_len = sizeof(tBTA_HD_CONN);
break;...
case BTA_HD_GET_REPORT_EVT:
param_len += sizeof(tBTA_HD_GET_REPORT);
break;...
case BTA_HD_SET_REPORT_EVT:
param_len = sizeof(tBTA_HD_SET_REPORT);
break;...
case BTA_HD_SET_PROTOCOL_EVT:
param_len += sizeof(p_data->set_protocol);
break;...
case BTA_HD_INTR_DATA_EVT:
param_len = sizeof(tBTA_HD_INTR_DATA);
break;...
case BTA_HD_SEND_REPORT_EVT:
param_len = sizeof(tBTA_HD_API_SEND_REPORT);
break;...
case BTA_HD_REPORT_ERR_EVT:
param_len = sizeof(tBTA_HD_API_REPORT_ERR);
break;...
}{...}
btc_msg_t msg;
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_HD;
msg.act = event;
status = btc_transfer_context(&msg, p_data, param_len, bte_hd_arg_deep_copy, btc_hd_cb_arg_deep_free);
if (status != BT_STATUS_SUCCESS) {
BTC_TRACE_ERROR("context transfer failed");
}{...}
}{...}
/* ... */
static void btc_hd_init(void)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
if (is_hidd_init()) {
BTC_TRACE_ERROR("%s HD has been initiated, shall uninit first!", __func__);
ret = ESP_HIDD_NEED_DEINIT;
break;
}{...}
memset(&btc_hd_cb, 0, sizeof(btc_hd_cb));
BTA_HdEnable(bte_hd_evt);
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param;
param.init.status = ret;
btc_hd_cb_to_app(ESP_HIDD_INIT_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_unregister_app(bool need_deinit);
static void btc_hd_deinit(void)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
if (!is_hidd_init()) {
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;
}{...}
if (btc_hd_cb.status == BTC_HD_DISABLING) {
BTC_TRACE_ERROR("%s is disabling, try later!", __func__);
ret = ESP_HIDD_BUSY;
break;
}{...}
btc_hd_cb.service_dereg_active = FALSE;
if (is_hidd_app_register()) {
btc_hd_unregister_app(true);
}{...} else {
btc_hd_cb.status = BTC_HD_DISABLING;
BTC_TRACE_WARNING("%s disabling hid device service now", __func__);
BTA_HdDisable();
}{...}
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param;
param.deinit.status = ret;
btc_hd_cb_to_app(ESP_HIDD_DEINIT_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_register_app(esp_hidd_app_param_t *p_app_param, esp_hidd_qos_param_t *p_in_qos,
esp_hidd_qos_param_t *p_out_qos)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
if (!is_hidd_init()) {
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;
}{...} else if (btc_hd_cb.status == BTC_HD_DISABLING) {
BTC_TRACE_ERROR("%s: deinit is in progress!", __func__);
ret = ESP_HIDD_BUSY;
break;
}{...}
if (is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application already registered, shall deregister first!", __func__);
ret = ESP_HIDD_NEED_DEREG;
break;
}{...}
if ((btc_hd_cb.app_info.p_name = (char *)osi_malloc(BTC_HD_APP_NAME_LEN)) == NULL ||
(btc_hd_cb.app_info.p_description = (char *)osi_malloc(BTC_HD_APP_DESCRIPTION_LEN)) == NULL ||
(btc_hd_cb.app_info.p_provider = (char *)osi_malloc(BTC_HD_APP_PROVIDER_LEN)) == NULL ||
(btc_hd_cb.app_info.descriptor.dsc_list = (uint8_t *)osi_malloc(p_app_param->desc_list_len)) == NULL) {
BTC_TRACE_ERROR(
"%s malloc app_info failed! p_name:%p, p_description:%p, p_provider:%p, descriptor.dsc_list:%p",
__func__, btc_hd_cb.app_info.p_name, btc_hd_cb.app_info.p_description, btc_hd_cb.app_info.p_provider,
btc_hd_cb.app_info.descriptor.dsc_list);
ret = ESP_HIDD_NO_RES;
break;
}{...}
memcpy(btc_hd_cb.app_info.p_name, p_app_param->name, BTC_HD_APP_NAME_LEN);
memcpy(btc_hd_cb.app_info.p_description, p_app_param->description, BTC_HD_APP_DESCRIPTION_LEN);
memcpy(btc_hd_cb.app_info.p_provider, p_app_param->provider, BTC_HD_APP_PROVIDER_LEN);
memcpy(btc_hd_cb.app_info.descriptor.dsc_list, p_app_param->desc_list, p_app_param->desc_list_len);
btc_hd_cb.app_info.subclass = p_app_param->subclass;
btc_hd_cb.app_info.descriptor.dl_len = p_app_param->desc_list_len;
btc_hd_cb.in_qos.service_type = p_in_qos->service_type;
btc_hd_cb.in_qos.token_rate = p_in_qos->token_rate;
btc_hd_cb.in_qos.token_bucket_size = p_in_qos->token_bucket_size;
btc_hd_cb.in_qos.peak_bandwidth = p_in_qos->peak_bandwidth;
btc_hd_cb.in_qos.access_latency = p_in_qos->access_latency;
btc_hd_cb.in_qos.delay_variation = p_in_qos->delay_variation;
btc_hd_cb.out_qos.service_type = p_out_qos->service_type;
btc_hd_cb.out_qos.token_rate = p_out_qos->token_rate;
btc_hd_cb.out_qos.token_bucket_size = p_out_qos->token_bucket_size;
btc_hd_cb.out_qos.peak_bandwidth = p_out_qos->peak_bandwidth;
btc_hd_cb.out_qos.access_latency = p_out_qos->access_latency;
btc_hd_cb.out_qos.delay_variation = p_out_qos->delay_variation;
BTA_HdRegisterApp(&btc_hd_cb.app_info, &btc_hd_cb.in_qos, &btc_hd_cb.out_qos);
}{...} while(0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param;
param.register_app.status = ret;
param.register_app.in_use = false;
memset(param.register_app.bd_addr, 0, BD_ADDR_LEN);
btc_hd_cb_to_app(ESP_HIDD_REGISTER_APP_EVT, ¶m);
}{...}
free_app_info_param();
}{...}
/* ... */
static void btc_hd_unregister_app(bool need_deinit)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
if (!is_hidd_init()) {
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;
}{...} else if (btc_hd_cb.status == BTC_HD_DISABLING) {
BTC_TRACE_ERROR("%s: deinit is in progress!", __func__);
ret = ESP_HIDD_BUSY;
break;
}{...}
if (!is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application has not been registered, shall register first!", __func__);
ret = ESP_HIDD_NEED_REG;
break;
}{...}
if (btc_hd_cb.service_dereg_active) {
BTC_TRACE_ERROR("%s: BT-HD deregistering in progress", __func__);
ret = ESP_HIDD_BUSY;
break;
}{...}
btc_hd_cb.service_dereg_active = TRUE;
if (need_deinit) {
btc_hd_cb.status = BTC_HD_DISABLING;
}{...}
BTA_HdUnregisterApp();
}{...} while(0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param = {0};
param.unregister_app.status = ret;
btc_hd_cb_to_app(ESP_HIDD_UNREGISTER_APP_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_connect(BD_ADDR bd_addr)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
switch (btc_hd_cb.status) {
case BTC_HD_DISABLED:
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;...
case BTC_HD_DISABLING:
BTC_TRACE_ERROR("%s: deinit is in progress!", __func__);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_CONNECTING:
case BTC_HD_DISCONNECTING:
BTC_TRACE_ERROR("%s: busy now, status:%d, try later!", __func__, btc_hd_cb.status);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_CONNECTED:
BTC_TRACE_ERROR("%s: already connect to the other HID host!", __func__);
ret = ESP_HIDD_NO_RES;
break;...
default:
break;...
}{...}
if (ret != ESP_HIDD_SUCCESS) {
break;
}{...}
if (!is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application has not been registered, shall register first!", __func__);
ret = ESP_HIDD_NEED_REG;
break;
}{...}
BTA_HdConnect(bd_addr);
btc_hd_cb.status = BTC_HD_CONNECTING;
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param = {0};
param.open.status = ret;
param.open.conn_status = ESP_HIDD_CONN_STATE_DISCONNECTED;
memcpy(param.open.bd_addr, bd_addr, BD_ADDR_LEN);
btc_hd_cb_to_app(ESP_HIDD_OPEN_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_disconnect(void)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
switch (btc_hd_cb.status) {
case BTC_HD_DISABLED:
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;...
case BTC_HD_DISABLING:
BTC_TRACE_ERROR("%s: deinit is in progress!", __func__);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_CONNECTING:
case BTC_HD_DISCONNECTING:
BTC_TRACE_ERROR("%s: busy now, status:%d, try later!", __func__, btc_hd_cb.status);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_ENABLED:
case BTC_HD_DISCONNECTED:
BTC_TRACE_ERROR("%s: no connection!", __func__);
ret = ESP_HIDD_NO_CONNECTION;
break;...
default:
break;...
}{...}
if (ret != ESP_HIDD_SUCCESS) {
break;
}{...}
if (!is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application has not been registered, shall register first!", __func__);
ret = ESP_HIDD_NEED_REG;
break;
}{...}
BTA_HdDisconnect();
btc_hd_cb.status = BTC_HD_DISCONNECTING;
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param = {0};
param.close.status = ret;
param.close.conn_status = ESP_HIDD_CONN_STATE_DISCONNECTED;
btc_hd_cb_to_app(ESP_HIDD_CLOSE_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_send_report(esp_hidd_report_type_t type, uint8_t id, uint16_t len, uint8_t *p_data)
{
BTC_TRACE_API("%s: type=%d id=%d len=%d", __func__, type, id, len);
tBTA_HD_REPORT report = {0};
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
switch (btc_hd_cb.status) {
case BTC_HD_DISABLED:
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;...
case BTC_HD_DISABLING:
BTC_TRACE_ERROR("%s: deinit is in progress!", __func__);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_CONNECTING:
case BTC_HD_DISCONNECTING:
BTC_TRACE_ERROR("%s: busy now, status:%d, try later!", __func__, btc_hd_cb.status);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_ENABLED:
case BTC_HD_DISCONNECTED:
if (type == ESP_HIDD_REPORT_TYPE_INTRDATA) {
BTC_TRACE_WARNING("%s: no connection, try to reconnect!", __func__);
btc_hd_cb.status = BTC_HD_CONNECTING;
}{...} else {
BTC_TRACE_ERROR("%s: no connection!", __func__);
ret = ESP_HIDD_NO_CONNECTION;
}{...}
break;...
default:
break;...
}{...}
if (ret != ESP_HIDD_SUCCESS) {
break;
}{...}
if (!is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application has not been registered, shall register first!", __func__);
ret = ESP_HIDD_NEED_REG;
break;
}{...}
if (type == ESP_HIDD_REPORT_TYPE_INTRDATA) {
report.type = ESP_HIDD_REPORT_TYPE_INPUT;
report.use_intr = TRUE;
}{...} else {
report.type = (type & 0x03);
report.use_intr = FALSE;
}{...}
report.id = id;
report.len = len;
report.p_data = p_data;
BTA_HdSendReport(&report);
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param = {0};
param.send_report.status = ret;
param.send_report.reason = 0;
param.send_report.report_type = report.type;
param.send_report.report_id = report.id;
btc_hd_cb_to_app(ESP_HIDD_SEND_REPORT_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_report_error(uint8_t error)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
if (!is_hidd_init()) {
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;
}{...}
if (!is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application has not been registered, shall register first!", __func__);
ret = ESP_HIDD_NEED_REG;
break;
}{...}
if (btc_hd_cb.status != BTC_HD_CONNECTED) {
BTC_TRACE_ERROR("%s: no connection!", __func__);
ret = ESP_HIDD_NO_CONNECTION;
break;
}{...}
BTA_HdReportError(error);
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param = {0};
param.report_err.status = ret;
param.report_err.reason = 0;
btc_hd_cb_to_app(ESP_HIDD_REPORT_ERR_EVT, ¶m);
}{...}
}{...}
/* ... */
static void btc_hd_virtual_cable_unplug(void)
{
BTC_TRACE_API("%s", __func__);
esp_hidd_status_t ret = ESP_HIDD_SUCCESS;
do {
switch (btc_hd_cb.status) {
case BTC_HD_DISABLED:
BTC_TRACE_ERROR("%s HD has not been initiated, shall init first!", __func__);
ret = ESP_HIDD_NEED_INIT;
break;...
case BTC_HD_DISABLING:
BTC_TRACE_ERROR("%s: deinit is in progress!", __func__);
ret = ESP_HIDD_BUSY;
break;...
case BTC_HD_CONNECTING:
case BTC_HD_DISCONNECTING:
BTC_TRACE_ERROR("%s: busy now, status:%d, try later!", __func__, btc_hd_cb.status);
ret = ESP_HIDD_BUSY;
break;...
default:
break;...
}{...}
if (ret != ESP_HIDD_SUCCESS) {
break;
}{...}
if (!is_hidd_app_register()) {
BTC_TRACE_ERROR("%s: application has not been registered, shall register first!", __func__);
ret = ESP_HIDD_NEED_REG;
break;
}{...}
BTA_HdVirtualCableUnplug();
if (btc_hd_cb.status == BTC_HD_CONNECTED) {
btc_hd_cb.status = BTC_HD_DISCONNECTING;
}{...}
}{...} while (0);
if (ret != ESP_HIDD_SUCCESS) {
esp_hidd_cb_param_t param = {0};
param.vc_unplug.status = ret;
param.vc_unplug.conn_status = ESP_HIDD_CONN_STATE_DISCONNECTED;
btc_hd_cb_to_app(ESP_HIDD_VC_UNPLUG_EVT, ¶m);
}{...}
}{...}
static void btc_hd_call_arg_deep_free(btc_msg_t *msg)
{
btc_hidd_args_t *arg = (btc_hidd_args_t *)msg->arg;
switch (msg->act) {
case BTC_HD_SEND_REPORT_EVT:
utl_freebuf((void **)&arg->send_report.data);
break;...
default:
break;...
}{...}
}{...}
void btc_hd_call_handler(btc_msg_t *msg)
{
btc_hidd_args_t *arg = (btc_hidd_args_t *)(msg->arg);
switch (msg->act) {
case BTC_HD_INIT_EVT:
btc_hd_init();
break;...
case BTC_HD_DEINIT_EVT:
btc_hd_deinit();
break;...
case BTC_HD_REGISTER_APP_EVT:
btc_hd_register_app(arg->register_app.app_param, arg->register_app.in_qos, arg->register_app.out_qos);
break;...
case BTC_HD_UNREGISTER_APP_EVT:
btc_hd_unregister_app(false);
break;...
case BTC_HD_CONNECT_EVT:
btc_hd_connect(arg->connect.bd_addr);
break;...
case BTC_HD_DISCONNECT_EVT:
btc_hd_disconnect();
break;...
case BTC_HD_SEND_REPORT_EVT:
btc_hd_send_report(arg->send_report.type, arg->send_report.id, arg->send_report.len, arg->send_report.data);
break;...
case BTC_HD_REPORT_ERROR_EVT:
btc_hd_report_error(arg->error);
break;...
case BTC_HD_UNPLUG_EVT:
btc_hd_virtual_cable_unplug();
break;...
default:
BTC_TRACE_WARNING("unknown hidd action %i", msg->act);
break;...
}{...}
btc_hd_call_arg_deep_free(msg);
}{...}
void btc_hd_cb_arg_deep_free(btc_msg_t *msg)
{
tBTA_HD *arg = (tBTA_HD *)msg->arg;
switch (msg->act) {
case BTA_HD_SET_REPORT_EVT:
utl_freebuf((void **)&arg->set_report.p_data);
break;...
case BTA_HD_INTR_DATA_EVT:
utl_freebuf((void **)&arg->intr_data.p_data);
break;...
default:
break;...
}{...}
}{...}
void btc_hd_cb_handler(btc_msg_t *msg)
{
uint16_t event = msg->act;
tBTA_HD *p_data = (tBTA_HD *)msg->arg;
esp_hidd_cb_param_t param = {0};
BTC_TRACE_API("%s: event=%s", __func__, dump_hd_event(event));
switch (event) {
case BTA_HD_ENABLE_EVT:
BTC_TRACE_DEBUG("%s: status=%d", __func__, p_data->status);
if (p_data->status == BTA_HD_OK) {
btc_storage_load_hidd();
btc_hd_cb.status = BTC_HD_ENABLED;
}{...} else {
btc_hd_cb.status = BTC_HD_DISABLED;
BTC_TRACE_WARNING("Failed to enable BT-HD, status=%d", p_data->status);
}{...}
param.init.status = p_data->status;
btc_hd_cb_to_app(ESP_HIDD_INIT_EVT, ¶m);
break;...
case BTA_HD_DISABLE_EVT:
BTC_TRACE_DEBUG("%s: status=%d", __func__, p_data->status);
if (p_data->status == BTA_HD_OK){
btc_hd_cb.status = BTC_HD_DISABLED;
if (btc_hd_cb.service_dereg_active) {
btc_hd_cb.service_dereg_active = FALSE;
}{...}
free_app_info_param();
memset(&btc_hd_cb, 0, sizeof(btc_hd_cb));
}{...} else {
BTC_TRACE_WARNING("Failed to disable BT-HD, status=%d", p_data->status);
}{...}
param.deinit.status = p_data->status;
btc_hd_cb_to_app(ESP_HIDD_DEINIT_EVT, ¶m);
break;...
case BTA_HD_REGISTER_APP_EVT:
if (p_data->reg_status.status == BTA_HD_OK) {
btc_hd_cb.app_registered = TRUE;
}{...}
param.register_app.status = p_data->reg_status.status;
param.register_app.in_use = p_data->reg_status.in_use;
if (!p_data->reg_status.in_use) {
memset(param.register_app.bd_addr, 0, BD_ADDR_LEN);
}{...} else {
memcpy(param.register_app.bd_addr, p_data->reg_status.bda, BD_ADDR_LEN);
}{...}
btc_hd_cb_to_app(ESP_HIDD_REGISTER_APP_EVT, ¶m);
break;...
case BTA_HD_UNREGISTER_APP_EVT:
btc_hd_cb.app_registered = FALSE;
param.unregister_app.status = p_data->status;
btc_hd_cb_to_app(ESP_HIDD_UNREGISTER_APP_EVT, ¶m);
if (btc_hd_cb.status == BTC_HD_DISABLING) {
BTC_TRACE_WARNING("disabling hid device service now");
BTA_HdDisable();
}{...}
break;...
case BTA_HD_OPEN_EVT: {
bt_bdaddr_t *addr = (bt_bdaddr_t *)&p_data->conn.bda;
BTC_TRACE_EVENT("BTA_HD_OPEN_EVT, address (%02x:%02x:%02x:%02x:%02x:%02x)", addr->address[0], addr->address[1],
addr->address[2], addr->address[3], addr->address[4], addr->address[5]);
if (p_data->conn.status == BTA_HD_OK && p_data->conn.conn_status == BTA_HD_CONN_STATE_CONNECTED) {
btc_hd_cb.status = BTC_HD_CONNECTED;
}{...} else if (p_data->conn.conn_status == BTA_HD_CONN_STATE_DISCONNECTED) {
btc_hd_cb.status = BTC_HD_DISCONNECTED;
}{...}
param.open.status = p_data->conn.status;
param.open.conn_status = p_data->conn.conn_status;
memcpy(param.open.bd_addr, p_data->conn.bda, BD_ADDR_LEN);
btc_hd_cb_to_app(ESP_HIDD_OPEN_EVT, ¶m);
break;
}{...}
... case BTA_HD_CLOSE_EVT:
if (p_data->conn.conn_status == BTA_HD_CONN_STATE_DISCONNECTED) {
btc_hd_cb.status = BTC_HD_DISCONNECTED;
if (btc_hd_cb.forced_disc) {
bt_bdaddr_t *addr = (bt_bdaddr_t *)&p_data->conn.bda;
BTC_TRACE_WARNING("remote device was forcefully disconnected");
btc_hd_remove_device(*addr);
btc_hd_cb.forced_disc = FALSE;
break;
}{...}
}{...}
param.close.status = p_data->conn.status;
param.close.conn_status = p_data->conn.conn_status;
btc_hd_cb_to_app(ESP_HIDD_CLOSE_EVT, ¶m);
break;...
case BTA_HD_GET_REPORT_EVT:
param.get_report.report_type = p_data->get_report.report_type;
param.get_report.report_id = p_data->get_report.report_id;
param.get_report.buffer_size = p_data->get_report.buffer_size;
btc_hd_cb_to_app(ESP_HIDD_GET_REPORT_EVT, ¶m);
break;...
case BTA_HD_SET_REPORT_EVT:
param.set_report.report_type = p_data->set_report.report_type;
param.set_report.report_id = p_data->set_report.report_id;
param.set_report.len = p_data->set_report.len;
param.set_report.data = p_data->set_report.p_data;
btc_hd_cb_to_app(ESP_HIDD_SET_REPORT_EVT, ¶m);
break;...
case BTA_HD_SET_PROTOCOL_EVT:
switch (p_data->set_protocol) {
case HID_PAR_PROTOCOL_BOOT_MODE:
param.set_protocol.protocol_mode = ESP_HIDD_BOOT_MODE;
break;...
case HID_PAR_PROTOCOL_REPORT:
param.set_protocol.protocol_mode = ESP_HIDD_REPORT_MODE;
break;...
default:
param.set_protocol.protocol_mode = ESP_HIDD_UNSUPPORTED_MODE;
break;...
}{...}
btc_hd_cb_to_app(ESP_HIDD_SET_PROTOCOL_EVT, ¶m);
break;...
case BTA_HD_INTR_DATA_EVT:
param.intr_data.report_id = p_data->intr_data.report_id;
param.intr_data.len = p_data->intr_data.len;
param.intr_data.data = p_data->intr_data.p_data;
btc_hd_cb_to_app(ESP_HIDD_INTR_DATA_EVT, ¶m);
break;...
case BTA_HD_VC_UNPLUG_EVT: {
bt_bdaddr_t *bd_addr = (bt_bdaddr_t *)&p_data->conn.bda;
if (bta_dm_check_if_only_hd_connected(p_data->conn.bda)) {
BTC_TRACE_DEBUG("%s: Removing bonding as only HID profile connected", __func__);
BTA_DmRemoveDevice((uint8_t *)&p_data->conn.bda, BT_TRANSPORT_BR_EDR);
}{...} else {
BTC_TRACE_DEBUG("%s: Only removing HID data as some other profiles connected", __func__);
btc_hd_remove_device(*bd_addr);
}{...}
if (btc_hd_cb.status == BTC_HD_DISCONNECTING || btc_hd_cb.status == BTC_HD_CONNECTING ||
btc_hd_cb.status == BTC_HD_CONNECTED) {
btc_hd_cb.status = BTC_HD_DISCONNECTED;
param.close.status = p_data->conn.status;
param.close.conn_status = p_data->conn.conn_status;
btc_hd_cb_to_app(ESP_HIDD_CLOSE_EVT, ¶m);
}{...}
param.vc_unplug.status = p_data->conn.status;
param.vc_unplug.conn_status = p_data->conn.conn_status;
btc_hd_cb_to_app(ESP_HIDD_VC_UNPLUG_EVT, ¶m);
break;
}{...}
... case BTA_HD_SEND_REPORT_EVT:
param.send_report.status = p_data->send_report.status;
param.send_report.reason = p_data->send_report.reason;
param.send_report.report_type = p_data->send_report.report_type;
param.send_report.report_id = p_data->send_report.report_id;
btc_hd_cb_to_app(ESP_HIDD_SEND_REPORT_EVT, ¶m);
break;...
case BTA_HD_REPORT_ERR_EVT:
param.report_err.status = p_data->report_err.status;
param.report_err.reason = p_data->report_err.reason;
btc_hd_cb_to_app(ESP_HIDD_REPORT_ERR_EVT, ¶m);
break;...
default:
BTC_TRACE_WARNING("%s: unknown event (%d)", __func__, event);
break;...
}{...}
btc_hd_cb_arg_deep_free(msg);
}{...}
void btc_hd_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
btc_hidd_args_t *dst = (btc_hidd_args_t *)p_dest;
btc_hidd_args_t *src = (btc_hidd_args_t *)p_src;
switch (msg->act) {
case BTC_HD_SEND_REPORT_EVT:
dst->send_report.data = (uint8_t *)osi_malloc(src->send_report.len);
if (dst->send_report.data) {
memcpy(dst->send_report.data, src->send_report.data, src->send_report.len);
}{...} else {
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
}{...}
break;...
default:
break;...
}{...}
}{...}
/* ... */
#endif