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