Home | History | Annotate | Download | only in btm
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2008-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  *  This file contains functions for BLE GAP.
     22  *
     23  ******************************************************************************/
     24 
     25 #include <string.h>
     26 #include <stdio.h>
     27 #include <stddef.h>
     28 
     29 #include "bt_types.h"
     30 #include "bt_utils.h"
     31 #include "btm_int.h"
     32 #include "btm_ble_api.h"
     33 #include "btu.h"
     34 #include "hcimsgs.h"
     35 #if (GAP_INCLUDED == TRUE)
     36 #include "gap_api.h"
     37 #endif
     38 
     39 #if BLE_INCLUDED == TRUE
     40 #include "l2c_int.h"
     41 #include "vendor_ble.h"
     42 
     43 #include "gattdefs.h"
     44 #include "gatt_int.h"
     45 
     46 #include "btm_ble_int.h"
     47 
     48 #define BTM_BLE_NAME_SHORT                  0x01
     49 #define BTM_BLE_NAME_CMPL                   0x02
     50 
     51 #define BTM_BLE_FILTER_TARGET_UNKNOWN       0xff
     52 #define BTM_BLE_POLICY_UNKNOWN              0xff
     53 
     54 #define BTM_EXT_BLE_RMT_NAME_TIMEOUT        30
     55 #define MIN_ADV_LENGTH                       2
     56 
     57 static tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
     58 static tBTM_BLE_CTRL_FEATURES_CBACK    *p_ctrl_le_feature_rd_cmpl_cback = NULL;
     59 
     60 /*******************************************************************************
     61 **  Local functions
     62 *******************************************************************************/
     63 static void btm_ble_update_adv_flag(UINT8 flag);
     64 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p);
     65 UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
     66                               tBTM_BLE_ADV_DATA *p_data);
     67 static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
     68                                      BD_ADDR_PTR p_addr_ptr,
     69                                      tBLE_ADDR_TYPE *p_init_addr_type,
     70                                      tBLE_ADDR_TYPE *p_own_addr_type);
     71 static void btm_ble_stop_observe(void);
     72 
     73 #define BTM_BLE_INQ_RESULT          0x01
     74 #define BTM_BLE_OBS_RESULT          0x02
     75 #define BTM_BLE_SEL_CONN_RESULT     0x04
     76 
     77 /* LE states combo bit to check */
     78 const UINT8 btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] =
     79 {
     80     {/* single state support */
     81         {HCI_SUPP_LE_STATES_CONN_ADV_MASK, HCI_SUPP_LE_STATES_CONN_ADV_OFF},  /* conn_adv */
     82         {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* init */
     83         {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* master */
     84         {HCI_SUPP_LE_STATES_SLAVE_MASK, HCI_SUPP_LE_STATES_SLAVE_OFF}, /* slave */
     85         {0, 0},                   /* todo: lo du dir adv, not covered ? */
     86         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_OFF}, /* hi duty dir adv */
     87         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_OFF},  /* non connectable adv */
     88         {HCI_SUPP_LE_STATES_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_OFF},   /*  passive scan */
     89         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_OFF},  /*   active scan */
     90         {HCI_SUPP_LE_STATES_SCAN_ADV_MASK, HCI_SUPP_LE_STATESSCAN_ADV_OFF}   /* scanable adv */
     91     },
     92     {    /* conn_adv =0 */
     93         {0, 0},                                                                           /* conn_adv */
     94         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* init: 32 */
     95         {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF},  /* master: 35 */
     96         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/
     97         {0, 0},                                                                           /* lo du dir adv */
     98         {0, 0},                                                                            /* hi duty dir adv */
     99         {0, 0},  /* non connectable adv */
    100         {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF},   /*  passive scan */
    101         {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF},  /*   active scan */
    102         {0, 0}   /* scanable adv */
    103     },
    104     {   /* init */
    105         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* conn_adv: 32 */
    106         {0, 0},                                                                             /* init */
    107         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* master 28 */
    108         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* slave 41 */
    109         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF} ,/* lo du dir adv 34 */
    110         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF},     /* hi duty dir adv 33 */
    111         {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF},  /*  non connectable adv */
    112         {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF},   /* passive scan */
    113         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF},  /*  active scan */
    114         {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}   /* scanable adv */
    115 
    116     },
    117     {   /* master */
    118         {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF},  /* conn_adv: 35 */
    119         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* init 28 */
    120         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* master 28 */
    121         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* slave: 32 */
    122         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF},  /* lo duty cycle adv 37 */
    123         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF},   /* hi duty cycle adv 36 */
    124         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF},  /*  non connectable adv */
    125         {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF},   /*  passive scan */
    126         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF},  /*   active scan */
    127         {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}   /*  scanable adv */
    128 
    129     },
    130     { /* slave */
    131         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* conn_adv: 38,*/
    132         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* init 41 */
    133         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* master 41 */
    134         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF},        /* slave: 38,*/
    135         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF},  /* lo duty cycle adv 40 */
    136         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF},   /* hi duty cycle adv 39 */
    137         {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF},  /* non connectable adv */
    138         {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF},   /* passive scan */
    139         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF},  /*  active scan */
    140         {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF}   /* scanable adv */
    141 
    142     },
    143     { /* lo duty cycle adv */
    144         {0, 0}, /* conn_adv: 38,*/
    145         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF} ,/* init 34 */
    146         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* master 37 */
    147         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 40 */
    148         {0, 0},  /* lo duty cycle adv 40 */
    149         {0, 0},   /* hi duty cycle adv 39 */
    150         {0, 0},  /*  non connectable adv */
    151         {0, 0},   /* TODO: passive scan, not covered? */
    152         {0, 0},  /* TODO:  active scan, not covered? */
    153         {0, 0}   /*  scanable adv */
    154     },
    155     { /* hi duty cycle adv */
    156         {0, 0}, /* conn_adv: 38,*/
    157         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* init 33 */
    158         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* master 36 */
    159         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF},   /* slave: 39*/
    160         {0, 0},  /* lo duty cycle adv 40 */
    161         {0, 0},   /* hi duty cycle adv 39 */
    162         {0, 0},  /* non connectable adv */
    163         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF},   /* passive scan */
    164         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF},  /* active scan */
    165         {0, 0}   /* scanable adv */
    166     },
    167     { /* non connectable adv */
    168         {0, 0}, /* conn_adv: */
    169         {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* init  */
    170         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* master  */
    171         {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF},   /* slave: */
    172         {0, 0},  /* lo duty cycle adv */
    173         {0, 0},   /* hi duty cycle adv */
    174         {0, 0},  /* non connectable adv */
    175         {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF},   /* passive scan */
    176         {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF},  /*  active scan */
    177         {0, 0}   /* scanable adv */
    178     },
    179     { /* passive scan */
    180         {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* conn_adv: */
    181         {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* init  */
    182         {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* master  */
    183         {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF},   /* slave: */
    184         {0, 0},  /* lo duty cycle adv */
    185         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF},   /* hi duty cycle adv */
    186         {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF},  /*  non connectable adv */
    187         {0, 0},   /* passive scan */
    188         {0, 0},  /* active scan */
    189          {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF}   /* scanable adv */
    190     },
    191     { /* active scan */
    192         {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* conn_adv: */
    193         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* init  */
    194         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* master  */
    195         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF},   /* slave: */
    196         {0, 0},  /* lo duty cycle adv */
    197         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF},   /* hi duty cycle adv */
    198         {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF},  /*  non connectable adv */
    199         {0, 0},   /* TODO: passive scan */
    200         {0, 0},  /* TODO:  active scan */
    201         {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF}   /*  scanable adv */
    202     },
    203     { /* scanable adv */
    204         {0, 0}, /* conn_adv: */
    205         {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}, /* init  */
    206         {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}, /* master  */
    207         {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF},   /* slave: */
    208         {0, 0},  /* lo duty cycle adv */
    209         {0, 0},   /* hi duty cycle adv */
    210         {0, 0},  /* non connectable adv */
    211         {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF},   /*  passive scan */
    212         {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF},  /*  active scan */
    213         {0, 0}   /* scanable adv */
    214     }
    215 
    216 };
    217 /* check LE combo state supported */
    218 #define BTM_LE_STATES_SUPPORTED(x, y, z)      ((x)[(z)] & (y))
    219 
    220 
    221 /*******************************************************************************
    222 **
    223 ** Function         BTM_BleUpdateAdvWhitelist
    224 **
    225 ** Description      Add or remove device from advertising white list
    226 **
    227 ** Returns          void
    228 **
    229 *******************************************************************************/
    230 BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda)
    231 {
    232     UNUSED(add_remove);
    233     UNUSED(remote_bda);
    234 
    235     return FALSE;
    236 }
    237 
    238 /*******************************************************************************
    239 **
    240 ** Function         BTM_BleUpdateAdvFilterPolicy
    241 **
    242 ** Description      This function update the filter policy of advertiser.
    243 **
    244 ** Parameter        adv_policy: advertising filter policy
    245 **
    246 ** Return           void
    247 *******************************************************************************/
    248 void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)
    249 {
    250     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
    251     tBLE_ADDR_TYPE   init_addr_type = BLE_ADDR_PUBLIC;
    252     BD_ADDR          p_addr_ptr= {0};
    253     UINT8            adv_mode = p_cb->adv_mode;
    254 
    255     BTM_TRACE_EVENT ("BTM_BleUpdateAdvFilterPolicy");
    256 
    257     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    258         return;
    259 
    260     if (p_cb->afp != adv_policy)
    261     {
    262         p_cb->afp = adv_policy;
    263 
    264         /* if adv active, stop and restart */
    265         btm_ble_stop_adv ();
    266 
    267         if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
    268             p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type,
    269                                                               &p_cb->adv_addr_type);
    270 
    271         btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min :
    272                                          BTM_BLE_GAP_ADV_SLOW_INT),
    273                                          (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max :
    274                                          BTM_BLE_GAP_ADV_SLOW_INT),
    275                                          p_cb->evt_type,
    276                                          p_cb->adv_addr_type,
    277                                          init_addr_type,
    278                                          p_addr_ptr,
    279                                          p_cb->adv_chnl_map,
    280                                          p_cb->afp);
    281 
    282         if (adv_mode == BTM_BLE_ADV_ENABLE)
    283             btm_ble_start_adv ();
    284 
    285     }
    286 }
    287 /*******************************************************************************
    288 **
    289 ** Function         BTM_BleObserve
    290 **
    291 ** Description      This procedure keep the device listening for advertising
    292 **                  events from a broadcast device.
    293 **
    294 ** Parameters       start: start or stop observe.
    295 **                  white_list: use white list in observer mode or not.
    296 **
    297 ** Returns          void
    298 **
    299 *******************************************************************************/
    300 tBTM_STATUS BTM_BleObserve(BOOLEAN start, UINT8 duration,
    301                            tBTM_INQ_RESULTS_CB *p_results_cb, tBTM_CMPL_CB *p_cmpl_cb)
    302 {
    303     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
    304     tBTM_STATUS     status = BTM_WRONG_MODE;
    305 
    306     BTM_TRACE_EVENT ("BTM_BleObserve : scan_type:%d",btm_cb.btm_inq_vars.scan_type);
    307 
    308     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    309         return BTM_ILLEGAL_VALUE;
    310 
    311     if (start)
    312     {
    313         /* shared inquiry database, do not allow observe if any inquiry is active */
    314         if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
    315         {
    316             BTM_TRACE_ERROR("Observe Already Active");
    317             return status;
    318         }
    319 
    320         btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
    321         btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
    322         status = BTM_CMD_STARTED;
    323 
    324         /* scan is not started */
    325         if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
    326         {
    327             p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE) ? BTM_BLE_SCAN_MODE_ACTI: p_inq->scan_type;
    328             /* allow config scanning type */
    329             btsnd_hcic_ble_set_scan_params (p_inq->scan_type,
    330                                             (UINT16)(!p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval),
    331                                             (UINT16)(!p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window),
    332                                             btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
    333                                             BTM_BLE_DEFAULT_SFP); /* assume observe always not using white list */
    334 
    335 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
    336             /* enable IRK list */
    337             btm_ble_vendor_irk_list_known_dev (TRUE);
    338 #endif
    339             status = btm_ble_start_scan(BTM_BLE_DUPLICATE_DISABLE);
    340         }
    341         if (status == BTM_CMD_STARTED)
    342         {
    343             btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
    344 
    345             if (duration != 0)
    346                 /* start observer timer */
    347                 btu_start_timer (&btm_cb.ble_ctr_cb.obs_timer_ent, BTU_TTYPE_BLE_OBSERVE, duration);
    348         }
    349     }
    350     else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
    351     {
    352         status = BTM_CMD_STARTED;
    353         btm_ble_stop_observe();
    354     }
    355     else
    356     {
    357         BTM_TRACE_ERROR("Observe not active");
    358     }
    359 
    360     return status;
    361 
    362 }
    363 
    364 /*******************************************************************************
    365 **
    366 ** Function         BTM_BleBroadcast
    367 **
    368 ** Description      This function is to start or stop broadcasting.
    369 **
    370 ** Parameters       start: start or stop broadcasting.
    371 **
    372 ** Returns          status.
    373 **
    374 *******************************************************************************/
    375 tBTM_STATUS BTM_BleBroadcast(BOOLEAN start)
    376 {
    377     tBTM_STATUS status = BTM_NO_RESOURCES;
    378     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
    379     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
    380     UINT8 evt_type = p_cb->scan_rsp ? BTM_BLE_DISCOVER_EVT: BTM_BLE_NON_CONNECT_EVT;
    381 
    382     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    383         return BTM_ILLEGAL_VALUE;
    384 
    385 #ifdef  BTM_BLE_PC_ADV_TEST_MODE
    386     if (BTM_BLE_PC_ADV_TEST_MODE)
    387     {
    388         evt_type = p_cb->scan_rsp ? BTM_BLE_CONNECT_EVT: BTM_BLE_NON_CONNECT_EVT;
    389     }
    390 #endif
    391 
    392     if (start && p_cb->adv_mode == BTM_BLE_ADV_DISABLE)
    393     {
    394         /* update adv params */
    395         if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min :
    396                                               BTM_BLE_GAP_ADV_INT),
    397                                               (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max :
    398                                               BTM_BLE_GAP_ADV_INT),
    399                                               evt_type,
    400                                               p_addr_cb->own_addr_type,
    401                                               p_cb->direct_bda.type,
    402                                               p_cb->direct_bda.bda,
    403                                               p_cb->adv_chnl_map,
    404                                               p_cb->afp))
    405 
    406             status = BTM_NO_RESOURCES;
    407         else
    408             p_cb->evt_type = evt_type;
    409 
    410         status = btm_ble_start_adv ();
    411     }
    412     else if (!start)
    413     {
    414         status = btm_ble_stop_adv();
    415     }
    416     else
    417     {
    418         status = BTM_WRONG_MODE;
    419         BTM_TRACE_ERROR("Can not %s Broadcast, device %s in Broadcast mode",
    420             (start ? "Start" : "Stop"), (start ? "already" :"not"));
    421     }
    422     return status;
    423 }
    424 
    425 /*******************************************************************************
    426 **
    427 ** Function         btm_vsc_brcm_features_complete
    428 **
    429 ** Description      Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
    430 **
    431 ** Returns          void
    432 **
    433 *******************************************************************************/
    434 static void btm_ble_vendor_capability_vsc_cmpl_cback (tBTM_VSC_CMPL *p_vcs_cplt_params)
    435 {
    436 #if BLE_VND_INCLUDED == TRUE
    437     UINT8  status = 0xFF, *p;
    438 
    439     BTM_TRACE_DEBUG("btm_ble_vendor_capability_vsc_cmpl_cback");
    440 
    441     /* Check status of command complete event */
    442     if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) &&(p_vcs_cplt_params->param_len > 0))
    443     {
    444         p = p_vcs_cplt_params->p_param_buf;
    445         STREAM_TO_UINT8  (status, p);
    446     }
    447 
    448     if (status == HCI_SUCCESS)
    449     {
    450         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
    451         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
    452         STREAM_TO_UINT16 (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
    453         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
    454         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.filter_support, p);
    455         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.max_filter, p);
    456         STREAM_TO_UINT8  (btm_cb.cmn_ble_vsc_cb.energy_support, p);
    457         btm_cb.cmn_ble_vsc_cb.values_read = TRUE;
    458     }
    459 
    460     BTM_TRACE_DEBUG("btm_ble_vnd_cap_vsc_cmpl_cback: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d",
    461          status, btm_ble_vendor_cb.irk_avail_size, btm_cb.cmn_ble_vsc_cb.adv_inst_max,
    462          btm_cb.cmn_ble_vsc_cb.rpa_offloading, btm_cb.cmn_ble_vsc_cb.energy_support);
    463 
    464     if (BTM_BleMaxMultiAdvInstanceCount() > 0)
    465         btm_ble_multi_adv_init();
    466 
    467     if (btm_cb.cmn_ble_vsc_cb.max_filter > 0)
    468     {
    469         btm_ble_adv_filter_init();
    470     }
    471 
    472     if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0)
    473     {
    474         btm_ble_vendor_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
    475     }
    476 
    477     if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0)
    478         btm_ble_batchscan_init();
    479 
    480     if (p_ctrl_le_feature_rd_cmpl_cback != NULL)
    481         p_ctrl_le_feature_rd_cmpl_cback(status);
    482 
    483 #else
    484     UNUSED(p_vcs_cplt_params);
    485 #endif
    486 }
    487 
    488 /*******************************************************************************
    489 **
    490 ** Function         BTM_BleGetVendorCapabilities
    491 **
    492 ** Description      This function reads local LE features
    493 **
    494 ** Parameters       p_cmn_vsc_cb : Locala LE capability structure
    495 **
    496 ** Returns          void
    497 **
    498 *******************************************************************************/
    499 BTM_API extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB *p_cmn_vsc_cb)
    500 {
    501     BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
    502 
    503     if (NULL != p_cmn_vsc_cb)
    504     {
    505         *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
    506     }
    507 }
    508 
    509 /******************************************************************************
    510 **
    511 ** Function         BTM_BleReadControllerFeatures
    512 **
    513 ** Description      Reads BLE specific controller features
    514 **
    515 ** Parameters:      tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when features are read
    516 **
    517 ** Returns          void
    518 **
    519 *******************************************************************************/
    520 BTM_API extern void BTM_BleReadControllerFeatures(tBTM_BLE_CTRL_FEATURES_CBACK  *p_vsc_cback)
    521 {
    522     if (TRUE == btm_cb.cmn_ble_vsc_cb.values_read)
    523         return;
    524 
    525 #if BLE_VND_INCLUDED == TRUE
    526     BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
    527 
    528     p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
    529     if ( BTM_VendorSpecificCommand (HCI_BLE_VENDOR_CAP_OCF,
    530                                     0,
    531                                     NULL,
    532                                     btm_ble_vendor_capability_vsc_cmpl_cback)
    533                                     != BTM_CMD_STARTED)
    534     {
    535         BTM_TRACE_ERROR("LE Get_Vendor Capabilities Command Failed.");
    536     }
    537 #else
    538     UNUSED(p_vsc_cback);
    539 #endif
    540     return ;
    541 }
    542 
    543 /*******************************************************************************
    544 **
    545 ** Function         BTM_RegisterScanReqEvt
    546 **
    547 ** Description      This function is called to register a scan request callback
    548 **                  on the advertiser.
    549 **
    550 ** Parameters       p_scan_req_cback: scan request callback.  If NULL, remove the
    551 **                                    registration.
    552 **
    553 ** Returns          void
    554 **
    555 *******************************************************************************/
    556 void BTM_RegisterScanReqEvt(tBTM_BLE_SCAN_REQ_CBACK   *p_scan_req_cback)
    557 {
    558 #ifdef BTM_BLE_PC_ADV_TEST_MODE /* For general stack code (e.g. BTInsight testing), we simply do not define it to exclude or set it to TRUE to include */
    559     if (BTM_BLE_PC_ADV_TEST_MODE)   /* For stack component, it is always defined and maps to a global variable g_bDraculaAdvertisingMode */
    560     {
    561         tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
    562         p_cb->p_scan_req_cback = p_scan_req_cback;
    563     }
    564 #else
    565     UNUSED(p_scan_req_cback);
    566 #endif
    567 }
    568 
    569 /*******************************************************************************
    570 **
    571 ** Function         BTM_BleConfigPrivacy
    572 **
    573 ** Description      This function is called to enable or disable the privacy in
    574 **                   LE channel of the local device.
    575 **
    576 ** Parameters       enable: TRUE to enable it; FALSE to disable it.
    577 **
    578 ** Returns          void
    579 **
    580 *******************************************************************************/
    581 void BTM_BleConfigPrivacy(BOOLEAN enable)
    582 {
    583     tBTM_BLE_CB     *p_cb = &btm_cb.ble_ctr_cb;
    584 
    585     BTM_TRACE_EVENT (" BTM_BleConfigPrivacy");
    586 
    587     if (p_cb->privacy != enable)
    588     {
    589         p_cb->privacy = enable;
    590 
    591         if (p_cb->privacy)
    592         {
    593             /* generate resolvable private address */
    594             btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
    595         }
    596         else /* if privacy disabled, always use public address */
    597         {
    598             p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
    599         }
    600 
    601         if (BTM_BleMaxMultiAdvInstanceCount() > 0)
    602             btm_ble_multi_adv_enb_privacy(p_cb->privacy);
    603     }
    604 }
    605 
    606 /*******************************************************************************
    607 **
    608 ** Function          BTM_BleMaxMultiAdvInstanceCount
    609 **
    610 ** Description        Returns max number of multi adv instances supported by controller
    611 **
    612 ** Returns          Max multi adv instance count
    613 **
    614 *******************************************************************************/
    615 BTM_API extern UINT8  BTM_BleMaxMultiAdvInstanceCount()
    616 {
    617     return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX ?
    618         btm_cb.cmn_ble_vsc_cb.adv_inst_max : BTM_BLE_MULTI_ADV_MAX;
    619 }
    620 
    621 #if BLE_PRIVACY_SPT == TRUE
    622 /*******************************************************************************
    623 **
    624 ** Function         btm_ble_resolve_random_addr_on_adv
    625 **
    626 ** Description      resolve random address complete callback.
    627 **
    628 ** Returns          void
    629 **
    630 *******************************************************************************/
    631 static void btm_ble_resolve_random_addr_on_adv(void * p_rec, void *p)
    632 {
    633     tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
    634     UINT8       addr_type = BLE_ADDR_RANDOM;
    635     BD_ADDR     bda;
    636     UINT8       *pp = (UINT8 *)p + 1;
    637     UINT8           evt_type;
    638 
    639     BTM_TRACE_EVENT ("btm_ble_resolve_random_addr_on_adv ");
    640 
    641     STREAM_TO_UINT8    (evt_type, pp);
    642     STREAM_TO_UINT8    (addr_type, pp);
    643     STREAM_TO_BDADDR   (bda, pp);
    644 
    645     if (match_rec)
    646     {
    647         BTM_TRACE_DEBUG("Random match");
    648         match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
    649         memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
    650         memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
    651         addr_type = match_rec->ble.ble_addr_type;
    652     }
    653 
    654     btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pp);
    655 
    656     return;
    657 }
    658 #endif
    659 
    660 /*******************************************************************************
    661 **
    662 ** Function         BTM_BleLocalPrivacyEnabled
    663 **
    664 ** Description        Checks if local device supports private address
    665 **
    666 ** Returns          Return TRUE if local privacy is enabled else FALSE
    667 **
    668 *******************************************************************************/
    669 BOOLEAN BTM_BleLocalPrivacyEnabled()
    670 {
    671 #if BLE_PRIVACY_SPT == TRUE
    672     return btm_cb.ble_ctr_cb.privacy;
    673 #else
    674     return false;
    675 #endif
    676 }
    677 
    678 /*******************************************************************************
    679 **
    680 ** Function         BTM_BleSetBgConnType
    681 **
    682 ** Description      This function is called to set BLE connectable mode for a
    683 **                  peripheral device.
    684 **
    685 ** Parameters       bg_conn_type: it can be auto connection, or selective connection.
    686 **                  p_select_cback: callback function when selective connection procedure
    687 **                              is being used.
    688 **
    689 ** Returns          void
    690 **
    691 *******************************************************************************/
    692 BOOLEAN BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE   bg_conn_type,
    693                              tBTM_BLE_SEL_CBACK   *p_select_cback)
    694 {
    695     BOOLEAN started = TRUE;
    696 
    697     BTM_TRACE_EVENT ("BTM_BleSetBgConnType ");
    698     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    699         return FALSE;
    700 
    701     if (btm_cb.ble_ctr_cb.bg_conn_type != bg_conn_type)
    702     {
    703         switch (bg_conn_type)
    704         {
    705             case BTM_BLE_CONN_AUTO:
    706                 btm_ble_start_auto_conn(TRUE);
    707                 break;
    708 
    709             case BTM_BLE_CONN_SELECTIVE:
    710                 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
    711                 {
    712                     btm_ble_start_auto_conn(FALSE);
    713                 }
    714                 started = btm_ble_start_select_conn(TRUE, p_select_cback);
    715                 break;
    716 
    717             case BTM_BLE_CONN_NONE:
    718                 if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO)
    719                 {
    720                     btm_ble_start_auto_conn(FALSE);
    721                 }
    722                 else if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE)
    723                 {
    724                     btm_ble_start_select_conn(FALSE, NULL);
    725                 }
    726                 started = TRUE;
    727                 break;
    728 
    729             default:
    730                 BTM_TRACE_ERROR("invalid bg connection type : %d ", bg_conn_type);
    731                 started = FALSE;
    732                 break;
    733         }
    734 
    735         if (started)
    736             btm_cb.ble_ctr_cb.bg_conn_type = bg_conn_type;
    737     }
    738     return started;
    739 }
    740 
    741 /*******************************************************************************
    742 **
    743 ** Function         BTM_BleClearBgConnDev
    744 **
    745 ** Description      This function is called to clear the whitelist,
    746 **                  end any pending whitelist connections,
    747 *                   and reset the local bg device list.
    748 **
    749 ** Parameters       void
    750 **
    751 ** Returns          void
    752 **
    753 *******************************************************************************/
    754 void BTM_BleClearBgConnDev(void)
    755 {
    756     btm_ble_start_auto_conn(FALSE);
    757     btm_ble_clear_white_list();
    758     gatt_reset_bgdev_list();
    759 }
    760 
    761 /*******************************************************************************
    762 **
    763 ** Function         BTM_BleUpdateBgConnDev
    764 **
    765 ** Description      This function is called to add or remove a device into/from
    766 **                  background connection procedure. The background connection
    767 *                   procedure is decided by the background connection type, it can be
    768 *                   auto connection, or selective connection.
    769 **
    770 ** Parameters       add_remove: TRUE to add; FALSE to remove.
    771 **                  remote_bda: device address to add/remove.
    772 **
    773 ** Returns          void
    774 **
    775 *******************************************************************************/
    776 BOOLEAN BTM_BleUpdateBgConnDev(BOOLEAN add_remove, BD_ADDR   remote_bda)
    777 {
    778     BOOLEAN ret = TRUE;
    779     BTM_TRACE_EVENT (" BTM_BleUpdateBgConnDev");
    780 
    781     /* update white list */
    782     ret = btm_update_bg_conn_list(add_remove, remote_bda);
    783 
    784     btm_update_dev_to_white_list(add_remove, remote_bda);
    785 
    786     return ret;
    787 }
    788 
    789 /*******************************************************************************
    790 **
    791 ** Function         BTM_BleSetConnMode
    792 **
    793 ** Description      This function is called to set BLE connectable mode for a
    794 **                  peripheral device.
    795 **
    796 ** Parameters       directed: is directed connectable mode, or non-directed.
    797 **                  p_dir_bda: connectable direct initiator's LE device address
    798 **
    799 ** Returns          void
    800 **
    801 *******************************************************************************/
    802 tBTM_STATUS BTM_BleSetConnMode(BOOLEAN is_directed)
    803 {
    804     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
    805 
    806     BTM_TRACE_EVENT ("BTM_BleSetConnMode is_directed = %d ", is_directed);
    807     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    808         return BTM_ILLEGAL_VALUE;
    809 
    810     p_cb->directed_conn = is_directed;
    811     return btm_ble_set_connectability( p_cb->connectable_mode);
    812 
    813 }
    814 
    815 /*******************************************************************************
    816 **
    817 ** Function         btm_set_conn_mode_adv_init_addr
    818 **
    819 ** Description      set initator address type and local address type based on adv
    820 **                  mode.
    821 **
    822 **
    823 *******************************************************************************/
    824 static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
    825                                      BD_ADDR_PTR p_addr_ptr,
    826                                      tBLE_ADDR_TYPE *p_init_addr_type,
    827                                      tBLE_ADDR_TYPE *p_own_addr_type)
    828 {
    829     UINT8 evt_type;
    830 
    831     UNUSED(p_own_addr_type);
    832 
    833     if ( p_cb->directed_conn)
    834     {
    835         /*  direct adv mode does not have privacy if privacy
    836         is not enabled or no reconn addr config */
    837 
    838         *p_init_addr_type  = p_cb->direct_bda.type;
    839          memcpy(p_addr_ptr, p_cb->direct_bda.bda, BD_ADDR_LEN);
    840         evt_type = BTM_BLE_CONNECT_DIR_EVT;
    841     }
    842     else /* undirect adv mode */
    843     {
    844         evt_type = BTM_BLE_CONNECT_EVT;
    845 
    846         /* may need to reset random address if privacy is enabled */
    847         if (btm_cb.ble_ctr_cb.privacy && /* own addr_type is random */
    848             !BTM_BLE_IS_RESOLVE_BDA(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr))
    849         {
    850             /* need to generate RRA and update random addresss in controller */
    851             btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
    852         }
    853     }
    854 
    855     return evt_type;
    856 
    857 }
    858 
    859 /*******************************************************************************
    860 **
    861 ** Function         BTM_BleSetAdvParams
    862 **
    863 ** Description      This function is called to set advertising parameters.
    864 **
    865 ** Parameters       adv_int_min: minimum advertising interval
    866 **                  adv_int_max: maximum advertising interval
    867 **                  p_dir_bda: connectable direct initiator's LE device address
    868 **                  chnl_map: advertising channel map.
    869 **
    870 ** Returns          void
    871 **
    872 *******************************************************************************/
    873 tBTM_STATUS BTM_BleSetAdvParams(UINT16 adv_int_min, UINT16 adv_int_max,
    874                                 tBLE_BD_ADDR *p_dir_bda,
    875                                 tBTM_BLE_ADV_CHNL_MAP chnl_map)
    876 {
    877     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
    878     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
    879     tBTM_STATUS status = BTM_SUCCESS;
    880     BD_ADDR     p_addr_ptr =  {0};
    881     tBLE_ADDR_TYPE   init_addr_type = BLE_ADDR_PUBLIC;
    882     tBLE_ADDR_TYPE   own_addr_type = p_addr_cb->own_addr_type;
    883     UINT8            adv_mode = p_cb->adv_mode;
    884 
    885     BTM_TRACE_EVENT ("BTM_BleSetAdvParams");
    886 
    887     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    888         return BTM_ILLEGAL_VALUE;
    889 
    890     if (!BTM_BLE_VALID_PRAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
    891         !BTM_BLE_VALID_PRAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX))
    892     {
    893         return BTM_ILLEGAL_VALUE;
    894     }
    895 
    896     p_cb->adv_interval_min = adv_int_min;
    897     p_cb->adv_interval_max = adv_int_max;
    898     p_cb->adv_chnl_map = chnl_map;
    899 
    900     if (p_dir_bda)
    901     {
    902         memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
    903     }
    904 
    905     BTM_TRACE_EVENT ("update params for an active adv");
    906 
    907     btm_ble_stop_adv();
    908 
    909     if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
    910         p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
    911 
    912     /* update adv params */
    913     btsnd_hcic_ble_write_adv_params (p_cb->adv_interval_min,
    914                                      p_cb->adv_interval_max,
    915                                      p_cb->evt_type,
    916                                      own_addr_type,
    917                                      init_addr_type,
    918                                      p_addr_ptr,
    919                                      p_cb->adv_chnl_map,
    920                                      p_cb->afp);
    921 
    922     if (adv_mode == BTM_BLE_ADV_ENABLE)
    923         btm_ble_start_adv();
    924 
    925     return status;
    926 }
    927 
    928 /*******************************************************************************
    929 **
    930 ** Function         BTM_BleReadAdvParams
    931 **
    932 ** Description      This function is called to set advertising parameters.
    933 **
    934 ** Parameters       adv_int_min: minimum advertising interval
    935 **                  adv_int_max: maximum advertising interval
    936 **                  p_dir_bda: connectable direct initiator's LE device address
    937 **                  chnl_map: advertising channel map.
    938 **
    939 ** Returns          void
    940 **
    941 *******************************************************************************/
    942 void BTM_BleReadAdvParams (UINT16 *adv_int_min, UINT16 *adv_int_max,
    943                            tBLE_BD_ADDR *p_dir_bda, tBTM_BLE_ADV_CHNL_MAP *p_chnl_map)
    944 {
    945     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
    946 
    947     BTM_TRACE_EVENT ("BTM_BleReadAdvParams ");
    948     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    949         return ;
    950 
    951     *adv_int_min = p_cb->adv_interval_min;
    952     *adv_int_max = p_cb->adv_interval_max;
    953     *p_chnl_map = p_cb->adv_chnl_map;
    954 
    955     if (p_dir_bda != NULL)
    956     {
    957         memcpy(p_dir_bda, &p_cb->direct_bda, sizeof(tBLE_BD_ADDR));
    958     }
    959 }
    960 
    961 /*******************************************************************************
    962 **
    963 ** Function         BTM_BleSetScanParams
    964 **
    965 ** Description      This function is called to set Scan parameters.
    966 **
    967 ** Parameters       adv_int_min: minimum advertising interval
    968 **                  adv_int_max: maximum advertising interval
    969 **                  p_dir_bda: connectable direct initiator's LE device address
    970 **                  chnl_map: advertising channel map.
    971 **                  scan_type: active scan or passive scan
    972 **
    973 ** Returns          void
    974 **
    975 *******************************************************************************/
    976 void BTM_BleSetScanParams(UINT16 scan_interval, UINT16 scan_window, tBTM_BLE_SCAN_MODE scan_mode)
    977 {
    978     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
    979 
    980     BTM_TRACE_EVENT (" BTM_BleSetScanParams");
    981     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
    982         return ;
    983 
    984     if (BTM_BLE_VALID_PRAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
    985         BTM_BLE_VALID_PRAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX) &&
    986         (scan_mode == BTM_BLE_SCAN_MODE_ACTI || scan_mode == BTM_BLE_SCAN_MODE_PASS))
    987     {
    988         p_cb->scan_type     = scan_mode;
    989 
    990         if (BTM_BLE_CONN_PARAM_UNDEF != scan_interval)
    991             p_cb->scan_interval = scan_interval;
    992 
    993         if (BTM_BLE_CONN_PARAM_UNDEF != scan_window)
    994             p_cb->scan_window   = scan_window;
    995     }
    996     else
    997     {
    998         BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
    999                         scan_interval, scan_window);
   1000     }
   1001 
   1002 }
   1003 
   1004 /*******************************************************************************
   1005 **
   1006 ** Function         BTM_BleWriteScanRsp
   1007 **
   1008 ** Description      This function is called to write LE scan response.
   1009 **
   1010 ** Parameters:      p_scan_rsp: scan response information.
   1011 **
   1012 ** Returns          void
   1013 **
   1014 *******************************************************************************/
   1015 tBTM_STATUS BTM_BleWriteScanRsp(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
   1016 {
   1017     tBTM_STATUS     status = BTM_NO_RESOURCES;
   1018     UINT8   rsp_data[BTM_BLE_AD_DATA_LEN],
   1019             *p = rsp_data;
   1020 
   1021     BTM_TRACE_EVENT (" BTM_BleWriteScanRsp");
   1022 
   1023     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
   1024         return BTM_ILLEGAL_VALUE;
   1025 
   1026     memset(rsp_data, 0, BTM_BLE_AD_DATA_LEN);
   1027     btm_ble_build_adv_data(&data_mask, &p, p_data);
   1028 
   1029     if (btsnd_hcic_ble_set_scan_rsp_data((UINT8)(p - rsp_data), rsp_data))
   1030     {
   1031         status = BTM_SUCCESS;
   1032 
   1033         if (data_mask != 0)
   1034             btm_cb.ble_ctr_cb.inq_var.scan_rsp = TRUE;
   1035         else
   1036             btm_cb.ble_ctr_cb.inq_var.scan_rsp = FALSE;
   1037     }
   1038     else
   1039         status = BTM_ILLEGAL_VALUE;
   1040 
   1041     return status;
   1042 }
   1043 
   1044 /*******************************************************************************
   1045 **
   1046 ** Function         BTM_BleWriteAdvData
   1047 **
   1048 ** Description      This function is called to write advertising data.
   1049 **
   1050 ** Parameters:       None.
   1051 **
   1052 ** Returns          void
   1053 **
   1054 *******************************************************************************/
   1055 tBTM_STATUS BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
   1056 {
   1057     tBTM_BLE_LOCAL_ADV_DATA *p_cb_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
   1058     UINT8  *p;
   1059     tBTM_BLE_AD_MASK   mask = data_mask;
   1060 
   1061     BTM_TRACE_EVENT ("BTM_BleWriteAdvData ");
   1062 
   1063     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
   1064         return BTM_ILLEGAL_VALUE;
   1065 
   1066     memset(p_cb_data, 0, sizeof(tBTM_BLE_LOCAL_ADV_DATA));
   1067     p = p_cb_data->ad_data;
   1068     p_cb_data->data_mask = data_mask;
   1069 
   1070     p_cb_data->p_flags = btm_ble_build_adv_data(&mask, &p, p_data);
   1071 
   1072     p_cb_data->p_pad = p;
   1073 
   1074     if (mask != 0)
   1075     {
   1076         BTM_TRACE_ERROR("Partial data write into ADV");
   1077     }
   1078 
   1079     p_cb_data->data_mask &= ~mask;
   1080 
   1081     if (btsnd_hcic_ble_set_adv_data((UINT8)(p_cb_data->p_pad - p_cb_data->ad_data),
   1082                                     p_cb_data->ad_data))
   1083         return BTM_SUCCESS;
   1084     else
   1085         return BTM_NO_RESOURCES;
   1086 
   1087 }
   1088 
   1089 /*******************************************************************************
   1090 **
   1091 ** Function         BTM_CheckAdvData
   1092 **
   1093 ** Description      This function is called to get ADV data for a specific type.
   1094 **
   1095 ** Parameters       p_adv - pointer of ADV data
   1096 **                  type   - finding ADV data type
   1097 **                  p_length - return the length of ADV data not including type
   1098 **
   1099 ** Returns          pointer of ADV data
   1100 **
   1101 *******************************************************************************/
   1102 UINT8 *BTM_CheckAdvData( UINT8 *p_adv, UINT8 type, UINT8 *p_length)
   1103 {
   1104     UINT8 *p = p_adv;
   1105     UINT8 length;
   1106     UINT8 adv_type;
   1107     BTM_TRACE_API("BTM_CheckAdvData type=0x%02X", type);
   1108 
   1109     STREAM_TO_UINT8(length, p);
   1110 
   1111     while ( length && (p - p_adv <= BTM_BLE_CACHE_ADV_DATA_MAX))
   1112     {
   1113         STREAM_TO_UINT8(adv_type, p);
   1114 
   1115         if ( adv_type == type )
   1116         {
   1117             /* length doesn't include itself */
   1118             *p_length = length - 1; /* minus the length of type */
   1119             return p;
   1120         }
   1121         p += length - 1; /* skip the length of data */
   1122         STREAM_TO_UINT8(length, p);
   1123     }
   1124 
   1125     *p_length = 0;
   1126     return NULL;
   1127 }
   1128 
   1129 /*******************************************************************************
   1130 **
   1131 ** Function         btm_ble_build_adv_data
   1132 **
   1133 ** Description      This function is called build the adv data and rsp data.
   1134 *******************************************************************************/
   1135 UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
   1136                               tBTM_BLE_ADV_DATA *p_data)
   1137 {
   1138     UINT32 data_mask = *p_data_mask;
   1139     UINT8   *p = *p_dst,
   1140     *p_flag = NULL;
   1141     UINT16  len = BTM_BLE_AD_DATA_LEN, cp_len = 0;
   1142     UINT8   i = 0;
   1143     tBTM_BLE_PROP_ELEM      *p_elem;
   1144 
   1145     BTM_TRACE_EVENT (" btm_ble_build_adv_data");
   1146 
   1147     /* build the adv data structure and build the data string */
   1148     if (data_mask)
   1149     {
   1150         /* flags */
   1151         if (data_mask & BTM_BLE_AD_BIT_FLAGS)
   1152         {
   1153             *p++ = MIN_ADV_LENGTH;
   1154             *p++ = BTM_BLE_AD_TYPE_FLAG;
   1155             p_flag = p;
   1156             if (p_data)
   1157                 *p++ = p_data->flag;
   1158             else
   1159                 *p++ = 0;
   1160 
   1161             len -= 3;
   1162 
   1163             data_mask &= ~BTM_BLE_AD_BIT_FLAGS;
   1164         }
   1165         /* appearance data */
   1166         if (len > 3 && data_mask & BTM_BLE_AD_BIT_APPEARANCE)
   1167         {
   1168             *p++ = 3; /* length */
   1169             *p++ = BTM_BLE_AD_TYPE_APPEARANCE;
   1170             UINT16_TO_STREAM(p, p_data->appearance);
   1171             len -= 4;
   1172 
   1173             data_mask &= ~BTM_BLE_AD_BIT_APPEARANCE;
   1174         }
   1175         /* device name */
   1176 #if BTM_MAX_LOC_BD_NAME_LEN > 0
   1177         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_DEV_NAME)
   1178         {
   1179             if (strlen(btm_cb.cfg.bd_name) > (UINT16)(len - MIN_ADV_LENGTH))
   1180             {
   1181                 *p++ = len - MIN_ADV_LENGTH + 1;
   1182                 *p++ = BTM_BLE_AD_TYPE_NAME_SHORT;
   1183                 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, len - MIN_ADV_LENGTH);
   1184             }
   1185             else
   1186             {
   1187                 cp_len = (UINT16)strlen(btm_cb.cfg.bd_name);
   1188                 *p++ = cp_len + 1;
   1189                 *p++ = BTM_BLE_AD_TYPE_NAME_CMPL;
   1190                 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, cp_len);
   1191             }
   1192             len -= (cp_len + MIN_ADV_LENGTH);
   1193             data_mask &= ~BTM_BLE_AD_BIT_DEV_NAME;
   1194         }
   1195 #endif
   1196         /* manufacturer data */
   1197         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_MANU &&
   1198             p_data && p_data->p_manu &&
   1199             p_data->p_manu->len != 0 && p_data->p_manu->p_val)
   1200         {
   1201             if (p_data->p_manu->len > (len - MIN_ADV_LENGTH))
   1202                 cp_len = len - MIN_ADV_LENGTH;
   1203             else
   1204                 cp_len = p_data->p_manu->len;
   1205 
   1206             *p++ = cp_len + 1;
   1207             *p++ = BTM_BLE_AD_TYPE_MANU;
   1208             ARRAY_TO_STREAM(p, p_data->p_manu->p_val, cp_len);
   1209 
   1210             len -= (cp_len + MIN_ADV_LENGTH);
   1211             data_mask &= ~BTM_BLE_AD_BIT_MANU;
   1212         }
   1213         /* TX power */
   1214         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_TX_PWR)
   1215         {
   1216             *p++ = MIN_ADV_LENGTH;
   1217             *p++ = BTM_BLE_AD_TYPE_TX_PWR;
   1218             if (p_data->tx_power > BTM_BLE_ADV_TX_POWER_MAX)
   1219                 p_data->tx_power = BTM_BLE_ADV_TX_POWER_MAX;
   1220             *p++ = btm_ble_map_adv_tx_power(p_data->tx_power);
   1221             len -= 3;
   1222             data_mask &= ~BTM_BLE_AD_BIT_TX_PWR;
   1223         }
   1224         /* 16 bits services */
   1225         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE &&
   1226             p_data && p_data->p_services &&
   1227             p_data->p_services->num_service != 0 &&
   1228             p_data->p_services->p_uuid)
   1229         {
   1230             if (p_data->p_services->num_service * LEN_UUID_16 > (len - MIN_ADV_LENGTH))
   1231             {
   1232                 cp_len = (len - MIN_ADV_LENGTH)/LEN_UUID_16;
   1233                 *p ++ = 1 + cp_len * LEN_UUID_16;
   1234                 *p++ = BTM_BLE_AD_TYPE_16SRV_PART;
   1235             }
   1236             else
   1237             {
   1238                 cp_len = p_data->p_services->num_service;
   1239                 *p++ = 1 + cp_len * LEN_UUID_16;
   1240                 *p++ = BTM_BLE_AD_TYPE_16SRV_CMPL;
   1241             }
   1242             for (i = 0; i < cp_len; i ++)
   1243             {
   1244                 UINT16_TO_STREAM(p, *(p_data->p_services->p_uuid + i));
   1245             }
   1246 
   1247             len -= (cp_len * MIN_ADV_LENGTH + MIN_ADV_LENGTH);
   1248             data_mask &= ~BTM_BLE_AD_BIT_SERVICE;
   1249         }
   1250         /* 32 bits service uuid */
   1251         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_32 &&
   1252             p_data && p_data->p_service_32b &&
   1253             p_data->p_service_32b->num_service != 0 &&
   1254             p_data->p_service_32b->p_uuid)
   1255         {
   1256             if ((p_data->p_service_32b->num_service * LEN_UUID_32) > (len - MIN_ADV_LENGTH))
   1257             {
   1258                 cp_len = (len - MIN_ADV_LENGTH)/LEN_UUID_32;
   1259                 *p ++ = 1 + cp_len * LEN_UUID_32;
   1260                 *p++ = BTM_BLE_AD_TYPE_32SRV_PART;
   1261             }
   1262             else
   1263             {
   1264                 cp_len = p_data->p_service_32b->num_service;
   1265                 *p++ = 1 + cp_len * LEN_UUID_32;
   1266                 *p++ = BTM_BLE_AD_TYPE_32SRV_CMPL;
   1267             }
   1268             for (i = 0; i < cp_len; i ++)
   1269             {
   1270                 UINT32_TO_STREAM(p, *(p_data->p_service_32b->p_uuid + i));
   1271             }
   1272 
   1273             len -= (cp_len * LEN_UUID_32 + MIN_ADV_LENGTH);
   1274             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_32;
   1275         }
   1276         /* 128 bits services */
   1277         if (len >= (MAX_UUID_SIZE + 2) && data_mask & BTM_BLE_AD_BIT_SERVICE_128 &&
   1278             p_data && p_data->p_services_128b)
   1279         {
   1280             *p ++ = 1 + MAX_UUID_SIZE;
   1281             if (!p_data->p_services_128b->list_cmpl)
   1282                 *p++ = BTM_BLE_AD_TYPE_128SRV_PART;
   1283             else
   1284                 *p++ = BTM_BLE_AD_TYPE_128SRV_CMPL;
   1285 
   1286             ARRAY_TO_STREAM(p, p_data->p_services_128b->uuid128, MAX_UUID_SIZE);
   1287 
   1288             len -= (MAX_UUID_SIZE + MIN_ADV_LENGTH);
   1289             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_128;
   1290         }
   1291         /* 32 bits Service Solicitation UUIDs */
   1292         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_32SOL &&
   1293             p_data && p_data->p_sol_service_32b &&
   1294             p_data->p_sol_service_32b->num_service != 0 &&
   1295             p_data->p_sol_service_32b->p_uuid)
   1296         {
   1297             if ((p_data->p_sol_service_32b->num_service * LEN_UUID_32) > (len - MIN_ADV_LENGTH))
   1298             {
   1299                 cp_len = (len - MIN_ADV_LENGTH)/LEN_UUID_32;
   1300                 *p ++ = 1 + cp_len * LEN_UUID_32;
   1301             }
   1302             else
   1303             {
   1304                 cp_len = p_data->p_sol_service_32b->num_service;
   1305                 *p++ = 1 + cp_len * LEN_UUID_32;
   1306             }
   1307 
   1308             *p++ = BTM_BLE_AD_TYPE_32SOL_SRV_UUID;
   1309             for (i = 0; i < cp_len; i ++)
   1310             {
   1311                 UINT32_TO_STREAM(p, *(p_data->p_sol_service_32b->p_uuid + i));
   1312             }
   1313 
   1314             len -= (cp_len * LEN_UUID_32 + MIN_ADV_LENGTH);
   1315             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_32SOL;
   1316         }
   1317         /* 128 bits Solicitation services UUID */
   1318         if (len >= (MAX_UUID_SIZE + MIN_ADV_LENGTH) && data_mask & BTM_BLE_AD_BIT_SERVICE_128SOL &&
   1319             p_data && p_data->p_sol_service_128b)
   1320         {
   1321             *p ++ = 1 + MAX_UUID_SIZE;
   1322             *p++ = BTM_BLE_AD_TYPE_128SOL_SRV_UUID;
   1323             ARRAY_TO_STREAM(p, p_data->p_sol_service_128b->uuid128, MAX_UUID_SIZE);
   1324             len -= (MAX_UUID_SIZE + MIN_ADV_LENGTH);
   1325             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_128SOL;
   1326         }
   1327         /* 16bits/32bits/128bits Service Data */
   1328         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_DATA &&
   1329             p_data && p_data->p_service_data->len != 0 && p_data->p_service_data->p_val)
   1330         {
   1331             if (len  > (p_data->p_service_data->service_uuid.len + MIN_ADV_LENGTH))
   1332             {
   1333                 if (p_data->p_service_data->len > (len - MIN_ADV_LENGTH))
   1334                     cp_len = len - MIN_ADV_LENGTH- p_data->p_service_data->service_uuid.len;
   1335                 else
   1336                     cp_len = p_data->p_service_data->len;
   1337 
   1338                 *p++ = cp_len + 1 + p_data->p_service_data->service_uuid.len;
   1339                 if (p_data->p_service_data->service_uuid.len == LEN_UUID_16)
   1340                 {
   1341                     *p++ = BTM_BLE_AD_TYPE_SERVICE_DATA;
   1342                     UINT16_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid16);
   1343                 }
   1344                 else if (p_data->p_service_data->service_uuid.len == LEN_UUID_32)
   1345                 {
   1346                     *p++ = BTM_BLE_AD_TYPE_32SERVICE_DATA;
   1347                     UINT32_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid32);
   1348                 }
   1349                 else
   1350                 {
   1351                     *p++ = BTM_BLE_AD_TYPE_128SERVICE_DATA;
   1352                     ARRAY_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid128,
   1353                                     LEN_UUID_128);
   1354                 }
   1355 
   1356                 ARRAY_TO_STREAM(p, p_data->p_service_data->p_val, cp_len);
   1357 
   1358                 len -= (cp_len + MIN_ADV_LENGTH + p_data->p_service_data->service_uuid.len);
   1359                 data_mask &= ~BTM_BLE_AD_BIT_SERVICE_DATA;
   1360             }
   1361             else
   1362             {
   1363                 BTM_TRACE_WARNING("service data does not fit");
   1364             }
   1365         }
   1366 
   1367         if (len >= 6 && data_mask & BTM_BLE_AD_BIT_INT_RANGE &&
   1368             p_data)
   1369         {
   1370             *p++ = 5;
   1371             *p++ = BTM_BLE_AD_TYPE_INT_RANGE;
   1372             UINT16_TO_STREAM(p, p_data->int_range.low);
   1373             UINT16_TO_STREAM(p, p_data->int_range.hi);
   1374             len -= 6;
   1375             data_mask &= ~BTM_BLE_AD_BIT_INT_RANGE;
   1376         }
   1377         if (data_mask & BTM_BLE_AD_BIT_PROPRIETARY && p_data && p_data->p_proprietary)
   1378         {
   1379             for (i = 0; i < p_data->p_proprietary->num_elem ; i ++)
   1380             {
   1381                 p_elem = p_data->p_proprietary->p_elem  + i;
   1382 
   1383                 if (len >= (MIN_ADV_LENGTH + p_elem->len))/* len byte(1) + ATTR type(1) + Uuid len(2)
   1384                                                           + value length */
   1385                 {
   1386                     *p ++ = p_elem->len + 1; /* Uuid len + value length */
   1387                     *p ++ = p_elem->adv_type;
   1388                     ARRAY_TO_STREAM(p, p_elem->p_val, p_elem->len);
   1389 
   1390                     len -= (MIN_ADV_LENGTH + p_elem->len);
   1391                 }
   1392                 else
   1393                 {
   1394                     BTM_TRACE_WARNING("data exceed max adv packet length");
   1395                     break;
   1396                 }
   1397             }
   1398             data_mask &= ~BTM_BLE_AD_BIT_PROPRIETARY;
   1399         }
   1400     }
   1401 
   1402     *p_data_mask = data_mask;
   1403     *p_dst = p;
   1404 
   1405     return p_flag;
   1406 }
   1407 /*******************************************************************************
   1408 **
   1409 ** Function         btm_ble_select_adv_interval
   1410 **
   1411 ** Description      select adv interval based on device mode
   1412 **
   1413 ** Returns          void
   1414 **
   1415 *******************************************************************************/
   1416 void btm_ble_select_adv_interval(tBTM_BLE_INQ_CB *p_cb, UINT8 evt_type, UINT16 *p_adv_int_min, UINT16 *p_adv_int_max)
   1417 {
   1418     if (p_cb->adv_interval_min && p_cb->adv_interval_max)
   1419     {
   1420         *p_adv_int_min = p_cb->adv_interval_min;
   1421         *p_adv_int_max = p_cb->adv_interval_max;
   1422     }
   1423     else
   1424     {
   1425         switch (evt_type)
   1426         {
   1427         case BTM_BLE_CONNECT_EVT:
   1428             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
   1429             break;
   1430 
   1431         case BTM_BLE_NON_CONNECT_EVT:
   1432         case BTM_BLE_DISCOVER_EVT:
   1433             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
   1434             break;
   1435         /* connectable directed event */
   1436         case BTM_BLE_CONNECT_DIR_EVT:
   1437             *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
   1438             *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
   1439             break;
   1440 
   1441         default:
   1442             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
   1443             break;
   1444         }
   1445     }
   1446     return;
   1447 }
   1448 /*******************************************************************************
   1449 **
   1450 ** Function         btm_ble_set_adv_flag
   1451 **
   1452 ** Description      Set adv flag in adv data.
   1453 **
   1454 ** Returns          void
   1455 **
   1456 *******************************************************************************/
   1457 void btm_ble_set_adv_flag(UINT16 connect_mode, UINT16 disc_mode)
   1458 {
   1459     UINT8 flag = 0, old_flag = 0;
   1460     tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
   1461 
   1462     if (p_adv_data->p_flags != NULL)
   1463         flag = old_flag = *(p_adv_data->p_flags);
   1464 
   1465     /* BR/EDR non-discoverable , non-connectable */
   1466     if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
   1467         (connect_mode & BTM_CONNECTABLE_MASK) == 0)
   1468         flag |= BTM_BLE_BREDR_NOT_SPT;
   1469     else
   1470         flag &= ~BTM_BLE_BREDR_NOT_SPT;
   1471 
   1472     /* if local controller support, mark both controller and host support in flag */
   1473     if (HCI_SIMUL_LE_BREDR_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_0]))
   1474         flag |= (BTM_BLE_DMT_CONTROLLER_SPT|BTM_BLE_DMT_HOST_SPT);
   1475     else
   1476         flag &= ~(BTM_BLE_DMT_CONTROLLER_SPT|BTM_BLE_DMT_HOST_SPT);
   1477 
   1478     BTM_TRACE_DEBUG("disc_mode %04x", disc_mode);
   1479     /* update discoverable flag */
   1480     if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE)
   1481     {
   1482         flag &= ~BTM_BLE_GEN_DISC_FLAG;
   1483         flag |= BTM_BLE_LIMIT_DISC_FLAG ;
   1484     }
   1485     else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE)
   1486     {
   1487         flag |= BTM_BLE_GEN_DISC_FLAG;
   1488         flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
   1489     }
   1490     else /* remove all discoverable flags */
   1491     {
   1492         flag &= ~(BTM_BLE_LIMIT_DISC_FLAG|BTM_BLE_GEN_DISC_FLAG);
   1493     }
   1494 
   1495     if (flag != old_flag)
   1496     {
   1497         btm_ble_update_adv_flag(flag);
   1498     }
   1499 }
   1500 /*******************************************************************************
   1501 **
   1502 ** Function         btm_ble_set_discoverability
   1503 **
   1504 ** Description      This function is called to set BLE discoverable mode.
   1505 **
   1506 ** Parameters:      combined_mode: discoverability mode.
   1507 **
   1508 ** Returns          BTM_SUCCESS is status set successfully; otherwise failure.
   1509 **
   1510 *******************************************************************************/
   1511 tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode)
   1512 {
   1513     tBTM_LE_RANDOM_CB   *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
   1514     tBTM_BLE_INQ_CB     *p_cb = &btm_cb.ble_ctr_cb.inq_var;
   1515     UINT16              mode = (combined_mode &  BTM_BLE_DISCOVERABLE_MASK);
   1516     UINT8               new_mode = BTM_BLE_ADV_ENABLE;
   1517     UINT8               evt_type = (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE) ? \
   1518                                    ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT )\
   1519                                    : BTM_BLE_CONNECT_EVT;
   1520     tBTM_STATUS         status = BTM_SUCCESS;
   1521     BD_ADDR             p_addr_ptr= {0};
   1522     tBLE_ADDR_TYPE      init_addr_type = BLE_ADDR_PUBLIC,
   1523                         own_addr_type = p_addr_cb->own_addr_type;
   1524     UINT16              adv_int_min, adv_int_max;
   1525 
   1526     BTM_TRACE_EVENT ("btm_ble_set_discoverability mode=0x%0x combined_mode=0x%x", mode, combined_mode);
   1527 
   1528     /*** Check mode parameter ***/
   1529     if (mode > BTM_BLE_MAX_DISCOVERABLE)
   1530         return(BTM_ILLEGAL_VALUE);
   1531 
   1532     btm_ble_set_adv_flag (btm_cb.btm_inq_vars.connectable_mode, combined_mode);
   1533 
   1534     evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
   1535 
   1536     if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE && mode == BTM_BLE_NON_DISCOVERABLE)
   1537         new_mode = BTM_BLE_ADV_DISABLE;
   1538 
   1539     btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
   1540 
   1541     btu_stop_timer(&p_cb->fast_adv_timer);
   1542 
   1543     /* update adv params if start advertising */
   1544     BTM_TRACE_EVENT ("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type, p_cb->evt_type);
   1545 
   1546     if (new_mode == BTM_BLE_ADV_ENABLE &&
   1547         (evt_type != p_cb->evt_type ||p_cb->adv_addr_type != own_addr_type || !p_cb->fast_adv_on))
   1548     {
   1549         btm_ble_stop_adv();
   1550 
   1551         /* update adv params */
   1552         if (!btsnd_hcic_ble_write_adv_params (adv_int_min,
   1553                                               adv_int_max,
   1554                                               evt_type,
   1555                                               own_addr_type,
   1556                                               init_addr_type,
   1557                                               p_addr_ptr,
   1558                                               p_cb->adv_chnl_map,
   1559                                               p_cb->afp))
   1560 
   1561             status = BTM_NO_RESOURCES;
   1562         else
   1563         {
   1564             p_cb->evt_type = evt_type;
   1565             p_cb->adv_addr_type = own_addr_type;
   1566         }
   1567 
   1568     }
   1569 
   1570     if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode)
   1571     {
   1572         if (new_mode == BTM_BLE_ADV_ENABLE)
   1573             status = btm_ble_start_adv();
   1574         else
   1575             status = btm_ble_stop_adv();
   1576     }
   1577     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
   1578     {
   1579         p_cb->fast_adv_on = TRUE;
   1580         /* start initial GAP mode adv timer */
   1581         btu_start_timer (&p_cb->fast_adv_timer, BTU_TTYPE_BLE_GAP_FAST_ADV,
   1582                           BTM_BLE_GAP_FAST_ADV_TOUT);
   1583     }
   1584 
   1585     /* set up stop advertising timer */
   1586     if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE)
   1587     {
   1588         BTM_TRACE_EVENT ("start timer for limited disc mode duration=%d (180 secs)", BTM_BLE_GAP_LIM_TOUT);
   1589         /* start Tgap(lim_timeout) */
   1590         btu_start_timer (&p_cb->inq_timer_ent, BTU_TTYPE_BLE_GAP_LIM_DISC,
   1591                          BTM_BLE_GAP_LIM_TOUT);
   1592     }
   1593     return status;
   1594 }
   1595 
   1596 /*******************************************************************************
   1597 **
   1598 ** Function         btm_ble_set_connectability
   1599 **
   1600 ** Description      This function is called to set BLE connectability mode.
   1601 **
   1602 ** Parameters:      combined_mode: connectability mode.
   1603 **
   1604 ** Returns          BTM_SUCCESS is status set successfully; otherwise failure.
   1605 **
   1606 *******************************************************************************/
   1607 tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode)
   1608 {
   1609     tBTM_LE_RANDOM_CB       *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
   1610     tBTM_BLE_INQ_CB         *p_cb = &btm_cb.ble_ctr_cb.inq_var;
   1611     UINT16                  mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
   1612     UINT8                   new_mode = BTM_BLE_ADV_ENABLE;
   1613     UINT8                   evt_type = (p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT: BTM_BLE_NON_CONNECT_EVT;
   1614     tBTM_STATUS             status = BTM_SUCCESS;
   1615     BD_ADDR                 p_addr_ptr =  {0};
   1616     tBLE_ADDR_TYPE          init_addr_type = BLE_ADDR_PUBLIC,
   1617                             own_addr_type = p_addr_cb->own_addr_type;
   1618     UINT16                  adv_int_min, adv_int_max;
   1619 
   1620     BTM_TRACE_EVENT ("btm_ble_set_connectability mode=0x%0x combined_mode=0x%x", mode, combined_mode);
   1621 
   1622     /*** Check mode parameter ***/
   1623     if (mode > BTM_BLE_MAX_CONNECTABLE)
   1624         return(BTM_ILLEGAL_VALUE);
   1625 
   1626     p_cb->connectable_mode = mode;
   1627 
   1628     btm_ble_set_adv_flag (combined_mode,  btm_cb.btm_inq_vars.discoverable_mode);
   1629 
   1630     evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
   1631 
   1632     if (mode == BTM_BLE_NON_CONNECTABLE && p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
   1633         new_mode = BTM_BLE_ADV_DISABLE;
   1634 
   1635     btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
   1636 
   1637     btu_stop_timer(&p_cb->fast_adv_timer);
   1638     /* update adv params if needed */
   1639     if ((p_cb->evt_type != evt_type || p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on))
   1640     {
   1641         btm_ble_stop_adv();
   1642 
   1643         if (!btsnd_hcic_ble_write_adv_params (adv_int_min,
   1644                                               adv_int_max,
   1645                                               evt_type,
   1646                                               own_addr_type,
   1647                                               init_addr_type,
   1648                                               p_addr_ptr,
   1649                                               p_cb->adv_chnl_map,
   1650                                               p_cb->afp))
   1651             status = BTM_NO_RESOURCES;
   1652         else
   1653         {
   1654             p_cb->evt_type = evt_type;
   1655             p_cb->adv_addr_type = own_addr_type;
   1656         }
   1657     }
   1658 
   1659     /* update advertising mode */
   1660     if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode)
   1661     {
   1662         if (new_mode == BTM_BLE_ADV_ENABLE)
   1663             status = btm_ble_start_adv();
   1664         else
   1665             status = btm_ble_stop_adv();
   1666     }
   1667     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
   1668     {
   1669         p_cb->fast_adv_on = TRUE;
   1670         /* start initial GAP mode adv timer */
   1671         btu_start_timer (&p_cb->fast_adv_timer, BTU_TTYPE_BLE_GAP_FAST_ADV,
   1672                              BTM_BLE_GAP_FAST_ADV_TOUT);
   1673     }
   1674     return status;
   1675 }
   1676 
   1677 
   1678 /*******************************************************************************
   1679 **
   1680 ** Function         btm_ble_start_inquiry
   1681 **
   1682 ** Description      This function is called to start BLE inquiry procedure.
   1683 **                  If the duration is zero, the periodic inquiry mode is cancelled.
   1684 **
   1685 ** Parameters:      mode - GENERAL or LIMITED inquiry
   1686 **                  p_inq_params - pointer to the BLE inquiry parameter.
   1687 **                  p_results_cb - callback returning pointer to results (tBTM_INQ_RESULTS)
   1688 **                  p_cmpl_cb - callback indicating the end of an inquiry
   1689 **
   1690 **
   1691 **
   1692 ** Returns          BTM_CMD_STARTED if successfully started
   1693 **                  BTM_NO_RESOURCES if could not allocate a message buffer
   1694 **                  BTM_BUSY - if an inquiry is already active
   1695 **
   1696 *******************************************************************************/
   1697 tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8   duration)
   1698 {
   1699     tBTM_STATUS status = BTM_CMD_STARTED;
   1700     tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
   1701     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
   1702 
   1703     BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x", mode, btm_cb.btm_inq_vars.inq_active);
   1704 
   1705     /* if selective connection is active, or inquiry is already active, reject it */
   1706     if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity) ||
   1707         BTM_BLE_IS_SEL_CONN_ACTIVE (p_ble_cb->scan_activity))
   1708     {
   1709         BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
   1710         return(BTM_BUSY);
   1711     }
   1712 
   1713     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
   1714     {
   1715         btm_update_scanner_filter_policy(SP_ADV_ALL);
   1716 
   1717 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
   1718         /* enable IRK list */
   1719         btm_ble_vendor_irk_list_known_dev (TRUE);
   1720 #endif
   1721         status = btm_ble_start_scan(BTM_BLE_DUPLICATE_DISABLE);
   1722     }
   1723 
   1724     if (status == BTM_CMD_STARTED)
   1725     {
   1726         p_inq->inq_active |= mode;
   1727         p_ble_cb->scan_activity |= mode;
   1728 
   1729         BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x", p_inq->inq_active);
   1730 
   1731         if (duration != 0)
   1732         {
   1733             /* start inquiry timer */
   1734             btu_start_timer (&p_inq->inq_timer_ent, BTU_TTYPE_BLE_INQUIRY, duration);
   1735         }
   1736     }
   1737 
   1738     return status;
   1739 
   1740 }
   1741 
   1742 /*******************************************************************************
   1743 **
   1744 ** Function         btm_ble_read_remote_name_cmpl
   1745 **
   1746 ** Description      This function is called when BLE remote name is received.
   1747 **
   1748 ** Returns          void
   1749 **
   1750 *******************************************************************************/
   1751 void btm_ble_read_remote_name_cmpl(BOOLEAN status, BD_ADDR bda, UINT16 length, char *p_name)
   1752 {
   1753     UINT8   hci_status = HCI_SUCCESS;
   1754     BD_NAME bd_name;
   1755 
   1756     memset(bd_name, 0, (BD_NAME_LEN + 1));
   1757     if (length > BD_NAME_LEN)
   1758     {
   1759         length = BD_NAME_LEN;
   1760     }
   1761     memcpy((UINT8*)bd_name, p_name, length);
   1762 
   1763     if ((!status) || (length==0))
   1764     {
   1765         hci_status = HCI_ERR_HOST_TIMEOUT;
   1766     }
   1767 
   1768     btm_process_remote_name(bda, bd_name, length +1, hci_status);
   1769     btm_sec_rmt_name_request_complete (bda, (UINT8 *)p_name, hci_status);
   1770 }
   1771 
   1772 /*******************************************************************************
   1773 **
   1774 ** Function         btm_ble_read_remote_name
   1775 **
   1776 ** Description      This function read remote LE device name using GATT read
   1777 **                  procedure.
   1778 **
   1779 ** Parameters:       None.
   1780 **
   1781 ** Returns          void
   1782 **
   1783 *******************************************************************************/
   1784 tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb)
   1785 {
   1786     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
   1787 
   1788     if (!HCI_LE_HOST_SUPPORTED(btm_cb.devcb.local_lmp_features[HCI_EXT_FEATURES_PAGE_1]))
   1789         return BTM_ERR_PROCESSING;
   1790 
   1791     if (p_cur &&
   1792         p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_ADV &&
   1793         p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_DIR_ADV)
   1794     {
   1795         BTM_TRACE_DEBUG("name request to non-connectable device failed.");
   1796         return BTM_ERR_PROCESSING;
   1797     }
   1798 
   1799     /* read remote device name using GATT procedure */
   1800     if (p_inq->remname_active)
   1801         return BTM_BUSY;
   1802 
   1803     if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
   1804         return BTM_BUSY;
   1805 
   1806     p_inq->p_remname_cmpl_cb = p_cb;
   1807     p_inq->remname_active = TRUE;
   1808 
   1809     memcpy(p_inq->remname_bda, remote_bda, BD_ADDR_LEN);
   1810 
   1811     btu_start_timer (&p_inq->rmt_name_timer_ent,
   1812                      BTU_TTYPE_BTM_RMT_NAME,
   1813                      BTM_EXT_BLE_RMT_NAME_TIMEOUT);
   1814 
   1815     return BTM_CMD_STARTED;
   1816 }
   1817 
   1818 /*******************************************************************************
   1819 **
   1820 ** Function         btm_ble_cancel_remote_name
   1821 **
   1822 ** Description      This function cancel read remote LE device name.
   1823 **
   1824 ** Parameters:       None.
   1825 **
   1826 ** Returns          void
   1827 **
   1828 *******************************************************************************/
   1829 BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda)
   1830 {
   1831     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
   1832     BOOLEAN     status;
   1833 
   1834     status = GAP_BleCancelReadPeerDevName(remote_bda);
   1835 
   1836     p_inq->remname_active = FALSE;
   1837     memset(p_inq->remname_bda, 0, BD_ADDR_LEN);
   1838     btu_stop_timer(&p_inq->rmt_name_timer_ent);
   1839 
   1840     return status;
   1841 }
   1842 
   1843 /*******************************************************************************
   1844 **
   1845 ** Function         btm_ble_update_adv_flag
   1846 **
   1847 ** Description      This function update the limited discoverable flag in the adv
   1848 **                  data.
   1849 **
   1850 ** Parameters:       None.
   1851 **
   1852 ** Returns          void
   1853 **
   1854 *******************************************************************************/
   1855 static void btm_ble_update_adv_flag(UINT8 flag)
   1856 {
   1857     tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
   1858     UINT8   *p;
   1859 
   1860     BTM_TRACE_DEBUG ("btm_ble_update_adv_flag new=0x%x", flag);
   1861 
   1862     if (p_adv_data->p_flags != NULL)
   1863     {
   1864         BTM_TRACE_DEBUG ("btm_ble_update_adv_flag old=0x%x",   *p_adv_data->p_flags);
   1865         *p_adv_data->p_flags = flag;
   1866     }
   1867     else /* no FLAGS in ADV data*/
   1868     {
   1869         p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
   1870         /* need 3 bytes space to stuff in the flags, if not */
   1871         /* erase all written data, just for flags */
   1872         if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3)
   1873         {
   1874             p = p_adv_data->p_pad = p_adv_data->ad_data;
   1875             memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
   1876         }
   1877 
   1878         *p++ = 2;
   1879         *p++ = BTM_BLE_AD_TYPE_FLAG;
   1880         p_adv_data->p_flags = p;
   1881         *p++ = flag;
   1882         p_adv_data->p_pad = p;
   1883     }
   1884 
   1885     if (btsnd_hcic_ble_set_adv_data((UINT8)(p_adv_data->p_pad - p_adv_data->ad_data),
   1886                                     p_adv_data->ad_data))
   1887         p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
   1888 
   1889 }
   1890 
   1891 #if 0
   1892 /*******************************************************************************
   1893 **
   1894 ** Function         btm_ble_parse_adv_data
   1895 **
   1896 ** Description      This function parse the adv data into a structure.
   1897 **
   1898 ** Returns          pointer to entry, or NULL if not found
   1899 **
   1900 *******************************************************************************/
   1901 static void btm_ble_parse_adv_data(tBTM_INQ_INFO *p_info, UINT8 *p_data,
   1902                                    UINT8 len, tBTM_BLE_INQ_DATA *p_adv_data, UINT8 *p_buf)
   1903 {
   1904     UINT8   *p_cur = p_data;
   1905     UINT8   ad_len, ad_type, ad_flag;
   1906 
   1907     BTM_TRACE_EVENT (" btm_ble_parse_adv_data");
   1908 
   1909     while (len > 0)
   1910     {
   1911         BTM_TRACE_DEBUG("btm_ble_parse_adv_data: len = %d", len);
   1912         if ((ad_len = *p_cur ++) == 0)
   1913             break;
   1914 
   1915         ad_type = *p_cur ++;
   1916 
   1917         BTM_TRACE_DEBUG("     ad_type = %02x ad_len = %d", ad_type, ad_len);
   1918 
   1919         switch (ad_type)
   1920         {
   1921             case BTM_BLE_AD_TYPE_NAME_SHORT:
   1922 
   1923             case BTM_BLE_AD_TYPE_NAME_CMPL:
   1924                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_DEV_NAME;
   1925                 if (p_info)
   1926                 {
   1927                     p_info->remote_name_type =(ad_type == BTM_BLE_AD_TYPE_NAME_SHORT) ?
   1928                                               BTM_BLE_NAME_SHORT: BTM_BLE_NAME_CMPL;
   1929                     memcpy(p_info->remote_name, p_cur, ad_len -1);
   1930                     p_info->remote_name[ad_len] = 0;
   1931                     p_adv_data->p_remote_name = p_info->remote_name;
   1932                     p_info->remote_name_len = p_adv_data->remote_name_len = ad_len - 1;
   1933                     BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_NAME name = %s",p_adv_data->p_remote_name);
   1934                 }
   1935                 p_cur += (ad_len -1);
   1936 
   1937                 break;
   1938 
   1939             case BTM_BLE_AD_TYPE_FLAG:
   1940                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_FLAGS;
   1941                 ad_flag = *p_cur ++;
   1942                 p_adv_data->flag = (UINT8)(ad_flag & BTM_BLE_ADV_FLAG_MASK) ;
   1943                 BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_FLAG flag = %s | %s | %s",
   1944                                  (p_adv_data->flag & BTM_BLE_LIMIT_DISC_FLAG)? "LE_LIMIT_DISC" : "",
   1945                                  (p_adv_data->flag & BTM_BLE_GEN_DISC_FLAG)? "LE_GENERAL_DISC" : "",
   1946                                  (p_adv_data->flag & BTM_BLE_BREDR_NOT_SPT)? "LE Only device" : "");
   1947                 break;
   1948 
   1949             case BTM_BLE_AD_TYPE_TX_PWR:
   1950                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_TX_PWR;
   1951                 p_adv_data->tx_power_level = (INT8)*p_cur ++;
   1952                 BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_TX_PWR tx_level = %d", p_adv_data->tx_power_level);
   1953                 break;
   1954 
   1955             case BTM_BLE_AD_TYPE_MANU:
   1956 
   1957             case BTM_BLE_AD_TYPE_16SRV_PART:
   1958             case BTM_BLE_AD_TYPE_16SRV_CMPL:
   1959                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE;
   1960                 /* need allocate memory to store UUID list */
   1961                 p_adv_data->service.num_service = (ad_len - 1)/2;
   1962                 BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
   1963                 p_cur += (ad_len - 1);
   1964                 break;
   1965 
   1966             case BTM_BLE_AD_TYPE_SOL_SRV_UUID:
   1967                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
   1968                 /* need allocate memory to store UUID list */
   1969                 p_adv_data->service.num_service = (ad_len - 1)/2;
   1970                 BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
   1971                 p_cur += (ad_len - 1);
   1972                 break;
   1973 
   1974             case BTM_BLE_AD_TYPE_128SOL_SRV_UUID:
   1975                 p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
   1976                 /* need allocate memory to store UUID list */
   1977                 p_adv_data->service.num_service = (ad_len - 1)/16;
   1978                 BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
   1979                 p_cur += (ad_len - 1);
   1980                 break;
   1981 
   1982             case BTM_BLE_AD_TYPE_APPEARANCE:
   1983             case BTM_BLE_AD_TYPE_PUBLIC_TARGET:
   1984             case BTM_BLE_AD_TYPE_RANDOM_TARGET:
   1985             default:
   1986                 break;
   1987         }
   1988         len -= (ad_len + 1);
   1989     }
   1990 }
   1991 #endif
   1992 
   1993 /*******************************************************************************
   1994 **
   1995 ** Function         btm_ble_cache_adv_data
   1996 **
   1997 ** Description      Update advertising cache data.
   1998 **
   1999 ** Returns          void
   2000 **
   2001 *******************************************************************************/
   2002 void btm_ble_cache_adv_data(tBTM_INQ_RESULTS *p_cur, UINT8 data_len, UINT8 *p, UINT8 evt_type)
   2003 {
   2004     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
   2005     UINT8 *p_cache;
   2006     UINT8 length;
   2007     UNUSED(p_cur);
   2008 
   2009     /* cache adv report/scan response data */
   2010     if (evt_type != BTM_BLE_SCAN_RSP_EVT)
   2011     {
   2012         p_le_inq_cb->adv_len = 0;
   2013         memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
   2014     }
   2015 
   2016     if (data_len > 0)
   2017     {
   2018         p_cache = &p_le_inq_cb->adv_data_cache[p_le_inq_cb->adv_len];
   2019         STREAM_TO_UINT8(length, p);
   2020         while ( length && ((p_le_inq_cb->adv_len + length + 1) <= BTM_BLE_CACHE_ADV_DATA_MAX))
   2021         {
   2022             /* copy from the length byte & data into cache */
   2023             memcpy(p_cache, p-1, length+1);
   2024             /* advance the cache pointer past data */
   2025             p_cache += length+1;
   2026             /* increment cache length */
   2027             p_le_inq_cb->adv_len += length+1;
   2028             /* skip the length of data */
   2029             p += length;
   2030             STREAM_TO_UINT8(length, p);
   2031         }
   2032     }
   2033 
   2034     /* parse service UUID from adv packet and save it in inq db eir_uuid */
   2035     /* TODO */
   2036 }
   2037 
   2038 /*******************************************************************************
   2039 **
   2040 ** Function         btm_ble_is_discoverable
   2041 **
   2042 ** Description      check ADV flag to make sure device is discoverable and match
   2043 **                  the search condition
   2044 **
   2045 ** Parameters
   2046 **
   2047 ** Returns          void
   2048 **
   2049 *******************************************************************************/
   2050 UINT8 btm_ble_is_discoverable(BD_ADDR bda, UINT8 evt_type, UINT8 *p)
   2051 {
   2052     UINT8               *p_flag, flag = 0, rt = 0;
   2053     UINT8                data_len;
   2054     tBTM_INQ_PARMS      *p_cond = &btm_cb.btm_inq_vars.inqparms;
   2055     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
   2056 
   2057     UNUSED(p);
   2058 
   2059     /* for observer, always "discoverable */
   2060     if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
   2061         rt |= BTM_BLE_OBS_RESULT;
   2062 
   2063     if (BTM_BLE_IS_SEL_CONN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity) &&
   2064         (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_CONNECT_DIR_EVT))
   2065         rt |= BTM_BLE_SEL_CONN_RESULT;
   2066 
   2067     /* does not match filter condition */
   2068     if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
   2069         memcmp(bda, p_cond->filter_cond.bdaddr_cond, BD_ADDR_LEN) != 0)
   2070     {
   2071         BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
   2072         return rt;
   2073     }
   2074 
   2075     if (p_le_inq_cb->adv_len != 0)
   2076     {
   2077         if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
   2078             BTM_BLE_AD_TYPE_FLAG, &data_len)) != NULL)
   2079         {
   2080             flag = * p_flag;
   2081 
   2082             if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
   2083                 (flag & (BTM_BLE_LIMIT_DISC_FLAG|BTM_BLE_GEN_DISC_FLAG)) != 0)
   2084             {
   2085                 BTM_TRACE_DEBUG("Find Generable Discoverable device");
   2086                 rt |= BTM_BLE_INQ_RESULT;
   2087             }
   2088 
   2089             else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
   2090                      (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0)
   2091             {
   2092                 BTM_TRACE_DEBUG("Find limited discoverable device");
   2093                 rt |= BTM_BLE_INQ_RESULT;
   2094             }
   2095         }
   2096     }
   2097     return rt;
   2098 }
   2099 
   2100 static void btm_ble_appearance_to_cod(UINT16 appearance, UINT8 *dev_class)
   2101 {
   2102     dev_class[0] = 0;
   2103 
   2104     switch (appearance)
   2105     {
   2106         case BTM_BLE_APPEARANCE_GENERIC_PHONE:
   2107             dev_class[1] = BTM_COD_MAJOR_PHONE;
   2108             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
   2109             break;
   2110         case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
   2111             dev_class[1] = BTM_COD_MAJOR_COMPUTER;
   2112             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
   2113             break;
   2114         case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
   2115             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2116             dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
   2117             break;
   2118         case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
   2119         case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
   2120             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2121             dev_class[2] = BTM_COD_MINOR_THERMOMETER;
   2122             break;
   2123         case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
   2124         case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
   2125             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2126             dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
   2127             break;
   2128         case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
   2129         case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
   2130         case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
   2131             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2132             dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
   2133             break;
   2134         case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
   2135         case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
   2136         case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
   2137             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2138             dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
   2139             break;
   2140         case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
   2141             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2142             dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
   2143             break;
   2144         case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
   2145             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2146             dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
   2147             break;
   2148         case BTM_BLE_APPEARANCE_GENERIC_WALKING:
   2149         case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
   2150         case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
   2151         case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
   2152             dev_class[1] = BTM_COD_MAJOR_HEALTH;
   2153             dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
   2154             break;
   2155         case BTM_BLE_APPEARANCE_GENERIC_WATCH:
   2156         case BTM_BLE_APPEARANCE_SPORTS_WATCH:
   2157             dev_class[1] = BTM_COD_MAJOR_WEARABLE;
   2158             dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
   2159             break;
   2160         case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
   2161             dev_class[1] = BTM_COD_MAJOR_WEARABLE;
   2162             dev_class[2] = BTM_COD_MINOR_GLASSES;
   2163             break;
   2164         case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
   2165             dev_class[1] = BTM_COD_MAJOR_IMAGING;
   2166             dev_class[2] = BTM_COD_MINOR_DISPLAY;
   2167             break;
   2168         case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
   2169             dev_class[1] = BTM_COD_MAJOR_AUDIO;
   2170             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
   2171             break;
   2172         case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
   2173         case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
   2174         case BTM_BLE_APPEARANCE_GENERIC_HID:
   2175             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2176             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
   2177             break;
   2178         case BTM_BLE_APPEARANCE_HID_KEYBOARD:
   2179             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2180             dev_class[2] = BTM_COD_MINOR_KEYBOARD;
   2181             break;
   2182         case BTM_BLE_APPEARANCE_HID_MOUSE:
   2183             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2184             dev_class[2] = BTM_COD_MINOR_POINTING;
   2185             break;
   2186         case BTM_BLE_APPEARANCE_HID_JOYSTICK:
   2187             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2188             dev_class[2] = BTM_COD_MINOR_JOYSTICK;
   2189             break;
   2190         case BTM_BLE_APPEARANCE_HID_GAMEPAD:
   2191             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2192             dev_class[2] = BTM_COD_MINOR_GAMEPAD;
   2193             break;
   2194         case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
   2195             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2196             dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
   2197             break;
   2198         case BTM_BLE_APPEARANCE_HID_CARD_READER:
   2199             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2200             dev_class[2] = BTM_COD_MINOR_CARD_READER;
   2201             break;
   2202         case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
   2203             dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2204             dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
   2205             break;
   2206         case BTM_BLE_APPEARANCE_UKNOWN:
   2207         case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
   2208         case BTM_BLE_APPEARANCE_GENERIC_TAG:
   2209         case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
   2210         case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
   2211         case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
   2212         case BTM_BLE_APPEARANCE_CYCLING_SPEED:
   2213         case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
   2214         case BTM_BLE_APPEARANCE_CYCLING_POWER:
   2215         case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
   2216         case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
   2217         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
   2218         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
   2219         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
   2220         case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
   2221         default:
   2222             dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
   2223             dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
   2224     };
   2225 }
   2226 
   2227 /*******************************************************************************
   2228 **
   2229 ** Function         btm_ble_update_inq_result
   2230 **
   2231 ** Description      Update adv packet information into inquiry result.
   2232 **
   2233 ** Parameters
   2234 **
   2235 ** Returns          void
   2236 **
   2237 *******************************************************************************/
   2238 BOOLEAN btm_ble_update_inq_result(tINQ_DB_ENT *p_i, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
   2239 {
   2240     BOOLEAN             to_report = TRUE;
   2241     tBTM_INQ_RESULTS     *p_cur = &p_i->inq_info.results;
   2242     UINT8               len;
   2243     UINT8               *p_flag;
   2244     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
   2245     UINT8                data_len, rssi;
   2246     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
   2247     UINT8 *p1;
   2248     UINT8               *p_uuid16;
   2249 
   2250     STREAM_TO_UINT8    (data_len, p);
   2251 
   2252     if (data_len > BTM_BLE_ADV_DATA_LEN_MAX)
   2253     {
   2254         BTM_TRACE_WARNING("EIR data too long %d. discard", data_len);
   2255         return FALSE;
   2256     }
   2257     btm_ble_cache_adv_data(p_cur, data_len, p, evt_type);
   2258 
   2259     p1 = (p + data_len);
   2260     STREAM_TO_UINT8 (rssi, p1);
   2261 
   2262     /* Save the info */
   2263     p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
   2264     p_cur->ble_addr_type    = addr_type;
   2265     p_cur->rssi = rssi;
   2266 
   2267     /* active scan, always wait until get scan_rsp to report the result */
   2268     if ((btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
   2269          (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_DISCOVER_EVT)))
   2270     {
   2271         BTM_TRACE_DEBUG("btm_ble_update_inq_result scan_rsp=false, to_report=false,\
   2272                               scan_type_active=%d", btm_cb.ble_ctr_cb.inq_var.scan_type);
   2273         p_i->scan_rsp = FALSE;
   2274         to_report = FALSE;
   2275     }
   2276     else
   2277         p_i->scan_rsp = TRUE;
   2278 
   2279     if (p_i->inq_count != p_inq->inq_counter)
   2280         p_cur->device_type = BT_DEVICE_TYPE_BLE;
   2281     else
   2282         p_cur->device_type |= BT_DEVICE_TYPE_BLE;
   2283 
   2284     if (evt_type != BTM_BLE_SCAN_RSP_EVT)
   2285         p_cur->ble_evt_type     = evt_type;
   2286 
   2287     p_i->inq_count = p_inq->inq_counter;   /* Mark entry for current inquiry */
   2288 
   2289     if (p_le_inq_cb->adv_len != 0)
   2290     {
   2291         if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_FLAG, &len)) != NULL)
   2292             p_cur->flag = * p_flag;
   2293     }
   2294 
   2295     if (p_le_inq_cb->adv_len != 0)
   2296     {
   2297         /* Check to see the BLE device has the Appearance UUID in the advertising data.  If it does
   2298          * then try to convert the appearance value to a class of device value Bluedroid can use.
   2299          * Otherwise fall back to trying to infer if it is a HID device based on the service class.
   2300          */
   2301         p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_APPEARANCE, &len);
   2302         if (p_uuid16 && len == 2)
   2303         {
   2304             btm_ble_appearance_to_cod((UINT16)p_uuid16[0] | (p_uuid16[1] << 8), p_cur->dev_class);
   2305         }
   2306         else
   2307         {
   2308             if ((p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
   2309                                              BTM_BLE_AD_TYPE_16SRV_CMPL, &len)) != NULL)
   2310             {
   2311                 UINT8 i;
   2312                 for (i = 0; i + 2 <= len; i = i + 2)
   2313                 {
   2314                     /* if this BLE device support HID over LE, set HID Major in class of device */
   2315                     if ((p_uuid16[i] | (p_uuid16[i+1] << 8)) == UUID_SERVCLASS_LE_HID)
   2316                     {
   2317                         p_cur->dev_class[0] = 0;
   2318                         p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
   2319                         p_cur->dev_class[2] = 0;
   2320                         break;
   2321                     }
   2322                 }
   2323             }
   2324         }
   2325     }
   2326 
   2327     /* if BR/EDR not supported is not set, assume is a DUMO device */
   2328     if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
   2329          evt_type != BTM_BLE_CONNECT_DIR_EVT)
   2330     {
   2331         if (p_cur->ble_addr_type != BLE_ADDR_RANDOM)
   2332         {
   2333             BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
   2334             p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
   2335         } else {
   2336             BTM_TRACE_DEBUG("Random address, treating device as LE only");
   2337         }
   2338     }
   2339     else
   2340     {
   2341         BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
   2342     }
   2343 
   2344     return to_report;
   2345 
   2346 }
   2347 
   2348 /*******************************************************************************
   2349 **
   2350 ** Function         btm_send_sel_conn_callback
   2351 **
   2352 ** Description      send selection connection request callback.
   2353 **
   2354 ** Parameters
   2355 **
   2356 ** Returns          void
   2357 **
   2358 *******************************************************************************/
   2359 void btm_send_sel_conn_callback(BD_ADDR remote_bda, UINT8 evt_type, UINT8 *p_data, UINT8 addr_type)
   2360 {
   2361     UINT8   data_len, len;
   2362     UINT8   *p_dev_name, remname[31] = {0};
   2363     UNUSED(addr_type);
   2364 
   2365     if (btm_cb.ble_ctr_cb.p_select_cback == NULL ||
   2366         /* non-connectable device */
   2367         (evt_type != BTM_BLE_EVT_CONN_ADV && evt_type != BTM_BLE_EVT_CONN_DIR_ADV))
   2368         return;
   2369 
   2370     STREAM_TO_UINT8    (data_len, p_data);
   2371 
   2372     /* get the device name if exist in ADV data */
   2373     if (data_len != 0)
   2374     {
   2375         p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_CMPL, &len);
   2376 
   2377         if (p_dev_name == NULL)
   2378             p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_SHORT, &len);
   2379 
   2380         if (p_dev_name)
   2381             memcpy(remname, p_dev_name, len);
   2382     }
   2383     /* allow connection */
   2384     if ((* btm_cb.ble_ctr_cb.p_select_cback)(remote_bda, remname))
   2385     {
   2386         /* terminate selective connection, initiate connection */
   2387         btm_ble_initiate_select_conn(remote_bda);
   2388     }
   2389 }
   2390 
   2391 /*******************************************************************************
   2392 **
   2393 ** Function         btm_ble_process_adv_pkt
   2394 **
   2395 ** Description      This function is called when adv packet report events are
   2396 **                  received from the device. It updates the inquiry database.
   2397 **                  If the inquiry database is full, the oldest entry is discarded.
   2398 **
   2399 ** Parameters
   2400 **
   2401 ** Returns          void
   2402 **
   2403 *******************************************************************************/
   2404 void btm_ble_process_adv_pkt (UINT8 *p_data)
   2405 {
   2406     BD_ADDR             bda;
   2407     UINT8               evt_type = 0, *p = p_data;
   2408     UINT8               addr_type = 0;
   2409     UINT8               num_reports;
   2410     UINT8               data_len;
   2411 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
   2412     BOOLEAN             match = FALSE;
   2413 #endif
   2414 
   2415     /* Extract the number of reports in this event. */
   2416     STREAM_TO_UINT8(num_reports, p);
   2417 
   2418     while (num_reports--)
   2419     {
   2420         /* Extract inquiry results */
   2421         STREAM_TO_UINT8    (evt_type, p);
   2422         STREAM_TO_UINT8    (addr_type, p);
   2423         STREAM_TO_BDADDR   (bda, p);
   2424 
   2425 #ifdef BTM_BLE_PC_ADV_TEST_MODE /* For general stack code (e.g. BTInsight testing), we simply do not define it to exclude or set it to TRUE to include */
   2426         if (BTM_BLE_PC_ADV_TEST_MODE)   /* For stack component, it is always defined and maps to a global variable g_bDraculaAdvertisingMode */
   2427         {
   2428             if (btm_cb.ble_ctr_cb.p_scan_req_cback)
   2429                 (*btm_cb.ble_ctr_cb.p_scan_req_cback)(bda, addr_type, evt_type);
   2430         }
   2431 #endif
   2432 
   2433 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
   2434     /* map address to security record */
   2435     btm_public_addr_to_random_pseudo(bda, &addr_type);
   2436     BTM_TRACE_DEBUG("new address: %02x:%02x:%02x:%02x:%02x:%02x",
   2437                      bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
   2438 #endif
   2439 
   2440         /* Only process the results if the inquiry is still active */
   2441         if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
   2442             return;
   2443 
   2444     BTM_TRACE_DEBUG("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x",
   2445                                      bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]);
   2446 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
   2447 #if SMP_INCLUDED == TRUE
   2448         /* always do RRA resolution on host */
   2449         if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
   2450         {
   2451             btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_adv, p_data);
   2452         }
   2453         else
   2454 #endif
   2455 #endif
   2456         {
   2457             btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
   2458         }
   2459 
   2460         STREAM_TO_UINT8(data_len, p);
   2461 
   2462         /* Advance to the next event data_len + rssi byte */
   2463         p += data_len + 1;
   2464     }
   2465 }
   2466 
   2467 /*******************************************************************************
   2468 **
   2469 ** Function         btm_ble_process_adv_pkt_cont
   2470 **
   2471 ** Description      This function is called after random address resolution is
   2472 **                  done, and proceed to process adv packet.
   2473 **
   2474 ** Parameters
   2475 **
   2476 ** Returns          void
   2477 **
   2478 *******************************************************************************/
   2479 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
   2480 {
   2481     tINQ_DB_ENT          *p_i;
   2482     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
   2483     tBTM_INQ_RESULTS_CB  *p_inq_results_cb = p_inq->p_inq_results_cb;
   2484     tBTM_INQ_RESULTS_CB  *p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
   2485     tBTM_BLE_INQ_CB      *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
   2486     BOOLEAN     update = TRUE;
   2487     UINT8       result = 0;
   2488 
   2489     p_i = btm_inq_db_find (bda);
   2490 
   2491     /* Check if this address has already been processed for this inquiry */
   2492     if (btm_inq_find_bdaddr(bda))
   2493     {
   2494         /* never been report as an LE device */
   2495         if (p_i &&
   2496             (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
   2497               /* scan repsonse to be updated */
   2498               (!p_i->scan_rsp)))
   2499         {
   2500             update = TRUE;
   2501         }
   2502         else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
   2503         {
   2504             update = FALSE;
   2505         }
   2506         else
   2507         {
   2508             /* if yes, skip it */
   2509             return; /* assumption: one result per event */
   2510         }
   2511     }
   2512     /* If existing entry, use that, else get  a new one (possibly reusing the oldest) */
   2513     if (p_i == NULL)
   2514     {
   2515         if ((p_i = btm_inq_db_new (bda)) != NULL)
   2516         {
   2517             p_inq->inq_cmpl_info.num_resp++;
   2518         }
   2519         else
   2520             return;
   2521     }
   2522     else if (p_i->inq_count != p_inq->inq_counter) /* first time seen in this inquiry */
   2523     {
   2524         p_inq->inq_cmpl_info.num_resp++;
   2525     }
   2526     /* update the LE device information in inquiry database */
   2527     if (!btm_ble_update_inq_result(p_i, addr_type, evt_type, p))
   2528         return;
   2529 
   2530     if ((result = btm_ble_is_discoverable(bda, evt_type, p)) == 0)
   2531     {
   2532         BTM_TRACE_ERROR("discard adv pkt");
   2533         return;
   2534     }
   2535     if (!update)
   2536         result &= ~BTM_BLE_INQ_RESULT;
   2537 #if BTM_USE_INQ_RESULTS_FILTER == TRUE
   2538     /* If the number of responses found and limited, issue a cancel inquiry */
   2539     if (p_inq->inqparms.max_resps &&
   2540         p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps)
   2541     {
   2542         /* new device */
   2543         if (p_i == NULL ||
   2544             (/* assume a DUMO device, BR/EDR inquiry is always active */
   2545              p_i && p_i->inq_info.results.device_type == BT_DEVICE_TYPE_BLE && p_i->scan_rsp))
   2546         {
   2547             BTM_TRACE_WARNING("INQ RES: Extra Response Received...cancelling inquiry..");
   2548 
   2549             /* if is non-periodic inquiry active, cancel now */
   2550             if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
   2551                 (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
   2552                 btsnd_hcic_inq_cancel();
   2553 
   2554             btm_ble_stop_inquiry();
   2555 
   2556 #if BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE
   2557             btm_acl_update_busy_level (BTM_BLI_INQ_DONE_EVT);
   2558 #endif
   2559         }
   2560     }
   2561 #endif
   2562     /* background connection in selective connection mode */
   2563     if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE)
   2564     {
   2565         if (result & BTM_BLE_SEL_CONN_RESULT)
   2566             btm_send_sel_conn_callback(bda, evt_type, p, addr_type);
   2567         else
   2568         {
   2569             BTM_TRACE_DEBUG("None LE device, can not initiate selective connection");
   2570         }
   2571     }
   2572     else
   2573     {
   2574         if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT))
   2575         {
   2576             (p_inq_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
   2577         }
   2578         if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT))
   2579         {
   2580             (p_obs_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
   2581         }
   2582     }
   2583 }
   2584 
   2585 /*******************************************************************************
   2586 **
   2587 ** Function         btm_ble_start_scan
   2588 **
   2589 ** Description      Start the BLE scan.
   2590 **
   2591 ** Returns          void
   2592 **
   2593 *******************************************************************************/
   2594 tBTM_STATUS btm_ble_start_scan (UINT8 filter_enable)
   2595 {
   2596     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
   2597     tBTM_STATUS status = BTM_CMD_STARTED;
   2598 
   2599     /* start scan, disable duplicate filtering */
   2600     if (!btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, filter_enable)) {
   2601         status = BTM_NO_RESOURCES;
   2602         btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_SCAN;
   2603     }
   2604     else
   2605     {
   2606         btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_SCAN;
   2607         if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI)
   2608             btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
   2609         else
   2610             btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
   2611     }
   2612     return status;
   2613 }
   2614 
   2615 /*******************************************************************************
   2616 **
   2617 ** Function         btm_ble_stop_scan
   2618 **
   2619 ** Description      Stop the BLE scan.
   2620 **
   2621 ** Returns          void
   2622 **
   2623 *******************************************************************************/
   2624 void btm_ble_stop_scan(void)
   2625 {
   2626     BTM_TRACE_EVENT ("btm_ble_stop_scan ");
   2627 
   2628     /* Clear the inquiry callback if set */
   2629     btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
   2630 
   2631     /* stop discovery now */
   2632     btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
   2633 
   2634     btm_update_scanner_filter_policy(SP_ADV_ALL);
   2635 
   2636     btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_SCAN;
   2637 }
   2638 /*******************************************************************************
   2639 **
   2640 ** Function         btm_ble_stop_inquiry
   2641 **
   2642 ** Description      Stop the BLE Inquiry.
   2643 **
   2644 ** Returns          void
   2645 **
   2646 *******************************************************************************/
   2647 void btm_ble_stop_inquiry(void)
   2648 {
   2649     tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
   2650     tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
   2651 
   2652     btu_stop_timer (&p_ble_cb->inq_var.inq_timer_ent);
   2653 
   2654     p_ble_cb->scan_activity &=  ~BTM_BLE_INQUIRY_MASK;
   2655 
   2656     /* If no more scan activity, stop LE scan now */
   2657     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
   2658         btm_ble_stop_scan();
   2659 
   2660     /* If we have a callback registered for inquiry complete, call it */
   2661     BTM_TRACE_DEBUG ("BTM Inq Compl Callback: status 0x%02x, num results %d",
   2662                       p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
   2663 
   2664     btm_process_inq_complete(HCI_SUCCESS, (UINT8)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
   2665 }
   2666 
   2667 /*******************************************************************************
   2668 **
   2669 ** Function         btm_ble_stop_observe
   2670 **
   2671 ** Description      Stop the BLE Observe.
   2672 **
   2673 ** Returns          void
   2674 **
   2675 *******************************************************************************/
   2676 static void btm_ble_stop_observe(void)
   2677 {
   2678     tBTM_BLE_CB *p_ble_cb = & btm_cb.ble_ctr_cb;
   2679     tBTM_CMPL_CB *p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
   2680 
   2681     btu_stop_timer (&p_ble_cb->obs_timer_ent);
   2682 
   2683     p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
   2684 
   2685     p_ble_cb->p_obs_results_cb = NULL;
   2686     p_ble_cb->p_obs_cmpl_cb = NULL;
   2687 
   2688     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
   2689         btm_ble_stop_scan();
   2690 
   2691     if (p_obs_cb)
   2692         (p_obs_cb)((tBTM_INQUIRY_CMPL *) &btm_cb.btm_inq_vars.inq_cmpl_info);
   2693 }
   2694 /*******************************************************************************
   2695 **
   2696 ** Function         btm_ble_adv_states_operation
   2697 **
   2698 ** Description      Set or clear adv states in topology mask
   2699 **
   2700 ** Returns          operation status. TRUE if sucessful, FALSE otherwise.
   2701 **
   2702 *******************************************************************************/
   2703 typedef BOOLEAN (BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
   2704 static BOOLEAN btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR *p_handler, UINT8 adv_evt)
   2705 {
   2706     BOOLEAN rt = FALSE;
   2707 
   2708     switch (adv_evt)
   2709     {
   2710     case BTM_BLE_CONNECT_EVT:
   2711         rt  = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
   2712         break;
   2713 
   2714     case  BTM_BLE_NON_CONNECT_EVT:
   2715         rt  = (*p_handler) (BTM_BLE_STATE_NON_CONN_ADV_BIT);
   2716         break;
   2717     case BTM_BLE_CONNECT_DIR_EVT:
   2718         rt  =  (*p_handler) (BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
   2719         break;
   2720 
   2721     case BTM_BLE_DISCOVER_EVT:
   2722         rt  =  (*p_handler) (BTM_BLE_STATE_SCAN_ADV_BIT);
   2723         break;
   2724 
   2725     default:
   2726         BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
   2727         break;
   2728     }
   2729 
   2730     return rt;
   2731 }
   2732 
   2733 
   2734 /*******************************************************************************
   2735 **
   2736 ** Function         btm_ble_start_adv
   2737 **
   2738 ** Description      start the BLE advertising.
   2739 **
   2740 ** Returns          void
   2741 **
   2742 *******************************************************************************/
   2743 tBTM_STATUS btm_ble_start_adv(void)
   2744 {
   2745     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
   2746     tBTM_STATUS     rt = BTM_NO_RESOURCES;
   2747 
   2748     if (!btm_ble_adv_states_operation (btm_ble_topology_check, p_cb->evt_type))
   2749         return BTM_WRONG_MODE;
   2750 
   2751     if (p_cb->afp != AP_SCAN_CONN_ALL)
   2752     {
   2753         btm_execute_wl_dev_operation();
   2754         btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
   2755     }
   2756 
   2757     if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE))
   2758     {
   2759         if (p_cb->afp != AP_SCAN_CONN_ALL)
   2760              btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
   2761 
   2762          p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
   2763 
   2764          rt = BTM_SUCCESS;
   2765     }
   2766     else
   2767     {
   2768         p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
   2769         btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
   2770     }
   2771     return rt;
   2772 }
   2773 /*******************************************************************************
   2774 **
   2775 ** Function         btm_ble_stop_adv
   2776 **
   2777 ** Description      Stop the BLE advertising.
   2778 **
   2779 ** Returns          void
   2780 **
   2781 *******************************************************************************/
   2782 tBTM_STATUS btm_ble_stop_adv(void)
   2783 {
   2784     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
   2785     tBTM_STATUS rt = BTM_SUCCESS;
   2786 
   2787     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
   2788     {
   2789         if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE))
   2790         {
   2791             p_cb->fast_adv_on = FALSE;
   2792             p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
   2793             btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
   2794 
   2795             /* clear all adv states */
   2796             btm_ble_clear_topology_mask (BTM_BLE_STATE_ALL_ADV_MASK);
   2797         }
   2798         else
   2799             rt = BTM_NO_RESOURCES;
   2800     }
   2801     return rt;
   2802 }
   2803 
   2804 
   2805 /*******************************************************************************
   2806 **
   2807 ** Function         btm_ble_set_topology_mask
   2808 **
   2809 ** Description      set BLE topology mask
   2810 **
   2811 ** Returns          TRUE is request is allowed, FALSE otherwise.
   2812 **
   2813 *******************************************************************************/
   2814 BOOLEAN btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)
   2815 {
   2816     BOOLEAN rt = TRUE;
   2817 
   2818     request_state_mask &= BTM_BLE_STATE_ALL_MASK;
   2819 
   2820     btm_cb.ble_ctr_cb.cur_states |= request_state_mask;
   2821 
   2822     return rt;
   2823 }
   2824 /*******************************************************************************
   2825 **
   2826 ** Function         btm_ble_clear_topology_mask
   2827 **
   2828 ** Description      Clear BLE topology bit mask
   2829 **
   2830 ** Returns          TRUE is request is allowed, FALSE otherwise.
   2831 **
   2832 *******************************************************************************/
   2833 BOOLEAN btm_ble_clear_topology_mask (tBTM_BLE_STATE_MASK request_state_mask)
   2834 {
   2835     request_state_mask &= BTM_BLE_STATE_ALL_MASK;
   2836 
   2837     btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
   2838 
   2839     return TRUE;
   2840 }
   2841 /*******************************************************************************
   2842 **
   2843 ** Function         btm_ble_update_mode_operation
   2844 **
   2845 ** Description      This function update the GAP role operation when a link status
   2846 **                  is updated.
   2847 **
   2848 ** Returns          void
   2849 **
   2850 *******************************************************************************/
   2851 void btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bd_addr, UINT8 status)
   2852 {
   2853     tACL_CONN   *pa = &btm_cb.acl_db[0];
   2854     UINT16       xx;
   2855     UINT16       mask = BTM_BLE_STATE_ALL_CONN_MASK;
   2856 
   2857     UNUSED(bd_addr);
   2858 
   2859     if (link_role == HCI_ROLE_SLAVE)
   2860     {
   2861         btm_cb.ble_ctr_cb.inq_var.adv_mode  = BTM_BLE_ADV_DISABLE;
   2862         /* clear all adv states */
   2863         mask |= BTM_BLE_STATE_ALL_ADV_MASK;
   2864     }
   2865 
   2866     btm_ble_clear_topology_mask (mask);
   2867 
   2868     /* check the device link role maps */
   2869     for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, pa++)
   2870     {
   2871         if (pa->in_use && pa->transport == BT_TRANSPORT_LE)
   2872         {
   2873             if (pa->link_role == HCI_ROLE_MASTER)
   2874                 btm_ble_set_topology_mask (BTM_BLE_STATE_MASTER_BIT);
   2875             else
   2876                 btm_ble_set_topology_mask (BTM_BLE_STATE_SLAVE_BIT);
   2877         }
   2878     }
   2879 
   2880     if (btm_cb.cmn_ble_vsc_cb.adv_inst_max == 0 &&
   2881         btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE)
   2882     {
   2883         btm_ble_set_connectability ( btm_cb.ble_ctr_cb.inq_var.connectable_mode );
   2884     }
   2885 
   2886     if (btm_ble_get_conn_st() == BLE_CONN_IDLE && status != HCI_ERR_HOST_REJECT_RESOURCES)
   2887     {
   2888         if (!btm_send_pending_direct_conn())
   2889         {
   2890             btm_ble_resume_bg_conn();
   2891         }
   2892     }
   2893 }
   2894 
   2895 /*******************************************************************************
   2896 **
   2897 ** Function         btm_ble_start_slow_adv
   2898 **
   2899 ** Description      Restart adv with slow adv interval
   2900 **
   2901 ** Returns          void
   2902 **
   2903 *******************************************************************************/
   2904 static void btm_ble_start_slow_adv (void)
   2905 {
   2906     tBTM_BLE_INQ_CB         *p_cb = &btm_cb.ble_ctr_cb.inq_var;
   2907     BD_ADDR                 p_addr_ptr= {0};
   2908 
   2909     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE)
   2910     {
   2911         btm_ble_stop_adv();
   2912 
   2913         btsnd_hcic_ble_write_adv_params (BTM_BLE_GAP_ADV_SLOW_INT,
   2914                                          BTM_BLE_GAP_ADV_SLOW_INT,
   2915                                          p_cb->evt_type,
   2916                                          p_cb->adv_addr_type,
   2917                                          btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,/* slow adv
   2918                                          mode never goes into directed adv */
   2919                                          p_addr_ptr,
   2920                                          p_cb->adv_chnl_map,
   2921                                          p_cb->afp);
   2922 
   2923         btm_ble_start_adv();
   2924     }
   2925 }
   2926 /*******************************************************************************
   2927 **
   2928 ** Function         btm_ble_timeout
   2929 **
   2930 ** Description      Called when BTM BLE inquiry timer expires
   2931 **
   2932 ** Returns          void
   2933 **
   2934 *******************************************************************************/
   2935 void btm_ble_timeout(TIMER_LIST_ENT *p_tle)
   2936 {
   2937     BTM_TRACE_EVENT ("btm_ble_timeout");
   2938 
   2939     switch (p_tle->event)
   2940     {
   2941         case BTU_TTYPE_BLE_OBSERVE:
   2942             btm_ble_stop_observe();
   2943             break;
   2944 
   2945         case BTU_TTYPE_BLE_INQUIRY:
   2946             btm_ble_stop_inquiry();
   2947             break;
   2948 
   2949         case BTU_TTYPE_BLE_GAP_LIM_DISC:
   2950             /* lim_timeout expiried, limited discovery should exit now */
   2951             btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
   2952             btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, btm_cb.btm_inq_vars.discoverable_mode);
   2953             break;
   2954 
   2955         case BTU_TTYPE_BLE_RANDOM_ADDR:
   2956             if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM)
   2957             {
   2958                 if (NULL == (void *)(p_tle->param))
   2959                 {
   2960                     /* refresh the random addr */
   2961                     btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
   2962                 }
   2963                 else
   2964                 {
   2965                     if (BTM_BleMaxMultiAdvInstanceCount() > 0)
   2966                        btm_ble_multi_adv_configure_rpa((tBTM_BLE_MULTI_ADV_INST*)p_tle->param);
   2967                 }
   2968             }
   2969             break;
   2970 
   2971         case BTU_TTYPE_BLE_GAP_FAST_ADV:
   2972             /* fast adv is completed, fall back to slow adv interval */
   2973             btm_ble_start_slow_adv();
   2974             break;
   2975 
   2976         default:
   2977             break;
   2978 
   2979     }
   2980 }
   2981 
   2982 
   2983 /*******************************************************************************
   2984 **
   2985 ** Function         btm_ble_read_remote_features_complete
   2986 **
   2987 ** Description      This function is called when the command complete message
   2988 **                  is received from the HCI for the read LE remote feature supported
   2989 **                  complete event.
   2990 **
   2991 ** Returns          void
   2992 **
   2993 *******************************************************************************/
   2994 void btm_ble_read_remote_features_complete(UINT8 *p)
   2995 {
   2996     tACL_CONN        *p_acl_cb = &btm_cb.acl_db[0];
   2997     UINT16            handle;
   2998     UINT8             status;
   2999     int               xx;
   3000 
   3001     BTM_TRACE_EVENT ("btm_ble_read_remote_features_complete ");
   3002 
   3003     STREAM_TO_UINT8(status, p);
   3004     /* if LE read remote feature failed, expect disconnect complete to be received */
   3005     if (status == HCI_SUCCESS)
   3006     {
   3007         STREAM_TO_UINT16 (handle, p);
   3008 
   3009         /* Look up the connection by handle and copy features */
   3010         for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl_cb++)
   3011         {
   3012             if ((p_acl_cb->in_use) && (p_acl_cb->hci_handle == handle))
   3013             {
   3014                 STREAM_TO_ARRAY(p_acl_cb->peer_le_features, p, BD_FEATURES_LEN);
   3015                 /*notify link up here */
   3016                 l2cble_notify_le_connection (p_acl_cb->remote_addr);
   3017                 break;
   3018             }
   3019         }
   3020     }
   3021 
   3022 }
   3023 
   3024 /*******************************************************************************
   3025 **
   3026 ** Function         btm_ble_write_adv_enable_complete
   3027 **
   3028 ** Description      This function process the write adv enable command complete.
   3029 **
   3030 ** Returns          void
   3031 **
   3032 *******************************************************************************/
   3033 void btm_ble_write_adv_enable_complete(UINT8 * p)
   3034 {
   3035     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
   3036 
   3037     /* if write adv enable/disbale not succeed */
   3038     if (*p != HCI_SUCCESS)
   3039     {
   3040         /* toggle back the adv mode */
   3041         p_cb->adv_mode = !p_cb->adv_mode;
   3042     }
   3043 }
   3044 
   3045 /*******************************************************************************
   3046 **
   3047 ** Function         btm_ble_dir_adv_tout
   3048 **
   3049 ** Description      when directed adv time out
   3050 **
   3051 ** Returns          void
   3052 **
   3053 *******************************************************************************/
   3054 void btm_ble_dir_adv_tout(void)
   3055 {
   3056     btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
   3057 
   3058     /* make device fall back into undirected adv mode by default */
   3059     btm_cb.ble_ctr_cb.inq_var.directed_conn = FALSE;
   3060 }
   3061 
   3062 /*******************************************************************************
   3063 **
   3064 ** Function         btm_ble_init
   3065 **
   3066 ** Description      Initialize the control block variable values.
   3067 **
   3068 ** Returns          void
   3069 **
   3070 *******************************************************************************/
   3071 void btm_ble_init (void)
   3072 {
   3073     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
   3074 
   3075     BTM_TRACE_EVENT ("btm_ble_init ");
   3076 
   3077     memset(p_cb, 0, sizeof(tBTM_BLE_CB));
   3078     memset(&(btm_cb.cmn_ble_vsc_cb), 0 , sizeof(tBTM_BLE_VSC_CB));
   3079     btm_cb.cmn_ble_vsc_cb.values_read = FALSE;
   3080     p_cb->cur_states       = 0;
   3081 
   3082     p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
   3083     p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
   3084     p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
   3085     p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
   3086     p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
   3087     p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
   3088     p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
   3089 
   3090     /* for background connection, reset connection params to be undefined */
   3091     p_cb->scan_int = p_cb->scan_win = BTM_BLE_CONN_PARAM_UNDEF;
   3092 
   3093     p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
   3094 
   3095 #if BLE_VND_INCLUDED == FALSE
   3096     btm_ble_vendor_init(BTM_CS_IRK_LIST_MAX);
   3097     btm_ble_adv_filter_init();
   3098 #endif
   3099 }
   3100 
   3101 /*******************************************************************************
   3102 **
   3103 ** Function         btm_ble_topology_check
   3104 **
   3105 ** Description      check to see requested state is supported. One state check at
   3106 **                  a time is supported
   3107 **
   3108 ** Returns          TRUE is request is allowed, FALSE otherwise.
   3109 **
   3110 *******************************************************************************/
   3111 BOOLEAN btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask)
   3112 {
   3113     BOOLEAN rt = FALSE;
   3114     UINT32  llt_mask = 0;
   3115     UINT8   *p;
   3116 
   3117     UINT8   state_offset = 0;
   3118     UINT16  cur_states = btm_cb.ble_ctr_cb.cur_states;
   3119     UINT8   mask, offset;
   3120     UINT8   request_state = 0;
   3121 
   3122     /* check only one bit is set and within valid range */
   3123     if (request_state_mask == BTM_BLE_STATE_INVALID ||
   3124         request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
   3125         (request_state_mask & (request_state_mask -1 )) != 0)
   3126     {
   3127         BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
   3128         return rt;
   3129     }
   3130 
   3131     while (request_state_mask)
   3132     {
   3133         request_state_mask >>= 1;
   3134         request_state ++;
   3135     }
   3136 
   3137     /* check if the requested state is supported or not */
   3138     mask = btm_le_state_combo_tbl[0][request_state - 1][0];
   3139     offset = btm_le_state_combo_tbl[0][request_state-1][1];
   3140 
   3141     if (!BTM_LE_STATES_SUPPORTED(btm_cb.devcb.le_supported_states, mask, offset))
   3142     {
   3143         BTM_TRACE_ERROR("state requested not supported: %d", request_state);
   3144         return rt;
   3145     }
   3146 
   3147     rt = TRUE;
   3148     /* make sure currently active states are all supported in conjunction with the requested
   3149        state. If the bit in table is not set, the combination is not supported */
   3150     while (cur_states != 0)
   3151     {
   3152         if (cur_states & 0x01)
   3153         {
   3154             mask = btm_le_state_combo_tbl[request_state][state_offset][0];
   3155             offset = btm_le_state_combo_tbl[request_state][state_offset][1];
   3156 
   3157             if (mask != 0 && offset != 0)
   3158             {
   3159                 if (!BTM_LE_STATES_SUPPORTED(btm_cb.devcb.le_supported_states, mask, offset))
   3160                 {
   3161                     rt = FALSE;
   3162                     break;
   3163                 }
   3164             }
   3165         }
   3166         cur_states >>= 1;
   3167         state_offset ++;
   3168     }
   3169     return rt;
   3170 }
   3171 
   3172 
   3173 #endif  /* BLE_INCLUDED */
   3174