Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2013 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  *
     22  *  Filename:      btif_gatt_client.c
     23  *
     24  *  Description:   GATT client implementation
     25  *
     26  *******************************************************************************/
     27 
     28 #include <hardware/bluetooth.h>
     29 #include <hardware/bt_gatt.h>
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <errno.h>
     33 #include <string.h>
     34 
     35 #define LOG_TAG "BtGatt.btif"
     36 
     37 #include "btif_common.h"
     38 #include "btif_util.h"
     39 
     40 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
     41 
     42 #include "bta_api.h"
     43 #include "bta_gatt_api.h"
     44 #include "bd.h"
     45 #include "btif_storage.h"
     46 #include "btif_config.h"
     47 
     48 #include "btif_gatt.h"
     49 #include "btif_gatt_util.h"
     50 #include "btif_dm.h"
     51 #include "btif_storage.h"
     52 
     53 /*******************************************************************************
     54 **  Constants & Macros
     55 ********************************************************************************/
     56 
     57 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
     58     {\
     59         ALOGW("%s: BTGATT not initialized", __FUNCTION__);\
     60         return BT_STATUS_NOT_READY;\
     61     } else {\
     62         ALOGD("%s", __FUNCTION__);\
     63     }
     64 
     65 
     66 typedef enum {
     67     BTIF_GATTC_REGISTER_APP = 1000,
     68     BTIF_GATTC_UNREGISTER_APP,
     69     BTIF_GATTC_SCAN_START,
     70     BTIF_GATTC_SCAN_STOP,
     71     BTIF_GATTC_OPEN,
     72     BTIF_GATTC_CLOSE,
     73     BTIF_GATTC_SEARCH_SERVICE,
     74     BTIF_GATTC_GET_FIRST_CHAR,
     75     BTIF_GATTC_GET_NEXT_CHAR,
     76     BTIF_GATTC_GET_FIRST_CHAR_DESCR,
     77     BTIF_GATTC_GET_NEXT_CHAR_DESCR,
     78     BTIF_GATTC_GET_FIRST_INCL_SERVICE,
     79     BTIF_GATTC_GET_NEXT_INCL_SERVICE,
     80     BTIF_GATTC_READ_CHAR,
     81     BTIF_GATTC_READ_CHAR_DESCR,
     82     BTIF_GATTC_WRITE_CHAR,
     83     BTIF_GATTC_WRITE_CHAR_DESCR,
     84     BTIF_GATTC_EXECUTE_WRITE,
     85     BTIF_GATTC_REG_FOR_NOTIFICATION,
     86     BTIF_GATTC_DEREG_FOR_NOTIFICATION,
     87     BTIF_GATTC_REFRESH,
     88     BTIF_GATTC_READ_RSSI
     89 } btif_gattc_event_t;
     90 
     91 #define BTIF_GATT_MAX_OBSERVED_DEV 40
     92 
     93 #define BTIF_GATT_OBSERVE_EVT   0x1000
     94 #define BTIF_GATTC_RSSI_EVT     0x1001
     95 
     96 /*******************************************************************************
     97 **  Local type definitions
     98 ********************************************************************************/
     99 
    100 typedef struct
    101 {
    102     uint8_t     value[BTGATT_MAX_ATTR_LEN];
    103     bt_bdaddr_t bd_addr;
    104     btgatt_srvc_id_t srvc_id;
    105     btgatt_srvc_id_t incl_srvc_id;
    106     btgatt_char_id_t char_id;
    107     bt_uuid_t   uuid;
    108     uint16_t    conn_id;
    109     uint16_t    len;
    110     uint8_t     client_if;
    111     uint8_t     action;
    112     uint8_t     is_direct;
    113     uint8_t     search_all;
    114     uint8_t     auth_req;
    115     uint8_t     write_type;
    116     uint8_t     status;
    117     uint8_t     addr_type;
    118     int8_t      rssi;
    119     tBT_DEVICE_TYPE device_type;
    120 } __attribute__((packed)) btif_gattc_cb_t;
    121 
    122 typedef struct
    123 {
    124     bt_bdaddr_t bd_addr;
    125     BOOLEAN     in_use;
    126 }__attribute__((packed)) btif_gattc_dev_t;
    127 
    128 typedef struct
    129 {
    130     btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV];
    131     uint8_t        addr_type;
    132     uint8_t        next_storage_idx;
    133 }__attribute__((packed)) btif_gattc_dev_cb_t;
    134 
    135 /*******************************************************************************
    136 **  Static variables
    137 ********************************************************************************/
    138 
    139 extern const btgatt_callbacks_t *bt_gatt_callbacks;
    140 static btif_gattc_dev_cb_t  btif_gattc_dev_cb;
    141 static btif_gattc_dev_cb_t  *p_dev_cb = &btif_gattc_dev_cb;
    142 static uint8_t rssi_request_client_if;
    143 
    144 /*******************************************************************************
    145 **  Static functions
    146 ********************************************************************************/
    147 
    148 static void btif_gattc_init_dev_cb(void)
    149 {
    150     memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t));
    151 }
    152 
    153 static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type)
    154 {
    155     BOOLEAN found=FALSE;
    156     uint8_t i;
    157     for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
    158     {
    159         if (!p_dev_cb->remote_dev[i].in_use )
    160         {
    161             memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
    162             p_dev_cb->addr_type = addr_type;
    163             p_dev_cb->remote_dev[i].in_use = TRUE;
    164             ALOGD("%s device added idx=%d", __FUNCTION__, i  );
    165             break;
    166         }
    167     }
    168 
    169     if ( i == BTIF_GATT_MAX_OBSERVED_DEV)
    170     {
    171         i= p_dev_cb->next_storage_idx;
    172         memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
    173         p_dev_cb->addr_type = addr_type;
    174         p_dev_cb->remote_dev[i].in_use = TRUE;
    175         ALOGD("%s device overwrite idx=%d", __FUNCTION__, i  );
    176         p_dev_cb->next_storage_idx++;
    177         if(p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV)
    178                p_dev_cb->next_storage_idx = 0;
    179     }
    180 }
    181 
    182 static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda)
    183 {
    184     uint8_t i;
    185     for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
    186     {
    187         if (p_dev_cb->remote_dev[i].in_use &&
    188             !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN))
    189         {
    190             return TRUE;
    191         }
    192     }
    193     return FALSE;
    194 }
    195 
    196 static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb )
    197 {
    198     uint8_t remote_name_len;
    199     uint8_t *p_eir_remote_name=NULL;
    200     bt_bdname_t bdname;
    201 
    202     p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
    203                                          BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
    204 
    205     if(p_eir_remote_name == NULL)
    206     {
    207         p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
    208                                 BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
    209     }
    210 
    211     if(p_eir_remote_name)
    212     {
    213          memcpy(bdname.name, p_eir_remote_name, remote_name_len);
    214          bdname.name[remote_name_len]='\0';
    215 
    216         ALOGD("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name,
    217               remote_name_len, p_btif_cb->device_type  );
    218         btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address,   bdname.name,
    219                                                p_btif_cb->device_type);
    220     }
    221 
    222     btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type);
    223 }
    224 
    225 static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
    226 {
    227     ALOGD("%s: Event %d", __FUNCTION__, event);
    228 
    229     tBTA_GATTC *p_data = (tBTA_GATTC*)p_param;
    230     switch (event)
    231     {
    232         case BTA_GATTC_REG_EVT:
    233         {
    234             bt_uuid_t app_uuid;
    235             bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
    236             HAL_CBACK(bt_gatt_callbacks, client->register_client_cb
    237                 , p_data->reg_oper.status
    238                 , p_data->reg_oper.client_if
    239                 , &app_uuid
    240             );
    241             break;
    242         }
    243 
    244         case BTA_GATTC_DEREG_EVT:
    245             break;
    246 
    247         case BTA_GATTC_READ_CHAR_EVT:
    248         {
    249             btgatt_read_params_t data;
    250             set_read_value(&data, &p_data->read);
    251 
    252             HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb
    253                 , p_data->read.conn_id, p_data->read.status, &data);
    254             break;
    255         }
    256 
    257         case BTA_GATTC_WRITE_CHAR_EVT:
    258         case BTA_GATTC_PREP_WRITE_EVT:
    259         {
    260             btgatt_write_params_t data;
    261             bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
    262             bta_to_btif_char_id(&data.char_id, &p_data->write.char_id);
    263 
    264             HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb
    265                 , p_data->write.conn_id, p_data->write.status, &data
    266             );
    267             break;
    268         }
    269 
    270         case BTA_GATTC_EXEC_EVT:
    271         {
    272             HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb
    273                 , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status
    274             );
    275             break;
    276         }
    277 
    278         case BTA_GATTC_SEARCH_CMPL_EVT:
    279         {
    280             HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb
    281                 , p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
    282             break;
    283         }
    284 
    285         case BTA_GATTC_SEARCH_RES_EVT:
    286         {
    287             btgatt_srvc_id_t data;
    288             bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid));
    289             HAL_CBACK(bt_gatt_callbacks, client->search_result_cb
    290                 , p_data->srvc_res.conn_id, &data);
    291             break;
    292         }
    293 
    294         case BTA_GATTC_READ_DESCR_EVT:
    295         {
    296             btgatt_read_params_t data;
    297             set_read_value(&data, &p_data->read);
    298 
    299             HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb
    300                 , p_data->read.conn_id, p_data->read.status, &data);
    301             break;
    302         }
    303 
    304         case BTA_GATTC_WRITE_DESCR_EVT:
    305         {
    306             btgatt_write_params_t data;
    307             bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
    308             bta_to_btif_char_id(&data.char_id, &p_data->write.char_id);
    309             bta_to_btif_uuid(&data.descr_id, &p_data->write.descr_type);
    310 
    311             HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb
    312                 , p_data->write.conn_id, p_data->write.status, &data);
    313             break;
    314         }
    315 
    316         case BTA_GATTC_NOTIF_EVT:
    317         {
    318             btgatt_notify_params_t data;
    319 
    320             bdcpy(data.bda.address, p_data->notify.bda);
    321 
    322             bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id);
    323             bta_to_btif_char_id(&data.char_id, &p_data->notify.char_id.char_id);
    324             memcpy(data.value, p_data->notify.value, p_data->notify.len);
    325 
    326             data.is_notify = p_data->notify.is_notify;
    327             data.len = p_data->notify.len;
    328 
    329             HAL_CBACK(bt_gatt_callbacks, client->notify_cb
    330                 , p_data->notify.conn_id, &data);
    331 
    332             if (p_data->notify.is_notify == FALSE)
    333             {
    334                 BTA_GATTC_SendIndConfirm(p_data->notify.conn_id,
    335                                          &p_data->notify.char_id);
    336             }
    337             break;
    338         }
    339 
    340         case BTA_GATTC_OPEN_EVT:
    341         {
    342             bt_bdaddr_t bda;
    343             bdcpy(bda.address, p_data->open.remote_bda);
    344 
    345             if (p_data->open.status == BTA_GATT_OK)
    346                 btif_gatt_check_encrypted_link(p_data->open.remote_bda);
    347 
    348             HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id
    349                 , p_data->open.status, p_data->open.client_if, &bda);
    350             break;
    351         }
    352 
    353         case BTA_GATTC_CLOSE_EVT:
    354         {
    355             bt_bdaddr_t bda;
    356             bdcpy(bda.address, p_data->close.remote_bda);
    357             HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
    358                 , p_data->status, p_data->close.client_if, &bda);
    359 
    360             if(p_data->status == BTA_GATT_OK)
    361                 btif_gatt_remove_encrypted_link(p_data->close.remote_bda);
    362             break;
    363         }
    364 
    365         case BTA_GATTC_ACL_EVT:
    366             ALOGD("BTA_GATTC_ACL_EVT: status = %d", p_data->status);
    367             /* Ignore for now */
    368             break;
    369 
    370         case BTA_GATTC_CANCEL_OPEN_EVT:
    371             break;
    372 
    373         case BTIF_GATT_OBSERVE_EVT:
    374         {
    375             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
    376             if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
    377             {
    378                 btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
    379                 btif_gattc_update_properties(p_btif_cb);
    380             }
    381             HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
    382                       &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
    383             break;
    384         }
    385 
    386         case BTIF_GATTC_RSSI_EVT:
    387         {
    388             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
    389             HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if,
    390                       &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status);
    391             break;
    392         }
    393 
    394         default:
    395             ALOGE("%s: Unhandled event (%d)!", __FUNCTION__, event);
    396             break;
    397     }
    398 }
    399 
    400 static void bte_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
    401 {
    402     bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
    403                     (uint16_t) event, (void*)p_data, sizeof(tBTA_GATTC), NULL);
    404     ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
    405 }
    406 
    407 static void bte_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
    408 {
    409     btif_gattc_cb_t btif_cb;
    410     uint8_t len;
    411 
    412     switch (event)
    413     {
    414         case BTA_DM_INQ_RES_EVT:
    415         {
    416             bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
    417             btif_cb.device_type = p_data->inq_res.device_type;
    418             btif_cb.rssi = p_data->inq_res.rssi;
    419             btif_cb.addr_type = p_data->inq_res.ble_addr_type;
    420             if (p_data->inq_res.p_eir)
    421             {
    422                 memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
    423                 if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
    424                                       &len))
    425                 {
    426                     p_data->inq_res.remt_name_not_required  = TRUE;
    427                 }
    428             }
    429         }
    430         break;
    431 
    432         case BTA_DM_INQ_CMPL_EVT:
    433         {
    434             BTIF_TRACE_DEBUG2("%s  BLE observe complete. Num Resp %d",
    435                               __FUNCTION__,p_data->inq_cmpl.num_resps);
    436             return;
    437         }
    438 
    439         default:
    440         BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
    441         return;
    442     }
    443     btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
    444                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    445 }
    446 
    447 static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
    448 {
    449     btif_gattc_cb_t btif_cb;
    450 
    451     bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
    452     btif_cb.rssi = p_result->rssi;
    453     btif_cb.status = p_result->status;
    454     btif_cb.client_if = rssi_request_client_if;
    455     btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
    456                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    457 }
    458 
    459 
    460 static void btgattc_handle_event(uint16_t event, char* p_param)
    461 {
    462     tBTA_GATT_STATUS           status;
    463     tBT_UUID                   uuid;
    464     tBTA_GATT_SRVC_ID          srvc_id;
    465     tGATT_CHAR_PROP            out_char_prop;
    466     tBTA_GATTC_CHAR_ID         in_char_id;
    467     tBTA_GATTC_CHAR_ID         out_char_id;
    468     tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
    469     tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
    470     tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
    471     tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
    472     tBTA_GATT_UNFMT            descr_val;
    473 
    474     btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*)p_param;
    475     if (!p_cb) return;
    476 
    477     ALOGD("%s: Event %d", __FUNCTION__, event);
    478 
    479     switch (event)
    480     {
    481         case BTIF_GATTC_REGISTER_APP:
    482             btif_to_bta_uuid(&uuid, &p_cb->uuid);
    483             BTA_GATTC_AppRegister(&uuid, bte_gattc_cback);
    484             break;
    485 
    486         case BTIF_GATTC_UNREGISTER_APP:
    487             BTA_GATTC_AppDeregister(p_cb->client_if);
    488             break;
    489 
    490         case BTIF_GATTC_SCAN_START:
    491             btif_gattc_init_dev_cb();
    492             BTA_DmBleObserve(TRUE, 0, bte_scan_results_cb);
    493             break;
    494 
    495         case BTIF_GATTC_SCAN_STOP:
    496             BTA_DmBleObserve(FALSE, 0, 0);
    497             break;
    498 
    499         case BTIF_GATTC_OPEN:
    500             if (!p_cb->is_direct)
    501                 BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
    502 
    503             BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct);
    504             break;
    505 
    506         case BTIF_GATTC_CLOSE:
    507             // Disconnect establiched connections
    508             if (p_cb->conn_id != 0)
    509                 BTA_GATTC_Close(p_cb->conn_id);
    510             else
    511                 BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
    512 
    513             // Cancel pending background connections (remove from whitelist)
    514             BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
    515             break;
    516 
    517         case BTIF_GATTC_SEARCH_SERVICE:
    518         {
    519             if (p_cb->search_all)
    520             {
    521                 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
    522             } else {
    523                 btif_to_bta_uuid(&uuid, &p_cb->uuid);
    524                 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
    525             }
    526             break;
    527         }
    528 
    529         case BTIF_GATTC_GET_FIRST_CHAR:
    530         {
    531             btgatt_char_id_t char_id;
    532             btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
    533             status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
    534                                             &out_char_id, &out_char_prop);
    535 
    536             if (status == 0)
    537                 bta_to_btif_char_id(&char_id, &out_char_id.char_id);
    538 
    539             HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
    540                 p_cb->conn_id, status, &p_cb->srvc_id,
    541                 &char_id, out_char_prop);
    542             break;
    543         }
    544 
    545         case BTIF_GATTC_GET_NEXT_CHAR:
    546         {
    547             btgatt_char_id_t char_id;
    548             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
    549             btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
    550 
    551             status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
    552                                             &out_char_id, &out_char_prop);
    553 
    554             if (status == 0)
    555                 bta_to_btif_char_id(&char_id, &out_char_id.char_id);
    556 
    557             HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
    558                 p_cb->conn_id, status, &p_cb->srvc_id,
    559                 &char_id, out_char_prop);
    560             break;
    561         }
    562 
    563         case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
    564         {
    565             bt_uuid_t descr_id;
    566             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
    567             btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
    568 
    569             status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
    570                                                     &out_char_descr_id);
    571 
    572             if (status == 0)
    573                 bta_to_btif_uuid(&descr_id, &out_char_descr_id.descr_type);
    574 
    575             HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
    576                 p_cb->conn_id, status, &p_cb->srvc_id,
    577                 &p_cb->char_id, &descr_id);
    578             break;
    579         }
    580 
    581         case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
    582         {
    583             bt_uuid_t descr_id;
    584             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
    585             btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
    586             btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid);
    587 
    588             status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
    589                                         , NULL, &out_char_descr_id);
    590 
    591             if (status == 0)
    592                 bta_to_btif_uuid(&descr_id, &out_char_descr_id.descr_type);
    593 
    594             HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
    595                 p_cb->conn_id, status, &p_cb->srvc_id,
    596                 &p_cb->char_id, &descr_id);
    597             break;
    598         }
    599 
    600         case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
    601         {
    602             btgatt_srvc_id_t incl_srvc_id;
    603             btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
    604 
    605             status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
    606                         &srvc_id, NULL, &out_incl_svc_id);
    607 
    608             bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
    609 
    610             HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
    611                 p_cb->conn_id, status, &p_cb->srvc_id,
    612                 &incl_srvc_id);
    613             break;
    614         }
    615 
    616         case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
    617         {
    618             btgatt_srvc_id_t incl_srvc_id;
    619             btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
    620             btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
    621 
    622             status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
    623                         &in_incl_svc_id, NULL, &out_incl_svc_id);
    624 
    625             bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
    626 
    627             HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
    628                 p_cb->conn_id, status, &p_cb->srvc_id,
    629                 &incl_srvc_id);
    630             break;
    631         }
    632 
    633         case BTIF_GATTC_READ_CHAR:
    634             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
    635             btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
    636 
    637             BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
    638             break;
    639 
    640         case BTIF_GATTC_READ_CHAR_DESCR:
    641             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
    642             btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
    643             btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid);
    644 
    645             BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
    646             break;
    647 
    648         case BTIF_GATTC_WRITE_CHAR:
    649             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
    650             btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
    651 
    652             BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
    653                                      p_cb->write_type,
    654                                      p_cb->len,
    655                                      p_cb->value,
    656                                      p_cb->auth_req);
    657             break;
    658 
    659         case BTIF_GATTC_WRITE_CHAR_DESCR:
    660             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
    661             btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
    662             btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid);
    663 
    664             descr_val.len = p_cb->len;
    665             descr_val.p_value = p_cb->value;
    666 
    667             BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
    668                                      p_cb->write_type, &descr_val,
    669                                      p_cb->auth_req);
    670             break;
    671 
    672         case BTIF_GATTC_EXECUTE_WRITE:
    673             BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
    674             break;
    675 
    676         case BTIF_GATTC_REG_FOR_NOTIFICATION:
    677             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
    678             btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
    679 
    680             status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
    681                                     p_cb->bd_addr.address, &in_char_id);
    682 
    683             HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
    684                 p_cb->conn_id, 1, status, &p_cb->srvc_id,
    685                 &p_cb->char_id);
    686             break;
    687 
    688         case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
    689             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
    690             btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id);
    691 
    692             status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
    693                                         p_cb->bd_addr.address, &in_char_id);
    694 
    695             HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
    696                 p_cb->conn_id, 0, status, &p_cb->srvc_id,
    697                 &p_cb->char_id);
    698             break;
    699 
    700         case BTIF_GATTC_REFRESH:
    701             BTA_GATTC_Refresh(p_cb->bd_addr.address);
    702             break;
    703 
    704         case BTIF_GATTC_READ_RSSI:
    705             rssi_request_client_if = p_cb->client_if;
    706             BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
    707             break;
    708 
    709         default:
    710             ALOGE("%s: Unknown event (%d)!", __FUNCTION__, event);
    711             break;
    712     }
    713 }
    714 
    715 /*******************************************************************************
    716 **  Client API Functions
    717 ********************************************************************************/
    718 
    719 static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
    720 {
    721     CHECK_BTGATT_INIT();
    722     btif_gattc_cb_t btif_cb;
    723     memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
    724     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
    725                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    726 }
    727 
    728 static bt_status_t btif_gattc_unregister_app(int client_if )
    729 {
    730     CHECK_BTGATT_INIT();
    731     btif_gattc_cb_t btif_cb;
    732     btif_cb.client_if = (uint8_t) client_if;
    733     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
    734                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    735 }
    736 
    737 static bt_status_t btif_gattc_scan( int client_if, bool start )
    738 {
    739     CHECK_BTGATT_INIT();
    740     btif_gattc_cb_t btif_cb;
    741     btif_cb.client_if = (uint8_t) client_if;
    742     return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
    743                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    744 }
    745 
    746 static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr, bool is_direct )
    747 {
    748     CHECK_BTGATT_INIT();
    749     btif_gattc_cb_t btif_cb;
    750     btif_cb.client_if = (uint8_t) client_if;
    751     btif_cb.is_direct = is_direct ? 1 : 0;
    752     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
    753     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
    754                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    755 }
    756 
    757 static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
    758 {
    759     CHECK_BTGATT_INIT();
    760     btif_gattc_cb_t btif_cb;
    761     btif_cb.client_if = (uint8_t) client_if;
    762     btif_cb.conn_id = (uint16_t) conn_id;
    763     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
    764     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
    765                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    766 }
    767 
    768 static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
    769 {
    770     CHECK_BTGATT_INIT();
    771     btif_gattc_cb_t btif_cb;
    772     btif_cb.client_if = (uint8_t) client_if;
    773     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
    774     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
    775                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    776 }
    777 
    778 static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
    779 {
    780     CHECK_BTGATT_INIT();
    781     btif_gattc_cb_t btif_cb;
    782     btif_cb.conn_id = (uint16_t) conn_id;
    783     btif_cb.search_all = filter_uuid ? 0 : 1;
    784     if (filter_uuid)
    785         memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
    786     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
    787                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    788 }
    789 
    790 static bt_status_t btif_gattc_get_characteristic( int conn_id
    791         , btgatt_srvc_id_t *srvc_id, btgatt_char_id_t *start_char_id)
    792 {
    793     CHECK_BTGATT_INIT();
    794     btif_gattc_cb_t btif_cb;
    795     btif_cb.conn_id = (uint16_t) conn_id;
    796     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    797     if (start_char_id)
    798     {
    799         memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_char_id_t));
    800         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
    801                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    802     }
    803     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
    804                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    805 }
    806 
    807 static bt_status_t btif_gattc_get_descriptor( int conn_id
    808         , btgatt_srvc_id_t *srvc_id, btgatt_char_id_t *char_id
    809         , bt_uuid_t *start_descr_id)
    810 {
    811     CHECK_BTGATT_INIT();
    812     btif_gattc_cb_t btif_cb;
    813     btif_cb.conn_id = (uint16_t) conn_id;
    814     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    815     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    816     if (start_descr_id)
    817     {
    818         memcpy(&btif_cb.uuid, start_descr_id, sizeof(bt_uuid_t));
    819         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
    820                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    821     }
    822 
    823     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
    824                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    825 }
    826 
    827 static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
    828                                                    btgatt_srvc_id_t *start_incl_srvc_id)
    829 {
    830     CHECK_BTGATT_INIT();
    831     btif_gattc_cb_t btif_cb;
    832     btif_cb.conn_id = (uint16_t) conn_id;
    833     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    834     if (start_incl_srvc_id)
    835     {
    836         memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
    837         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
    838                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    839     }
    840     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
    841                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    842 }
    843 
    844 static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
    845                                         btgatt_char_id_t* char_id, int auth_req )
    846 {
    847     CHECK_BTGATT_INIT();
    848     btif_gattc_cb_t btif_cb;
    849     btif_cb.conn_id = (uint16_t) conn_id;
    850     btif_cb.auth_req = (uint8_t) auth_req;
    851     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    852     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    853     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
    854                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    855 }
    856 
    857 static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
    858                                               btgatt_char_id_t* char_id, bt_uuid_t* descr_id,
    859                                               int auth_req )
    860 {
    861     CHECK_BTGATT_INIT();
    862     btif_gattc_cb_t btif_cb;
    863     btif_cb.conn_id = (uint16_t) conn_id;
    864     btif_cb.auth_req = (uint8_t) auth_req;
    865     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    866     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    867     memcpy(&btif_cb.uuid, descr_id, sizeof(bt_uuid_t));
    868     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
    869                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    870 }
    871 
    872 static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
    873                                          btgatt_char_id_t* char_id, int write_type,
    874                                          int len, int auth_req, char* p_value)
    875 {
    876     CHECK_BTGATT_INIT();
    877     btif_gattc_cb_t btif_cb;
    878     btif_cb.conn_id = (uint16_t) conn_id;
    879     btif_cb.auth_req = (uint8_t) auth_req;
    880     btif_cb.write_type = (uint8_t) write_type;
    881     btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
    882     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    883     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    884     memcpy(btif_cb.value, p_value, btif_cb.len);
    885     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
    886                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    887 }
    888 
    889 static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
    890                                                btgatt_char_id_t* char_id, bt_uuid_t* descr_id,
    891                                                int write_type, int len, int auth_req,
    892                                                char* p_value)
    893 {
    894     CHECK_BTGATT_INIT();
    895     btif_gattc_cb_t btif_cb;
    896     btif_cb.conn_id = (uint16_t) conn_id;
    897     btif_cb.auth_req = (uint8_t) auth_req;
    898     btif_cb.write_type = (uint8_t) write_type;
    899     btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
    900     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    901     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    902     memcpy(&btif_cb.uuid, descr_id, sizeof(bt_uuid_t));
    903     memcpy(btif_cb.value, p_value, btif_cb.len);
    904     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
    905                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    906 }
    907 
    908 static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
    909 {
    910     CHECK_BTGATT_INIT();
    911     btif_gattc_cb_t btif_cb;
    912     btif_cb.conn_id = (uint16_t) conn_id;
    913     btif_cb.action = (uint8_t) execute;
    914     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
    915                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    916 }
    917 
    918 static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
    919                                                    btgatt_srvc_id_t* srvc_id,
    920                                                    btgatt_char_id_t* char_id)
    921 {
    922     CHECK_BTGATT_INIT();
    923     btif_gattc_cb_t btif_cb;
    924     btif_cb.client_if = (uint8_t) client_if;
    925     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
    926     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    927     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    928     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
    929                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    930 }
    931 
    932 static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
    933                                                      btgatt_srvc_id_t* srvc_id,
    934                                                      btgatt_char_id_t* char_id)
    935 {
    936     CHECK_BTGATT_INIT();
    937     btif_gattc_cb_t btif_cb;
    938     btif_cb.client_if = (uint8_t) client_if;
    939     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
    940     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
    941     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t));
    942     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
    943                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    944 }
    945 
    946 static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
    947 {
    948     CHECK_BTGATT_INIT();
    949     btif_gattc_cb_t btif_cb;
    950     btif_cb.client_if = (uint8_t) client_if;
    951     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
    952     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
    953                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    954 }
    955 
    956 static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
    957 {
    958     int device_type = 0;
    959     char bd_addr_str[18] = {0};
    960 
    961     bd2str(bd_addr, &bd_addr_str);
    962     if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
    963         return device_type;
    964     return 0;
    965 }
    966 
    967 extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
    968 
    969 static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
    970 {
    971     return btif_gattc_test_command_impl(command, params);
    972 }
    973 
    974 
    975 const btgatt_client_interface_t btgattClientInterface = {
    976     btif_gattc_register_app,
    977     btif_gattc_unregister_app,
    978     btif_gattc_scan,
    979     btif_gattc_open,
    980     btif_gattc_close,
    981     btif_gattc_refresh,
    982     btif_gattc_search_service,
    983     btif_gattc_get_included_service,
    984     btif_gattc_get_characteristic,
    985     btif_gattc_get_descriptor,
    986     btif_gattc_read_char,
    987     btif_gattc_write_char,
    988     btif_gattc_read_char_descr,
    989     btif_gattc_write_char_descr,
    990     btif_gattc_execute_write,
    991     btif_gattc_reg_for_notification,
    992     btif_gattc_dereg_for_notification,
    993     btif_gattc_read_remote_rssi,
    994     btif_gattc_get_device_type,
    995     btif_gattc_test_command
    996 };
    997 
    998 #endif
    999