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