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             btif_gattc_clear_clientif(p_btif_cb->client_if, FALSE);
    664             HAL_CBACK(bt_gatt_callbacks, client->multi_adv_data_cb
    665                 , p_btif_cb->client_if
    666                 , p_btif_cb->status
    667             );
    668             break;
    669         }
    670 
    671         case BTA_GATTC_MULT_ADV_DIS_EVT:
    672         {
    673             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*) p_param;
    674             btif_gattc_clear_clientif(p_btif_cb->client_if, TRUE);
    675             HAL_CBACK(bt_gatt_callbacks, client->multi_adv_disable_cb
    676                 , p_btif_cb->client_if
    677                 , p_btif_cb->status
    678             );
    679             break;
    680         }
    681 
    682         case BTA_GATTC_ADV_DATA_EVT:
    683         {
    684             btif_gattc_cleanup_inst_cb(STD_ADV_INSTID, FALSE);
    685             /* No HAL callback available */
    686             break;
    687         }
    688 
    689         case BTA_GATTC_CONGEST_EVT:
    690             HAL_CBACK(bt_gatt_callbacks, client->congestion_cb
    691                 , p_data->congest.conn_id
    692                 , p_data->congest.congested
    693             );
    694             break;
    695 
    696         case BTA_GATTC_BTH_SCAN_CFG_EVT:
    697         {
    698             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    699             HAL_CBACK(bt_gatt_callbacks, client->batchscan_cfg_storage_cb
    700                 , p_data->client_if
    701                 , p_data->status
    702             );
    703             break;
    704         }
    705 
    706         case BTA_GATTC_BTH_SCAN_ENB_EVT:
    707         {
    708             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    709             HAL_CBACK(bt_gatt_callbacks, client->batchscan_enb_disable_cb
    710                     , ENABLE_BATCH_SCAN
    711                     , p_data->client_if
    712                     , p_data->status);
    713             break;
    714         }
    715 
    716         case BTA_GATTC_BTH_SCAN_DIS_EVT:
    717         {
    718             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    719             HAL_CBACK(bt_gatt_callbacks, client->batchscan_enb_disable_cb
    720                     , DISABLE_BATCH_SCAN
    721                     , p_data->client_if
    722                     , p_data->status);
    723             break;
    724         }
    725 
    726         case BTA_GATTC_BTH_SCAN_THR_EVT:
    727         {
    728             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    729             HAL_CBACK(bt_gatt_callbacks, client->batchscan_threshold_cb
    730                     , p_data->client_if);
    731             break;
    732         }
    733 
    734         case BTA_GATTC_BTH_SCAN_RD_EVT:
    735         {
    736             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    737             uint8_t *p_rep_data = NULL;
    738 
    739             if (p_data->read_reports.data_len > 0 && NULL != p_data->read_reports.p_rep_data)
    740             {
    741                 p_rep_data = GKI_getbuf(p_data->read_reports.data_len);
    742                 memcpy(p_rep_data, p_data->read_reports.p_rep_data, p_data->read_reports.data_len);
    743             }
    744 
    745             HAL_CBACK(bt_gatt_callbacks, client->batchscan_reports_cb
    746                     , p_data->client_if, p_data->status, p_data->read_reports.report_format
    747                     , p_data->read_reports.num_records, p_data->read_reports.data_len, p_rep_data);
    748             if (NULL != p_rep_data)
    749                 GKI_freebuf(p_rep_data);
    750             break;
    751         }
    752 
    753         case BTA_GATTC_SCAN_FLT_CFG_EVT:
    754         {
    755             btgatt_adv_filter_cb_t *p_btif_cb = (btgatt_adv_filter_cb_t*) p_param;
    756             HAL_CBACK(bt_gatt_callbacks, client->scan_filter_cfg_cb, p_btif_cb->action,
    757                       p_btif_cb->client_if, p_btif_cb->status, p_btif_cb->cond_op,
    758                       p_btif_cb->avbl_space);
    759             break;
    760         }
    761 
    762         case BTA_GATTC_SCAN_FLT_PARAM_EVT:
    763         {
    764             btgatt_adv_filter_cb_t *p_data = (btgatt_adv_filter_cb_t*) p_param;
    765             BTIF_TRACE_DEBUG("BTA_GATTC_SCAN_FLT_PARAM_EVT: %d, %d, %d, %d",p_data->client_if,
    766                 p_data->action, p_data->avbl_space, p_data->status);
    767             HAL_CBACK(bt_gatt_callbacks, client->scan_filter_param_cb
    768                     , p_data->action, p_data->client_if, p_data->status
    769                     , p_data->avbl_space);
    770             break;
    771         }
    772 
    773         case BTA_GATTC_SCAN_FLT_STATUS_EVT:
    774         {
    775             btgatt_adv_filter_cb_t *p_data = (btgatt_adv_filter_cb_t*) p_param;
    776             BTIF_TRACE_DEBUG("BTA_GATTC_SCAN_FLT_STATUS_EVT: %d, %d, %d",p_data->client_if,
    777                 p_data->action, p_data->status);
    778             HAL_CBACK(bt_gatt_callbacks, client->scan_filter_status_cb
    779                     , p_data->action, p_data->client_if, p_data->status);
    780             break;
    781         }
    782 
    783         case BTA_GATTC_ADV_VSC_EVT:
    784         {
    785             btgatt_batch_track_cb_t *p_data = (btgatt_batch_track_cb_t*) p_param;
    786             HAL_CBACK(bt_gatt_callbacks, client->track_adv_event_cb
    787                     ,p_data->client_if, p_data->filt_index, p_data->addr_type, &p_data->bd_addr
    788                     ,p_data->adv_state);
    789             break;
    790         }
    791 
    792         default:
    793             BTIF_TRACE_ERROR("%s: Unhandled event (%d)!", __FUNCTION__, event);
    794             break;
    795     }
    796 
    797     btapp_gattc_free_req_data(event, p_data);
    798 }
    799 
    800 static void bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
    801 {
    802     bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt,
    803                     (uint16_t) event, (void*) p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data);
    804     ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
    805 }
    806 
    807 static void bta_gattc_multi_adv_cback(tBTA_BLE_MULTI_ADV_EVT event, UINT8 inst_id,
    808                                     void *p_ref, tBTA_STATUS call_status)
    809 {
    810     btif_gattc_cb_t btif_cb;
    811     tBTA_GATTC_EVT upevt;
    812     uint8_t client_if = 0;
    813 
    814     if (NULL == p_ref)
    815     {
    816         BTIF_TRACE_WARNING("%s Invalid p_ref received",__FUNCTION__);
    817     }
    818     else
    819     {
    820         client_if = *(UINT8 *) p_ref;
    821     }
    822 
    823     BTIF_TRACE_DEBUG("%s -Inst ID %d, Status:%x, client_if:%d",__FUNCTION__,inst_id, call_status,
    824                        client_if);
    825     btif_cb.status = call_status;
    826     btif_cb.client_if = client_if;
    827     btif_cb.inst_id = inst_id;
    828 
    829     switch(event)
    830     {
    831         case BTA_BLE_MULTI_ADV_ENB_EVT:
    832             upevt = BTA_GATTC_MULT_ADV_ENB_EVT;
    833             break;
    834 
    835         case BTA_BLE_MULTI_ADV_DISABLE_EVT:
    836             upevt = BTA_GATTC_MULT_ADV_DIS_EVT;
    837             break;
    838 
    839         case BTA_BLE_MULTI_ADV_PARAM_EVT:
    840             upevt = BTA_GATTC_MULT_ADV_UPD_EVT;
    841             break;
    842 
    843         case BTA_BLE_MULTI_ADV_DATA_EVT:
    844             upevt = BTA_GATTC_MULT_ADV_DATA_EVT;
    845             break;
    846 
    847         default:
    848             return;
    849     }
    850 
    851     bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt, (uint16_t) upevt,
    852                         (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    853     ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
    854 }
    855 
    856 static void bta_gattc_set_adv_data_cback(tBTA_STATUS call_status)
    857 {
    858     UNUSED(call_status);
    859     btif_gattc_cb_t btif_cb;
    860     btif_cb.status = call_status;
    861     btif_cb.action = 0;
    862     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_DATA_EVT,
    863                           (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    864 }
    865 
    866 static void bta_batch_scan_setup_cb (tBTA_BLE_BATCH_SCAN_EVT evt,
    867                                             tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
    868 {
    869     UINT8 upevt = 0;
    870     btgatt_batch_track_cb_t btif_scan_track_cb;
    871 
    872     btif_scan_track_cb.status = status;
    873     btif_scan_track_cb.client_if = ref_value;
    874     BTIF_TRACE_DEBUG("bta_batch_scan_setup_cb-Status:%x, client_if:%d, evt=%d",
    875             status, ref_value, evt);
    876 
    877     switch(evt)
    878     {
    879         case BTA_BLE_BATCH_SCAN_ENB_EVT:
    880         {
    881            upevt = BTA_GATTC_BTH_SCAN_ENB_EVT;
    882            break;
    883         }
    884 
    885         case BTA_BLE_BATCH_SCAN_DIS_EVT:
    886         {
    887            upevt = BTA_GATTC_BTH_SCAN_DIS_EVT;
    888            break;
    889         }
    890 
    891         case BTA_BLE_BATCH_SCAN_CFG_STRG_EVT:
    892         {
    893            upevt = BTA_GATTC_BTH_SCAN_CFG_EVT;
    894            break;
    895         }
    896 
    897         case BTA_BLE_BATCH_SCAN_DATA_EVT:
    898         {
    899            upevt = BTA_GATTC_BTH_SCAN_RD_EVT;
    900            break;
    901         }
    902 
    903         case BTA_BLE_BATCH_SCAN_THRES_EVT:
    904         {
    905            upevt = BTA_GATTC_BTH_SCAN_THR_EVT;
    906            break;
    907         }
    908 
    909         default:
    910             return;
    911     }
    912 
    913     btif_transfer_context(btif_gattc_upstreams_evt, upevt,(char*) &btif_scan_track_cb,
    914                           sizeof(btgatt_batch_track_cb_t), NULL);
    915 
    916 }
    917 
    918 static void bta_batch_scan_threshold_cb(tBTA_DM_BLE_REF_VALUE ref_value)
    919 {
    920     btgatt_batch_track_cb_t btif_scan_track_cb;
    921     btif_scan_track_cb.status = 0;
    922     btif_scan_track_cb.client_if = ref_value;
    923 
    924     BTIF_TRACE_DEBUG("%s - client_if:%d",__FUNCTION__, ref_value);
    925 
    926     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_BTH_SCAN_THR_EVT,
    927                           (char*) &btif_scan_track_cb, sizeof(btif_gattc_cb_t), NULL);
    928 }
    929 
    930 static void bta_batch_scan_reports_cb(tBTA_DM_BLE_REF_VALUE ref_value, UINT8 report_format,
    931                                             UINT8 num_records, UINT16 data_len,
    932                                             UINT8* p_rep_data, tBTA_STATUS status)
    933 {
    934     btgatt_batch_track_cb_t btif_scan_track_cb;
    935     memset(&btif_scan_track_cb, 0, sizeof(btgatt_batch_track_cb_t));
    936     BTIF_TRACE_DEBUG("%s - client_if:%d, %d, %d, %d",__FUNCTION__, ref_value, status, num_records,
    937                                     data_len);
    938 
    939     btif_scan_track_cb.status = status;
    940 
    941     btif_scan_track_cb.client_if = ref_value;
    942     btif_scan_track_cb.read_reports.report_format = report_format;
    943     btif_scan_track_cb.read_reports.data_len = data_len;
    944     btif_scan_track_cb.read_reports.num_records = num_records;
    945 
    946     if (data_len > 0)
    947     {
    948         btif_scan_track_cb.read_reports.p_rep_data = GKI_getbuf(data_len);
    949         memcpy(btif_scan_track_cb.read_reports.p_rep_data, p_rep_data, data_len);
    950         GKI_freebuf(p_rep_data);
    951     }
    952 
    953     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_BTH_SCAN_RD_EVT,
    954         (char*) &btif_scan_track_cb, sizeof(btgatt_batch_track_cb_t), NULL);
    955 
    956     if (data_len > 0)
    957         GKI_freebuf(btif_scan_track_cb.read_reports.p_rep_data);
    958 }
    959 
    960 static void bta_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
    961 {
    962     btif_gattc_cb_t btif_cb;
    963     uint8_t len;
    964 
    965     switch (event)
    966     {
    967         case BTA_DM_INQ_RES_EVT:
    968         {
    969             bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr);
    970             btif_cb.device_type = p_data->inq_res.device_type;
    971             btif_cb.rssi = p_data->inq_res.rssi;
    972             btif_cb.addr_type = p_data->inq_res.ble_addr_type;
    973             btif_cb.flag = p_data->inq_res.flag;
    974             if (p_data->inq_res.p_eir)
    975             {
    976                 memcpy(btif_cb.value, p_data->inq_res.p_eir, 62);
    977                 if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE,
    978                                       &len))
    979                 {
    980                     p_data->inq_res.remt_name_not_required  = TRUE;
    981                 }
    982             }
    983         }
    984         break;
    985 
    986         case BTA_DM_INQ_CMPL_EVT:
    987         {
    988             BTIF_TRACE_DEBUG("%s  BLE observe complete. Num Resp %d",
    989                               __FUNCTION__,p_data->inq_cmpl.num_resps);
    990             return;
    991         }
    992 
    993         default:
    994         BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
    995         return;
    996     }
    997     btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT,
    998                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
    999 }
   1000 
   1001 static void bta_track_adv_event_cb(int filt_index, tBLE_ADDR_TYPE addr_type, BD_ADDR bda,
   1002                                         int adv_state, tBTA_DM_BLE_REF_VALUE ref_value)
   1003 {
   1004     btgatt_batch_track_cb_t btif_scan_track_cb;
   1005     BTIF_TRACE_DEBUG("%s :%d, %d, %d, %d",
   1006         __FUNCTION__,filt_index, addr_type, adv_state, ref_value);
   1007     btif_scan_track_cb.filt_index = filt_index;
   1008     btif_scan_track_cb.addr_type = addr_type;
   1009     memcpy(btif_scan_track_cb.bd_addr.address, bda, sizeof(BD_ADDR));
   1010     btif_scan_track_cb.client_if = ref_value;
   1011     btif_scan_track_cb.adv_state = adv_state;
   1012     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_ADV_VSC_EVT,
   1013                           (char*) &btif_scan_track_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   1014 }
   1015 
   1016 static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result)
   1017 {
   1018     btif_gattc_cb_t btif_cb;
   1019 
   1020     bdcpy(btif_cb.bd_addr.address, p_result->rem_bda);
   1021     btif_cb.rssi = p_result->rssi;
   1022     btif_cb.status = p_result->status;
   1023     btif_cb.client_if = rssi_request_client_if;
   1024     btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT,
   1025                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1026 }
   1027 
   1028 static void bta_scan_filt_cfg_cb(tBTA_DM_BLE_PF_ACTION action, tBTA_DM_BLE_SCAN_COND_OP cfg_op,
   1029                                 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space, tBTA_STATUS status,
   1030                                 tBTA_DM_BLE_REF_VALUE ref_value)
   1031 {
   1032     btgatt_adv_filter_cb_t btif_cb;
   1033     btif_cb.status = status;
   1034     btif_cb.action = action;
   1035     btif_cb.cond_op = cfg_op;
   1036     btif_cb.avbl_space = avbl_space;
   1037     btif_cb.client_if = ref_value;
   1038     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_CFG_EVT,
   1039                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   1040 }
   1041 
   1042 static void bta_scan_filt_param_setup_cb(UINT8 action_type,
   1043                                         tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
   1044                                         tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
   1045 {
   1046     btgatt_adv_filter_cb_t btif_cb;
   1047 
   1048     btif_cb.status = status;
   1049     btif_cb.action = action_type;
   1050     btif_cb.client_if = ref_value;
   1051     btif_cb.avbl_space = avbl_space;
   1052     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_PARAM_EVT,
   1053                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   1054 }
   1055 
   1056 static void bta_scan_filt_status_cb(UINT8 action, tBTA_STATUS status,
   1057                                     tBTA_DM_BLE_REF_VALUE ref_value)
   1058 {
   1059     btgatt_adv_filter_cb_t btif_cb;
   1060 
   1061     btif_cb.status = status;
   1062     btif_cb.action = action;
   1063     btif_cb.client_if = ref_value;
   1064     btif_transfer_context(btif_gattc_upstreams_evt, BTA_GATTC_SCAN_FLT_STATUS_EVT,
   1065                           (char*) &btif_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   1066 }
   1067 
   1068 static void btgattc_handle_event(uint16_t event, char* p_param)
   1069 {
   1070     tBTA_GATT_STATUS           status;
   1071     tBT_UUID                   uuid;
   1072     tBTA_GATT_SRVC_ID          srvc_id;
   1073     tGATT_CHAR_PROP            out_char_prop;
   1074     tBTA_GATTC_CHAR_ID         in_char_id;
   1075     tBTA_GATTC_CHAR_ID         out_char_id;
   1076     tBTA_GATTC_CHAR_DESCR_ID   in_char_descr_id;
   1077     tBTA_GATTC_CHAR_DESCR_ID   out_char_descr_id;
   1078     tBTA_GATTC_INCL_SVC_ID     in_incl_svc_id;
   1079     tBTA_GATTC_INCL_SVC_ID     out_incl_svc_id;
   1080     tBTA_GATT_UNFMT            descr_val;
   1081 
   1082     btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*) p_param;
   1083     if (!p_cb) return;
   1084 
   1085     BTIF_TRACE_EVENT("%s: Event %d", __FUNCTION__, event);
   1086 
   1087     switch (event)
   1088     {
   1089         case BTIF_GATTC_REGISTER_APP:
   1090             btif_to_bta_uuid(&uuid, &p_cb->uuid);
   1091             btif_gattc_incr_app_count();
   1092             BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
   1093             break;
   1094 
   1095         case BTIF_GATTC_UNREGISTER_APP:
   1096             btif_gattc_clear_clientif(p_cb->client_if, TRUE);
   1097             btif_gattc_decr_app_count();
   1098             BTA_GATTC_AppDeregister(p_cb->client_if);
   1099             break;
   1100 
   1101         case BTIF_GATTC_SCAN_START:
   1102             btif_gattc_init_dev_cb();
   1103             BTA_DmBleObserve(TRUE, 0, bta_scan_results_cb);
   1104             break;
   1105 
   1106         case BTIF_GATTC_SCAN_STOP:
   1107             BTA_DmBleObserve(FALSE, 0, 0);
   1108             break;
   1109 
   1110         case BTIF_GATTC_OPEN:
   1111         {
   1112             // Ensure device is in inquiry database
   1113             int addr_type = 0;
   1114             int device_type = 0;
   1115             tBTA_GATT_TRANSPORT transport = BTA_GATT_TRANSPORT_LE;
   1116 
   1117             if (btif_get_address_type(p_cb->bd_addr.address, &addr_type) &&
   1118                 btif_get_device_type(p_cb->bd_addr.address, &device_type) &&
   1119                 device_type != BT_DEVICE_TYPE_BREDR)
   1120             {
   1121                 BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
   1122             }
   1123 
   1124             // Mark background connections
   1125             if (!p_cb->is_direct)
   1126             {
   1127                 // Check if RPA offloading is supported, otherwise, do not start
   1128                 // background connection, since it will not connect after address
   1129                 // changes
   1130                 if ((p_cb->addr_type == BLE_ADDR_RANDOM)
   1131                         && BTM_BLE_IS_RESOLVE_BDA(p_cb->bd_addr.address))
   1132                 {
   1133                     tBTM_BLE_VSC_CB vnd_capabilities;
   1134                     BTM_BleGetVendorCapabilities(&vnd_capabilities);
   1135                     if (!vnd_capabilities.rpa_offloading)
   1136                     {
   1137                         HAL_CBACK(bt_gatt_callbacks, client->open_cb, 0, BT_STATUS_UNSUPPORTED,
   1138                                         p_cb->client_if, &p_cb->bd_addr);
   1139                         return;
   1140                     }
   1141                 }
   1142                 BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
   1143             }
   1144 
   1145             switch(device_type)
   1146             {
   1147                 case BT_DEVICE_TYPE_BREDR:
   1148                     transport = BTA_GATT_TRANSPORT_BR_EDR;
   1149                     break;
   1150 
   1151                 case BT_DEVICE_TYPE_BLE:
   1152                     transport = BTA_GATT_TRANSPORT_LE;
   1153                     break;
   1154 
   1155                 case BT_DEVICE_TYPE_DUMO:
   1156                     if ((p_cb->transport == GATT_TRANSPORT_LE) &&
   1157                         (btif_storage_is_dmt_supported_device(&(p_cb->bd_addr)) == TRUE))
   1158                         transport = BTA_GATT_TRANSPORT_LE;
   1159                     else
   1160                         transport = BTA_GATT_TRANSPORT_BR_EDR;
   1161                     break;
   1162             }
   1163 
   1164             // Connect!
   1165             BTIF_TRACE_DEBUG ("BTA_GATTC_Open Transport  = %d, dev type = %d",
   1166                                 transport, device_type);
   1167             BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct, transport);
   1168             break;
   1169         }
   1170 
   1171         case BTIF_GATTC_CLOSE:
   1172             // Disconnect established connections
   1173             if (p_cb->conn_id != 0)
   1174                 BTA_GATTC_Close(p_cb->conn_id);
   1175             else
   1176                 BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE);
   1177 
   1178             // Cancel pending background connections (remove from whitelist)
   1179             BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE);
   1180             break;
   1181 
   1182         case BTIF_GATTC_SEARCH_SERVICE:
   1183         {
   1184             if (p_cb->search_all)
   1185             {
   1186                 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL);
   1187             } else {
   1188                 btif_to_bta_uuid(&uuid, &p_cb->uuid);
   1189                 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid);
   1190             }
   1191             break;
   1192         }
   1193 
   1194         case BTIF_GATTC_GET_FIRST_CHAR:
   1195         {
   1196             btgatt_gatt_id_t char_id;
   1197             btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
   1198             status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL,
   1199                                             &out_char_id, &out_char_prop);
   1200 
   1201             if (status == 0)
   1202                 bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
   1203 
   1204             HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
   1205                 p_cb->conn_id, status, &p_cb->srvc_id,
   1206                 &char_id, out_char_prop);
   1207             break;
   1208         }
   1209 
   1210         case BTIF_GATTC_GET_NEXT_CHAR:
   1211         {
   1212             btgatt_gatt_id_t char_id;
   1213             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1214             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1215 
   1216             status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL,
   1217                                             &out_char_id, &out_char_prop);
   1218 
   1219             if (status == 0)
   1220                 bta_to_btif_gatt_id(&char_id, &out_char_id.char_id);
   1221 
   1222             HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb,
   1223                 p_cb->conn_id, status, &p_cb->srvc_id,
   1224                 &char_id, out_char_prop);
   1225             break;
   1226         }
   1227 
   1228         case BTIF_GATTC_GET_FIRST_CHAR_DESCR:
   1229         {
   1230             btgatt_gatt_id_t descr_id;
   1231             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1232             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1233 
   1234             status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL,
   1235                                                     &out_char_descr_id);
   1236 
   1237             if (status == 0)
   1238                 bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
   1239 
   1240             HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
   1241                 p_cb->conn_id, status, &p_cb->srvc_id,
   1242                 &p_cb->char_id, &descr_id);
   1243             break;
   1244         }
   1245 
   1246         case BTIF_GATTC_GET_NEXT_CHAR_DESCR:
   1247         {
   1248             btgatt_gatt_id_t descr_id;
   1249             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
   1250             btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
   1251             btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
   1252 
   1253             status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id
   1254                                         , NULL, &out_char_descr_id);
   1255 
   1256             if (status == 0)
   1257                 bta_to_btif_gatt_id(&descr_id, &out_char_descr_id.descr_id);
   1258 
   1259             HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb,
   1260                 p_cb->conn_id, status, &p_cb->srvc_id,
   1261                 &p_cb->char_id, &descr_id);
   1262             break;
   1263         }
   1264 
   1265         case BTIF_GATTC_GET_FIRST_INCL_SERVICE:
   1266         {
   1267             btgatt_srvc_id_t incl_srvc_id;
   1268             btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id);
   1269 
   1270             status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id,
   1271                         &srvc_id, NULL, &out_incl_svc_id);
   1272 
   1273             bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
   1274 
   1275             HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
   1276                 p_cb->conn_id, status, &p_cb->srvc_id,
   1277                 &incl_srvc_id);
   1278             break;
   1279         }
   1280 
   1281         case BTIF_GATTC_GET_NEXT_INCL_SERVICE:
   1282         {
   1283             btgatt_srvc_id_t incl_srvc_id;
   1284             btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id);
   1285             btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id);
   1286 
   1287             status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id,
   1288                         &in_incl_svc_id, NULL, &out_incl_svc_id);
   1289 
   1290             bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id);
   1291 
   1292             HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb,
   1293                 p_cb->conn_id, status, &p_cb->srvc_id,
   1294                 &incl_srvc_id);
   1295             break;
   1296         }
   1297 
   1298         case BTIF_GATTC_READ_CHAR:
   1299             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1300             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1301 
   1302             BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req);
   1303             break;
   1304 
   1305         case BTIF_GATTC_READ_CHAR_DESCR:
   1306             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
   1307             btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
   1308             btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
   1309 
   1310             BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req);
   1311             break;
   1312 
   1313         case BTIF_GATTC_WRITE_CHAR:
   1314             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1315             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1316 
   1317             BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id,
   1318                                      p_cb->write_type,
   1319                                      p_cb->len,
   1320                                      p_cb->value,
   1321                                      p_cb->auth_req);
   1322             break;
   1323 
   1324         case BTIF_GATTC_WRITE_CHAR_DESCR:
   1325             btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id);
   1326             btif_to_bta_gatt_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id);
   1327             btif_to_bta_gatt_id(&in_char_descr_id.descr_id, &p_cb->descr_id);
   1328 
   1329             descr_val.len = p_cb->len;
   1330             descr_val.p_value = p_cb->value;
   1331 
   1332             BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id,
   1333                                      p_cb->write_type, &descr_val,
   1334                                      p_cb->auth_req);
   1335             break;
   1336 
   1337         case BTIF_GATTC_EXECUTE_WRITE:
   1338             BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action);
   1339             break;
   1340 
   1341         case BTIF_GATTC_REG_FOR_NOTIFICATION:
   1342             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1343             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1344 
   1345             status = BTA_GATTC_RegisterForNotifications(p_cb->client_if,
   1346                                     p_cb->bd_addr.address, &in_char_id);
   1347 
   1348             HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
   1349                 p_cb->conn_id, 1, status, &p_cb->srvc_id,
   1350                 &p_cb->char_id);
   1351             break;
   1352 
   1353         case BTIF_GATTC_DEREG_FOR_NOTIFICATION:
   1354             btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id);
   1355             btif_to_bta_gatt_id(&in_char_id.char_id, &p_cb->char_id);
   1356 
   1357             status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if,
   1358                                         p_cb->bd_addr.address, &in_char_id);
   1359 
   1360             HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
   1361                 p_cb->conn_id, 0, status, &p_cb->srvc_id,
   1362                 &p_cb->char_id);
   1363             break;
   1364 
   1365         case BTIF_GATTC_REFRESH:
   1366             BTA_GATTC_Refresh(p_cb->bd_addr.address);
   1367             break;
   1368 
   1369         case BTIF_GATTC_READ_RSSI:
   1370             rssi_request_client_if = p_cb->client_if;
   1371             BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
   1372             break;
   1373 
   1374         case BTIF_GATTC_SCAN_FILTER_PARAM_SETUP:
   1375         {
   1376             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1377             if (1 == p_adv_filt_cb->adv_filt_param.dely_mode)
   1378                BTA_DmBleTrackAdvertiser(p_adv_filt_cb->client_if, bta_track_adv_event_cb);
   1379             BTA_DmBleScanFilterSetup(p_adv_filt_cb->action, p_adv_filt_cb->filt_index,
   1380                 &p_adv_filt_cb->adv_filt_param, NULL, bta_scan_filt_param_setup_cb,
   1381                 p_adv_filt_cb->client_if);
   1382             break;
   1383         }
   1384 
   1385         case BTIF_GATTC_SCAN_FILTER_CONFIG:
   1386         {
   1387             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1388             tBTA_DM_BLE_PF_COND_PARAM cond;
   1389             memset(&cond, 0, sizeof(cond));
   1390 
   1391             switch (p_adv_filt_cb->filt_type)
   1392             {
   1393                 case BTA_DM_BLE_PF_ADDR_FILTER: // 0
   1394                     bdcpy(cond.target_addr.bda, p_adv_filt_cb->bd_addr.address);
   1395                     cond.target_addr.type = p_adv_filt_cb->addr_type;
   1396                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1397                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1398                                               &cond, bta_scan_filt_cfg_cb,
   1399                                               p_adv_filt_cb->client_if);
   1400                     break;
   1401 
   1402                 case BTA_DM_BLE_PF_SRVC_DATA: // 1
   1403                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1404                                             p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1405                                             NULL, bta_scan_filt_cfg_cb, p_adv_filt_cb->client_if);
   1406                     break;
   1407 
   1408                 case BTA_DM_BLE_PF_SRVC_UUID: // 2
   1409                 {
   1410                     tBTA_DM_BLE_PF_COND_MASK uuid_mask;
   1411 
   1412                     cond.srvc_uuid.p_target_addr = NULL;
   1413                     cond.srvc_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
   1414                     btif_to_bta_uuid(&cond.srvc_uuid.uuid, &p_adv_filt_cb->uuid);
   1415 
   1416                     cond.srvc_uuid.p_uuid_mask = NULL;
   1417                     if (p_adv_filt_cb->has_mask)
   1418                     {
   1419                         btif_to_bta_uuid_mask(&uuid_mask, &p_adv_filt_cb->uuid_mask);
   1420                         cond.srvc_uuid.p_uuid_mask = &uuid_mask;
   1421                     }
   1422                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1423                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1424                                               &cond, bta_scan_filt_cfg_cb,
   1425                                               p_adv_filt_cb->client_if);
   1426                     break;
   1427                 }
   1428 
   1429                 case BTA_DM_BLE_PF_SRVC_SOL_UUID: // 3
   1430                 {
   1431                     cond.solicitate_uuid.p_target_addr = NULL;
   1432                     cond.solicitate_uuid.cond_logic = BTA_DM_BLE_PF_LOGIC_AND;
   1433                     btif_to_bta_uuid(&cond.solicitate_uuid.uuid, &p_adv_filt_cb->uuid);
   1434                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1435                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1436                                               &cond, bta_scan_filt_cfg_cb,
   1437                                               p_adv_filt_cb->client_if);
   1438                     break;
   1439                 }
   1440 
   1441                 case BTA_DM_BLE_PF_LOCAL_NAME: // 4
   1442                 {
   1443                     cond.local_name.data_len = p_adv_filt_cb->value_len;
   1444                     cond.local_name.p_data = p_adv_filt_cb->value;
   1445                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1446                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1447                                               &cond, bta_scan_filt_cfg_cb,
   1448                                               p_adv_filt_cb->client_if);
   1449                     break;
   1450                 }
   1451 
   1452                 case BTA_DM_BLE_PF_MANU_DATA: // 5
   1453                 {
   1454                     cond.manu_data.company_id = p_adv_filt_cb->conn_id;
   1455                     cond.manu_data.company_id_mask = p_adv_filt_cb->company_id_mask;
   1456                     cond.manu_data.data_len = p_adv_filt_cb->value_len;
   1457                     cond.manu_data.p_pattern = p_adv_filt_cb->value;
   1458                     cond.manu_data.p_pattern_mask = p_adv_filt_cb->value_mask;
   1459                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1460                                               p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1461                                               &cond, bta_scan_filt_cfg_cb,
   1462                                               p_adv_filt_cb->client_if);
   1463                     break;
   1464                 }
   1465 
   1466                 case BTA_DM_BLE_PF_SRVC_DATA_PATTERN: //6
   1467                 {
   1468                     cond.srvc_data.data_len = p_adv_filt_cb->value_len;
   1469                     cond.srvc_data.p_pattern = p_adv_filt_cb->value;
   1470                     cond.srvc_data.p_pattern_mask = p_adv_filt_cb->value_mask;
   1471                     BTA_DmBleCfgFilterCondition(p_adv_filt_cb->action,
   1472                                                 p_adv_filt_cb->filt_type, p_adv_filt_cb->filt_index,
   1473                                                 &cond, bta_scan_filt_cfg_cb,
   1474                                                 p_adv_filt_cb->client_if);
   1475                    break;
   1476                 }
   1477 
   1478                 default:
   1479                     BTIF_TRACE_ERROR("%s: Unknown filter type (%d)!", __FUNCTION__, p_cb->action);
   1480                     break;
   1481             }
   1482             break;
   1483         }
   1484 
   1485         case BTIF_GATTC_SCAN_FILTER_CLEAR:
   1486         {
   1487             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1488             BTA_DmBleCfgFilterCondition(BTA_DM_BLE_SCAN_COND_CLEAR, BTA_DM_BLE_PF_TYPE_ALL,
   1489                                         p_adv_filt_cb->filt_index, NULL, bta_scan_filt_cfg_cb,
   1490                                         p_adv_filt_cb->client_if);
   1491             break;
   1492         }
   1493 
   1494         case BTIF_GATTC_SCAN_FILTER_ENABLE:
   1495         {
   1496             btgatt_adv_filter_cb_t *p_adv_filt_cb = (btgatt_adv_filter_cb_t *) p_param;
   1497             BTA_DmEnableScanFilter(p_adv_filt_cb->action, bta_scan_filt_status_cb,
   1498                                    p_adv_filt_cb->client_if);
   1499             break;
   1500         }
   1501 
   1502         case BTIF_GATTC_LISTEN:
   1503 #if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
   1504             BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
   1505 #else
   1506             BTA_GATTC_Broadcast(p_cb->client_if, p_cb->start);
   1507 #endif
   1508             break;
   1509 
   1510         case BTIF_GATTC_SET_ADV_DATA:
   1511         {
   1512             btif_adv_data_t *p_adv_data = (btif_adv_data_t*) p_param;
   1513             int cbindex = CLNT_IF_IDX;
   1514             if (cbindex >= 0 && NULL != p_adv_data)
   1515             {
   1516                 btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1517                 if (!btif_gattc_copy_datacb(cbindex, p_adv_data, false))
   1518                     return;
   1519 
   1520                 if (!p_adv_data->set_scan_rsp)
   1521                 {
   1522                     BTA_DmBleSetAdvConfig(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                 else
   1526                 {
   1527                     BTA_DmBleSetScanRsp(p_multi_adv_data_cb->inst_cb[cbindex].mask,
   1528                         &p_multi_adv_data_cb->inst_cb[cbindex].data, bta_gattc_set_adv_data_cback);
   1529                 }
   1530                 break;
   1531             }
   1532         }
   1533 
   1534         case BTIF_GATTC_ADV_INSTANCE_ENABLE:
   1535         {
   1536             btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
   1537 
   1538             int cbindex = -1, arrindex = -1;
   1539 
   1540             arrindex = btif_multi_adv_add_instid_map(p_inst_cb->client_if,INVALID_ADV_INST, true);
   1541             if (arrindex >= 0)
   1542                 cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
   1543 
   1544             if (cbindex >= 0 && arrindex >= 0)
   1545             {
   1546                 btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1547                 memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param,
   1548                        &p_inst_cb->param, sizeof(tBTA_BLE_ADV_PARAMS));
   1549                 p_multi_adv_data_cb->inst_cb[cbindex].timeout_s = p_inst_cb->timeout_s;
   1550                 BTIF_TRACE_DEBUG("%s, client_if value: %d", __FUNCTION__,
   1551                             p_multi_adv_data_cb->clntif_map[arrindex + arrindex]);
   1552                 BTA_BleEnableAdvInstance(&(p_multi_adv_data_cb->inst_cb[cbindex].param),
   1553                     bta_gattc_multi_adv_cback,
   1554                     &(p_multi_adv_data_cb->clntif_map[arrindex + arrindex]));
   1555             }
   1556             else
   1557             {
   1558                 /* let the error propagate up from BTA layer */
   1559                 BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_ENABLE_ADV",__FUNCTION__);
   1560                 BTA_BleEnableAdvInstance(&p_inst_cb->param, bta_gattc_multi_adv_cback, NULL);
   1561             }
   1562             break;
   1563         }
   1564 
   1565         case BTIF_GATTC_ADV_INSTANCE_UPDATE:
   1566         {
   1567             btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
   1568             int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
   1569             int cbindex = btif_gattc_obtain_idx_for_datacb(p_inst_cb->client_if, CLNT_IF_IDX);
   1570             if (inst_id >= 0 && cbindex >= 0 && NULL != p_inst_cb)
   1571             {
   1572                 btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1573                 memcpy(&p_multi_adv_data_cb->inst_cb[cbindex].param, &p_inst_cb->param,
   1574                         sizeof(tBTA_BLE_ADV_PARAMS));
   1575                 BTA_BleUpdateAdvInstParam((UINT8)inst_id,
   1576                     &(p_multi_adv_data_cb->inst_cb[cbindex].param));
   1577             }
   1578             else
   1579                 BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_UPDATE_ADV", __FUNCTION__);
   1580             break;
   1581         }
   1582 
   1583         case BTIF_GATTC_ADV_INSTANCE_SET_DATA:
   1584         {
   1585             btif_adv_data_t *p_adv_data = (btif_adv_data_t*) p_param;
   1586             int cbindex = btif_gattc_obtain_idx_for_datacb(p_adv_data->client_if, CLNT_IF_IDX);
   1587             int inst_id = btif_multi_adv_instid_for_clientif(p_adv_data->client_if);
   1588             if (inst_id < 0 || cbindex < 0)
   1589             {
   1590                BTIF_TRACE_ERROR("%s invalid index in BTIF_GATTC_SETADV_INST_DATA", __FUNCTION__);
   1591                return;
   1592             }
   1593 
   1594             if (!btif_gattc_copy_datacb(cbindex, p_adv_data, true))
   1595                 return;
   1596 
   1597             btgatt_multi_adv_common_data *p_multi_adv_data_cb = btif_obtain_multi_adv_data_cb();
   1598             BTA_BleCfgAdvInstData((UINT8)inst_id, p_multi_adv_data_cb->inst_cb[cbindex].is_scan_rsp,
   1599                       p_multi_adv_data_cb->inst_cb[cbindex].mask,
   1600                       &p_multi_adv_data_cb->inst_cb[cbindex].data);
   1601             break;
   1602         }
   1603 
   1604         case BTIF_GATTC_ADV_INSTANCE_DISABLE:
   1605         {
   1606             btgatt_multi_adv_inst_cb *p_inst_cb = (btgatt_multi_adv_inst_cb*) p_param;
   1607             int inst_id = btif_multi_adv_instid_for_clientif(p_inst_cb->client_if);
   1608             if (inst_id >=0)
   1609                 BTA_BleDisableAdvInstance((UINT8)inst_id);
   1610             else
   1611                 BTIF_TRACE_ERROR("%s invalid instance ID in BTIF_GATTC_DISABLE_ADV",__FUNCTION__);
   1612             break;
   1613         }
   1614 
   1615         case BTIF_GATTC_CONFIGURE_MTU:
   1616             BTA_GATTC_ConfigureMTU(p_cb->conn_id, p_cb->len);
   1617             break;
   1618 
   1619         case BTIF_GATTC_CONN_PARAM_UPDT:
   1620         {
   1621             btif_conn_param_cb_t *p_conn_param_cb = (btif_conn_param_cb_t*) p_param;
   1622             if (BTA_DmGetConnectionState(p_conn_param_cb->bd_addr.address))
   1623             {
   1624                 BTA_DmBleUpdateConnectionParams(p_conn_param_cb->bd_addr.address,
   1625                                p_conn_param_cb->min_interval, p_conn_param_cb->max_interval,
   1626                                p_conn_param_cb->latency, p_conn_param_cb->timeout);
   1627             } else {
   1628                 BTA_DmSetBlePrefConnParams(p_conn_param_cb->bd_addr.address,
   1629                                p_conn_param_cb->min_interval, p_conn_param_cb->max_interval,
   1630                                p_conn_param_cb->latency, p_conn_param_cb->timeout);
   1631             }
   1632             break;
   1633         }
   1634 
   1635         case BTIF_GATTC_SET_SCAN_PARAMS:
   1636             BTM_BleSetScanParams(p_cb->scan_interval, p_cb->scan_window, BTM_BLE_SCAN_MODE_ACTI);
   1637             break;
   1638 
   1639         case BTIF_GATTC_CONFIG_STORAGE_PARAMS:
   1640         {
   1641             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1642             BTA_DmBleSetStorageParams(p_scan_track_cb->batch_scan_full_max,
   1643                p_scan_track_cb->batch_scan_trunc_max, p_scan_track_cb->batch_scan_notify_threshold,
   1644                bta_batch_scan_setup_cb, bta_batch_scan_threshold_cb, bta_batch_scan_reports_cb,
   1645                (tBTA_DM_BLE_REF_VALUE) p_scan_track_cb->client_if);
   1646             break;
   1647         }
   1648 
   1649         case BTIF_GATTC_ENABLE_BATCH_SCAN:
   1650         {
   1651             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1652             BTA_DmBleEnableBatchScan(p_scan_track_cb->scan_mode, p_scan_track_cb->scan_interval,
   1653                p_scan_track_cb->scan_window, p_scan_track_cb->discard_rule,
   1654                p_scan_track_cb->addr_type, p_scan_track_cb->client_if);
   1655             break;
   1656         }
   1657 
   1658         case BTIF_GATTC_DISABLE_BATCH_SCAN:
   1659         {
   1660             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1661             BTA_DmBleDisableBatchScan(p_scan_track_cb->client_if);
   1662             break;
   1663         }
   1664 
   1665         case BTIF_GATTC_READ_BATCH_SCAN_REPORTS:
   1666         {
   1667             btgatt_batch_track_cb_t *p_scan_track_cb = (btgatt_batch_track_cb_t *) p_param;
   1668             BTA_DmBleReadScanReports(p_scan_track_cb->scan_mode, p_scan_track_cb->client_if);
   1669             break;
   1670         }
   1671 
   1672         default:
   1673             BTIF_TRACE_ERROR("%s: Unknown event (%d)!", __FUNCTION__, event);
   1674             break;
   1675     }
   1676 }
   1677 
   1678 /*******************************************************************************
   1679 **  Client API Functions
   1680 ********************************************************************************/
   1681 
   1682 static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid)
   1683 {
   1684     CHECK_BTGATT_INIT();
   1685     btif_gattc_cb_t btif_cb;
   1686     memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t));
   1687     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP,
   1688                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1689 }
   1690 
   1691 static bt_status_t btif_gattc_unregister_app(int client_if )
   1692 {
   1693     CHECK_BTGATT_INIT();
   1694     btif_gattc_cb_t btif_cb;
   1695     btif_cb.client_if = (uint8_t) client_if;
   1696     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP,
   1697                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1698 }
   1699 
   1700 static bt_status_t btif_gattc_scan( bool start )
   1701 {
   1702     CHECK_BTGATT_INIT();
   1703     btif_gattc_cb_t btif_cb;
   1704     return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP,
   1705                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1706 }
   1707 
   1708 static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr,
   1709                                         bool is_direct,int transport)
   1710 {
   1711     CHECK_BTGATT_INIT();
   1712     btif_gattc_cb_t btif_cb;
   1713     btif_cb.client_if = (uint8_t) client_if;
   1714     btif_cb.is_direct = is_direct ? 1 : 0;
   1715     btif_cb.transport = (btgatt_transport_t)transport;
   1716     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1717     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN,
   1718                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1719 }
   1720 
   1721 static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id)
   1722 {
   1723     CHECK_BTGATT_INIT();
   1724     btif_gattc_cb_t btif_cb;
   1725     btif_cb.client_if = (uint8_t) client_if;
   1726     btif_cb.conn_id = (uint16_t) conn_id;
   1727     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1728     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE,
   1729                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1730 }
   1731 
   1732 static bt_status_t btif_gattc_listen(int client_if, bool start)
   1733 {
   1734     CHECK_BTGATT_INIT();
   1735     btif_gattc_cb_t btif_cb;
   1736     btif_cb.client_if = (uint8_t) client_if;
   1737     btif_cb.start = start ? 1 : 0;
   1738     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_LISTEN,
   1739                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1740 }
   1741 
   1742 static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
   1743                 bool include_txpower, int min_interval, int max_interval, int appearance,
   1744                 uint16_t manufacturer_len, char* manufacturer_data,
   1745                 uint16_t service_data_len, char* service_data,
   1746                 uint16_t service_uuid_len, char* service_uuid)
   1747 {
   1748     CHECK_BTGATT_INIT();
   1749     bt_status_t status =0;
   1750 
   1751     btif_adv_data_t adv_data;
   1752 
   1753     btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name,
   1754         include_txpower, min_interval, max_interval, appearance, manufacturer_len,
   1755         manufacturer_data, service_data_len, service_data, service_uuid_len, service_uuid,
   1756         &adv_data);
   1757 
   1758     status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
   1759                        (char*) &adv_data, sizeof(btif_adv_data_t), NULL);
   1760 
   1761     if (NULL != adv_data.p_service_data)
   1762         GKI_freebuf(adv_data.p_service_data);
   1763 
   1764     if (NULL != adv_data.p_service_uuid)
   1765         GKI_freebuf(adv_data.p_service_uuid);
   1766 
   1767     if (NULL != adv_data.p_manufacturer_data)
   1768         GKI_freebuf(adv_data.p_manufacturer_data);
   1769 
   1770     return status;
   1771 }
   1772 
   1773 static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
   1774 {
   1775     CHECK_BTGATT_INIT();
   1776     btif_gattc_cb_t btif_cb;
   1777     btif_cb.client_if = (uint8_t) client_if;
   1778     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1779     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH,
   1780                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1781 }
   1782 
   1783 static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid )
   1784 {
   1785     CHECK_BTGATT_INIT();
   1786     btif_gattc_cb_t btif_cb;
   1787     btif_cb.conn_id = (uint16_t) conn_id;
   1788     btif_cb.search_all = filter_uuid ? 0 : 1;
   1789     if (filter_uuid)
   1790         memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t));
   1791     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE,
   1792                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1793 }
   1794 
   1795 static bt_status_t btif_gattc_get_characteristic( int conn_id
   1796         , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *start_char_id)
   1797 {
   1798     CHECK_BTGATT_INIT();
   1799     btif_gattc_cb_t btif_cb;
   1800     btif_cb.conn_id = (uint16_t) conn_id;
   1801     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1802     if (start_char_id)
   1803     {
   1804         memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_gatt_id_t));
   1805         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR,
   1806                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1807     }
   1808     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR,
   1809                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1810 }
   1811 
   1812 static bt_status_t btif_gattc_get_descriptor( int conn_id
   1813         , btgatt_srvc_id_t *srvc_id, btgatt_gatt_id_t *char_id
   1814         , btgatt_gatt_id_t *start_descr_id)
   1815 {
   1816     CHECK_BTGATT_INIT();
   1817     btif_gattc_cb_t btif_cb;
   1818     btif_cb.conn_id = (uint16_t) conn_id;
   1819     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1820     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1821     if (start_descr_id)
   1822     {
   1823         memcpy(&btif_cb.descr_id, start_descr_id, sizeof(btgatt_gatt_id_t));
   1824         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR,
   1825                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1826     }
   1827 
   1828     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR,
   1829                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1830 }
   1831 
   1832 static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id,
   1833                                                    btgatt_srvc_id_t *start_incl_srvc_id)
   1834 {
   1835     CHECK_BTGATT_INIT();
   1836     btif_gattc_cb_t btif_cb;
   1837     btif_cb.conn_id = (uint16_t) conn_id;
   1838     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1839     if (start_incl_srvc_id)
   1840     {
   1841         memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t));
   1842         return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE,
   1843                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1844     }
   1845     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE,
   1846                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1847 }
   1848 
   1849 static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id,
   1850                                         btgatt_gatt_id_t* char_id, int auth_req )
   1851 {
   1852     CHECK_BTGATT_INIT();
   1853     btif_gattc_cb_t btif_cb;
   1854     btif_cb.conn_id = (uint16_t) conn_id;
   1855     btif_cb.auth_req = (uint8_t) auth_req;
   1856     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1857     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1858     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR,
   1859                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1860 }
   1861 
   1862 static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
   1863                                               btgatt_gatt_id_t* char_id,
   1864                                               btgatt_gatt_id_t* descr_id,
   1865                                               int auth_req )
   1866 {
   1867     CHECK_BTGATT_INIT();
   1868     btif_gattc_cb_t btif_cb;
   1869     btif_cb.conn_id = (uint16_t) conn_id;
   1870     btif_cb.auth_req = (uint8_t) auth_req;
   1871     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1872     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1873     memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
   1874     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR,
   1875                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1876 }
   1877 
   1878 static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id,
   1879                                          btgatt_gatt_id_t* char_id, int write_type,
   1880                                          int len, int auth_req, char* p_value)
   1881 {
   1882     CHECK_BTGATT_INIT();
   1883     btif_gattc_cb_t btif_cb;
   1884     btif_cb.conn_id = (uint16_t) conn_id;
   1885     btif_cb.auth_req = (uint8_t) auth_req;
   1886     btif_cb.write_type = (uint8_t) write_type;
   1887     btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
   1888     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1889     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1890     memcpy(btif_cb.value, p_value, btif_cb.len);
   1891     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR,
   1892                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1893 }
   1894 
   1895 static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id,
   1896                                                btgatt_gatt_id_t* char_id,
   1897                                                btgatt_gatt_id_t* descr_id,
   1898                                                int write_type, int len, int auth_req,
   1899                                                char* p_value)
   1900 {
   1901     CHECK_BTGATT_INIT();
   1902     btif_gattc_cb_t btif_cb;
   1903     btif_cb.conn_id = (uint16_t) conn_id;
   1904     btif_cb.auth_req = (uint8_t) auth_req;
   1905     btif_cb.write_type = (uint8_t) write_type;
   1906     btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len;
   1907     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1908     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1909     memcpy(&btif_cb.descr_id, descr_id, sizeof(btgatt_gatt_id_t));
   1910     memcpy(btif_cb.value, p_value, btif_cb.len);
   1911     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR,
   1912                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1913 }
   1914 
   1915 static bt_status_t btif_gattc_execute_write(int conn_id, int execute)
   1916 {
   1917     CHECK_BTGATT_INIT();
   1918     btif_gattc_cb_t btif_cb;
   1919     btif_cb.conn_id = (uint16_t) conn_id;
   1920     btif_cb.action = (uint8_t) execute;
   1921     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE,
   1922                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1923 }
   1924 
   1925 static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
   1926                                                    btgatt_srvc_id_t* srvc_id,
   1927                                                    btgatt_gatt_id_t* char_id)
   1928 {
   1929     CHECK_BTGATT_INIT();
   1930     btif_gattc_cb_t btif_cb;
   1931     btif_cb.client_if = (uint8_t) client_if;
   1932     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1933     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1934     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1935     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION,
   1936                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1937 }
   1938 
   1939 static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr,
   1940                                                      btgatt_srvc_id_t* srvc_id,
   1941                                                      btgatt_gatt_id_t* char_id)
   1942 {
   1943     CHECK_BTGATT_INIT();
   1944     btif_gattc_cb_t btif_cb;
   1945     btif_cb.client_if = (uint8_t) client_if;
   1946     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1947     memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t));
   1948     memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_gatt_id_t));
   1949     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION,
   1950                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1951 }
   1952 
   1953 static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr)
   1954 {
   1955     CHECK_BTGATT_INIT();
   1956     btif_gattc_cb_t btif_cb;
   1957     btif_cb.client_if = (uint8_t) client_if;
   1958     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1959     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI,
   1960                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1961 }
   1962 
   1963 static bt_status_t btif_gattc_configure_mtu(int conn_id, int mtu)
   1964 {
   1965     CHECK_BTGATT_INIT();
   1966     btif_gattc_cb_t btif_cb;
   1967     btif_cb.conn_id = conn_id;
   1968     btif_cb.len = mtu; // Re-use len field
   1969     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIGURE_MTU,
   1970                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   1971 }
   1972 
   1973 static bt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t *bd_addr, int min_interval,
   1974                     int max_interval, int latency, int timeout)
   1975 {
   1976     CHECK_BTGATT_INIT();
   1977     btif_conn_param_cb_t btif_cb;
   1978     btif_cb.min_interval = min_interval;
   1979     btif_cb.max_interval = max_interval;
   1980     btif_cb.latency = latency;
   1981     btif_cb.timeout = timeout;
   1982     bdcpy(btif_cb.bd_addr.address, bd_addr->address);
   1983     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONN_PARAM_UPDT,
   1984                                  (char*) &btif_cb, sizeof(btif_conn_param_cb_t), NULL);
   1985 }
   1986 
   1987 static bt_status_t btif_gattc_scan_filter_param_setup(int client_if, int action,
   1988     int filt_index, int feat_seln, int list_logic_type, int filt_logic_type, int rssi_high_thres,
   1989     int rssi_low_thres, int dely_mode, int found_timeout, int lost_timeout, int found_timeout_cnt)
   1990 {
   1991     CHECK_BTGATT_INIT();
   1992     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
   1993     btgatt_adv_filter_cb_t btif_filt_cb;
   1994     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   1995     btif_filt_cb.action = action;
   1996     btif_filt_cb.client_if = client_if;
   1997     btif_filt_cb.filt_index = filt_index;
   1998     btif_filt_cb.adv_filt_param.feat_seln = feat_seln;
   1999     btif_filt_cb.adv_filt_param.list_logic_type = list_logic_type;
   2000     btif_filt_cb.adv_filt_param.filt_logic_type = filt_logic_type;
   2001     btif_filt_cb.adv_filt_param.rssi_high_thres = rssi_high_thres;
   2002     btif_filt_cb.adv_filt_param.rssi_low_thres = rssi_low_thres;
   2003     btif_filt_cb.adv_filt_param.dely_mode = dely_mode;
   2004     btif_filt_cb.adv_filt_param.found_timeout = found_timeout;
   2005     btif_filt_cb.adv_filt_param.lost_timeout = lost_timeout;
   2006     btif_filt_cb.adv_filt_param.found_timeout_cnt = found_timeout_cnt;
   2007     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_PARAM_SETUP,
   2008                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2009 }
   2010 
   2011 static bt_status_t btif_gattc_scan_filter_add_remove(int client_if, int action,
   2012                               int filt_type, int filt_index, int company_id,
   2013                               int company_id_mask, const bt_uuid_t *p_uuid,
   2014                               const bt_uuid_t *p_uuid_mask, const bt_bdaddr_t *bd_addr,
   2015                               char addr_type, int data_len, char* p_data, int mask_len,
   2016                               char* p_mask)
   2017 {
   2018     CHECK_BTGATT_INIT();
   2019     btgatt_adv_filter_cb_t btif_filt_cb;
   2020     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   2021     BTIF_TRACE_DEBUG("%s, %d, %d", __FUNCTION__, action, filt_type);
   2022 
   2023     /* If data is passed, both mask and data have to be the same length */
   2024     if (data_len != mask_len && NULL != p_data && NULL != p_mask)
   2025         return BT_STATUS_PARM_INVALID;
   2026 
   2027     btif_filt_cb.client_if = client_if;
   2028     btif_filt_cb.action = action;
   2029     btif_filt_cb.filt_index = filt_index;
   2030     btif_filt_cb.filt_type = filt_type;
   2031     btif_filt_cb.conn_id = company_id;
   2032     btif_filt_cb.company_id_mask = company_id_mask ? company_id_mask : 0xFFFF;
   2033     if (bd_addr)
   2034         bdcpy(btif_filt_cb.bd_addr.address, bd_addr->address);
   2035 
   2036     btif_filt_cb.addr_type = addr_type;
   2037     btif_filt_cb.has_mask = (p_uuid_mask != NULL);
   2038 
   2039     if (p_uuid != NULL)
   2040         memcpy(&btif_filt_cb.uuid, p_uuid, sizeof(bt_uuid_t));
   2041     if (p_uuid_mask != NULL)
   2042         memcpy(&btif_filt_cb.uuid_mask, p_uuid_mask, sizeof(bt_uuid_t));
   2043     if (p_data != NULL && data_len != 0)
   2044     {
   2045         memcpy(btif_filt_cb.value, p_data, data_len);
   2046         btif_filt_cb.value_len = data_len;
   2047         memcpy(btif_filt_cb.value_mask, p_mask, mask_len);
   2048         btif_filt_cb.value_mask_len = mask_len;
   2049     }
   2050     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
   2051                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2052 }
   2053 
   2054 static bt_status_t btif_gattc_scan_filter_clear(int client_if, int filt_index)
   2055 {
   2056     CHECK_BTGATT_INIT();
   2057     BTIF_TRACE_DEBUG("%s, %d", __FUNCTION__, filt_index);
   2058 
   2059     btgatt_adv_filter_cb_t btif_filt_cb;
   2060     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   2061     btif_filt_cb.client_if = client_if;
   2062     btif_filt_cb.filt_index = filt_index;
   2063     btif_filt_cb.action = BTA_DM_BLE_SCAN_COND_CLEAR;
   2064     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_CONFIG,
   2065                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2066 }
   2067 
   2068 static bt_status_t btif_gattc_scan_filter_enable(int client_if, bool enable)
   2069 {
   2070     int action = 0;
   2071     CHECK_BTGATT_INIT();
   2072     BTIF_TRACE_DEBUG("%s, %d", __FUNCTION__, enable);
   2073 
   2074     btgatt_adv_filter_cb_t btif_filt_cb;
   2075     memset(&btif_filt_cb, 0, sizeof(btgatt_adv_filter_cb_t));
   2076     btif_filt_cb.client_if = client_if;
   2077     if (true == enable)
   2078         action = 1;
   2079     btif_filt_cb.action = action;
   2080     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SCAN_FILTER_ENABLE,
   2081                                  (char*) &btif_filt_cb, sizeof(btgatt_adv_filter_cb_t), NULL);
   2082 }
   2083 
   2084 static bt_status_t btif_gattc_set_scan_parameters(int scan_interval, int scan_window)
   2085 {
   2086     CHECK_BTGATT_INIT();
   2087     btif_gattc_cb_t btif_cb;
   2088     btif_cb.scan_interval = scan_interval;
   2089     btif_cb.scan_window = scan_window;
   2090     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_SCAN_PARAMS,
   2091                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
   2092 }
   2093 
   2094 static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr )
   2095 {
   2096     int device_type = 0;
   2097     char bd_addr_str[18] = {0};
   2098 
   2099     bd2str(bd_addr, &bd_addr_str);
   2100     if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type))
   2101         return device_type;
   2102     return 0;
   2103 }
   2104 
   2105 static bt_status_t btif_gattc_multi_adv_enable(int client_if, int min_interval, int max_interval,
   2106                                             int adv_type, int chnl_map, int tx_power, int timeout_s)
   2107 {
   2108     CHECK_BTGATT_INIT();
   2109     btgatt_multi_adv_inst_cb adv_cb;
   2110     memset(&adv_cb, 0, sizeof(btgatt_multi_adv_inst_cb));
   2111     adv_cb.client_if = (uint8_t) client_if;
   2112 
   2113     adv_cb.param.adv_int_min = min_interval;
   2114     adv_cb.param.adv_int_max = max_interval;
   2115     adv_cb.param.adv_type = adv_type;
   2116     adv_cb.param.channel_map = chnl_map;
   2117     adv_cb.param.adv_filter_policy = 0;
   2118     adv_cb.param.tx_power = tx_power;
   2119     adv_cb.timeout_s = timeout_s;
   2120     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_ENABLE,
   2121                              (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
   2122 }
   2123 
   2124 static bt_status_t btif_gattc_multi_adv_update(int client_if, int min_interval, int max_interval,
   2125                                             int adv_type, int chnl_map,int tx_power, int timeout_s)
   2126 {
   2127     CHECK_BTGATT_INIT();
   2128     btgatt_multi_adv_inst_cb adv_cb;
   2129     memset(&adv_cb, 0, sizeof(btgatt_multi_adv_inst_cb));
   2130     adv_cb.client_if = (uint8_t) client_if;
   2131 
   2132     adv_cb.param.adv_int_min = min_interval;
   2133     adv_cb.param.adv_int_max = max_interval;
   2134     adv_cb.param.adv_type = adv_type;
   2135     adv_cb.param.channel_map = chnl_map;
   2136     adv_cb.param.adv_filter_policy = 0;
   2137     adv_cb.param.tx_power = tx_power;
   2138     adv_cb.timeout_s = timeout_s;
   2139     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_UPDATE,
   2140                          (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
   2141 }
   2142 
   2143 static bt_status_t btif_gattc_multi_adv_setdata(int client_if, bool set_scan_rsp,
   2144                 bool include_name, bool incl_txpower, int appearance,
   2145                 int manufacturer_len, char* manufacturer_data,
   2146                 int service_data_len, char* service_data,
   2147                 int service_uuid_len, char* service_uuid)
   2148 {
   2149     CHECK_BTGATT_INIT();
   2150 
   2151     int min_interval = 0, max_interval = 0;
   2152     bt_status_t status =0;
   2153 
   2154     btif_adv_data_t multi_adv_data_inst;
   2155     memset(&multi_adv_data_inst, 0, sizeof(btif_adv_data_t));
   2156 
   2157     btif_gattc_adv_data_packager(client_if, set_scan_rsp, include_name, incl_txpower,
   2158         min_interval, max_interval, appearance, manufacturer_len, manufacturer_data,
   2159         service_data_len, service_data, service_uuid_len, service_uuid, &multi_adv_data_inst);
   2160 
   2161     status = btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_SET_DATA,
   2162                        (char*) &multi_adv_data_inst, sizeof(btif_adv_data_t), NULL);
   2163 
   2164     if (NULL != multi_adv_data_inst.p_service_data)
   2165         GKI_freebuf(multi_adv_data_inst.p_service_data);
   2166 
   2167     if (NULL != multi_adv_data_inst.p_service_uuid)
   2168         GKI_freebuf(multi_adv_data_inst.p_service_uuid);
   2169 
   2170     if (NULL != multi_adv_data_inst.p_manufacturer_data)
   2171         GKI_freebuf(multi_adv_data_inst.p_manufacturer_data);
   2172 
   2173     return status;
   2174 }
   2175 
   2176 static bt_status_t btif_gattc_multi_adv_disable(int client_if)
   2177 {
   2178     CHECK_BTGATT_INIT();
   2179     btgatt_multi_adv_inst_cb adv_cb;
   2180     memset(&adv_cb, 0, sizeof(btgatt_multi_adv_inst_cb));
   2181     adv_cb.client_if = (uint8_t) client_if;
   2182 
   2183     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ADV_INSTANCE_DISABLE,
   2184                            (char*) &adv_cb, sizeof(btgatt_multi_adv_inst_cb), NULL);
   2185 }
   2186 
   2187 static bt_status_t btif_gattc_cfg_storage(int client_if,int batch_scan_full_max,
   2188     int batch_scan_trunc_max, int batch_scan_notify_threshold)
   2189 {
   2190     CHECK_BTGATT_INIT();
   2191     btgatt_batch_track_cb_t bt_scan_cb;
   2192     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2193     bt_scan_cb.client_if = (uint8_t) client_if;
   2194     bt_scan_cb.batch_scan_full_max = batch_scan_full_max;
   2195     bt_scan_cb.batch_scan_trunc_max = batch_scan_trunc_max;
   2196     bt_scan_cb.batch_scan_notify_threshold = batch_scan_notify_threshold;
   2197     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CONFIG_STORAGE_PARAMS,
   2198                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2199 }
   2200 
   2201 static bt_status_t btif_gattc_enb_batch_scan(int client_if,int scan_mode, int scan_interval,
   2202                 int scan_window, int addr_type, int discard_rule)
   2203 {
   2204     CHECK_BTGATT_INIT();
   2205     btgatt_batch_track_cb_t bt_scan_cb;
   2206     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2207     bt_scan_cb.client_if = (uint8_t) client_if;
   2208     bt_scan_cb.scan_mode = scan_mode;
   2209     bt_scan_cb.scan_interval = scan_interval;
   2210     bt_scan_cb.scan_window = scan_window;
   2211     bt_scan_cb.discard_rule = discard_rule;
   2212     bt_scan_cb.addr_type = addr_type;
   2213     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_ENABLE_BATCH_SCAN,
   2214                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2215 }
   2216 
   2217 static bt_status_t btif_gattc_dis_batch_scan(int client_if)
   2218 {
   2219     CHECK_BTGATT_INIT();
   2220     btgatt_batch_track_cb_t bt_scan_cb;
   2221     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2222     bt_scan_cb.client_if = (uint8_t) client_if;
   2223     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DISABLE_BATCH_SCAN,
   2224                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2225 }
   2226 
   2227 static bt_status_t btif_gattc_read_batch_scan_reports(int client_if, int scan_mode)
   2228 {
   2229     CHECK_BTGATT_INIT();
   2230     btgatt_batch_track_cb_t bt_scan_cb;
   2231     memset(&bt_scan_cb, 0, sizeof(btgatt_batch_track_cb_t));
   2232     bt_scan_cb.client_if = (uint8_t) client_if;
   2233     bt_scan_cb.scan_mode = scan_mode;
   2234     return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_BATCH_SCAN_REPORTS,
   2235                                  (char*) &bt_scan_cb, sizeof(btgatt_batch_track_cb_t), NULL);
   2236 }
   2237 
   2238 extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params);
   2239 
   2240 static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params)
   2241 {
   2242     return btif_gattc_test_command_impl(command, params);
   2243 }
   2244 
   2245 
   2246 const btgatt_client_interface_t btgattClientInterface = {
   2247     btif_gattc_register_app,
   2248     btif_gattc_unregister_app,
   2249     btif_gattc_scan,
   2250     btif_gattc_open,
   2251     btif_gattc_close,
   2252     btif_gattc_listen,
   2253     btif_gattc_refresh,
   2254     btif_gattc_search_service,
   2255     btif_gattc_get_included_service,
   2256     btif_gattc_get_characteristic,
   2257     btif_gattc_get_descriptor,
   2258     btif_gattc_read_char,
   2259     btif_gattc_write_char,
   2260     btif_gattc_read_char_descr,
   2261     btif_gattc_write_char_descr,
   2262     btif_gattc_execute_write,
   2263     btif_gattc_reg_for_notification,
   2264     btif_gattc_dereg_for_notification,
   2265     btif_gattc_read_remote_rssi,
   2266     btif_gattc_scan_filter_param_setup,
   2267     btif_gattc_scan_filter_add_remove,
   2268     btif_gattc_scan_filter_clear,
   2269     btif_gattc_scan_filter_enable,
   2270     btif_gattc_get_device_type,
   2271     btif_gattc_set_adv_data,
   2272     btif_gattc_configure_mtu,
   2273     btif_gattc_conn_parameter_update,
   2274     btif_gattc_set_scan_parameters,
   2275     btif_gattc_multi_adv_enable,
   2276     btif_gattc_multi_adv_update,
   2277     btif_gattc_multi_adv_setdata,
   2278     btif_gattc_multi_adv_disable,
   2279     btif_gattc_cfg_storage,
   2280     btif_gattc_enb_batch_scan,
   2281     btif_gattc_dis_batch_scan,
   2282     btif_gattc_read_batch_scan_reports,
   2283     btif_gattc_test_command
   2284 };
   2285 
   2286 #endif
   2287