summaryrefslogtreecommitdiffstats
path: root/btif/src/btif_hd.c
blob: 6235861315f2840a59e82d078f0e5ab8fea0e945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/******************************************************************************
 *
 *  Copyright (c) 2013, The Linux Foundation. All rights reserved.
 *  Not a Contribution.
 *  Copyright (C) 2009-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/************************************************************************************
 *
 *  Filename:      btif_hd.c
 *
 *  Description:   HID Device Profile Bluetooth Interface
 *
 *
 ***********************************************************************************/
#include <hardware/bluetooth.h>
#include <hardware/bt_hd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define LOG_TAG "BTIF_HD"

#include "bta_api.h"
#include "bta_hd_api.h"
#include "bta_hh_api.h"

#include "btif_common.h"
#include "btif_util.h"
#include "btif_storage.h"
#include "btif_hd.h"

#define BTIF_HD_APP_NAME_LEN 50
#define BTIF_HD_APP_DESCRIPTION_LEN 50
#define BTIF_HD_APP_PROVIDER_LEN 50
#define BTIF_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

extern BOOLEAN bta_dm_check_if_only_hd_connected(BD_ADDR peer_addr);
extern BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod);
extern void btif_hh_service_registration(BOOLEAN enable);

/* HD request events */
typedef enum
{
    BTIF_HD_DUMMY_REQ_EVT = 0
} btif_hd_req_evt_t;

btif_hd_cb_t btif_hd_cb;

static bthd_callbacks_t *bt_hd_callbacks = NULL;
static tBTA_HD_APP_INFO app_info;
static tBTA_HD_QOS_INFO in_qos;
static tBTA_HD_QOS_INFO out_qos;

static void intr_data_copy_cb(UINT16 event, char *p_dst, char *p_src)
{
    tBTA_HD_INTR_DATA *p_dst_data = (tBTA_HD_INTR_DATA *) p_dst;
    tBTA_HD_INTR_DATA *p_src_data = (tBTA_HD_INTR_DATA *) p_src;
    UINT8 *p_data;

    if (!p_src)
        return;

    if (event != BTA_HD_INTR_DATA_EVT)
        return;

    memcpy(p_dst, p_src, sizeof(tBTA_HD_INTR_DATA));

    p_data = ((UINT8 *) p_dst_data) + sizeof(tBTA_HD_INTR_DATA);

    memcpy(p_data, p_src_data->p_data, p_src_data->len);

    p_dst_data->p_data = p_data;
}

static void set_report_copy_cb(UINT16 event, char *p_dst, char *p_src)
{
    tBTA_HD_SET_REPORT *p_dst_data = (tBTA_HD_SET_REPORT *) p_dst;
    tBTA_HD_SET_REPORT *p_src_data = (tBTA_HD_SET_REPORT *) p_src;
    UINT8 *p_data;

    if (!p_src)
        return;

    if (event != BTA_HD_SET_REPORT_EVT)
        return;

    memcpy(p_dst, p_src, sizeof(tBTA_HD_SET_REPORT));

    p_data = ((UINT8 *) p_dst_data) + sizeof(tBTA_HD_SET_REPORT);

    memcpy(p_data, p_src_data->p_data, p_src_data->len);

    p_dst_data->p_data = p_data;
}

static void btif_hd_free_buf()
{
    if (app_info.descriptor.dsc_list)
        GKI_freebuf(app_info.descriptor.dsc_list);
    if (app_info.p_description)
        GKI_freebuf(app_info.p_description);
    if (app_info.p_name)
        GKI_freebuf(app_info.p_name);
    if (app_info.p_provider)
        GKI_freebuf(app_info.p_provider);
    app_info.descriptor.dsc_list = NULL;
    app_info.p_description = NULL;
    app_info.p_name = NULL;
    app_info.p_provider = NULL;
}

/*******************************************************************************
**
** Function         btif_hd_remove_device
**
** Description      Removes plugged device
**
** Returns          void
**
*******************************************************************************/
void btif_hd_remove_device(bt_bdaddr_t bd_addr)
{
    BTA_HdRemoveDevice((UINT8 *) &bd_addr);
    btif_storage_remove_hidd(&bd_addr);
}

/*******************************************************************************
**
** Function         btif_hd_upstreams_evt
**
** Description      Executes events in btif context
**
** Returns          void
**
*******************************************************************************/
static void btif_hd_upstreams_evt(UINT16 event, char* p_param)
{
    tBTA_HD *p_data = (tBTA_HD *)p_param;

    BTIF_TRACE_API("%s: event=%s", __FUNCTION__, dump_hd_event(event));

    switch (event)
    {
        case BTA_HD_ENABLE_EVT:
            BTIF_TRACE_DEBUG("%s: status=%d", __FUNCTION__, p_data->status);
            if (p_data->status == BTA_HD_OK)
            {
                btif_storage_load_hidd();
                btif_hd_cb.status = BTIF_HD_ENABLED;
                /* Register the app if not yet registered */
                if (!btif_hd_cb.app_registered)
                {
                    BTA_HdRegisterApp(&app_info, &in_qos, &out_qos);
                    btif_hd_free_buf();
                }
            }
            else
            {
                btif_hd_cb.status = BTIF_HD_DISABLED;
                BTIF_TRACE_WARNING("Failed to enable BT-HD, status=%d", p_data->status);
            }
            break;

        case BTA_HD_DISABLE_EVT:
            BTIF_TRACE_DEBUG("%s: status=%d", __FUNCTION__, p_data->status);
            btif_hd_cb.status = BTIF_HD_DISABLED;
            if (btif_hd_cb.service_dereg_active)
            {
                BTIF_TRACE_WARNING("registering hid host now");
                btif_hh_service_registration(TRUE);
                btif_hd_cb.service_dereg_active = FALSE;
            }
            btif_hd_free_buf();
            if (p_data->status == BTA_HD_OK)
                memset(&btif_hd_cb, 0, sizeof(btif_hd_cb));
            else
                BTIF_TRACE_WARNING("Failed to disable BT-HD, status=%d", p_data->status);
            break;

        case BTA_HD_REGISTER_APP_EVT:
            {
                bt_bdaddr_t *addr = (bt_bdaddr_t*) &p_data->reg_status.bda;

                if (!p_data->reg_status.in_use)
                {
                    addr = NULL;
                }

                btif_hd_cb.app_registered = TRUE;
                HAL_CBACK(bt_hd_callbacks, application_state_cb, addr, BTHD_APP_STATE_REGISTERED);
            }
            break;

        case BTA_HD_UNREGISTER_APP_EVT:
            btif_hd_cb.app_registered = FALSE;
            HAL_CBACK(bt_hd_callbacks, application_state_cb, NULL, BTHD_APP_STATE_NOT_REGISTERED);
            if (btif_hd_cb.service_dereg_active)
            {
                BTIF_TRACE_WARNING("disabling hid device service now");
                btif_hd_free_buf();
                BTA_HdDisable();
            }
            break;

        case BTA_HD_OPEN_EVT:
            {
                bt_bdaddr_t *addr = (bt_bdaddr_t*) &p_data->conn.bda;
                BTIF_TRACE_WARNING("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]);
                /* Check if the connection is from hid host and not hid device */
                if (check_cod_hid(addr, COD_HID_MAJOR))
                {
                    /* Incoming connection from hid device, reject it */
                    BTIF_TRACE_WARNING("remote device is not hid host, disconnecting");
                    btif_hd_cb.forced_disc = TRUE;
                    BTA_HdDisconnect();
                    break;
                }
                btif_storage_set_hidd((bt_bdaddr_t*) &p_data->conn.bda);

                HAL_CBACK(bt_hd_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,
                    BTHD_CONN_STATE_CONNECTED);
            }
            break;

        case BTA_HD_CLOSE_EVT:
             if (btif_hd_cb.forced_disc)
             {
                 bt_bdaddr_t *addr = (bt_bdaddr_t*) &p_data->conn.bda;
                 BTIF_TRACE_WARNING("remote device was forcefully disconnected");
                 btif_hd_remove_device(*addr);
                 btif_hd_cb.forced_disc = FALSE;
                 break;
             }
             HAL_CBACK(bt_hd_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,
                BTHD_CONN_STATE_DISCONNECTED);
             break;

        case BTA_HD_GET_REPORT_EVT:
            HAL_CBACK(bt_hd_callbacks, get_report_cb,
                        p_data->get_report.report_type, p_data->get_report.report_id,
                        p_data->get_report.buffer_size);
            break;

        case BTA_HD_SET_REPORT_EVT:
            HAL_CBACK(bt_hd_callbacks, set_report_cb, p_data->set_report.report_type,
                p_data->set_report.report_id, p_data->set_report.len,
                p_data->set_report.p_data);
            break;

        case BTA_HD_SET_PROTOCOL_EVT:
            HAL_CBACK(bt_hd_callbacks, set_protocol_cb, p_data->set_protocol);
            break;

        case BTA_HD_INTR_DATA_EVT:
            HAL_CBACK(bt_hd_callbacks, intr_data_cb, p_data->intr_data.report_id,
                p_data->intr_data.len, p_data->intr_data.p_data);
            break;

        case BTA_HD_VC_UNPLUG_EVT:
            HAL_CBACK(bt_hd_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,
                BTHD_CONN_STATE_DISCONNECTED);
            if (bta_dm_check_if_only_hd_connected(p_data->conn.bda)) {
                BTIF_TRACE_DEBUG("%s: Removing bonding as only HID profile connected",
                    __FUNCTION__);
                BTA_DmRemoveDevice((UINT8 *) &p_data->conn.bda);
            } else {
                bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)&p_data->conn.bda;
                BTIF_TRACE_DEBUG("%s: Only removing HID data as some other profiles "
                    "connected", __FUNCTION__);
                btif_hd_remove_device(*bd_addr);
            }
            HAL_CBACK(bt_hd_callbacks, vc_unplug_cb);
            break;

        default:
            BTIF_TRACE_WARNING("%s: unknown event (%d)", __FUNCTION__, event);
            break;
    }
}

/*******************************************************************************
**
** Function         bte_hd_evt
**
** Description      Switches context from BTE to BTIF for all BT-HD events
**
** Returns          void
**
*******************************************************************************/

static void bte_hd_evt(tBTA_HD_EVT event, tBTA_HD *p_data)
{
    bt_status_t status;
    int param_len = 0;
    tBTIF_COPY_CBACK *p_copy_cback = NULL;

    BTIF_TRACE_API("%s event=%d", __FUNCTION__, 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) + p_data->set_report.len;
        p_copy_cback = set_report_copy_cb;
        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) + p_data->intr_data.len;
        p_copy_cback = intr_data_copy_cb;
        break;

    }

    status = btif_transfer_context(btif_hd_upstreams_evt, (uint16_t)event,
        (void*)p_data, param_len, p_copy_cback);

    ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
}

/*******************************************************************************
**
** Function        init
**
** Description     Initializes BT-HD interface
**
** Returns         BT_STATUS_SUCCESS
**
*******************************************************************************/
static bt_status_t init(bthd_callbacks_t* callbacks )
{
    BTIF_TRACE_API("%s", __FUNCTION__);

    bt_hd_callbacks = callbacks;
    memset(&btif_hd_cb, 0, sizeof(btif_hd_cb));

    btif_enable_service(BTA_HIDD_SERVICE_ID);

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         cleanup
**
** Description      Cleans up BT-HD interface
**
** Returns          none
**
*******************************************************************************/
static void  cleanup(void)
{
    BTIF_TRACE_API("hd:%s", __FUNCTION__);

    if (bt_hd_callbacks)
    {
        /* update flag, not to enable hid host service now as BT is switching off */
        btif_hd_cb.service_dereg_active = FALSE;
        btif_disable_service(BTA_HIDD_SERVICE_ID);
        bt_hd_callbacks = NULL;
    }
}

/*******************************************************************************
**
** Function         register_app
**
** Description      Registers HID Device application
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t register_app(bthd_app_param_t *p_app_param, bthd_qos_param_t *p_in_qos,
                                            bthd_qos_param_t *p_out_qos)
{

    BTIF_TRACE_API("%s", __FUNCTION__);

    if (btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application already registered", __FUNCTION__);
        return BT_STATUS_BUSY;
    }

    app_info.p_name = GKI_getbuf(BTIF_HD_APP_NAME_LEN);
    if (app_info.p_name == NULL)
        return BT_STATUS_NOMEM;
    memcpy(app_info.p_name, p_app_param->name, BTIF_HD_APP_NAME_LEN);
    app_info.p_description = GKI_getbuf(BTIF_HD_APP_DESCRIPTION_LEN);
    if (app_info.p_description == NULL) {
        GKI_freebuf(app_info.p_name);
        return BT_STATUS_NOMEM;
    }
    memcpy(app_info.p_description, p_app_param->description, BTIF_HD_APP_DESCRIPTION_LEN);
    app_info.p_provider = GKI_getbuf(BTIF_HD_APP_PROVIDER_LEN);
    if (app_info.p_provider == NULL) {
        GKI_freebuf(app_info.p_name);
        GKI_freebuf(app_info.p_description);
        return BT_STATUS_NOMEM;
    }
    memcpy(app_info.p_provider, p_app_param->provider, BTIF_HD_APP_PROVIDER_LEN);
    app_info.subclass = p_app_param->subclass;
    app_info.descriptor.dl_len = p_app_param->desc_list_len;
    app_info.descriptor.dsc_list = GKI_getbuf(app_info.descriptor.dl_len);
    if (app_info.descriptor.dsc_list == NULL) {
        GKI_freebuf(app_info.p_name);
        GKI_freebuf(app_info.p_description);
        GKI_freebuf(app_info.p_provider);
        return BT_STATUS_NOMEM;
    }
    memcpy(app_info.descriptor.dsc_list, p_app_param->desc_list, p_app_param->desc_list_len);

    in_qos.service_type = p_in_qos->service_type;
    in_qos.token_rate = p_in_qos->token_rate;
    in_qos.token_bucket_size = p_in_qos->token_bucket_size;
    in_qos.peak_bandwidth = p_in_qos->peak_bandwidth;
    in_qos.access_latency = p_in_qos->access_latency;
    in_qos.delay_variation = p_in_qos->delay_variation;

    out_qos.service_type = p_out_qos->service_type;
    out_qos.token_rate = p_out_qos->token_rate;
    out_qos.token_bucket_size = p_out_qos->token_bucket_size;
    out_qos.peak_bandwidth = p_out_qos->peak_bandwidth;
    out_qos.access_latency = p_out_qos->access_latency;
    out_qos.delay_variation = p_out_qos->delay_variation;

    /* register HID Device with L2CAP and unregister HID Host with L2CAP */
    /* Disable HH */
    btif_hh_service_registration(FALSE);

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         unregister_app
**
** Description      Unregisters HID Device application
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t unregister_app(void)
{
    BTIF_TRACE_API("%s", __FUNCTION__);

    if (!btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application not yet registered", __FUNCTION__);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.status != BTIF_HD_ENABLED)
    {
        BTIF_TRACE_WARNING("%s: BT-HD not enabled, status=%d", __FUNCTION__, btif_hd_cb.status);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.service_dereg_active)
    {
        BTIF_TRACE_WARNING("%s: BT-HD deregistering in progress", __FUNCTION__);
        return BT_STATUS_BUSY;
    }

    btif_hd_cb.service_dereg_active = TRUE;
    BTA_HdUnregisterApp();

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         connect
**
** Description      Connects to host
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t connect(void)
{
    BTIF_TRACE_API("%s", __FUNCTION__);

    if (!btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application not yet registered", __FUNCTION__);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.status != BTIF_HD_ENABLED)
    {
        BTIF_TRACE_WARNING("%s: BT-HD not enabled, status=%d", __FUNCTION__, btif_hd_cb.status);
        return BT_STATUS_NOT_READY;
    }

    BTA_HdConnect();

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         disconnect
**
** Description      Disconnects from host
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t disconnect(void)
{
    BTIF_TRACE_API("%s", __FUNCTION__);

    if (!btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application not yet registered", __FUNCTION__);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.status != BTIF_HD_ENABLED)
    {
        BTIF_TRACE_WARNING("%s: BT-HD not enabled, status=%d", __FUNCTION__, btif_hd_cb.status);
        return BT_STATUS_NOT_READY;
    }

    BTA_HdDisconnect();

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         send_report
**
** Description      Sends Reports to hid host
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t send_report(bthd_report_type_t type, uint8_t id, uint16_t len, uint8_t *p_data)
{
    tBTA_HD_REPORT report;

    APPL_TRACE_VERBOSE("%s: type=%d id=%d len=%d", __FUNCTION__, type, id, len);

    if (!btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application not yet registered", __FUNCTION__);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.status != BTIF_HD_ENABLED)
    {
        BTIF_TRACE_WARNING("%s: BT-HD not enabled, status=%d", __FUNCTION__, btif_hd_cb.status);
        return BT_STATUS_NOT_READY;
    }

    if (type == BTHD_REPORT_TYPE_INTRDATA)
    {
        report.type = BTHD_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);

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         report_error
**
** Description      Sends HANDSHAKE with error info for invalid SET_REPORT
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t report_error(uint8_t error)
{
    BTIF_TRACE_API("%s", __FUNCTION__);

    if (!btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application not yet registered", __FUNCTION__);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.status != BTIF_HD_ENABLED)
    {
        BTIF_TRACE_WARNING("%s: BT-HD not enabled, status=%d", __FUNCTION__, btif_hd_cb.status);
        return BT_STATUS_NOT_READY;
    }

    BTA_HdReportError(error);

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         virtual_cable_unplug
**
** Description      Sends Virtual Cable Unplug to host
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t virtual_cable_unplug(void)
{
    BTIF_TRACE_API("%s", __FUNCTION__);

    if (!btif_hd_cb.app_registered)
    {
        BTIF_TRACE_WARNING("%s: application not yet registered", __FUNCTION__);
        return BT_STATUS_NOT_READY;
    }

    if (btif_hd_cb.status != BTIF_HD_ENABLED)
    {
        BTIF_TRACE_WARNING("%s: BT-HD not enabled, status=%d", __FUNCTION__, btif_hd_cb.status);
        return BT_STATUS_NOT_READY;
    }

    BTA_HdVirtualCableUnplug();

    return BT_STATUS_SUCCESS;
}

static const bthd_interface_t bthdInterface = {
    sizeof(bthdInterface),
    init,
    cleanup,
    register_app,
    unregister_app,
    connect,
    disconnect,
    send_report,
    report_error,
    virtual_cable_unplug,
};

/*******************************************************************************
**
** Function         btif_hd_execute_service
**
** Description      Enabled/disables BT-HD service
**
** Returns          BT_STATUS_SUCCESS
**
*******************************************************************************/
bt_status_t btif_hd_execute_service(BOOLEAN b_enable)
{
    BTIF_TRACE_API("%s: b_enable=%d", __FUNCTION__, b_enable);

     if (!b_enable)
         BTA_HdDisable();

     return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         btif_hd_get_interface
**
** Description      Gets BT-HD interface
**
** Returns          bthd_interface_t
**
*******************************************************************************/
const bthd_interface_t *btif_hd_get_interface()
{
    BTIF_TRACE_API("%s", __FUNCTION__);
    return &bthdInterface;
}

/*******************************************************************************
**
** Function         btif_hd_service_registration
**
** Description      Registers hid device service
**
** Returns          none
**
*******************************************************************************/
void btif_hd_service_registration()
{
    BTIF_TRACE_API("%s", __FUNCTION__);
    /* enable HD */
    BTA_HdEnable(bte_hd_evt);
}