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