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