Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-2014 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 <stdio.h>
     30 #include <stdlib.h>
     31 #include <errno.h>
     32 #include <string.h>
     33 
     34 #define LOG_TAG "BtGatt.btif"
     35 
     36 #include "btif_common.h"
     37 #include "btif_util.h"
     38 
     39 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
     40 
     41 #include "btif_gatt_multi_adv_util.h"
     42 #include <hardware/bt_gatt.h>
     43 #include "bta_api.h"
     44 #include "bta_gatt_api.h"
     45 #include "bd.h"
     46 #include "btif_storage.h"
     47 #include "btif_config.h"
     48 
     49 #include "btif_gatt.h"
     50 #include "btif_gatt_util.h"
     51 #include "btif_dm.h"
     52 #include "btif_storage.h"
     53 
     54 #include "vendor_api.h"
     55 
     56 /*******************************************************************************
     57 **  Constants & Macros
     58 ********************************************************************************/
     59 
     60 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
     61     {\
     62         BTIF_TRACE_WARNING("%s: BTGATT not initialized", __FUNCTION__);\
     63         return BT_STATUS_NOT_READY;\
     64     } else {\
     65         BTIF_TRACE_DEBUG("%s", __FUNCTION__);\
     66     }
     67 
     68 #define BLE_RESOLVE_ADDR_MSB                 0x40   /* bit7, bit6 is 01 to be resolvable random */
     69 #define BLE_RESOLVE_ADDR_MASK                0xc0   /* bit 6, and bit7 */
     70 #define BTM_BLE_IS_RESOLVE_BDA(x)           ((x[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB)
     71 
     72 typedef enum {
     73     BTIF_GATTC_REGISTER_APP = 1000,
     74     BTIF_GATTC_UNREGISTER_APP,
     75     BTIF_GATTC_SCAN_START,
     76     BTIF_GATTC_SCAN_STOP,
     77     BTIF_GATTC_OPEN,
     78     BTIF_GATTC_CLOSE,
     79     BTIF_GATTC_SEARCH_SERVICE,
     80     BTIF_GATTC_GET_FIRST_CHAR,
     81     BTIF_GATTC_GET_NEXT_CHAR,
     82     BTIF_GATTC_GET_FIRST_CHAR_DESCR,
     83     BTIF_GATTC_GET_NEXT_CHAR_DESCR,
     84     BTIF_GATTC_GET_FIRST_INCL_SERVICE,
     85     BTIF_GATTC_GET_NEXT_INCL_SERVICE,
     86     BTIF_GATTC_READ_CHAR,
     87     BTIF_GATTC_READ_CHAR_DESCR,
     88     BTIF_GATTC_WRITE_CHAR,
     89     BTIF_GATTC_WRITE_CHAR_DESCR,
     90     BTIF_GATTC_EXECUTE_WRITE,
     91     BTIF_GATTC_REG_FOR_NOTIFICATION,
     92     BTIF_GATTC_DEREG_FOR_NOTIFICATION,
     93     BTIF_GATTC_REFRESH,
     94     BTIF_GATTC_READ_RSSI,
     95     BTIF_GATTC_LISTEN,
     96     BTIF_GATTC_SET_ADV_DATA,
     97     BTIF_GATTC_CONFIGURE_MTU,
     98     BTIF_GATTC_CONN_PARAM_UPDT,
     99     BTIF_GATTC_SCAN_FILTER_PARAM_SETUP,
    100     BTIF_GATTC_SCAN_FILTER_CONFIG,
    101     BTIF_GATTC_SCAN_FILTER_CLEAR,
    102     BTIF_GATTC_SCAN_FILTER_ENABLE,
    103     BTIF_GATTC_SET_SCAN_PARAMS,
    104     BTIF_GATTC_ADV_INSTANCE_ENABLE,
    105     BTIF_GATTC_ADV_INSTANCE_UPDATE,
    106     BTIF_GATTC_ADV_INSTANCE_SET_DATA,
    107     BTIF_GATTC_ADV_INSTANCE_DISABLE,
    108     BTIF_GATTC_CONFIG_STORAGE_PARAMS,
    109     BTIF_GATTC_ENABLE_BATCH_SCAN,
    110     BTIF_GATTC_READ_BATCH_SCAN_REPORTS,
    111     BTIF_GATTC_DISABLE_BATCH_SCAN
    112 } btif_gattc_event_t;
    113 
    114 #define BTIF_GATT_MAX_OBSERVED_DEV 40
    115 
    116 #define BTIF_GATT_OBSERVE_EVT   0x1000
    117 #define BTIF_GATTC_RSSI_EVT     0x1001
    118 #define BTIF_GATTC_SCAN_FILTER_EVT   0x1003
    119 
    120 #define ENABLE_BATCH_SCAN 1
    121 #define DISABLE_BATCH_SCAN 0
    122 
    123 /*******************************************************************************
    124 **  Local type definitions
    125 ********************************************************************************/
    126 typedef struct
    127 {
    128     uint8_t report_format;
    129     uint16_t data_len;
    130     uint8_t num_records;
    131     uint8_t *p_rep_data;
    132 } btgatt_batch_reports;
    133 
    134 typedef struct
    135 {
    136     uint8_t  status;
    137     uint8_t  client_if;
    138     uint8_t  filt_index;
    139     uint8_t  adv_state;
    140     uint8_t  action;
    141     uint8_t  avbl_space;
    142     uint8_t  lost_timeout;
    143     bt_bdaddr_t bd_addr;
    144     uint8_t  batch_scan_full_max;
    145     uint8_t  batch_scan_trunc_max;
    146     uint8_t  batch_scan_notify_threshold;
    147     tBTA_BLE_SCAN_MODE scan_mode;
    148     uint32_t scan_interval;
    149     uint32_t scan_window;
    150     tBTA_BLE_DISCARD_RULE discard_rule;
    151     tBLE_ADDR_TYPE        addr_type;
    152     btgatt_batch_reports read_reports;
    153 } btgatt_batch_track_cb_t;
    154 
    155 typedef tBTA_DM_BLE_PF_FILT_PARAMS btgatt_adv_filt_param_t;
    156 
    157 typedef struct
    158 {
    159     uint8_t     client_if;
    160     uint8_t     action;
    161     tBTA_DM_BLE_PF_COND_TYPE filt_type;
    162     bt_bdaddr_t bd_addr;
    163     uint8_t     value[BTGATT_MAX_ATTR_LEN];
    164     uint8_t     value_len;
    165     uint8_t     filt_index;
    166     uint16_t    conn_id;
    167     uint16_t    company_id_mask;
    168     bt_uuid_t   uuid;
    169     bt_uuid_t   uuid_mask;
    170     uint8_t     value_mask[BTGATT_MAX_ATTR_LEN];
    171     uint8_t     value_mask_len;
    172     uint8_t     has_mask;
    173     uint8_t     addr_type;
    174     uint8_t     status;
    175     tBTA_DM_BLE_PF_AVBL_SPACE avbl_space;
    176     tBTA_DM_BLE_SCAN_COND_OP cond_op;
    177     btgatt_adv_filt_param_t adv_filt_param;
    178 } btgatt_adv_filter_cb_t;
    179 
    180 typedef struct
    181 {
    182     uint8_t     value[BTGATT_MAX_ATTR_LEN];
    183     uint8_t     inst_id;
    184     bt_bdaddr_t bd_addr;
    185     btgatt_srvc_id_t srvc_id;
    186     btgatt_srvc_id_t incl_srvc_id;
    187     btgatt_gatt_id_t char_id;
    188     btgatt_gatt_id_t descr_id;
    189     bt_uuid_t   uuid;
    190     bt_uuid_t   uuid_mask;
    191     uint16_t    conn_id;
    192     uint16_t    len;
    193     uint16_t    mask;
    194     uint16_t    scan_interval;
    195     uint16_t    scan_window;
    196     uint8_t     client_if;
    197     uint8_t     action;
    198     uint8_t     is_direct;
    199     uint8_t     search_all;
    200     uint8_t     auth_req;
    201     uint8_t     write_type;
    202     uint8_t     status;
    203     uint8_t     addr_type;
    204     uint8_t     start;
    205     uint8_t     has_mask;
    206     int8_t      rssi;
    207     uint8_t     flag;
    208     tBT_DEVICE_TYPE device_type;
    209     btgatt_transport_t transport;
    210 } __attribute__((packed)) btif_gattc_cb_t;
    211 
    212 typedef struct
    213 {
    214     bt_bdaddr_t bd_addr;
    215     uint16_t    min_interval;
    216     uint16_t    max_interval;
    217     uint16_t    timeout;
    218     uint16_t    latency;
    219 } btif_conn_param_cb_t;
    220 
    221 typedef struct
    222 {
    223     bt_bdaddr_t bd_addr;
    224     BOOLEAN     in_use;
    225 }__attribute__((packed)) btif_gattc_dev_t;
    226 
    227 typedef struct
    228 {
    229     btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV];
    230     uint8_t            addr_type;
    231     uint8_t            next_storage_idx;
    232 }__attribute__((packed)) btif_gattc_dev_cb_t;
    233 
    234 /*******************************************************************************
    235 **  Static variables
    236 ********************************************************************************/
    237 
    238 extern const btgatt_callbacks_t *bt_gatt_callbacks;
    239 static btif_gattc_dev_cb_t  btif_gattc_dev_cb;
    240 static btif_gattc_dev_cb_t  *p_dev_cb = &btif_gattc_dev_cb;
    241 static uint8_t rssi_request_client_if;
    242 
    243 /*******************************************************************************
    244 **  Static functions
    245 ********************************************************************************/
    246 
    247 static bt_status_t btif_gattc_multi_adv_disable(int client_if);
    248 static void btif_multi_adv_stop_cb(void *p_tle)
    249 {
    250     int client_if = ((TIMER_LIST_ENT*)p_tle)->data;
    251     btif_gattc_multi_adv_disable(client_if); // Does context switch
    252 }
    253 
    254 static void btapp_gattc_req_data(UINT16 event, char *p_dest, char *p_src)
    255 {
    256     tBTA_GATTC *p_dest_data = (tBTA_GATTC*) p_dest;
    257     tBTA_GATTC *p_src_data = (tBTA_GATTC*) p_src;
    258 
    259     if (!p_src_data || !p_dest_data)
    260        return;
    261 
    262     // Copy basic structure first
    263     memcpy(p_dest_data, p_src_data, sizeof(tBTA_GATTC));
    264 
    265     // Allocate buffer for request data if necessary
    266     switch (event)
    267     {
    268         case BTA_GATTC_READ_CHAR_EVT:
    269         case BTA_GATTC_READ_DESCR_EVT:
    270 
    271             if (p_src_data->read.p_value != NULL)
    272             {
    273                 p_dest_data->read.p_value = GKI_getbuf(sizeof(tBTA_GATT_READ_VAL));
    274 
    275                 if (p_dest_data->read.p_value != NULL)
    276                 {
    277                     memcpy(p_dest_data->read.p_value, p_src_data->read.p_value,
    278                         sizeof(tBTA_GATT_READ_VAL));
    279 
    280                     // Allocate buffer for att value if necessary
    281                     if (get_uuid16(&p_src_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
    282                       && p_src_data->read.p_value->unformat.len > 0
    283                       && p_src_data->read.p_value->unformat.p_value != NULL)
    284                     {
    285                         p_dest_data->read.p_value->unformat.p_value =
    286                                        GKI_getbuf(p_src_data->read.p_value->unformat.len);
    287                         if (p_dest_data->read.p_value->unformat.p_value != NULL)
    288                         {
    289                             memcpy(p_dest_data->read.p_value->unformat.p_value,
    290                                    p_src_data->read.p_value->unformat.p_value,
    291                                    p_src_data->read.p_value->unformat.len);
    292                         }
    293                     }
    294                 }
    295             }
    296             else
    297             {
    298                 BTIF_TRACE_WARNING("%s :Src read.p_value ptr is NULL for event  0x%x",
    299                                     __FUNCTION__, event);
    300                 p_dest_data->read.p_value = NULL;
    301 
    302             }
    303             break;
    304 
    305         default:
    306             break;
    307     }
    308 }
    309 
    310 static void btapp_gattc_free_req_data(UINT16 event, tBTA_GATTC *p_data)
    311 {
    312     switch (event)
    313     {
    314         case BTA_GATTC_READ_CHAR_EVT:
    315         case BTA_GATTC_READ_DESCR_EVT:
    316             if (p_data != NULL && p_data->read.p_value != NULL)
    317             {
    318                 if (get_uuid16 (&p_data->read.descr_type.uuid) != GATT_UUID_CHAR_AGG_FORMAT
    319                   && p_data->read.p_value->unformat.len > 0
    320                   && p_data->read.p_value->unformat.p_value != NULL)
    321                 {
    322                     GKI_freebuf(p_data->read.p_value->unformat.p_value);
    323                 }
    324                 GKI_freebuf(p_data->read.p_value);
    325             }
    326             break;
    327 
    328         default:
    329             break;
    330     }
    331 }
    332 
    333 static void btif_gattc_init_dev_cb(void)
    334 {
    335     memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t));
    336 }
    337 
    338 static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type)
    339 {
    340     BOOLEAN found=FALSE;
    341     uint8_t i;
    342     for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
    343     {
    344         if (!p_dev_cb->remote_dev[i].in_use )
    345         {
    346             memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
    347             p_dev_cb->addr_type = addr_type;
    348             p_dev_cb->remote_dev[i].in_use = TRUE;
    349             BTIF_TRACE_DEBUG("%s device added idx=%d", __FUNCTION__, i  );
    350             break;
    351         }
    352     }
    353 
    354     if ( i == BTIF_GATT_MAX_OBSERVED_DEV)
    355     {
    356         i= p_dev_cb->next_storage_idx;
    357         memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN);
    358         p_dev_cb->addr_type = addr_type;
    359         p_dev_cb->remote_dev[i].in_use = TRUE;
    360         BTIF_TRACE_DEBUG("%s device overwrite idx=%d", __FUNCTION__, i  );
    361         p_dev_cb->next_storage_idx++;
    362         if (p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV)
    363                p_dev_cb->next_storage_idx = 0;
    364     }
    365 }
    366 
    367 static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda)
    368 {
    369     uint8_t i;
    370     for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++)
    371     {
    372         if (p_dev_cb->remote_dev[i].in_use &&
    373             !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN))
    374         {
    375             return TRUE;
    376         }
    377     }
    378     return FALSE;
    379 }
    380 
    381 static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb )
    382 {
    383     uint8_t remote_name_len;
    384     uint8_t *p_eir_remote_name=NULL;
    385     bt_bdname_t bdname;
    386 
    387     p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
    388                                          BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
    389 
    390     if (p_eir_remote_name == NULL)
    391     {
    392         p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
    393                                 BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
    394     }
    395 
    396     if (p_eir_remote_name)
    397     {
    398         memcpy(bdname.name, p_eir_remote_name, remote_name_len);
    399         bdname.name[remote_name_len]='\0';
    400 
    401         BTIF_TRACE_DEBUG("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name,
    402               remote_name_len, p_btif_cb->device_type  );
    403         btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address,   bdname.name,
    404                                                p_btif_cb->device_type);
    405     }
    406 
    407     btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type);
    408 }
    409 
    410 static void btif_gattc_upstreams_evt(uint16_t event, char* p_param)
    411 {
    412     BTIF_TRACE_EVENT("%s: Event %d", __FUNCTION__, event);
    413 
    414     tBTA_GATTC *p_data = (tBTA_GATTC*) p_param;
    415     switch (event)
    416     {
    417         case BTA_GATTC_REG_EVT:
    418         {
    419             bt_uuid_t app_uuid;
    420             bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
    421             HAL_CBACK(bt_gatt_callbacks, client->register_client_cb
    422                 , p_data->reg_oper.status
    423                 , p_data->reg_oper.client_if
    424                 , &app_uuid
    425             );
    426             break;
    427         }
    428 
    429         case BTA_GATTC_DEREG_EVT:
    430             break;
    431 
    432         case BTA_GATTC_READ_CHAR_EVT:
    433         {
    434             btgatt_read_params_t data;
    435             set_read_value(&data, &p_data->read);
    436 
    437             HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb
    438                 , p_data->read.conn_id, p_data->read.status, &data);
    439             break;
    440         }
    441 
    442         case BTA_GATTC_WRITE_CHAR_EVT:
    443         case BTA_GATTC_PREP_WRITE_EVT:
    444         {
    445             btgatt_write_params_t data;
    446             bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
    447             bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
    448 
    449             HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb
    450                 , p_data->write.conn_id, p_data->write.status, &data
    451             );
    452             break;
    453         }
    454 
    455         case BTA_GATTC_EXEC_EVT:
    456         {
    457             HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb
    458                 , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status
    459             );
    460             break;
    461         }
    462 
    463         case BTA_GATTC_SEARCH_CMPL_EVT:
    464         {
    465             HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb
    466                 , p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
    467             break;
    468         }
    469 
    470         case BTA_GATTC_SEARCH_RES_EVT:
    471         {
    472             btgatt_srvc_id_t data;
    473             bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid));
    474             HAL_CBACK(bt_gatt_callbacks, client->search_result_cb
    475                 , p_data->srvc_res.conn_id, &data);
    476             break;
    477         }
    478 
    479         case BTA_GATTC_READ_DESCR_EVT:
    480         {
    481             btgatt_read_params_t data;
    482             set_read_value(&data, &p_data->read);
    483 
    484             HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb
    485                 , p_data->read.conn_id, p_data->read.status, &data);
    486             break;
    487         }
    488 
    489         case BTA_GATTC_WRITE_DESCR_EVT:
    490         {
    491             btgatt_write_params_t data;
    492             bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id);
    493             bta_to_btif_gatt_id(&data.char_id, &p_data->write.char_id);
    494             bta_to_btif_gatt_id(&data.descr_id, &p_data->write.descr_type);
    495 
    496             HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb
    497                 , p_data->write.conn_id, p_data->write.status, &data);
    498             break;
    499         }
    500 
    501         case BTA_GATTC_NOTIF_EVT:
    502         {
    503             btgatt_notify_params_t data;
    504 
    505             bdcpy(data.bda.address, p_data->notify.bda);
    506 
    507             bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id);
    508             bta_to_btif_gatt_id(&data.char_id, &p_data->notify.char_id.char_id);
    509             memcpy(data.value, p_data->notify.value, p_data->notify.len);
    510 
    511             data.is_notify = p_data->notify.is_notify;
    512             data.len = p_data->notify.len;
    513 
    514             HAL_CBACK(bt_gatt_callbacks, client->notify_cb
    515                 , p_data->notify.conn_id, &data);
    516 
    517             if (p_data->notify.is_notify == FALSE)
    518             {
    519                 BTA_GATTC_SendIndConfirm(p_data->notify.conn_id,
    520                                          &p_data->notify.char_id);
    521             }
    522             break;
    523         }
    524 
    525         case BTA_GATTC_OPEN_EVT:
    526         {
    527             bt_bdaddr_t bda;
    528             bdcpy(bda.address, p_data->open.remote_bda);
    529 
    530             HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id
    531                 , p_data->open.status, p_data->open.client_if, &bda);
    532 
    533             if (GATT_DEF_BLE_MTU_SIZE != p_data->open.mtu && p_data->open.mtu)
    534             {
    535                 HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb, p_data->open.conn_id
    536                     , p_data->open.status , p_data->open.mtu);
    537             }
    538 
    539             if (p_data->open.status == BTA_GATT_OK)
    540                 btif_gatt_check_encrypted_link(p_data->open.remote_bda);
    541             break;
    542         }
    543 
    544         case BTA_GATTC_CLOSE_EVT:
    545         {
    546             bt_bdaddr_t bda;
    547             bdcpy(bda.address, p_data->close.remote_bda);
    548             HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id
    549                 , p_data->status, p_data->close.client_if, &bda);
    550             break;
    551         }
    552 
    553         case BTA_GATTC_ACL_EVT:
    554             BTIF_TRACE_EVENT("BTA_GATTC_ACL_EVT: status = %d", p_data->status);
    555             /* Ignore for now */
    556             break;
    557 
    558         case BTA_GATTC_CANCEL_OPEN_EVT:
    559             break;
    560 
    561         case BTIF_GATT_OBSERVE_EVT:
    562         {
    563             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    564             uint8_t remote_name_len;
    565             uint8_t *p_eir_remote_name=NULL;
    566             bt_device_type_t dev_type;
    567             bt_property_t properties;
    568 
    569             p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
    570                                          BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
    571 
    572             if (p_eir_remote_name == NULL)
    573             {
    574                 p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
    575                                 BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
    576             }
    577 
    578             if ((p_btif_cb->addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name))
    579             {
    580                if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
    581                {
    582                   static const char* exclude_filter[] =
    583                         {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
    584 
    585                   btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
    586                   btif_gattc_update_properties(p_btif_cb);
    587                   btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
    588                   BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
    589                }
    590 
    591             }
    592 
    593             if (( p_btif_cb->device_type == BT_DEVICE_TYPE_DUMO)&&
    594                (p_btif_cb->flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
    595                (p_btif_cb->flag & BTA_BLE_DMT_HOST_SPT))
    596              {
    597                 btif_storage_set_dmt_support_type (&(p_btif_cb->bd_addr), TRUE);
    598              }
    599 
    600              dev_type =  p_btif_cb->device_type;
    601              BTIF_STORAGE_FILL_PROPERTY(&properties,
    602                         BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
    603              btif_storage_set_remote_device_property(&(p_btif_cb->bd_addr), &properties);
    604 
    605             HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
    606                       &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
    607             break;
    608         }
    609 
    610         case BTIF_GATTC_RSSI_EVT:
    611         {
    612             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    613             HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if,
    614                       &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status);
    615             break;
    616         }
    617 
    618         case BTA_GATTC_LISTEN_EVT:
    619         {
    620             HAL_CBACK(bt_gatt_callbacks, client->listen_cb
    621                 , p_data->reg_oper.status
    622                 , p_data->reg_oper.client_if
    623             );
    624             break;
    625         }
    626 
    627         case BTA_GATTC_CFG_MTU_EVT:
    628         {
    629             HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb, p_data->cfg_mtu.conn_id
    630                 , p_data->cfg_mtu.status , p_data->cfg_mtu.mtu);
    631             break;
    632         }
    633 
    634         case BTA_GATTC_MULT_ADV_ENB_EVT:
    635         {
    636             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    637             if (0xFF != p_btif_cb->inst_id)
    638                 btif_multi_adv_add_instid_map(p_btif_cb->client_if, p_btif_cb->inst_id, false);
    639             HAL_CBACK(bt_gatt_callbacks, client->multi_adv_enable_cb
    640                     , p_btif_cb->client_if
    641                     , p_btif_cb->status
    642                 );
    643             btif_multi_adv_timer_ctrl(p_btif_cb->client_if,
    644                     (p_btif_cb->status==0 ? btif_multi_adv_stop_cb : NULL));
    645             break;
    646         }
    647 
    648         case BTA_GATTC_MULT_ADV_UPD_EVT:
    649         {
    650             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    651             HAL_CBACK(bt_gatt_callbacks, client->multi_adv_update_cb
    652                 , p_btif_cb->client_if
    653                 , p_btif_cb->status
    654             );
    655             btif_multi_adv_timer_ctrl(p_btif_cb->client_if,
    656                     (p_btif_cb->status==0 ? btif_multi_adv_stop_cb : NULL));
    657             break;
    658         }
    659 
    660         case BTA_GATTC_MULT_ADV_DATA_EVT:
    661          {
    662             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    663             HAL_CBACK(bt_gatt_callbacks, client->multi_adv_data_cb
    664                 , p_btif_cb->client_if
    665                 , p_btif_cb->status
    666             );
    667             break;
    668         }
    669 
    670         case BTA_GATTC_MULT_ADV_DIS_EVT:
    671         {
    672             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    673             btif_gattc_clear_clientif(p_btif_cb->client_if);
    674             HAL_CBACK(bt_gatt_callbacks, client->multi_adv_disable_cb
    675                 , p_btif_cb->client_if
    676                 , p_btif_cb->status
    677             );
    678             break;
    679         }
    680 
    681         case BTA_GATTC_ADV_DATA_EVT:
    682         {
    683             btif_gattc_cleanup_inst_cb(STD_ADV_INSTID);
    684             /* No HAL callback available */
    685             break;
    686         }
    687 
    688         case BTA_GATTC_CONGEST_EVT:
    689             HAL_CBACK(bt_gatt_callbacks, client->congestion_cb
    690                 , p_data->congest.conn_id
    691                 , p_data->congest.congested
    692             );
    693             break;
    694 
    695         case BTA_GATTC_BTH_SCAN_CFG_EVT:
    696         {
    697             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    698             HAL_CBACK(bt_gatt_callbacks, client->batchscan_cfg_storage_cb
    699                 , p_data->client_if
    700                 , p_data->status
    701             );
    702             break;
    703         }
    704 
    705         case BTA_GATTC_BTH_SCAN_ENB_EVT:
    706         {
    707             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    708             HAL_CBACK(bt_gatt_callbacks, client->batchscan_enb_disable_cb
    709                     , ENABLE_BATCH_SCAN
    710                     , p_data->client_if
    711                     , p_data->status);
    712             break;
    713         }
    714 
    715         case BTA_GATTC_BTH_SCAN_DIS_EVT:
    716         {
    717             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    718             HAL_CBACK(bt_gatt_callbacks, client->batchscan_enb_disable_cb
    719                     , DISABLE_BATCH_SCAN
    720                     , p_data->client_if
    721                     , p_data->status);
    722             break;
    723         }
    724 
    725         case BTA_GATTC_BTH_SCAN_THR_EVT:
    726         {
    727             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    728             HAL_CBACK(bt_gatt_callbacks, client->batchscan_threshold_cb
    729                     , p_data->client_if);
    730             break;
    731         }
    732 
    733         case BTA_GATTC_BTH_SCAN_RD_EVT:
    734         {
    735             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    736             uint8_t *p_rep_data = NULL;
    737 
    738             if (p_data->read_reports.data_len > 0 && NULL != p_data->read_reports.p_rep_data)
    739             {
    740                 p_rep_data = GKI_getbuf(p_data->read_reports.data_len);
    741                 memcpy(p_rep_data, p_data->read_reports.p_rep_data, p_data->read_reports.data_len);
    742             }
    743 
    744             HAL_CBACK(bt_gatt_callbacks, client->batchscan_reports_cb
    745                     , p_data->client_if, p_data->status, p_data->read_reports.report_format
    746                     , p_data->read_reports.num_records, p_data->read_reports.data_len, p_rep_data);
    747             if (NULL != p_rep_data)
    748                 GKI_freebuf(p_rep_data);
    749             break;
    750         }
    751 
    752         case BTA_GATTC_SCAN_FLT_CFG_EVT:
    753         {
    754             btgatt_adv_filter_cb_t *p_btif_cb = (btgatt_adv_filter_cb_t*) p_param;
    755             HAL_CBACK(bt_gatt_callbacks, client->scan_filter_cfg_cb, p_btif_cb->action,
    756                       p_btif_cb->client_if, p_btif_cb->status, p_btif_cb->cond_op,
    757                       p_btif_cb->avbl_space);
    758             break;
    759         }
    760 
    761         case BTA_GATTC_SCAN_FLT_PARAM_EVT:
    762         {
    763             btgatt_adv_filter_cb_t *p_data = (btgatt_adv_filter_cb_t*) p_param;
    764             BTIF_TRACE_DEBUG("BTA_GATTC_SCAN_FLT_PARAM_EVT: %d, %d, %d, %d",p_data->client_if,
    765                 p_data->action, p_data->avbl_space, p_data->status);
    766             HAL_CBACK(bt_gatt_callbacks, client->scan_filter_param_cb
    767                     , p_data->action, p_data->client_if, p_data->status
    768                     , p_data->avbl_space);
    769             break;
    770         }
    771 
    772         case BTA_GATTC_SCAN_FLT_STATUS_EVT:
    773         {
    774             btgatt_adv_filter_cb_t *p_data = (btgatt_adv_filter_cb_t*) p_param;
    775             BTIF_TRACE_DEBUG("BTA_GATTC_SCAN_FLT_STATUS_EVT: %d, %d, %d",p_data->client_if,
    776                 p_data->action, p_data->status);
    777             HAL_CBACK(bt_gatt_callbacks, client->scan_filter_status_cb
    778                     , p_data->action, p_data->client_if, p_data->status);
    779             break;
    780         }
    781 
    782         case BTA_GATTC_ADV_VSC_EVT:
    783         {
    784             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    785             HAL_CBACK(bt_gatt_callbacks, client->track_adv_event_cb
    786                     ,p_data->client_if, p_data->filt_index, p_data->addr_type, &p_data->bd_addr
    787                     ,p_data->adv_state);
    788             break;
    789         }
    790 
    791         default:
    792             BTIF_TRACE_ERROR("%s: Unhandled event (%d)!", __FUNCTION__, event);
    793             break;
    794     }
    795 
    796     btapp_gattc_free_req_data(event, p_data);
    797 }
    798 
    799 static void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
    800 {
    801     bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
    802                     (uint16_t) event, (void*) p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data);
    803     ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
    804 }
    805 
    806 static void bta_gattc_multi_adv_cback(tBTA_BLE_MULTI_ADV_EVT event, UINT8 inst_id,
    807                                     void *p_ref, tBTA_STATUS call_status)
    808 {
    809     btif_gattc_cb_t btif_cb;
    810     tBTA_GATTC_EVT upevt;
    811     uint8_t client_if = 0;
    812 
    813     if (NULL == p_ref)
    814     {
    815         BTIF_TRACE_WARNING("%s Invalid p_ref received",__FUNCTION__);
    816     }
    817     else
    818     {
    819         client_if = *(UINT8 *) p_ref;
    820     }
    821 
    822     BTIF_TRACE_DEBUG("%s -Inst ID %d, Status:%x, client_if:%d",__FUNCTION__,inst_id, call_status,
    823                        client_if);
    824     btif_cb.status = call_status;
    825     btif_cb.client_if = client_if;
    826     btif_cb.inst_id = inst_id;
    827 
    828     switch(event)
    829     {
    830         case BTA_BLE_MULTI_ADV_ENB_EVT:
    831             upevt = BTA_GATTC_MULT_ADV_ENB_EVT;
    832             break;
    833 
    834         case BTA_BLE_MULTI_ADV_DISABLE_EVT:
    835             upevt = BTA_GATTC_MULT_ADV_DIS_EVT;
    836             break;
    837 
    838         case BTA_BLE_MULTI_ADV_PARAM_EVT:
    839             upevt = BTA_GATTC_MULT_ADV_UPD_EVT;
    840             break;
    841 
    842         case BTA_BLE_MULTI_ADV_DATA_EVT:
    843             upevt = BTA_GATTC_MULT_ADV_DATA_EVT;
    844             break;
    845 
    846         default:
    847             return;
    848     }
    849 
    850     bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt, (uint16_t) upevt,
    851                         (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    852     ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
    853 }
    854 
    855 static void bta_gattc_set_adv_data_cback(tBTA_STATUS call_status)
    856 {
    857     UNUSED(call_status);
    858     btif_gattc_cb_t btif_cb;
    859     btif_cb.status = call_status;
    860     btif_cb.action = 0;
    861     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_DATA_EVT,
    862                           (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    863 }
    864 
    865 static void bta_batch_scan_setup_cb (tBTA_BLE_BATCH_SCAN_EVT evt,
    866                                             tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
    867 {
    868     UINT8 upevt = 0;
    869     btgatt_batch_track_cb_t btif_scan_track_cb;
    870 
    871     btif_scan_track_cb.status = status;
    872     btif_scan_track_cb.client_if = ref_value;
    873     BTIF_TRACE_DEBUG("bta_batch_scan_setup_cb-Status:%x, client_if:%d, evt=%d",
    874             status, ref_value, evt);
    875 
    876     switch(evt)
    877     {
    878         case BTA_BLE_BATCH_SCAN_ENB_EVT:
    879         {
    880            upevt = BTA_GATTC_BTH_SCAN_ENB_EVT;
    881            break;
    882         }
    883 
    884         case BTA_BLE_BATCH_SCAN_DIS_EVT:
    885         {
    886            upevt = BTA_GATTC_BTH_SCAN_DIS_EVT;
    887            break;
    888         }
    889 
    890         case BTA_BLE_BATCH_SCAN_CFG_STRG_EVT:
    891         {
    892            upevt = BTA_GATTC_BTH_SCAN_CFG_EVT;
    893            break;
    894         }
    895 
    896         case BTA_BLE_BATCH_SCAN_DATA_EVT:
    897         {
    898            upevt = BTA_GATTC_BTH_SCAN_RD_EVT;
    899            break;
    900         }
    901 
    902         case BTA_BLE_BATCH_SCAN_THRES_EVT:
    903         {
    904            upevt = BTA_GATTC_BTH_SCAN_THR_EVT;
    905            break;
    906         }
    907 
    908         default:
    909             return;
    910     }
    911 
    912     btif_transfer_context(btif_gattc_upstreams_evt, upevt,(char*) &btif_scan_track_cb,
    913                           sizeof(btgatt_batch_track_cb_t), NULL);
    914 
    915 }
    916 
    917 static void bta_batch_scan_threshold_cb(tBTA_DM_BLE_REF_VALUE ref_value)
    918 {
    919     btgatt_batch_track_cb_t btif_scan_track_cb;
    920     btif_scan_track_cb.status = 0;
    921     btif_scan_track_cb.client_if = ref_value;
    922 
    923     BTIF_TRACE_DEBUG("%s - client_if:%d",__FUNCTION__, ref_value);
    924 
    925     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_BTH_SCAN_THR_EVT,
    926                           (char*) &btif_scan_track_cb, sizeof(btif_gattc_cb_t), NULL);
    927 }
    928 
    929 static void bta_batch_scan_reports_cb(tBTA_DM_BLE_REF_VALUE ref_value, UINT8 report_format,
    930                                             UINT8 num_records, UINT16 data_len,
    931                                             UINT8* p_rep_data, tBTA_STATUS status)
    932 {
    933     btgatt_batch_track_cb_t btif_scan_track_cb;
    934     memset(&btif_scan_track_cb, 0, sizeof(btgatt_batch_track_cb_t));
    935     BTIF_TRACE_DEBUG("%s - client_if:%d, %d, %d, %d",__FUNCTION__, ref_value, status, num_records,
    936                                     data_len);
    937 
    938     btif_scan_track_cb.status = status;
    939 
    940     btif_scan_track_cb.client_if = ref_value;
    941     btif_scan_track_cb.read_reports.report_format = report_format;
    942     btif_scan_track_cb.read_reports.data_len = data_len;
    943     btif_scan_track_cb.read_reports.num_records = num_records;
    944 
    945     if (data_len > 0)
    946     {
    947         btif_scan_track_cb.read_reports.p_rep_data = GKI_getbuf(data_len);
    948         memcpy(btif_scan_track_cb.read_reports.p_rep_data, p_rep_data, data_len);
    949         GKI_freebuf(p_rep_data);
    950     }
    951 
    952     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_BTH_SCAN_RD_EVT,
    953         (char*) &btif_scan_track_cb, sizeof(btgatt_batch_track_cb_t), NULL);
    954 
    955     if (data_len > 0)
    956         GKI_freebuf(btif_scan_track_cb.read_reports.p_rep_data);
    957 }
    958 
    959 static void bta_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
    960 {
    961     btif_gattc_cb_t btif_cb;
    962     uint8_t len;
    963 
    964     switch (event)
    965     {
    966         case BTA_DM_INQ_RES_EVT:
    967         {
    968             bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
    969             btif_cb.device_type = p_data->inq_res.device_type;
    970             btif_cb.rssi = p_data->inq_res.rssi;
    971             btif_cb.addr_type = p_data->inq_res.ble_addr_type;
    972             btif_cb.flag = p_data->inq_res.flag;
    973             if (p_data->inq_res.p_eir)
    974             {
    975                 memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
    976                 if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
    977                                       &len))
    978                 {
    979                     p_data->inq_res.remt_name_not_required  = TRUE;
    980                 }
    981             }
    982         }
    983         break;
    984 
    985         case BTA_DM_INQ_CMPL_EVT:
    986         {
    987             BTIF_TRACE_DEBUG("%s  BLE observe complete. Num Resp %d",
    988                               __FUNCTION__,p_data->inq_cmpl.num_resps);
    989             return;
    990         }
    991 
    992         default:
    993         BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
    994         return;
    995     }
    996     btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
    997                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    998 }
    999 
   1000 static void bta_track_adv_event_cb(int filt_index, tBLE_ADDR_TYPE addr_type, BD_ADDR bda,
   1001                                         int adv_state, tBTA_DM_BLE_REF_VALUE ref_value)
   1002 {
   1003     btgatt_batch_track_cb_t btif_scan_track_cb;
   1004     BTIF_TRACE_DEBUG("%s :%d, %d, %d, %d",
   1005         __FUNCTION__,filt_index, addr_type, adv_state, ref_value);
   1006     btif_scan_track_cb.filt_index = filt_index;
   1007     btif_scan_track_cb.addr_type = addr_type;
   1008     memcpy(btif_scan_track_cb.bd_addr.address, bda, sizeof(BD_ADDR));
   1009     btif_scan_track_cb.client_if = ref_value;
   1010     btif_scan_track_cb.adv_state = adv_state;
   1011     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_VSC_EVT,
   1012                           (char*) &btif_scan_track_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   1013 }
   1014 
   1015 static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
   1016 {
   1017     btif_gattc_cb_t btif_cb;
   1018 
   1019     bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
   1020     btif_cb.rssi = p_result->rssi;
   1021     btif_cb.status = p_result->status;
   1022     btif_cb.client_if = rssi_request_client_if;
   1023     btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
   1024                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1025 }
   1026 
   1027 static void bta_scan_filt_cfg_cb(tBTA_DM_BLE_PF_ACTION action, tBTA_DM_BLE_SCAN_COND_OP cfg_op,
   1028                                 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, tBTA_STATUS status,
   1029                                 tBTA_DM_BLE_REF_VALUE ref_value)
   1030 {
   1031     btgatt_adv_filter_cb_t btif_cb;
   1032     btif_cb.status = status;
   1033     btif_cb.action = action;
   1034     btif_cb.cond_op = cfg_op;
   1035     btif_cb.avbl_space = avbl_space;
   1036     btif_cb.client_if = ref_value;
   1037     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_CFG_EVT,
   1038                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   1039 }
   1040 
   1041 static void bta_scan_filt_param_setup_cb(UINT8 action_type,
   1042                                         tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
   1043                                         tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
   1044 {
   1045     btgatt_adv_filter_cb_t btif_cb;
   1046 
   1047     btif_cb.status = status;
   1048     btif_cb.action = action_type;
   1049     btif_cb.client_if = ref_value;
   1050     btif_cb.avbl_space = avbl_space;
   1051     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_PARAM_EVT,
   1052                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   1053 }
   1054 
   1055 static void bta_scan_filt_status_cb(UINT8 action, tBTA_STATUS status,
   1056                                     tBTA_DM_BLE_REF_VALUE ref_value)
   1057 {
   1058     btgatt_adv_filter_cb_t btif_cb;
   1059 
   1060     btif_cb.status = status;
   1061     btif_cb.action = action;
   1062     btif_cb.client_if = ref_value;
   1063     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_STATUS_EVT,
   1064                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   1065 }
   1066 
   1067 static void btgattc_handle_event(uint16_t event, char* p_param)
   1068 {
   1069     tBTA_GATT_STATUS           status;
   1070     tBT_UUID                   uuid;
   1071     tBTA_GATT_SRVC_ID          srvc_id;
   1072     tGATT_CHAR_PROP            out_char_prop;
   1073     tBTA_GATTC_CHAR_ID         in_char_id;
   1074     tBTA_GATTC_CHAR_ID         out_char_id;
   1075     tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
   1076     tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
   1077     tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
   1078     tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
   1079     tBTA_GATT_UNFMT            descr_val;
   1080 
   1081     btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*) p_param;
   1082     if (!p_cb) return;
   1083 
   1084     BTIF_TRACE_EVENT("%s: Event %d", __FUNCTION__, event);
   1085 
   1086     switch (event)
   1087     {
   1088         case BTIF_GATTC_REGISTER_APP:
   1089             btif_to_bta_uuid(&uuid, &p_cb->uuid);
   1090             btif_gattc_init_multi_adv_cb();
   1091             BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
   1092             break;
   1093 
   1094         case BTIF_GATTC_UNREGISTER_APP:
   1095             btif_gattc_clear_clientif(p_cb->client_if);
   1096             btif_gattc_destroy_multi_adv_cb();
   1097             BTA_GATTC_AppDeregister(p_cb->client_if);
   1098             break;
   1099 
   1100         case BTIF_GATTC_SCAN_START:
   1101             btif_gattc_init_dev_cb();
   1102             BTA_DmBleObserve(TRUE, 0, bta_scan_results_cb);
   1103             break;
   1104 
   1105         case BTIF_GATTC_SCAN_STOP:
   1106             BTA_DmBleObserve(FALSE, 0, 0);
   1107             break;
   1108 
   1109         case BTIF_GATTC_OPEN:
   1110         {
   1111             // Ensure device is in inquiry database
   1112             int addr_type = 0;
   1113             int device_type = 0;
   1114             tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
   1115 
   1116             if (btif_get_device_type(p_cb->bd_addr.address, &addr_type, &device_type) == TRUE
   1117                   && device_type != BT_DEVICE_TYPE_BREDR)
   1118                 BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
   1119 
   1120             // Mark background connections
   1121             if (!p_cb->is_direct)
   1122             {
   1123                 // Check if RPA offloading is supported, otherwise, do not start
   1124                 // background connection, since it will not connect after address
   1125                 // changes
   1126                 if (BTM_BLE_IS_RESOLVE_BDA(p_cb->bd_addr.address))
   1127                 {
   1128                     tBTM_BLE_VSC_CB vnd_capabilities;
   1129                     BTM_BleGetVendorCapabilities(&vnd_capabilities);
   1130                     if (!vnd_capabilities.rpa_offloading)
   1131                     {
   1132                         HAL_CBACK(bt_gatt_callbacks, client->open_cb, 0, BT_STATUS_UNSUPPORTED,
   1133                                         p_cb->client_if, &p_cb->bd_addr);
   1134                         return;
   1135                     }
   1136                 }
   1137                 BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
   1138             }
   1139 
   1140             switch(device_type)
   1141             {
   1142                 case BT_DEVICE_TYPE_BREDR:
   1143                     transport = BTA_GATT_TRANSPORT_BR_EDR;
   1144                     break;
   1145 
   1146                 case BT_DEVICE_TYPE_BLE:
   1147                     transport = BTA_GATT_TRANSPORT_LE;
   1148                     break;
   1149 
   1150                 case BT_DEVICE_TYPE_DUMO:
   1151                     if ((p_cb->transport == GATT_TRANSPORT_LE) &&
   1152                         (btif_storage_is_dmt_supported_device(&(p_cb->bd_addr)) == TRUE))
   1153                         transport = BTA_GATT_TRANSPORT_LE;
   1154                     else
   1155                         transport = BTA_GATT_TRANSPORT_BR_EDR;
   1156                     break;
   1157             }
   1158 
   1159             // Connect!
   1160             BTIF_TRACE_DEBUG ("BTA_GATTC_Open Transport  = %d, dev type = %d",
   1161                                 transport, device_type);
   1162             BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct, transport);
   1163             break;
   1164         }
   1165 
   1166         case BTIF_GATTC_CLOSE:
   1167             // Disconnect established connections
   1168             if (p_cb->conn_id != 0)
   1169                 BTA_GATTC_Close(p_cb->conn_id);
   1170             else
   1171                 BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
   1172 
   1173             // Cancel pending background connections (remove from whitelist)
   1174             BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
   1175             break;
   1176 
   1177         case BTIF_GATTC_SEARCH_SERVICE:
   1178         {
   1179             if (p_cb->search_all)
   1180             {
   1181                 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
   1182             } else {
   1183                 btif_to_bta_uuid(&uuid, &p_cb->uuid);
   1184                 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
   1185             }
   1186             break;
   1187         }
   1188 
   1189         case BTIF_GATTC_GET_FIRST_CHAR:
   1190         {
   1191             btgatt_gatt_id_t char_id;
   1192             btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
   1193             status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
   1194                                             &out_char_id, &out_char_prop);
   1195 
   1196             if (status == 0)
   1197                 bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
   1198 
   1199             HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
   1200                 p_cb->conn_id, status, &p_cb->srvc_id,
   1201                 &char_id, out_char_prop);
   1202             break;
   1203         }
   1204 
   1205         case BTIF_GATTC_GET_NEXT_CHAR:
   1206         {
   1207             btgatt_gatt_id_t char_id;
   1208             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1209             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1210 
   1211             status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
   1212                                             &out_char_id, &out_char_prop);
   1213 
   1214             if (status == 0)
   1215                 bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
   1216 
   1217             HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
   1218                 p_cb->conn_id, status, &p_cb->srvc_id,
   1219                 &char_id, out_char_prop);
   1220             break;
   1221         }
   1222 
   1223         case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
   1224         {
   1225             btgatt_gatt_id_t descr_id;
   1226             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1227             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1228 
   1229             status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
   1230                                                     &out_char_descr_id);
   1231 
   1232             if (status == 0)
   1233                 bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
   1234 
   1235             HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
   1236                 p_cb->conn_id, status, &p_cb->srvc_id,
   1237                 &p_cb->char_id, &descr_id);
   1238             break;
   1239         }
   1240 
   1241         case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
   1242         {
   1243             btgatt_gatt_id_t descr_id;
   1244             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
   1245             btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
   1246             btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
   1247 
   1248             status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
   1249                                         , NULL, &out_char_descr_id);
   1250 
   1251             if (status == 0)
   1252                 bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
   1253 
   1254             HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
   1255                 p_cb->conn_id, status, &p_cb->srvc_id,
   1256                 &p_cb->char_id, &descr_id);
   1257             break;
   1258         }
   1259 
   1260         case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
   1261         {
   1262             btgatt_srvc_id_t incl_srvc_id;
   1263             btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
   1264 
   1265             status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
   1266                         &srvc_id, NULL, &out_incl_svc_id);
   1267 
   1268             bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
   1269 
   1270             HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
   1271                 p_cb->conn_id, status, &p_cb->srvc_id,
   1272                 &incl_srvc_id);
   1273             break;
   1274         }
   1275 
   1276         case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
   1277         {
   1278             btgatt_srvc_id_t incl_srvc_id;
   1279             btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
   1280             btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
   1281 
   1282             status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
   1283                         &in_incl_svc_id, NULL, &out_incl_svc_id);
   1284 
   1285             bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
   1286 
   1287             HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
   1288                 p_cb->conn_id, status, &p_cb->srvc_id,
   1289                 &incl_srvc_id);
   1290             break;
   1291         }
   1292 
   1293         case BTIF_GATTC_READ_CHAR:
   1294             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1295             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1296 
   1297             BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
   1298             break;
   1299 
   1300         case BTIF_GATTC_READ_CHAR_DESCR:
   1301             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
   1302             btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
   1303             btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
   1304 
   1305             BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
   1306             break;
   1307 
   1308         case BTIF_GATTC_WRITE_CHAR:
   1309             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1310             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1311 
   1312             BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
   1313                                      p_cb->write_type,
   1314                                      p_cb->len,
   1315                                      p_cb->value,
   1316                                      p_cb->auth_req);
   1317             break;
   1318 
   1319         case BTIF_GATTC_WRITE_CHAR_DESCR:
   1320             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
   1321             btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
   1322             btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
   1323 
   1324             descr_val.len = p_cb->len;
   1325             descr_val.p_value = p_cb->value;
   1326 
   1327             BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
   1328                                      p_cb->write_type, &descr_val,
   1329                                      p_cb->auth_req);
   1330             break;
   1331 
   1332         case BTIF_GATTC_EXECUTE_WRITE:
   1333             BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
   1334             break;
   1335 
   1336         case BTIF_GATTC_REG_FOR_NOTIFICATION:
   1337             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1338             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1339 
   1340             status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
   1341                                     p_cb->bd_addr.address, &in_char_id);
   1342 
   1343             HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
   1344                 p_cb->conn_id, 1, status, &p_cb->srvc_id,
   1345                 &p_cb->char_id);
   1346             break;
   1347 
   1348         case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
   1349             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1350             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1351 
   1352             status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
   1353                                         p_cb->bd_addr.address, &in_char_id);
   1354 
   1355             HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
   1356                 p_cb->conn_id, 0, status, &p_cb->srvc_id,
   1357                 &p_cb->char_id);
   1358             break;
   1359 
   1360         case BTIF_GATTC_REFRESH:
   1361             BTA_GATTC_Refresh(p_cb->bd_addr.address);
   1362             break;
   1363 
   1364         case BTIF_GATTC_READ_RSSI:
   1365             rssi_request_client_if = p_cb->client_if;
   1366             BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
   1367             break;
   1368 
   1369         case BTIF_GATTC_SCAN_FILTER_PARAM_SETUP:
   1370         {
   1371             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1372             if (1 == p_adv_filt_cb->adv_filt_param.dely_mode)
   1373                BTA_DmBleTrackAdvertiser(p_adv_filt_cb->client_if, bta_track_adv_event_cb);
   1374             BTA_DmBleScanFilterSetup(p_adv_filt_cb->action, p_adv_filt_cb->filt_index,
   1375                 &p_adv_filt_cb->adv_filt_param, NULL, bta_scan_filt_param_setup_cb,
   1376                 p_adv_filt_cb->client_if);
   1377             break;
   1378         }
   1379 
   1380         case BTIF_GATTC_SCAN_FILTER_CONFIG:
   1381         {
   1382             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1383             tBTA_DM_BLE_PF_COND_PARAM cond;
   1384             memset(&cond, 0, sizeof(cond));
   1385 
   1386             switch (p_adv_filt_cb->filt_type)
   1387             {
   1388                 case BTA_DM_BLE_PF_ADDR_FILTER: // 0
   1389                     bdcpy(cond.target_addr.bda, p_adv_filt_cb->bd_addr.address);
   1390                     cond.target_addr.type = p_adv_filt_cb->addr_type;
   1391                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1392                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1393                                               &cond, bta_scan_filt_cfg_cb,
   1394                                               p_adv_filt_cb->client_if);
   1395                     break;
   1396 
   1397                 case BTA_DM_BLE_PF_SRVC_DATA: // 1
   1398                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1399                                             p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1400                                             NULL, bta_scan_filt_cfg_cb, p_adv_filt_cb->client_if);
   1401                     break;
   1402 
   1403                 case BTA_DM_BLE_PF_SRVC_UUID: // 2
   1404                 {
   1405                     tBTA_DM_BLE_PF_COND_MASK uuid_mask;
   1406 
   1407                     cond.srvc_uuid.p_target_addr = NULL;
   1408                     cond.srvc_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
   1409                     btif_to_bta_uuid(&cond.srvc_uuid.uuid, &p_adv_filt_cb->uuid);
   1410 
   1411                     cond.srvc_uuid.p_uuid_mask = NULL;
   1412                     if (p_adv_filt_cb->has_mask)
   1413                     {
   1414                         btif_to_bta_uuid_mask(&uuid_mask, &p_adv_filt_cb->uuid_mask);
   1415                         cond.srvc_uuid.p_uuid_mask = &uuid_mask;
   1416                     }
   1417                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1418                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1419                                               &cond, bta_scan_filt_cfg_cb,
   1420                                               p_adv_filt_cb->client_if);
   1421                     break;
   1422                 }
   1423 
   1424                 case BTA_DM_BLE_PF_SRVC_SOL_UUID: // 3
   1425                 {
   1426                     cond.solicitate_uuid.p_target_addr = NULL;
   1427                     cond.solicitate_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
   1428                     btif_to_bta_uuid(&cond.solicitate_uuid.uuid, &p_adv_filt_cb->uuid);
   1429                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1430                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1431                                               &cond, bta_scan_filt_cfg_cb,
   1432                                               p_adv_filt_cb->client_if);
   1433                     break;
   1434                 }
   1435 
   1436                 case BTA_DM_BLE_PF_LOCAL_NAME: // 4
   1437                 {
   1438                     cond.local_name.data_len = p_adv_filt_cb->value_len;
   1439                     cond.local_name.p_data = p_adv_filt_cb->value;
   1440                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1441                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1442                                               &cond, bta_scan_filt_cfg_cb,
   1443                                               p_adv_filt_cb->client_if);
   1444                     break;
   1445                 }
   1446 
   1447                 case BTA_DM_BLE_PF_MANU_DATA: // 5
   1448                 {
   1449                     cond.manu_data.company_id = p_adv_filt_cb->conn_id;
   1450                     cond.manu_data.company_id_mask = p_adv_filt_cb->company_id_mask;
   1451                     cond.manu_data.data_len = p_adv_filt_cb->value_len;
   1452                     cond.manu_data.p_pattern = p_adv_filt_cb->value;
   1453                     cond.manu_data.p_pattern_mask = p_adv_filt_cb->value_mask;
   1454                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1455                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1456                                               &cond, bta_scan_filt_cfg_cb,
   1457                                               p_adv_filt_cb->client_if);
   1458                     break;
   1459                 }
   1460 
   1461                 case BTA_DM_BLE_PF_SRVC_DATA_PATTERN: //6
   1462                 {
   1463                     cond.srvc_data.data_len = p_adv_filt_cb->value_len;
   1464                     cond.srvc_data.p_pattern = p_adv_filt_cb->value;
   1465                     cond.srvc_data.p_pattern_mask = p_adv_filt_cb->value_mask;
   1466                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1467                                                 p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1468                                                 &cond, bta_scan_filt_cfg_cb,
   1469                                                 p_adv_filt_cb->client_if);
   1470                    break;
   1471                 }
   1472 
   1473                 default:
   1474                     BTIF_TRACE_ERROR("%s: Unknown filter type (%d)!", __FUNCTION__, p_cb->action);
   1475                     break;
   1476             }
   1477             break;
   1478         }
   1479 
   1480         case BTIF_GATTC_SCAN_FILTER_CLEAR:
   1481         {
   1482             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1483             BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_CLEAR, BTA_DM_BLE_PF_TYPE_ALL,
   1484                                         p_adv_filt_cb->filt_index, NULL, bta_scan_filt_cfg_cb,
   1485                                         p_adv_filt_cb->client_if);
   1486             break;
   1487         }
   1488 
   1489         case BTIF_GATTC_SCAN_FILTER_ENABLE:
   1490         {
   1491             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1492             BTA_DmEnableScanFilter(p_adv_filt_cb->action, bta_scan_filt_status_cb,
   1493                                    p_adv_filt_cb->client_if);
   1494             break;
   1495         }
   1496 
   1497         case BTIF_GATTC_LISTEN:
   1498 #if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
   1499             BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
   1500 #else
   1501             BTA_GATTC_Broadcast(p_cb->client_if, p_cb->start);
   1502 #endif
   1503             break;
   1504 
   1505         case BTIF_GATTC_SET_ADV_DATA:
   1506         {
   1507             btif_adv_data_t *p_adv_data = (btif_adv_data_t*) p_param;
   1508             int cbindex = CLNT_IF_IDX;
   1509             if (cbindex >= 0 && NULL != p_adv_data)
   1510             {
   1511                 btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1512                 if (!btif_gattc_copy_datacb(cbindex, p_adv_data, false))
   1513                     return;
   1514 
   1515                 if (!p_adv_data->set_scan_rsp)
   1516                 {
   1517                     BTA_DmBleSetAdvConfig(p_multi_adv_data_cb->inst_cb[cbindex].mask,
   1518                         &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
   1519                 }
   1520                 else
   1521                 {
   1522                     BTA_DmBleSetScanRsp(p_multi_adv_data_cb->inst_cb[cbindex].mask,
   1523                         &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
   1524                 }
   1525                 break;
   1526             }
   1527         }
   1528 
   1529         case BTIF_GATTC_ADV_INSTANCE_ENABLE:
   1530         {
   1531             btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
   1532 
   1533             int cbindex = -1, arrindex = -1;
   1534 
   1535             arrindex = btif_multi_adv_add_instid_map(p_inst_cb->client_if,INVALID_ADV_INST, true);
   1536             if (arrindex >= 0)
   1537                 cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
   1538 
   1539             if (cbindex >= 0 && arrindex >= 0)
   1540             {
   1541                 btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1542                 memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param,
   1543                        &p_inst_cb->param, sizeof(tBTA_BLE_ADV_PARAMS));
   1544                 p_multi_adv_data_cb->inst_cb[cbindex].timeout_s = p_inst_cb->timeout_s;
   1545                 BTIF_TRACE_DEBUG("%s, client_if value: %d", __FUNCTION__,
   1546                             p_multi_adv_data_cb->clntif_map[arrindex + arrindex]);
   1547                 BTA_BleEnableAdvInstance(&(p_multi_adv_data_cb->inst_cb[cbindex].param),
   1548                     bta_gattc_multi_adv_cback,
   1549                     &(p_multi_adv_data_cb->clntif_map[arrindex + arrindex]));
   1550             }
   1551             else
   1552             {
   1553                 /* let the error propagate up from BTA layer */
   1554                 BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_ENABLE_ADV",__FUNCTION__);
   1555                 BTA_BleEnableAdvInstance(&p_inst_cb->param, bta_gattc_multi_adv_cback, NULL);
   1556             }
   1557             break;
   1558         }
   1559 
   1560         case BTIF_GATTC_ADV_INSTANCE_UPDATE:
   1561         {
   1562             btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
   1563             int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
   1564             int cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
   1565             if (inst_id >= 0 && cbindex >= 0 && NULL != p_inst_cb)
   1566             {
   1567                 btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1568                 memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param, &p_inst_cb->param,
   1569                         sizeof(tBTA_BLE_ADV_PARAMS));
   1570                 BTA_BleUpdateAdvInstParam((UINT8)inst_id,
   1571                     &(p_multi_adv_data_cb->inst_cb[cbindex].param));
   1572             }
   1573             else
   1574                 BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_UPDATE_ADV", __FUNCTION__);
   1575             break;
   1576         }
   1577 
   1578         case BTIF_GATTC_ADV_INSTANCE_SET_DATA:
   1579         {
   1580             btif_adv_data_t *p_adv_data = (btif_adv_data_t*) p_param;
   1581             int cbindex = btif_gattc_obtain_idx_for_datacb(p_adv_data->client_if, CLNT_IF_IDX);
   1582             int inst_id = btif_multi_adv_instid_for_clientif(p_adv_data->client_if);
   1583             if (inst_id < 0 || cbindex < 0)
   1584             {
   1585                BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_SETADV_INST_DATA", __FUNCTION__);
   1586                return;
   1587             }
   1588 
   1589             if (!btif_gattc_copy_datacb(cbindex, p_adv_data, true))
   1590                 return;
   1591 
   1592             btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1593             BTA_BleCfgAdvInstData((UINT8)inst_id, p_multi_adv_data_cb->inst_cb[cbindex].is_scan_rsp,
   1594                       p_multi_adv_data_cb->inst_cb[cbindex].mask,
   1595                       &p_multi_adv_data_cb->inst_cb[cbindex].data);
   1596             break;
   1597         }
   1598 
   1599         case BTIF_GATTC_ADV_INSTANCE_DISABLE:
   1600         {
   1601             btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
   1602             int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
   1603             if (inst_id >=0)
   1604                 BTA_BleDisableAdvInstance((UINT8)inst_id);
   1605             else
   1606                 BTIF_TRACE_ERROR("%s invalid instance ID in BTIF_GATTC_DISABLE_ADV",__FUNCTION__);
   1607             break;
   1608         }
   1609 
   1610         case BTIF_GATTC_CONFIGURE_MTU:
   1611             BTA_GATTC_ConfigureMTU(p_cb->conn_id, p_cb->len);
   1612             break;
   1613 
   1614         case BTIF_GATTC_CONN_PARAM_UPDT:
   1615         {
   1616             btif_conn_param_cb_t *p_conn_param_cb = (btif_conn_param_cb_t*) p_param;
   1617             if (BTA_DmGetConnectionState(p_conn_param_cb->bd_addr.address))
   1618             {
   1619                 BTA_DmBleUpdateConnectionParams(p_conn_param_cb->bd_addr.address,
   1620                                p_conn_param_cb->min_interval, p_conn_param_cb->max_interval,
   1621                                p_conn_param_cb->latency, p_conn_param_cb->timeout);
   1622             } else {
   1623                 BTA_DmSetBlePrefConnParams(p_conn_param_cb->bd_addr.address,
   1624                                p_conn_param_cb->min_interval, p_conn_param_cb->max_interval,
   1625                                p_conn_param_cb->latency, p_conn_param_cb->timeout);
   1626             }
   1627             break;
   1628         }
   1629 
   1630         case BTIF_GATTC_SET_SCAN_PARAMS:
   1631             BTM_BleSetScanParams(p_cb->scan_interval, p_cb->scan_window, BTM_BLE_SCAN_MODE_ACTI);
   1632             break;
   1633 
   1634         case BTIF_GATTC_CONFIG_STORAGE_PARAMS:
   1635         {
   1636             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1637             BTA_DmBleSetStorageParams(p_scan_track_cb->batch_scan_full_max,
   1638                p_scan_track_cb->batch_scan_trunc_max, p_scan_track_cb->batch_scan_notify_threshold,
   1639                bta_batch_scan_setup_cb, bta_batch_scan_threshold_cb, bta_batch_scan_reports_cb,
   1640                (tBTA_DM_BLE_REF_VALUE) p_scan_track_cb->client_if);
   1641             break;
   1642         }
   1643 
   1644         case BTIF_GATTC_ENABLE_BATCH_SCAN:
   1645         {
   1646             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1647             BTA_DmBleEnableBatchScan(p_scan_track_cb->scan_mode, p_scan_track_cb->scan_interval,
   1648                p_scan_track_cb->scan_window, p_scan_track_cb->discard_rule,
   1649                p_scan_track_cb->addr_type, p_scan_track_cb->client_if);
   1650             break;
   1651         }
   1652 
   1653         case BTIF_GATTC_DISABLE_BATCH_SCAN:
   1654         {
   1655             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1656             BTA_DmBleDisableBatchScan(p_scan_track_cb->client_if);
   1657             break;
   1658         }
   1659 
   1660         case BTIF_GATTC_READ_BATCH_SCAN_REPORTS:
   1661         {
   1662             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1663             BTA_DmBleReadScanReports(p_scan_track_cb->scan_mode, p_scan_track_cb->client_if);
   1664             break;
   1665         }
   1666 
   1667         default:
   1668             BTIF_TRACE_ERROR("%s: Unknown event (%d)!", __FUNCTION__, event);
   1669             break;
   1670     }
   1671 }
   1672 
   1673 /*******************************************************************************
   1674 **  Client API Functions
   1675 ********************************************************************************/
   1676 
   1677 static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
   1678 {
   1679     CHECK_BTGATT_INIT();
   1680     btif_gattc_cb_t btif_cb;
   1681     memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
   1682     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
   1683                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1684 }
   1685 
   1686 static bt_status_t btif_gattc_unregister_app(int client_if )
   1687 {
   1688     CHECK_BTGATT_INIT();
   1689     btif_gattc_cb_t btif_cb;
   1690     btif_cb.client_if = (uint8_t) client_if;
   1691     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
   1692                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1693 }
   1694 
   1695 static bt_status_t btif_gattc_scan( bool start )
   1696 {
   1697     CHECK_BTGATT_INIT();
   1698     btif_gattc_cb_t btif_cb;
   1699     return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
   1700                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1701 }
   1702 
   1703 static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr,
   1704                                         bool is_direct,int transport)
   1705 {
   1706     CHECK_BTGATT_INIT();
   1707     btif_gattc_cb_t btif_cb;
   1708     btif_cb.client_if = (uint8_t) client_if;
   1709     btif_cb.is_direct = is_direct ? 1 : 0;
   1710     btif_cb.transport = (btgatt_transport_t)transport;
   1711     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1712     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
   1713                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1714 }
   1715 
   1716 static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
   1717 {
   1718     CHECK_BTGATT_INIT();
   1719     btif_gattc_cb_t btif_cb;
   1720     btif_cb.client_if = (uint8_t) client_if;
   1721     btif_cb.conn_id = (uint16_t) conn_id;
   1722     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1723     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
   1724                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1725 }
   1726 
   1727 static bt_status_t btif_gattc_listen(int client_if, bool start)
   1728 {
   1729     CHECK_BTGATT_INIT();
   1730     btif_gattc_cb_t btif_cb;
   1731     btif_cb.client_if = (uint8_t) client_if;
   1732     btif_cb.start = start ? 1 : 0;
   1733     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_LISTEN,
   1734                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1735 }
   1736 
   1737 static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
   1738                 bool include_txpower, int min_interval, int max_interval, int appearance,
   1739                 uint16_t manufacturer_len, char* manufacturer_data,
   1740                 uint16_t service_data_len, char* service_data,
   1741                 uint16_t service_uuid_len, char* service_uuid)
   1742 {
   1743     CHECK_BTGATT_INIT();
   1744     bt_status_t status =0;
   1745 
   1746     btif_adv_data_t adv_data;
   1747 
   1748     btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name,
   1749         include_txpower, min_interval, max_interval, appearance, manufacturer_len,
   1750         manufacturer_data, service_data_len, service_data, service_uuid_len, service_uuid,
   1751         &adv_data);
   1752 
   1753     status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
   1754                        (char*) &adv_data, sizeof(btif_adv_data_t), NULL);
   1755 
   1756     if (NULL != adv_data.p_service_data)
   1757         GKI_freebuf(adv_data.p_service_data);
   1758 
   1759     if (NULL != adv_data.p_service_uuid)
   1760         GKI_freebuf(adv_data.p_service_uuid);
   1761 
   1762     if (NULL != adv_data.p_manufacturer_data)
   1763         GKI_freebuf(adv_data.p_manufacturer_data);
   1764 
   1765     return status;
   1766 }
   1767 
   1768 static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
   1769 {
   1770     CHECK_BTGATT_INIT();
   1771     btif_gattc_cb_t btif_cb;
   1772     btif_cb.client_if = (uint8_t) client_if;
   1773     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1774     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
   1775                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1776 }
   1777 
   1778 static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
   1779 {
   1780     CHECK_BTGATT_INIT();
   1781     btif_gattc_cb_t btif_cb;
   1782     btif_cb.conn_id = (uint16_t) conn_id;
   1783     btif_cb.search_all = filter_uuid ? 0 : 1;
   1784     if (filter_uuid)
   1785         memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
   1786     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
   1787                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1788 }
   1789 
   1790 static bt_status_t btif_gattc_get_characteristic( int conn_id
   1791         , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
   1792 {
   1793     CHECK_BTGATT_INIT();
   1794     btif_gattc_cb_t btif_cb;
   1795     btif_cb.conn_id = (uint16_t) conn_id;
   1796     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1797     if (start_char_id)
   1798     {
   1799         memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_gatt_id_t));
   1800         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
   1801                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1802     }
   1803     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
   1804                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1805 }
   1806 
   1807 static bt_status_t btif_gattc_get_descriptor( int conn_id
   1808         , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id
   1809         , btgatt_gatt_id_t *start_descr_id)
   1810 {
   1811     CHECK_BTGATT_INIT();
   1812     btif_gattc_cb_t btif_cb;
   1813     btif_cb.conn_id = (uint16_t) conn_id;
   1814     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1815     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1816     if (start_descr_id)
   1817     {
   1818         memcpy(&btif_cb.descr_id, start_descr_id, sizeof(btgatt_gatt_id_t));
   1819         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
   1820                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1821     }
   1822 
   1823     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
   1824                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1825 }
   1826 
   1827 static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
   1828                                                    btgatt_srvc_id_t *start_incl_srvc_id)
   1829 {
   1830     CHECK_BTGATT_INIT();
   1831     btif_gattc_cb_t btif_cb;
   1832     btif_cb.conn_id = (uint16_t) conn_id;
   1833     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1834     if (start_incl_srvc_id)
   1835     {
   1836         memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
   1837         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
   1838                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1839     }
   1840     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
   1841                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1842 }
   1843 
   1844 static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
   1845                                         btgatt_gatt_id_t* char_id, int auth_req )
   1846 {
   1847     CHECK_BTGATT_INIT();
   1848     btif_gattc_cb_t btif_cb;
   1849     btif_cb.conn_id = (uint16_t) conn_id;
   1850     btif_cb.auth_req = (uint8_t) auth_req;
   1851     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1852     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1853     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
   1854                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1855 }
   1856 
   1857 static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
   1858                                               btgatt_gatt_id_t* char_id,
   1859                                               btgatt_gatt_id_t* descr_id,
   1860                                               int auth_req )
   1861 {
   1862     CHECK_BTGATT_INIT();
   1863     btif_gattc_cb_t btif_cb;
   1864     btif_cb.conn_id = (uint16_t) conn_id;
   1865     btif_cb.auth_req = (uint8_t) auth_req;
   1866     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1867     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1868     memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
   1869     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
   1870                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1871 }
   1872 
   1873 static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
   1874                                          btgatt_gatt_id_t* char_id, int write_type,
   1875                                          int len, int auth_req, char* p_value)
   1876 {
   1877     CHECK_BTGATT_INIT();
   1878     btif_gattc_cb_t btif_cb;
   1879     btif_cb.conn_id = (uint16_t) conn_id;
   1880     btif_cb.auth_req = (uint8_t) auth_req;
   1881     btif_cb.write_type = (uint8_t) write_type;
   1882     btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
   1883     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1884     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1885     memcpy(btif_cb.value, p_value, btif_cb.len);
   1886     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
   1887                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1888 }
   1889 
   1890 static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
   1891                                                btgatt_gatt_id_t* char_id,
   1892                                                btgatt_gatt_id_t* descr_id,
   1893                                                int write_type, int len, int auth_req,
   1894                                                char* p_value)
   1895 {
   1896     CHECK_BTGATT_INIT();
   1897     btif_gattc_cb_t btif_cb;
   1898     btif_cb.conn_id = (uint16_t) conn_id;
   1899     btif_cb.auth_req = (uint8_t) auth_req;
   1900     btif_cb.write_type = (uint8_t) write_type;
   1901     btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
   1902     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1903     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1904     memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
   1905     memcpy(btif_cb.value, p_value, btif_cb.len);
   1906     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
   1907                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1908 }
   1909 
   1910 static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
   1911 {
   1912     CHECK_BTGATT_INIT();
   1913     btif_gattc_cb_t btif_cb;
   1914     btif_cb.conn_id = (uint16_t) conn_id;
   1915     btif_cb.action = (uint8_t) execute;
   1916     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
   1917                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1918 }
   1919 
   1920 static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
   1921                                                    btgatt_srvc_id_t* srvc_id,
   1922                                                    btgatt_gatt_id_t* char_id)
   1923 {
   1924     CHECK_BTGATT_INIT();
   1925     btif_gattc_cb_t btif_cb;
   1926     btif_cb.client_if = (uint8_t) client_if;
   1927     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1928     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1929     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1930     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
   1931                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1932 }
   1933 
   1934 static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
   1935                                                      btgatt_srvc_id_t* srvc_id,
   1936                                                      btgatt_gatt_id_t* char_id)
   1937 {
   1938     CHECK_BTGATT_INIT();
   1939     btif_gattc_cb_t btif_cb;
   1940     btif_cb.client_if = (uint8_t) client_if;
   1941     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1942     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1943     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1944     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
   1945                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1946 }
   1947 
   1948 static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
   1949 {
   1950     CHECK_BTGATT_INIT();
   1951     btif_gattc_cb_t btif_cb;
   1952     btif_cb.client_if = (uint8_t) client_if;
   1953     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1954     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
   1955                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1956 }
   1957 
   1958 static bt_status_t btif_gattc_configure_mtu(int conn_id, int mtu)
   1959 {
   1960     CHECK_BTGATT_INIT();
   1961     btif_gattc_cb_t btif_cb;
   1962     btif_cb.conn_id = conn_id;
   1963     btif_cb.len = mtu; // Re-use len field
   1964     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIGURE_MTU,
   1965                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1966 }
   1967 
   1968 static bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t *bd_addr, int min_interval,
   1969                     int max_interval, int latency, int timeout)
   1970 {
   1971     CHECK_BTGATT_INIT();
   1972     btif_conn_param_cb_t btif_cb;
   1973     btif_cb.min_interval = min_interval;
   1974     btif_cb.max_interval = max_interval;
   1975     btif_cb.latency = latency;
   1976     btif_cb.timeout = timeout;
   1977     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1978     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONN_PARAM_UPDT,
   1979                                  (char*) &btif_cb, sizeof(btif_conn_param_cb_t), NULL);
   1980 }
   1981 
   1982 static bt_status_t btif_gattc_scan_filter_param_setup(int client_if, int action,
   1983     int filt_index, int feat_seln, int list_logic_type, int filt_logic_type, int rssi_high_thres,
   1984     int rssi_low_thres, int dely_mode, int found_timeout, int lost_timeout, int found_timeout_cnt)
   1985 {
   1986     CHECK_BTGATT_INIT();
   1987     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
   1988     btgatt_adv_filter_cb_t btif_filt_cb;
   1989     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   1990     btif_filt_cb.action = action;
   1991     btif_filt_cb.client_if = client_if;
   1992     btif_filt_cb.filt_index = filt_index;
   1993     btif_filt_cb.adv_filt_param.feat_seln = feat_seln;
   1994     btif_filt_cb.adv_filt_param.list_logic_type = list_logic_type;
   1995     btif_filt_cb.adv_filt_param.filt_logic_type = filt_logic_type;
   1996     btif_filt_cb.adv_filt_param.rssi_high_thres = rssi_high_thres;
   1997     btif_filt_cb.adv_filt_param.rssi_low_thres = rssi_low_thres;
   1998     btif_filt_cb.adv_filt_param.dely_mode = dely_mode;
   1999     btif_filt_cb.adv_filt_param.found_timeout = found_timeout;
   2000     btif_filt_cb.adv_filt_param.lost_timeout = lost_timeout;
   2001     btif_filt_cb.adv_filt_param.found_timeout_cnt = found_timeout_cnt;
   2002     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_PARAM_SETUP,
   2003                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2004 }
   2005 
   2006 static bt_status_t btif_gattc_scan_filter_add_remove(int client_if, int action,
   2007                               int filt_type, int filt_index, int company_id,
   2008                               int company_id_mask, const bt_uuid_t *p_uuid,
   2009                               const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
   2010                               char addr_type, int data_len, char* p_data, int mask_len,
   2011                               char* p_mask)
   2012 {
   2013     CHECK_BTGATT_INIT();
   2014     btgatt_adv_filter_cb_t btif_filt_cb;
   2015     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   2016     BTIF_TRACE_DEBUG("%s, %d, %d", __FUNCTION__, action, filt_type);
   2017 
   2018     /* If data is passed, both mask and data have to be the same length */
   2019     if (data_len != mask_len && NULL != p_data && NULL != p_mask)
   2020         return BT_STATUS_PARM_INVALID;
   2021 
   2022     btif_filt_cb.client_if = client_if;
   2023     btif_filt_cb.action = action;
   2024     btif_filt_cb.filt_index = filt_index;
   2025     btif_filt_cb.filt_type = filt_type;
   2026     btif_filt_cb.conn_id = company_id;
   2027     btif_filt_cb.company_id_mask = company_id_mask ? company_id_mask : 0xFFFF;
   2028     if (bd_addr)
   2029         bdcpy(btif_filt_cb.bd_addr.address, bd_addr->address);
   2030 
   2031     btif_filt_cb.addr_type = addr_type;
   2032     btif_filt_cb.has_mask = (p_uuid_mask != NULL);
   2033 
   2034     if (p_uuid != NULL)
   2035         memcpy(&btif_filt_cb.uuid, p_uuid, sizeof(bt_uuid_t));
   2036     if (p_uuid_mask != NULL)
   2037         memcpy(&btif_filt_cb.uuid_mask, p_uuid_mask, sizeof(bt_uuid_t));
   2038     if (p_data != NULL && data_len != 0)
   2039     {
   2040         memcpy(btif_filt_cb.value, p_data, data_len);
   2041         btif_filt_cb.value_len = data_len;
   2042         memcpy(btif_filt_cb.value_mask, p_mask, mask_len);
   2043         btif_filt_cb.value_mask_len = mask_len;
   2044     }
   2045     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
   2046                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2047 }
   2048 
   2049 static bt_status_t btif_gattc_scan_filter_clear(int client_if, int filt_index)
   2050 {
   2051     CHECK_BTGATT_INIT();
   2052     BTIF_TRACE_DEBUG("%s, %d", __FUNCTION__, filt_index);
   2053 
   2054     btgatt_adv_filter_cb_t btif_filt_cb;
   2055     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   2056     btif_filt_cb.client_if = client_if;
   2057     btif_filt_cb.filt_index = filt_index;
   2058     btif_filt_cb.action = BTA_DM_BLE_SCAN_COND_CLEAR;
   2059     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
   2060                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2061 }
   2062 
   2063 static bt_status_t btif_gattc_scan_filter_enable(int client_if, bool enable)
   2064 {
   2065     int action = 0;
   2066     CHECK_BTGATT_INIT();
   2067     BTIF_TRACE_DEBUG("%s, %d", __FUNCTION__, enable);
   2068 
   2069     btgatt_adv_filter_cb_t btif_filt_cb;
   2070     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   2071     btif_filt_cb.client_if = client_if;
   2072     if (true == enable)
   2073         action = 1;
   2074     btif_filt_cb.action = action;
   2075     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_ENABLE,
   2076                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2077 }
   2078 
   2079 static bt_status_t btif_gattc_set_scan_parameters(int scan_interval, int scan_window)
   2080 {
   2081     CHECK_BTGATT_INIT();
   2082     btif_gattc_cb_t btif_cb;
   2083     btif_cb.scan_interval = scan_interval;
   2084     btif_cb.scan_window = scan_window;
   2085     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_SCAN_PARAMS,
   2086                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   2087 }
   2088 
   2089 static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
   2090 {
   2091     int device_type = 0;
   2092     char bd_addr_str[18] = {0};
   2093 
   2094     bd2str(bd_addr, &bd_addr_str);
   2095     if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
   2096         return device_type;
   2097     return 0;
   2098 }
   2099 
   2100 static bt_status_t btif_gattc_multi_adv_enable(int client_if, int min_interval, int max_interval,
   2101                                             int adv_type, int chnl_map, int tx_power, int timeout_s)
   2102 {
   2103     CHECK_BTGATT_INIT();
   2104     btgatt_multi_adv_inst_cb adv_cb;
   2105     memset(&adv_cb, 0, sizeof(btgatt_multi_adv_inst_cb));
   2106     adv_cb.client_if = (uint8_t) client_if;
   2107 
   2108     adv_cb.param.adv_int_min = min_interval;
   2109     adv_cb.param.adv_int_max = max_interval;
   2110     adv_cb.param.adv_type = adv_type;
   2111     adv_cb.param.channel_map = chnl_map;
   2112     adv_cb.param.adv_filter_policy = 0;
   2113     adv_cb.param.tx_power = tx_power;
   2114     adv_cb.timeout_s = timeout_s;
   2115     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_ENABLE,
   2116                              (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
   2117 }
   2118 
   2119 static bt_status_t btif_gattc_multi_adv_update(int client_if, int min_interval, int max_interval,
   2120                                             int adv_type, int chnl_map,int tx_power, int timeout_s)
   2121 {
   2122     CHECK_BTGATT_INIT();
   2123     btgatt_multi_adv_inst_cb adv_cb;
   2124     memset(&adv_cb, 0, sizeof(btgatt_multi_adv_inst_cb));
   2125     adv_cb.client_if = (uint8_t) client_if;
   2126 
   2127     adv_cb.param.adv_int_min = min_interval;
   2128     adv_cb.param.adv_int_max = max_interval;
   2129     adv_cb.param.adv_type = adv_type;
   2130     adv_cb.param.channel_map = chnl_map;
   2131     adv_cb.param.adv_filter_policy = 0;
   2132     adv_cb.param.tx_power = tx_power;
   2133     adv_cb.timeout_s = timeout_s;
   2134     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_UPDATE,
   2135                          (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
   2136 }
   2137 
   2138 static bt_status_t btif_gattc_multi_adv_setdata(int client_if, bool set_scan_rsp,
   2139                 bool include_name, bool incl_txpower, int appearance,
   2140                 int manufacturer_len, char* manufacturer_data,
   2141                 int service_data_len, char* service_data,
   2142                 int service_uuid_len, char* service_uuid)
   2143 {
   2144     CHECK_BTGATT_INIT();
   2145 
   2146     int min_interval = 0, max_interval = 0;
   2147     bt_status_t status =0;
   2148 
   2149     btif_adv_data_t multi_adv_data_inst;
   2150     memset(&multi_adv_data_inst, 0, sizeof(btif_adv_data_t));
   2151 
   2152     btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name, incl_txpower,
   2153         min_interval, max_interval, appearance, manufacturer_len, manufacturer_data,
   2154         service_data_len, service_data, service_uuid_len, service_uuid, &multi_adv_data_inst);
   2155 
   2156     status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_SET_DATA,
   2157                        (char*) &multi_adv_data_inst, sizeof(btif_adv_data_t), NULL);
   2158 
   2159     if (NULL != multi_adv_data_inst.p_service_data)
   2160         GKI_freebuf(multi_adv_data_inst.p_service_data);
   2161 
   2162     if (NULL != multi_adv_data_inst.p_service_uuid)
   2163         GKI_freebuf(multi_adv_data_inst.p_service_uuid);
   2164 
   2165     if (NULL != multi_adv_data_inst.p_manufacturer_data)
   2166         GKI_freebuf(multi_adv_data_inst.p_manufacturer_data);
   2167 
   2168     return status;
   2169 }
   2170 
   2171 static bt_status_t btif_gattc_multi_adv_disable(int client_if)
   2172 {
   2173     CHECK_BTGATT_INIT();
   2174     btgatt_multi_adv_inst_cb adv_cb;
   2175     memset(&adv_cb, 0, sizeof(btgatt_multi_adv_inst_cb));
   2176     adv_cb.client_if = (uint8_t) client_if;
   2177 
   2178     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_DISABLE,
   2179                            (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
   2180 }
   2181 
   2182 static bt_status_t btif_gattc_cfg_storage(int client_if,int batch_scan_full_max,
   2183     int batch_scan_trunc_max, int batch_scan_notify_threshold)
   2184 {
   2185     CHECK_BTGATT_INIT();
   2186     btgatt_batch_track_cb_t bt_scan_cb;
   2187     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2188     bt_scan_cb.client_if = (uint8_t) client_if;
   2189     bt_scan_cb.batch_scan_full_max = batch_scan_full_max;
   2190     bt_scan_cb.batch_scan_trunc_max = batch_scan_trunc_max;
   2191     bt_scan_cb.batch_scan_notify_threshold = batch_scan_notify_threshold;
   2192     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIG_STORAGE_PARAMS,
   2193                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2194 }
   2195 
   2196 static bt_status_t btif_gattc_enb_batch_scan(int client_if,int scan_mode, int scan_interval,
   2197                 int scan_window, int addr_type, int discard_rule)
   2198 {
   2199     CHECK_BTGATT_INIT();
   2200     btgatt_batch_track_cb_t bt_scan_cb;
   2201     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2202     bt_scan_cb.client_if = (uint8_t) client_if;
   2203     bt_scan_cb.scan_mode = scan_mode;
   2204     bt_scan_cb.scan_interval = scan_interval;
   2205     bt_scan_cb.scan_window = scan_window;
   2206     bt_scan_cb.discard_rule = discard_rule;
   2207     bt_scan_cb.addr_type = addr_type;
   2208     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ENABLE_BATCH_SCAN,
   2209                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2210 }
   2211 
   2212 static bt_status_t btif_gattc_dis_batch_scan(int client_if)
   2213 {
   2214     CHECK_BTGATT_INIT();
   2215     btgatt_batch_track_cb_t bt_scan_cb;
   2216     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2217     bt_scan_cb.client_if = (uint8_t) client_if;
   2218     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DISABLE_BATCH_SCAN,
   2219                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2220 }
   2221 
   2222 static bt_status_t btif_gattc_read_batch_scan_reports(int client_if, int scan_mode)
   2223 {
   2224     CHECK_BTGATT_INIT();
   2225     btgatt_batch_track_cb_t bt_scan_cb;
   2226     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2227     bt_scan_cb.client_if = (uint8_t) client_if;
   2228     bt_scan_cb.scan_mode = scan_mode;
   2229     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_BATCH_SCAN_REPORTS,
   2230                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2231 }
   2232 
   2233 extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
   2234 
   2235 static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
   2236 {
   2237     return btif_gattc_test_command_impl(command, params);
   2238 }
   2239 
   2240 
   2241 const btgatt_client_interface_t btgattClientInterface = {
   2242     btif_gattc_register_app,
   2243     btif_gattc_unregister_app,
   2244     btif_gattc_scan,
   2245     btif_gattc_open,
   2246     btif_gattc_close,
   2247     btif_gattc_listen,
   2248     btif_gattc_refresh,
   2249     btif_gattc_search_service,
   2250     btif_gattc_get_included_service,
   2251     btif_gattc_get_characteristic,
   2252     btif_gattc_get_descriptor,
   2253     btif_gattc_read_char,
   2254     btif_gattc_write_char,
   2255     btif_gattc_read_char_descr,
   2256     btif_gattc_write_char_descr,
   2257     btif_gattc_execute_write,
   2258     btif_gattc_reg_for_notification,
   2259     btif_gattc_dereg_for_notification,
   2260     btif_gattc_read_remote_rssi,
   2261     btif_gattc_scan_filter_param_setup,
   2262     btif_gattc_scan_filter_add_remove,
   2263     btif_gattc_scan_filter_clear,
   2264     btif_gattc_scan_filter_enable,
   2265     btif_gattc_get_device_type,
   2266     btif_gattc_set_adv_data,
   2267     btif_gattc_configure_mtu,
   2268     btif_gattc_conn_parameter_update,
   2269     btif_gattc_set_scan_parameters,
   2270     btif_gattc_multi_adv_enable,
   2271     btif_gattc_multi_adv_update,
   2272     btif_gattc_multi_adv_setdata,
   2273     btif_gattc_multi_adv_disable,
   2274     btif_gattc_cfg_storage,
   2275     btif_gattc_enb_batch_scan,
   2276     btif_gattc_dis_batch_scan,
   2277     btif_gattc_read_batch_scan_reports,
   2278     btif_gattc_test_command
   2279 };
   2280 
   2281 #endif
   2282