Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /************************************************************************************
     20  *
     21  *  Filename:      btif_hh.c
     22  *
     23  *  Description:   HID Host Profile Bluetooth Interface
     24  *
     25  *
     26  ***********************************************************************************/
     27 #include <hardware/bluetooth.h>
     28 #include <hardware/bt_hh.h>
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <errno.h>
     32 #include <string.h>
     33 
     34 #define LOG_TAG "BTIF_HH"
     35 
     36 #include "bta_api.h"
     37 #include "bta_hh_api.h"
     38 #include "bd.h"
     39 #include "btif_storage.h"
     40 
     41 #include "btif_common.h"
     42 #include "btif_util.h"
     43 #include "btif_hh.h"
     44 #include "gki.h"
     45 #include "l2c_api.h"
     46 
     47 
     48 #define BTIF_HH_APP_ID_MI       0x01
     49 #define BTIF_HH_APP_ID_KB       0x02
     50 
     51 #define COD_HID_KEYBOARD        0x0540
     52 #define COD_HID_POINTING        0x0580
     53 #define COD_HID_COMBO           0x05C0
     54 #define COD_HID_MAJOR           0x0500
     55 
     56 #define KEYSTATE_FILEPATH "/data/misc/bluedroid/bt_hh_ks" //keep this in sync with HID host jni
     57 
     58 #define HID_REPORT_CAPSLOCK   0x39
     59 #define HID_REPORT_NUMLOCK    0x53
     60 #define HID_REPORT_SCROLLLOCK 0x47
     61 
     62 //For Apple Magic Mouse
     63 #define MAGICMOUSE_VENDOR_ID 0x05ac
     64 #define MAGICMOUSE_PRODUCT_ID 0x030d
     65 
     66 #define LOGITECH_KB_MX5500_VENDOR_ID  0x046D
     67 #define LOGITECH_KB_MX5500_PRODUCT_ID 0xB30B
     68 
     69 extern const int BT_UID;
     70 extern const int BT_GID;
     71 static int btif_hh_prev_keyevents=0; //The previous key events
     72 static int btif_hh_keylockstates=0; //The current key state of each key
     73 
     74 #define BTIF_HH_ID_1        0
     75 #define BTIF_HH_DEV_DISCONNECTED 3
     76 
     77 #define BTIF_TIMEOUT_VUP_SECS   3
     78 
     79 
     80 #ifndef BTUI_HH_SECURITY
     81 #define BTUI_HH_SECURITY (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT)
     82 #endif
     83 
     84 #ifndef BTUI_HH_MOUSE_SECURITY
     85 #define BTUI_HH_MOUSE_SECURITY (BTA_SEC_NONE)
     86 #endif
     87 
     88 /* HH request events */
     89 typedef enum
     90 {
     91     BTIF_HH_CONNECT_REQ_EVT = 0,
     92     BTIF_HH_DISCONNECT_REQ_EVT,
     93     BTIF_HH_VUP_REQ_EVT
     94 } btif_hh_req_evt_t;
     95 
     96 
     97 /************************************************************************************
     98 **  Constants & Macros
     99 ************************************************************************************/
    100 #define BTIF_HH_SERVICES    (BTA_HID_SERVICE_MASK)
    101 
    102 
    103 
    104 /************************************************************************************
    105 **  Local type definitions
    106 ************************************************************************************/
    107 
    108 typedef struct hid_kb_list
    109 {
    110     UINT16 product_id;
    111     UINT16 version_id;
    112     char*  kb_name;
    113 } tHID_KB_LIST;
    114 
    115 /************************************************************************************
    116 **  Static variables
    117 ************************************************************************************/
    118 btif_hh_cb_t btif_hh_cb;
    119 
    120 static bthh_callbacks_t *bt_hh_callbacks = NULL;
    121 
    122 /* List of HID keyboards for which the NUMLOCK state needs to be
    123  * turned ON by default. Add devices to this list to apply the
    124  * NUMLOCK state toggle on fpr first connect.*/
    125 static tHID_KB_LIST hid_kb_numlock_on_list[] =
    126 {
    127     {LOGITECH_KB_MX5500_PRODUCT_ID,
    128     LOGITECH_KB_MX5500_VENDOR_ID,
    129     "Logitech MX5500 Keyboard"}
    130 };
    131 
    132 
    133 #define CHECK_BTHH_INIT() if (bt_hh_callbacks == NULL)\
    134     {\
    135         BTIF_TRACE_WARNING("BTHH: %s: BTHH not initialized", __FUNCTION__);\
    136         return BT_STATUS_NOT_READY;\
    137     }\
    138     else\
    139     {\
    140         BTIF_TRACE_EVENT("BTHH: %s", __FUNCTION__);\
    141     }
    142 
    143 
    144 
    145 /************************************************************************************
    146 **  Static functions
    147 ************************************************************************************/
    148 
    149 /************************************************************************************
    150 **  Externs
    151 ************************************************************************************/
    152 extern void bta_hh_co_destroy(int fd);
    153 extern void bta_hh_co_write(int fd, UINT8* rpt, UINT16 len);
    154 extern bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);
    155 extern void bta_hh_co_send_hid_info(btif_hh_device_t *p_dev, char *dev_name, UINT16 vendor_id,
    156                                     UINT16 product_id, UINT16 version, UINT8 ctry_code,
    157                                     int dscp_len, UINT8 *p_dscp);
    158 extern BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod);
    159 extern void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr);
    160 extern BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod);
    161 extern int  scru_ascii_2_hex(char *p_ascii, int len, UINT8 *p_hex);
    162 extern void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr);
    163 
    164 /*****************************************************************************
    165 **  Local Function prototypes
    166 *****************************************************************************/
    167 static void set_keylockstate(int keymask, BOOLEAN isSet);
    168 static void toggle_os_keylockstates(int fd, int changedkeystates);
    169 static void sync_lockstate_on_connect(btif_hh_device_t *p_dev);
    170 //static void hh_update_keyboard_lockstates(btif_hh_device_t *p_dev);
    171 void btif_hh_tmr_hdlr(TIMER_LIST_ENT *tle);
    172 
    173 
    174 /************************************************************************************
    175 **  Functions
    176 ************************************************************************************/
    177 
    178 static int get_keylockstates()
    179 {
    180     return btif_hh_keylockstates;
    181 }
    182 
    183 static void set_keylockstate(int keymask, BOOLEAN isSet)
    184 {
    185     if(isSet)
    186         btif_hh_keylockstates |= keymask;
    187 }
    188 
    189 /*******************************************************************************
    190 **
    191 ** Function         toggle_os_keylockstates
    192 **
    193 ** Description      Function to toggle the keyboard lock states managed by the linux.
    194 **                  This function is used in by two call paths
    195 **                  (1) if the lock state change occurred from an onscreen keyboard,
    196 **                  this function is called to update the lock state maintained
    197                     for the HID keyboard(s)
    198 **                  (2) if a HID keyboard is disconnected and reconnected,
    199 **                  this function is called to update the lock state maintained
    200                     for the HID keyboard(s)
    201 ** Returns          void
    202 *******************************************************************************/
    203 
    204 static void toggle_os_keylockstates(int fd, int changedlockstates)
    205 {
    206     BTIF_TRACE_EVENT("%s: fd = %d, changedlockstates = 0x%x",
    207         __FUNCTION__, fd, changedlockstates);
    208     UINT8 hidreport[9];
    209     int reportIndex;
    210     memset(hidreport,0,9);
    211     hidreport[0]=1;
    212     reportIndex=4;
    213 
    214     if (changedlockstates & BTIF_HH_KEYSTATE_MASK_CAPSLOCK) {
    215         BTIF_TRACE_DEBUG("%s Setting CAPSLOCK", __FUNCTION__);
    216         hidreport[reportIndex++] = (UINT8)HID_REPORT_CAPSLOCK;
    217     }
    218 
    219     if (changedlockstates & BTIF_HH_KEYSTATE_MASK_NUMLOCK)  {
    220         BTIF_TRACE_DEBUG("%s Setting NUMLOCK", __FUNCTION__);
    221         hidreport[reportIndex++] = (UINT8)HID_REPORT_NUMLOCK;
    222     }
    223 
    224     if (changedlockstates & BTIF_HH_KEYSTATE_MASK_SCROLLLOCK) {
    225         BTIF_TRACE_DEBUG("%s Setting SCROLLLOCK", __FUNCTION__);
    226         hidreport[reportIndex++] = (UINT8) HID_REPORT_SCROLLLOCK;
    227     }
    228 
    229      BTIF_TRACE_DEBUG("Writing hidreport #1 to os: "\
    230         "%s:  %x %x %x", __FUNCTION__,
    231          hidreport[0], hidreport[1], hidreport[2]);
    232     BTIF_TRACE_DEBUG("%s:  %x %x %x", __FUNCTION__,
    233          hidreport[3], hidreport[4], hidreport[5]);
    234     BTIF_TRACE_DEBUG("%s:  %x %x %x", __FUNCTION__,
    235          hidreport[6], hidreport[7], hidreport[8]);
    236     bta_hh_co_write(fd , hidreport, sizeof(hidreport));
    237     usleep(200000);
    238     memset(hidreport,0,9);
    239     hidreport[0]=1;
    240     BTIF_TRACE_DEBUG("Writing hidreport #2 to os: "\
    241        "%s:  %x %x %x", __FUNCTION__,
    242          hidreport[0], hidreport[1], hidreport[2]);
    243     BTIF_TRACE_DEBUG("%s:  %x %x %x", __FUNCTION__,
    244          hidreport[3], hidreport[4], hidreport[5]);
    245     BTIF_TRACE_DEBUG("%s:  %x %x %x ", __FUNCTION__,
    246          hidreport[6], hidreport[7], hidreport[8]);
    247     bta_hh_co_write(fd , hidreport, sizeof(hidreport));
    248 }
    249 
    250 /*******************************************************************************
    251 **
    252 ** Function         create_pbuf
    253 **
    254 ** Description      Helper function to create p_buf for send_data or set_report
    255 **
    256 *******************************************************************************/
    257 static BT_HDR *create_pbuf(UINT16 len, UINT8 *data)
    258 {
    259     BT_HDR* p_buf = GKI_getbuf((UINT16) (len + BTA_HH_MIN_OFFSET + sizeof(BT_HDR)));
    260 
    261     if (p_buf) {
    262         UINT8* pbuf_data;
    263 
    264         p_buf->len = len;
    265         p_buf->offset = BTA_HH_MIN_OFFSET;
    266 
    267         pbuf_data = (UINT8*) (p_buf + 1) + p_buf->offset;
    268         memcpy(pbuf_data, data, len);
    269     }
    270     return p_buf;
    271 }
    272 
    273 /*******************************************************************************
    274 **
    275 ** Function         update_keyboard_lockstates
    276 **
    277 ** Description      Sends a report to the keyboard to set the lock states of keys
    278 **
    279 *******************************************************************************/
    280 static void update_keyboard_lockstates(btif_hh_device_t *p_dev)
    281 {
    282     UINT8 len = 2;  /* reportid + 1 byte report*/
    283     BD_ADDR* bda;
    284     BT_HDR* p_buf;
    285     UINT8 data[] = {0x01, /* report id */
    286                     btif_hh_keylockstates}; /* keystate */
    287 
    288     /* Set report for other keyboards */
    289     BTIF_TRACE_EVENT("%s: setting report on dev_handle %d to 0x%x",
    290          __FUNCTION__, p_dev->dev_handle, btif_hh_keylockstates);
    291 
    292     /* Get SetReport buffer */
    293     p_buf = create_pbuf(len, data);
    294     if (p_buf != NULL) {
    295         p_buf->layer_specific = BTA_HH_RPTT_OUTPUT;
    296         bda = (BD_ADDR*) (&p_dev->bd_addr);
    297         BTA_HhSendData(p_dev->dev_handle, *bda, p_buf);
    298     }
    299 }
    300 
    301 /*******************************************************************************
    302 **
    303 ** Function         sync_lockstate_on_connect
    304 **
    305 ** Description      Function to update the keyboard lock states managed by the OS
    306 **                  when a HID keyboard is connected or disconnected and reconnected
    307 ** Returns          void
    308 *******************************************************************************/
    309 static void sync_lockstate_on_connect(btif_hh_device_t *p_dev)
    310 {
    311     int keylockstates;
    312 
    313     BTIF_TRACE_EVENT("%s: Syncing keyboard lock states after "\
    314         "reconnect...",__FUNCTION__);
    315     /*If the device is connected, update keyboard state */
    316     update_keyboard_lockstates(p_dev);
    317 
    318     /*Check if the lockstate of caps,scroll,num is set.
    319      If so, send a report to the kernel
    320     so the lockstate is in sync */
    321     keylockstates = get_keylockstates();
    322     if (keylockstates)
    323     {
    324         BTIF_TRACE_DEBUG("%s: Sending hid report to kernel "\
    325             "indicating lock key state 0x%x",__FUNCTION__,
    326             keylockstates);
    327         usleep(200000);
    328         toggle_os_keylockstates(p_dev->fd, keylockstates);
    329     }
    330     else
    331     {
    332         BTIF_TRACE_DEBUG("%s: NOT sending hid report to kernel "\
    333             "indicating lock key state 0x%x",__FUNCTION__,
    334             keylockstates);
    335     }
    336 }
    337 
    338 /*******************************************************************************
    339 **
    340 ** Function         btif_hh_find_connected_dev_by_handle
    341 **
    342 ** Description      Return the connected device pointer of the specified device handle
    343 **
    344 ** Returns          Device entry pointer in the device table
    345 *******************************************************************************/
    346 btif_hh_device_t *btif_hh_find_connected_dev_by_handle(UINT8 handle)
    347 {
    348     UINT32 i;
    349     for (i = 0; i < BTIF_HH_MAX_HID; i++) {
    350         if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED &&
    351             btif_hh_cb.devices[i].dev_handle == handle)
    352         {
    353             return &btif_hh_cb.devices[i];
    354         }
    355     }
    356     return NULL;
    357 }
    358 
    359 /*******************************************************************************
    360 **
    361 ** Function         btif_hh_find_dev_by_bda
    362 **
    363 ** Description      Return the device pointer of the specified bt_bdaddr_t.
    364 **
    365 ** Returns          Device entry pointer in the device table
    366 *******************************************************************************/
    367 static btif_hh_device_t *btif_hh_find_dev_by_bda(bt_bdaddr_t *bd_addr)
    368 {
    369     UINT32 i;
    370     for (i = 0; i < BTIF_HH_MAX_HID; i++) {
    371         if (btif_hh_cb.devices[i].dev_status != BTHH_CONN_STATE_UNKNOWN &&
    372             memcmp(&(btif_hh_cb.devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0)
    373         {
    374             return &btif_hh_cb.devices[i];
    375         }
    376     }
    377     return NULL;
    378 }
    379 
    380 /*******************************************************************************
    381 **
    382 ** Function         btif_hh_find_connected_dev_by_bda
    383 **
    384 ** Description      Return the connected device pointer of the specified bt_bdaddr_t.
    385 **
    386 ** Returns          Device entry pointer in the device table
    387 *******************************************************************************/
    388 static btif_hh_device_t *btif_hh_find_connected_dev_by_bda(bt_bdaddr_t *bd_addr)
    389 {
    390     UINT32 i;
    391     for (i = 0; i < BTIF_HH_MAX_HID; i++) {
    392         if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED &&
    393             memcmp(&(btif_hh_cb.devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0)
    394         {
    395             return &btif_hh_cb.devices[i];
    396         }
    397     }
    398     return NULL;
    399 }
    400 
    401 /*******************************************************************************
    402 **
    403 ** Function      btif_hh_stop_vup_timer
    404 **
    405 ** Description  stop vitual unplug timer
    406 **
    407 ** Returns      void
    408 *******************************************************************************/
    409 void btif_hh_stop_vup_timer(bt_bdaddr_t *bd_addr)
    410 {
    411     btif_hh_device_t *p_dev  = btif_hh_find_connected_dev_by_bda(bd_addr);
    412     if(p_dev != NULL)
    413     {
    414         if (p_dev->vup_timer_active)
    415         {
    416             BTIF_TRACE_DEBUG("stop VUP timer ");
    417             btu_stop_timer(&p_dev->vup_timer);
    418         }
    419         p_dev->vup_timer_active = FALSE;
    420     }
    421 }
    422 /*******************************************************************************
    423 **
    424 ** Function      btif_hh_start_vup_timer
    425 **
    426 ** Description  start virtual unplug timer
    427 **
    428 ** Returns      void
    429 *******************************************************************************/
    430 void btif_hh_start_vup_timer(bt_bdaddr_t *bd_addr)
    431 {
    432     btif_hh_device_t *p_dev  = btif_hh_find_connected_dev_by_bda(bd_addr);
    433 
    434     if (p_dev->vup_timer_active == FALSE)
    435     {
    436         BTIF_TRACE_DEBUG("Start VUP timer ");
    437         memset(&p_dev->vup_timer, 0, sizeof(TIMER_LIST_ENT));
    438         p_dev->vup_timer.param = (UINT32)btif_hh_tmr_hdlr;
    439         btu_start_timer(&p_dev->vup_timer, BTU_TTYPE_USER_FUNC,
    440                         BTIF_TIMEOUT_VUP_SECS);
    441     }
    442     else
    443     {
    444         BTIF_TRACE_DEBUG("Restart VUP timer ");
    445         btu_stop_timer(&p_dev->vup_timer);
    446         btu_start_timer(&p_dev->vup_timer, BTU_TTYPE_USER_FUNC,
    447                         BTIF_TIMEOUT_VUP_SECS);
    448     }
    449         p_dev->vup_timer_active = TRUE;
    450 
    451 }
    452 
    453 /*******************************************************************************
    454 **
    455 ** Function         btif_hh_add_added_dev
    456 **
    457 ** Description      Add a new device to the added device list.
    458 **
    459 ** Returns          TRUE if add successfully, otherwise FALSE.
    460 *******************************************************************************/
    461 BOOLEAN btif_hh_add_added_dev(bt_bdaddr_t bda, tBTA_HH_ATTR_MASK attr_mask)
    462 {
    463     int i;
    464     for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
    465         if (memcmp(&(btif_hh_cb.added_devices[i].bd_addr), &bda, BD_ADDR_LEN) == 0) {
    466             BTIF_TRACE_WARNING(" Device %02X:%02X:%02X:%02X:%02X:%02X already added",
    467                   bda.address[0], bda.address[1], bda.address[2], bda.address[3], bda.address[4], bda.address[5]);
    468             return FALSE;
    469         }
    470     }
    471     for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
    472         if (btif_hh_cb.added_devices[i].bd_addr.address[0] == 0 &&
    473             btif_hh_cb.added_devices[i].bd_addr.address[1] == 0 &&
    474             btif_hh_cb.added_devices[i].bd_addr.address[2] == 0 &&
    475             btif_hh_cb.added_devices[i].bd_addr.address[3] == 0 &&
    476             btif_hh_cb.added_devices[i].bd_addr.address[4] == 0 &&
    477             btif_hh_cb.added_devices[i].bd_addr.address[5] == 0)
    478         {
    479             BTIF_TRACE_WARNING(" Added device %02X:%02X:%02X:%02X:%02X:%02X",
    480                   bda.address[0], bda.address[1], bda.address[2], bda.address[3], bda.address[4], bda.address[5]);
    481             memcpy(&(btif_hh_cb.added_devices[i].bd_addr), &bda, BD_ADDR_LEN);
    482             btif_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE;
    483             btif_hh_cb.added_devices[i].attr_mask  = attr_mask;
    484             return TRUE;
    485         }
    486     }
    487 
    488     BTIF_TRACE_WARNING("%s: Error, out of space to add device",__FUNCTION__);
    489     return FALSE;
    490 }
    491 
    492 /*******************************************************************************
    493  **
    494  ** Function         btif_hh_remove_device
    495  **
    496  ** Description      Remove an added device from the stack.
    497  **
    498  ** Returns          void
    499  *******************************************************************************/
    500 void btif_hh_remove_device(bt_bdaddr_t bd_addr)
    501 {
    502     int                    i;
    503     btif_hh_device_t       *p_dev;
    504     btif_hh_added_device_t *p_added_dev;
    505 
    506     ALOGI("%s: bda = %02x:%02x:%02x:%02x:%02x:%02x", __FUNCTION__,
    507          bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]);
    508 
    509     for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
    510         p_added_dev = &btif_hh_cb.added_devices[i];
    511         if (memcmp(&(p_added_dev->bd_addr),&bd_addr, 6) == 0) {
    512             BTA_HhRemoveDev(p_added_dev->dev_handle);
    513             btif_storage_remove_hid_info(&(p_added_dev->bd_addr));
    514             memset(&(p_added_dev->bd_addr), 0, 6);
    515             p_added_dev->dev_handle = BTA_HH_INVALID_HANDLE;
    516             break;
    517         }
    518     }
    519 
    520     p_dev = btif_hh_find_dev_by_bda(&bd_addr);
    521     if (p_dev == NULL) {
    522         BTIF_TRACE_WARNING(" Oops, can't find device [%02x:%02x:%02x:%02x:%02x:%02x]",
    523              bd_addr.address[0], bd_addr.address[1], bd_addr.address[2], bd_addr.address[3], bd_addr.address[4], bd_addr.address[5]);
    524         return;
    525     }
    526 
    527     /* need to notify up-layer device is disconnected to avoid state out of sync with up-layer */
    528     HAL_CBACK(bt_hh_callbacks, connection_state_cb, &(p_dev->bd_addr), BTHH_CONN_STATE_DISCONNECTED);
    529 
    530     p_dev->dev_status = BTHH_CONN_STATE_UNKNOWN;
    531     p_dev->dev_handle = BTA_HH_INVALID_HANDLE;
    532     if (btif_hh_cb.device_num > 0) {
    533         btif_hh_cb.device_num--;
    534     }
    535     else {
    536         BTIF_TRACE_WARNING("%s: device_num = 0", __FUNCTION__);
    537     }
    538 
    539     p_dev->hh_keep_polling = 0;
    540     p_dev->hh_poll_thread_id = -1;
    541     BTIF_TRACE_DEBUG("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
    542     if (p_dev->fd >= 0) {
    543         bta_hh_co_destroy(p_dev->fd);
    544         p_dev->fd = -1;
    545     }
    546 }
    547 
    548 
    549 BOOLEAN btif_hh_copy_hid_info(tBTA_HH_DEV_DSCP_INFO* dest , tBTA_HH_DEV_DSCP_INFO* src)
    550 {
    551     dest->descriptor.dl_len = 0;
    552     if (src->descriptor.dl_len >0)
    553     {
    554         dest->descriptor.dsc_list = (UINT8 *) GKI_getbuf(src->descriptor.dl_len);
    555         if (dest->descriptor.dsc_list == NULL)
    556         {
    557             BTIF_TRACE_WARNING("%s: Failed to allocate DSCP for CB", __FUNCTION__);
    558             return FALSE;
    559         }
    560     }
    561     memcpy(dest->descriptor.dsc_list, src->descriptor.dsc_list, src->descriptor.dl_len);
    562     dest->descriptor.dl_len = src->descriptor.dl_len;
    563     dest->vendor_id  = src->vendor_id;
    564     dest->product_id = src->product_id;
    565     dest->version    = src->version;
    566     dest->ctry_code  = src->ctry_code;
    567     dest->ssr_max_latency = src->ssr_max_latency;
    568     dest->ssr_min_tout = src->ssr_min_tout;
    569     return TRUE;
    570 }
    571 
    572 
    573 /*******************************************************************************
    574 **
    575 ** Function         btif_hh_virtual_unplug
    576 **
    577 ** Description      Virtual unplug initiated from the BTIF thread context
    578 **                  Special handling for HID mouse-
    579 **
    580 ** Returns          void
    581 **
    582 *******************************************************************************/
    583 
    584 bt_status_t btif_hh_virtual_unplug(bt_bdaddr_t *bd_addr)
    585 {
    586     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
    587     btif_hh_device_t *p_dev;
    588     char bd_str[18];
    589     sprintf(bd_str, "%02X:%02X:%02X:%02X:%02X:%02X",
    590             bd_addr->address[0],  bd_addr->address[1],  bd_addr->address[2],  bd_addr->address[3],
    591             bd_addr->address[4], bd_addr->address[5]);
    592     p_dev = btif_hh_find_dev_by_bda(bd_addr);
    593     if ((p_dev != NULL) && (p_dev->dev_status == BTHH_CONN_STATE_CONNECTED)
    594         && (p_dev->attr_mask & HID_VIRTUAL_CABLE))
    595     {
    596         BTIF_TRACE_DEBUG("%s Sending BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG", __FUNCTION__);
    597         /* start the timer */
    598         btif_hh_start_vup_timer(bd_addr);
    599         p_dev->local_vup = TRUE;
    600         BTA_HhSendCtrl(p_dev->dev_handle, BTA_HH_CTRL_VIRTUAL_CABLE_UNPLUG);
    601         return BT_STATUS_SUCCESS;
    602     }
    603     else
    604     {
    605         BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __FUNCTION__, bd_str);
    606         return BT_STATUS_FAIL;
    607     }
    608 }
    609 
    610 /*******************************************************************************
    611 **
    612 ** Function         btif_hh_connect
    613 **
    614 ** Description      connection initiated from the BTIF thread context
    615 **
    616 ** Returns          int status
    617 **
    618 *******************************************************************************/
    619 
    620 bt_status_t btif_hh_connect(bt_bdaddr_t *bd_addr)
    621 {
    622     btif_hh_device_t *dev;
    623     btif_hh_added_device_t *added_dev = NULL;
    624     char bda_str[20];
    625     int i;
    626     BD_ADDR *bda = (BD_ADDR*)bd_addr;
    627     tBTA_HH_CONN conn;
    628     CHECK_BTHH_INIT();
    629     dev = btif_hh_find_dev_by_bda(bd_addr);
    630     BTIF_TRACE_DEBUG("Connect _hh");
    631     sprintf(bda_str, "%02X:%02X:%02X:%02X:%02X:%02X",
    632             (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
    633     if (dev == NULL && btif_hh_cb.device_num >= BTIF_HH_MAX_HID) {
    634         // No space for more HID device now.
    635          BTIF_TRACE_WARNING("%s: Error, exceeded the maximum supported HID device number %d",
    636              __FUNCTION__, BTIF_HH_MAX_HID);
    637         return BT_STATUS_FAIL;
    638     }
    639 
    640     for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
    641         if (memcmp(&(btif_hh_cb.added_devices[i].bd_addr), bd_addr, BD_ADDR_LEN) == 0) {
    642             added_dev = &btif_hh_cb.added_devices[i];
    643              BTIF_TRACE_WARNING("%s: Device %s already added, attr_mask = 0x%x",
    644                  __FUNCTION__, bda_str, added_dev->attr_mask);
    645         }
    646     }
    647 
    648     if (added_dev != NULL) {
    649         if (added_dev->dev_handle == BTA_HH_INVALID_HANDLE) {
    650             // No space for more HID device now.
    651             BTIF_TRACE_ERROR("%s: Error, device %s added but addition failed", __FUNCTION__, bda_str);
    652             memset(&(added_dev->bd_addr), 0, 6);
    653             added_dev->dev_handle = BTA_HH_INVALID_HANDLE;
    654             return BT_STATUS_FAIL;
    655         }
    656     }
    657 
    658     if (added_dev == NULL ||
    659         (added_dev->attr_mask & HID_NORMALLY_CONNECTABLE) != 0 ||
    660         (added_dev->attr_mask & HID_RECONN_INIT) == 0)
    661     {
    662         tBTA_SEC sec_mask = BTUI_HH_SECURITY;
    663         btif_hh_cb.status = BTIF_HH_DEV_CONNECTING;
    664         BD_ADDR *bda = (BD_ADDR*)bd_addr;
    665         BTA_HhOpen(*bda, BTA_HH_PROTO_RPT_MODE, sec_mask);
    666     }
    667     else
    668     {
    669         // This device shall be connected from the host side.
    670         BTIF_TRACE_ERROR("%s: Error, device %s can only be reconnected from device side",
    671              __FUNCTION__, bda_str);
    672         return BT_STATUS_FAIL;
    673     }
    674 
    675     HAL_CBACK(bt_hh_callbacks, connection_state_cb, bd_addr, BTHH_CONN_STATE_CONNECTING);
    676     return BT_STATUS_SUCCESS;
    677 }
    678 
    679 /*******************************************************************************
    680 **
    681 ** Function         btif_hh_disconnect
    682 **
    683 ** Description      disconnection initiated from the BTIF thread context
    684 **
    685 ** Returns          void
    686 **
    687 *******************************************************************************/
    688 
    689 void btif_hh_disconnect(bt_bdaddr_t *bd_addr)
    690 {
    691     BD_ADDR *bda = (BD_ADDR*)bd_addr;
    692     btif_hh_device_t *p_dev;
    693     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
    694     if (p_dev != NULL)
    695     {
    696         BTA_HhClose(p_dev->dev_handle);
    697     }
    698     else
    699         BTIF_TRACE_DEBUG("%s-- Error: device not connected:",__FUNCTION__);
    700 }
    701 
    702 /*******************************************************************************
    703 **
    704 ** Function         btif_btif_hh_setreport
    705 **
    706 ** Description      setreport initiated from the BTIF thread context
    707 **
    708 ** Returns          void
    709 **
    710 *******************************************************************************/
    711 void btif_hh_setreport(btif_hh_device_t *p_dev, bthh_report_type_t r_type, UINT16 size,
    712                             UINT8* report)
    713 {
    714     BT_HDR* p_buf = create_pbuf(size, report);
    715     if (p_buf == NULL) {
    716         APPL_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, size = %d", __FUNCTION__, size);
    717         return;
    718     }
    719     BTA_HhSetReport(p_dev->dev_handle, r_type, p_buf);
    720 }
    721 
    722 /*****************************************************************************
    723 **   Section name (Group of functions)
    724 *****************************************************************************/
    725 
    726 /*****************************************************************************
    727 **
    728 **   btif hh api functions (no context switch)
    729 **
    730 *****************************************************************************/
    731 
    732 
    733 /*******************************************************************************
    734 **
    735 ** Function         btif_hh_upstreams_evt
    736 **
    737 ** Description      Executes HH UPSTREAMS events in btif context
    738 **
    739 ** Returns          void
    740 **
    741 *******************************************************************************/
    742 static void btif_hh_upstreams_evt(UINT16 event, char* p_param)
    743 {
    744     tBTA_HH *p_data = (tBTA_HH *)p_param;
    745     bdstr_t bdstr;
    746     btif_hh_device_t *p_dev = NULL;
    747     int i;
    748     int len, tmplen;
    749 
    750     BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_hh_event(event));
    751 
    752     switch (event)
    753     {
    754         case BTA_HH_ENABLE_EVT:
    755             BTIF_TRACE_DEBUG("%s: BTA_HH_ENABLE_EVT: status =%d",__FUNCTION__, p_data->status);
    756             if (p_data->status == BTA_HH_OK) {
    757                 btif_hh_cb.status = BTIF_HH_ENABLED;
    758                 BTIF_TRACE_DEBUG("%s--Loading added devices",__FUNCTION__);
    759                 /* Add hid descriptors for already bonded hid devices*/
    760                 btif_storage_load_bonded_hid_info();
    761             }
    762             else {
    763                 btif_hh_cb.status = BTIF_HH_DISABLED;
    764                 BTIF_TRACE_WARNING("BTA_HH_ENABLE_EVT: Error, HH enabling failed, status = %d", p_data->status);
    765             }
    766             break;
    767 
    768         case BTA_HH_DISABLE_EVT:
    769             btif_hh_cb.status = BTIF_HH_DISABLED;
    770             if (p_data->status == BTA_HH_OK) {
    771                 int i;
    772                 //Clear the control block
    773                 memset(&btif_hh_cb, 0, sizeof(btif_hh_cb));
    774                 for (i = 0; i < BTIF_HH_MAX_HID; i++){
    775                     btif_hh_cb.devices[i].dev_status = BTHH_CONN_STATE_UNKNOWN;
    776                 }
    777             }
    778             else
    779                 BTIF_TRACE_WARNING("BTA_HH_DISABLE_EVT: Error, HH disabling failed, status = %d", p_data->status);
    780             break;
    781 
    782         case BTA_HH_OPEN_EVT:
    783             BTIF_TRACE_WARNING("%s: BTA_HH_OPN_EVT: handle=%d, status =%d",__FUNCTION__, p_data->conn.handle, p_data->conn.status);
    784             if (p_data->conn.status == BTA_HH_OK) {
    785                 p_dev = btif_hh_find_connected_dev_by_handle(p_data->conn.handle);
    786                 if (p_dev == NULL) {
    787                     BTIF_TRACE_WARNING("BTA_HH_OPEN_EVT: Error, cannot find device with handle %d", p_data->conn.handle);
    788                     btif_hh_cb.status = BTIF_HH_DEV_DISCONNECTED;
    789                     // The connect request must come from device side and exceeded the connected
    790                                    // HID device number.
    791                     BTA_HhClose(p_data->conn.handle);
    792                     HAL_CBACK(bt_hh_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,BTHH_CONN_STATE_DISCONNECTED);
    793                 }
    794                 else if (p_dev->fd < 0) {
    795                     BTIF_TRACE_WARNING("BTA_HH_OPEN_EVT: Error, failed to find the uhid driver...");
    796                     memcpy(&(p_dev->bd_addr), p_data->conn.bda, BD_ADDR_LEN);
    797                     //remove the connection  and then try again to reconnect from the mouse side to recover
    798                     btif_hh_cb.status = BTIF_HH_DEV_DISCONNECTED;
    799                     BTA_HhClose(p_data->conn.handle);
    800                 }
    801                 else {
    802                     BTIF_TRACE_WARNING("BTA_HH_OPEN_EVT: Found device...Getting dscp info for handle ... %d",p_data->conn.handle);
    803                     memcpy(&(p_dev->bd_addr), p_data->conn.bda, BD_ADDR_LEN);
    804                     btif_hh_cb.status = BTIF_HH_DEV_CONNECTED;
    805                     // Send set_idle if the peer_device is a keyboard
    806                     if (check_cod((bt_bdaddr_t*)p_data->conn.bda, COD_HID_KEYBOARD )||
    807                                 check_cod((bt_bdaddr_t*)p_data->conn.bda, COD_HID_COMBO))
    808                         BTA_HhSetIdle(p_data->conn.handle, 0);
    809                     btif_hh_cb.p_curr_dev = btif_hh_find_connected_dev_by_handle(p_data->conn.handle);
    810                     BTA_HhGetDscpInfo(p_data->conn.handle);
    811                     p_dev->dev_status = BTHH_CONN_STATE_CONNECTED;
    812                     HAL_CBACK(bt_hh_callbacks, connection_state_cb,&(p_dev->bd_addr), p_dev->dev_status);
    813                 }
    814             }
    815             else {
    816                 bt_bdaddr_t *bdaddr = (bt_bdaddr_t*)p_data->conn.bda;
    817                 btif_dm_hh_open_failed(bdaddr);
    818                 HAL_CBACK(bt_hh_callbacks, connection_state_cb, (bt_bdaddr_t*) &p_data->conn.bda,BTHH_CONN_STATE_DISCONNECTED);
    819                 btif_hh_cb.status = BTIF_HH_DEV_DISCONNECTED;
    820             }
    821             break;
    822         case BTA_HH_CLOSE_EVT:
    823             BTIF_TRACE_DEBUG("BTA_HH_CLOSE_EVT: status = %d, handle = %d",
    824             p_data->dev_status.status, p_data->dev_status.handle);
    825             p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
    826             if (p_dev != NULL) {
    827                 BTIF_TRACE_DEBUG("%s: uhid fd = %d", __FUNCTION__, p_dev->fd);
    828                 if(p_dev->vup_timer_active)
    829                 {
    830                     btif_hh_stop_vup_timer(&(p_dev->bd_addr));
    831                 }
    832                 btif_hh_cb.status = BTIF_HH_DEV_DISCONNECTED;
    833                 p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED;
    834                 HAL_CBACK(bt_hh_callbacks, connection_state_cb,&(p_dev->bd_addr), p_dev->dev_status);
    835                 BTIF_TRACE_DEBUG("%s: Closing uhid fd = %d", __FUNCTION__, p_dev->fd);
    836                 bta_hh_co_destroy(p_dev->fd);
    837                 p_dev->fd = -1;
    838             }
    839             else {
    840                 BTIF_TRACE_WARNING("Error: cannot find device with handle %d", p_data->dev_status.handle);
    841             }
    842             break;
    843         case BTA_HH_GET_RPT_EVT: {
    844             BT_HDR *hdr = p_data->hs_data.rsp_data.p_rpt_data;
    845             UINT8 *data = NULL;
    846             UINT16 len = 0;
    847 
    848             BTIF_TRACE_DEBUG("BTA_HH_GET_RPT_EVT: status = %d, handle = %d",
    849                  p_data->hs_data.status, p_data->hs_data.handle);
    850             p_dev = btif_hh_find_connected_dev_by_handle(p_data->hs_data.handle);
    851             if (p_dev) {
    852                 /* p_rpt_data is NULL in HANDSHAKE response case */
    853                 if (hdr) {
    854                     data = (UINT8 *)(hdr + 1) + hdr->offset;
    855                     len = hdr->len;
    856                     HAL_CBACK(bt_hh_callbacks, get_report_cb,
    857                               (bt_bdaddr_t*) &(p_dev->bd_addr),
    858                               (bthh_status_t) p_data->hs_data.status, data, len);
    859                 } else {
    860                     HAL_CBACK(bt_hh_callbacks, handshake_cb,
    861                               (bt_bdaddr_t*) &(p_dev->bd_addr),
    862                               (bthh_status_t) p_data->hs_data.status);
    863                 }
    864             } else {
    865                 BTIF_TRACE_WARNING("Error: cannot find device with handle %d", p_data->hs_data.handle);
    866             }
    867             break;
    868         }
    869         case BTA_HH_SET_RPT_EVT:
    870             BTIF_TRACE_DEBUG("BTA_HH_SET_RPT_EVT: status = %d, handle = %d",
    871             p_data->dev_status.status, p_data->dev_status.handle);
    872             p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
    873             if (p_dev != NULL) {
    874                 HAL_CBACK(bt_hh_callbacks, handshake_cb,
    875                           (bt_bdaddr_t*) &(p_dev->bd_addr),
    876                           (bthh_status_t) p_data->hs_data.status);
    877             }
    878             break;
    879 
    880         case BTA_HH_GET_PROTO_EVT:
    881             p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
    882             BTIF_TRACE_WARNING("BTA_HH_GET_PROTO_EVT: status = %d, handle = %d, proto = [%d], %s",
    883                  p_data->hs_data.status, p_data->hs_data.handle,
    884                  p_data->hs_data.rsp_data.proto_mode,
    885                  (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_RPT_MODE) ? "Report Mode" :
    886                  (p_data->hs_data.rsp_data.proto_mode == BTA_HH_PROTO_BOOT_MODE) ? "Boot Mode" : "Unsupported");
    887             if (p_data->hs_data.rsp_data.proto_mode != BTA_HH_PROTO_UNKNOWN) {
    888                 HAL_CBACK(bt_hh_callbacks, protocol_mode_cb,
    889                           (bt_bdaddr_t*) &(p_dev->bd_addr),
    890                           (bthh_status_t)p_data->hs_data.status,
    891                           (bthh_protocol_mode_t) p_data->hs_data.rsp_data.proto_mode);
    892             } else {
    893                 HAL_CBACK(bt_hh_callbacks, handshake_cb,
    894                           (bt_bdaddr_t*) &(p_dev->bd_addr),
    895                           (bthh_status_t)p_data->hs_data.status);
    896             }
    897             break;
    898 
    899         case BTA_HH_SET_PROTO_EVT:
    900             BTIF_TRACE_DEBUG("BTA_HH_SET_PROTO_EVT: status = %d, handle = %d",
    901                  p_data->dev_status.status, p_data->dev_status.handle);
    902             p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
    903             if (p_dev) {
    904                 HAL_CBACK(bt_hh_callbacks, handshake_cb,
    905                           (bt_bdaddr_t*)&(p_dev->bd_addr),
    906                           (bthh_status_t)p_data->hs_data.status);
    907             }
    908             break;
    909 
    910         case BTA_HH_GET_IDLE_EVT:
    911             BTIF_TRACE_DEBUG("BTA_HH_GET_IDLE_EVT: handle = %d, status = %d, rate = %d",
    912                  p_data->hs_data.handle, p_data->hs_data.status,
    913                  p_data->hs_data.rsp_data.idle_rate);
    914             break;
    915 
    916         case BTA_HH_SET_IDLE_EVT:
    917             BTIF_TRACE_DEBUG("BTA_HH_SET_IDLE_EVT: status = %d, handle = %d",
    918             p_data->dev_status.status, p_data->dev_status.handle);
    919             break;
    920 
    921         case BTA_HH_GET_DSCP_EVT:
    922             len = p_data->dscp_info.descriptor.dl_len;
    923             BTIF_TRACE_DEBUG("BTA_HH_GET_DSCP_EVT: len = %d", len);
    924             p_dev = btif_hh_cb.p_curr_dev;
    925             if (p_dev == NULL) {
    926                 BTIF_TRACE_ERROR("BTA_HH_GET_DSCP_EVT: No HID device is currently connected");
    927                 return;
    928             }
    929             if (p_dev->fd < 0) {
    930                 ALOGE("BTA_HH_GET_DSCP_EVT: Error, failed to find the uhid driver...");
    931                 return;
    932             }
    933             {
    934                 char *cached_name = NULL;
    935                 bt_bdname_t bdname;
    936                 bt_property_t prop_name;
    937                 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
    938                                            sizeof(bt_bdname_t), &bdname);
    939                 if (btif_storage_get_remote_device_property(
    940                     &p_dev->bd_addr, &prop_name) == BT_STATUS_SUCCESS)
    941                 {
    942                     cached_name = (char *)bdname.name;
    943                 }
    944                 else
    945                 {
    946                     cached_name = "Bluetooth HID";
    947                 }
    948 
    949                 BTIF_TRACE_WARNING("%s: name = %s", __FUNCTION__, cached_name);
    950                 bta_hh_co_send_hid_info(p_dev, cached_name,
    951                     p_data->dscp_info.vendor_id, p_data->dscp_info.product_id,
    952                     p_data->dscp_info.version,   p_data->dscp_info.ctry_code,
    953                     len, p_data->dscp_info.descriptor.dsc_list);
    954                 if (btif_hh_add_added_dev(p_dev->bd_addr, p_dev->attr_mask)) {
    955                     BD_ADDR bda;
    956                     bdcpy(bda, p_dev->bd_addr.address);
    957                     tBTA_HH_DEV_DSCP_INFO dscp_info;
    958                     bt_status_t ret;
    959                     bdcpy(bda, p_dev->bd_addr.address);
    960                     btif_hh_copy_hid_info(&dscp_info, &p_data->dscp_info);
    961                     BTIF_TRACE_DEBUG("BTA_HH_GET_DSCP_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x",
    962                               p_dev->bd_addr.address[0], p_dev->bd_addr.address[1],
    963                               p_dev->bd_addr.address[2],p_dev->bd_addr.address[3],
    964                               p_dev->bd_addr.address[4], p_dev->bd_addr.address[5]);
    965                     BTA_HhAddDev(bda, p_dev->attr_mask,p_dev->sub_class,p_dev->app_id, dscp_info);
    966                     // write hid info to nvram
    967                     ret = btif_storage_add_hid_device_info(&(p_dev->bd_addr), p_dev->attr_mask,p_dev->sub_class,p_dev->app_id,
    968                                                         p_data->dscp_info.vendor_id, p_data->dscp_info.product_id,
    969                                                         p_data->dscp_info.version,   p_data->dscp_info.ctry_code,
    970                                                         p_data->dscp_info.ssr_max_latency, p_data->dscp_info.ssr_min_tout,
    971                                                         len, p_data->dscp_info.descriptor.dsc_list);
    972 
    973                     ASSERTC(ret == BT_STATUS_SUCCESS, "storing hid info failed", ret);
    974                     BTIF_TRACE_WARNING("BTA_HH_GET_DSCP_EVT: Called add device");
    975 
    976                     //Free buffer created for dscp_info;
    977                     if (dscp_info.descriptor.dl_len >0 && dscp_info.descriptor.dsc_list != NULL)
    978                     {
    979                       GKI_freebuf(dscp_info.descriptor.dsc_list);
    980                       dscp_info.descriptor.dsc_list = NULL;
    981                       dscp_info.descriptor.dl_len=0;
    982                     }
    983                 }
    984                 else {
    985                     //Device already added.
    986                     BTIF_TRACE_WARNING("%s: Device already added ",__FUNCTION__);
    987                 }
    988                 /*Sync HID Keyboard lockstates */
    989                 tmplen = sizeof(hid_kb_numlock_on_list)
    990                             / sizeof(tHID_KB_LIST);
    991                 for(i = 0; i< tmplen; i++)
    992                 {
    993                     if(p_data->dscp_info.vendor_id
    994                         == hid_kb_numlock_on_list[i].version_id &&
    995                         p_data->dscp_info.product_id
    996                         == hid_kb_numlock_on_list[i].product_id)
    997                     {
    998                         BTIF_TRACE_DEBUG("%s() idx[%d] Enabling "\
    999                             "NUMLOCK for device :: %s", __FUNCTION__,
   1000                             i, hid_kb_numlock_on_list[i].kb_name);
   1001                         /* Enable NUMLOCK by default so that numeric
   1002                             keys work from first keyboard connect */
   1003                         set_keylockstate(BTIF_HH_KEYSTATE_MASK_NUMLOCK,
   1004                                         TRUE);
   1005                         sync_lockstate_on_connect(p_dev);
   1006                         /* End Sync HID Keyboard lockstates */
   1007                         break;
   1008                     }
   1009                 }
   1010             }
   1011             break;
   1012 
   1013         case BTA_HH_ADD_DEV_EVT:
   1014             BTIF_TRACE_WARNING("BTA_HH_ADD_DEV_EVT: status = %d, handle = %d",p_data->dev_info.status, p_data->dev_info.handle);
   1015             int i;
   1016             for (i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
   1017                 if (memcmp(btif_hh_cb.added_devices[i].bd_addr.address, p_data->dev_info.bda, 6) == 0) {
   1018                     if (p_data->dev_info.status == BTA_HH_OK) {
   1019                         btif_hh_cb.added_devices[i].dev_handle = p_data->dev_info.handle;
   1020                     }
   1021                     else {
   1022                         memset(btif_hh_cb.added_devices[i].bd_addr.address, 0, 6);
   1023                         btif_hh_cb.added_devices[i].dev_handle = BTA_HH_INVALID_HANDLE;
   1024                     }
   1025                     break;
   1026                 }
   1027             }
   1028             break;
   1029         case BTA_HH_RMV_DEV_EVT:
   1030                 BTIF_TRACE_DEBUG("BTA_HH_RMV_DEV_EVT: status = %d, handle = %d",
   1031                      p_data->dev_info.status, p_data->dev_info.handle);
   1032                 BTIF_TRACE_DEBUG("BTA_HH_RMV_DEV_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x",
   1033                      p_data->dev_info.bda[0], p_data->dev_info.bda[1], p_data->dev_info.bda[2],
   1034                      p_data->dev_info.bda[3], p_data->dev_info.bda[4], p_data->dev_info.bda[5]);
   1035                 break;
   1036 
   1037 
   1038         case BTA_HH_VC_UNPLUG_EVT:
   1039                 BTIF_TRACE_DEBUG("BTA_HH_VC_UNPLUG_EVT: status = %d, handle = %d",
   1040                      p_data->dev_status.status, p_data->dev_status.handle);
   1041                 p_dev = btif_hh_find_connected_dev_by_handle(p_data->dev_status.handle);
   1042                 btif_hh_cb.status = BTIF_HH_DEV_DISCONNECTED;
   1043                 if (p_dev != NULL) {
   1044                     BTIF_TRACE_DEBUG("BTA_HH_VC_UNPLUG_EVT:bda = %02x:%02x:%02x:%02x:%02x:%02x",
   1045                          p_dev->bd_addr.address[0], p_dev->bd_addr.address[1],
   1046                          p_dev->bd_addr.address[2],p_dev->bd_addr.address[3],
   1047                          p_dev->bd_addr.address[4], p_dev->bd_addr.address[5]);
   1048                     /* Stop the VUP timer */
   1049                     if(p_dev->vup_timer_active)
   1050                     {
   1051                         btif_hh_stop_vup_timer(&(p_dev->bd_addr));
   1052                     }
   1053                     p_dev->dev_status = BTHH_CONN_STATE_DISCONNECTED;
   1054                     BTIF_TRACE_DEBUG("%s---Sending connection state change", __FUNCTION__);
   1055                     HAL_CBACK(bt_hh_callbacks, connection_state_cb,&(p_dev->bd_addr), p_dev->dev_status);
   1056                     BTIF_TRACE_DEBUG("%s---Removing HID bond", __FUNCTION__);
   1057                     /* If it is locally initiated VUP or remote device has its major COD as
   1058                     Peripheral removed the bond.*/
   1059                     if (p_dev->local_vup  || check_cod_hid(&(p_dev->bd_addr), COD_HID_MAJOR))
   1060                     {
   1061                         p_dev->local_vup = FALSE;
   1062                         BTA_DmRemoveDevice((UINT8 *)p_dev->bd_addr.address);
   1063                     }
   1064                     else
   1065                         btif_hh_remove_device(p_dev->bd_addr);
   1066                     HAL_CBACK(bt_hh_callbacks, virtual_unplug_cb,&(p_dev->bd_addr),
   1067                                     p_data->dev_status.status);
   1068                 }
   1069                 break;
   1070 
   1071         case BTA_HH_API_ERR_EVT  :
   1072                 ALOGI("BTA_HH API_ERR");
   1073                 break;
   1074 
   1075 
   1076 
   1077             default:
   1078                 BTIF_TRACE_WARNING("%s: Unhandled event: %d", __FUNCTION__, event);
   1079                 break;
   1080         }
   1081 }
   1082 
   1083 /*******************************************************************************
   1084 **
   1085 ** Function         bte_hh_evt
   1086 **
   1087 ** Description      Switches context from BTE to BTIF for all HH events
   1088 **
   1089 ** Returns          void
   1090 **
   1091 *******************************************************************************/
   1092 
   1093 static void bte_hh_evt(tBTA_HH_EVT event, tBTA_HH *p_data)
   1094 {
   1095     bt_status_t status;
   1096     int param_len = 0;
   1097 
   1098     if (BTA_HH_ENABLE_EVT == event)
   1099         param_len = sizeof(tBTA_HH_STATUS);
   1100     else if (BTA_HH_OPEN_EVT == event)
   1101         param_len = sizeof(tBTA_HH_CONN);
   1102     else if (BTA_HH_DISABLE_EVT == event)
   1103         param_len = sizeof(tBTA_HH_STATUS);
   1104     else if (BTA_HH_CLOSE_EVT == event)
   1105         param_len = sizeof(tBTA_HH_CBDATA);
   1106     else if (BTA_HH_GET_DSCP_EVT == event)
   1107         param_len = sizeof(tBTA_HH_DEV_DSCP_INFO);
   1108     else if ((BTA_HH_GET_PROTO_EVT == event) || (BTA_HH_GET_RPT_EVT == event)|| (BTA_HH_GET_IDLE_EVT == event))
   1109         param_len = sizeof(tBTA_HH_HSDATA);
   1110     else if ((BTA_HH_SET_PROTO_EVT == event) || (BTA_HH_SET_RPT_EVT == event) || (BTA_HH_VC_UNPLUG_EVT == event) || (BTA_HH_SET_IDLE_EVT == event))
   1111         param_len = sizeof(tBTA_HH_CBDATA);
   1112     else if ((BTA_HH_ADD_DEV_EVT == event) || (BTA_HH_RMV_DEV_EVT == event) )
   1113         param_len = sizeof(tBTA_HH_DEV_INFO);
   1114     else if (BTA_HH_API_ERR_EVT == event)
   1115         param_len = 0;
   1116     /* switch context to btif task context (copy full union size for convenience) */
   1117     status = btif_transfer_context(btif_hh_upstreams_evt, (uint16_t)event, (void*)p_data, param_len, NULL);
   1118 
   1119     /* catch any failed context transfers */
   1120     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   1121 }
   1122 
   1123 /*******************************************************************************
   1124 **
   1125 ** Function         btif_hh_handle_evt
   1126 **
   1127 ** Description      Switches context for immediate callback
   1128 **
   1129 ** Returns          void
   1130 **
   1131 *******************************************************************************/
   1132 
   1133 static void btif_hh_handle_evt(UINT16 event, char *p_param)
   1134 {
   1135     bt_bdaddr_t *bd_addr = (bt_bdaddr_t*)p_param;
   1136     BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
   1137     int ret;
   1138     switch(event)
   1139     {
   1140         case BTIF_HH_CONNECT_REQ_EVT:
   1141         {
   1142             ret = btif_hh_connect(bd_addr);
   1143             if(ret == BT_STATUS_SUCCESS)
   1144             {
   1145                 HAL_CBACK(bt_hh_callbacks, connection_state_cb,bd_addr,BTHH_CONN_STATE_CONNECTING);
   1146             }
   1147             else
   1148                 HAL_CBACK(bt_hh_callbacks, connection_state_cb,bd_addr,BTHH_CONN_STATE_DISCONNECTED);
   1149         }
   1150         break;
   1151 
   1152         case BTIF_HH_DISCONNECT_REQ_EVT:
   1153         {
   1154             BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
   1155             btif_hh_disconnect(bd_addr);
   1156             HAL_CBACK(bt_hh_callbacks, connection_state_cb,bd_addr,BTHH_CONN_STATE_DISCONNECTING);
   1157         }
   1158         break;
   1159 
   1160         case BTIF_HH_VUP_REQ_EVT:
   1161         {
   1162             BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
   1163             ret = btif_hh_virtual_unplug(bd_addr);
   1164         }
   1165         break;
   1166 
   1167         default:
   1168         {
   1169             BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
   1170         }
   1171         break;
   1172     }
   1173 }
   1174 
   1175 /*******************************************************************************
   1176 **
   1177 ** Function      btif_hh_tmr_hdlr
   1178 **
   1179 ** Description   Process timer timeout
   1180 **
   1181 ** Returns      void
   1182 *******************************************************************************/
   1183 void btif_hh_tmr_hdlr(TIMER_LIST_ENT *tle)
   1184 {
   1185     btif_hh_device_t *p_dev;
   1186     UINT8               i,j;
   1187     tBTA_HH_EVT event;
   1188     tBTA_HH p_data;
   1189     int param_len = 0;
   1190     memset(&p_data, 0, sizeof(tBTA_HH));
   1191 
   1192     BTIF_TRACE_DEBUG("%s timer_in_use=%d",  __FUNCTION__, tle->in_use );
   1193 
   1194     for (i = 0; i < BTIF_HH_MAX_HID; i++) {
   1195         if (btif_hh_cb.devices[i].dev_status == BTHH_CONN_STATE_CONNECTED)
   1196         {
   1197 
   1198             p_dev = &btif_hh_cb.devices[i];
   1199 
   1200             if (p_dev->vup_timer_active)
   1201             {
   1202                 p_dev->vup_timer_active = FALSE;
   1203                 event = BTA_HH_VC_UNPLUG_EVT;
   1204                 p_data.dev_status.status = BTHH_ERR;
   1205                 p_data.dev_status.handle = p_dev->dev_handle;
   1206                 param_len = sizeof(tBTA_HH_CBDATA);
   1207 
   1208                 /* switch context to btif task context */
   1209                 btif_transfer_context(btif_hh_upstreams_evt, (uint16_t)event, (void*)&p_data,
   1210                             param_len, NULL);
   1211             }
   1212         }
   1213     }
   1214 }
   1215 
   1216 /*******************************************************************************
   1217 **
   1218 ** Function         btif_hh_init
   1219 **
   1220 ** Description     initializes the hh interface
   1221 **
   1222 ** Returns         bt_status_t
   1223 **
   1224 *******************************************************************************/
   1225 static bt_status_t init( bthh_callbacks_t* callbacks )
   1226 {
   1227     UINT32 i;
   1228     BTIF_TRACE_EVENT("%s", __FUNCTION__);
   1229 
   1230     bt_hh_callbacks = callbacks;
   1231     memset(&btif_hh_cb, 0, sizeof(btif_hh_cb));
   1232     for (i = 0; i < BTIF_HH_MAX_HID; i++){
   1233         btif_hh_cb.devices[i].dev_status = BTHH_CONN_STATE_UNKNOWN;
   1234     }
   1235     /* Invoke the enable service API to the core to set the appropriate service_id */
   1236     btif_enable_service(BTA_HID_SERVICE_ID);
   1237     return BT_STATUS_SUCCESS;
   1238 }
   1239 
   1240 /*******************************************************************************
   1241 **
   1242 ** Function        connect
   1243 **
   1244 ** Description     connect to hid device
   1245 **
   1246 ** Returns         bt_status_t
   1247 **
   1248 *******************************************************************************/
   1249 static bt_status_t connect( bt_bdaddr_t *bd_addr)
   1250 {
   1251     if(btif_hh_cb.status != BTIF_HH_DEV_CONNECTING)
   1252     {
   1253         btif_transfer_context(btif_hh_handle_evt, BTIF_HH_CONNECT_REQ_EVT,
   1254                                  (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
   1255         return BT_STATUS_SUCCESS;
   1256     }
   1257     else
   1258         return BT_STATUS_BUSY;
   1259 }
   1260 
   1261 /*******************************************************************************
   1262 **
   1263 ** Function         disconnect
   1264 **
   1265 ** Description      disconnect from hid device
   1266 **
   1267 ** Returns         bt_status_t
   1268 **
   1269 *******************************************************************************/
   1270 static bt_status_t disconnect( bt_bdaddr_t *bd_addr )
   1271 {
   1272     CHECK_BTHH_INIT();
   1273     btif_hh_device_t *p_dev;
   1274 
   1275     if (btif_hh_cb.status == BTIF_HH_DISABLED)
   1276     {
   1277         BTIF_TRACE_WARNING("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1278         return BT_STATUS_FAIL;
   1279     }
   1280     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
   1281     if (p_dev != NULL)
   1282     {
   1283         return btif_transfer_context(btif_hh_handle_evt, BTIF_HH_DISCONNECT_REQ_EVT,
   1284                      (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
   1285     }
   1286     else
   1287     {
   1288         BTIF_TRACE_WARNING("%s: Error, device  not opened.", __FUNCTION__);
   1289         return BT_STATUS_FAIL;
   1290     }
   1291 }
   1292 
   1293 /*******************************************************************************
   1294 **
   1295 ** Function         virtual_unplug
   1296 **
   1297 ** Description      Virtual UnPlug (VUP) the specified HID device.
   1298 **
   1299 ** Returns         bt_status_t
   1300 **
   1301 *******************************************************************************/
   1302 static bt_status_t virtual_unplug (bt_bdaddr_t *bd_addr)
   1303 {
   1304     CHECK_BTHH_INIT();
   1305     btif_hh_device_t *p_dev;
   1306     char bd_str[18];
   1307     sprintf(bd_str, "%02X:%02X:%02X:%02X:%02X:%02X",
   1308             bd_addr->address[0],  bd_addr->address[1],  bd_addr->address[2],  bd_addr->address[3],
   1309             bd_addr->address[4], bd_addr->address[5]);
   1310     if (btif_hh_cb.status == BTIF_HH_DISABLED)
   1311     {
   1312         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1313         return BT_STATUS_FAIL;
   1314     }
   1315     p_dev = btif_hh_find_dev_by_bda(bd_addr);
   1316     if (!p_dev)
   1317     {
   1318         BTIF_TRACE_ERROR("%s: Error, device %s not opened.", __FUNCTION__, bd_str);
   1319         return BT_STATUS_FAIL;
   1320     }
   1321     btif_transfer_context(btif_hh_handle_evt, BTIF_HH_VUP_REQ_EVT,
   1322                                  (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
   1323     return BT_STATUS_SUCCESS;
   1324 }
   1325 
   1326 
   1327 /*******************************************************************************
   1328 **
   1329 ** Function         set_info
   1330 **
   1331 ** Description      Set the HID device descriptor for the specified HID device.
   1332 **
   1333 ** Returns         bt_status_t
   1334 **
   1335 *******************************************************************************/
   1336 static bt_status_t set_info (bt_bdaddr_t *bd_addr, bthh_hid_info_t hid_info )
   1337 {
   1338     CHECK_BTHH_INIT();
   1339     tBTA_HH_DEV_DSCP_INFO dscp_info;
   1340     BD_ADDR* bda = (BD_ADDR*) bd_addr;
   1341 
   1342     BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X",
   1343          (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1344     BTIF_TRACE_DEBUG("%s: sub_class = 0x%02x, app_id = %d, vendor_id = 0x%04x, "
   1345          "product_id = 0x%04x, version= 0x%04x",
   1346          __FUNCTION__, hid_info.sub_class,
   1347          hid_info.app_id, hid_info.vendor_id, hid_info.product_id,
   1348          hid_info.version);
   1349 
   1350     if (btif_hh_cb.status == BTIF_HH_DISABLED)
   1351     {
   1352         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1353         return BT_STATUS_FAIL;
   1354     }
   1355 
   1356     dscp_info.vendor_id  = hid_info.vendor_id;
   1357     dscp_info.product_id = hid_info.product_id;
   1358     dscp_info.version    = hid_info.version;
   1359     dscp_info.ctry_code  = hid_info.ctry_code;
   1360 
   1361     dscp_info.descriptor.dl_len = hid_info.dl_len;
   1362     dscp_info.descriptor.dsc_list = (UINT8 *) GKI_getbuf(dscp_info.descriptor.dl_len);
   1363     if (dscp_info.descriptor.dsc_list == NULL)
   1364     {
   1365         ALOGE("%s: Failed to allocate DSCP for CB", __FUNCTION__);
   1366         return BT_STATUS_FAIL;
   1367     }
   1368     memcpy(dscp_info.descriptor.dsc_list, &(hid_info.dsc_list), hid_info.dl_len);
   1369 
   1370     if (btif_hh_add_added_dev(*bd_addr, hid_info.attr_mask))
   1371     {
   1372         BTA_HhAddDev(*bda, hid_info.attr_mask, hid_info.sub_class,
   1373                      hid_info.app_id, dscp_info);
   1374     }
   1375 
   1376     GKI_freebuf(dscp_info.descriptor.dsc_list);
   1377 
   1378     return BT_STATUS_SUCCESS;
   1379 }
   1380 
   1381 /*******************************************************************************
   1382 **
   1383 ** Function         get_protocol
   1384 **
   1385 ** Description      Get the HID proto mode.
   1386 **
   1387 ** Returns         bt_status_t
   1388 **
   1389 *******************************************************************************/
   1390 static bt_status_t get_protocol (bt_bdaddr_t *bd_addr, bthh_protocol_mode_t protocolMode)
   1391 {
   1392     CHECK_BTHH_INIT();
   1393     btif_hh_device_t *p_dev;
   1394     BD_ADDR* bda = (BD_ADDR*) bd_addr;
   1395     UNUSED(protocolMode);
   1396 
   1397     BTIF_TRACE_DEBUG(" addr = %02X:%02X:%02X:%02X:%02X:%02X",
   1398          (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1399 
   1400     if (btif_hh_cb.status == BTIF_HH_DISABLED) {
   1401         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1402         return BT_STATUS_FAIL;
   1403     }
   1404 
   1405     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
   1406     if (p_dev != NULL) {
   1407 
   1408         BTA_HhGetProtoMode(p_dev->dev_handle);
   1409     }
   1410     else {
   1411         return BT_STATUS_FAIL;
   1412     }
   1413     return BT_STATUS_SUCCESS;
   1414 }
   1415 
   1416 /*******************************************************************************
   1417 **
   1418 ** Function         set_protocol
   1419 **
   1420 ** Description      Set the HID proto mode.
   1421 **
   1422 ** Returns         bt_status_t
   1423 **
   1424 *******************************************************************************/
   1425 static bt_status_t set_protocol (bt_bdaddr_t *bd_addr, bthh_protocol_mode_t protocolMode)
   1426 {
   1427     CHECK_BTHH_INIT();
   1428     btif_hh_device_t *p_dev;
   1429     UINT8 proto_mode = protocolMode;
   1430     BD_ADDR* bda = (BD_ADDR*) bd_addr;
   1431 
   1432     BTIF_TRACE_DEBUG("%s:proto_mode = %d", __FUNCTION__,protocolMode);
   1433 
   1434     BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X",
   1435          (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1436 
   1437     if (btif_hh_cb.status == BTIF_HH_DISABLED) {
   1438         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1439         return BT_STATUS_FAIL;
   1440     }
   1441 
   1442     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
   1443     if (p_dev == NULL) {
   1444         BTIF_TRACE_WARNING(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.",
   1445              (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1446         return BT_STATUS_FAIL;
   1447     }
   1448     else if (protocolMode != BTA_HH_PROTO_RPT_MODE && protocolMode != BTA_HH_PROTO_BOOT_MODE) {
   1449         BTIF_TRACE_WARNING("s: Error, device proto_mode = %d.", __FUNCTION__, proto_mode);
   1450         return BT_STATUS_FAIL;
   1451     }
   1452     else {
   1453         BTA_HhSetProtoMode(p_dev->dev_handle, protocolMode);
   1454     }
   1455 
   1456 
   1457     return BT_STATUS_SUCCESS;
   1458 }
   1459 
   1460 /*******************************************************************************
   1461 **
   1462 ** Function         get_report
   1463 **
   1464 ** Description      Send a GET_REPORT to HID device.
   1465 **
   1466 ** Returns         bt_status_t
   1467 **
   1468 *******************************************************************************/
   1469 static bt_status_t get_report (bt_bdaddr_t *bd_addr, bthh_report_type_t reportType, uint8_t reportId, int bufferSize)
   1470 {
   1471     CHECK_BTHH_INIT();
   1472     btif_hh_device_t *p_dev;
   1473     BD_ADDR* bda = (BD_ADDR*) bd_addr;
   1474 
   1475     BTIF_TRACE_DEBUG("%s:proto_mode = %dr_type = %d, rpt_id = %d, buf_size = %d", __FUNCTION__,
   1476           reportType, reportId, bufferSize);
   1477 
   1478     BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X",
   1479          (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1480 
   1481     if (btif_hh_cb.status == BTIF_HH_DISABLED) {
   1482         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1483         return BT_STATUS_FAIL;
   1484     }
   1485 
   1486 
   1487     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
   1488     if (p_dev == NULL) {
   1489         BTIF_TRACE_ERROR("%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.",
   1490              (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1491         return BT_STATUS_FAIL;
   1492     }
   1493     else if ( ((int) reportType) <= BTA_HH_RPTT_RESRV || ((int) reportType) > BTA_HH_RPTT_FEATURE) {
   1494         BTIF_TRACE_ERROR(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.",
   1495              (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1496         return BT_STATUS_FAIL;
   1497     }
   1498     else {
   1499         BTA_HhGetReport(p_dev->dev_handle, reportType,
   1500                         reportId, bufferSize);
   1501     }
   1502 
   1503     return BT_STATUS_SUCCESS;
   1504 }
   1505 
   1506 /*******************************************************************************
   1507 **
   1508 ** Function         set_report
   1509 **
   1510 ** Description      Send a SET_REPORT to HID device.
   1511 **
   1512 ** Returns         bt_status_t
   1513 **
   1514 *******************************************************************************/
   1515 static bt_status_t set_report (bt_bdaddr_t *bd_addr, bthh_report_type_t reportType, char* report)
   1516 {
   1517     CHECK_BTHH_INIT();
   1518     btif_hh_device_t *p_dev;
   1519     BD_ADDR* bda = (BD_ADDR*) bd_addr;
   1520 
   1521     BTIF_TRACE_DEBUG("%s:reportType = %d", __FUNCTION__,reportType);
   1522 
   1523     BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X",
   1524          (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1525 
   1526 
   1527     if (btif_hh_cb.status == BTIF_HH_DISABLED) {
   1528         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1529         return BT_STATUS_FAIL;
   1530     }
   1531 
   1532     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
   1533     if (p_dev == NULL) {
   1534         BTIF_TRACE_ERROR("%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.",
   1535              (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1536         return BT_STATUS_FAIL;
   1537     }
   1538     else if ( ( (int) reportType) <= BTA_HH_RPTT_RESRV || ( (int) reportType) > BTA_HH_RPTT_FEATURE) {
   1539         BTIF_TRACE_ERROR(" Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.",
   1540              (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1541         return BT_STATUS_FAIL;
   1542     }
   1543     else {
   1544         int    hex_bytes_filled;
   1545         UINT8  *hexbuf;
   1546         UINT16 len = (strlen(report) + 1) / 2;
   1547 
   1548         hexbuf = GKI_getbuf(len);
   1549         if (hexbuf == NULL) {
   1550             BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d",
   1551                 __FUNCTION__, len);
   1552             return BT_STATUS_FAIL;
   1553         }
   1554 
   1555         /* Build a SetReport data buffer */
   1556         memset(hexbuf, 0, len);
   1557         //TODO
   1558         hex_bytes_filled = ascii_2_hex(report, len, hexbuf);
   1559         BTIF_TRACE_DEBUG("Hex bytes filled, hex value: %d", hex_bytes_filled);
   1560         if (hex_bytes_filled) {
   1561             BT_HDR* p_buf = create_pbuf(hex_bytes_filled, hexbuf);
   1562             if (p_buf == NULL) {
   1563                 BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d",
   1564                                   __FUNCTION__, hex_bytes_filled);
   1565                 GKI_freebuf(hexbuf);
   1566                 return BT_STATUS_FAIL;
   1567             }
   1568             BTA_HhSetReport(p_dev->dev_handle, reportType, p_buf);
   1569             GKI_freebuf(hexbuf);
   1570             return BT_STATUS_SUCCESS;
   1571         }
   1572         GKI_freebuf(hexbuf);
   1573         return BT_STATUS_FAIL;
   1574     }
   1575 }
   1576 
   1577 /*******************************************************************************
   1578 **
   1579 ** Function         send_data
   1580 **
   1581 ** Description      Send a SEND_DATA to HID device.
   1582 **
   1583 ** Returns         bt_status_t
   1584 **
   1585 *******************************************************************************/
   1586 static bt_status_t send_data (bt_bdaddr_t *bd_addr, char* data)
   1587 {
   1588     CHECK_BTHH_INIT();
   1589     btif_hh_device_t *p_dev;
   1590     BD_ADDR* bda = (BD_ADDR*) bd_addr;
   1591 
   1592     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
   1593 
   1594     BTIF_TRACE_DEBUG("addr = %02X:%02X:%02X:%02X:%02X:%02X",
   1595          (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1596 
   1597     if (btif_hh_cb.status == BTIF_HH_DISABLED) {
   1598         BTIF_TRACE_ERROR("%s: Error, HH status = %d", __FUNCTION__, btif_hh_cb.status);
   1599         return BT_STATUS_FAIL;
   1600     }
   1601 
   1602     p_dev = btif_hh_find_connected_dev_by_bda(bd_addr);
   1603     if (p_dev == NULL) {
   1604         BTIF_TRACE_ERROR("%s: Error, device %02X:%02X:%02X:%02X:%02X:%02X not opened.",
   1605              (*bda)[0], (*bda)[1], (*bda)[2], (*bda)[3], (*bda)[4], (*bda)[5]);
   1606         return BT_STATUS_FAIL;
   1607     }
   1608 
   1609     else {
   1610         int    hex_bytes_filled;
   1611         UINT8  *hexbuf;
   1612         UINT16 len = (strlen(data) + 1) / 2;
   1613 
   1614         hexbuf = GKI_getbuf(len);
   1615         if (hexbuf == NULL) {
   1616             BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d",
   1617                 __FUNCTION__, len);
   1618             return BT_STATUS_FAIL;
   1619         }
   1620 
   1621         /* Build a SendData data buffer */
   1622         memset(hexbuf, 0, len);
   1623         hex_bytes_filled = ascii_2_hex(data, len, hexbuf);
   1624         BTIF_TRACE_ERROR("Hex bytes filled, hex value: %d, %d", hex_bytes_filled, len);
   1625 
   1626         if (hex_bytes_filled) {
   1627             BT_HDR* p_buf = create_pbuf(hex_bytes_filled, hexbuf);
   1628             if (p_buf == NULL) {
   1629                 BTIF_TRACE_ERROR("%s: Error, failed to allocate RPT buffer, len = %d",
   1630                                   __FUNCTION__, hex_bytes_filled);
   1631                 GKI_freebuf(hexbuf);
   1632                 return BT_STATUS_FAIL;
   1633             }
   1634             p_buf->layer_specific = BTA_HH_RPTT_OUTPUT;
   1635             BTA_HhSendData(p_dev->dev_handle, *bda, p_buf);
   1636             GKI_freebuf(hexbuf);
   1637             return BT_STATUS_SUCCESS;
   1638         }
   1639         GKI_freebuf(hexbuf);
   1640         return BT_STATUS_FAIL;
   1641     }
   1642 }
   1643 
   1644 
   1645 /*******************************************************************************
   1646 **
   1647 ** Function         cleanup
   1648 **
   1649 ** Description      Closes the HH interface
   1650 **
   1651 ** Returns          bt_status_t
   1652 **
   1653 *******************************************************************************/
   1654 static void  cleanup( void )
   1655 {
   1656     BTIF_TRACE_EVENT("%s", __FUNCTION__);
   1657     btif_hh_device_t *p_dev;
   1658     int i;
   1659     if (btif_hh_cb.status == BTIF_HH_DISABLED) {
   1660         BTIF_TRACE_WARNING("%s: HH disabling or disabled already, status = %d", __FUNCTION__, btif_hh_cb.status);
   1661         return;
   1662     }
   1663     btif_hh_cb.status = BTIF_HH_DISABLING;
   1664     for (i = 0; i < BTIF_HH_MAX_HID; i++) {
   1665          p_dev = &btif_hh_cb.devices[i];
   1666          if (p_dev->dev_status != BTHH_CONN_STATE_UNKNOWN && p_dev->fd >= 0) {
   1667              BTIF_TRACE_DEBUG("%s: Closing uhid fd = %d", __FUNCTION__, p_dev->fd);
   1668              bta_hh_co_destroy(p_dev->fd);
   1669              p_dev->fd = -1;
   1670              p_dev->hh_keep_polling = 0;
   1671              p_dev->hh_poll_thread_id = -1;
   1672          }
   1673      }
   1674 
   1675     if (bt_hh_callbacks)
   1676     {
   1677         btif_disable_service(BTA_HID_SERVICE_ID);
   1678         bt_hh_callbacks = NULL;
   1679     }
   1680 
   1681 }
   1682 
   1683 static const bthh_interface_t bthhInterface = {
   1684     sizeof(bthhInterface),
   1685     init,
   1686     connect,
   1687     disconnect,
   1688     virtual_unplug,
   1689     set_info,
   1690     get_protocol,
   1691     set_protocol,
   1692 //    get_idle_time,
   1693 //    set_idle_time,
   1694     get_report,
   1695     set_report,
   1696     send_data,
   1697     cleanup,
   1698 };
   1699 
   1700 /*******************************************************************************
   1701 **
   1702 ** Function         btif_hh_execute_service
   1703 **
   1704 ** Description      Initializes/Shuts down the service
   1705 **
   1706 ** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
   1707 **
   1708 *******************************************************************************/
   1709 bt_status_t btif_hh_execute_service(BOOLEAN b_enable)
   1710 {
   1711      if (b_enable)
   1712      {
   1713           /* Enable and register with BTA-HH */
   1714           BTA_HhEnable(BTUI_HH_SECURITY, bte_hh_evt);
   1715      }
   1716      else {
   1717          /* Disable HH */
   1718          BTA_HhDisable();
   1719      }
   1720      return BT_STATUS_SUCCESS;
   1721 }
   1722 
   1723 /*******************************************************************************
   1724 **
   1725 ** Function         btif_hh_get_interface
   1726 **
   1727 ** Description      Get the hh callback interface
   1728 **
   1729 ** Returns          bthh_interface_t
   1730 **
   1731 *******************************************************************************/
   1732 const bthh_interface_t *btif_hh_get_interface()
   1733 {
   1734     BTIF_TRACE_EVENT("%s", __FUNCTION__);
   1735     return &bthhInterface;
   1736 }
   1737