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 "stack/bt_types.h"
30 //#include "bt_utils.h"
31 #include "btm_int.h"
32 #include "stack/btm_ble_api.h"
33 #include "stack/btu.h"
34 #include "device/controller.h"
35 #include "stack/hcimsgs.h"
36 #include "stack/gap_api.h"
37 #include "hci/hci_layer.h"
38 #if BLE_INCLUDED == TRUE
39 #include "l2c_int.h"
40 
41 #include "stack/gattdefs.h"
42 #include "gatt_int.h"
43 
44 #include "btm_ble_int.h"
45 //#define LOG_TAG "bt_btm_ble"
46 //#include "osi/include/log.h"
47 #include "osi/osi.h"
48 #include "osi/mutex.h"
49 
50 #define BTM_BLE_NAME_SHORT                  0x01
51 #define BTM_BLE_NAME_CMPL                   0x02
52 
53 #define BTM_BLE_FILTER_TARGET_UNKNOWN       0xff
54 #define BTM_BLE_POLICY_UNKNOWN              0xff
55 
56 #define BTM_EXT_BLE_RMT_NAME_TIMEOUT        30
57 #define MIN_ADV_LENGTH                       2
58 #define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE 9
59 
60 #define BTM_BLE_GAP_ADV_RPT_BATCH_SIZE           (10)
61 
62 #if BTM_DYNAMIC_MEMORY == FALSE
63 static tBTM_BLE_VSC_CB cmn_ble_gap_vsc_cb;
64 #else
65 static tBTM_BLE_VSC_CB *cmn_ble_gap_vsc_cb_ptr;
66 #define cmn_ble_gap_vsc_cb (*cmn_ble_gap_vsc_cb_ptr)
67 #endif
68 
69 #if BLE_VND_INCLUDED == TRUE
70 static tBTM_BLE_CTRL_FEATURES_CBACK    *p_ctrl_le_feature_rd_cmpl_cback = NULL;
71 #endif
72 
73 tBTM_CallbackFunc conn_param_update_cb;
74 /*******************************************************************************
75 **  Local functions
76 *******************************************************************************/
77 static void btm_ble_update_adv_flag(UINT8 flag);
78 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p);
79 UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
80                               tBTM_BLE_ADV_DATA *p_data);
81 static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
82         BD_ADDR_PTR p_peer_addr_ptr,
83         tBLE_ADDR_TYPE *p_peer_addr_type,
84         tBLE_ADDR_TYPE *p_own_addr_type);
85 static void btm_ble_stop_observe(void);
86 static void btm_ble_stop_discover(void);
87 static void btm_adv_pkt_handler(void *arg);
88 uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb);
89 
90 #define BTM_BLE_INQ_RESULT          0x01
91 #define BTM_BLE_OBS_RESULT          0x02
92 #define BTM_BLE_SEL_CONN_RESULT     0x04
93 #define BTM_BLE_DISCO_RESULT        0x08
94 
95 static bool is_ble50_inter = false;
96 
btm_ble_inter_set(bool extble_inter)97 void btm_ble_inter_set(bool extble_inter)
98 {
99     is_ble50_inter = extble_inter;
100 }
101 
btm_ble_inter_get(void)102 bool btm_ble_inter_get(void)
103 {
104     return is_ble50_inter;
105 }
106 
107 /* LE states combo bit to check */
108 const UINT8 btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] = {
109     {/* single state support */
110         {HCI_SUPP_LE_STATES_CONN_ADV_MASK, HCI_SUPP_LE_STATES_CONN_ADV_OFF},  /* conn_adv */
111         {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* init */
112         {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* master */
113         {HCI_SUPP_LE_STATES_SLAVE_MASK, HCI_SUPP_LE_STATES_SLAVE_OFF}, /* slave */
114         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_OFF}, /* lo du dir adv */
115         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_OFF}, /* hi duty dir adv */
116         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_OFF},  /* non connectable adv */
117         {HCI_SUPP_LE_STATES_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_OFF},   /*  passive scan */
118         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_OFF},  /*   active scan */
119         {HCI_SUPP_LE_STATES_SCAN_ADV_MASK, HCI_SUPP_LE_STATESSCAN_ADV_OFF}   /* scanable adv */
120     },
121     {    /* conn_adv =0 */
122         {0, 0},                                                                           /* conn_adv */
123         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* init: 32 */
124         {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF},  /* master: 35 */
125         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/
126         {0, 0},                                                                           /* lo du dir adv */
127         {0, 0},                                                                            /* hi duty dir adv */
128         {0, 0},  /* non connectable adv */
129         {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF},   /*  passive scan */
130         {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF},  /*   active scan */
131         {0, 0}   /* scanable adv */
132     },
133     {   /* init */
134         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* conn_adv: 32 */
135         {0, 0},                                                                             /* init */
136         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* master 28 */
137         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* slave 41 */
138         {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 */
139         {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 */
140         {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF},  /*  non connectable adv */
141         {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF},   /* passive scan */
142         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF},  /*  active scan */
143         {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}   /* scanable adv */
144 
145     },
146     {   /* master */
147         {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF},  /* conn_adv: 35 */
148         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* init 28 */
149         {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_OFF},          /* master 28 */
150         {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF},      /* slave: 32 */
151         {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 */
152         {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 */
153         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF},  /*  non connectable adv */
154         {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF},   /*  passive scan */
155         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF},  /*   active scan */
156         {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}   /*  scanable adv */
157 
158     },
159     { /* slave */
160         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* conn_adv: 38,*/
161         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* init 41 */
162         {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* master 41 */
163         {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF},        /* slave: 38,*/
164         {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 */
165         {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 */
166         {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF},  /* non connectable adv */
167         {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF},   /* passive scan */
168         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF},  /*  active scan */
169         {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF}   /* scanable adv */
170 
171     },
172     { /* lo duty cycle adv */
173         {0, 0}, /* conn_adv: 38,*/
174         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF} ,/* init 34 */
175         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* master 37 */
176         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 40 */
177         {0, 0},  /* lo duty cycle adv 40 */
178         {0, 0},   /* hi duty cycle adv 39 */
179         {0, 0},  /*  non connectable adv */
180         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_PASS_SCAN_OFF},   /* passive scan */
181         {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_ACTIVE_SCAN_OFF},  /* active scan */
182         {0, 0}   /*  scanable adv */
183     },
184     { /* hi duty cycle adv */
185         {0, 0}, /* conn_adv: 38,*/
186         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* init 33 */
187         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* master 36 */
188         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF},   /* slave: 39*/
189         {0, 0},  /* lo duty cycle adv 40 */
190         {0, 0},   /* hi duty cycle adv 39 */
191         {0, 0},  /* non connectable adv */
192         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF},   /* passive scan */
193         {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF},  /* active scan */
194         {0, 0}   /* scanable adv */
195     },
196     { /* non connectable adv */
197         {0, 0}, /* conn_adv: */
198         {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* init  */
199         {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* master  */
200         {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF},   /* slave: */
201         {0, 0},  /* lo duty cycle adv */
202         {0, 0},   /* hi duty cycle adv */
203         {0, 0},  /* non connectable adv */
204         {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF},   /* passive scan */
205         {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF},  /*  active scan */
206         {0, 0}   /* scanable adv */
207     },
208     { /* passive scan */
209         {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* conn_adv: */
210         {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* init  */
211         {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* master  */
212         {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF},   /* slave: */
213         {0, 0},  /* lo duty cycle adv */
214         {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 */
215         {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF},  /*  non connectable adv */
216         {0, 0},   /* passive scan */
217         {0, 0},  /* active scan */
218         {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF}   /* scanable adv */
219     },
220     { /* active scan */
221         {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* conn_adv: */
222         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* init  */
223         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* master  */
224         {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF},   /* slave: */
225         {0, 0},  /* lo duty cycle adv */
226         {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 */
227         {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF},  /*  non connectable adv */
228         {0, 0},   /* TODO: passive scan */
229         {0, 0},  /* TODO:  active scan */
230         {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF}   /*  scanable adv */
231     },
232     { /* scanable adv */
233         {0, 0}, /* conn_adv: */
234         {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}, /* init  */
235         {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}, /* master  */
236         {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF},   /* slave: */
237         {0, 0},  /* lo duty cycle adv */
238         {0, 0},   /* hi duty cycle adv */
239         {0, 0},  /* non connectable adv */
240         {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF},   /*  passive scan */
241         {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF},  /*  active scan */
242         {0, 0}   /* scanable adv */
243     }
244 
245 };
246 /* check LE combo state supported */
247 #define BTM_LE_STATES_SUPPORTED(x, y, z)      ((x)[(z)] & (y))
248 
249 static osi_mutex_t adv_enable_lock;
250 static osi_mutex_t adv_data_lock;
251 static osi_mutex_t adv_param_lock;
252 static osi_mutex_t scan_enable_lock;
253 static osi_mutex_t scan_param_lock;
254 osi_sem_t adv_enable_sem;
255 osi_sem_t adv_data_sem;
256 osi_sem_t adv_param_sem;
257 osi_sem_t scan_enable_sem;
258 osi_sem_t scan_param_sem;
259 uint8_t adv_enable_status  = 0;
260 uint8_t adv_data_status    = 0;
261 uint8_t adv_param_status   = 0;
262 uint8_t scan_enable_status = 0;
263 uint8_t scan_param_status  = 0;
264 
btm_ble_lock_init(void)265 void btm_ble_lock_init(void)
266 {
267     osi_mutex_new(&adv_enable_lock);
268     osi_mutex_new(&adv_data_lock);
269     osi_mutex_new(&adv_param_lock);
270     osi_mutex_new(&scan_enable_lock);
271     osi_mutex_new(&scan_param_lock);
272 }
273 
btm_ble_lock_free(void)274 void btm_ble_lock_free(void)
275 {
276     osi_mutex_free(&adv_enable_lock);
277     osi_mutex_free(&adv_data_lock);
278     osi_mutex_free(&adv_param_lock);
279     osi_mutex_free(&scan_enable_lock);
280     osi_mutex_free(&scan_param_lock);
281 }
282 
btm_ble_sem_init(void)283 void btm_ble_sem_init(void)
284 {
285     osi_sem_new(&adv_enable_sem, 1, 0);
286     osi_sem_new(&adv_data_sem, 1, 0);
287     osi_sem_new(&adv_param_sem, 1, 0);
288     osi_sem_new(&scan_enable_sem, 1, 0);
289     osi_sem_new(&scan_param_sem, 1, 0);
290 }
291 
btm_ble_sem_free(void)292 void btm_ble_sem_free(void)
293 {
294     osi_sem_free(&adv_enable_sem);
295     osi_sem_free(&adv_data_sem);
296     osi_sem_free(&adv_param_sem);
297     osi_sem_free(&scan_enable_sem);
298     osi_sem_free(&scan_param_sem);
299 }
300 
301 /*******************************************************************************
302 **
303 ** Function         BTM_BleRegiseterConnParamCallback
304 **
305 ** Description      register connection parameters update callback func
306 **
307 ** Returns          void
308 **
309 *******************************************************************************/
BTM_BleRegiseterConnParamCallback(tBTM_UPDATE_CONN_PARAM_CBACK * update_conn_param_cb)310 void BTM_BleRegiseterConnParamCallback(tBTM_UPDATE_CONN_PARAM_CBACK *update_conn_param_cb)
311 {
312     conn_param_update_cb.update_conn_param_cb = update_conn_param_cb;
313 }
314 
315 /*******************************************************************************
316 **
317 ** Function         BTM_BleUpdateAdvWhitelist
318 **
319 ** Description      Add or remove device from advertising white list
320 **
321 ** Returns          void
322 **
323 *******************************************************************************/
BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove,BD_ADDR remote_bda,tBLE_ADDR_TYPE addr_type,tBTM_UPDATE_WHITELIST_CBACK * update_wl_cb)324 BOOLEAN BTM_BleUpdateAdvWhitelist(BOOLEAN add_remove, BD_ADDR remote_bda, tBLE_ADDR_TYPE addr_type, tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
325 {
326     return btm_update_dev_to_white_list(add_remove, remote_bda, addr_type, update_wl_cb);
327 }
328 
329 /*******************************************************************************
330 **
331 ** Function         BTM_BleUpdateAdvWhitelist
332 **
333 ** Description      Add or remove device from advertising white list
334 **
335 ** Returns          void
336 **
337 *******************************************************************************/
BTM_BleClearWhitelist(tBTM_UPDATE_WHITELIST_CBACK * update_wl_cb)338 void BTM_BleClearWhitelist(tBTM_UPDATE_WHITELIST_CBACK *update_wl_cb)
339 {
340    btm_ble_clear_white_list(update_wl_cb);
341 }
342 
343 /*******************************************************************************
344 **
345 ** Function         BTM_BleUpdateAdvFilterPolicy
346 **
347 ** Description      This function update the filter policy of advertiser.
348 **
349 ** Parameter        adv_policy: advertising filter policy
350 **
351 ** Return           void
352 *******************************************************************************/
BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)353 void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy)
354 {
355     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
356     tBLE_ADDR_TYPE   init_addr_type = BLE_ADDR_PUBLIC;
357     BD_ADDR          p_addr_ptr = {0};
358     UINT8            adv_mode = p_cb->adv_mode;
359 
360     BTM_TRACE_EVENT ("BTM_BleUpdateAdvFilterPolicy\n");
361 
362     if (!controller_get_interface()->supports_ble()) {
363         return;
364     }
365 
366     if (p_cb->afp != adv_policy) {
367         p_cb->afp = adv_policy;
368 
369         /* if adv active, stop and restart */
370         btm_ble_stop_adv ();
371 
372         if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE) {
373             p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type,
374                              &p_cb->adv_addr_type);
375         }
376 
377         btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min :
378                                          BTM_BLE_GAP_ADV_SLOW_INT),
379                                          (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max :
380                                                  BTM_BLE_GAP_ADV_SLOW_INT),
381                                          p_cb->evt_type,
382                                          p_cb->adv_addr_type,
383                                          init_addr_type,
384                                          p_addr_ptr,
385                                          p_cb->adv_chnl_map,
386                                          p_cb->afp);
387 
388         if (adv_mode == BTM_BLE_ADV_ENABLE) {
389             btm_ble_start_adv ();
390         }
391 
392     }
393 }
394 
395 /*******************************************************************************
396 **
397 ** Function         btm_ble_send_extended_scan_params
398 **
399 ** Description      This function sends out the extended scan parameters command to the controller
400 **
401 ** Parameters       scan_type - Scan type
402 **                  scan_int - Scan interval
403 **                  scan_win - Scan window
404 **                  addr_type_own - Own address type
405 **                  scan_filter_policy - Scan filter policy
406 **
407 ** Returns          TRUE or FALSE
408 **
409 *******************************************************************************/
btm_ble_send_extended_scan_params(UINT8 scan_type,UINT32 scan_int,UINT32 scan_win,UINT8 addr_type_own,UINT8 scan_filter_policy)410 BOOLEAN btm_ble_send_extended_scan_params(UINT8 scan_type, UINT32 scan_int,
411         UINT32 scan_win, UINT8 addr_type_own,
412         UINT8 scan_filter_policy)
413 {
414     UINT8 scan_param[HCIC_PARAM_SIZE_BLE_WRITE_EXTENDED_SCAN_PARAM];
415     UINT8 *pp_scan = scan_param;
416 
417     memset(scan_param, 0, HCIC_PARAM_SIZE_BLE_WRITE_EXTENDED_SCAN_PARAM);
418 
419     UINT8_TO_STREAM(pp_scan, scan_type);
420     UINT32_TO_STREAM(pp_scan, scan_int);
421     UINT32_TO_STREAM(pp_scan, scan_win);
422     UINT8_TO_STREAM(pp_scan, addr_type_own);
423     UINT8_TO_STREAM(pp_scan, scan_filter_policy);
424 
425     BTM_TRACE_DEBUG("%s, %d, %d", __func__, scan_int, scan_win);
426     if ((BTM_VendorSpecificCommand(HCI_BLE_EXTENDED_SCAN_PARAMS_OCF,
427                                    HCIC_PARAM_SIZE_BLE_WRITE_EXTENDED_SCAN_PARAM, scan_param, NULL)) != BTM_SUCCESS) {
428         BTM_TRACE_ERROR("%s error sending extended scan parameters", __func__);
429         return FALSE;
430     }
431     return TRUE;
432 }
433 
434 /*******************************************************************************
435 **
436 ** Function         BTM_BleObserve
437 **
438 ** Description      This procedure keep the device listening for advertising
439 **                  events from a broadcast device.
440 **
441 ** Parameters       start: start or stop observe.
442 **                  white_list: use white list in observer mode or not.
443 **
444 ** Returns          void
445 **
446 *******************************************************************************/
BTM_BleObserve(BOOLEAN start,UINT32 duration,tBTM_INQ_RESULTS_CB * p_results_cb,tBTM_CMPL_CB * p_cmpl_cb)447 tBTM_STATUS BTM_BleObserve(BOOLEAN start, UINT32 duration,
448                            tBTM_INQ_RESULTS_CB *p_results_cb, tBTM_CMPL_CB *p_cmpl_cb)
449 {
450     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
451     tBTM_STATUS status = BTM_WRONG_MODE;
452 
453     UINT32 scan_interval = !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
454     UINT32 scan_window = !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
455 
456     BTM_TRACE_EVENT ("%s : scan_type:%d, %d, %d\n", __func__, btm_cb.btm_inq_vars.scan_type,
457                      p_inq->scan_interval, p_inq->scan_window);
458 
459     if (!controller_get_interface()->supports_ble()) {
460         return BTM_ILLEGAL_VALUE;
461     }
462 
463     if (start) {
464         /* shared inquiry database, do not allow observe if any inquiry is active */
465         if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
466             BTM_TRACE_ERROR("%s Observe Already Active", __func__);
467             return status;
468         }
469 
470         btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
471         btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
472         status = BTM_CMD_STARTED;
473 
474         /* scan is not started */
475         if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
476             /* allow config of scan type */
477             p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE) ?
478                                BTM_BLE_SCAN_MODE_ACTI : p_inq->scan_type;
479             /* assume observe always not using white list */
480 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
481             /* enable resolving list */
482             //btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
483 #endif
484 
485             if (cmn_ble_gap_vsc_cb.extended_scan_support == 0) {
486                 btsnd_hcic_ble_set_scan_params(p_inq->scan_type, (UINT16)scan_interval,
487                                                (UINT16)scan_window,
488                                                btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
489                                                BTM_BLE_DEFAULT_SFP);
490             } else {
491                 btm_ble_send_extended_scan_params(p_inq->scan_type, scan_interval, scan_window,
492                                                   btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
493                                                   BTM_BLE_DEFAULT_SFP);
494             }
495 
496             status = btm_ble_start_scan();
497         }
498 
499         if (status == BTM_CMD_STARTED) {
500             btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
501             if (duration != 0)
502                 /* start observer timer */
503             {
504                 btu_start_timer (&btm_cb.ble_ctr_cb.obs_timer_ent, BTU_TTYPE_BLE_OBSERVE, duration);
505             }
506         }
507     } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
508         status = BTM_CMD_STARTED;
509         btm_ble_stop_observe();
510     } else {
511         BTM_TRACE_ERROR("%s Observe not active\n", __func__);
512     }
513 
514     return status;
515 
516 }
517 
518 /*******************************************************************************
519 **
520 ** Function         BTM_BleScan
521 **
522 ** Description      This procedure keep the device listening for advertising
523 **                  events from a broadcast device.
524 **
525 ** Parameters       start: start or stop scan.
526 **                  white_list: use white list in observer mode or not.
527 **
528 ** Returns          void
529 **
530 *******************************************************************************/
BTM_BleScan(BOOLEAN start,UINT32 duration,tBTM_INQ_RESULTS_CB * p_results_cb,tBTM_CMPL_CB * p_cmpl_cb,tBTM_INQ_DIS_CB * p_discard_cb)531 tBTM_STATUS BTM_BleScan(BOOLEAN start, UINT32 duration,
532                            tBTM_INQ_RESULTS_CB *p_results_cb, tBTM_CMPL_CB *p_cmpl_cb, tBTM_INQ_DIS_CB *p_discard_cb)
533 {
534     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
535     tBTM_STATUS status = BTM_WRONG_MODE;
536 
537     if (!controller_get_interface()->supports_ble()) {
538         return BTM_ILLEGAL_VALUE;
539     }
540 
541     if (start) {
542         /* shared inquiry database, do not allow scan if any inquiry is active */
543         if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
544             BTM_TRACE_ERROR("%s scan already active", __func__);
545             return status;
546         }
547 
548         btm_cb.ble_ctr_cb.p_scan_results_cb = p_results_cb;
549         btm_cb.ble_ctr_cb.p_scan_cmpl_cb = p_cmpl_cb;
550         btm_cb.ble_ctr_cb.p_obs_discard_cb = p_discard_cb;
551         status = BTM_CMD_STARTED;
552 
553         /* scan is not started */
554         if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
555             /* assume observe always not using white list */
556 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
557             /* enable resolving list */
558             //btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
559 #endif
560             // if not set scan params, set default scan params
561             if (!p_inq->scan_params_set) {
562                 /* allow config of scan type */
563                 p_inq->scan_type = BTM_BLE_SCAN_MODE_ACTI;
564                 p_inq->scan_interval = BTM_BLE_GAP_DISC_SCAN_INT;
565                 p_inq->scan_window = BTM_BLE_GAP_DISC_SCAN_WIN;
566                 p_inq->sfp = BTM_BLE_DEFAULT_SFP;
567                 p_inq->scan_params_set = TRUE;
568                 p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
569                 btsnd_hcic_ble_set_scan_params(p_inq->scan_type, p_inq->scan_interval,
570                                                p_inq->scan_window,
571                                                btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
572                                                p_inq->sfp);
573             }
574             status = btm_ble_start_scan();
575         }
576 
577         if (status == BTM_CMD_STARTED) {
578             btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_DISCOVER_ACTIVE;
579             if (duration != 0)
580                 /* start observer timer */
581             {
582                 btu_start_timer (&btm_cb.ble_ctr_cb.scan_timer_ent, BTU_TTYPE_BLE_SCAN, duration);
583             }
584         }
585     } else if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
586         status = BTM_CMD_STARTED;
587         btm_ble_stop_discover();
588     } else {
589         BTM_TRACE_ERROR("%s scan not active\n", __func__);
590     }
591 
592     return status;
593 
594 }
595 
596 /*******************************************************************************
597 **
598 ** Function         BTM_BleBroadcast
599 **
600 ** Description      This function is to start or stop broadcasting.
601 **
602 ** Parameters       start: start or stop broadcasting.
603 **
604 ** Returns          status.
605 **
606 *******************************************************************************/
BTM_BleBroadcast(BOOLEAN start,tBTM_START_STOP_ADV_CMPL_CBACK * p_stop_adv_cback)607 tBTM_STATUS BTM_BleBroadcast(BOOLEAN start, tBTM_START_STOP_ADV_CMPL_CBACK  *p_stop_adv_cback)
608 {
609     tBTM_STATUS status = BTM_NO_RESOURCES;
610     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
611     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
612     UINT8 evt_type = p_cb->scan_rsp ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT;
613 
614     if (!controller_get_interface()->supports_ble()) {
615         return BTM_ILLEGAL_VALUE;
616     }
617 
618 #ifdef  BTM_BLE_PC_ADV_TEST_MODE
619     if (BTM_BLE_PC_ADV_TEST_MODE) {
620         evt_type = p_cb->scan_rsp ? BTM_BLE_CONNECT_EVT : BTM_BLE_NON_CONNECT_EVT;
621     }
622 #endif
623 
624     if (start) {
625         /* update adv params */
626         if (!btsnd_hcic_ble_write_adv_params ((UINT16)(p_cb->adv_interval_min ? p_cb->adv_interval_min :
627                                               BTM_BLE_GAP_ADV_INT),
628                                               (UINT16)(p_cb->adv_interval_max ? p_cb->adv_interval_max :
629                                                       BTM_BLE_GAP_ADV_INT),
630                                               evt_type,
631                                               p_addr_cb->own_addr_type,
632                                               p_cb->direct_bda.type,
633                                               p_cb->direct_bda.bda,
634                                               p_cb->adv_chnl_map,
635                                               p_cb->afp))
636 
637         {
638             status = BTM_NO_RESOURCES;
639         } else {
640             p_cb->evt_type = evt_type;
641         }
642 
643         status = btm_ble_start_adv ();
644     } else {
645         //save the stop adv callback to the BTM env.
646         p_cb->p_stop_adv_cb = p_stop_adv_cback;
647         status = btm_ble_stop_adv();
648 #if BLE_PRIVACY_SPT == TRUE
649         btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
650 #endif
651     }
652     return status;
653 }
654 
655 #if BLE_VND_INCLUDED == TRUE
656 /*******************************************************************************
657 **
658 ** Function         btm_vsc_brcm_features_complete
659 **
660 ** Description      Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
661 **
662 ** Returns          void
663 **
664 *******************************************************************************/
btm_ble_vendor_capability_vsc_cmpl_cback(tBTM_VSC_CMPL * p_vcs_cplt_params)665 static void btm_ble_vendor_capability_vsc_cmpl_cback (tBTM_VSC_CMPL *p_vcs_cplt_params)
666 {
667     UINT8 status = 0xFF;
668     UINT8 *p;
669 
670     BTM_TRACE_DEBUG("%s", __func__);
671 
672     /* Check status of command complete event */
673     if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) &&
674             (p_vcs_cplt_params->param_len > 0)) {
675         p = p_vcs_cplt_params->p_param_buf;
676         STREAM_TO_UINT8(status, p);
677     }
678 
679     if (status == HCI_SUCCESS) {
680         STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
681         STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
682         STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
683         STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
684         STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.filter_support, p);
685         STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_filter, p);
686         STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.energy_support, p);
687 
688         if (p_vcs_cplt_params->param_len > BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE) {
689             STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.version_supported, p);
690         } else {
691             btm_cb.cmn_ble_vsc_cb.version_supported = BTM_VSC_CHIP_CAPABILITY_L_VERSION;
692         }
693 
694         if (btm_cb.cmn_ble_vsc_cb.version_supported >= BTM_VSC_CHIP_CAPABILITY_M_VERSION) {
695             STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.total_trackable_advertisers, p);
696             STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.extended_scan_support, p);
697             STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.debug_logging_supported, p);
698         }
699         btm_cb.cmn_ble_vsc_cb.values_read = TRUE;
700     }
701 
702     BTM_TRACE_DEBUG("%s: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d, ext_scan=%d",
703                     __func__, status, btm_cb.cmn_ble_vsc_cb.max_irk_list_sz,
704                     btm_cb.cmn_ble_vsc_cb.adv_inst_max, btm_cb.cmn_ble_vsc_cb.rpa_offloading,
705                     btm_cb.cmn_ble_vsc_cb.energy_support, btm_cb.cmn_ble_vsc_cb.extended_scan_support);
706 
707     if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
708         btm_ble_multi_adv_init();
709     }
710 
711     if (btm_cb.cmn_ble_vsc_cb.max_filter > 0) {
712         btm_ble_adv_filter_init();
713     }
714 
715 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
716     /* VS capability included and non-4.2 device */
717     if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0 &&
718             controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
719         btm_ble_resolving_list_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
720     }
721 #endif
722 
723     if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0) {
724         btm_ble_batchscan_init();
725     }
726 
727     if (p_ctrl_le_feature_rd_cmpl_cback != NULL) {
728         p_ctrl_le_feature_rd_cmpl_cback(status);
729     }
730 }
731 #endif
732 
733 /*******************************************************************************
734 **
735 ** Function         BTM_BleGetVendorCapabilities
736 **
737 ** Description      This function reads local LE features
738 **
739 ** Parameters       p_cmn_vsc_cb : Locala LE capability structure
740 **
741 ** Returns          void
742 **
743 *******************************************************************************/
BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB * p_cmn_vsc_cb)744 extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB *p_cmn_vsc_cb)
745 {
746     BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
747 
748     if (NULL != p_cmn_vsc_cb) {
749         *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
750     }
751 }
752 
753 /******************************************************************************
754 **
755 ** Function         BTM_BleReadControllerFeatures
756 **
757 ** Description      Reads BLE specific controller features
758 **
759 ** Parameters:      tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when features are read
760 **
761 ** Returns          void
762 **
763 *******************************************************************************/
BTM_BleReadControllerFeatures(tBTM_BLE_CTRL_FEATURES_CBACK * p_vsc_cback)764 extern void BTM_BleReadControllerFeatures(tBTM_BLE_CTRL_FEATURES_CBACK  *p_vsc_cback)
765 {
766     if (TRUE == btm_cb.cmn_ble_vsc_cb.values_read) {
767         return;
768     }
769 
770 #if BLE_VND_INCLUDED == TRUE
771     BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
772 
773     p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
774     if ( BTM_VendorSpecificCommand (HCI_BLE_VENDOR_CAP_OCF,
775                                     0,
776                                     NULL,
777                                     btm_ble_vendor_capability_vsc_cmpl_cback)
778             != BTM_CMD_STARTED) {
779         BTM_TRACE_ERROR("LE Get_Vendor Capabilities Command Failed.");
780     }
781 #else
782     UNUSED(p_vsc_cback);
783 #endif
784     return ;
785 }
786 
BTM_VendorHciEchoCmdCallback(tBTM_VSC_CMPL * p1)787 void BTM_VendorHciEchoCmdCallback(tBTM_VSC_CMPL *p1)
788 {
789 #if (!CONFIG_BT_STACK_NO_LOG)
790     if (!p1) {
791         return;
792     }
793     uint8_t *p = p1->p_param_buf;
794     uint8_t status, echo;
795     STREAM_TO_UINT8  (status, p);
796     STREAM_TO_UINT8  (echo, p);
797 #endif
798     BTM_TRACE_DEBUG("%s status 0x%x echo 0x%x", __func__, status, echo);
799 }
800 
801 /******************************************************************************
802 **
803 ** Function         BTM_VendorHciEchoCmdTest
804 **
805 ** Description      vendor common echo hci cmd test, controller will return status and echo
806 **
807 ** Parameters:      echo : echo value
808 **
809 ** Returns          void
810 **
811 *******************************************************************************/
BTM_VendorHciEchoCmdTest(uint8_t echo)812 void BTM_VendorHciEchoCmdTest(uint8_t echo)
813 {
814     BTM_VendorSpecificCommand (HCI_VENDOR_COMMON_ECHO_CMD_OPCODE,
815                                 1,
816                                 &echo,
817                                 BTM_VendorHciEchoCmdCallback);
818 }
819 
820 /*******************************************************************************
821 **
822 ** Function         BTM_BleEnableMixedPrivacyMode
823 **
824 ** Description      This function is called to enabled Mixed mode if privacy 1.2
825 **                  is applicable in controller.
826 **
827 ** Parameters       mixed_on:  mixed mode to be used or not.
828 **
829 ** Returns          void
830 **
831 *******************************************************************************/
BTM_BleEnableMixedPrivacyMode(BOOLEAN mixed_on)832 void BTM_BleEnableMixedPrivacyMode(BOOLEAN mixed_on)
833 {
834 
835 #if BLE_PRIVACY_SPT == TRUE
836     btm_cb.ble_ctr_cb.mixed_mode = mixed_on;
837 
838     /* TODO: send VSC to enabled mixed mode */
839 #endif
840 }
841 
842 /*******************************************************************************
843 **
844 ** Function         BTM_BleConfigPrivacy
845 **
846 ** Description      This function is called to enable or disable the privacy in
847 **                   LE channel of the local device.
848 **
849 ** Parameters       privacy_mode:  privacy mode on or off.
850 **
851 ** Returns          BOOLEAN privacy mode set success; otherwise failed.
852 **
853 *******************************************************************************/
BTM_BleConfigPrivacy(BOOLEAN privacy_mode,tBTM_SET_LOCAL_PRIVACY_CBACK * set_local_privacy_cback)854 BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode, tBTM_SET_LOCAL_PRIVACY_CBACK *set_local_privacy_cback)
855 {
856 #if BLE_PRIVACY_SPT == TRUE
857     tBTM_BLE_CB  *p_cb = &btm_cb.ble_ctr_cb;
858     tBTM_LE_RANDOM_CB *random_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
859     if (random_cb){
860         random_cb->set_local_privacy_cback = set_local_privacy_cback;
861     }else{
862         BTM_TRACE_ERROR("%s,random_cb = NULL", __func__);
863     }
864 
865     BTM_TRACE_EVENT ("%s\n", __func__);
866 
867     /* if LE is not supported, return error */
868     if (!controller_get_interface()->supports_ble()) {
869         return FALSE;
870     }
871 
872     if (p_cb->inq_var.state != BTM_BLE_IDLE) {
873         BTM_TRACE_ERROR("Advertising or scaning now, can't set privacy ");
874         if (random_cb && random_cb->set_local_privacy_cback){
875             (*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_FAIL);
876             random_cb->set_local_privacy_cback = NULL;
877         }
878         return FALSE;
879     }
880 
881 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
882     uint8_t addr_resolution = 0;
883 #endif  /* defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */
884     if (!privacy_mode) { /* if privacy disabled, always use public address */
885         p_cb->addr_mgnt_cb.exist_addr_bit &= (~BTM_BLE_GAP_ADDR_BIT_RESOLVABLE);
886         memset(p_cb->addr_mgnt_cb.resolvale_addr, 0, BD_ADDR_LEN);
887         p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
888         p_cb->privacy_mode = BTM_PRIVACY_NONE;
889         if (random_cb && random_cb->set_local_privacy_cback){
890             (*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_SUCCESS);
891             random_cb->set_local_privacy_cback = NULL;
892         }
893         // Disable RPA function
894         btsnd_hcic_ble_set_addr_resolution_enable(FALSE);
895     } else { /* privacy is turned on*/
896 #if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
897         /* always set host random address, used when privacy 1.1 or priavcy 1.2 is disabled */
898         btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
899 #else
900         /* Controller generates RPA, Host don't need to set random address */
901         if (random_cb && random_cb->set_local_privacy_cback){
902             (*random_cb->set_local_privacy_cback)(BTM_SET_PRIVACY_SUCCESS);
903             random_cb->set_local_privacy_cback = NULL;
904         }
905 #endif
906 
907         if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
908             btm_ble_multi_adv_enb_privacy(privacy_mode);
909         }
910 
911         /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private address in controller */
912         if (controller_get_interface()->supports_ble_privacy()) {
913 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
914             addr_resolution = 1;
915 #endif  /* defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE */
916             /* check vendor specific capability */
917             p_cb->privacy_mode = btm_cb.ble_ctr_cb.mixed_mode ? BTM_PRIVACY_MIXED : BTM_PRIVACY_1_2;
918         } else { /* 4.1/4.0 controller */
919             p_cb->privacy_mode = BTM_PRIVACY_1_1;
920         }
921         // Enable RPA function
922         btsnd_hcic_ble_set_addr_resolution_enable(TRUE);
923     }
924 
925 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
926     GAP_BleAttrDBUpdate (GATT_UUID_GAP_CENTRAL_ADDR_RESOL, (tGAP_BLE_ATTR_VALUE *)&addr_resolution);
927 #endif
928 
929     return TRUE;
930 #else
931     return FALSE;
932 #endif
933 }
934 
935 /*******************************************************************************
936 **
937 ** Function         BTMGetLocalResolvablePrivateAddr
938 **
939 ** Description      This function is called to get local RPA address
940 **
941 ** Parameters       bda: address pointer.
942 **
943 **
944 *******************************************************************************/
945 
BTM_GetLocalResolvablePrivateAddr(BD_ADDR bda)946 BOOLEAN BTM_GetLocalResolvablePrivateAddr(BD_ADDR bda)
947 {
948     tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
949     BTM_TRACE_DEBUG ("get owm resolvable random address");
950 
951     if (bda) {
952         /* if privacy disabled, return false */
953         if ((p_cb->exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) {
954             memcpy(bda, p_cb->resolvale_addr, BD_ADDR_LEN);
955             BTM_TRACE_DEBUG("own resolvable random address: 0x%02x:%02x:%02x:%02x:%02x:%02x",
956                             p_cb->resolvale_addr[0], p_cb->resolvale_addr[1],
957                             p_cb->resolvale_addr[2], p_cb->resolvale_addr[3],
958                             p_cb->resolvale_addr[4], p_cb->resolvale_addr[5]);
959             return TRUE;
960         }
961 
962         return FALSE;
963     }
964 
965    return FALSE;
966 }
967 
968 /*******************************************************************************
969 **
970 ** Function         BTM_UpdateAddrInfor
971 **
972 ** Description      This function is called to update address information
973 **
974 ** Parameters       addr_type: address type
975 **                  bda: address pointer.
976 **
977 **
978 *******************************************************************************/
BTM_UpdateAddrInfor(uint8_t addr_type,BD_ADDR bda)979 void BTM_UpdateAddrInfor(uint8_t addr_type, BD_ADDR bda)
980 {
981     btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = addr_type;
982     memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, bda, BD_ADDR_LEN);
983 }
984 
985 /*******************************************************************************
986 **
987 ** Function         BTM_BleSetStaticAddr
988 **
989 ** Description      This function is called to save random address
990 **
991 ** Parameters       rand_addr: address pointer.
992 **
993 **
994 *******************************************************************************/
BTM_BleSetStaticAddr(BD_ADDR rand_addr)995 void BTM_BleSetStaticAddr(BD_ADDR rand_addr)
996 {
997     memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, rand_addr, BD_ADDR_LEN);
998     btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RANDOM;
999 }
1000 
1001 #if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
BTM_BleUpdateOwnType(uint8_t * own_bda_type,tBTM_START_ADV_CMPL_CBACK * cb)1002 uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb)
1003 {
1004     if(*own_bda_type == BLE_ADDR_RANDOM) {
1005         if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) {
1006             //close privacy
1007             #if BLE_PRIVACY_SPT == TRUE
1008             if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
1009                 BTM_BleConfigPrivacy(FALSE, NULL);
1010             }
1011             #endif
1012             btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
1013             memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN);
1014             // set address to controller
1015             btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr);
1016 
1017         } else if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) {
1018             btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
1019             memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN);
1020             btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr);
1021         }else {
1022             BTM_TRACE_ERROR ("No random address yet, please set random address and try\n");
1023             if(cb) {
1024                 (* cb)(HCI_ERR_ESP_VENDOR_FAIL);
1025             }
1026             return BTM_ILLEGAL_VALUE;
1027         }
1028     } else if(*own_bda_type == BLE_ADDR_PUBLIC_ID || *own_bda_type == BLE_ADDR_RANDOM_ID) {
1029         if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) == BTM_BLE_GAP_ADDR_BIT_RESOLVABLE) {
1030             *own_bda_type = BLE_ADDR_RANDOM;
1031             btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
1032             memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr, BD_ADDR_LEN);
1033             btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.resolvale_addr);
1034         } else {
1035             #if BLE_PRIVACY_SPT == TRUE
1036             if(btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
1037                 BTM_TRACE_ERROR ("Error state\n");
1038                 if(cb) {
1039                     (* cb)(HCI_ERR_ESP_VENDOR_FAIL);
1040                 }
1041                 return BTM_ILLEGAL_VALUE;
1042             }
1043             #endif
1044             if(*own_bda_type == BLE_ADDR_PUBLIC_ID) {
1045                 *own_bda_type = BLE_ADDR_PUBLIC;
1046                 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
1047             } else { //own_bda_type == BLE_ADDR_RANDOM_ID
1048                 if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) == BTM_BLE_GAP_ADDR_BIT_RANDOM) {
1049                     *own_bda_type = BLE_ADDR_RANDOM;
1050                     btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
1051                     memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, BD_ADDR_LEN);
1052                     btsnd_hcic_ble_set_random_addr(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr);
1053                 } else {
1054                     BTM_TRACE_ERROR ("No RPA and no random address yet, please set RPA or random address and try\n");
1055                     if(cb) {
1056                         (* cb)(HCI_ERR_ESP_VENDOR_FAIL);
1057                     }
1058                     return BTM_ILLEGAL_VALUE;
1059                 }
1060             }
1061         }
1062     } else {
1063         btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
1064     }
1065 
1066     return BTM_SUCCESS;
1067 }
1068 #else
BTM_BleUpdateOwnType(uint8_t * own_bda_type,tBTM_START_ADV_CMPL_CBACK * cb)1069 uint32_t BTM_BleUpdateOwnType(uint8_t *own_bda_type, tBTM_START_ADV_CMPL_CBACK *cb)
1070 {
1071     if((*own_bda_type == BLE_ADDR_RANDOM) || (*own_bda_type == BLE_ADDR_RANDOM_ID)) {
1072         if((btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit & BTM_BLE_GAP_ADDR_BIT_RANDOM) != BTM_BLE_GAP_ADDR_BIT_RANDOM) {
1073             BTM_TRACE_ERROR("No random address yet, please set random address and try\n");
1074             if(cb) {
1075                 (* cb)(HCI_ERR_ESP_VENDOR_FAIL);
1076             }
1077             return BTM_ILLEGAL_VALUE;
1078         }
1079     }
1080 
1081     btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = *own_bda_type;
1082 
1083     return BTM_SUCCESS;
1084 }
1085 #endif
1086 
1087 
1088 /*******************************************************************************
1089 **
1090 ** Function         BTM_BleConfigLocalIcon
1091 **
1092 ** Description      This function is called to set local icon
1093 **
1094 ** Parameters       icon:  appearance value.
1095 **
1096 **
1097 *******************************************************************************/
BTM_BleConfigLocalIcon(uint16_t icon)1098 void BTM_BleConfigLocalIcon(uint16_t icon)
1099 {
1100 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
1101     tGAP_BLE_ATTR_VALUE p_value;
1102     p_value.icon = icon;
1103     GAP_BleAttrDBUpdate(GATT_UUID_GAP_ICON, &p_value);
1104 #else
1105     BTM_TRACE_ERROR("%s\n", __func__);
1106 #endif
1107 }
1108 
1109 /*******************************************************************************
1110 **
1111 ** Function         BTM_BleConfigConnParams
1112 **
1113 ** Description      This function is called to set the connection parameters
1114 **
1115 ** Parameters       int_min:  minimum connection interval
1116 **                  int_max:  maximum connection interval
1117 **                  latency:  slave latency
1118 **                  timeout:  supervision timeout
1119 **
1120 *******************************************************************************/
BTM_BleConfigConnParams(uint16_t int_min,uint16_t int_max,uint16_t latency,uint16_t timeout)1121 void BTM_BleConfigConnParams(uint16_t int_min, uint16_t int_max, uint16_t latency, uint16_t timeout)
1122 {
1123 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
1124     tGAP_BLE_ATTR_VALUE p_value;
1125 
1126     p_value.conn_param.int_min = int_min;
1127     p_value.conn_param.int_max = int_max;
1128     p_value.conn_param.latency = latency;
1129     p_value.conn_param.sp_tout = timeout;
1130     GAP_BleAttrDBUpdate(GATT_UUID_GAP_PREF_CONN_PARAM, &p_value);
1131 #else
1132     BTM_TRACE_ERROR("%s\n", __func__);
1133 #endif
1134 }
1135 
1136 /*******************************************************************************
1137 **
1138 ** Function          BTM_BleMaxMultiAdvInstanceCount
1139 **
1140 ** Description        Returns max number of multi adv instances supported by controller
1141 **
1142 ** Returns          Max multi adv instance count
1143 **
1144 *******************************************************************************/
BTM_BleMaxMultiAdvInstanceCount(void)1145 extern UINT8  BTM_BleMaxMultiAdvInstanceCount(void)
1146 {
1147     return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX ?
1148            btm_cb.cmn_ble_vsc_cb.adv_inst_max : BTM_BLE_MULTI_ADV_MAX;
1149 }
1150 
1151 #if BLE_PRIVACY_SPT == TRUE
1152 /*******************************************************************************
1153 **
1154 ** Function         btm_ble_resolve_random_addr_on_adv
1155 **
1156 ** Description      resolve random address complete callback.
1157 **
1158 ** Returns          void
1159 **
1160 *******************************************************************************/
btm_ble_resolve_random_addr_on_adv(void * p_rec,void * p)1161 static void btm_ble_resolve_random_addr_on_adv(void *p_rec, void *p)
1162 {
1163     tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
1164     UINT8       addr_type = BLE_ADDR_RANDOM;
1165     BD_ADDR     bda;
1166     UINT8       *pp = (UINT8 *)p + 1;
1167     UINT8           evt_type;
1168 
1169     BTM_TRACE_EVENT ("btm_ble_resolve_random_addr_on_adv ");
1170 
1171     STREAM_TO_UINT8    (evt_type, pp);
1172     STREAM_TO_UINT8    (addr_type, pp);
1173     STREAM_TO_BDADDR   (bda, pp);
1174 
1175     if (match_rec) {
1176         BTM_TRACE_DEBUG("Random match");
1177         match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
1178         memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
1179 
1180         if (btm_ble_init_pseudo_addr(match_rec, bda)) {
1181             memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
1182         } else {
1183             // Assign the original address to be the current report address
1184             memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
1185         }
1186     }
1187 
1188     btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pp);
1189 
1190     return;
1191 }
1192 #endif
1193 
1194 /*******************************************************************************
1195 **
1196 ** Function         BTM_BleLocalPrivacyEnabled
1197 **
1198 ** Description        Checks if local device supports private address
1199 **
1200 ** Returns          Return TRUE if local privacy is enabled else FALSE
1201 **
1202 *******************************************************************************/
BTM_BleLocalPrivacyEnabled(void)1203 BOOLEAN BTM_BleLocalPrivacyEnabled(void)
1204 {
1205 #if BLE_PRIVACY_SPT == TRUE
1206     return (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE);
1207 #else
1208     return false;
1209 #endif
1210 }
1211 
1212 /*******************************************************************************
1213 **
1214 ** Function         BTM_BleSetBgConnType
1215 **
1216 ** Description      This function is called to set BLE connectable mode for a
1217 **                  peripheral device.
1218 **
1219 ** Parameters       bg_conn_type: it can be auto connection, or selective connection.
1220 **                  p_select_cback: callback function when selective connection procedure
1221 **                              is being used.
1222 **
1223 ** Returns          void
1224 **
1225 *******************************************************************************/
BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE bg_conn_type,tBTM_BLE_SEL_CBACK * p_select_cback)1226 BOOLEAN BTM_BleSetBgConnType(tBTM_BLE_CONN_TYPE   bg_conn_type,
1227                              tBTM_BLE_SEL_CBACK   *p_select_cback)
1228 {
1229     BOOLEAN started = TRUE;
1230 
1231     BTM_TRACE_EVENT ("BTM_BleSetBgConnType ");
1232     if (!controller_get_interface()->supports_ble()) {
1233         return FALSE;
1234     }
1235 
1236     if (btm_cb.ble_ctr_cb.bg_conn_type != bg_conn_type) {
1237         switch (bg_conn_type) {
1238         case BTM_BLE_CONN_AUTO:
1239             btm_ble_start_auto_conn(TRUE);
1240             break;
1241 
1242         case BTM_BLE_CONN_SELECTIVE:
1243             if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO) {
1244                 btm_ble_start_auto_conn(FALSE);
1245             }
1246             btm_ble_start_select_conn(TRUE, p_select_cback);
1247             break;
1248 
1249         case BTM_BLE_CONN_NONE:
1250             if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_AUTO) {
1251                 btm_ble_start_auto_conn(FALSE);
1252             } else if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
1253                 btm_ble_start_select_conn(FALSE, NULL);
1254             }
1255             started = TRUE;
1256             break;
1257 
1258         default:
1259             BTM_TRACE_ERROR("invalid bg connection type : %d ", bg_conn_type);
1260             started = FALSE;
1261             break;
1262         }
1263 
1264         if (started) {
1265             btm_cb.ble_ctr_cb.bg_conn_type = bg_conn_type;
1266         }
1267     }
1268     return started;
1269 }
1270 
1271 /*******************************************************************************
1272 **
1273 ** Function         BTM_BleClearBgConnDev
1274 **
1275 ** Description      This function is called to clear the whitelist,
1276 **                  end any pending whitelist connections,
1277 *                   and reset the local bg device list.
1278 **
1279 ** Parameters       void
1280 **
1281 ** Returns          void
1282 **
1283 *******************************************************************************/
BTM_BleClearBgConnDev(void)1284 void BTM_BleClearBgConnDev(void)
1285 {
1286     btm_ble_start_auto_conn(FALSE);
1287     btm_ble_clear_white_list(NULL);
1288     gatt_reset_bgdev_list();
1289 }
1290 
1291 /*******************************************************************************
1292 **
1293 ** Function         BTM_BleUpdateBgConnDev
1294 **
1295 ** Description      This function is called to add or remove a device into/from
1296 **                  background connection procedure. The background connection
1297 *                   procedure is decided by the background connection type, it can be
1298 *                   auto connection, or selective connection.
1299 **
1300 ** Parameters       add_remove: TRUE to add; FALSE to remove.
1301 **                  remote_bda: device address to add/remove.
1302 **
1303 ** Returns          void
1304 **
1305 *******************************************************************************/
BTM_BleUpdateBgConnDev(BOOLEAN add_remove,BD_ADDR remote_bda)1306 BOOLEAN BTM_BleUpdateBgConnDev(BOOLEAN add_remove, BD_ADDR   remote_bda)
1307 {
1308     BTM_TRACE_EVENT("%s() add=%d", __func__, add_remove);
1309     return btm_update_dev_to_white_list(add_remove, remote_bda, 0, NULL);
1310 }
1311 
1312 /*******************************************************************************
1313 **
1314 ** Function         BTM_BleSetConnectableMode
1315 **
1316 ** Description      This function is called to set BLE connectable mode for a
1317 **                  peripheral device.
1318 **
1319 ** Parameters       conn_mode:  directed connectable mode, or non-directed.It can
1320 **                              be BTM_BLE_CONNECT_EVT, BTM_BLE_CONNECT_DIR_EVT or
1321 **                              BTM_BLE_CONNECT_LO_DUTY_DIR_EVT
1322 **
1323 ** Returns          BTM_ILLEGAL_VALUE if controller does not support BLE.
1324 **                  BTM_SUCCESS is status set successfully; otherwise failure.
1325 **
1326 *******************************************************************************/
BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode)1327 tBTM_STATUS BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode)
1328 {
1329     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1330 
1331     BTM_TRACE_EVENT ("%s connectable_mode = %d ", __func__, connectable_mode);
1332     if (!controller_get_interface()->supports_ble()) {
1333         return BTM_ILLEGAL_VALUE;
1334     }
1335 
1336     p_cb->directed_conn = connectable_mode;
1337     return btm_ble_set_connectability( p_cb->connectable_mode);
1338 }
1339 
1340 /*******************************************************************************
1341 **
1342 ** Function         btm_set_conn_mode_adv_init_addr
1343 **
1344 ** Description      set initator address type and local address type based on adv
1345 **                  mode.
1346 **
1347 **
1348 *******************************************************************************/
btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB * p_cb,BD_ADDR_PTR p_peer_addr_ptr,tBLE_ADDR_TYPE * p_peer_addr_type,tBLE_ADDR_TYPE * p_own_addr_type)1349 static UINT8 btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB *p_cb,
1350         BD_ADDR_PTR p_peer_addr_ptr,
1351         tBLE_ADDR_TYPE *p_peer_addr_type,
1352         tBLE_ADDR_TYPE *p_own_addr_type)
1353 {
1354     UINT8 evt_type;
1355 #if BLE_PRIVACY_SPT == TRUE
1356     UINT8 i = BTM_SEC_MAX_DEVICE_RECORDS;
1357     tBTM_SEC_DEV_REC    *p_dev_rec;
1358     list_node_t         *p_node = NULL;
1359 #endif  ///BLE_PRIVACY_SPT == TRUE
1360     evt_type = (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE) ? \
1361                ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT )\
1362                : BTM_BLE_CONNECT_EVT;
1363 
1364     if (evt_type == BTM_BLE_CONNECT_EVT) {
1365         evt_type = p_cb->directed_conn;
1366 
1367         if ( p_cb->directed_conn == BTM_BLE_CONNECT_DIR_EVT ||
1368                 p_cb->directed_conn == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT) {
1369 
1370 #if BLE_PRIVACY_SPT == TRUE
1371             /* for privacy 1.2, convert peer address as static, own address set as ID addr */
1372             if (btm_cb.ble_ctr_cb.privacy_mode ==  BTM_PRIVACY_1_2 ||
1373                     btm_cb.ble_ctr_cb.privacy_mode ==  BTM_PRIVACY_MIXED) {
1374                 /* only do so for bonded device */
1375                 if ((p_dev_rec = btm_find_or_alloc_dev (p_cb->direct_bda.bda)) != NULL &&
1376                         p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
1377                     //btm_ble_enable_resolving_list(BTM_BLE_RL_ADV);
1378                     memcpy(p_peer_addr_ptr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
1379                     *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
1380                     *p_own_addr_type = BLE_ADDR_RANDOM_ID;
1381                     return evt_type;
1382                 }
1383                 /* otherwise fall though as normal directed adv */
1384                 else {
1385                     btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
1386                 }
1387             }
1388 #endif
1389             /* direct adv mode does not have privacy, if privacy is not enabled  */
1390             *p_peer_addr_type  = p_cb->direct_bda.type;
1391             memcpy(p_peer_addr_ptr, p_cb->direct_bda.bda, BD_ADDR_LEN);
1392             return evt_type;
1393         }
1394     }
1395 
1396     /* undirect adv mode or non-connectable mode*/
1397 #if BLE_PRIVACY_SPT == TRUE
1398     /* when privacy 1.2 privacy only mode is used, or mixed mode */
1399     if ((btm_cb.ble_ctr_cb.privacy_mode ==  BTM_PRIVACY_1_2 && p_cb->afp != AP_SCAN_CONN_ALL) ||
1400             btm_cb.ble_ctr_cb.privacy_mode ==  BTM_PRIVACY_MIXED) {
1401         /* if enhanced privacy is required, set Identity address and matching IRK peer */
1402         for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
1403             p_dev_rec = list_node(p_node);
1404             if ((p_dev_rec->sec_flags & BTM_SEC_IN_USE) != 0 &&
1405                     (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) != 0) {
1406                 memcpy(p_peer_addr_ptr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
1407                 *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
1408                 break;
1409 	    }
1410 	}
1411 
1412         if (i != BTM_SEC_MAX_DEVICE_RECORDS) {
1413             *p_own_addr_type = BLE_ADDR_RANDOM_ID;
1414         } else
1415             /* resolving list is empty, not enabled */
1416         {
1417             *p_own_addr_type = BLE_ADDR_RANDOM;
1418         }
1419     }
1420     /* privacy 1.1, or privacy 1.2, general discoverable/connectable mode, disable privacy in */
1421     /* controller fall back to host based privacy */
1422     else if (btm_cb.ble_ctr_cb.privacy_mode !=  BTM_PRIVACY_NONE) {
1423         *p_own_addr_type = BLE_ADDR_RANDOM;
1424     }
1425 #endif
1426 
1427     /* if no privacy,do not set any peer address,*/
1428     /* local address type go by global privacy setting */
1429     return evt_type;
1430 }
1431 
1432 /*******************************************************************************
1433 **
1434 ** Function         BTM_BleSetAdvParams
1435 **
1436 ** Description      This function is called to set advertising parameters.
1437 **
1438 ** Parameters       adv_int_min: minimum advertising interval
1439 **                  adv_int_max: maximum advertising interval
1440 **                  p_dir_bda: connectable direct initiator's LE device address
1441 **                  chnl_map: advertising channel map.
1442 **
1443 ** Returns          void
1444 **
1445 *******************************************************************************/
BTM_BleSetAdvParams(UINT16 adv_int_min,UINT16 adv_int_max,tBLE_BD_ADDR * p_dir_bda,tBTM_BLE_ADV_CHNL_MAP chnl_map)1446 tBTM_STATUS BTM_BleSetAdvParams(UINT16 adv_int_min, UINT16 adv_int_max,
1447                                 tBLE_BD_ADDR *p_dir_bda,
1448                                 tBTM_BLE_ADV_CHNL_MAP chnl_map)
1449 {
1450     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1451     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1452     tBTM_STATUS status = BTM_SUCCESS;
1453     BD_ADDR     p_addr_ptr =  {0};
1454     tBLE_ADDR_TYPE   init_addr_type = BLE_ADDR_PUBLIC;
1455     tBLE_ADDR_TYPE   own_addr_type = p_addr_cb->own_addr_type;
1456     UINT8            adv_mode = p_cb->adv_mode;
1457 
1458     BTM_TRACE_EVENT ("BTM_BleSetAdvParams");
1459 
1460     if (!controller_get_interface()->supports_ble()) {
1461         return BTM_ILLEGAL_VALUE;
1462     }
1463 
1464     if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
1465             !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX)) {
1466         return BTM_ILLEGAL_VALUE;
1467     }
1468 
1469     p_cb->adv_interval_min = adv_int_min;
1470     p_cb->adv_interval_max = adv_int_max;
1471     p_cb->adv_chnl_map = chnl_map;
1472 
1473     if (p_dir_bda) {
1474         memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
1475     }
1476 
1477     BTM_TRACE_EVENT ("update params for an active adv\n");
1478 
1479     btm_ble_stop_adv();
1480 
1481     p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type,
1482                      &own_addr_type);
1483 
1484     /* update adv params */
1485     btsnd_hcic_ble_write_adv_params (p_cb->adv_interval_min,
1486                                      p_cb->adv_interval_max,
1487                                      p_cb->evt_type,
1488                                      own_addr_type,
1489                                      init_addr_type,
1490                                      p_addr_ptr,
1491                                      p_cb->adv_chnl_map,
1492                                      p_cb->afp);
1493 
1494     if (adv_mode == BTM_BLE_ADV_ENABLE) {
1495         btm_ble_start_adv();
1496     }
1497 
1498     return status;
1499 }
1500 
1501 
1502 /*******************************************************************************
1503 **
1504 ** Function         BTM_BleSetAdvParamsAll
1505 **
1506 ** Description      This function is called to set all of the advertising parameters.
1507 **
1508 ** Parameters:       None.
1509 **
1510 ** Returns          void
1511 **
1512 *******************************************************************************/
BTM_BleSetAdvParamsAll(UINT16 adv_int_min,UINT16 adv_int_max,UINT8 adv_type,tBLE_ADDR_TYPE own_bda_type,tBLE_BD_ADDR * p_dir_bda,tBTM_BLE_ADV_CHNL_MAP chnl_map,tBTM_BLE_AFP afp,tBTM_START_ADV_CMPL_CBACK * adv_cb)1513 tBTM_STATUS BTM_BleSetAdvParamsAll(UINT16 adv_int_min, UINT16 adv_int_max, UINT8 adv_type,
1514                                         tBLE_ADDR_TYPE own_bda_type, tBLE_BD_ADDR *p_dir_bda,
1515                                         tBTM_BLE_ADV_CHNL_MAP chnl_map, tBTM_BLE_AFP afp, tBTM_START_ADV_CMPL_CBACK *adv_cb)
1516 {
1517     tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1518     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1519 
1520     BTM_TRACE_EVENT ("BTM_BleSetAdvParamsAll\n");
1521 
1522     if (!controller_get_interface()->supports_ble()) {
1523         return BTM_ILLEGAL_VALUE;
1524     }
1525     if (BTM_BleUpdateOwnType(&own_bda_type, adv_cb) != 0) {
1526         return BTM_ILLEGAL_VALUE;
1527     }
1528     if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX) ||
1529             !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN, BTM_BLE_ADV_INT_MAX)) {
1530          BTM_TRACE_ERROR ("adv_int_min or adv_int_max is invalid\n");
1531         if(adv_cb) {
1532             (* adv_cb)(HCI_ERR_ESP_VENDOR_FAIL);
1533         }
1534         return BTM_ILLEGAL_VALUE;
1535     }
1536 
1537     btm_ble_stop_adv();
1538 
1539     osi_mutex_lock(&adv_param_lock, OSI_MUTEX_MAX_TIMEOUT);
1540     if(adv_type == BTM_BLE_CONNECT_DIR_EVT){
1541         btm_ble_set_topology_mask(BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
1542     }else if(adv_type == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT){
1543         btm_ble_set_topology_mask(BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT);
1544     }else if(adv_type == BTM_BLE_NON_CONNECT_EVT){
1545         btm_ble_set_topology_mask(BTM_BLE_STATE_NON_CONN_ADV_BIT);
1546     }
1547 
1548     p_cb->adv_interval_min = adv_int_min;
1549     p_cb->adv_interval_max = adv_int_max;
1550     p_cb->adv_chnl_map = chnl_map;
1551     p_addr_cb->own_addr_type = own_bda_type;
1552     p_cb->evt_type = adv_type;
1553     p_cb->afp = afp;
1554     p_cb->p_adv_cb = adv_cb;
1555 
1556     if (p_dir_bda) {
1557         memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
1558     } else {
1559         return BTM_ILLEGAL_VALUE;
1560     }
1561 
1562     BTM_TRACE_EVENT ("update params for an active adv\n");
1563 
1564     tBTM_STATUS status = BTM_SUCCESS;
1565     /* update adv params */
1566     if (btsnd_hcic_ble_write_adv_params (adv_int_min,
1567                                         adv_int_max,
1568                                         adv_type,
1569                                         own_bda_type,
1570                                         p_dir_bda->type,
1571                                         p_dir_bda->bda,
1572                                         chnl_map,
1573                                         p_cb->afp)) {
1574         osi_sem_take(&adv_param_sem, OSI_SEM_MAX_TIMEOUT);
1575         status = adv_param_status;
1576     } else {
1577         status = BTM_NO_RESOURCES;
1578     }
1579     osi_mutex_unlock(&adv_param_lock);
1580     return status;
1581 }
1582 
BTM_BleStartAdv(void)1583 tBTM_STATUS BTM_BleStartAdv(void)
1584 {
1585     tBTM_STATUS status = BTM_SUCCESS;
1586     if (!controller_get_interface()->supports_ble()) {
1587         return BTM_ILLEGAL_VALUE;
1588     }
1589 
1590     btm_ble_stop_adv();
1591 
1592     status = btm_ble_start_adv();
1593 
1594     return status;
1595 }
1596 /*******************************************************************************
1597 **
1598 ** Function         BTM_BleReadAdvParams
1599 **
1600 ** Description      This function is called to set advertising parameters.
1601 **
1602 ** Parameters       adv_int_min: minimum advertising interval
1603 **                  adv_int_max: maximum advertising interval
1604 **                  p_dir_bda: connectable direct initiator's LE device address
1605 **                  chnl_map: advertising channel map.
1606 **
1607 ** Returns          void
1608 **
1609 *******************************************************************************/
BTM_BleReadAdvParams(UINT16 * adv_int_min,UINT16 * adv_int_max,tBLE_BD_ADDR * p_dir_bda,tBTM_BLE_ADV_CHNL_MAP * p_chnl_map)1610 void BTM_BleReadAdvParams (UINT16 *adv_int_min, UINT16 *adv_int_max,
1611                            tBLE_BD_ADDR *p_dir_bda, tBTM_BLE_ADV_CHNL_MAP *p_chnl_map)
1612 {
1613     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1614 
1615     BTM_TRACE_EVENT ("BTM_BleReadAdvParams ");
1616     if (!controller_get_interface()->supports_ble()) {
1617         return ;
1618     }
1619 
1620     *adv_int_min = p_cb->adv_interval_min;
1621     *adv_int_max = p_cb->adv_interval_max;
1622     *p_chnl_map = p_cb->adv_chnl_map;
1623 
1624     if (p_dir_bda != NULL) {
1625         memcpy(p_dir_bda, &p_cb->direct_bda, sizeof(tBLE_BD_ADDR));
1626     }
1627 }
1628 
1629 /*******************************************************************************
1630 **
1631 ** Function         BTM_BleSetScanParams
1632 **
1633 ** Description      This function is called to set scan parameters.
1634 **
1635 ** Parameters       client_if - Client IF
1636 **                  scan_interval - Scan interval
1637 **                  scan_window - Scan window
1638 **                  scan_mode -    Scan mode
1639 **                  scan_setup_status_cback - Scan param setup status callback
1640 **
1641 ** Returns          void
1642 **
1643 *******************************************************************************/
BTM_BleSetScanParams(tGATT_IF client_if,UINT32 scan_interval,UINT32 scan_window,tBLE_SCAN_MODE scan_mode,tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback)1644 void BTM_BleSetScanParams(tGATT_IF client_if, UINT32 scan_interval, UINT32 scan_window,
1645                           tBLE_SCAN_MODE scan_mode,
1646                           tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback)
1647 {
1648     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1649     UINT32 max_scan_interval;
1650     UINT32 max_scan_window;
1651 
1652     BTM_TRACE_EVENT ("%s\n", __func__);
1653     if (!controller_get_interface()->supports_ble()) {
1654         return;
1655     }
1656 
1657     /* If not supporting extended scan support, use the older range for checking */
1658     if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
1659         max_scan_interval = BTM_BLE_SCAN_INT_MAX;
1660         max_scan_window = BTM_BLE_SCAN_WIN_MAX;
1661     } else {
1662         /* If supporting extended scan support, use the new extended range for checking */
1663         max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX;
1664         max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX;
1665     }
1666 
1667     if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN, max_scan_interval) &&
1668             BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN, max_scan_window) &&
1669             (scan_mode == BTM_BLE_SCAN_MODE_ACTI || scan_mode == BTM_BLE_SCAN_MODE_PASS)) {
1670         p_cb->scan_type = scan_mode;
1671         p_cb->scan_interval = scan_interval;
1672         p_cb->scan_window = scan_window;
1673 
1674         if (scan_setup_status_cback != NULL) {
1675             scan_setup_status_cback(client_if, BTM_SUCCESS);
1676         }
1677     } else {
1678         if (scan_setup_status_cback != NULL) {
1679             scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE);
1680         }
1681 
1682         BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d\n",
1683                         scan_interval, scan_window);
1684     }
1685 
1686 }
1687 
BTM_BleSetScanFilterParams(tGATT_IF client_if,UINT32 scan_interval,UINT32 scan_window,tBLE_SCAN_MODE scan_mode,UINT8 addr_type_own,UINT8 scan_duplicate_filter,tBTM_BLE_SFP scan_filter_policy,tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback)1688 tBTM_STATUS BTM_BleSetScanFilterParams(tGATT_IF client_if, UINT32 scan_interval, UINT32 scan_window,
1689                                 tBLE_SCAN_MODE scan_mode, UINT8 addr_type_own, UINT8 scan_duplicate_filter, tBTM_BLE_SFP scan_filter_policy,
1690                                 tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback)
1691 {
1692     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
1693     UINT32 max_scan_interval;
1694     UINT32 max_scan_window;
1695     tBTM_STATUS ret = BTM_SUCCESS;
1696 
1697     BTM_TRACE_EVENT ("%s\n", __func__);
1698     if (!controller_get_interface()->supports_ble()) {
1699         return BTM_ILLEGAL_VALUE;
1700     }
1701     if (BTM_BleUpdateOwnType(&addr_type_own, NULL) != 0) {
1702         return BTM_ILLEGAL_VALUE;
1703     }
1704     /* If not supporting extended scan support, use the older range for checking */
1705     if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
1706         max_scan_interval = BTM_BLE_SCAN_INT_MAX;
1707         max_scan_window = BTM_BLE_SCAN_WIN_MAX;
1708     } else {
1709         /* If supporting extended scan support, use the new extended range for checking */
1710         max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX;
1711         max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX;
1712     }
1713 
1714     osi_mutex_lock(&scan_param_lock, OSI_MUTEX_MAX_TIMEOUT);
1715 
1716     if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN, max_scan_interval) &&
1717             BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN, max_scan_window) &&
1718             (scan_mode == BTM_BLE_SCAN_MODE_ACTI || scan_mode == BTM_BLE_SCAN_MODE_PASS) &&
1719             (scan_duplicate_filter < BTM_BLE_SCAN_DUPLICATE_MAX) && (scan_window <= scan_interval)) {
1720         p_cb->scan_type = scan_mode;
1721         p_cb->scan_interval = scan_interval;
1722         p_cb->scan_window = scan_window;
1723         p_cb->sfp = scan_filter_policy;
1724         p_cb->scan_params_set = TRUE;
1725         p_cb->scan_duplicate_filter = scan_duplicate_filter;
1726 
1727 
1728         if (btsnd_hcic_ble_set_scan_params(p_cb->scan_type, (UINT16)scan_interval,
1729                                        (UINT16)scan_window,
1730                                        addr_type_own,
1731                                        scan_filter_policy)) {
1732             osi_sem_take(&scan_param_sem, OSI_SEM_MAX_TIMEOUT);
1733             ret = scan_param_status;
1734         }
1735     } else {
1736         ret = BTM_ILLEGAL_VALUE;
1737         BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d\n",
1738                         scan_interval, scan_window);
1739     }
1740     osi_mutex_unlock(&scan_param_lock);
1741     return ret;
1742 }
1743 
1744 
1745 /*******************************************************************************
1746 **
1747 ** Function         BTM_BleWriteScanRsp
1748 **
1749 ** Description      This function is called to write LE scan response.
1750 **
1751 ** Parameters:      p_scan_rsp: scan response information.
1752 **
1753 ** Returns          void
1754 **
1755 *******************************************************************************/
BTM_BleWriteScanRsp(tBTM_BLE_AD_MASK data_mask,tBTM_BLE_ADV_DATA * p_data)1756 tBTM_STATUS BTM_BleWriteScanRsp(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
1757 {
1758     tBTM_STATUS     ret;
1759     UINT8   rsp_data[BTM_BLE_AD_DATA_LEN],
1760             *p = rsp_data;
1761 
1762     BTM_TRACE_EVENT (" BTM_BleWriteScanRsp");
1763 
1764     if (!controller_get_interface()->supports_ble()) {
1765         return BTM_ILLEGAL_VALUE;
1766     }
1767 
1768     osi_mutex_lock(&adv_data_lock, OSI_MUTEX_MAX_TIMEOUT);
1769     memset(rsp_data, 0, BTM_BLE_AD_DATA_LEN);
1770     btm_ble_build_adv_data(&data_mask, &p, p_data);
1771     if (data_mask != 0) {
1772         //data length should not exceed 31 bytes
1773         BTM_TRACE_WARNING("%s, Partial data write into ADV", __func__);
1774     }
1775 
1776     if (btsnd_hcic_ble_set_scan_rsp_data((UINT8)(p - rsp_data), rsp_data)) {
1777         osi_sem_take(&adv_data_sem, OSI_SEM_MAX_TIMEOUT);
1778         ret = adv_data_status;
1779 
1780         if (adv_data_status == BTM_SUCCESS && data_mask != 0) {
1781             btm_cb.ble_ctr_cb.inq_var.scan_rsp = TRUE;
1782         } else {
1783             btm_cb.ble_ctr_cb.inq_var.scan_rsp = FALSE;
1784         }
1785     } else {
1786         ret = BTM_ILLEGAL_VALUE;
1787     }
1788 
1789     osi_mutex_unlock(&adv_data_lock);
1790     return ret;
1791 }
1792 
1793 /*******************************************************************************
1794 **
1795 ** Function         BTM_BleWriteScanRspRaw
1796 **
1797 ** Description      This function is called to write raw scan response data
1798 **
1799 ** Parameters:      None.
1800 **
1801 ** Returns          void
1802 **
1803 *******************************************************************************/
BTM_BleWriteScanRspRaw(UINT8 * p_raw_scan_rsp,UINT32 raw_scan_rsp_len)1804 tBTM_STATUS BTM_BleWriteScanRspRaw(UINT8 *p_raw_scan_rsp, UINT32 raw_scan_rsp_len)
1805 {
1806     tBTM_STATUS     ret;
1807 
1808     osi_mutex_lock(&adv_data_lock, OSI_MUTEX_MAX_TIMEOUT);
1809     if (btsnd_hcic_ble_set_scan_rsp_data((UINT8)raw_scan_rsp_len, p_raw_scan_rsp)) {
1810         osi_sem_take(&adv_data_sem, OSI_SEM_MAX_TIMEOUT);
1811         ret = adv_data_status;
1812     } else {
1813         ret = BTM_NO_RESOURCES;
1814     }
1815     osi_mutex_unlock(&adv_data_lock);
1816 
1817     return ret;
1818 }
1819 
1820 /*******************************************************************************
1821 **
1822 ** Function         BTM_UpdateBleDuplicateExceptionalList
1823 **
1824 ** Description      This function is called to update duplicate scan exceptional list.
1825 **
1826 ** Parameters:      subcode: add, remove or clean duplicate scan exceptional list.
1827 **                  type: device info type
1828 **                  device_info: device information
1829 **                  update_exceptional_list_cmp_cb: complete callback
1830 **
1831 ** Returns          status
1832 **
1833 *******************************************************************************/
BTM_UpdateBleDuplicateExceptionalList(uint8_t subcode,uint32_t type,BD_ADDR device_info,tBTM_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_CMPL_CBACK update_exceptional_list_cmp_cb)1834 tBTM_STATUS BTM_UpdateBleDuplicateExceptionalList(uint8_t subcode, uint32_t type, BD_ADDR device_info,
1835                                                 tBTM_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_CMPL_CBACK update_exceptional_list_cmp_cb)
1836 {
1837     tBTM_BLE_CB *ble_cb = &btm_cb.ble_ctr_cb;
1838     tBTM_STATUS status = BTM_NO_RESOURCES;
1839 
1840     ble_cb->update_exceptional_list_cmp_cb = update_exceptional_list_cmp_cb;
1841 
1842     if (!controller_get_interface()->supports_ble()) {
1843         return BTM_ILLEGAL_VALUE;
1844     }
1845     if(!device_info) {
1846         return BTM_ILLEGAL_VALUE;
1847     }
1848     // subcoe + type + device info
1849     uint8_t device_info_array[1 + 4 + BD_ADDR_LEN] = {0};
1850     device_info_array[0] = subcode;
1851     device_info_array[1] = type & 0xff;
1852     device_info_array[2] = (type >> 8) & 0xff;
1853     device_info_array[3] = (type >> 16) & 0xff;
1854     device_info_array[4] = (type >> 24) & 0xff;
1855     switch (type)
1856     {
1857         case BTM_DUPLICATE_SCAN_EXCEPTIONAL_INFO_ADV_ADDR:
1858             bt_rcopy(&device_info_array[5], device_info, BD_ADDR_LEN);
1859             break;
1860         case BTM_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_LINK_ID:
1861             memcpy(&device_info_array[5], device_info, 4);
1862             break;
1863         case BTM_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_BEACON_TYPE:
1864             //do nothing
1865             break;
1866         case BTM_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_PROV_SRV_ADV:
1867             //do nothing
1868             break;
1869         case BTM_DUPLICATE_SCAN_EXCEPTIONAL_INFO_MESH_PROXY_SRV_ADV:
1870             //do nothing
1871             break;
1872         default:
1873             //do nothing
1874             break;
1875     }
1876 
1877     status = BTM_VendorSpecificCommand(HCI_VENDOR_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST, 1 + 4 + BD_ADDR_LEN, device_info_array, NULL);
1878     if(status == BTM_CMD_STARTED) {
1879         status = BTM_SUCCESS;
1880     }
1881 
1882     return status;
1883 }
1884 
1885 /*******************************************************************************
1886 **
1887 ** Function         BTM_BleWriteAdvData
1888 **
1889 ** Description      This function is called to write advertising data.
1890 **
1891 ** Parameters:       None.
1892 **
1893 ** Returns          void
1894 **
1895 *******************************************************************************/
BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask,tBTM_BLE_ADV_DATA * p_data)1896 tBTM_STATUS BTM_BleWriteAdvData(tBTM_BLE_AD_MASK data_mask, tBTM_BLE_ADV_DATA *p_data)
1897 {
1898     tBTM_BLE_LOCAL_ADV_DATA *p_cb_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1899     UINT8  *p;
1900     tBTM_BLE_AD_MASK   mask = data_mask;
1901     tBTM_STATUS ret;
1902 
1903     BTM_TRACE_EVENT ("BTM_BleWriteAdvData ");
1904 
1905     if (!controller_get_interface()->supports_ble()) {
1906         return BTM_ILLEGAL_VALUE;
1907     }
1908     osi_mutex_lock(&adv_data_lock, OSI_MUTEX_MAX_TIMEOUT);
1909     memset(p_cb_data, 0, sizeof(tBTM_BLE_LOCAL_ADV_DATA));
1910     p = p_cb_data->ad_data;
1911     p_cb_data->data_mask = data_mask;
1912 
1913     p_cb_data->p_flags = btm_ble_build_adv_data(&mask, &p, p_data);
1914 
1915     p_cb_data->p_pad = p;
1916 
1917     if (mask != 0) {
1918         //data length should not exceed 31 bytes
1919         BTM_TRACE_WARNING("%s, Partial data write into ADV", __func__);
1920     }
1921 
1922     p_cb_data->data_mask &= ~mask;
1923 
1924     if (btsnd_hcic_ble_set_adv_data((UINT8)(p_cb_data->p_pad - p_cb_data->ad_data),
1925                                     p_cb_data->ad_data)) {
1926         osi_sem_take(&adv_data_sem, OSI_SEM_MAX_TIMEOUT);
1927         ret = adv_data_status;
1928     } else {
1929         ret = BTM_NO_RESOURCES;
1930     }
1931     osi_mutex_unlock(&adv_data_lock);
1932     return ret;
1933 }
1934 
1935 /*******************************************************************************
1936 **
1937 ** Function         BTM_BleWriteLongAdvData
1938 **
1939 ** Description      This function is called to write long advertising data.
1940 **
1941 ** Parameters:      adv_data: long advertising data
1942 **                  adv_data_len: the length of long advertising data
1943 **
1944 ** Returns          void
1945 **
1946 *******************************************************************************/
BTM_BleWriteLongAdvData(uint8_t * adv_data,uint8_t adv_data_len)1947 tBTM_STATUS BTM_BleWriteLongAdvData(uint8_t *adv_data, uint8_t adv_data_len)
1948 {
1949     tBTM_STATUS status = BTM_NO_RESOURCES;
1950     if (!controller_get_interface()->supports_ble()) {
1951         return BTM_ILLEGAL_VALUE;
1952     }
1953     if(!adv_data || adv_data_len <= 0 || adv_data_len > BTM_BLE_LONG_ADV_MAX_LEN) {
1954         return BTM_ILLEGAL_VALUE;
1955     }
1956     uint8_t long_adv[BTM_BLE_LONG_ADV_MAX_LEN + 1] = {0};
1957     long_adv[0] = adv_data_len;
1958     memcpy(&long_adv[1], adv_data, adv_data_len);
1959     status = BTM_VendorSpecificCommand(HCI_VENDOR_BLE_LONG_ADV_DATA, BTM_BLE_LONG_ADV_MAX_LEN + 1, long_adv, NULL);
1960     if(status == BTM_CMD_STARTED) {
1961         status = BTM_SUCCESS;
1962     }
1963 
1964     return status;
1965 }
1966 
1967 /*******************************************************************************
1968 **
1969 ** Function         BTM_BleWriteAdvDataRaw
1970 **
1971 ** Description      This function is called to write raw advertising data.
1972 **
1973 ** Parameters:       None.
1974 **
1975 ** Returns          void
1976 **
1977 *******************************************************************************/
BTM_BleWriteAdvDataRaw(UINT8 * p_raw_adv,UINT32 raw_adv_len)1978 tBTM_STATUS BTM_BleWriteAdvDataRaw(UINT8 *p_raw_adv, UINT32 raw_adv_len)
1979 {
1980     tBTM_STATUS ret;
1981     osi_mutex_lock(&adv_data_lock, OSI_MUTEX_MAX_TIMEOUT);
1982     if (btsnd_hcic_ble_set_adv_data((UINT8)raw_adv_len, p_raw_adv)) {
1983         osi_sem_take(&adv_data_sem, OSI_SEM_MAX_TIMEOUT);
1984         ret = adv_data_status;
1985     } else {
1986         ret = BTM_NO_RESOURCES;
1987     }
1988     osi_mutex_unlock(&adv_data_lock);
1989 
1990     return ret;
1991 }
1992 
1993 
1994 /*******************************************************************************
1995 **
1996 ** Function         BTM_BleSetRandAddress
1997 **
1998 ** Description      This function is called to set the LE random address.
1999 **
2000 ** Parameters:       None.
2001 **
2002 ** Returns          void
2003 **
2004 *******************************************************************************/
BTM_BleSetRandAddress(BD_ADDR rand_addr)2005 tBTM_STATUS BTM_BleSetRandAddress(BD_ADDR rand_addr)
2006 {
2007 	if (rand_addr == NULL) {
2008 		return BTM_SET_STATIC_RAND_ADDR_FAIL;
2009     }
2010 
2011     if (btm_cb.ble_ctr_cb.inq_var.state != BTM_BLE_IDLE) {
2012         BTM_TRACE_ERROR("Advertising or scaning now, can't set randaddress %d", btm_cb.ble_ctr_cb.inq_var.state);
2013         return BTM_SET_STATIC_RAND_ADDR_FAIL;
2014     }
2015     memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, rand_addr, BD_ADDR_LEN);
2016     memcpy(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, rand_addr, BD_ADDR_LEN);
2017     //send the set random address to the controller
2018     if(btsnd_hcic_ble_set_random_addr(rand_addr)) {
2019         btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit |= BTM_BLE_GAP_ADDR_BIT_RANDOM;
2020         return BTM_SUCCESS;
2021     } else {
2022         return BTM_SET_STATIC_RAND_ADDR_FAIL;
2023     }
2024 }
2025 
2026 /*******************************************************************************
2027 **
2028 ** Function         BTM_BleClearRandAddress
2029 **
2030 ** Description      This function is called to clear the LE random address.
2031 **
2032 ** Parameters:       None.
2033 **
2034 ** Returns          void
2035 **
2036 *******************************************************************************/
BTM_BleClearRandAddress(void)2037 void BTM_BleClearRandAddress(void)
2038 {
2039     tBTM_BLE_CB  *p_cb = &btm_cb.ble_ctr_cb;
2040     if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM && (p_cb->inq_var.state != BTM_BLE_IDLE)) {
2041         BTM_TRACE_ERROR("Advertising or scaning now, can't restore public address ");
2042         return;
2043     }
2044     memset(btm_cb.ble_ctr_cb.addr_mgnt_cb.static_rand_addr, 0, BD_ADDR_LEN);
2045     btm_cb.ble_ctr_cb.addr_mgnt_cb.exist_addr_bit &= (~BTM_BLE_GAP_ADDR_BIT_RANDOM);
2046     btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
2047 }
2048 /*******************************************************************************
2049 **
2050 ** Function         BTM_BleGetCurrentAddress
2051 **
2052 ** Description      This function is called to get local used BLE address.
2053 **
2054 ** Parameters:       None.
2055 **
2056 ** Returns          success or fail
2057 **
2058 *******************************************************************************/
BTM_BleGetCurrentAddress(BD_ADDR addr,uint8_t * addr_type)2059 BOOLEAN BTM_BleGetCurrentAddress(BD_ADDR addr, uint8_t *addr_type)
2060 {
2061     if(addr == NULL || addr_type == NULL) {
2062         BTM_TRACE_ERROR("%s addr or addr_type is NULL\n", __func__);
2063         return FALSE;
2064     }
2065     if(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
2066         *addr_type = BLE_ADDR_RANDOM;
2067         memcpy(addr, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr, BD_ADDR_LEN);
2068     } else if(btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_PUBLIC) {
2069         *addr_type = BLE_ADDR_PUBLIC;
2070         memcpy(addr, &controller_get_interface()->get_address()->address, BD_ADDR_LEN);
2071     } else {
2072         BTM_TRACE_ERROR("%s\n", __func__);
2073         memset(addr, 0, BD_ADDR_LEN);
2074         return FALSE;
2075     }
2076     return TRUE;
2077 }
2078 
2079 /*******************************************************************************
2080 **
2081 ** Function         BTM_CheckAdvData
2082 **
2083 ** Description      This function is called to get ADV data for a specific type.
2084 **
2085 ** Parameters       p_adv - pointer of ADV data
2086 **                  type   - finding ADV data type
2087 **                  p_length - return the length of ADV data not including type
2088 **
2089 ** Returns          pointer of ADV data
2090 **
2091 *******************************************************************************/
BTM_CheckAdvData(UINT8 * p_adv,UINT8 type,UINT8 * p_length)2092 UINT8 *BTM_CheckAdvData( UINT8 *p_adv, UINT8 type, UINT8 *p_length)
2093 {
2094     UINT8 *p = p_adv;
2095     UINT8 length;
2096     UINT8 adv_type;
2097     BTM_TRACE_API("BTM_CheckAdvData type=0x%02X", type);
2098 
2099     STREAM_TO_UINT8(length, p);
2100 
2101     while ( length && (p - p_adv <= BTM_BLE_CACHE_ADV_DATA_MAX)) {
2102         STREAM_TO_UINT8(adv_type, p);
2103 
2104         if ( adv_type == type ) {
2105             /* length doesn't include itself */
2106             *p_length = length - 1; /* minus the length of type */
2107             return p;
2108         }
2109         p += length - 1; /* skip the length of data */
2110         STREAM_TO_UINT8(length, p);
2111     }
2112 
2113     *p_length = 0;
2114     return NULL;
2115 }
2116 
2117 /*******************************************************************************
2118 **
2119 ** Function         BTM__BLEReadDiscoverability
2120 **
2121 ** Description      This function is called to read the current LE discoverability
2122 **                  mode of the device.
2123 **
2124 ** Returns          BTM_BLE_NON_DISCOVERABLE ,BTM_BLE_LIMITED_DISCOVERABLE or
2125 **                     BTM_BLE_GENRAL_DISCOVERABLE
2126 **
2127 *******************************************************************************/
BTM_BleReadDiscoverability(void)2128 UINT16 BTM_BleReadDiscoverability(void)
2129 {
2130     BTM_TRACE_API("%s\n", __FUNCTION__);
2131 
2132     return (btm_cb.ble_ctr_cb.inq_var.discoverable_mode);
2133 }
2134 
2135 /*******************************************************************************
2136 **
2137 ** Function         BTM__BLEReadConnectability
2138 **
2139 ** Description      This function is called to read the current LE connectibility
2140 **                  mode of the device.
2141 **
2142 ** Returns          BTM_BLE_NON_CONNECTABLE or BTM_BLE_CONNECTABLE
2143 **
2144 *******************************************************************************/
BTM_BleReadConnectability(void)2145 UINT16 BTM_BleReadConnectability(void)
2146 {
2147     BTM_TRACE_API ("%s\n", __FUNCTION__);
2148 
2149     return (btm_cb.ble_ctr_cb.inq_var.connectable_mode);
2150 }
2151 
BTM_Recovery_Pre_State(void)2152 void BTM_Recovery_Pre_State(void)
2153 {
2154     tBTM_BLE_INQ_CB *ble_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
2155 
2156     if (ble_inq_cb->state & BTM_BLE_ADVERTISING) {
2157         btm_ble_stop_adv();
2158         btm_ble_start_adv();
2159     }
2160     if (ble_inq_cb->state & BTM_BLE_SCANNING) {
2161         btm_ble_start_scan();
2162     }
2163 
2164     return;
2165 }
2166 
2167 /*******************************************************************************
2168 **
2169 ** Function         BTM_GetCurrentConnParams
2170 **
2171 ** Description      This function is called to read the current connection parameters
2172 **                  of the device
2173 **
2174 ** Returns          TRUE or FALSE
2175 **
2176 *******************************************************************************/
2177 
BTM_GetCurrentConnParams(BD_ADDR bda,uint16_t * interval,uint16_t * latency,uint16_t * timeout)2178 BOOLEAN BTM_GetCurrentConnParams(BD_ADDR bda, uint16_t *interval, uint16_t *latency, uint16_t *timeout)
2179 {
2180     if( (interval == NULL) || (latency == NULL) || (timeout == NULL) ) {
2181         BTM_TRACE_ERROR("%s error ", __func__);
2182         return FALSE;
2183     }
2184 
2185     if(btm_get_current_conn_params(bda, interval, latency, timeout)) {
2186         return TRUE;
2187     }
2188 
2189     return FALSE;
2190 }
2191 
2192 /*******************************************************************************
2193 **
2194 ** Function         btm_ble_build_adv_data
2195 **
2196 ** Description      This function is called build the adv data and rsp data.
2197 *******************************************************************************/
btm_ble_build_adv_data(tBTM_BLE_AD_MASK * p_data_mask,UINT8 ** p_dst,tBTM_BLE_ADV_DATA * p_data)2198 UINT8 *btm_ble_build_adv_data(tBTM_BLE_AD_MASK *p_data_mask, UINT8 **p_dst,
2199                               tBTM_BLE_ADV_DATA *p_data)
2200 {
2201     UINT32 data_mask = *p_data_mask;
2202     UINT8   *p = *p_dst,
2203              *p_flag = NULL;
2204     UINT16  len = BTM_BLE_AD_DATA_LEN, cp_len = 0;
2205     UINT8   i = 0;
2206     tBTM_BLE_PROP_ELEM      *p_elem;
2207 
2208     BTM_TRACE_EVENT (" btm_ble_build_adv_data");
2209 
2210     /* build the adv data structure and build the data string */
2211     if (data_mask) {
2212         /* flags */
2213         if (data_mask & BTM_BLE_AD_BIT_FLAGS) {
2214             *p++ = MIN_ADV_LENGTH;
2215             *p++ = BTM_BLE_AD_TYPE_FLAG;
2216             p_flag = p;
2217             if (p_data) {
2218                 *p++ = p_data->flag;
2219             } else {
2220                 *p++ = 0;
2221             }
2222 
2223             len -= 3;
2224 
2225             data_mask &= ~BTM_BLE_AD_BIT_FLAGS;
2226         }
2227         /* appearance data */
2228         if (len > 3 && data_mask & BTM_BLE_AD_BIT_APPEARANCE) {
2229             *p++ = 3; /* length */
2230             *p++ = BTM_BLE_AD_TYPE_APPEARANCE;
2231             UINT16_TO_STREAM(p, p_data->appearance);
2232             len -= 4;
2233 
2234             data_mask &= ~BTM_BLE_AD_BIT_APPEARANCE;
2235         }
2236         /* device name */
2237 #if BTM_MAX_LOC_BD_NAME_LEN > 0
2238         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_DEV_NAME) {
2239             if (strlen(btm_cb.cfg.bd_name) > (UINT16)(len - MIN_ADV_LENGTH)) {
2240                 cp_len = (UINT16)(len - MIN_ADV_LENGTH);
2241                 *p++ = cp_len + 1;
2242                 *p++ = BTM_BLE_AD_TYPE_NAME_SHORT;
2243                 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, cp_len);
2244             } else {
2245                 cp_len = (UINT16)strlen(btm_cb.cfg.bd_name);
2246                 *p++ = cp_len + 1;
2247                 *p++ = BTM_BLE_AD_TYPE_NAME_CMPL;
2248                 ARRAY_TO_STREAM(p, btm_cb.cfg.bd_name, cp_len);
2249             }
2250             len -= (cp_len + MIN_ADV_LENGTH);
2251             data_mask &= ~BTM_BLE_AD_BIT_DEV_NAME;
2252         }
2253 #endif
2254         /* manufacturer data */
2255         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_MANU &&
2256                 p_data && p_data->p_manu &&
2257                 p_data->p_manu->len != 0 && p_data->p_manu->p_val) {
2258             if (p_data->p_manu->len > (len - MIN_ADV_LENGTH)) {
2259                 cp_len = len - MIN_ADV_LENGTH;
2260             } else {
2261                 cp_len = p_data->p_manu->len;
2262             }
2263             BTM_TRACE_DEBUG("cp_len = %d\n,p_data->p_manu->len=%d\n", cp_len, p_data->p_manu->len);
2264             for (int i = 0; i < p_data->p_manu->len; i++) {
2265                 BTM_TRACE_DEBUG("p_data->p_manu->p_val[%d] = %x\n", i, p_data->p_manu->p_val[i]);
2266             }
2267             *p++ = cp_len + 1;
2268             *p++ = BTM_BLE_AD_TYPE_MANU;
2269             ARRAY_TO_STREAM(p, p_data->p_manu->p_val, cp_len);
2270             BTM_TRACE_DEBUG("p_addr = %p\n,p_data->p_manu->p_val = %p\n", p, p_data->p_manu->p_val);
2271             len -= (cp_len + MIN_ADV_LENGTH);
2272             data_mask &= ~BTM_BLE_AD_BIT_MANU;
2273         }
2274         /* TX power */
2275         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_TX_PWR) {
2276             *p++ = MIN_ADV_LENGTH;
2277             *p++ = BTM_BLE_AD_TYPE_TX_PWR;
2278             if (p_data->tx_power > BTM_BLE_ADV_TX_POWER_MAX) {
2279                 p_data->tx_power = BTM_BLE_ADV_TX_POWER_MAX;
2280             }
2281             *p++ = btm_ble_map_adv_tx_power(p_data->tx_power);
2282             len -= 3;
2283             data_mask &= ~BTM_BLE_AD_BIT_TX_PWR;
2284         }
2285         /* 16 bits services */
2286         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE &&
2287                 p_data && p_data->p_services &&
2288                 p_data->p_services->num_service != 0 &&
2289                 p_data->p_services->p_uuid) {
2290             if (p_data->p_services->num_service * LEN_UUID_16 > (len - MIN_ADV_LENGTH)) {
2291                 cp_len = (len - MIN_ADV_LENGTH) / LEN_UUID_16;
2292                 *p ++ = 1 + cp_len * LEN_UUID_16;
2293                 *p++ = BTM_BLE_AD_TYPE_16SRV_PART;
2294             } else {
2295                 cp_len = p_data->p_services->num_service;
2296                 *p++ = 1 + cp_len * LEN_UUID_16;
2297                 *p++ = BTM_BLE_AD_TYPE_16SRV_CMPL;
2298             }
2299             for (i = 0; i < cp_len; i ++) {
2300                 UINT16_TO_STREAM(p, *(p_data->p_services->p_uuid + i));
2301             }
2302 
2303             len -= (cp_len * MIN_ADV_LENGTH + MIN_ADV_LENGTH);
2304             data_mask &= ~BTM_BLE_AD_BIT_SERVICE;
2305         }
2306         /* 32 bits service uuid */
2307         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_32 &&
2308                 p_data && p_data->p_service_32b &&
2309                 p_data->p_service_32b->num_service != 0 &&
2310                 p_data->p_service_32b->p_uuid) {
2311             if ((p_data->p_service_32b->num_service * LEN_UUID_32) > (len - MIN_ADV_LENGTH)) {
2312                 cp_len = (len - MIN_ADV_LENGTH) / LEN_UUID_32;
2313                 *p ++ = 1 + cp_len * LEN_UUID_32;
2314                 *p++ = BTM_BLE_AD_TYPE_32SRV_PART;
2315             } else {
2316                 cp_len = p_data->p_service_32b->num_service;
2317                 *p++ = 1 + cp_len * LEN_UUID_32;
2318                 *p++ = BTM_BLE_AD_TYPE_32SRV_CMPL;
2319             }
2320             for (i = 0; i < cp_len; i ++) {
2321                 UINT32_TO_STREAM(p, *(p_data->p_service_32b->p_uuid + i));
2322             }
2323 
2324             len -= (cp_len * LEN_UUID_32 + MIN_ADV_LENGTH);
2325             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_32;
2326         }
2327         /* 128 bits services */
2328         if (len >= (MAX_UUID_SIZE + 2) && data_mask & BTM_BLE_AD_BIT_SERVICE_128 &&
2329                 p_data && p_data->p_services_128b) {
2330             *p ++ = 1 + MAX_UUID_SIZE;
2331             if (!p_data->p_services_128b->list_cmpl) {
2332                 *p++ = BTM_BLE_AD_TYPE_128SRV_PART;
2333             } else {
2334                 *p++ = BTM_BLE_AD_TYPE_128SRV_CMPL;
2335             }
2336 
2337             ARRAY_TO_STREAM(p, p_data->p_services_128b->uuid128, MAX_UUID_SIZE);
2338 
2339             len -= (MAX_UUID_SIZE + MIN_ADV_LENGTH);
2340             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_128;
2341         }
2342         /* 32 bits Service Solicitation UUIDs */
2343         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_32SOL &&
2344                 p_data && p_data->p_sol_service_32b &&
2345                 p_data->p_sol_service_32b->num_service != 0 &&
2346                 p_data->p_sol_service_32b->p_uuid) {
2347             if ((p_data->p_sol_service_32b->num_service * LEN_UUID_32) > (len - MIN_ADV_LENGTH)) {
2348                 cp_len = (len - MIN_ADV_LENGTH) / LEN_UUID_32;
2349                 *p ++ = 1 + cp_len * LEN_UUID_32;
2350             } else {
2351                 cp_len = p_data->p_sol_service_32b->num_service;
2352                 *p++ = 1 + cp_len * LEN_UUID_32;
2353             }
2354 
2355             *p++ = BTM_BLE_AD_TYPE_32SOL_SRV_UUID;
2356             for (i = 0; i < cp_len; i ++) {
2357                 UINT32_TO_STREAM(p, *(p_data->p_sol_service_32b->p_uuid + i));
2358             }
2359 
2360             len -= (cp_len * LEN_UUID_32 + MIN_ADV_LENGTH);
2361             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_32SOL;
2362         }
2363         /* 128 bits Solicitation services UUID */
2364         if (len >= (MAX_UUID_SIZE + MIN_ADV_LENGTH) && data_mask & BTM_BLE_AD_BIT_SERVICE_128SOL &&
2365                 p_data && p_data->p_sol_service_128b) {
2366             *p ++ = 1 + MAX_UUID_SIZE;
2367             *p++ = BTM_BLE_AD_TYPE_128SOL_SRV_UUID;
2368             ARRAY_TO_STREAM(p, p_data->p_sol_service_128b->uuid128, MAX_UUID_SIZE);
2369             len -= (MAX_UUID_SIZE + MIN_ADV_LENGTH);
2370             data_mask &= ~BTM_BLE_AD_BIT_SERVICE_128SOL;
2371         }
2372         /* 16bits/32bits/128bits Service Data */
2373         if (len > MIN_ADV_LENGTH && data_mask & BTM_BLE_AD_BIT_SERVICE_DATA &&
2374                 p_data && p_data->p_service_data->len != 0 && p_data->p_service_data->p_val) {
2375             if (len  > (p_data->p_service_data->service_uuid.len + MIN_ADV_LENGTH)) {
2376                 if (p_data->p_service_data->len > (len - MIN_ADV_LENGTH)) {
2377                     cp_len = len - MIN_ADV_LENGTH - p_data->p_service_data->service_uuid.len;
2378                 } else {
2379                     cp_len = p_data->p_service_data->len;
2380                 }
2381 
2382                 *p++ = cp_len + 1 + p_data->p_service_data->service_uuid.len;
2383                 if (p_data->p_service_data->service_uuid.len == LEN_UUID_16) {
2384                     *p++ = BTM_BLE_AD_TYPE_SERVICE_DATA;
2385                     UINT16_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid16);
2386                 } else if (p_data->p_service_data->service_uuid.len == LEN_UUID_32) {
2387                     *p++ = BTM_BLE_AD_TYPE_32SERVICE_DATA;
2388                     UINT32_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid32);
2389                 } else {
2390                     *p++ = BTM_BLE_AD_TYPE_128SERVICE_DATA;
2391                     ARRAY_TO_STREAM(p, p_data->p_service_data->service_uuid.uu.uuid128,
2392                                     LEN_UUID_128);
2393                 }
2394 
2395                 ARRAY_TO_STREAM(p, p_data->p_service_data->p_val, cp_len);
2396 
2397                 len -= (cp_len + MIN_ADV_LENGTH + p_data->p_service_data->service_uuid.len);
2398                 data_mask &= ~BTM_BLE_AD_BIT_SERVICE_DATA;
2399             } else {
2400                 BTM_TRACE_WARNING("service data does not fit");
2401             }
2402         }
2403 
2404         if (len >= 6 && data_mask & BTM_BLE_AD_BIT_INT_RANGE &&
2405                 p_data) {
2406             *p++ = 5;
2407             *p++ = BTM_BLE_AD_TYPE_INT_RANGE;
2408             UINT16_TO_STREAM(p, p_data->int_range.low);
2409             UINT16_TO_STREAM(p, p_data->int_range.hi);
2410             len -= 6;
2411             data_mask &= ~BTM_BLE_AD_BIT_INT_RANGE;
2412         }
2413         if (data_mask & BTM_BLE_AD_BIT_PROPRIETARY && p_data && p_data->p_proprietary) {
2414             for (i = 0; i < p_data->p_proprietary->num_elem ; i ++) {
2415                 p_elem = p_data->p_proprietary->p_elem  + i;
2416 
2417                 if (len >= (MIN_ADV_LENGTH + p_elem->len))/* len byte(1) + ATTR type(1) + Uuid len(2)
2418                                                           + value length */
2419                 {
2420                     *p ++ = p_elem->len + 1; /* Uuid len + value length */
2421                     *p ++ = p_elem->adv_type;
2422                     ARRAY_TO_STREAM(p, p_elem->p_val, p_elem->len);
2423 
2424                     len -= (MIN_ADV_LENGTH + p_elem->len);
2425                 } else {
2426                     BTM_TRACE_WARNING("data exceed max adv packet length");
2427                     break;
2428                 }
2429             }
2430             data_mask &= ~BTM_BLE_AD_BIT_PROPRIETARY;
2431         }
2432     }
2433 
2434     *p_data_mask = data_mask;
2435     *p_dst = p;
2436 
2437     return p_flag;
2438 }
2439 /*******************************************************************************
2440 **
2441 ** Function         btm_ble_select_adv_interval
2442 **
2443 ** Description      select adv interval based on device mode
2444 **
2445 ** Returns          void
2446 **
2447 *******************************************************************************/
btm_ble_select_adv_interval(tBTM_BLE_INQ_CB * p_cb,UINT8 evt_type,UINT16 * p_adv_int_min,UINT16 * p_adv_int_max)2448 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)
2449 {
2450     if (p_cb->adv_interval_min && p_cb->adv_interval_max) {
2451         *p_adv_int_min = p_cb->adv_interval_min;
2452         *p_adv_int_max = p_cb->adv_interval_max;
2453     } else {
2454         switch (evt_type) {
2455         case BTM_BLE_CONNECT_EVT:
2456         case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
2457             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
2458             break;
2459 
2460         case BTM_BLE_NON_CONNECT_EVT:
2461         case BTM_BLE_DISCOVER_EVT:
2462             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
2463             break;
2464 
2465         /* connectable directed event */
2466         case BTM_BLE_CONNECT_DIR_EVT:
2467             *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
2468             *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
2469             break;
2470 
2471         default:
2472             *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
2473             break;
2474         }
2475     }
2476     return;
2477 }
2478 
2479 /*******************************************************************************
2480 **
2481 ** Function         btm_ble_update_dmt_flag_bits
2482 **
2483 ** Description      Obtain updated adv flag value based on connect and discoverability mode.
2484 **                  Also, setup DMT support value in the flag based on whether the controller
2485 **                  supports both LE and BR/EDR.
2486 **
2487 ** Parameters:      flag_value (Input / Output) - flag value
2488 **                  connect_mode (Input) - Connect mode value
2489 **                  disc_mode (Input) - discoverability mode
2490 **
2491 ** Returns          void
2492 **
2493 *******************************************************************************/
btm_ble_update_dmt_flag_bits(UINT8 * adv_flag_value,const UINT16 connect_mode,const UINT16 disc_mode)2494 void btm_ble_update_dmt_flag_bits(UINT8 *adv_flag_value, const UINT16 connect_mode,
2495                                   const UINT16 disc_mode)
2496 {
2497     /* BR/EDR non-discoverable , non-connectable */
2498     if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
2499             (connect_mode & BTM_CONNECTABLE_MASK) == 0) {
2500         *adv_flag_value |= BTM_BLE_BREDR_NOT_SPT;
2501     } else {
2502         *adv_flag_value &= ~BTM_BLE_BREDR_NOT_SPT;
2503     }
2504 
2505     /* if local controller support, mark both controller and host support in flag */
2506     if (controller_get_interface()->supports_simultaneous_le_bredr()) {
2507         *adv_flag_value |= (BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
2508     } else {
2509         *adv_flag_value &= ~(BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
2510     }
2511 }
2512 
2513 /*******************************************************************************
2514 **
2515 ** Function         btm_ble_set_adv_flag
2516 **
2517 ** Description      Set adv flag in adv data.
2518 **
2519 ** Parameters:      connect_mode (Input)- Connect mode value
2520 **                  disc_mode (Input) - discoverability mode
2521 **
2522 ** Returns          void
2523 **
2524 *******************************************************************************/
btm_ble_set_adv_flag(UINT16 connect_mode,UINT16 disc_mode)2525 void btm_ble_set_adv_flag(UINT16 connect_mode, UINT16 disc_mode)
2526 {
2527     UINT8 flag = 0, old_flag = 0;
2528     tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
2529 
2530     if (p_adv_data->p_flags != NULL) {
2531         flag = old_flag = *(p_adv_data->p_flags);
2532     }
2533 
2534     btm_ble_update_dmt_flag_bits (&flag, connect_mode, disc_mode);
2535 
2536     BTM_TRACE_DEBUG("disc_mode %04x", disc_mode);
2537     /* update discoverable flag */
2538     if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE) {
2539         flag &= ~BTM_BLE_GEN_DISC_FLAG;
2540         flag |= BTM_BLE_LIMIT_DISC_FLAG;
2541     } else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE) {
2542         flag |= BTM_BLE_GEN_DISC_FLAG;
2543         flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
2544     } else { /* remove all discoverable flags */
2545         flag &= ~(BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG);
2546     }
2547 
2548     if (flag != old_flag) {
2549         BTM_TRACE_ERROR("flag = 0x%x,old_flag = 0x%x", flag, old_flag);
2550         btm_ble_update_adv_flag(flag);
2551     }
2552 }
2553 /*******************************************************************************
2554 **
2555 ** Function         btm_ble_set_discoverability
2556 **
2557 ** Description      This function is called to set BLE discoverable mode.
2558 **
2559 ** Parameters:      combined_mode: discoverability mode.
2560 **
2561 ** Returns          BTM_SUCCESS is status set successfully; otherwise failure.
2562 **
2563 *******************************************************************************/
btm_ble_set_discoverability(UINT16 combined_mode)2564 tBTM_STATUS btm_ble_set_discoverability(UINT16 combined_mode)
2565 {
2566     tBTM_LE_RANDOM_CB   *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2567     tBTM_BLE_INQ_CB     *p_cb = &btm_cb.ble_ctr_cb.inq_var;
2568     UINT16              mode = (combined_mode &  BTM_BLE_DISCOVERABLE_MASK);
2569     UINT8               new_mode = BTM_BLE_ADV_ENABLE;
2570     UINT8               evt_type;
2571     tBTM_STATUS         status = BTM_SUCCESS;
2572     BD_ADDR             p_addr_ptr = {0};
2573     tBLE_ADDR_TYPE      init_addr_type = BLE_ADDR_PUBLIC,
2574                         own_addr_type = p_addr_cb->own_addr_type;
2575     UINT16              adv_int_min, adv_int_max;
2576 
2577     BTM_TRACE_EVENT ("%s mode=0x%0x combined_mode=0x%x\n", __FUNCTION__, mode, combined_mode);
2578 
2579     /*** Check mode parameter ***/
2580     if (mode > BTM_BLE_MAX_DISCOVERABLE) {
2581         return (BTM_ILLEGAL_VALUE);
2582     }
2583 
2584     p_cb->discoverable_mode = mode;
2585 
2586     evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
2587 
2588     if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE && mode == BTM_BLE_NON_DISCOVERABLE) {
2589         new_mode = BTM_BLE_ADV_DISABLE;
2590     }
2591 
2592     btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
2593 
2594     btu_stop_timer(&p_cb->fast_adv_timer);
2595 
2596     /* update adv params if start advertising */
2597     BTM_TRACE_EVENT ("evt_type=0x%x p-cb->evt_type=0x%x\n ", evt_type, p_cb->evt_type);
2598 
2599     if (new_mode == BTM_BLE_ADV_ENABLE) {
2600         btm_ble_set_adv_flag (btm_cb.btm_inq_vars.connectable_mode, combined_mode);
2601 
2602         if (evt_type != p_cb->evt_type || p_cb->adv_addr_type != own_addr_type
2603                 || !p_cb->fast_adv_on) {
2604             btm_ble_stop_adv();
2605 
2606             /* update adv params */
2607             if (!btsnd_hcic_ble_write_adv_params (adv_int_min,
2608                                                   adv_int_max,
2609                                                   evt_type,
2610                                                   own_addr_type,
2611                                                   init_addr_type,
2612                                                   p_addr_ptr,
2613                                                   p_cb->adv_chnl_map,
2614                                                   p_cb->afp)) {
2615                 status = BTM_NO_RESOURCES;
2616             } else {
2617                 p_cb->evt_type = evt_type;
2618                 p_cb->adv_addr_type = own_addr_type;
2619             }
2620         }
2621     }
2622 
2623     if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode) {
2624         if (new_mode == BTM_BLE_ADV_ENABLE) {
2625             status = btm_ble_start_adv();
2626         } else {
2627             status = btm_ble_stop_adv();
2628         }
2629     }
2630 
2631     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2632         p_cb->fast_adv_on = TRUE;
2633         /* start initial GAP mode adv timer */
2634         btu_start_timer (&p_cb->fast_adv_timer, BTU_TTYPE_BLE_GAP_FAST_ADV,
2635                          BTM_BLE_GAP_FAST_ADV_TOUT);
2636     } else {
2637 #if BLE_PRIVACY_SPT == TRUE
2638         btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
2639 #endif
2640     }
2641 
2642     /* set up stop advertising timer */
2643     if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE) {
2644         BTM_TRACE_EVENT ("start timer for limited disc mode duration=%d (180 secs)", BTM_BLE_GAP_LIM_TOUT);
2645         /* start Tgap(lim_timeout) */
2646         btu_start_timer (&p_cb->inq_timer_ent, BTU_TTYPE_BLE_GAP_LIM_DISC,
2647                          BTM_BLE_GAP_LIM_TOUT);
2648     }
2649     return status;
2650 }
2651 
2652 /*******************************************************************************
2653 **
2654 ** Function         btm_ble_set_connectability
2655 **
2656 ** Description      This function is called to set BLE connectability mode.
2657 **
2658 ** Parameters:      combined_mode: connectability mode.
2659 **
2660 ** Returns          BTM_SUCCESS is status set successfully; otherwise failure.
2661 **
2662 *******************************************************************************/
btm_ble_set_connectability(UINT16 combined_mode)2663 tBTM_STATUS btm_ble_set_connectability(UINT16 combined_mode)
2664 {
2665     tBTM_LE_RANDOM_CB       *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2666     tBTM_BLE_INQ_CB         *p_cb = &btm_cb.ble_ctr_cb.inq_var;
2667     UINT16                  mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
2668     UINT8                   new_mode = BTM_BLE_ADV_ENABLE;
2669     UINT8                   evt_type;
2670     tBTM_STATUS             status = BTM_SUCCESS;
2671     BD_ADDR                 p_addr_ptr =  {0};
2672     tBLE_ADDR_TYPE          peer_addr_type = BLE_ADDR_PUBLIC,
2673                             own_addr_type = p_addr_cb->own_addr_type;
2674     UINT16                  adv_int_min, adv_int_max;
2675 
2676     BTM_TRACE_EVENT ("%s mode=0x%0x combined_mode=0x%x\n", __FUNCTION__, mode, combined_mode);
2677 
2678     /*** Check mode parameter ***/
2679     if (mode > BTM_BLE_MAX_CONNECTABLE) {
2680         return (BTM_ILLEGAL_VALUE);
2681     }
2682 
2683     p_cb->connectable_mode = mode;
2684 
2685     evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &peer_addr_type, &own_addr_type);
2686 
2687     if (mode == BTM_BLE_NON_CONNECTABLE && p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE) {
2688         new_mode = BTM_BLE_ADV_DISABLE;
2689     }
2690 
2691     btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
2692 
2693     btu_stop_timer(&p_cb->fast_adv_timer);
2694     /* update adv params if needed */
2695     if (new_mode == BTM_BLE_ADV_ENABLE) {
2696         btm_ble_set_adv_flag (combined_mode, btm_cb.btm_inq_vars.discoverable_mode);
2697         if (p_cb->evt_type != evt_type || p_cb->adv_addr_type != p_addr_cb->own_addr_type
2698                 || !p_cb->fast_adv_on) {
2699             btm_ble_stop_adv();
2700 
2701             if (!btsnd_hcic_ble_write_adv_params (adv_int_min,
2702                                                   adv_int_max,
2703                                                   evt_type,
2704                                                   own_addr_type,
2705                                                   peer_addr_type,
2706                                                   p_addr_ptr,
2707                                                   p_cb->adv_chnl_map,
2708                                                   p_cb->afp)) {
2709                 status = BTM_NO_RESOURCES;
2710             } else {
2711                 p_cb->evt_type = evt_type;
2712                 p_cb->adv_addr_type = own_addr_type;
2713             }
2714         }
2715     }
2716 
2717     /* update advertising mode */
2718     if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode) {
2719         if (new_mode == BTM_BLE_ADV_ENABLE) {
2720             status = btm_ble_start_adv();
2721         } else {
2722             status = btm_ble_stop_adv();
2723         }
2724     }
2725 
2726     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2727         p_cb->fast_adv_on = TRUE;
2728         /* start initial GAP mode adv timer */
2729         btu_start_timer (&p_cb->fast_adv_timer, BTU_TTYPE_BLE_GAP_FAST_ADV,
2730                          BTM_BLE_GAP_FAST_ADV_TOUT);
2731     } else {
2732 #if BLE_PRIVACY_SPT == TRUE
2733         btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
2734 #endif
2735     }
2736     return status;
2737 }
2738 
2739 
2740 /*******************************************************************************
2741 **
2742 ** Function         btm_ble_start_inquiry
2743 **
2744 ** Description      This function is called to start BLE inquiry procedure.
2745 **                  If the duration is zero, the periodic inquiry mode is cancelled.
2746 **
2747 ** Parameters:      mode - GENERAL or LIMITED inquiry
2748 **                  p_inq_params - pointer to the BLE inquiry parameter.
2749 **                  p_results_cb - callback returning pointer to results (tBTM_INQ_RESULTS)
2750 **                  p_cmpl_cb - callback indicating the end of an inquiry
2751 **
2752 **
2753 **
2754 ** Returns          BTM_CMD_STARTED if successfully started
2755 **                  BTM_NO_RESOURCES if could not allocate a message buffer
2756 **                  BTM_BUSY - if an inquiry is already active
2757 **
2758 *******************************************************************************/
btm_ble_start_inquiry(UINT8 mode,UINT8 duration)2759 tBTM_STATUS btm_ble_start_inquiry (UINT8 mode, UINT8   duration)
2760 {
2761     tBTM_STATUS status = BTM_CMD_STARTED;
2762     tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
2763     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
2764 
2765     BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x", mode, btm_cb.btm_inq_vars.inq_active);
2766 
2767     /* if selective connection is active, or inquiry is already active, reject it */
2768     if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity) ||
2769             BTM_BLE_IS_SEL_CONN_ACTIVE (p_ble_cb->scan_activity)) {
2770         BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
2771         return (BTM_BUSY);
2772     }
2773 
2774     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
2775         btsnd_hcic_ble_set_scan_params(BTM_BLE_SCAN_MODE_ACTI,
2776                                        BTM_BLE_LOW_LATENCY_SCAN_INT,
2777                                        BTM_BLE_LOW_LATENCY_SCAN_WIN,
2778                                        btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
2779                                        SP_ADV_ALL);
2780 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
2781         /* enable IRK list */
2782         //btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
2783 #endif
2784         status = btm_ble_start_scan();
2785     } else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) ||
2786                (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
2787         BTM_TRACE_DEBUG("%s, restart LE scan with low latency scan params", __FUNCTION__);
2788         btsnd_hcic_ble_set_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
2789         btsnd_hcic_ble_set_scan_params(BTM_BLE_SCAN_MODE_ACTI,
2790                                        BTM_BLE_LOW_LATENCY_SCAN_INT,
2791                                        BTM_BLE_LOW_LATENCY_SCAN_WIN,
2792                                        btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type,
2793                                        SP_ADV_ALL);
2794         btsnd_hcic_ble_set_scan_enable(BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE);
2795     }
2796 
2797     if (status == BTM_CMD_STARTED) {
2798         p_inq->inq_active |= mode;
2799         p_ble_cb->scan_activity |= mode;
2800 
2801         BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x", p_inq->inq_active);
2802 
2803         if (duration != 0) {
2804             /* start inquiry timer */
2805             btu_start_timer (&p_ble_cb->inq_var.inq_timer_ent, BTU_TTYPE_BLE_INQUIRY, duration);
2806         }
2807     }
2808 
2809     return status;
2810 
2811 }
2812 
2813 /*******************************************************************************
2814 **
2815 ** Function         btm_ble_read_remote_name_cmpl
2816 **
2817 ** Description      This function is called when BLE remote name is received.
2818 **
2819 ** Returns          void
2820 **
2821 *******************************************************************************/
btm_ble_read_remote_name_cmpl(BOOLEAN status,BD_ADDR bda,UINT16 length,char * p_name)2822 void btm_ble_read_remote_name_cmpl(BOOLEAN status, BD_ADDR bda, UINT16 length, char *p_name)
2823 {
2824     UINT8   hci_status = HCI_SUCCESS;
2825     BD_NAME bd_name;
2826 
2827     memset(bd_name, 0, (BD_NAME_LEN + 1));
2828     if (length > BD_NAME_LEN) {
2829         length = BD_NAME_LEN;
2830     }
2831     memcpy((UINT8 *)bd_name, p_name, length);
2832 
2833     if ((!status) || (length == 0)) {
2834         hci_status = HCI_ERR_HOST_TIMEOUT;
2835     }
2836 
2837     btm_process_remote_name(bda, bd_name, length + 1, hci_status);
2838 #if (SMP_INCLUDED == TRUE)
2839     btm_sec_rmt_name_request_complete (bda, (UINT8 *)p_name, hci_status);
2840 #endif  ///SMP_INCLUDED == TRUE
2841 }
2842 
2843 /*******************************************************************************
2844 **
2845 ** Function         btm_ble_read_remote_name
2846 **
2847 ** Description      This function read remote LE device name using GATT read
2848 **                  procedure.
2849 **
2850 ** Parameters:       None.
2851 **
2852 ** Returns          void
2853 **
2854 *******************************************************************************/
btm_ble_read_remote_name(BD_ADDR remote_bda,tBTM_INQ_INFO * p_cur,tBTM_CMPL_CB * p_cb)2855 tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO *p_cur, tBTM_CMPL_CB *p_cb)
2856 {
2857     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
2858 
2859     if (!controller_get_interface()->supports_ble()) {
2860         return BTM_ERR_PROCESSING;
2861     }
2862 
2863     if (p_cur &&
2864             p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_ADV &&
2865             p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_DIR_ADV) {
2866         BTM_TRACE_DEBUG("name request to non-connectable device failed.");
2867         return BTM_ERR_PROCESSING;
2868     }
2869 
2870     /* read remote device name using GATT procedure */
2871     if (p_inq->remname_active) {
2872         return BTM_BUSY;
2873     }
2874 
2875 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
2876     if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl)) {
2877         return BTM_BUSY;
2878     }
2879 #endif
2880 
2881     p_inq->p_remname_cmpl_cb = p_cb;
2882     p_inq->remname_active = TRUE;
2883 
2884     memcpy(p_inq->remname_bda, remote_bda, BD_ADDR_LEN);
2885 
2886     btu_start_timer (&p_inq->rmt_name_timer_ent,
2887                      BTU_TTYPE_BTM_RMT_NAME,
2888                      BTM_EXT_BLE_RMT_NAME_TIMEOUT);
2889 
2890     return BTM_CMD_STARTED;
2891 }
2892 
2893 /*******************************************************************************
2894 **
2895 ** Function         btm_ble_cancel_remote_name
2896 **
2897 ** Description      This function cancel read remote LE device name.
2898 **
2899 ** Parameters:       None.
2900 **
2901 ** Returns          void
2902 **
2903 *******************************************************************************/
btm_ble_cancel_remote_name(BD_ADDR remote_bda)2904 BOOLEAN btm_ble_cancel_remote_name(BD_ADDR remote_bda)
2905 {
2906     tBTM_INQUIRY_VAR_ST      *p_inq = &btm_cb.btm_inq_vars;
2907     BOOLEAN     status = TRUE;
2908 
2909 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE && GATTS_INCLUDED == TRUE)
2910     status = GAP_BleCancelReadPeerDevName(remote_bda);
2911 #endif
2912 
2913     p_inq->remname_active = FALSE;
2914     memset(p_inq->remname_bda, 0, BD_ADDR_LEN);
2915     btu_stop_timer(&p_inq->rmt_name_timer_ent);
2916 
2917     return status;
2918 }
2919 
2920 /*******************************************************************************
2921 **
2922 ** Function         btm_ble_update_adv_flag
2923 **
2924 ** Description      This function update the limited discoverable flag in the adv
2925 **                  data.
2926 **
2927 ** Parameters:       None.
2928 **
2929 ** Returns          void
2930 **
2931 *******************************************************************************/
btm_ble_update_adv_flag(UINT8 flag)2932 static void btm_ble_update_adv_flag(UINT8 flag)
2933 {
2934     tBTM_BLE_LOCAL_ADV_DATA *p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
2935     UINT8   *p;
2936 
2937     BTM_TRACE_DEBUG ("btm_ble_update_adv_flag new=0x%x", flag);
2938 
2939     if (p_adv_data->p_flags != NULL) {
2940         BTM_TRACE_DEBUG ("btm_ble_update_adv_flag old=0x%x",   *p_adv_data->p_flags);
2941         *p_adv_data->p_flags = flag;
2942     } else { /* no FLAGS in ADV data*/
2943         p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
2944         /* need 3 bytes space to stuff in the flags, if not */
2945         /* erase all written data, just for flags */
2946         if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3) {
2947             p = p_adv_data->p_pad = p_adv_data->ad_data;
2948             memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
2949         }
2950 
2951         *p++ = 2;
2952         *p++ = BTM_BLE_AD_TYPE_FLAG;
2953         p_adv_data->p_flags = p;
2954         *p++ = flag;
2955         p_adv_data->p_pad = p;
2956     }
2957 
2958     if (btsnd_hcic_ble_set_adv_data((UINT8)(p_adv_data->p_pad - p_adv_data->ad_data),
2959                                     p_adv_data->ad_data)) {
2960         p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
2961     }
2962 
2963 }
2964 
2965 #if 0
2966 /*******************************************************************************
2967 **
2968 ** Function         btm_ble_parse_adv_data
2969 **
2970 ** Description      This function parse the adv data into a structure.
2971 **
2972 ** Returns          pointer to entry, or NULL if not found
2973 **
2974 *******************************************************************************/
2975 static void btm_ble_parse_adv_data(tBTM_INQ_INFO *p_info, UINT8 *p_data,
2976                                    UINT8 len, tBTM_BLE_INQ_DATA *p_adv_data, UINT8 *p_buf)
2977 {
2978     UINT8   *p_cur = p_data;
2979     UINT8   ad_len, ad_type, ad_flag;
2980 
2981     BTM_TRACE_EVENT (" btm_ble_parse_adv_data");
2982 
2983     while (len > 0) {
2984         BTM_TRACE_DEBUG("btm_ble_parse_adv_data: len = %d", len);
2985         if ((ad_len = *p_cur ++) == 0) {
2986             break;
2987         }
2988 
2989         ad_type = *p_cur ++;
2990 
2991         BTM_TRACE_DEBUG("     ad_type = %02x ad_len = %d", ad_type, ad_len);
2992 
2993         switch (ad_type) {
2994         case BTM_BLE_AD_TYPE_NAME_SHORT:
2995 
2996         case BTM_BLE_AD_TYPE_NAME_CMPL:
2997             p_adv_data->ad_mask |= BTM_BLE_AD_BIT_DEV_NAME;
2998             if (p_info) {
2999                 p_info->remote_name_type = (ad_type == BTM_BLE_AD_TYPE_NAME_SHORT) ?
3000                                            BTM_BLE_NAME_SHORT : BTM_BLE_NAME_CMPL;
3001                 memcpy(p_info->remote_name, p_cur, ad_len - 1);
3002                 p_info->remote_name[ad_len] = 0;
3003                 p_adv_data->p_remote_name = p_info->remote_name;
3004                 p_info->remote_name_len = p_adv_data->remote_name_len = ad_len - 1;
3005                 BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_NAME name = %s", p_adv_data->p_remote_name);
3006             }
3007             p_cur += (ad_len - 1);
3008 
3009             break;
3010 
3011         case BTM_BLE_AD_TYPE_FLAG:
3012             p_adv_data->ad_mask |= BTM_BLE_AD_BIT_FLAGS;
3013             ad_flag = *p_cur ++;
3014             p_adv_data->flag = (UINT8)(ad_flag & BTM_BLE_ADV_FLAG_MASK) ;
3015             BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_FLAG flag = %s | %s | %s",
3016                             (p_adv_data->flag & BTM_BLE_LIMIT_DISC_FLAG) ? "LE_LIMIT_DISC" : "",
3017                             (p_adv_data->flag & BTM_BLE_GEN_DISC_FLAG) ? "LE_GENERAL_DISC" : "",
3018                             (p_adv_data->flag & BTM_BLE_BREDR_NOT_SPT) ? "LE Only device" : "");
3019             break;
3020 
3021         case BTM_BLE_AD_TYPE_TX_PWR:
3022             p_adv_data->ad_mask |= BTM_BLE_AD_BIT_TX_PWR;
3023             p_adv_data->tx_power_level = (INT8) * p_cur ++;
3024             BTM_TRACE_DEBUG("BTM_BLE_AD_TYPE_TX_PWR tx_level = %d", p_adv_data->tx_power_level);
3025             break;
3026 
3027         case BTM_BLE_AD_TYPE_MANU:
3028 
3029         case BTM_BLE_AD_TYPE_16SRV_PART:
3030         case BTM_BLE_AD_TYPE_16SRV_CMPL:
3031             p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE;
3032             /* need allocate memory to store UUID list */
3033             p_adv_data->service.num_service = (ad_len - 1) / 2;
3034             BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
3035             p_cur += (ad_len - 1);
3036             break;
3037 
3038         case BTM_BLE_AD_TYPE_SOL_SRV_UUID:
3039             p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_SOL;
3040             /* need allocate memory to store UUID list */
3041             p_adv_data->service.num_service = (ad_len - 1) / 2;
3042             BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
3043             p_cur += (ad_len - 1);
3044             break;
3045 
3046         case BTM_BLE_AD_TYPE_128SOL_SRV_UUID:
3047             p_adv_data->ad_mask |= BTM_BLE_AD_BIT_SERVICE_128SOL;
3048             /* need allocate memory to store UUID list */
3049             p_adv_data->service.num_service = (ad_len - 1) / 16;
3050             BTM_TRACE_DEBUG("service UUID list, num = %d", p_adv_data->service.num_service);
3051             p_cur += (ad_len - 1);
3052             break;
3053 
3054         case BTM_BLE_AD_TYPE_APPEARANCE:
3055         case BTM_BLE_AD_TYPE_PUBLIC_TARGET:
3056         case BTM_BLE_AD_TYPE_RANDOM_TARGET:
3057         default:
3058             break;
3059         }
3060         len -= (ad_len + 1);
3061     }
3062 }
3063 #endif
3064 
3065 /*******************************************************************************
3066 **
3067 ** Function         btm_ble_cache_adv_data
3068 **
3069 ** Description      Update advertising cache data.
3070 **
3071 ** Returns          void
3072 **
3073 *******************************************************************************/
btm_ble_cache_adv_data(BD_ADDR bda,tBTM_INQ_RESULTS * p_cur,UINT8 data_len,UINT8 * p,UINT8 evt_type)3074 void btm_ble_cache_adv_data(BD_ADDR bda, tBTM_INQ_RESULTS *p_cur, UINT8 data_len, UINT8 *p, UINT8 evt_type)
3075 {
3076     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
3077     UINT8 *p_cache;
3078     //UINT8 length;
3079 
3080     /* cache adv report/scan response data */
3081     if (evt_type != BTM_BLE_SCAN_RSP_EVT) {
3082         p_le_inq_cb->adv_len = 0;
3083         memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
3084         p_cur->adv_data_len = 0;
3085         p_cur->scan_rsp_len = 0;
3086     }
3087 
3088     //Clear the adv cache if the addresses are not equal
3089     if(memcmp(bda, p_le_inq_cb->adv_addr, BD_ADDR_LEN) != 0) {
3090         p_le_inq_cb->adv_len = 0;
3091         memcpy(p_le_inq_cb->adv_addr, bda, BD_ADDR_LEN);
3092         memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
3093         p_cur->adv_data_len = 0;
3094         p_cur->scan_rsp_len = 0;
3095     }
3096 
3097     if (data_len > 0) {
3098         p_cache = &p_le_inq_cb->adv_data_cache[p_le_inq_cb->adv_len];
3099         if((data_len + p_le_inq_cb->adv_len) <= BTM_BLE_CACHE_ADV_DATA_MAX) {
3100             memcpy(p_cache, p, data_len);
3101             p_le_inq_cb->adv_len += data_len;
3102         }
3103     }
3104 
3105     if (evt_type != BTM_BLE_SCAN_RSP_EVT) {
3106         p_cur->adv_data_len = p_le_inq_cb->adv_len;
3107     }
3108     else {
3109         p_cur->scan_rsp_len = p_le_inq_cb->adv_len - p_cur->adv_data_len;
3110     }
3111 
3112     /* parse service UUID from adv packet and save it in inq db eir_uuid */
3113     /* TODO */
3114 }
3115 
3116 /*******************************************************************************
3117 **
3118 ** Function         btm_ble_is_discoverable
3119 **
3120 ** Description      check ADV flag to make sure device is discoverable and match
3121 **                  the search condition
3122 **
3123 ** Parameters
3124 **
3125 ** Returns          void
3126 **
3127 *******************************************************************************/
btm_ble_is_discoverable(BD_ADDR bda,UINT8 evt_type,UINT8 * p)3128 UINT8 btm_ble_is_discoverable(BD_ADDR bda, UINT8 evt_type, UINT8 *p)
3129 {
3130     UINT8               *p_flag, flag = 0, rt = 0;
3131     UINT8                data_len;
3132     tBTM_INQ_PARMS      *p_cond = &btm_cb.btm_inq_vars.inqparms;
3133     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
3134 
3135     UNUSED(p);
3136 
3137     /* for observer, always "discoverable */
3138     if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
3139         rt |= BTM_BLE_OBS_RESULT;
3140     }
3141     /* for discover, always "discoverable */
3142     if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
3143         rt |= BTM_BLE_DISCO_RESULT;
3144     }
3145 
3146     if (BTM_BLE_IS_SEL_CONN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity) &&
3147             (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_CONNECT_DIR_EVT)) {
3148         rt |= BTM_BLE_SEL_CONN_RESULT;
3149     }
3150 
3151     /* does not match filter condition */
3152     if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
3153             memcmp(bda, p_cond->filter_cond.bdaddr_cond, BD_ADDR_LEN) != 0) {
3154         BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
3155         return rt;
3156     }
3157 
3158     if (p_le_inq_cb->adv_len != 0) {
3159         if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
3160                                        BTM_BLE_AD_TYPE_FLAG, &data_len)) != NULL) {
3161             flag = * p_flag;
3162 
3163             if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
3164                     (flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) {
3165                 BTM_TRACE_DEBUG("Find Generable Discoverable device");
3166                 rt |= BTM_BLE_INQ_RESULT;
3167             }
3168 
3169             else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
3170                      (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0) {
3171                 BTM_TRACE_DEBUG("Find limited discoverable device");
3172                 rt |= BTM_BLE_INQ_RESULT;
3173             }
3174         }
3175     }
3176     return rt;
3177 }
3178 
btm_ble_appearance_to_cod(UINT16 appearance,UINT8 * dev_class)3179 static void btm_ble_appearance_to_cod(UINT16 appearance, UINT8 *dev_class)
3180 {
3181     dev_class[0] = 0;
3182 
3183     switch (appearance) {
3184     case BTM_BLE_APPEARANCE_GENERIC_PHONE:
3185         dev_class[1] = BTM_COD_MAJOR_PHONE;
3186         dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
3187         break;
3188     case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
3189         dev_class[1] = BTM_COD_MAJOR_COMPUTER;
3190         dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
3191         break;
3192     case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
3193         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3194         dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
3195         break;
3196     case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
3197     case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
3198         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3199         dev_class[2] = BTM_COD_MINOR_THERMOMETER;
3200         break;
3201     case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
3202     case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
3203         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3204         dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
3205         break;
3206     case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
3207     case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
3208     case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
3209         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3210         dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
3211         break;
3212     case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
3213     case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
3214     case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
3215         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3216         dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
3217         break;
3218     case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
3219         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3220         dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
3221         break;
3222     case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
3223         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3224         dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
3225         break;
3226     case BTM_BLE_APPEARANCE_GENERIC_WALKING:
3227     case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
3228     case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
3229     case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
3230         dev_class[1] = BTM_COD_MAJOR_HEALTH;
3231         dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
3232         break;
3233     case BTM_BLE_APPEARANCE_GENERIC_WATCH:
3234     case BTM_BLE_APPEARANCE_SPORTS_WATCH:
3235         dev_class[1] = BTM_COD_MAJOR_WEARABLE;
3236         dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
3237         break;
3238     case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
3239         dev_class[1] = BTM_COD_MAJOR_WEARABLE;
3240         dev_class[2] = BTM_COD_MINOR_GLASSES;
3241         break;
3242     case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
3243         dev_class[1] = BTM_COD_MAJOR_IMAGING;
3244         dev_class[2] = BTM_COD_MINOR_DISPLAY;
3245         break;
3246     case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
3247         dev_class[1] = BTM_COD_MAJOR_AUDIO;
3248         dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
3249         break;
3250     case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
3251     case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
3252     case BTM_BLE_APPEARANCE_GENERIC_HID:
3253         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3254         dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
3255         break;
3256     case BTM_BLE_APPEARANCE_HID_KEYBOARD:
3257         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3258         dev_class[2] = BTM_COD_MINOR_KEYBOARD;
3259         break;
3260     case BTM_BLE_APPEARANCE_HID_MOUSE:
3261         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3262         dev_class[2] = BTM_COD_MINOR_POINTING;
3263         break;
3264     case BTM_BLE_APPEARANCE_HID_JOYSTICK:
3265         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3266         dev_class[2] = BTM_COD_MINOR_JOYSTICK;
3267         break;
3268     case BTM_BLE_APPEARANCE_HID_GAMEPAD:
3269         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3270         dev_class[2] = BTM_COD_MINOR_GAMEPAD;
3271         break;
3272     case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
3273         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3274         dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
3275         break;
3276     case BTM_BLE_APPEARANCE_HID_CARD_READER:
3277         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3278         dev_class[2] = BTM_COD_MINOR_CARD_READER;
3279         break;
3280     case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
3281         dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3282         dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
3283         break;
3284     case BTM_BLE_APPEARANCE_UNKNOWN:
3285     case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
3286     case BTM_BLE_APPEARANCE_GENERIC_TAG:
3287     case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
3288     case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
3289     case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
3290     case BTM_BLE_APPEARANCE_CYCLING_SPEED:
3291     case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
3292     case BTM_BLE_APPEARANCE_CYCLING_POWER:
3293     case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
3294     case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
3295     case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
3296     case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
3297     case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
3298     case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
3299     default:
3300         dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
3301         dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
3302     };
3303 }
3304 
3305 /*******************************************************************************
3306 **
3307 ** Function         btm_ble_update_inq_result
3308 **
3309 ** Description      Update adv packet information into inquiry result.
3310 **
3311 ** Parameters
3312 **
3313 ** Returns          void
3314 **
3315 *******************************************************************************/
btm_ble_update_inq_result(BD_ADDR bda,tINQ_DB_ENT * p_i,UINT8 addr_type,UINT8 evt_type,UINT8 * p)3316 BOOLEAN btm_ble_update_inq_result(BD_ADDR bda, tINQ_DB_ENT *p_i, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
3317 {
3318     BOOLEAN             to_report = TRUE;
3319     tBTM_INQ_RESULTS     *p_cur = &p_i->inq_info.results;
3320     UINT8               len;
3321     UINT8               *p_flag;
3322     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
3323     UINT8                data_len, rssi;
3324     tBTM_BLE_INQ_CB     *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
3325     UINT8 *p1;
3326     UINT8               *p_uuid16;
3327 
3328     STREAM_TO_UINT8    (data_len, p);
3329 
3330     if (data_len > BTM_BLE_ADV_DATA_LEN_MAX) {
3331         BTM_TRACE_WARNING("EIR data too long %d. discard", data_len);
3332         return FALSE;
3333     }
3334     btm_ble_cache_adv_data(bda, p_cur, data_len, p, evt_type);
3335 
3336     p1 = (p + data_len);
3337     STREAM_TO_UINT8 (rssi, p1);
3338 
3339     /* Save the info */
3340     p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
3341     p_cur->ble_addr_type    = addr_type;
3342     p_cur->rssi = rssi;
3343 
3344     /* active scan, always wait until get scan_rsp to report the result */
3345     if ((btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
3346             (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_DISCOVER_EVT))) {
3347         BTM_TRACE_DEBUG("btm_ble_update_inq_result scan_rsp=false, to_report=false,\
3348                               scan_type_active=%d", btm_cb.ble_ctr_cb.inq_var.scan_type);
3349         p_i->scan_rsp = FALSE;
3350 #if BTM_BLE_ACTIVE_SCAN_REPORT_ADV_SCAN_RSP_INDIVIDUALLY == FALSE
3351         to_report = FALSE;
3352 #endif
3353     } else {
3354         p_i->scan_rsp = TRUE;
3355     }
3356 
3357     if (p_i->inq_count != p_inq->inq_counter) {
3358         p_cur->device_type = BT_DEVICE_TYPE_BLE;
3359     } else {
3360         p_cur->device_type |= BT_DEVICE_TYPE_BLE;
3361     }
3362 
3363     if (evt_type != BTM_BLE_SCAN_RSP_EVT) {
3364         p_cur->ble_evt_type     = evt_type;
3365     }
3366 
3367     p_i->inq_count = p_inq->inq_counter;   /* Mark entry for current inquiry */
3368 
3369     if (p_le_inq_cb->adv_len != 0) {
3370         if ((p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_FLAG, &len)) != NULL) {
3371             p_cur->flag = * p_flag;
3372         }
3373     }
3374 
3375     if (p_le_inq_cb->adv_len != 0) {
3376         /* Check to see the BLE device has the Appearance UUID in the advertising data.  If it does
3377          * then try to convert the appearance value to a class of device value Bluedroid can use.
3378          * Otherwise fall back to trying to infer if it is a HID device based on the service class.
3379          */
3380         p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_APPEARANCE, &len);
3381         if (p_uuid16 && len == 2) {
3382             btm_ble_appearance_to_cod((UINT16)p_uuid16[0] | (p_uuid16[1] << 8), p_cur->dev_class);
3383         } else {
3384             if ((p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
3385                                              BTM_BLE_AD_TYPE_16SRV_CMPL, &len)) != NULL) {
3386                 UINT8 i;
3387                 for (i = 0; i + 2 <= len; i = i + 2) {
3388 #if BTA_HH_LE_INCLUDED == TRUE
3389 					/* if this BLE device support HID over LE, set HID Major in class of device */
3390                     if ((p_uuid16[i] | (p_uuid16[i + 1] << 8)) == UUID_SERVCLASS_LE_HID) {
3391                         p_cur->dev_class[0] = 0;
3392                         p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
3393                         p_cur->dev_class[2] = 0;
3394                         break;
3395                     }
3396 #endif /* BTA_HH_LE_INCLUDED */
3397                 }
3398             }
3399         }
3400     }
3401 
3402     /* if BR/EDR not supported is not set, assume is a DUMO device */
3403     if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
3404             evt_type != BTM_BLE_CONNECT_DIR_EVT) {
3405         if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) {
3406             BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
3407             p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
3408         } else {
3409             BTM_TRACE_DEBUG("Random address, treating device as LE only");
3410         }
3411     } else {
3412         BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
3413     }
3414 
3415     return to_report;
3416 
3417 }
3418 
3419 /*******************************************************************************
3420 **
3421 ** Function         btm_clear_all_pending_le_entry
3422 **
3423 ** Description      This function is called to clear all LE pending entry in
3424 **                  inquiry database.
3425 **
3426 ** Returns          void
3427 **
3428 *******************************************************************************/
btm_clear_all_pending_le_entry(void)3429 void btm_clear_all_pending_le_entry(void)
3430 {
3431     UINT16       xx;
3432     tINQ_DB_ENT  *p_ent = btm_cb.btm_inq_vars.inq_db;
3433 
3434     for (xx = 0; xx < BTM_INQ_DB_SIZE; xx++, p_ent++) {
3435         /* mark all pending LE entry as unused if an LE only device has scan response outstanding */
3436         if ((p_ent->in_use) &&
3437                 (p_ent->inq_info.results.device_type == BT_DEVICE_TYPE_BLE) &&
3438                 !p_ent->scan_rsp) {
3439             p_ent->in_use = FALSE;
3440         }
3441     }
3442 }
3443 
3444 /*******************************************************************************
3445 **
3446 ** Function         btm_send_sel_conn_callback
3447 **
3448 ** Description      send selection connection request callback.
3449 **
3450 ** Parameters
3451 **
3452 ** Returns          void
3453 **
3454 *******************************************************************************/
btm_send_sel_conn_callback(BD_ADDR remote_bda,UINT8 evt_type,UINT8 * p_data,UINT8 addr_type)3455 void btm_send_sel_conn_callback(BD_ADDR remote_bda, UINT8 evt_type, UINT8 *p_data, UINT8 addr_type)
3456 {
3457     UINT8   data_len, len;
3458     UINT8   *p_dev_name, remname[31] = {0};
3459     UNUSED(addr_type);
3460 
3461     if (btm_cb.ble_ctr_cb.p_select_cback == NULL ||
3462             /* non-connectable device */
3463             (evt_type != BTM_BLE_EVT_CONN_ADV && evt_type != BTM_BLE_EVT_CONN_DIR_ADV)) {
3464         return;
3465     }
3466 
3467     STREAM_TO_UINT8    (data_len, p_data);
3468 
3469     /* get the device name if exist in ADV data */
3470     if (data_len != 0) {
3471         p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_CMPL, &len);
3472 
3473         if (p_dev_name == NULL) {
3474             p_dev_name = BTM_CheckAdvData(p_data, BTM_BLE_AD_TYPE_NAME_SHORT, &len);
3475         }
3476 
3477         if (p_dev_name) {
3478             memcpy(remname, p_dev_name, len);
3479         }
3480     }
3481     /* allow connection */
3482     if ((* btm_cb.ble_ctr_cb.p_select_cback)(remote_bda, remname)) {
3483         /* terminate selective connection, initiate connection */
3484         btm_ble_initiate_select_conn(remote_bda);
3485     }
3486 }
3487 
btm_adv_pkt_handler(void * arg)3488 static void btm_adv_pkt_handler(void *arg)
3489 {
3490     UINT8   hci_evt_code, hci_evt_len;
3491     UINT8   ble_sub_code;
3492 
3493     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
3494     size_t pkts_to_process = pkt_queue_length(p_cb->adv_rpt_queue);
3495     if (pkts_to_process > BTM_BLE_GAP_ADV_RPT_BATCH_SIZE) {
3496         pkts_to_process = BTM_BLE_GAP_ADV_RPT_BATCH_SIZE;
3497     }
3498 
3499     for (size_t i = 0; i < pkts_to_process; i++) {
3500         pkt_linked_item_t *linked_pkt = pkt_queue_dequeue(p_cb->adv_rpt_queue);
3501         assert(linked_pkt != NULL);
3502         BT_HDR *packet = (BT_HDR *)linked_pkt->data;
3503         uint8_t *p = packet->data + packet->offset;
3504         STREAM_TO_UINT8  (hci_evt_code, p);
3505         STREAM_TO_UINT8  (hci_evt_len, p);
3506         STREAM_TO_UINT8  (ble_sub_code, p);
3507         if (ble_sub_code == HCI_BLE_ADV_PKT_RPT_EVT) {
3508             btm_ble_process_adv_pkt(p);
3509         } else if (ble_sub_code == HCI_BLE_ADV_DISCARD_REPORT_EVT) {
3510             btm_ble_process_adv_discard_evt(p);
3511         } else if (ble_sub_code == HCI_BLE_DIRECT_ADV_EVT) {
3512             btm_ble_process_direct_adv_pkt(p);
3513         } else {
3514             assert (0);
3515         }
3516 
3517         osi_free(linked_pkt);
3518 #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
3519         hci_adv_credits_try_release(1);
3520 #endif
3521     }
3522 
3523     if (pkt_queue_length(p_cb->adv_rpt_queue) != 0) {
3524         btu_task_post(SIG_BTU_HCI_ADV_RPT_MSG, NULL, OSI_THREAD_MAX_TIMEOUT);
3525     }
3526 
3527     UNUSED(hci_evt_code);
3528     UNUSED(hci_evt_len);
3529 }
3530 
3531 /*******************************************************************************
3532 **
3533 ** Function         btm_ble_process_adv_pkt
3534 **
3535 ** Description      This function is called when adv packet report events are
3536 **                  received from the device. It updates the inquiry database.
3537 **                  If the inquiry database is full, the oldest entry is discarded.
3538 **
3539 ** Parameters
3540 **
3541 ** Returns          void
3542 **
3543 *******************************************************************************/
btm_ble_process_adv_pkt(UINT8 * p_data)3544 void btm_ble_process_adv_pkt (UINT8 *p_data)
3545 {
3546     BD_ADDR             bda;
3547     UINT8               evt_type = 0, *p = p_data;
3548     UINT8               addr_type = 0;
3549     UINT8               num_reports;
3550     UINT8               data_len;
3551 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
3552     BOOLEAN             match = FALSE;
3553 #if (!CONTROLLER_RPA_LIST_ENABLE)
3554     BD_ADDR             temp_bda;
3555     UINT8               temp_addr_type = 0;
3556 #endif // (!CONTROLLER_RPA_LIST_ENABLE)
3557 #endif//(defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
3558 
3559     /* Only process the results if the inquiry is still active */
3560     if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
3561         return;
3562     }
3563 
3564     /* Extract the number of reports in this event. */
3565     STREAM_TO_UINT8(num_reports, p);
3566 
3567     while (num_reports--) {
3568         /* Extract inquiry results */
3569         STREAM_TO_UINT8    (evt_type, p);
3570         STREAM_TO_UINT8    (addr_type, p);
3571         STREAM_TO_BDADDR   (bda, p);
3572         //BTM_TRACE_ERROR("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x\n",
3573         //                              bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]);
3574 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
3575 
3576 #if (!CONTROLLER_RPA_LIST_ENABLE)
3577         temp_addr_type = addr_type;
3578         memcpy(temp_bda, bda, BD_ADDR_LEN);
3579 #endif // (!CONTROLLER_RPA_LIST_ENABLE)
3580 
3581         /* map address to security record */
3582         match = btm_identity_addr_to_random_pseudo(bda, &addr_type, FALSE);
3583 
3584         // BTM_TRACE_ERROR("btm_ble_process_adv_pkt:bda= %0x:%0x:%0x:%0x:%0x:%0x\n",
3585         //                             bda[0],bda[1],bda[2],bda[3],bda[4],bda[5]);
3586         /* always do RRA resolution on host */
3587         if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) {
3588             btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_adv, p_data);
3589         } else
3590 #endif
3591         btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
3592 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE && (!CONTROLLER_RPA_LIST_ENABLE))
3593         //save current adv addr information if p_dev_rec!= NULL
3594         tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
3595         if(p_dev_rec) {
3596             p_dev_rec->ble.current_addr_type = temp_addr_type;
3597             memcpy(p_dev_rec->ble.current_addr, temp_bda, BD_ADDR_LEN);
3598             p_dev_rec->ble.current_addr_valid = true;
3599         }
3600 #endif
3601         STREAM_TO_UINT8(data_len, p);
3602 
3603         /* Advance to the next event data_len + rssi byte */
3604         p += data_len + 1;
3605     }
3606 }
3607 
3608 /*******************************************************************************
3609 **
3610 ** Function         btm_ble_process_last_adv_pkt
3611 **
3612 ** Description      This function is called to report last adv packet
3613 **
3614 ** Parameters
3615 **
3616 ** Returns          void
3617 **
3618 *******************************************************************************/
3619 
btm_ble_process_last_adv_pkt(void)3620 static void btm_ble_process_last_adv_pkt(void)
3621 {
3622     UINT8       result = 0;
3623     UINT8       null_bda[6] = {0};
3624     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
3625     tBTM_INQ_RESULTS_CB  *p_inq_results_cb = p_inq->p_inq_results_cb;
3626     tBTM_INQ_RESULTS_CB  *p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
3627     tBTM_INQ_RESULTS_CB  *p_scan_results_cb = btm_cb.ble_ctr_cb.p_scan_results_cb;
3628     tBTM_BLE_INQ_CB      *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
3629     tINQ_DB_ENT          *p_i = btm_inq_db_find (p_le_inq_cb->adv_addr);
3630 
3631     if(memcmp(null_bda, p_le_inq_cb->adv_addr, BD_ADDR_LEN) == 0) {
3632         return;
3633     }
3634 
3635     if(p_i == NULL) {
3636         BTM_TRACE_DEBUG("no last adv");
3637         return;
3638     }
3639 
3640     if ((result = btm_ble_is_discoverable(p_le_inq_cb->adv_addr, p_i->inq_info.results.ble_evt_type, NULL)) == 0) {
3641         BTM_TRACE_WARNING("%s device is no longer discoverable so discarding advertising packet pkt",
3642                  __func__);
3643         return;
3644     }
3645     /* background connection in selective connection mode */
3646     if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
3647         //do nothing
3648     } else {
3649         if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {
3650             (p_inq_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
3651             p_le_inq_cb->adv_len = 0;
3652             memset(p_le_inq_cb->adv_addr, 0, BD_ADDR_LEN);
3653             p_i->inq_info.results.adv_data_len = 0;
3654             p_i->inq_info.results.scan_rsp_len = 0;
3655         }
3656         if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
3657             (p_obs_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
3658             p_le_inq_cb->adv_len = 0;
3659             memset(p_le_inq_cb->adv_addr, 0, BD_ADDR_LEN);
3660             p_i->inq_info.results.adv_data_len = 0;
3661             p_i->inq_info.results.scan_rsp_len = 0;
3662         }
3663         if (p_scan_results_cb && (result & BTM_BLE_DISCO_RESULT)) {
3664             (p_scan_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
3665             p_le_inq_cb->adv_len = 0;
3666             memset(p_le_inq_cb->adv_addr, 0, BD_ADDR_LEN);
3667             p_i->inq_info.results.adv_data_len = 0;
3668             p_i->inq_info.results.scan_rsp_len = 0;
3669         }
3670     }
3671 }
3672 
3673 /*******************************************************************************
3674 **
3675 ** Function         btm_ble_process_adv_pkt_cont
3676 **
3677 ** Description      This function is called after random address resolution is
3678 **                  done, and proceed to process adv packet.
3679 **
3680 ** Parameters
3681 **
3682 ** Returns          void
3683 **
3684 *******************************************************************************/
btm_ble_process_adv_pkt_cont(BD_ADDR bda,UINT8 addr_type,UINT8 evt_type,UINT8 * p)3685 static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, UINT8 addr_type, UINT8 evt_type, UINT8 *p)
3686 {
3687 
3688     tINQ_DB_ENT          *p_i;
3689     tBTM_INQUIRY_VAR_ST  *p_inq = &btm_cb.btm_inq_vars;
3690     tBTM_INQ_RESULTS_CB  *p_inq_results_cb = p_inq->p_inq_results_cb;
3691     tBTM_INQ_RESULTS_CB  *p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
3692     tBTM_INQ_RESULTS_CB  *p_scan_results_cb = btm_cb.ble_ctr_cb.p_scan_results_cb;
3693     tBTM_BLE_INQ_CB      *p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
3694     BOOLEAN     update = TRUE;
3695     UINT8       result = 0;
3696 
3697     /* Event_Type:
3698         0x00 Connectable undirected advertising (ADV_IND).
3699         0x01 Connectable directed advertising (ADV_DIRECT_IND)
3700         0x02 Scannable undirected advertising (ADV_SCAN_IND)
3701         0x03 Non connectable undirected advertising (ADV_NONCONN_IND)
3702         0x04 Scan Response (SCAN_RSP)
3703         0x05-0xFF Reserved for future use
3704     */
3705     // The adv packet without scan response is allowed to report to higher layer
3706     /*
3707     Bluedroid will put the advertising packet and scan response into a packet and send it to the higher layer.
3708     If two advertising packets are not with the same address, or can't be combined into a packet, then the first advertising
3709     packet will be discarded. So we added the following judgment:
3710     1. For different addresses, send the last advertising packet to higher layer
3711     2. For same address and same advertising type (not scan response), send the last advertising packet to higher layer
3712     3. For same address and scan response, do nothing
3713     */
3714     int same_addr = memcmp(bda, p_le_inq_cb->adv_addr, BD_ADDR_LEN);
3715     if (same_addr != 0 || (same_addr == 0 && evt_type != BTM_BLE_SCAN_RSP_EVT)) {
3716         btm_ble_process_last_adv_pkt();
3717     }
3718 
3719 
3720     p_i = btm_inq_db_find (bda);
3721 
3722     /* Check if this address has already been processed for this inquiry */
3723     if (btm_inq_find_bdaddr(bda)) {
3724         /* never been report as an LE device */
3725         if (p_i &&
3726                 (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
3727                  /* scan repsonse to be updated */
3728                  (!p_i->scan_rsp))) {
3729             update = TRUE;
3730         } else if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
3731             update = FALSE;
3732         } else {
3733             /* if yes, skip it */
3734             return; /* assumption: one result per event */
3735         }
3736     }
3737     /* If existing entry, use that, else get  a new one (possibly reusing the oldest) */
3738     if (p_i == NULL) {
3739         if ((p_i = btm_inq_db_new (bda)) != NULL) {
3740             p_inq->inq_cmpl_info.num_resp++;
3741         } else {
3742             return;
3743         }
3744     } else if (p_i->inq_count != p_inq->inq_counter) { /* first time seen in this inquiry */
3745         p_inq->inq_cmpl_info.num_resp++;
3746     }
3747     /* update the LE device information in inquiry database */
3748     if (!btm_ble_update_inq_result(bda, p_i, addr_type, evt_type, p)) {
3749         return;
3750     }
3751 
3752     if ((result = btm_ble_is_discoverable(bda, evt_type, p)) == 0) {
3753         BTM_TRACE_WARNING("%s device is no longer discoverable so discarding advertising packet pkt",
3754                  __func__);
3755         return;
3756     }
3757     if (!update) {
3758         result &= ~BTM_BLE_INQ_RESULT;
3759     }
3760     /* If the number of responses found and limited, issue a cancel inquiry */
3761     if (p_inq->inqparms.max_resps &&
3762             p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps) {
3763         /* new device */
3764         if (p_i == NULL ||
3765                 /* assume a DUMO device, BR/EDR inquiry is always active */
3766                 (p_i &&
3767                  (p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE &&
3768                  p_i->scan_rsp)) {
3769             BTM_TRACE_WARNING("INQ RES: Extra Response Received...cancelling inquiry..");
3770 
3771             /* if is non-periodic inquiry active, cancel now */
3772             if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
3773                     (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0) {
3774                 btsnd_hcic_inq_cancel();
3775             }
3776 
3777             btm_ble_stop_inquiry();
3778 
3779             btm_acl_update_busy_level (BTM_BLI_INQ_DONE_EVT);
3780         }
3781     }
3782     /* background connection in selective connection mode */
3783     if (btm_cb.ble_ctr_cb.bg_conn_type == BTM_BLE_CONN_SELECTIVE) {
3784         if (result & BTM_BLE_SEL_CONN_RESULT) {
3785             btm_send_sel_conn_callback(bda, evt_type, p, addr_type);
3786         } else {
3787             BTM_TRACE_DEBUG("None LE device, can not initiate selective connection\n");
3788         }
3789     } else {
3790         if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {
3791             (p_inq_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
3792             p_le_inq_cb->adv_len = 0;
3793             memset(p_le_inq_cb->adv_addr, 0, BD_ADDR_LEN);
3794             p_i->inq_info.results.adv_data_len = 0;
3795             p_i->inq_info.results.scan_rsp_len = 0;
3796         }
3797         if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
3798             (p_obs_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
3799             p_le_inq_cb->adv_len = 0;
3800             memset(p_le_inq_cb->adv_addr, 0, BD_ADDR_LEN);
3801             p_i->inq_info.results.adv_data_len = 0;
3802             p_i->inq_info.results.scan_rsp_len = 0;
3803         }
3804         if (p_scan_results_cb && (result & BTM_BLE_DISCO_RESULT)) {
3805             (p_scan_results_cb)((tBTM_INQ_RESULTS *) &p_i->inq_info.results, p_le_inq_cb->adv_data_cache);
3806             p_le_inq_cb->adv_len = 0;
3807             memset(p_le_inq_cb->adv_addr, 0, BD_ADDR_LEN);
3808             p_i->inq_info.results.adv_data_len = 0;
3809             p_i->inq_info.results.scan_rsp_len = 0;
3810         }
3811     }
3812 }
3813 
btm_ble_process_adv_discard_evt(UINT8 * p)3814 void btm_ble_process_adv_discard_evt(UINT8 *p)
3815 {
3816 #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
3817     uint32_t num_dis = 0;
3818     STREAM_TO_UINT32 (num_dis, p);
3819     tBTM_INQ_DIS_CB *p_obs_discard_cb = btm_cb.ble_ctr_cb.p_obs_discard_cb;
3820     if(p_obs_discard_cb) {
3821         (p_obs_discard_cb)(num_dis);
3822     }
3823 #endif
3824 }
3825 
btm_ble_process_direct_adv_pkt(UINT8 * p)3826 void btm_ble_process_direct_adv_pkt(UINT8 *p)
3827 {
3828     // TODO
3829 }
3830 
3831 /*******************************************************************************
3832 **
3833 ** Function         btm_ble_start_scan
3834 **
3835 ** Description      Start the BLE scan.
3836 **
3837 ** Returns          void
3838 **
3839 *******************************************************************************/
btm_ble_start_scan(void)3840 tBTM_STATUS btm_ble_start_scan(void)
3841 {
3842     tBTM_BLE_INQ_CB *p_inq = &btm_cb.ble_ctr_cb.inq_var;
3843     tBTM_STATUS status = BTM_CMD_STARTED;
3844 
3845     osi_mutex_lock(&scan_enable_lock, OSI_MUTEX_MAX_TIMEOUT);
3846 
3847     if(p_inq->scan_duplicate_filter > BTM_BLE_DUPLICATE_MAX) {
3848         p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
3849     }
3850     /* start scan, disable duplicate filtering */
3851     if (!btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, p_inq->scan_duplicate_filter)) {
3852         status = BTM_NO_RESOURCES;
3853     } else {
3854         osi_sem_take(&scan_enable_sem, OSI_SEM_MAX_TIMEOUT);
3855         if(scan_enable_status != BTM_SUCCESS) {
3856             status = BTM_NO_RESOURCES;
3857         }
3858         btm_cb.ble_ctr_cb.inq_var.state |= BTM_BLE_SCANNING;
3859         if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI) {
3860             btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
3861         } else {
3862             btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
3863         }
3864     }
3865     osi_mutex_unlock(&scan_enable_lock);
3866     return status;
3867 }
3868 
3869 /*******************************************************************************
3870 **
3871 ** Function         btm_ble_stop_scan
3872 **
3873 ** Description      Stop the BLE scan.
3874 **
3875 ** Returns          void
3876 **
3877 *******************************************************************************/
btm_ble_stop_scan(void)3878 void btm_ble_stop_scan(void)
3879 {
3880     BTM_TRACE_EVENT ("btm_ble_stop_scan ");
3881 
3882     /* Clear the inquiry callback if set */
3883     btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
3884     btm_cb.ble_ctr_cb.inq_var.state &= ~BTM_BLE_SCANNING;
3885     /* stop discovery now */
3886     btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
3887 
3888     btm_update_scanner_filter_policy(SP_ADV_ALL);
3889 
3890     btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_SCAN;
3891 }
3892 
3893 /*******************************************************************************
3894 **
3895 ** Function         btm_ble_stop_inquiry
3896 **
3897 ** Description      Stop the BLE Inquiry.
3898 **
3899 ** Returns          void
3900 **
3901 *******************************************************************************/
btm_ble_stop_inquiry(void)3902 void btm_ble_stop_inquiry(void)
3903 {
3904     tBTM_INQUIRY_VAR_ST *p_inq = &btm_cb.btm_inq_vars;
3905     tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
3906 
3907     btu_stop_timer (&p_ble_cb->inq_var.inq_timer_ent);
3908 
3909     p_ble_cb->scan_activity &=  ~BTM_BLE_INQUIRY_MASK;
3910 
3911     /* If no more scan activity, stop LE scan now */
3912     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
3913         btm_ble_stop_scan();
3914     } else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) ||
3915                (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
3916         BTM_TRACE_DEBUG("%s: setting default params for ongoing observe", __FUNCTION__);
3917         btm_ble_stop_scan();
3918         btm_ble_start_scan();
3919     }
3920 
3921     /* If we have a callback registered for inquiry complete, call it */
3922     BTM_TRACE_DEBUG ("BTM Inq Compl Callback: status 0x%02x, num results %d",
3923                      p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
3924 
3925     btm_process_inq_complete(HCI_SUCCESS, (UINT8)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
3926 }
3927 
3928 /*******************************************************************************
3929 **
3930 ** Function         btm_ble_stop_observe
3931 **
3932 ** Description      Stop the BLE Observe.
3933 **
3934 ** Returns          void
3935 **
3936 *******************************************************************************/
btm_ble_stop_observe(void)3937 static void btm_ble_stop_observe(void)
3938 {
3939     tBTM_BLE_CB *p_ble_cb = & btm_cb.ble_ctr_cb;
3940     tBTM_CMPL_CB *p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
3941 
3942     btu_stop_timer (&p_ble_cb->obs_timer_ent);
3943 
3944     p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
3945 
3946     p_ble_cb->p_obs_results_cb = NULL;
3947     p_ble_cb->p_obs_cmpl_cb = NULL;
3948 
3949     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
3950         btm_ble_stop_scan();
3951     }
3952 
3953     if (p_obs_cb) {
3954         (p_obs_cb)((tBTM_INQUIRY_CMPL *) &btm_cb.btm_inq_vars.inq_cmpl_info);
3955     }
3956 }
3957 
3958 /*******************************************************************************
3959 **
3960 ** Function         btm_ble_stop_observe
3961 **
3962 ** Description      Stop the BLE Observe.
3963 **
3964 ** Returns          void
3965 **
3966 *******************************************************************************/
btm_ble_stop_discover(void)3967 static void btm_ble_stop_discover(void)
3968 {
3969     tBTM_BLE_CB *p_ble_cb = & btm_cb.ble_ctr_cb;
3970     tBTM_CMPL_CB *p_scan_cb = p_ble_cb->p_scan_cmpl_cb;
3971     btu_stop_timer (&p_ble_cb->scan_timer_ent);
3972 
3973     osi_mutex_lock(&scan_enable_lock, OSI_MUTEX_MAX_TIMEOUT);
3974     p_ble_cb->scan_activity &= ~BTM_LE_DISCOVER_ACTIVE;
3975 
3976     p_ble_cb->p_scan_results_cb = NULL;
3977     p_ble_cb->p_scan_cmpl_cb = NULL;
3978 
3979     if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
3980         /* Clear the inquiry callback if set */
3981         btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
3982         btm_cb.ble_ctr_cb.inq_var.state &= ~BTM_BLE_SCANNING;
3983         /* stop discovery now */
3984         if(btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE)) {
3985             osi_sem_take(&scan_enable_sem, OSI_SEM_MAX_TIMEOUT);
3986         }
3987     }
3988 
3989     if (p_scan_cb) {
3990         (p_scan_cb)((tBTM_INQUIRY_CMPL *) &btm_cb.btm_inq_vars.inq_cmpl_info);
3991     }
3992     osi_mutex_unlock(&scan_enable_lock);
3993 }
3994 
3995 /*******************************************************************************
3996 **
3997 ** Function         btm_ble_adv_states_operation
3998 **
3999 ** Description      Set or clear adv states in topology mask
4000 **
4001 ** Returns          operation status. TRUE if sucessful, FALSE otherwise.
4002 **
4003 *******************************************************************************/
4004 typedef BOOLEAN (BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR * p_handler,UINT8 adv_evt)4005 static BOOLEAN btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR *p_handler, UINT8 adv_evt)
4006 {
4007     BOOLEAN rt = FALSE;
4008 
4009     switch (adv_evt) {
4010     case BTM_BLE_CONNECT_EVT:
4011         rt  = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
4012         break;
4013 
4014     case  BTM_BLE_NON_CONNECT_EVT:
4015         rt  = (*p_handler) (BTM_BLE_STATE_NON_CONN_ADV_BIT);
4016         break;
4017     case BTM_BLE_CONNECT_DIR_EVT:
4018         rt  =  (*p_handler) (BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
4019         break;
4020 
4021     case BTM_BLE_DISCOVER_EVT:
4022         rt  =  (*p_handler) (BTM_BLE_STATE_SCAN_ADV_BIT);
4023         break;
4024 
4025     case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
4026         rt = (*p_handler) (BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT);
4027         break;
4028 
4029     default:
4030         BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
4031         break;
4032     }
4033 
4034     return rt;
4035 }
4036 
4037 
4038 /*******************************************************************************
4039 **
4040 ** Function         btm_ble_start_adv
4041 **
4042 ** Description      start the BLE advertising.
4043 **
4044 ** Returns          void
4045 **
4046 *******************************************************************************/
btm_ble_start_adv(void)4047 tBTM_STATUS btm_ble_start_adv(void)
4048 {
4049     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
4050     tBTM_STATUS     rt = BTM_NO_RESOURCES;
4051     BTM_TRACE_EVENT ("btm_ble_start_adv\n");
4052 
4053 
4054     if (!btm_ble_adv_states_operation (btm_ble_topology_check, p_cb->evt_type)) {
4055         return BTM_WRONG_MODE;
4056     }
4057 
4058     osi_mutex_lock(&adv_enable_lock, OSI_MUTEX_MAX_TIMEOUT);
4059 
4060 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
4061     /* To relax resolving list,  always have resolving list enabled, unless directed adv */
4062     if (p_cb->evt_type != BTM_BLE_CONNECT_LO_DUTY_DIR_EVT &&
4063             p_cb->evt_type != BTM_BLE_CONNECT_DIR_EVT)
4064         /* enable resolving list is desired */
4065     {
4066         //btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_ADV);
4067     }
4068 #endif
4069     if (p_cb->afp != AP_SCAN_CONN_ALL) {
4070         //find the device in the btm dev buffer and write it to the controller white list
4071         btm_execute_wl_dev_operation();
4072         btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
4073     }
4074     /* The complete event comes up immediately after the 'btsnd_hcic_ble_set_adv_enable' being called in dual core,
4075     this causes the 'adv_mode' and 'state' not be set yet, so we set the state first */
4076     tBTM_BLE_GAP_STATE temp_state = p_cb->state;
4077     UINT8 adv_mode = p_cb->adv_mode;
4078     p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
4079     p_cb->state |= BTM_BLE_ADVERTISING;
4080     btm_ble_adv_states_operation(btm_ble_set_topology_mask, p_cb->evt_type);
4081     if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE)) {
4082         osi_sem_take(&adv_enable_sem, OSI_SEM_MAX_TIMEOUT);
4083         rt = adv_enable_status;
4084         BTM_TRACE_EVENT ("BTM_SUCCESS\n");
4085     } else {
4086         p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
4087         p_cb->state = temp_state;
4088         p_cb->adv_mode = adv_mode;
4089         btm_ble_adv_states_operation(btm_ble_clear_topology_mask, p_cb->evt_type);
4090         btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
4091     }
4092 
4093     if(adv_enable_status != HCI_SUCCESS) {
4094         p_cb->adv_mode = adv_mode;
4095     }
4096     osi_mutex_unlock(&adv_enable_lock);
4097     return rt;
4098 }
4099 
4100 /*******************************************************************************
4101 **
4102 ** Function         btm_ble_stop_adv
4103 **
4104 ** Description      Stop the BLE advertising.
4105 **
4106 ** Returns          void
4107 **
4108 *******************************************************************************/
btm_ble_stop_adv(void)4109 tBTM_STATUS btm_ble_stop_adv(void)
4110 {
4111     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
4112     tBTM_STATUS rt = BTM_SUCCESS;
4113     if (p_cb) {
4114         osi_mutex_lock(&adv_enable_lock, OSI_MUTEX_MAX_TIMEOUT);
4115         UINT8 temp_adv_mode = p_cb->adv_mode;
4116         BOOLEAN temp_fast_adv_on = p_cb->fast_adv_on;
4117         tBTM_BLE_GAP_STATE temp_state = p_cb->state;
4118         tBTM_BLE_WL_STATE temp_wl_state = btm_cb.ble_ctr_cb.wl_state;
4119         tBTM_BLE_STATE_MASK temp_mask = btm_ble_get_topology_mask ();
4120 
4121         p_cb->fast_adv_on = FALSE;
4122         p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
4123         p_cb->state &= ~BTM_BLE_ADVERTISING;
4124         btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
4125 
4126         /* clear all adv states */
4127         btm_ble_clear_topology_mask (BTM_BLE_STATE_ALL_ADV_MASK);
4128 
4129         if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE)) {
4130             osi_sem_take(&adv_enable_sem, OSI_SEM_MAX_TIMEOUT);
4131             rt = adv_enable_status;
4132         } else {
4133             // reset state
4134             p_cb->fast_adv_on = temp_fast_adv_on;
4135             p_cb->adv_mode = temp_adv_mode;
4136             p_cb->state = temp_state;
4137             btm_cb.ble_ctr_cb.wl_state = temp_wl_state;
4138             btm_ble_set_topology_mask (temp_mask);
4139 
4140             rt = BTM_NO_RESOURCES;
4141         }
4142         if(adv_enable_status != HCI_SUCCESS) {
4143             p_cb->adv_mode = temp_adv_mode;
4144         }
4145         osi_mutex_unlock(&adv_enable_lock);
4146     }
4147     return rt;
4148 }
4149 
btm_ble_set_random_addr(BD_ADDR random_bda)4150 tBTM_STATUS btm_ble_set_random_addr(BD_ADDR random_bda)
4151 {
4152     tBTM_STATUS rt = BTM_SUCCESS;
4153 
4154     osi_mutex_lock(&adv_enable_lock, OSI_MUTEX_MAX_TIMEOUT);
4155     osi_mutex_lock(&scan_enable_lock, OSI_MUTEX_MAX_TIMEOUT);
4156 
4157     if (btm_cb.ble_ctr_cb.inq_var.adv_mode == BTM_BLE_ADV_ENABLE) {
4158         if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE)) {
4159             osi_sem_take(&adv_enable_sem, OSI_SEM_MAX_TIMEOUT);
4160             rt = adv_enable_status;
4161         } else {
4162             rt = BTM_BAD_VALUE_RET;
4163         }
4164     }
4165 
4166     if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
4167         if (btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_DISABLE, BTM_BLE_SCAN_DUPLICATE_DISABLE)) {
4168             osi_sem_take(&scan_enable_sem, OSI_SEM_MAX_TIMEOUT);
4169             rt = scan_enable_status;
4170         } else {
4171             rt = BTM_BAD_VALUE_RET;
4172         }
4173     }
4174 
4175     if (rt == BTM_SUCCESS) {
4176         btsnd_hcic_ble_set_random_addr(random_bda);
4177     }
4178 
4179     if (btm_cb.ble_ctr_cb.inq_var.adv_mode == BTM_BLE_ADV_ENABLE) {
4180         if (btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE)) {
4181             osi_sem_take(&adv_enable_sem, OSI_SEM_MAX_TIMEOUT);
4182             rt = adv_enable_status;
4183         } else {
4184             rt = BTM_BAD_VALUE_RET;
4185         }
4186     }
4187 
4188     if (BTM_BLE_IS_DISCO_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
4189         if (btsnd_hcic_ble_set_scan_enable (BTM_BLE_SCAN_ENABLE, btm_cb.ble_ctr_cb.inq_var.scan_duplicate_filter)) {
4190             osi_sem_take(&scan_enable_sem, OSI_SEM_MAX_TIMEOUT);
4191             rt = scan_enable_status;
4192         } else {
4193             rt = BTM_BAD_VALUE_RET;
4194         }
4195     }
4196 
4197     osi_mutex_unlock(&adv_enable_lock);
4198     osi_mutex_unlock(&scan_enable_lock);
4199 
4200     return rt;
4201 }
4202 
4203 
4204 /*******************************************************************************
4205 **
4206 ** Function         btm_ble_start_slow_adv
4207 **
4208 ** Description      Restart adv with slow adv interval
4209 **
4210 ** Returns          void
4211 **
4212 *******************************************************************************/
btm_ble_start_slow_adv(void)4213 static void btm_ble_start_slow_adv (void)
4214 {
4215     tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
4216 
4217     if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
4218         tBTM_LE_RANDOM_CB *p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
4219         BD_ADDR p_addr_ptr = {0};
4220         tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
4221         tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
4222 
4223         btm_ble_stop_adv();
4224 
4225         p_cb->evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type,
4226                          &own_addr_type);
4227 
4228         /* slow adv mode never goes into directed adv */
4229         btsnd_hcic_ble_write_adv_params (BTM_BLE_GAP_ADV_SLOW_INT, BTM_BLE_GAP_ADV_SLOW_INT,
4230                                          p_cb->evt_type, own_addr_type,
4231                                          init_addr_type, p_addr_ptr,
4232                                          p_cb->adv_chnl_map, p_cb->afp);
4233 
4234         btm_ble_start_adv();
4235     }
4236 }
4237 /*******************************************************************************
4238 **
4239 ** Function         btm_ble_timeout
4240 **
4241 ** Description      Called when BTM BLE inquiry timer expires
4242 **
4243 ** Returns          void
4244 **
4245 *******************************************************************************/
btm_ble_timeout(TIMER_LIST_ENT * p_tle)4246 void btm_ble_timeout(TIMER_LIST_ENT *p_tle)
4247 {
4248     BTM_TRACE_EVENT ("btm_ble_timeout");
4249 
4250     switch (p_tle->event) {
4251     case BTU_TTYPE_BLE_OBSERVE:
4252         btm_ble_stop_observe();
4253         break;
4254     case BTU_TTYPE_BLE_SCAN:
4255         btm_ble_stop_discover();
4256         break;
4257     case BTU_TTYPE_BLE_INQUIRY:
4258         btm_ble_stop_inquiry();
4259         break;
4260 
4261     case BTU_TTYPE_BLE_GAP_LIM_DISC:
4262         /* lim_timeout expiried, limited discovery should exit now */
4263         btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
4264         btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, btm_cb.btm_inq_vars.discoverable_mode);
4265         break;
4266 
4267     case BTU_TTYPE_BLE_RANDOM_ADDR:
4268         if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
4269             if (NULL == (void *)(p_tle->param)) {
4270 #if (CONTROLLER_RPA_LIST_ENABLE == FALSE)
4271                 /* refresh the random addr */
4272                 btm_gen_resolvable_private_addr((void *)btm_gen_resolve_paddr_low);
4273 #endif
4274             } else {
4275                 if (BTM_BleMaxMultiAdvInstanceCount() > 0) {
4276                     btm_ble_multi_adv_configure_rpa((tBTM_BLE_MULTI_ADV_INST *)p_tle->param);
4277                 }
4278             }
4279         }
4280         break;
4281 
4282     case BTU_TTYPE_BLE_GAP_FAST_ADV:
4283         /* fast adv is completed, fall back to slow adv interval */
4284         btm_ble_start_slow_adv();
4285         break;
4286 
4287     default:
4288         break;
4289 
4290     }
4291 }
4292 
4293 
4294 /*******************************************************************************
4295 **
4296 ** Function         btm_ble_read_remote_features_complete
4297 **
4298 ** Description      This function is called when the command complete message
4299 **                  is received from the HCI for the read LE remote feature supported
4300 **                  complete event.
4301 **
4302 ** Returns          void
4303 **
4304 *******************************************************************************/
btm_ble_read_remote_features_complete(UINT8 * p)4305 void btm_ble_read_remote_features_complete(UINT8 *p)
4306 {
4307     tACL_CONN        *p_acl_cb = NULL;
4308     UINT16            handle;
4309     UINT8             status;
4310 
4311     BTM_TRACE_EVENT ("btm_ble_read_remote_features_complete ");
4312 
4313     STREAM_TO_UINT8(status, p);
4314 
4315     // if LE read remote feature failed for HCI_ERR_CONN_FAILED_ESTABLISHMENT,
4316     // expect disconnect complete to be received
4317     if (status != HCI_ERR_CONN_FAILED_ESTABLISHMENT) {
4318         STREAM_TO_UINT16 (handle, p);
4319 
4320         /* Look up the connection by handle and copy features */
4321         p_acl_cb = btm_handle_to_acl(handle);
4322 	if (p_acl_cb) {
4323 	    {
4324                 STREAM_TO_ARRAY(p_acl_cb->peer_le_features, p, BD_FEATURES_LEN);
4325 #if BLE_INCLUDED == TRUE
4326                 /* In the original Bluedroid version, slave need to send LL_VERSION_IND(call btsnd_hcic_rmt_ver_req)
4327                  * to remote device if it has not received ll_version_ind.
4328                  * Delete it to resolve Android 7.0 incompatible problem. But it may cause that slave host
4329                  * can't get remote device's version.*/
4330                 if (p_acl_cb->link_role == HCI_ROLE_MASTER){
4331                     btsnd_hcic_rmt_ver_req (p_acl_cb->hci_handle);
4332                 }
4333                 else{
4334                     uint16_t data_length = controller_get_interface()->get_ble_default_data_packet_length();
4335                     uint16_t data_txtime = controller_get_interface()->get_ble_default_data_packet_txtime();
4336                     if (p_acl_cb->transport == BT_TRANSPORT_LE) {
4337                         if (HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl_cb->peer_le_features) &&
4338                             (p_acl_cb->data_length_params.tx_len != data_length)) {
4339                             p_acl_cb->data_len_updating = true;
4340                             btsnd_hcic_ble_set_data_length(p_acl_cb->hci_handle, data_length, data_txtime);
4341                         }
4342                         l2cble_notify_le_connection (p_acl_cb->remote_addr);
4343                     }
4344                 }
4345 #endif
4346             }
4347         }
4348     }
4349 
4350 }
4351 
4352 /*******************************************************************************
4353 **
4354 ** Function         btm_ble_write_adv_enable_complete
4355 **
4356 ** Description      This function process the write adv enable command complete.
4357 **
4358 ** Returns          void
4359 **
4360 *******************************************************************************/
btm_ble_write_adv_enable_complete(UINT8 * p)4361 void btm_ble_write_adv_enable_complete(UINT8 *p)
4362 {
4363     /* if write adv enable/disbale not succeed */
4364     if (*p != HCI_SUCCESS) {
4365         BTM_TRACE_ERROR("%s failed", __func__);
4366     }
4367 }
4368 
4369 /*******************************************************************************
4370 **
4371 ** Function         btm_ble_dir_adv_tout
4372 **
4373 ** Description      when directed adv time out
4374 **
4375 ** Returns          void
4376 **
4377 *******************************************************************************/
btm_ble_dir_adv_tout(void)4378 void btm_ble_dir_adv_tout(void)
4379 {
4380     btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
4381 
4382     /* make device fall back into undirected adv mode by default */
4383     btm_cb.ble_ctr_cb.inq_var.directed_conn = FALSE;
4384 }
4385 
4386 /*******************************************************************************
4387 **
4388 ** Function         btm_ble_set_topology_mask
4389 **
4390 ** Description      set BLE topology mask
4391 **
4392 ** Returns          TRUE is request is allowed, FALSE otherwise.
4393 **
4394 *******************************************************************************/
btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)4395 BOOLEAN btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)
4396 {
4397     request_state_mask &= BTM_BLE_STATE_ALL_MASK;
4398     btm_cb.ble_ctr_cb.cur_states |= (request_state_mask & BTM_BLE_STATE_ALL_MASK);
4399     return TRUE;
4400 }
4401 
4402 /*******************************************************************************
4403 **
4404 ** Function         btm_ble_clear_topology_mask
4405 **
4406 ** Description      Clear BLE topology bit mask
4407 **
4408 ** Returns          TRUE is request is allowed, FALSE otherwise.
4409 **
4410 *******************************************************************************/
btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask)4411 BOOLEAN btm_ble_clear_topology_mask (tBTM_BLE_STATE_MASK request_state_mask)
4412 {
4413     request_state_mask &= BTM_BLE_STATE_ALL_MASK;
4414     btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
4415     return TRUE;
4416 }
4417 
4418 /*******************************************************************************
4419 **
4420 ** Function         btm_ble_get_topology_mask
4421 **
4422 ** Description      Get BLE topology bit mask
4423 **
4424 ** Returns          state mask.
4425 **
4426 *******************************************************************************/
btm_ble_get_topology_mask(void)4427 tBTM_BLE_STATE_MASK btm_ble_get_topology_mask (void)
4428 {
4429     return btm_cb.ble_ctr_cb.cur_states;
4430 }
4431 
4432 /*******************************************************************************
4433 **
4434 ** Function         btm_ble_update_link_topology_mask
4435 **
4436 ** Description      This function update the link topology mask
4437 **
4438 ** Returns          void
4439 **
4440 *******************************************************************************/
btm_ble_update_link_topology_mask(UINT8 link_role,BOOLEAN increase)4441 void btm_ble_update_link_topology_mask(UINT8 link_role, BOOLEAN increase)
4442 {
4443     btm_ble_clear_topology_mask (BTM_BLE_STATE_ALL_CONN_MASK);
4444 
4445     if (increase) {
4446         btm_cb.ble_ctr_cb.link_count[link_role]++;
4447     } else if (btm_cb.ble_ctr_cb.link_count[link_role] > 0) {
4448         btm_cb.ble_ctr_cb.link_count[link_role]--;
4449     }
4450 
4451     if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_MASTER]) {
4452         btm_ble_set_topology_mask (BTM_BLE_STATE_MASTER_BIT);
4453     }
4454 
4455     if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_SLAVE]) {
4456         btm_ble_set_topology_mask(BTM_BLE_STATE_SLAVE_BIT);
4457     }
4458 
4459     if (link_role == HCI_ROLE_SLAVE && increase) {
4460         btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
4461         /* make device fall back into undirected adv mode by default */
4462         btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
4463         /* clear all adv states */
4464         btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
4465     }
4466 }
4467 
4468 /*******************************************************************************
4469 **
4470 ** Function         btm_ble_update_mode_operation
4471 **
4472 ** Description      This function update the GAP role operation when a link status
4473 **                  is updated.
4474 **
4475 ** Returns          void
4476 **
4477 *******************************************************************************/
btm_ble_update_mode_operation(UINT8 link_role,BD_ADDR bd_addr,UINT8 status)4478 BOOLEAN btm_ble_update_mode_operation(UINT8 link_role, BD_ADDR bd_addr, UINT8 status)
4479 {
4480     BOOLEAN bg_con = FALSE;
4481     if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
4482         btm_cb.ble_ctr_cb.inq_var.adv_mode  = BTM_BLE_ADV_DISABLE;
4483         /* make device fall back into undirected adv mode by default */
4484         btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
4485         /* clear all adv states */
4486         btm_ble_clear_topology_mask (BTM_BLE_STATE_ALL_ADV_MASK);
4487     }
4488 
4489     if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE) {
4490         btm_ble_set_connectability(btm_cb.btm_inq_vars.connectable_mode |
4491                                    btm_cb.ble_ctr_cb.inq_var.connectable_mode);
4492     }
4493 
4494     /* when no connection is attempted, and controller is not rejecting last request
4495        due to resource limitation, start next direct connection or background connection
4496        now in order */
4497     if (btm_ble_get_conn_st() == BLE_CONN_IDLE && status != HCI_ERR_HOST_REJECT_RESOURCES &&
4498             !btm_send_pending_direct_conn()) {
4499         bg_con = btm_ble_resume_bg_conn();
4500     }
4501 
4502     return bg_con;
4503 }
4504 
4505 /*******************************************************************************
4506 **
4507 ** Function         btm_ble_init
4508 **
4509 ** Description      Initialize the control block variable values.
4510 **
4511 ** Returns          void
4512 **
4513 *******************************************************************************/
btm_ble_init(void)4514 void btm_ble_init (void)
4515 {
4516     BTM_TRACE_DEBUG("%s", __func__);
4517 
4518 #if BTM_DYNAMIC_MEMORY == TRUE
4519     cmn_ble_gap_vsc_cb_ptr = (tBTM_BLE_VSC_CB *)osi_malloc(sizeof(tBTM_BLE_VSC_CB));
4520     if (cmn_ble_gap_vsc_cb_ptr == NULL) {
4521         BTM_TRACE_ERROR("%s malloc failed", __func__);
4522         return;
4523     }
4524 #endif
4525 
4526     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
4527 
4528     btu_free_timer(&p_cb->obs_timer_ent);
4529     btu_free_timer(&p_cb->scan_timer_ent);
4530     btu_free_timer(&p_cb->inq_var.fast_adv_timer);
4531     memset(p_cb, 0, sizeof(tBTM_BLE_CB));
4532     memset(&(btm_cb.cmn_ble_vsc_cb), 0 , sizeof(tBTM_BLE_VSC_CB));
4533     btm_cb.cmn_ble_vsc_cb.values_read = FALSE;
4534     p_cb->cur_states       = 0;
4535 
4536     p_cb->conn_pending_q = fixed_queue_new(QUEUE_SIZE_MAX);
4537 
4538     p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
4539     p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
4540     p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
4541     p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
4542     p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
4543     p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
4544     p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
4545 
4546     /* for background connection, reset connection params to be undefined */
4547     p_cb->scan_int = p_cb->scan_win = BTM_BLE_SCAN_PARAM_UNDEF;
4548 
4549     p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
4550 
4551     p_cb->adv_rpt_queue = pkt_queue_create();
4552     assert(p_cb->adv_rpt_queue != NULL);
4553 
4554     p_cb->adv_rpt_ready = osi_event_create(btm_adv_pkt_handler, NULL);
4555     assert(p_cb->adv_rpt_ready != NULL);
4556     osi_event_bind(p_cb->adv_rpt_ready, btu_get_current_thread(), 0);
4557 
4558 #if BLE_VND_INCLUDED == FALSE
4559     btm_ble_adv_filter_init();
4560 #endif
4561 }
4562 
4563 /*******************************************************************************
4564 **
4565 ** Function         btm_ble_free
4566 **
4567 ** Description      free the control block variable values.
4568 **
4569 ** Returns          void
4570 **
4571 *******************************************************************************/
btm_ble_free(void)4572 void btm_ble_free (void)
4573 {
4574     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
4575 
4576     BTM_TRACE_DEBUG("%s", __func__);
4577 
4578     fixed_queue_free(p_cb->conn_pending_q, osi_free_func);
4579 
4580     pkt_queue_destroy(p_cb->adv_rpt_queue, NULL);
4581     p_cb->adv_rpt_queue = NULL;
4582 
4583     osi_event_delete(p_cb->adv_rpt_ready);
4584     p_cb->adv_rpt_ready = NULL;
4585 
4586 #if BTM_DYNAMIC_MEMORY == TRUE
4587     osi_free(cmn_ble_gap_vsc_cb_ptr);
4588     cmn_ble_gap_vsc_cb_ptr = NULL;
4589 #endif
4590 }
4591 
4592 /*******************************************************************************
4593 **
4594 ** Function         btm_ble_topology_check
4595 **
4596 ** Description      check to see requested state is supported. One state check at
4597 **                  a time is supported
4598 **
4599 ** Returns          TRUE is request is allowed, FALSE otherwise.
4600 **
4601 *******************************************************************************/
btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask)4602 BOOLEAN btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask)
4603 {
4604     BOOLEAN rt = FALSE;
4605 
4606     UINT8   state_offset = 0;
4607     UINT16  cur_states = btm_cb.ble_ctr_cb.cur_states;
4608     UINT8   mask, offset;
4609     UINT8   request_state = 0;
4610 
4611     /* check only one bit is set and within valid range */
4612     if (request_state_mask == BTM_BLE_STATE_INVALID ||
4613             request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
4614             (request_state_mask & (request_state_mask - 1 )) != 0) {
4615         BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
4616         return rt;
4617     }
4618 
4619     while (request_state_mask) {
4620         request_state_mask >>= 1;
4621         request_state ++;
4622     }
4623 
4624     /* check if the requested state is supported or not */
4625     mask = btm_le_state_combo_tbl[0][request_state - 1][0];
4626     offset = btm_le_state_combo_tbl[0][request_state - 1][1];
4627 
4628     const uint8_t *ble_supported_states = controller_get_interface()->get_ble_supported_states();
4629 
4630     if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, mask, offset)) {
4631         BTM_TRACE_ERROR("state requested not supported: %d", request_state);
4632         return rt;
4633     }
4634 
4635     rt = TRUE;
4636     /* make sure currently active states are all supported in conjunction with the requested
4637        state. If the bit in table is not set, the combination is not supported */
4638     while (cur_states != 0) {
4639         if (cur_states & 0x01) {
4640             mask = btm_le_state_combo_tbl[request_state][state_offset][0];
4641             offset = btm_le_state_combo_tbl[request_state][state_offset][1];
4642 
4643             if (mask != 0 && offset != 0) {
4644                 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, mask, offset)) {
4645                     rt = FALSE;
4646                     break;
4647                 }
4648             }
4649         }
4650         cur_states >>= 1;
4651         state_offset ++;
4652     }
4653     return rt;
4654 }
4655 
4656 /*******************************************************************************
4657  **
4658  ** Function         BTM_Ble_Authorization
4659  **
4660  ** Description      This function is used to authorize a specified device
4661  **
4662  ** Returns          TRUE or FALSE
4663  **
4664  *******************************************************************************/
BTM_Ble_Authorization(BD_ADDR bd_addr,BOOLEAN authorize)4665 BOOLEAN BTM_Ble_Authorization(BD_ADDR bd_addr, BOOLEAN authorize)
4666 {
4667     if (bd_addr == NULL) {
4668         BTM_TRACE_ERROR("bd_addr is NULL");
4669         return FALSE;
4670     }
4671 
4672     if (btm_sec_dev_authorization(bd_addr, authorize)) {
4673         return TRUE;
4674     }
4675 
4676     BTM_TRACE_ERROR("Authorization fail");
4677     return FALSE;
4678 }
4679 
btm_ble_adv_pkt_ready(void)4680 bool btm_ble_adv_pkt_ready(void)
4681 {
4682     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
4683     osi_thread_post_event(p_cb->adv_rpt_ready, OSI_THREAD_MAX_TIMEOUT);
4684 
4685     return true;
4686 }
4687 
btm_ble_adv_pkt_post(pkt_linked_item_t * pkt)4688 bool btm_ble_adv_pkt_post(pkt_linked_item_t *pkt)
4689 {
4690     if (pkt == NULL) {
4691         return false;
4692     }
4693 
4694     tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
4695     pkt_queue_enqueue(p_cb->adv_rpt_queue, pkt);
4696     return true;
4697 }
4698 #endif  /* BLE_INCLUDED */
4699