blob: 8a8627e9f97316a842d0a436db070a681797d5fd [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Wei Wanga6ce7752014-05-20 06:30:32 +00003 * Copyright (C) 2008-2014 Broadcom Corporation
The Android Open Source Project5738f832012-12-12 16:00:35 -08004 *
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
Marie Janssen49a86702015-07-08 11:48:57 -070025#define LOG_TAG "bt_btm_ble"
26
The Android Open Source Project5738f832012-12-12 16:00:35 -080027#include <stddef.h>
Myles Watson911d1ae2016-11-28 16:44:40 -080028#include <stdio.h>
29#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080030
31#include "bt_types.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080032#include "bt_utils.h"
Wei Wang21015a42014-10-03 10:58:03 -070033#include "btm_ble_api.h"
Marie Janssen49a86702015-07-08 11:48:57 -070034#include "btm_int.h"
Wei Wang21015a42014-10-03 10:58:03 -070035#include "btu.h"
Chris Manton79ecab52014-10-31 14:54:51 -070036#include "device/include/controller.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include "gap_api.h"
Marie Janssen49a86702015-07-08 11:48:57 -070038#include "hcimsgs.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070039#include "osi/include/osi.h"
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -070040
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070041#include "btm_ble_int.h"
Marie Janssen49a86702015-07-08 11:48:57 -070042#include "gatt_int.h"
43#include "gattdefs.h"
44#include "l2c_int.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080045#include "osi/include/log.h"
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070046
Myles Watson911d1ae2016-11-28 16:44:40 -080047#define BTM_BLE_NAME_SHORT 0x01
48#define BTM_BLE_NAME_CMPL 0x02
The Android Open Source Project5738f832012-12-12 16:00:35 -080049
Myles Watson911d1ae2016-11-28 16:44:40 -080050#define BTM_BLE_FILTER_TARGET_UNKNOWN 0xff
51#define BTM_BLE_POLICY_UNKNOWN 0xff
The Android Open Source Project5738f832012-12-12 16:00:35 -080052
Myles Watson911d1ae2016-11-28 16:44:40 -080053#define BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS (30 * 1000)
54#define MIN_ADV_LENGTH 2
Satya Calloji935324a2015-03-31 13:24:32 -070055#define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE 9
56
Myles Watson911d1ae2016-11-28 16:44:40 -080057extern fixed_queue_t* btu_general_alarm_queue;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080058
Marie Janssend19e0782016-07-15 12:48:27 -070059#if (BLE_VND_INCLUDED == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -080060static tBTM_BLE_CTRL_FEATURES_CBACK* p_ctrl_le_feature_rd_cmpl_cback = NULL;
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +010061#endif
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -070062
The Android Open Source Project5738f832012-12-12 16:00:35 -080063/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080064 * Local functions
65 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -070066static void btm_ble_update_adv_flag(uint8_t flag);
Myles Watson911d1ae2016-11-28 16:44:40 -080067static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, uint8_t addr_type,
Jakub Pawlowski9e300562016-12-07 10:54:44 -080068 uint8_t evt_type, uint8_t data_len,
69 uint8_t* data, int8_t rssi);
Myles Watson911d1ae2016-11-28 16:44:40 -080070static uint8_t btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB* p_cb,
71 BD_ADDR_PTR p_peer_addr_ptr,
72 tBLE_ADDR_TYPE* p_peer_addr_type,
73 tBLE_ADDR_TYPE* p_own_addr_type);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070074static void btm_ble_stop_observe(void);
Myles Watson911d1ae2016-11-28 16:44:40 -080075static void btm_ble_fast_adv_timer_timeout(void* data);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080076static void btm_ble_start_slow_adv(void);
Myles Watson911d1ae2016-11-28 16:44:40 -080077static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(void* data);
78static void btm_ble_inquiry_timer_timeout(void* data);
79static void btm_ble_observer_timer_timeout(void* data);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080080
Myles Watson911d1ae2016-11-28 16:44:40 -080081#define BTM_BLE_INQ_RESULT 0x01
82#define BTM_BLE_OBS_RESULT 0x02
The Android Open Source Project5738f832012-12-12 16:00:35 -080083
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070084/* LE states combo bit to check */
Marie Janssend19e0782016-07-15 12:48:27 -070085const uint8_t btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] =
Myles Watson911d1ae2016-11-28 16:44:40 -080086 {{
87 /* single state support */
88 {HCI_SUPP_LE_STATES_CONN_ADV_MASK,
89 HCI_SUPP_LE_STATES_CONN_ADV_OFF}, /* conn_adv */
90 {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* init */
91 {HCI_SUPP_LE_STATES_INIT_MASK,
92 HCI_SUPP_LE_STATES_INIT_OFF}, /* master */
93 {HCI_SUPP_LE_STATES_SLAVE_MASK,
94 HCI_SUPP_LE_STATES_SLAVE_OFF}, /* slave */
95 {0, 0}, /* todo: lo du dir adv, not covered ? */
96 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASK,
97 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_OFF}, /* hi duty dir adv */
98 {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASK,
99 HCI_SUPP_LE_STATES_NON_CONN_ADV_OFF}, /* non connectable adv */
100 {HCI_SUPP_LE_STATES_PASS_SCAN_MASK,
101 HCI_SUPP_LE_STATES_PASS_SCAN_OFF}, /* passive scan */
102 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASK,
103 HCI_SUPP_LE_STATES_ACTIVE_SCAN_OFF}, /* active scan */
104 {HCI_SUPP_LE_STATES_SCAN_ADV_MASK,
105 HCI_SUPP_LE_STATESSCAN_ADV_OFF} /* scanable adv */
106 },
107 {
108 /* conn_adv =0 */
109 {0, 0}, /* conn_adv */
110 {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK,
111 HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF}, /* init: 32 */
112 {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK,
113 HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF}, /* master: 35 */
114 {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK,
115 HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/
116 {0, 0}, /* lo du dir adv */
117 {0, 0}, /* hi duty dir adv */
118 {0, 0}, /* non connectable adv */
119 {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK,
120 HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* passive scan */
121 {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK,
122 HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* active scan */
123 {0, 0} /* scanable adv */
124 },
125 {
126 /* init */
127 {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK,
128 HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF}, /* conn_adv: 32 */
129 {0, 0}, /* init */
130 {HCI_SUPP_LE_STATES_INIT_MASTER_MASK,
131 HCI_SUPP_LE_STATES_INIT_MASTER_OFF}, /* master 28 */
132 {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK,
133 HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* slave 41 */
134 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK,
135 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,
137 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* hi duty dir adv 33 */
138 {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK,
139 HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* non connectable adv */
140 {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK,
141 HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* passive scan */
142 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK,
143 HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* active scan */
144 {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK,
145 HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF} /* scanable adv */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700146
Myles Watson911d1ae2016-11-28 16:44:40 -0800147 },
148 {
149 /* master */
150 {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK,
151 HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF}, /* conn_adv: 35 */
152 {HCI_SUPP_LE_STATES_INIT_MASTER_MASK,
153 HCI_SUPP_LE_STATES_INIT_MASTER_OFF}, /* init 28 */
154 {HCI_SUPP_LE_STATES_INIT_MASTER_MASK,
155 HCI_SUPP_LE_STATES_INIT_MASTER_OFF}, /* master 28 */
156 {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK,
157 HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF}, /* slave: 32 */
158 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK,
159 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* lo duty cycle adv
160 37 */
161 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK,
162 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* hi duty cycle adv
163 36 */
164 {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK,
165 HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* non connectable adv
166 */
167 {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK,
168 HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* passive scan */
169 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK,
170 HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* active scan */
171 {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK,
172 HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF} /* scanable adv */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700173
Myles Watson911d1ae2016-11-28 16:44:40 -0800174 },
175 {
176 /* slave */
177 {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK,
178 HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* conn_adv: 38,*/
179 {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK,
180 HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* init 41 */
181 {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK,
182 HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* master 41 */
183 {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK,
184 HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/
185 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK,
186 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* lo duty cycle adv 40
187 */
188 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK,
189 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF}, /* hi duty cycle adv 39
190 */
191 {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK,
192 HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF}, /* non connectable adv */
193 {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK,
194 HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF}, /* passive scan */
195 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK,
196 HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF}, /* active scan */
197 {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK,
198 HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF} /* scanable adv */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700199
Myles Watson911d1ae2016-11-28 16:44:40 -0800200 },
201 {
202 /* lo duty cycle adv */
203 {0, 0}, /* conn_adv: 38,*/
204 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK,
205 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF}, /* init 34 */
206 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK,
207 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* master 37 */
208 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK,
209 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 40 */
210 {0, 0}, /* lo duty cycle adv 40 */
211 {0, 0}, /* hi duty cycle adv 39 */
212 {0, 0}, /* non connectable adv */
213 {0, 0}, /* TODO: passive scan, not covered? */
214 {0, 0}, /* TODO: active scan, not covered? */
215 {0, 0} /* scanable adv */
216 },
217 {
218 /* hi duty cycle adv */
219 {0, 0}, /* conn_adv: 38,*/
220 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK,
221 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* init 33 */
222 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK,
223 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* master 36 */
224 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK,
225 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 39*/
226 {0, 0}, /* lo duty cycle adv 40 */
227 {0, 0}, /* hi duty cycle adv 39 */
228 {0, 0}, /* non connectable adv */
229 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK,
230 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF}, /* passive scan */
231 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK,
232 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF}, /* active scan */
233 {0, 0} /* scanable adv */
234 },
235 {
236 /* non connectable adv */
237 {0, 0}, /* conn_adv: */
238 {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK,
239 HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* init */
240 {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK,
241 HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* master */
242 {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK,
243 HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF}, /* slave: */
244 {0, 0}, /* lo duty cycle adv */
245 {0, 0}, /* hi duty cycle adv */
246 {0, 0}, /* non connectable adv */
247 {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK,
248 HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF}, /* passive scan */
249 {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK,
250 HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF}, /* active scan */
251 {0, 0} /* scanable adv */
252 },
253 {
254 /* passive scan */
255 {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK,
256 HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* conn_adv: */
257 {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK,
258 HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* init */
259 {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK,
260 HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* master */
261 {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK,
262 HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF}, /* slave: */
263 {0, 0}, /* lo duty cycle adv */
264 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK,
265 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF}, /* hi duty cycle
266 adv */
267 {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK,
268 HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF}, /* non connectable
269 adv */
270 {0, 0}, /* passive scan */
271 {0, 0}, /* active scan */
272 {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK,
273 HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF} /* scanable adv */
274 },
275 {
276 /* active scan */
277 {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK,
278 HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* conn_adv: */
279 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK,
280 HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* init */
281 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK,
282 HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* master */
283 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK,
284 HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF}, /* slave: */
285 {0, 0}, /* lo duty cycle adv */
286 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK,
287 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF}, /* hi duty
288 cycle adv
289 */
290 {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK,
291 HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF}, /* non
292 connectable
293 adv */
294 {0, 0}, /* TODO: passive scan */
295 {0, 0}, /* TODO: active scan */
296 {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK,
297 HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF} /* scanable adv */
298 },
299 {
300 /* scanable adv */
301 {0, 0}, /* conn_adv: */
302 {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK,
303 HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}, /* init */
304 {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK,
305 HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}, /* master */
306 {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK,
307 HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF}, /* slave: */
308 {0, 0}, /* lo duty cycle adv */
309 {0, 0}, /* hi duty cycle adv */
310 {0, 0}, /* non connectable adv */
311 {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK,
312 HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF}, /* passive scan */
313 {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK,
314 HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF}, /* active scan */
315 {0, 0} /* scanable adv */
316 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700317
318};
319/* check LE combo state supported */
Myles Watson911d1ae2016-11-28 16:44:40 -0800320#define BTM_LE_STATES_SUPPORTED(x, y, z) ((x)[(z)] & (y))
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700321
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800322/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800323 *
324 * Function BTM_BleUpdateAdvFilterPolicy
325 *
326 * Description This function update the filter policy of advertiser.
327 *
328 * Parameter adv_policy: advertising filter policy
329 *
330 * Return void
331 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800332void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) {
333 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
334 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
335 BD_ADDR p_addr_ptr = {0};
336 uint8_t adv_mode = p_cb->adv_mode;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800337
Myles Watson911d1ae2016-11-28 16:44:40 -0800338 BTM_TRACE_EVENT("BTM_BleUpdateAdvFilterPolicy");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800339
Myles Watson911d1ae2016-11-28 16:44:40 -0800340 if (!controller_get_interface()->supports_ble()) return;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700341
Myles Watson911d1ae2016-11-28 16:44:40 -0800342 if (p_cb->afp != adv_policy) {
343 p_cb->afp = adv_policy;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800344
Myles Watson911d1ae2016-11-28 16:44:40 -0800345 /* if adv active, stop and restart */
346 btm_ble_stop_adv();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800347
Myles Watson911d1ae2016-11-28 16:44:40 -0800348 if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
349 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
350 p_cb, p_addr_ptr, &init_addr_type, &p_cb->adv_addr_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800351
Myles Watson911d1ae2016-11-28 16:44:40 -0800352 btsnd_hcic_ble_write_adv_params(
353 (uint16_t)(p_cb->adv_interval_min ? p_cb->adv_interval_min
354 : BTM_BLE_GAP_ADV_SLOW_INT),
355 (uint16_t)(p_cb->adv_interval_max ? p_cb->adv_interval_max
356 : BTM_BLE_GAP_ADV_SLOW_INT),
357 p_cb->evt_type, p_cb->adv_addr_type, init_addr_type, p_addr_ptr,
358 p_cb->adv_chnl_map, p_cb->afp);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800359
Myles Watson911d1ae2016-11-28 16:44:40 -0800360 if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
361 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800362}
Satya Calloji935324a2015-03-31 13:24:32 -0700363
364/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800365 *
366 * Function btm_ble_send_extended_scan_params
367 *
Myles Watson9ca07092016-11-28 16:41:53 -0800368 * Description This function sends out the extended scan parameters command
369 * to the controller
Myles Watsonee96a3c2016-11-23 14:49:54 -0800370 *
371 * Parameters scan_type - Scan type
372 * scan_int - Scan interval
373 * scan_win - Scan window
374 * addr_type_own - Own address type
375 * scan_filter_policy - Scan filter policy
376 *
377 ******************************************************************************/
Jakub Pawlowski3d5fbca2016-10-10 12:36:49 -0700378void btm_ble_send_extended_scan_params(uint8_t scan_type, uint32_t scan_int,
Myles Watson911d1ae2016-11-28 16:44:40 -0800379 uint32_t scan_win, uint8_t addr_type_own,
380 uint8_t scan_filter_policy) {
381 uint8_t scan_param[HCIC_PARAM_SIZE_BLE_WRITE_EXTENDED_SCAN_PARAM];
382 uint8_t* pp_scan = scan_param;
Satya Calloji935324a2015-03-31 13:24:32 -0700383
Myles Watson911d1ae2016-11-28 16:44:40 -0800384 memset(scan_param, 0, HCIC_PARAM_SIZE_BLE_WRITE_EXTENDED_SCAN_PARAM);
Satya Calloji935324a2015-03-31 13:24:32 -0700385
Myles Watson911d1ae2016-11-28 16:44:40 -0800386 UINT8_TO_STREAM(pp_scan, scan_type);
387 UINT32_TO_STREAM(pp_scan, scan_int);
388 UINT32_TO_STREAM(pp_scan, scan_win);
389 UINT8_TO_STREAM(pp_scan, addr_type_own);
390 UINT8_TO_STREAM(pp_scan, scan_filter_policy);
Satya Calloji935324a2015-03-31 13:24:32 -0700391
Myles Watson911d1ae2016-11-28 16:44:40 -0800392 BTM_TRACE_DEBUG("%s, %d, %d", __func__, scan_int, scan_win);
393 BTM_VendorSpecificCommand(HCI_BLE_EXTENDED_SCAN_PARAMS_OCF,
394 HCIC_PARAM_SIZE_BLE_WRITE_EXTENDED_SCAN_PARAM,
395 scan_param, NULL);
Satya Calloji935324a2015-03-31 13:24:32 -0700396}
397
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800398/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800399 *
400 * Function BTM_BleObserve
401 *
402 * Description This procedure keep the device listening for advertising
403 * events from a broadcast device.
404 *
405 * Parameters start: start or stop observe.
406 * white_list: use white list in observer mode or not.
407 *
408 * Returns void
409 *
410 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800411tBTM_STATUS BTM_BleObserve(bool start, uint8_t duration,
412 tBTM_INQ_RESULTS_CB* p_results_cb,
413 tBTM_CMPL_CB* p_cmpl_cb) {
414 tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
415 tBTM_STATUS status = BTM_WRONG_MODE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800416
Myles Watson911d1ae2016-11-28 16:44:40 -0800417 uint32_t scan_interval =
418 !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
419 uint32_t scan_window =
420 !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
Satya Calloji935324a2015-03-31 13:24:32 -0700421
Myles Watson911d1ae2016-11-28 16:44:40 -0800422 BTM_TRACE_EVENT("%s : scan_type:%d, %d, %d", __func__,
423 btm_cb.btm_inq_vars.scan_type, p_inq->scan_interval,
424 p_inq->scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800425
Myles Watson911d1ae2016-11-28 16:44:40 -0800426 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700427
Myles Watson911d1ae2016-11-28 16:44:40 -0800428 if (start) {
429 /* shared inquiry database, do not allow observe if any inquiry is active */
430 if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
431 BTM_TRACE_ERROR("%s Observe Already Active", __func__);
432 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800433 }
434
Myles Watson911d1ae2016-11-28 16:44:40 -0800435 btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
436 btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
437 status = BTM_CMD_STARTED;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700438
Myles Watson911d1ae2016-11-28 16:44:40 -0800439 /* scan is not started */
440 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
441 /* allow config of scan type */
442 p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE)
443 ? BTM_BLE_SCAN_MODE_ACTI
444 : p_inq->scan_type;
445/* assume observe always not using white list */
446#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == true)
447 /* enable resolving list */
448 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
449#endif
450
451 if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
Jakub Pawlowski9df2a552016-12-02 11:34:06 -0800452 btm_send_hci_set_scan_params(
Myles Watson911d1ae2016-11-28 16:44:40 -0800453 p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window,
454 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, BTM_BLE_DEFAULT_SFP);
455 } else {
456 btm_ble_send_extended_scan_params(
457 p_inq->scan_type, scan_interval, scan_window,
458 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, BTM_BLE_DEFAULT_SFP);
459 }
460
461 p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
462 status = btm_ble_start_scan();
463 }
464
465 if (status == BTM_CMD_STARTED) {
466 btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
467 if (duration != 0) {
468 /* start observer timer */
469 period_ms_t duration_ms = duration * 1000;
470 alarm_set_on_queue(btm_cb.ble_ctr_cb.observer_timer, duration_ms,
471 btm_ble_observer_timer_timeout, NULL,
472 btu_general_alarm_queue);
473 }
474 }
475 } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
476 status = BTM_CMD_STARTED;
477 btm_ble_stop_observe();
478 } else {
479 BTM_TRACE_ERROR("%s Observe not active", __func__);
480 }
481
482 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800483}
484
Marie Janssend19e0782016-07-15 12:48:27 -0700485#if (BLE_VND_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800486/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800487 *
488 * Function btm_vsc_brcm_features_complete
489 *
490 * Description Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
491 *
492 * Returns void
493 *
494 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800495static void btm_ble_vendor_capability_vsc_cmpl_cback(
496 tBTM_VSC_CMPL* p_vcs_cplt_params) {
497 uint8_t status = 0xFF;
498 uint8_t* p;
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700499
Myles Watson911d1ae2016-11-28 16:44:40 -0800500 BTM_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700501
Myles Watson911d1ae2016-11-28 16:44:40 -0800502 /* Check status of command complete event */
503 if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) &&
504 (p_vcs_cplt_params->param_len > 0)) {
505 p = p_vcs_cplt_params->p_param_buf;
506 STREAM_TO_UINT8(status, p);
507 }
508
509 if (status == HCI_SUCCESS) {
510 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
511 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
512 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
513 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
514 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.filter_support, p);
515 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_filter, p);
516 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.energy_support, p);
517
518 if (p_vcs_cplt_params->param_len >
519 BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE) {
520 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.version_supported, p);
521 } else {
522 btm_cb.cmn_ble_vsc_cb.version_supported =
523 BTM_VSC_CHIP_CAPABILITY_L_VERSION;
Wei Wanga6ce7752014-05-20 06:30:32 +0000524 }
525
Myles Watson911d1ae2016-11-28 16:44:40 -0800526 if (btm_cb.cmn_ble_vsc_cb.version_supported >=
527 BTM_VSC_CHIP_CAPABILITY_M_VERSION) {
528 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.total_trackable_advertisers, p);
529 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.extended_scan_support, p);
530 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.debug_logging_supported, p);
Wei Wanga6ce7752014-05-20 06:30:32 +0000531 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800532 btm_cb.cmn_ble_vsc_cb.values_read = true;
533 }
Satya Callojic4e25962014-05-10 23:46:24 -0700534
Myles Watson911d1ae2016-11-28 16:44:40 -0800535 BTM_TRACE_DEBUG(
536 "%s: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d, ext_scan=%d", __func__,
537 status, btm_cb.cmn_ble_vsc_cb.max_irk_list_sz,
538 btm_cb.cmn_ble_vsc_cb.adv_inst_max, btm_cb.cmn_ble_vsc_cb.rpa_offloading,
539 btm_cb.cmn_ble_vsc_cb.energy_support,
540 btm_cb.cmn_ble_vsc_cb.extended_scan_support);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700541
Jakub Pawlowski22552502016-12-22 03:13:00 -0800542 btm_ble_adv_init();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700543
Myles Watson911d1ae2016-11-28 16:44:40 -0800544 if (btm_cb.cmn_ble_vsc_cb.max_filter > 0) btm_ble_adv_filter_init();
Satya Callojid5aa2472014-09-23 18:27:09 -0700545
Marie Janssend19e0782016-07-15 12:48:27 -0700546#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800547 /* VS capability included and non-4.2 device */
548 if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0 &&
549 controller_get_interface()->get_ble_resolving_list_max_size() == 0)
550 btm_ble_resolving_list_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
551#endif /* (BLE_PRIVACY_SPT == TRUE) */
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700552
Myles Watson911d1ae2016-11-28 16:44:40 -0800553 if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_init();
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700554
Myles Watson911d1ae2016-11-28 16:44:40 -0800555 if (p_ctrl_le_feature_rd_cmpl_cback != NULL)
556 p_ctrl_le_feature_rd_cmpl_cback(status);
Satya Calloji935324a2015-03-31 13:24:32 -0700557}
Myles Watson911d1ae2016-11-28 16:44:40 -0800558#endif /* (BLE_VND_INCLUDED == TRUE) */
Wei Wanga6ce7752014-05-20 06:30:32 +0000559
560/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800561 *
562 * Function BTM_BleGetVendorCapabilities
563 *
564 * Description This function reads local LE features
565 *
566 * Parameters p_cmn_vsc_cb : Locala LE capability structure
567 *
568 * Returns void
569 *
570 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800571extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB* p_cmn_vsc_cb) {
572 BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700573
Myles Watson911d1ae2016-11-28 16:44:40 -0800574 if (NULL != p_cmn_vsc_cb) {
575 *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
576 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700577}
578
579/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800580 *
581 * Function BTM_BleReadControllerFeatures
582 *
583 * Description Reads BLE specific controller features
584 *
Myles Watson9ca07092016-11-28 16:41:53 -0800585 * Parameters: tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when
586 * features are read
Myles Watsonee96a3c2016-11-23 14:49:54 -0800587 *
588 * Returns void
589 *
590 ******************************************************************************/
Myles Watsond628a062016-10-27 10:02:37 -0700591#if (BLE_VND_INCLUDED == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800592extern void BTM_BleReadControllerFeatures(
593 tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {
594 if (true == btm_cb.cmn_ble_vsc_cb.values_read) return;
Satya Calloji3f24f462014-09-16 22:44:43 -0700595
Myles Watson911d1ae2016-11-28 16:44:40 -0800596 BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700597
Myles Watson911d1ae2016-11-28 16:44:40 -0800598 p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
599 BTM_VendorSpecificCommand(HCI_BLE_VENDOR_CAP_OCF, 0, NULL,
600 btm_ble_vendor_capability_vsc_cmpl_cback);
Wei Wanga6ce7752014-05-20 06:30:32 +0000601}
Myles Watsond628a062016-10-27 10:02:37 -0700602#else
Myles Watson911d1ae2016-11-28 16:44:40 -0800603extern void BTM_BleReadControllerFeatures(
604 UNUSED_ATTR tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {}
Myles Watsond628a062016-10-27 10:02:37 -0700605#endif
Wei Wanga6ce7752014-05-20 06:30:32 +0000606
607/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800608 *
609 * Function BTM_BleEnableMixedPrivacyMode
610 *
611 * Description This function is called to enabled Mixed mode if privacy 1.2
612 * is applicable in controller.
613 *
614 * Parameters mixed_on: mixed mode to be used or not.
615 *
616 * Returns void
617 *
618 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800619void BTM_BleEnableMixedPrivacyMode(bool mixed_on) {
Marie Janssend19e0782016-07-15 12:48:27 -0700620#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800621 btm_cb.ble_ctr_cb.mixed_mode = mixed_on;
Satya Calloji444a8da2015-03-06 10:38:22 -0800622
Myles Watson911d1ae2016-11-28 16:44:40 -0800623/* TODO: send VSC to enabled mixed mode */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800624#endif
625}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700627/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800628 *
629 * Function BTM_BleConfigPrivacy
630 *
631 * Description This function is called to enable or disable the privacy in
632 * LE channel of the local device.
633 *
634 * Parameters privacy_mode: privacy mode on or off.
635 *
636 * Returns bool privacy mode set success; otherwise failed.
637 *
638 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800639bool BTM_BleConfigPrivacy(bool privacy_mode) {
Marie Janssend19e0782016-07-15 12:48:27 -0700640#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800641 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700642
Myles Watson911d1ae2016-11-28 16:44:40 -0800643 BTM_TRACE_EVENT("%s", __func__);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700644
Myles Watson911d1ae2016-11-28 16:44:40 -0800645 /* if LE is not supported, return error */
646 if (!controller_get_interface()->supports_ble()) return false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800647
Myles Watson911d1ae2016-11-28 16:44:40 -0800648 uint8_t addr_resolution = 0;
649 if (!privacy_mode) /* if privacy disabled, always use public address */
650 {
651 p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
652 p_cb->privacy_mode = BTM_PRIVACY_NONE;
653 } else /* privacy is turned on*/
654 {
655 /* always set host random address, used when privacy 1.1 or priavcy 1.2 is
656 * disabled */
657 p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
658 btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700659
Myles Watson911d1ae2016-11-28 16:44:40 -0800660 /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private
661 * address in controller */
662 if (controller_get_interface()->supports_ble_privacy()) {
663 addr_resolution = 1;
664 /* check vendor specific capability */
665 p_cb->privacy_mode =
666 btm_cb.ble_ctr_cb.mixed_mode ? BTM_PRIVACY_MIXED : BTM_PRIVACY_1_2;
667 } else /* 4.1/4.0 controller */
668 p_cb->privacy_mode = BTM_PRIVACY_1_1;
669 }
Satya Calloji444a8da2015-03-06 10:38:22 -0800670
Myles Watson911d1ae2016-11-28 16:44:40 -0800671 GAP_BleAttrDBUpdate(GATT_UUID_GAP_CENTRAL_ADDR_RESOL,
672 (tGAP_BLE_ATTR_VALUE*)&addr_resolution);
Satya Calloji444a8da2015-03-06 10:38:22 -0800673
Myles Watson911d1ae2016-11-28 16:44:40 -0800674 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800675#else
Myles Watson911d1ae2016-11-28 16:44:40 -0800676 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800677#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700678}
679
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700680/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800681 *
682 * Function BTM_BleMaxMultiAdvInstanceCount
683 *
Myles Watson9ca07092016-11-28 16:41:53 -0800684 * Description Returns max number of multi adv instances supported by
685 * controller
Myles Watsonee96a3c2016-11-23 14:49:54 -0800686 *
687 * Returns Max multi adv instance count
688 *
689 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800690extern uint8_t BTM_BleMaxMultiAdvInstanceCount(void) {
691 return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX
692 ? btm_cb.cmn_ble_vsc_cb.adv_inst_max
693 : BTM_BLE_MULTI_ADV_MAX;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700694}
695
The Android Open Source Project5738f832012-12-12 16:00:35 -0800696/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800697 *
698 * Function BTM_BleLocalPrivacyEnabled
699 *
700 * Description Checks if local device supports private address
701 *
702 * Returns Return true if local privacy is enabled else false
703 *
704 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800705bool BTM_BleLocalPrivacyEnabled(void) {
Marie Janssend19e0782016-07-15 12:48:27 -0700706#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800707 return (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE);
Sharvil Nanavati26112632014-06-07 00:56:14 -0700708#else
Myles Watson911d1ae2016-11-28 16:44:40 -0800709 return false;
Sharvil Nanavati26112632014-06-07 00:56:14 -0700710#endif
711}
712
Jakub Pawlowski83211b02016-12-07 11:25:15 -0800713/**
714 * Set BLE connectable mode to auto connect
715 */
716void BTM_BleStartAutoConn() {
717 BTM_TRACE_EVENT("%s", __func__);
718 if (!controller_get_interface()->supports_ble()) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800719
Jakub Pawlowski83211b02016-12-07 11:25:15 -0800720 if (btm_cb.ble_ctr_cb.bg_conn_type != BTM_BLE_CONN_AUTO) {
721 btm_ble_start_auto_conn(true);
722 btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_AUTO;
Myles Watson911d1ae2016-11-28 16:44:40 -0800723 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800724}
725
726/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800727 *
728 * Function BTM_BleClearBgConnDev
729 *
730 * Description This function is called to clear the whitelist,
731 * end any pending whitelist connections,
Nitin Arora021e17a2014-01-29 19:18:39 -0800732* and reset the local bg device list.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800733 *
734 * Parameters void
735 *
736 * Returns void
737 *
738 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800739void BTM_BleClearBgConnDev(void) {
740 btm_ble_start_auto_conn(false);
741 btm_ble_clear_white_list();
742 gatt_reset_bgdev_list();
Nitin Arora021e17a2014-01-29 19:18:39 -0800743}
744
745/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800746 *
747 * Function BTM_BleUpdateBgConnDev
748 *
749 * Description This function is called to add or remove a device into/from
750 * background connection procedure. The background connection
Myles Watson911d1ae2016-11-28 16:44:40 -0800751* procedure is decided by the background connection type, it
752*can be
The Android Open Source Project5738f832012-12-12 16:00:35 -0800753* auto connection, or selective connection.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800754 *
755 * Parameters add_remove: true to add; false to remove.
756 * remote_bda: device address to add/remove.
757 *
758 * Returns void
759 *
760 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800761bool BTM_BleUpdateBgConnDev(bool add_remove, BD_ADDR remote_bda) {
762 BTM_TRACE_EVENT("%s() add=%d", __func__, add_remove);
763 return btm_update_dev_to_white_list(add_remove, remote_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800764}
765
766/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800767 *
768 * Function BTM_BleSetConnectableMode
769 *
770 * Description This function is called to set BLE connectable mode for a
771 * peripheral device.
772 *
Myles Watson9ca07092016-11-28 16:41:53 -0800773 * Parameters conn_mode: directed connectable mode, or non-directed. It
774 * can be BTM_BLE_CONNECT_EVT,
775 * BTM_BLE_CONNECT_DIR_EVT or
Myles Watsonee96a3c2016-11-23 14:49:54 -0800776 * BTM_BLE_CONNECT_LO_DUTY_DIR_EVT
777 *
778 * Returns BTM_ILLEGAL_VALUE if controller does not support BLE.
779 * BTM_SUCCESS is status set successfully; otherwise failure.
780 *
781 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800782tBTM_STATUS BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode) {
783 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800784
Myles Watson911d1ae2016-11-28 16:44:40 -0800785 BTM_TRACE_EVENT("%s connectable_mode = %d ", __func__, connectable_mode);
786 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800787
Myles Watson911d1ae2016-11-28 16:44:40 -0800788 p_cb->directed_conn = connectable_mode;
789 return btm_ble_set_connectability(p_cb->connectable_mode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800790}
791
Marie Janssend19e0782016-07-15 12:48:27 -0700792#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800793static bool is_resolving_list_bit_set(void* data, void* context) {
794 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800795
Myles Watson911d1ae2016-11-28 16:44:40 -0800796 if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) != 0)
797 return false;
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800798
Myles Watson911d1ae2016-11-28 16:44:40 -0800799 return true;
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800800}
Pavlin Radoslavova9ea43b2016-02-15 11:47:37 -0800801#endif
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800802
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800803/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800804 *
805 * Function btm_set_conn_mode_adv_init_addr
806 *
Myles Watson9ca07092016-11-28 16:41:53 -0800807 * Description set initator address type and local address type based on
808 * adv mode.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800809 *
810 *
811 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800812static uint8_t btm_set_conn_mode_adv_init_addr(
813 tBTM_BLE_INQ_CB* p_cb, BD_ADDR_PTR p_peer_addr_ptr,
814 tBLE_ADDR_TYPE* p_peer_addr_type, tBLE_ADDR_TYPE* p_own_addr_type) {
815 uint8_t evt_type;
Marie Janssend19e0782016-07-15 12:48:27 -0700816#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800817 tBTM_SEC_DEV_REC* p_dev_rec;
Pavlin Radoslavova9ea43b2016-02-15 11:47:37 -0800818#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800819
Myles Watson911d1ae2016-11-28 16:44:40 -0800820 evt_type =
821 (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE)
822 ? ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT)
823 : BTM_BLE_CONNECT_EVT;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700824
Myles Watson911d1ae2016-11-28 16:44:40 -0800825 if (evt_type == BTM_BLE_CONNECT_EVT) {
826 evt_type = p_cb->directed_conn;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700827
Myles Watson911d1ae2016-11-28 16:44:40 -0800828 if (p_cb->directed_conn == BTM_BLE_CONNECT_DIR_EVT ||
829 p_cb->directed_conn == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT) {
Marie Janssend19e0782016-07-15 12:48:27 -0700830#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800831 /* for privacy 1.2, convert peer address as static, own address set as ID
832 * addr */
833 if (btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 ||
834 btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
835 /* only do so for bonded device */
836 if ((p_dev_rec = btm_find_or_alloc_dev(p_cb->direct_bda.bda)) != NULL &&
837 p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
838 btm_ble_enable_resolving_list(BTM_BLE_RL_ADV);
839 memcpy(p_peer_addr_ptr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
840 *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
841 *p_own_addr_type = BLE_ADDR_RANDOM_ID;
842 return evt_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700843 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800844 /* otherwise fall though as normal directed adv */
845 else {
846 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800847 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800848 }
849#endif
850 /* direct adv mode does not have privacy, if privacy is not enabled */
851 *p_peer_addr_type = p_cb->direct_bda.type;
852 memcpy(p_peer_addr_ptr, p_cb->direct_bda.bda, BD_ADDR_LEN);
853 return evt_type;
Satya Calloji444a8da2015-03-06 10:38:22 -0800854 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800855 }
856
857/* undirect adv mode or non-connectable mode*/
858#if (BLE_PRIVACY_SPT == TRUE)
859 /* when privacy 1.2 privacy only mode is used, or mixed mode */
860 if ((btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 &&
861 p_cb->afp != AP_SCAN_CONN_ALL) ||
862 btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
863 list_node_t* n =
864 list_foreach(btm_cb.sec_dev_rec, is_resolving_list_bit_set, NULL);
865 if (n) {
866 /* if enhanced privacy is required, set Identity address and matching IRK
867 * peer */
868 tBTM_SEC_DEV_REC* p_dev_rec =
869 static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
870 memcpy(p_peer_addr_ptr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
871 *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
872
873 *p_own_addr_type = BLE_ADDR_RANDOM_ID;
874 } else {
875 /* resolving list is empty, not enabled */
876 *p_own_addr_type = BLE_ADDR_RANDOM;
Satya Calloji444a8da2015-03-06 10:38:22 -0800877 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800878 }
879 /* privacy 1.1, or privacy 1.2, general discoverable/connectable mode, disable
880 privacy in */
881 /* controller fall back to host based privacy */
882 else if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
883 *p_own_addr_type = BLE_ADDR_RANDOM;
884 }
Satya Calloji444a8da2015-03-06 10:38:22 -0800885#endif
886
Myles Watson911d1ae2016-11-28 16:44:40 -0800887 /* if no privacy,do not set any peer address,*/
888 /* local address type go by global privacy setting */
889 return evt_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800890}
891
892/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800893 *
894 * Function BTM_BleSetAdvParams
895 *
896 * Description This function is called to set advertising parameters.
897 *
898 * Parameters adv_int_min: minimum advertising interval
899 * adv_int_max: maximum advertising interval
900 * p_dir_bda: connectable direct initiator's LE device address
901 * chnl_map: advertising channel map.
902 *
903 * Returns void
904 *
905 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700906tBTM_STATUS BTM_BleSetAdvParams(uint16_t adv_int_min, uint16_t adv_int_max,
Myles Watson911d1ae2016-11-28 16:44:40 -0800907 tBLE_BD_ADDR* p_dir_bda,
908 tBTM_BLE_ADV_CHNL_MAP chnl_map) {
909 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
910 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
911 tBTM_STATUS status = BTM_SUCCESS;
912 BD_ADDR p_addr_ptr = {0};
913 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
914 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
915 uint8_t adv_mode = p_cb->adv_mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800916
Myles Watson911d1ae2016-11-28 16:44:40 -0800917 BTM_TRACE_EVENT("BTM_BleSetAdvParams");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800918
Myles Watson911d1ae2016-11-28 16:44:40 -0800919 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700920
Myles Watson911d1ae2016-11-28 16:44:40 -0800921 if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN,
922 BTM_BLE_ADV_INT_MAX) ||
923 !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN,
924 BTM_BLE_ADV_INT_MAX)) {
925 return BTM_ILLEGAL_VALUE;
926 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800927
Myles Watson911d1ae2016-11-28 16:44:40 -0800928 p_cb->adv_interval_min = adv_int_min;
929 p_cb->adv_interval_max = adv_int_max;
930 p_cb->adv_chnl_map = chnl_map;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
Myles Watson911d1ae2016-11-28 16:44:40 -0800932 if (p_dir_bda) {
933 memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
934 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800935
Myles Watson911d1ae2016-11-28 16:44:40 -0800936 BTM_TRACE_EVENT("update params for an active adv");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800937
Myles Watson911d1ae2016-11-28 16:44:40 -0800938 btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
Myles Watson911d1ae2016-11-28 16:44:40 -0800940 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
941 p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800942
Myles Watson911d1ae2016-11-28 16:44:40 -0800943 /* update adv params */
944 btsnd_hcic_ble_write_adv_params(
945 p_cb->adv_interval_min, p_cb->adv_interval_max, p_cb->evt_type,
946 own_addr_type, init_addr_type, p_addr_ptr, p_cb->adv_chnl_map, p_cb->afp);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947
Myles Watson911d1ae2016-11-28 16:44:40 -0800948 if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800949
Myles Watson911d1ae2016-11-28 16:44:40 -0800950 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800951}
952
953/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800954 *
955 * Function BTM_BleSetScanParams
956 *
957 * Description This function is called to set scan parameters.
958 *
959 * Parameters client_if - Client IF
960 * scan_interval - Scan interval
961 * scan_window - Scan window
962 * scan_mode - Scan mode
963 * scan_setup_status_cback - Scan param setup status callback
964 *
965 * Returns void
966 *
967 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800968void BTM_BleSetScanParams(tGATT_IF client_if, uint32_t scan_interval,
969 uint32_t scan_window, tBLE_SCAN_MODE scan_mode,
970 tBLE_SCAN_PARAM_SETUP_CBACK scan_setup_status_cback) {
971 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
972 uint32_t max_scan_interval;
973 uint32_t max_scan_window;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974
Myles Watson911d1ae2016-11-28 16:44:40 -0800975 BTM_TRACE_EVENT("%s", __func__);
976 if (!controller_get_interface()->supports_ble()) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977
Myles Watson911d1ae2016-11-28 16:44:40 -0800978 /* If not supporting extended scan support, use the older range for checking
979 */
980 if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
981 max_scan_interval = BTM_BLE_SCAN_INT_MAX;
982 max_scan_window = BTM_BLE_SCAN_WIN_MAX;
983 } else {
984 /* If supporting extended scan support, use the new extended range for
985 * checking */
986 max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX;
987 max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX;
988 }
Satya Calloji935324a2015-03-31 13:24:32 -0700989
Myles Watson911d1ae2016-11-28 16:44:40 -0800990 if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN,
991 max_scan_interval) &&
992 BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN,
993 max_scan_window) &&
994 (scan_mode == BTM_BLE_SCAN_MODE_ACTI ||
995 scan_mode == BTM_BLE_SCAN_MODE_PASS)) {
996 p_cb->scan_type = scan_mode;
997 p_cb->scan_interval = scan_interval;
998 p_cb->scan_window = scan_window;
Satya Calloji935324a2015-03-31 13:24:32 -0700999
Myles Watson911d1ae2016-11-28 16:44:40 -08001000 if (scan_setup_status_cback != NULL)
1001 scan_setup_status_cback(client_if, BTM_SUCCESS);
1002 } else {
1003 if (scan_setup_status_cback != NULL)
1004 scan_setup_status_cback(client_if, BTM_ILLEGAL_VALUE);
Satya Calloji935324a2015-03-31 13:24:32 -07001005
Myles Watson911d1ae2016-11-28 16:44:40 -08001006 BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
1007 scan_interval, scan_window);
1008 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001009}
1010
1011/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001012 *
1013 * Function BTM_BleWriteScanRsp
1014 *
1015 * Description This function is called to write LE scan response.
1016 *
1017 * Parameters: p_scan_rsp: scan response information.
1018 *
1019 * Returns void
1020 *
1021 ******************************************************************************/
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -07001022void BTM_BleWriteScanRsp(uint8_t* data, uint8_t length,
Myles Watson911d1ae2016-11-28 16:44:40 -08001023 tBTM_BLE_ADV_DATA_CMPL_CBACK* p_adv_data_cback) {
1024 BTM_TRACE_EVENT("%s: length: %d", __func__, length);
1025 if (!controller_get_interface()->supports_ble()) {
1026 p_adv_data_cback(BTM_ILLEGAL_VALUE);
1027 return;
1028 }
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001029
Myles Watson911d1ae2016-11-28 16:44:40 -08001030 btsnd_hcic_ble_set_scan_rsp_data(length, data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001031
Myles Watson911d1ae2016-11-28 16:44:40 -08001032 if (length != 0)
1033 btm_cb.ble_ctr_cb.inq_var.scan_rsp = true;
1034 else
1035 btm_cb.ble_ctr_cb.inq_var.scan_rsp = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001036
Myles Watson911d1ae2016-11-28 16:44:40 -08001037 p_adv_data_cback(BTM_SUCCESS);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038}
1039
1040/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001041 *
1042 * Function BTM_CheckAdvData
1043 *
1044 * Description This function is called to get ADV data for a specific type.
1045 *
1046 * Parameters p_adv - pointer of ADV data
1047 * type - finding ADV data type
1048 * p_length - return the length of ADV data not including type
1049 *
1050 * Returns pointer of ADV data
1051 *
1052 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001053uint8_t* BTM_CheckAdvData(uint8_t* p_adv, uint8_t type, uint8_t* p_length) {
1054 uint8_t* p = p_adv;
1055 uint8_t length;
1056 uint8_t adv_type;
1057 BTM_TRACE_API("%s: type=0x%02x", __func__, type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058
Myles Watson911d1ae2016-11-28 16:44:40 -08001059 STREAM_TO_UINT8(length, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060
Myles Watson911d1ae2016-11-28 16:44:40 -08001061 while (length && (p - p_adv <= BTM_BLE_CACHE_ADV_DATA_MAX)) {
1062 STREAM_TO_UINT8(adv_type, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001063
Myles Watson911d1ae2016-11-28 16:44:40 -08001064 if (adv_type == type) {
1065 /* length doesn't include itself */
1066 *p_length = length - 1; /* minus the length of type */
1067 return p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001069 p += length - 1; /* skip the length of data */
1070 STREAM_TO_UINT8(length, p);
1071 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001072
Myles Watson911d1ae2016-11-28 16:44:40 -08001073 *p_length = 0;
1074 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001075}
1076
1077/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001078 *
1079 * Function BTM__BLEReadDiscoverability
1080 *
Myles Watson9ca07092016-11-28 16:41:53 -08001081 * Description This function is called to read the current LE
1082 * discoverability mode of the device.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001083 *
1084 * Returns BTM_BLE_NON_DISCOVERABLE ,BTM_BLE_LIMITED_DISCOVERABLE or
1085 * BTM_BLE_GENRAL_DISCOVERABLE
1086 *
1087 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001088uint16_t BTM_BleReadDiscoverability() {
1089 BTM_TRACE_API("%s", __func__);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001090
Myles Watson911d1ae2016-11-28 16:44:40 -08001091 return (btm_cb.ble_ctr_cb.inq_var.discoverable_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001092}
1093
1094/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001095 *
1096 * Function BTM__BLEReadConnectability
1097 *
Myles Watson9ca07092016-11-28 16:41:53 -08001098 * Description This function is called to read the current LE
1099 * connectability mode of the device.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001100 *
1101 * Returns BTM_BLE_NON_CONNECTABLE or BTM_BLE_CONNECTABLE
1102 *
1103 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001104uint16_t BTM_BleReadConnectability() {
1105 BTM_TRACE_API("%s", __func__);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001106
Myles Watson911d1ae2016-11-28 16:44:40 -08001107 return (btm_cb.ble_ctr_cb.inq_var.connectable_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001108}
1109
1110/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001111 *
1112 * Function btm_ble_select_adv_interval
1113 *
1114 * Description select adv interval based on device mode
1115 *
1116 * Returns void
1117 *
1118 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001119void btm_ble_select_adv_interval(tBTM_BLE_INQ_CB* p_cb, uint8_t evt_type,
1120 uint16_t* p_adv_int_min,
1121 uint16_t* p_adv_int_max) {
1122 if (p_cb->adv_interval_min && p_cb->adv_interval_max) {
1123 *p_adv_int_min = p_cb->adv_interval_min;
1124 *p_adv_int_max = p_cb->adv_interval_max;
1125 } else {
1126 switch (evt_type) {
1127 case BTM_BLE_CONNECT_EVT:
1128 case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
1129 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
1130 break;
1131
1132 case BTM_BLE_NON_CONNECT_EVT:
1133 case BTM_BLE_DISCOVER_EVT:
1134 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
1135 break;
1136
1137 /* connectable directed event */
1138 case BTM_BLE_CONNECT_DIR_EVT:
1139 *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
1140 *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
1141 break;
1142
1143 default:
1144 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
1145 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001146 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001147 }
1148 return;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001149}
Satya Calloji444a8da2015-03-06 10:38:22 -08001150
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001151/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001152 *
1153 * Function btm_ble_update_dmt_flag_bits
1154 *
Myles Watson9ca07092016-11-28 16:41:53 -08001155 * Description Obtain updated adv flag value based on connect and
1156 * discoverability mode. Also, setup DMT support value in the
1157 * flag based on whether the controller supports both LE and
1158 * BR/EDR.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001159 *
1160 * Parameters: flag_value (Input / Output) - flag value
1161 * connect_mode (Input) - Connect mode value
1162 * disc_mode (Input) - discoverability mode
1163 *
1164 * Returns void
1165 *
1166 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001167void btm_ble_update_dmt_flag_bits(uint8_t* adv_flag_value,
1168 const uint16_t connect_mode,
1169 const uint16_t disc_mode) {
1170 /* BR/EDR non-discoverable , non-connectable */
1171 if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
1172 (connect_mode & BTM_CONNECTABLE_MASK) == 0)
1173 *adv_flag_value |= BTM_BLE_BREDR_NOT_SPT;
1174 else
1175 *adv_flag_value &= ~BTM_BLE_BREDR_NOT_SPT;
Satya Calloji7cf7da92015-02-05 13:12:15 -08001176
Myles Watson911d1ae2016-11-28 16:44:40 -08001177 /* if local controller support, mark both controller and host support in flag
1178 */
1179 if (controller_get_interface()->supports_simultaneous_le_bredr())
1180 *adv_flag_value |= (BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
1181 else
1182 *adv_flag_value &= ~(BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
Satya Calloji7cf7da92015-02-05 13:12:15 -08001183}
1184
1185/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001186 *
1187 * Function btm_ble_set_adv_flag
1188 *
1189 * Description Set adv flag in adv data.
1190 *
1191 * Parameters: connect_mode (Input)- Connect mode value
1192 * disc_mode (Input) - discoverability mode
1193 *
1194 * Returns void
1195 *
1196 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001197void btm_ble_set_adv_flag(uint16_t connect_mode, uint16_t disc_mode) {
1198 uint8_t flag = 0, old_flag = 0;
1199 tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001200
Myles Watson911d1ae2016-11-28 16:44:40 -08001201 if (p_adv_data->p_flags != NULL) flag = old_flag = *(p_adv_data->p_flags);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001202
Myles Watson911d1ae2016-11-28 16:44:40 -08001203 btm_ble_update_dmt_flag_bits(&flag, connect_mode, disc_mode);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001204
Myles Watson911d1ae2016-11-28 16:44:40 -08001205 LOG_DEBUG(LOG_TAG, "disc_mode %04x", disc_mode);
1206 /* update discoverable flag */
1207 if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE) {
1208 flag &= ~BTM_BLE_GEN_DISC_FLAG;
1209 flag |= BTM_BLE_LIMIT_DISC_FLAG;
1210 } else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE) {
1211 flag |= BTM_BLE_GEN_DISC_FLAG;
1212 flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
1213 } else /* remove all discoverable flags */
1214 {
1215 flag &= ~(BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG);
1216 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001217
Myles Watson911d1ae2016-11-28 16:44:40 -08001218 if (flag != old_flag) {
1219 btm_ble_update_adv_flag(flag);
1220 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001221}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001222/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001223 *
1224 * Function btm_ble_set_discoverability
1225 *
1226 * Description This function is called to set BLE discoverable mode.
1227 *
1228 * Parameters: combined_mode: discoverability mode.
1229 *
1230 * Returns BTM_SUCCESS is status set successfully; otherwise failure.
1231 *
1232 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001233tBTM_STATUS btm_ble_set_discoverability(uint16_t combined_mode) {
1234 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1235 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
1236 uint16_t mode = (combined_mode & BTM_BLE_DISCOVERABLE_MASK);
1237 uint8_t new_mode = BTM_BLE_ADV_ENABLE;
1238 uint8_t evt_type;
1239 tBTM_STATUS status = BTM_SUCCESS;
1240 BD_ADDR p_addr_ptr = {0};
1241 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC,
1242 own_addr_type = p_addr_cb->own_addr_type;
1243 uint16_t adv_int_min, adv_int_max;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001244
Myles Watson911d1ae2016-11-28 16:44:40 -08001245 BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
1246 combined_mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001247
Myles Watson911d1ae2016-11-28 16:44:40 -08001248 /*** Check mode parameter ***/
1249 if (mode > BTM_BLE_MAX_DISCOVERABLE) return (BTM_ILLEGAL_VALUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001250
Myles Watson911d1ae2016-11-28 16:44:40 -08001251 p_cb->discoverable_mode = mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001252
Myles Watson911d1ae2016-11-28 16:44:40 -08001253 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type,
1254 &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001255
Myles Watson911d1ae2016-11-28 16:44:40 -08001256 if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE &&
1257 mode == BTM_BLE_NON_DISCOVERABLE)
1258 new_mode = BTM_BLE_ADV_DISABLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001259
Myles Watson911d1ae2016-11-28 16:44:40 -08001260 btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001261
Myles Watson911d1ae2016-11-28 16:44:40 -08001262 alarm_cancel(p_cb->fast_adv_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263
Myles Watson911d1ae2016-11-28 16:44:40 -08001264 /* update adv params if start advertising */
1265 BTM_TRACE_EVENT("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type,
1266 p_cb->evt_type);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001267
Myles Watson911d1ae2016-11-28 16:44:40 -08001268 if (new_mode == BTM_BLE_ADV_ENABLE) {
1269 btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, combined_mode);
1270
1271 if (evt_type != p_cb->evt_type || p_cb->adv_addr_type != own_addr_type ||
1272 !p_cb->fast_adv_on) {
1273 btm_ble_stop_adv();
1274
1275 /* update adv params */
1276 btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
1277 own_addr_type, init_addr_type, p_addr_ptr,
1278 p_cb->adv_chnl_map, p_cb->afp);
1279 p_cb->evt_type = evt_type;
1280 p_cb->adv_addr_type = own_addr_type;
1281 }
1282 }
1283
1284 if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode) {
Satya Calloji444a8da2015-03-06 10:38:22 -08001285 if (new_mode == BTM_BLE_ADV_ENABLE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001286 status = btm_ble_start_adv();
Satya Calloji70b95982015-04-23 23:39:49 -07001287 else
Myles Watson911d1ae2016-11-28 16:44:40 -08001288 status = btm_ble_stop_adv();
1289 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001290
Myles Watson911d1ae2016-11-28 16:44:40 -08001291 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
1292 p_cb->fast_adv_on = true;
1293 /* start initial GAP mode adv timer */
1294 alarm_set_on_queue(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
1295 btm_ble_fast_adv_timer_timeout, NULL,
1296 btu_general_alarm_queue);
1297 } else {
1298#if (BLE_PRIVACY_SPT == TRUE)
1299 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
1300#endif
1301 }
1302
1303 /* set up stop advertising timer */
1304 if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE) {
1305 BTM_TRACE_EVENT("start timer for limited disc mode duration=%d ms",
1306 BTM_BLE_GAP_LIM_TIMEOUT_MS);
1307 /* start Tgap(lim_timeout) */
1308 alarm_set_on_queue(p_cb->inquiry_timer, BTM_BLE_GAP_LIM_TIMEOUT_MS,
1309 btm_ble_inquiry_timer_gap_limited_discovery_timeout,
1310 NULL, btu_general_alarm_queue);
1311 }
1312 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001313}
1314
1315/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001316 *
1317 * Function btm_ble_set_connectability
1318 *
1319 * Description This function is called to set BLE connectability mode.
1320 *
1321 * Parameters: combined_mode: connectability mode.
1322 *
1323 * Returns BTM_SUCCESS is status set successfully; otherwise failure.
1324 *
1325 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001326tBTM_STATUS btm_ble_set_connectability(uint16_t combined_mode) {
1327 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1328 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
1329 uint16_t mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
1330 uint8_t new_mode = BTM_BLE_ADV_ENABLE;
1331 uint8_t evt_type;
1332 tBTM_STATUS status = BTM_SUCCESS;
1333 BD_ADDR p_addr_ptr = {0};
1334 tBLE_ADDR_TYPE peer_addr_type = BLE_ADDR_PUBLIC,
1335 own_addr_type = p_addr_cb->own_addr_type;
1336 uint16_t adv_int_min, adv_int_max;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001337
Myles Watson911d1ae2016-11-28 16:44:40 -08001338 BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
1339 combined_mode);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001340
Myles Watson911d1ae2016-11-28 16:44:40 -08001341 /*** Check mode parameter ***/
1342 if (mode > BTM_BLE_MAX_CONNECTABLE) return (BTM_ILLEGAL_VALUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001343
Myles Watson911d1ae2016-11-28 16:44:40 -08001344 p_cb->connectable_mode = mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001345
Myles Watson911d1ae2016-11-28 16:44:40 -08001346 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &peer_addr_type,
1347 &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001348
Myles Watson911d1ae2016-11-28 16:44:40 -08001349 if (mode == BTM_BLE_NON_CONNECTABLE &&
1350 p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
1351 new_mode = BTM_BLE_ADV_DISABLE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001352
Myles Watson911d1ae2016-11-28 16:44:40 -08001353 btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001354
Myles Watson911d1ae2016-11-28 16:44:40 -08001355 alarm_cancel(p_cb->fast_adv_timer);
1356 /* update adv params if needed */
1357 if (new_mode == BTM_BLE_ADV_ENABLE) {
1358 btm_ble_set_adv_flag(combined_mode, btm_cb.btm_inq_vars.discoverable_mode);
1359 if (p_cb->evt_type != evt_type ||
1360 p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on) {
1361 btm_ble_stop_adv();
1362
1363 btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
1364 own_addr_type, peer_addr_type, p_addr_ptr,
1365 p_cb->adv_chnl_map, p_cb->afp);
1366 p_cb->evt_type = evt_type;
1367 p_cb->adv_addr_type = own_addr_type;
1368 }
1369 }
1370
1371 /* update advertising mode */
1372 if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode) {
Satya Calloji444a8da2015-03-06 10:38:22 -08001373 if (new_mode == BTM_BLE_ADV_ENABLE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001374 status = btm_ble_start_adv();
Satya Calloji70b95982015-04-23 23:39:49 -07001375 else
Myles Watson911d1ae2016-11-28 16:44:40 -08001376 status = btm_ble_stop_adv();
1377 }
1378
1379 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
1380 p_cb->fast_adv_on = true;
1381 /* start initial GAP mode adv timer */
1382 alarm_set_on_queue(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
1383 btm_ble_fast_adv_timer_timeout, NULL,
1384 btu_general_alarm_queue);
1385 } else {
Marie Janssend19e0782016-07-15 12:48:27 -07001386#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001387 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
Andre Eisenbacha021a122015-05-20 23:55:13 -07001388#endif
Myles Watson911d1ae2016-11-28 16:44:40 -08001389 }
1390 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391}
1392
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001393void btm_send_hci_scan_enable(uint8_t enable, uint8_t filter_duplicates) {
1394 if (controller_get_interface()->supports_ble_extended_advertising()) {
1395 btsnd_hcic_ble_set_extended_scan_enable(enable, filter_duplicates, 0x0000,
1396 0x0000);
1397 } else {
1398 btsnd_hcic_ble_set_scan_enable(enable, filter_duplicates);
1399 }
1400}
1401
1402void btm_send_hci_set_scan_params(uint8_t scan_type, uint16_t scan_int,
1403 uint16_t scan_win, uint8_t addr_type_own,
1404 uint8_t scan_filter_policy) {
1405 if (controller_get_interface()->supports_ble_extended_advertising()) {
1406 scanning_phy_cfg phy_cfg;
1407 phy_cfg.scan_type = scan_type;
1408 phy_cfg.scan_int = scan_int;
1409 phy_cfg.scan_win = scan_win;
1410
1411 btsnd_hcic_ble_set_extended_scan_params(addr_type_own, scan_filter_policy,
1412 1, &phy_cfg);
1413 } else {
1414 btsnd_hcic_ble_set_scan_params(scan_type, scan_int, scan_win, addr_type_own,
1415 scan_filter_policy);
1416 }
1417}
1418
The Android Open Source Project5738f832012-12-12 16:00:35 -08001419/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001420 *
1421 * Function btm_ble_start_inquiry
1422 *
1423 * Description This function is called to start BLE inquiry procedure.
Myles Watson9ca07092016-11-28 16:41:53 -08001424 * If the duration is zero, the periodic inquiry mode is
1425 * cancelled.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001426 *
1427 * Parameters: mode - GENERAL or LIMITED inquiry
1428 * p_inq_params - pointer to the BLE inquiry parameter.
Myles Watson9ca07092016-11-28 16:41:53 -08001429 * p_results_cb - callback returning pointer to results
1430 * (tBTM_INQ_RESULTS)
Myles Watsonee96a3c2016-11-23 14:49:54 -08001431 * p_cmpl_cb - callback indicating the end of an inquiry
1432 *
1433 *
1434 *
1435 * Returns BTM_CMD_STARTED if successfully started
1436 * BTM_NO_RESOURCES if could not allocate a message buffer
1437 * BTM_BUSY - if an inquiry is already active
1438 *
1439 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001440tBTM_STATUS btm_ble_start_inquiry(uint8_t mode, uint8_t duration) {
1441 tBTM_STATUS status = BTM_CMD_STARTED;
1442 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
1443 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001444
Myles Watson911d1ae2016-11-28 16:44:40 -08001445 BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x",
1446 mode, btm_cb.btm_inq_vars.inq_active);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001447
Myles Watson911d1ae2016-11-28 16:44:40 -08001448 /* if selective connection is active, or inquiry is already active, reject it
1449 */
Jakub Pawlowski83211b02016-12-07 11:25:15 -08001450 if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001451 BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
1452 return (BTM_BUSY);
1453 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001454
Myles Watson911d1ae2016-11-28 16:44:40 -08001455 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001456 btm_send_hci_set_scan_params(
Myles Watson911d1ae2016-11-28 16:44:40 -08001457 BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
1458 BTM_BLE_LOW_LATENCY_SCAN_WIN,
1459 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
Marie Janssend19e0782016-07-15 12:48:27 -07001460#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001461 /* enable IRK list */
1462 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
Wei Wanged534e32014-05-20 06:30:13 +00001463#endif
Myles Watson911d1ae2016-11-28 16:44:40 -08001464 p_ble_cb->inq_var.scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
1465 status = btm_ble_start_scan();
1466 } else if ((p_ble_cb->inq_var.scan_interval !=
1467 BTM_BLE_LOW_LATENCY_SCAN_INT) ||
1468 (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
1469 BTM_TRACE_DEBUG("%s, restart LE scan with low latency scan params",
1470 __func__);
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001471 btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
1472 btm_send_hci_set_scan_params(
Myles Watson911d1ae2016-11-28 16:44:40 -08001473 BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
1474 BTM_BLE_LOW_LATENCY_SCAN_WIN,
1475 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001476 btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE);
Myles Watson911d1ae2016-11-28 16:44:40 -08001477 }
1478
1479 if (status == BTM_CMD_STARTED) {
1480 p_inq->inq_active |= mode;
1481 p_ble_cb->scan_activity |= mode;
1482
1483 BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x",
1484 p_inq->inq_active);
1485
1486 if (duration != 0) {
1487 /* start inquiry timer */
1488 period_ms_t duration_ms = duration * 1000;
1489 alarm_set_on_queue(p_ble_cb->inq_var.inquiry_timer, duration_ms,
1490 btm_ble_inquiry_timer_timeout, NULL,
1491 btu_general_alarm_queue);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001492 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001493 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001494
Myles Watson911d1ae2016-11-28 16:44:40 -08001495 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001496}
1497
1498/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001499 *
1500 * Function btm_ble_read_remote_name_cmpl
1501 *
1502 * Description This function is called when BLE remote name is received.
1503 *
1504 * Returns void
1505 *
1506 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001507void btm_ble_read_remote_name_cmpl(bool status, BD_ADDR bda, uint16_t length,
1508 char* p_name) {
1509 uint8_t hci_status = HCI_SUCCESS;
1510 BD_NAME bd_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001511
Myles Watson911d1ae2016-11-28 16:44:40 -08001512 memset(bd_name, 0, (BD_NAME_LEN + 1));
1513 if (length > BD_NAME_LEN) {
1514 length = BD_NAME_LEN;
1515 }
1516 memcpy((uint8_t*)bd_name, p_name, length);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001517
Myles Watson911d1ae2016-11-28 16:44:40 -08001518 if ((!status) || (length == 0)) {
1519 hci_status = HCI_ERR_HOST_TIMEOUT;
1520 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001521
Myles Watson911d1ae2016-11-28 16:44:40 -08001522 btm_process_remote_name(bda, bd_name, length + 1, hci_status);
1523 btm_sec_rmt_name_request_complete(bda, (uint8_t*)p_name, hci_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001524}
1525
1526/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001527 *
1528 * Function btm_ble_read_remote_name
1529 *
1530 * Description This function read remote LE device name using GATT read
1531 * procedure.
1532 *
1533 * Parameters: None.
1534 *
1535 * Returns void
1536 *
1537 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001538tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_INQ_INFO* p_cur,
1539 tBTM_CMPL_CB* p_cb) {
1540 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001541
Myles Watson911d1ae2016-11-28 16:44:40 -08001542 if (!controller_get_interface()->supports_ble()) return BTM_ERR_PROCESSING;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001543
Myles Watson911d1ae2016-11-28 16:44:40 -08001544 if (p_cur && p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_ADV &&
1545 p_cur->results.ble_evt_type != BTM_BLE_EVT_CONN_DIR_ADV) {
1546 BTM_TRACE_DEBUG("name request to non-connectable device failed.");
1547 return BTM_ERR_PROCESSING;
1548 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001549
Myles Watson911d1ae2016-11-28 16:44:40 -08001550 /* read remote device name using GATT procedure */
1551 if (p_inq->remname_active) return BTM_BUSY;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001552
Myles Watson911d1ae2016-11-28 16:44:40 -08001553 if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
1554 return BTM_BUSY;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001555
Myles Watson911d1ae2016-11-28 16:44:40 -08001556 p_inq->p_remname_cmpl_cb = p_cb;
1557 p_inq->remname_active = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001558
Myles Watson911d1ae2016-11-28 16:44:40 -08001559 memcpy(p_inq->remname_bda, remote_bda, BD_ADDR_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001560
Myles Watson911d1ae2016-11-28 16:44:40 -08001561 alarm_set_on_queue(p_inq->remote_name_timer, BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS,
1562 btm_inq_remote_name_timer_timeout, NULL,
1563 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001564
Myles Watson911d1ae2016-11-28 16:44:40 -08001565 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001566}
1567
1568/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001569 *
1570 * Function btm_ble_cancel_remote_name
1571 *
1572 * Description This function cancel read remote LE device name.
1573 *
1574 * Parameters: None.
1575 *
1576 * Returns void
1577 *
1578 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001579bool btm_ble_cancel_remote_name(BD_ADDR remote_bda) {
1580 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
1581 bool status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001582
Myles Watson911d1ae2016-11-28 16:44:40 -08001583 status = GAP_BleCancelReadPeerDevName(remote_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001584
Myles Watson911d1ae2016-11-28 16:44:40 -08001585 p_inq->remname_active = false;
1586 memset(p_inq->remname_bda, 0, BD_ADDR_LEN);
1587 alarm_cancel(p_inq->remote_name_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001588
Myles Watson911d1ae2016-11-28 16:44:40 -08001589 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001590}
1591
1592/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001593 *
1594 * Function btm_ble_update_adv_flag
1595 *
Myles Watson9ca07092016-11-28 16:41:53 -08001596 * Description This function update the limited discoverable flag in the
1597 * adv data.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001598 *
1599 * Parameters: None.
1600 *
1601 * Returns void
1602 *
1603 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001604static void btm_ble_update_adv_flag(uint8_t flag) {
1605 tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1606 uint8_t* p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001607
Myles Watson911d1ae2016-11-28 16:44:40 -08001608 BTM_TRACE_DEBUG("btm_ble_update_adv_flag new=0x%x", flag);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001609
Myles Watson911d1ae2016-11-28 16:44:40 -08001610 if (p_adv_data->p_flags != NULL) {
1611 BTM_TRACE_DEBUG("btm_ble_update_adv_flag old=0x%x", *p_adv_data->p_flags);
1612 *p_adv_data->p_flags = flag;
1613 } else /* no FLAGS in ADV data*/
1614 {
1615 p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
1616 /* need 3 bytes space to stuff in the flags, if not */
1617 /* erase all written data, just for flags */
1618 if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3) {
1619 p = p_adv_data->p_pad = p_adv_data->ad_data;
1620 memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001621 }
1622
Myles Watson911d1ae2016-11-28 16:44:40 -08001623 *p++ = 2;
1624 *p++ = BTM_BLE_AD_TYPE_FLAG;
1625 p_adv_data->p_flags = p;
1626 *p++ = flag;
1627 p_adv_data->p_pad = p;
1628 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001629
Myles Watson911d1ae2016-11-28 16:44:40 -08001630 btsnd_hcic_ble_set_adv_data(
1631 (uint8_t)(p_adv_data->p_pad - p_adv_data->ad_data), p_adv_data->ad_data);
1632 p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001633}
1634
The Android Open Source Project5738f832012-12-12 16:00:35 -08001635/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001636 *
1637 * Function btm_ble_cache_adv_data
1638 *
1639 * Description Update advertising cache data.
1640 *
1641 * Returns void
1642 *
1643 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001644void btm_ble_cache_adv_data(UNUSED_ATTR tBTM_INQ_RESULTS* p_cur,
1645 uint8_t data_len, uint8_t* p, uint8_t evt_type) {
1646 tBTM_BLE_INQ_CB* p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
1647 uint8_t* p_cache;
1648 uint8_t length;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001649
Myles Watson911d1ae2016-11-28 16:44:40 -08001650 /* cache adv report/scan response data */
1651 if (evt_type != BTM_BLE_SCAN_RSP_EVT) {
1652 p_le_inq_cb->adv_len = 0;
1653 memset(p_le_inq_cb->adv_data_cache, 0, BTM_BLE_CACHE_ADV_DATA_MAX);
1654 }
1655
1656 if (data_len > 0) {
1657 p_cache = &p_le_inq_cb->adv_data_cache[p_le_inq_cb->adv_len];
1658 STREAM_TO_UINT8(length, p);
1659 while (length && ((p_le_inq_cb->adv_len + length + 1) <=
1660 BTM_BLE_CACHE_ADV_DATA_MAX)) {
1661 /* copy from the length byte & data into cache */
1662 memcpy(p_cache, p - 1, length + 1);
1663 /* advance the cache pointer past data */
1664 p_cache += length + 1;
1665 /* increment cache length */
1666 p_le_inq_cb->adv_len += length + 1;
1667 /* skip the length of data */
1668 p += length;
1669 STREAM_TO_UINT8(length, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001670 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001671 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001672
Myles Watson911d1ae2016-11-28 16:44:40 -08001673 /* parse service UUID from adv packet and save it in inq db eir_uuid */
1674 /* TODO */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001675}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001676
The Android Open Source Project5738f832012-12-12 16:00:35 -08001677/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001678 *
1679 * Function btm_ble_is_discoverable
1680 *
1681 * Description check ADV flag to make sure device is discoverable and match
1682 * the search condition
1683 *
1684 * Parameters
1685 *
1686 * Returns void
1687 *
1688 ******************************************************************************/
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001689uint8_t btm_ble_is_discoverable(BD_ADDR bda, uint8_t evt_type) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001690 uint8_t *p_flag, flag = 0, rt = 0;
1691 uint8_t data_len;
1692 tBTM_INQ_PARMS* p_cond = &btm_cb.btm_inq_vars.inqparms;
1693 tBTM_BLE_INQ_CB* p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001694
Myles Watson911d1ae2016-11-28 16:44:40 -08001695 /* for observer, always "discoverable */
1696 if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
1697 rt |= BTM_BLE_OBS_RESULT;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001698
Myles Watson911d1ae2016-11-28 16:44:40 -08001699 /* does not match filter condition */
1700 if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
1701 memcmp(bda, p_cond->filter_cond.bdaddr_cond, BD_ADDR_LEN) != 0) {
1702 BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001703 return rt;
Myles Watson911d1ae2016-11-28 16:44:40 -08001704 }
1705
1706 if (p_le_inq_cb->adv_len != 0) {
1707 p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_FLAG,
1708 &data_len);
1709 if (p_flag != NULL) {
1710 flag = *p_flag;
1711
1712 if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
1713 (flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) {
1714 BTM_TRACE_DEBUG("Find Generable Discoverable device");
1715 rt |= BTM_BLE_INQ_RESULT;
1716 }
1717
1718 else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
1719 (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0) {
1720 BTM_TRACE_DEBUG("Find limited discoverable device");
1721 rt |= BTM_BLE_INQ_RESULT;
1722 }
1723 }
1724 }
1725 return rt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001726}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001727
Myles Watson911d1ae2016-11-28 16:44:40 -08001728static void btm_ble_appearance_to_cod(uint16_t appearance, uint8_t* dev_class) {
1729 dev_class[0] = 0;
Adam Hampsone6c74502014-05-30 15:07:08 -07001730
Myles Watson911d1ae2016-11-28 16:44:40 -08001731 switch (appearance) {
1732 case BTM_BLE_APPEARANCE_GENERIC_PHONE:
1733 dev_class[1] = BTM_COD_MAJOR_PHONE;
1734 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1735 break;
1736 case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
1737 dev_class[1] = BTM_COD_MAJOR_COMPUTER;
1738 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1739 break;
1740 case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
1741 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1742 dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
1743 break;
1744 case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
1745 case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
1746 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1747 dev_class[2] = BTM_COD_MINOR_THERMOMETER;
1748 break;
1749 case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
1750 case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
1751 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1752 dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
1753 break;
1754 case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
1755 case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
1756 case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
1757 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1758 dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
1759 break;
1760 case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
1761 case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
1762 case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
1763 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1764 dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
1765 break;
1766 case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
1767 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1768 dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
1769 break;
1770 case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
1771 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1772 dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
1773 break;
1774 case BTM_BLE_APPEARANCE_GENERIC_WALKING:
1775 case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
1776 case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
1777 case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
1778 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1779 dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
1780 break;
1781 case BTM_BLE_APPEARANCE_GENERIC_WATCH:
1782 case BTM_BLE_APPEARANCE_SPORTS_WATCH:
1783 dev_class[1] = BTM_COD_MAJOR_WEARABLE;
1784 dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
1785 break;
1786 case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
1787 dev_class[1] = BTM_COD_MAJOR_WEARABLE;
1788 dev_class[2] = BTM_COD_MINOR_GLASSES;
1789 break;
1790 case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
1791 dev_class[1] = BTM_COD_MAJOR_IMAGING;
1792 dev_class[2] = BTM_COD_MINOR_DISPLAY;
1793 break;
1794 case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
1795 dev_class[1] = BTM_COD_MAJOR_AUDIO;
1796 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1797 break;
1798 case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
1799 case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
1800 case BTM_BLE_APPEARANCE_GENERIC_HID:
1801 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1802 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1803 break;
1804 case BTM_BLE_APPEARANCE_HID_KEYBOARD:
1805 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1806 dev_class[2] = BTM_COD_MINOR_KEYBOARD;
1807 break;
1808 case BTM_BLE_APPEARANCE_HID_MOUSE:
1809 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1810 dev_class[2] = BTM_COD_MINOR_POINTING;
1811 break;
1812 case BTM_BLE_APPEARANCE_HID_JOYSTICK:
1813 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1814 dev_class[2] = BTM_COD_MINOR_JOYSTICK;
1815 break;
1816 case BTM_BLE_APPEARANCE_HID_GAMEPAD:
1817 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1818 dev_class[2] = BTM_COD_MINOR_GAMEPAD;
1819 break;
1820 case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
1821 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1822 dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
1823 break;
1824 case BTM_BLE_APPEARANCE_HID_CARD_READER:
1825 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1826 dev_class[2] = BTM_COD_MINOR_CARD_READER;
1827 break;
1828 case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
1829 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1830 dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
1831 break;
1832 case BTM_BLE_APPEARANCE_UKNOWN:
1833 case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
1834 case BTM_BLE_APPEARANCE_GENERIC_TAG:
1835 case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
1836 case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
1837 case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
1838 case BTM_BLE_APPEARANCE_CYCLING_SPEED:
1839 case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
1840 case BTM_BLE_APPEARANCE_CYCLING_POWER:
1841 case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
1842 case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
1843 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
1844 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
1845 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
1846 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
1847 default:
1848 dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
1849 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1850 };
Adam Hampsone6c74502014-05-30 15:07:08 -07001851}
1852
The Android Open Source Project5738f832012-12-12 16:00:35 -08001853/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001854 *
1855 * Function btm_ble_update_inq_result
1856 *
1857 * Description Update adv packet information into inquiry result.
1858 *
1859 * Parameters
1860 *
1861 * Returns void
1862 *
1863 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001864bool btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001865 uint8_t evt_type, uint8_t data_len,
1866 uint8_t* data, int8_t rssi) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001867 bool to_report = true;
1868 tBTM_INQ_RESULTS* p_cur = &p_i->inq_info.results;
1869 uint8_t len;
1870 uint8_t* p_flag;
1871 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
Myles Watson911d1ae2016-11-28 16:44:40 -08001872 tBTM_BLE_INQ_CB* p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
Myles Watson911d1ae2016-11-28 16:44:40 -08001873 uint8_t* p_uuid16;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001874
Myles Watson911d1ae2016-11-28 16:44:40 -08001875 if (data_len > BTM_BLE_ADV_DATA_LEN_MAX) {
1876 BTM_TRACE_WARNING("EIR data too long %d. discard", data_len);
1877 return false;
1878 }
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001879 btm_ble_cache_adv_data(p_cur, data_len, data, evt_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001880
Myles Watson911d1ae2016-11-28 16:44:40 -08001881 /* Save the info */
1882 p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
1883 p_cur->ble_addr_type = addr_type;
1884 p_cur->rssi = rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001885
Myles Watson911d1ae2016-11-28 16:44:40 -08001886 /* active scan, always wait until get scan_rsp to report the result */
1887 if ((btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
1888 (evt_type == BTM_BLE_CONNECT_EVT || evt_type == BTM_BLE_DISCOVER_EVT))) {
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001889 BTM_TRACE_DEBUG("%s: scan_rsp=false, to_report=false, scan_type_active=%d",
1890 __func__, btm_cb.ble_ctr_cb.inq_var.scan_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08001891 p_i->scan_rsp = false;
1892 to_report = false;
1893 } else
1894 p_i->scan_rsp = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001895
Myles Watson911d1ae2016-11-28 16:44:40 -08001896 if (p_i->inq_count != p_inq->inq_counter)
1897 p_cur->device_type = BT_DEVICE_TYPE_BLE;
1898 else
1899 p_cur->device_type |= BT_DEVICE_TYPE_BLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001900
Myles Watson911d1ae2016-11-28 16:44:40 -08001901 if (evt_type != BTM_BLE_SCAN_RSP_EVT) p_cur->ble_evt_type = evt_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001902
Myles Watson911d1ae2016-11-28 16:44:40 -08001903 p_i->inq_count = p_inq->inq_counter; /* Mark entry for current inquiry */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001904
Myles Watson911d1ae2016-11-28 16:44:40 -08001905 if (p_le_inq_cb->adv_len != 0) {
1906 p_flag = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache, BTM_BLE_AD_TYPE_FLAG,
1907 &len);
1908 if (p_flag != NULL) p_cur->flag = *p_flag;
1909 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001910
Myles Watson911d1ae2016-11-28 16:44:40 -08001911 if (p_le_inq_cb->adv_len != 0) {
1912 /* Check to see the BLE device has the Appearance UUID in the advertising
1913 * data. If it does
1914 * then try to convert the appearance value to a class of device value
1915 * Bluedroid can use.
1916 * Otherwise fall back to trying to infer if it is a HID device based on the
1917 * service class.
1918 */
1919 p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
1920 BTM_BLE_AD_TYPE_APPEARANCE, &len);
1921 if (p_uuid16 && len == 2) {
1922 btm_ble_appearance_to_cod((uint16_t)p_uuid16[0] | (p_uuid16[1] << 8),
1923 p_cur->dev_class);
1924 } else {
1925 p_uuid16 = BTM_CheckAdvData(p_le_inq_cb->adv_data_cache,
1926 BTM_BLE_AD_TYPE_16SRV_CMPL, &len);
1927 if (p_uuid16 != NULL) {
1928 uint8_t i;
1929 for (i = 0; i + 2 <= len; i = i + 2) {
1930 /* if this BLE device support HID over LE, set HID Major in class of
1931 * device */
1932 if ((p_uuid16[i] | (p_uuid16[i + 1] << 8)) == UUID_SERVCLASS_LE_HID) {
1933 p_cur->dev_class[0] = 0;
1934 p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1935 p_cur->dev_class[2] = 0;
1936 break;
1937 }
Adam Hampsone6c74502014-05-30 15:07:08 -07001938 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001939 }
Zhihai Xu1237ee32013-11-26 18:18:29 -08001940 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001941 }
Zhihai Xu1237ee32013-11-26 18:18:29 -08001942
Myles Watson911d1ae2016-11-28 16:44:40 -08001943 /* if BR/EDR not supported is not set, assume is a DUMO device */
1944 if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
1945 evt_type != BTM_BLE_CONNECT_DIR_EVT) {
1946 if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) {
1947 BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
1948 p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
1949 } else {
1950 BTM_TRACE_DEBUG("Random address, treating device as LE only");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001951 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001952 } else {
1953 BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
1954 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001955
Myles Watson911d1ae2016-11-28 16:44:40 -08001956 return to_report;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001957}
1958
1959/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001960 *
1961 * Function btm_clear_all_pending_le_entry
1962 *
1963 * Description This function is called to clear all LE pending entry in
1964 * inquiry database.
1965 *
1966 * Returns void
1967 *
1968 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001969void btm_clear_all_pending_le_entry(void) {
1970 uint16_t xx;
1971 tINQ_DB_ENT* p_ent = btm_cb.btm_inq_vars.inq_db;
Satya Calloji444a8da2015-03-06 10:38:22 -08001972
Myles Watson911d1ae2016-11-28 16:44:40 -08001973 for (xx = 0; xx < BTM_INQ_DB_SIZE; xx++, p_ent++) {
1974 /* mark all pending LE entry as unused if an LE only device has scan
1975 * response outstanding */
1976 if ((p_ent->in_use) &&
1977 (p_ent->inq_info.results.device_type == BT_DEVICE_TYPE_BLE) &&
1978 !p_ent->scan_rsp)
1979 p_ent->in_use = false;
1980 }
Satya Calloji444a8da2015-03-06 10:38:22 -08001981}
1982
Jakub Pawlowski801db302016-12-12 16:22:56 -08001983void btm_ble_process_adv_addr(BD_ADDR bda, uint8_t addr_type) {
1984#if (BLE_PRIVACY_SPT == TRUE)
1985 /* map address to security record */
1986 bool match = btm_identity_addr_to_random_pseudo(bda, &addr_type, false);
1987
1988 BTM_TRACE_DEBUG("%s: bda= %0x:%0x:%0x:%0x:%0x:%0x", __func__, bda[0], bda[1],
1989 bda[2], bda[3], bda[4], bda[5]);
1990 /* always do RRA resolution on host */
1991 if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) {
1992 tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda);
1993 if (match_rec) {
1994 match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
1995 memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
1996
1997 if (btm_ble_init_pseudo_addr(match_rec, bda)) {
1998 memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
1999 } else {
2000 // Assign the original address to be the current report address
2001 memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN);
2002 }
2003 }
2004 }
2005#endif
2006}
2007
2008/**
2009 * This function is called when extended advertising report event is received .
2010 * It updates the inquiry database. If the inquiry database is full, the oldest
2011 * entry is discarded.
2012 */
2013void btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) {
2014 BD_ADDR bda, direct_address;
2015 uint8_t* p = data;
2016 uint8_t addr_type, num_reports, pkt_data_len, primary_phy, secondary_phy,
2017 advertising_sid;
2018 int8_t rssi, tx_power;
2019 uint16_t event_type, periodic_adv_int, direct_address_type;
2020
2021 /* Only process the results if the inquiry is still active */
2022 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
2023
2024 /* Extract the number of reports in this event. */
2025 STREAM_TO_UINT8(num_reports, p);
2026
2027 while (num_reports--) {
2028 if (p > data + data_len) {
2029 // TODO(jpawlowski): we should crash the stack here
2030 BTM_TRACE_ERROR(
2031 "Malformed LE Extended Advertising Report Event from controller - "
2032 "can't loop the data");
2033 return;
2034 }
2035
2036 /* Extract inquiry results */
2037 STREAM_TO_UINT16(event_type, p);
2038 STREAM_TO_UINT8(addr_type, p);
2039 STREAM_TO_BDADDR(bda, p);
2040 STREAM_TO_UINT8(primary_phy, p);
2041 STREAM_TO_UINT8(secondary_phy, p);
2042 STREAM_TO_UINT8(advertising_sid, p);
2043 STREAM_TO_INT8(tx_power, p);
2044 STREAM_TO_INT8(rssi, p);
2045 STREAM_TO_UINT16(periodic_adv_int, p);
2046 STREAM_TO_UINT8(direct_address_type, p);
2047 STREAM_TO_BDADDR(direct_address, p);
2048 STREAM_TO_UINT8(pkt_data_len, p);
2049
2050 uint8_t* pkt_data = p;
2051 p += pkt_data_len; /* Advance to the the next packet*/
2052
2053 if (rssi >= 21 && rssi <= 126) {
2054 BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
2055 pkt_data_len, rssi);
2056 }
2057
2058 // we parse legacy packets only for now.
2059 if ((event_type & 0x0010) == 0) {
2060 continue;
2061 }
2062
2063 // TODO(jpawlowski): event type should be passed to
2064 // btm_ble_process_adv_pkt_cont. Legacy values should be transformed to new
2065 // value in btm_ble_process_adv_pkt
2066 uint8_t legacy_evt_type;
2067 if (event_type == 0x0013) {
2068 legacy_evt_type = 0x00; // ADV_IND;
2069 } else if (event_type == 0x0015) {
2070 legacy_evt_type = 0x01; // ADV_DIRECT_IND;
2071 } else if (event_type == 0x0012) {
2072 legacy_evt_type = 0x02; // ADV_SCAN_IND;
2073 } else if (event_type == 0x0010) {
2074 legacy_evt_type = 0x03; // ADV_NONCONN_IND;
2075 } else if (event_type == 0x001B) {
2076 legacy_evt_type = 0x02; // SCAN_RSP;
2077 } else if (event_type == 0x001A) {
2078 legacy_evt_type = 0x02; // SCAN_RSP;
2079 } else {
2080 BTM_TRACE_ERROR(
2081 "Malformed LE Advertising Report Event from controller - unsupported "
2082 "legacy event_type 0x%04x",
2083 event_type);
2084 return;
2085 }
2086
2087 btm_ble_process_adv_addr(bda, addr_type);
2088 btm_ble_process_adv_pkt_cont(bda, addr_type, legacy_evt_type, pkt_data_len,
2089 pkt_data, rssi);
2090 }
2091}
2092
2093/**
2094 * This function is called when advertising report event is received. It updates
2095 * the inquiry database. If the inquiry database is full, the oldest entry is
2096 * discarded.
2097 */
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002098void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08002099 BD_ADDR bda;
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002100 uint8_t* p = data;
2101 uint8_t evt_type, addr_type, num_reports, pkt_data_len;
2102 int8_t rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002103
Myles Watson911d1ae2016-11-28 16:44:40 -08002104 /* Only process the results if the inquiry is still active */
2105 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
Satya Calloji444a8da2015-03-06 10:38:22 -08002106
Myles Watson911d1ae2016-11-28 16:44:40 -08002107 /* Extract the number of reports in this event. */
2108 STREAM_TO_UINT8(num_reports, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002109
Myles Watson911d1ae2016-11-28 16:44:40 -08002110 while (num_reports--) {
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002111 if (p > data + data_len) {
2112 // TODO(jpawlowski): we should crash the stack here
2113 BTM_TRACE_ERROR("Malformed LE Advertising Report Event from controller");
2114 return;
2115 }
2116
Myles Watson911d1ae2016-11-28 16:44:40 -08002117 /* Extract inquiry results */
2118 STREAM_TO_UINT8(evt_type, p);
2119 STREAM_TO_UINT8(addr_type, p);
2120 STREAM_TO_BDADDR(bda, p);
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002121 STREAM_TO_UINT8(pkt_data_len, p);
2122
2123 uint8_t* pkt_data = p;
2124 p += pkt_data_len; /* Advance to the the rssi byte */
2125
2126 STREAM_TO_INT8(rssi, p);
2127
2128 if (rssi >= 21 && rssi <= 126) {
2129 BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
2130 pkt_data_len, rssi);
2131 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002132
Jakub Pawlowski801db302016-12-12 16:22:56 -08002133 btm_ble_process_adv_addr(bda, addr_type);
Jakub Pawlowski9462c5d2016-12-06 15:40:58 -08002134
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002135 btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pkt_data_len,
2136 pkt_data, rssi);
Myles Watson911d1ae2016-11-28 16:44:40 -08002137 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002138}
2139
2140/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002141 *
2142 * Function btm_ble_process_adv_pkt_cont
2143 *
2144 * Description This function is called after random address resolution is
2145 * done, and proceed to process adv packet.
2146 *
2147 * Parameters
2148 *
2149 * Returns void
2150 *
2151 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002152static void btm_ble_process_adv_pkt_cont(BD_ADDR bda, uint8_t addr_type,
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002153 uint8_t evt_type, uint8_t data_len,
2154 uint8_t* data, int8_t rssi) {
Myles Watson911d1ae2016-11-28 16:44:40 -08002155 tINQ_DB_ENT* p_i;
2156 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
2157 tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb;
2158 tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
2159 tBTM_BLE_INQ_CB* p_le_inq_cb = &btm_cb.ble_ctr_cb.inq_var;
2160 bool update = true;
2161 uint8_t result = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002162
Myles Watson911d1ae2016-11-28 16:44:40 -08002163 p_i = btm_inq_db_find(bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002164
Myles Watson911d1ae2016-11-28 16:44:40 -08002165 /* Check if this address has already been processed for this inquiry */
2166 if (btm_inq_find_bdaddr(bda)) {
2167 /* never been report as an LE device */
2168 if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
2169 /* scan repsonse to be updated */
2170 (!p_i->scan_rsp))) {
2171 update = true;
2172 } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
2173 update = false;
2174 } else {
2175 /* if yes, skip it */
2176 return; /* assumption: one result per event */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002177 }
Myles Watson911d1ae2016-11-28 16:44:40 -08002178 }
2179 /* If existing entry, use that, else get a new one (possibly reusing the
2180 * oldest) */
2181 if (p_i == NULL) {
2182 p_i = btm_inq_db_new(bda);
2183 if (p_i != NULL) {
2184 p_inq->inq_cmpl_info.num_resp++;
2185 } else
2186 return;
2187 } else if (p_i->inq_count !=
2188 p_inq->inq_counter) /* first time seen in this inquiry */
2189 {
2190 p_inq->inq_cmpl_info.num_resp++;
2191 }
2192 /* update the LE device information in inquiry database */
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002193 if (!btm_ble_update_inq_result(p_i, addr_type, evt_type, data_len, data,
2194 rssi))
2195 return;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002196
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002197 result = btm_ble_is_discoverable(bda, evt_type);
Myles Watson911d1ae2016-11-28 16:44:40 -08002198 if (result == 0) {
2199 LOG_WARN(LOG_TAG,
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002200 "%s device no longer discoverable, discarding advertising packet",
Myles Watson911d1ae2016-11-28 16:44:40 -08002201 __func__);
2202 return;
2203 }
2204 if (!update) result &= ~BTM_BLE_INQ_RESULT;
2205 /* If the number of responses found and limited, issue a cancel inquiry */
2206 if (p_inq->inqparms.max_resps &&
2207 p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps) {
2208 /* new device */
2209 if (p_i == NULL ||
2210 /* assume a DUMO device, BR/EDR inquiry is always active */
2211 (p_i &&
2212 (p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ==
2213 BT_DEVICE_TYPE_BLE &&
2214 p_i->scan_rsp)) {
2215 BTM_TRACE_WARNING(
2216 "INQ RES: Extra Response Received...cancelling inquiry..");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002217
Myles Watson911d1ae2016-11-28 16:44:40 -08002218 /* if is non-periodic inquiry active, cancel now */
2219 if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
2220 (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
2221 btsnd_hcic_inq_cancel();
The Android Open Source Project5738f832012-12-12 16:00:35 -08002222
Myles Watson911d1ae2016-11-28 16:44:40 -08002223 btm_ble_stop_inquiry();
The Android Open Source Project5738f832012-12-12 16:00:35 -08002224
Myles Watson911d1ae2016-11-28 16:44:40 -08002225 btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226 }
Myles Watson911d1ae2016-11-28 16:44:40 -08002227 }
Jakub Pawlowski83211b02016-12-07 11:25:15 -08002228
2229 if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {
2230 (p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
2231 p_le_inq_cb->adv_data_cache);
2232 }
2233 if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
2234 (p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
2235 p_le_inq_cb->adv_data_cache);
Myles Watson911d1ae2016-11-28 16:44:40 -08002236 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002237}
2238
2239/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002240 *
2241 * Function btm_ble_start_scan
2242 *
2243 * Description Start the BLE scan.
2244 *
2245 * Returns void
2246 *
2247 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002248tBTM_STATUS btm_ble_start_scan(void) {
2249 tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
Myles Watson911d1ae2016-11-28 16:44:40 -08002250 /* start scan, disable duplicate filtering */
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08002251 btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, p_inq->scan_duplicate_filter);
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002252
Myles Watson911d1ae2016-11-28 16:44:40 -08002253 if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI)
2254 btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
2255 else
2256 btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002257
Myles Watson911d1ae2016-11-28 16:44:40 -08002258 return BTM_CMD_STARTED;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002259}
2260
2261/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002262 *
2263 * Function btm_ble_stop_scan
2264 *
2265 * Description Stop the BLE scan.
2266 *
2267 * Returns void
2268 *
2269 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002270void btm_ble_stop_scan(void) {
2271 BTM_TRACE_EVENT("btm_ble_stop_scan ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002272
Myles Watson911d1ae2016-11-28 16:44:40 -08002273 /* Clear the inquiry callback if set */
2274 btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002275
Myles Watson911d1ae2016-11-28 16:44:40 -08002276 /* stop discovery now */
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08002277 btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002278
Myles Watson911d1ae2016-11-28 16:44:40 -08002279 btm_update_scanner_filter_policy(SP_ADV_ALL);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002280}
2281/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002282 *
2283 * Function btm_ble_stop_inquiry
2284 *
2285 * Description Stop the BLE Inquiry.
2286 *
2287 * Returns void
2288 *
2289 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002290void btm_ble_stop_inquiry(void) {
2291 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
2292 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002293
Myles Watson911d1ae2016-11-28 16:44:40 -08002294 alarm_cancel(p_ble_cb->inq_var.inquiry_timer);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002295
Myles Watson911d1ae2016-11-28 16:44:40 -08002296 p_ble_cb->scan_activity &= ~BTM_BLE_INQUIRY_MASK;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002297
Myles Watson911d1ae2016-11-28 16:44:40 -08002298 /* If no more scan activity, stop LE scan now */
2299 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
2300 btm_ble_stop_scan();
2301 else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) ||
2302 (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
2303 BTM_TRACE_DEBUG("%s: setting default params for ongoing observe", __func__);
2304 btm_ble_stop_scan();
2305 btm_ble_start_scan();
2306 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002307
Myles Watson911d1ae2016-11-28 16:44:40 -08002308 /* If we have a callback registered for inquiry complete, call it */
2309 BTM_TRACE_DEBUG("BTM Inq Compl Callback: status 0x%02x, num results %d",
2310 p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002311
Myles Watson911d1ae2016-11-28 16:44:40 -08002312 btm_process_inq_complete(
2313 HCI_SUCCESS, (uint8_t)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002314}
2315
2316/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002317 *
2318 * Function btm_ble_stop_observe
2319 *
2320 * Description Stop the BLE Observe.
2321 *
2322 * Returns void
2323 *
2324 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002325static void btm_ble_stop_observe(void) {
2326 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
2327 tBTM_CMPL_CB* p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002328
Myles Watson911d1ae2016-11-28 16:44:40 -08002329 alarm_cancel(p_ble_cb->observer_timer);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002330
Myles Watson911d1ae2016-11-28 16:44:40 -08002331 p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002332
Myles Watson911d1ae2016-11-28 16:44:40 -08002333 p_ble_cb->p_obs_results_cb = NULL;
2334 p_ble_cb->p_obs_cmpl_cb = NULL;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002335
Myles Watson911d1ae2016-11-28 16:44:40 -08002336 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_stop_scan();
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002337
Myles Watson911d1ae2016-11-28 16:44:40 -08002338 if (p_obs_cb)
2339 (p_obs_cb)((tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002340}
2341/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002342 *
2343 * Function btm_ble_adv_states_operation
2344 *
2345 * Description Set or clear adv states in topology mask
2346 *
2347 * Returns operation status. true if sucessful, false otherwise.
2348 *
2349 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002350typedef bool(BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
2351static bool btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR* p_handler,
2352 uint8_t adv_evt) {
2353 bool rt = false;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002354
Myles Watson911d1ae2016-11-28 16:44:40 -08002355 switch (adv_evt) {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002356 case BTM_BLE_CONNECT_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002357 rt = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
2358 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002359
Myles Watson911d1ae2016-11-28 16:44:40 -08002360 case BTM_BLE_NON_CONNECT_EVT:
2361 rt = (*p_handler)(BTM_BLE_STATE_NON_CONN_ADV_BIT);
2362 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002363 case BTM_BLE_CONNECT_DIR_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002364 rt = (*p_handler)(BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
2365 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002366
2367 case BTM_BLE_DISCOVER_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002368 rt = (*p_handler)(BTM_BLE_STATE_SCAN_ADV_BIT);
2369 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002370
Satya Calloji444a8da2015-03-06 10:38:22 -08002371 case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002372 rt = (*p_handler)(BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT);
2373 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08002374
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002375 default:
Myles Watson911d1ae2016-11-28 16:44:40 -08002376 BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
2377 break;
2378 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002379
Myles Watson911d1ae2016-11-28 16:44:40 -08002380 return rt;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002381}
2382
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002383/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002384 *
2385 * Function btm_ble_start_adv
2386 *
2387 * Description start the BLE advertising.
2388 *
2389 * Returns void
2390 *
2391 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002392tBTM_STATUS btm_ble_start_adv(void) {
2393 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002394
Myles Watson911d1ae2016-11-28 16:44:40 -08002395 if (!btm_ble_adv_states_operation(btm_ble_topology_check, p_cb->evt_type))
2396 return BTM_WRONG_MODE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002397
Marie Janssend19e0782016-07-15 12:48:27 -07002398#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08002399 /* To relax resolving list, always have resolving list enabled, unless
2400 * directed adv */
2401 if (p_cb->evt_type != BTM_BLE_CONNECT_LO_DUTY_DIR_EVT &&
2402 p_cb->evt_type != BTM_BLE_CONNECT_DIR_EVT)
2403 /* enable resolving list is desired */
2404 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_ADV);
Satya Calloji444a8da2015-03-06 10:38:22 -08002405#endif
Myles Watson911d1ae2016-11-28 16:44:40 -08002406 if (p_cb->afp != AP_SCAN_CONN_ALL) {
2407 btm_execute_wl_dev_operation();
2408 btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV;
2409 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002410
Myles Watson911d1ae2016-11-28 16:44:40 -08002411 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_ENABLE);
2412 p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
2413 btm_ble_adv_states_operation(btm_ble_set_topology_mask, p_cb->evt_type);
2414 return BTM_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002415}
Satya Calloji444a8da2015-03-06 10:38:22 -08002416
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002417/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002418 *
2419 * Function btm_ble_stop_adv
2420 *
2421 * Description Stop the BLE advertising.
2422 *
2423 * Returns void
2424 *
2425 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002426tBTM_STATUS btm_ble_stop_adv(void) {
2427 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002428
Myles Watson911d1ae2016-11-28 16:44:40 -08002429 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2430 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_DISABLE);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002431
Myles Watson911d1ae2016-11-28 16:44:40 -08002432 p_cb->fast_adv_on = false;
2433 p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
2434 btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002435
Myles Watson911d1ae2016-11-28 16:44:40 -08002436 /* clear all adv states */
2437 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2438 }
2439 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002440}
2441
Myles Watson911d1ae2016-11-28 16:44:40 -08002442static void btm_ble_fast_adv_timer_timeout(UNUSED_ATTR void* data) {
2443 /* fast adv is completed, fall back to slow adv interval */
2444 btm_ble_start_slow_adv();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002445}
2446
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002447/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002448 *
2449 * Function btm_ble_start_slow_adv
2450 *
2451 * Description Restart adv with slow adv interval
2452 *
2453 * Returns void
2454 *
2455 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002456static void btm_ble_start_slow_adv(void) {
2457 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002458
Myles Watson911d1ae2016-11-28 16:44:40 -08002459 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2460 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2461 BD_ADDR p_addr_ptr = {0};
2462 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
2463 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
Satya Calloji444a8da2015-03-06 10:38:22 -08002464
Myles Watson911d1ae2016-11-28 16:44:40 -08002465 btm_ble_stop_adv();
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002466
Myles Watson911d1ae2016-11-28 16:44:40 -08002467 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
2468 p_cb, p_addr_ptr, &init_addr_type, &own_addr_type);
Satya Calloji444a8da2015-03-06 10:38:22 -08002469
Myles Watson911d1ae2016-11-28 16:44:40 -08002470 /* slow adv mode never goes into directed adv */
2471 btsnd_hcic_ble_write_adv_params(BTM_BLE_GAP_ADV_SLOW_INT,
2472 BTM_BLE_GAP_ADV_SLOW_INT, p_cb->evt_type,
2473 own_addr_type, init_addr_type, p_addr_ptr,
2474 p_cb->adv_chnl_map, p_cb->afp);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002475
Myles Watson911d1ae2016-11-28 16:44:40 -08002476 btm_ble_start_adv();
2477 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002478}
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002479
Myles Watson911d1ae2016-11-28 16:44:40 -08002480static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(
2481 UNUSED_ATTR void* data) {
2482 /* lim_timeout expired, limited discovery should exit now */
2483 btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
2484 btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode,
2485 btm_cb.btm_inq_vars.discoverable_mode);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002486}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002487
Myles Watson911d1ae2016-11-28 16:44:40 -08002488static void btm_ble_inquiry_timer_timeout(UNUSED_ATTR void* data) {
2489 btm_ble_stop_inquiry();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002490}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002491
Myles Watson911d1ae2016-11-28 16:44:40 -08002492static void btm_ble_observer_timer_timeout(UNUSED_ATTR void* data) {
2493 btm_ble_stop_observe();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002494}
The Android Open Source Project5738f832012-12-12 16:00:35 -08002495
Myles Watson911d1ae2016-11-28 16:44:40 -08002496void btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) {
2497 if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
2498 /* refresh the random addr */
2499 btm_gen_resolvable_private_addr((void*)btm_gen_resolve_paddr_low);
2500 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002501}
2502
The Android Open Source Project5738f832012-12-12 16:00:35 -08002503/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002504 *
2505 * Function btm_ble_read_remote_features_complete
2506 *
2507 * Description This function is called when the command complete message
Myles Watson9ca07092016-11-28 16:41:53 -08002508 * is received from the HCI for the read LE remote feature
2509 * supported complete event.
Myles Watsonee96a3c2016-11-23 14:49:54 -08002510 *
2511 * Returns void
2512 *
2513 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002514void btm_ble_read_remote_features_complete(uint8_t* p) {
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302515 BTM_TRACE_EVENT("%s", __func__);
2516
Myles Watson911d1ae2016-11-28 16:44:40 -08002517 uint16_t handle;
2518 uint8_t status;
Myles Watson911d1ae2016-11-28 16:44:40 -08002519 STREAM_TO_UINT8(status, p);
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302520 STREAM_TO_UINT16(handle, p);
2521 handle = handle & 0x0FFF; // only 12 bits meaningful
Satya Calloji444a8da2015-03-06 10:38:22 -08002522
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302523 if (status != HCI_SUCCESS) {
2524 BTM_TRACE_ERROR("%s: failed for handle: 0x%04d", __func__, handle);
2525 return;
Myles Watson911d1ae2016-11-28 16:44:40 -08002526 }
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302527
2528 int idx = btm_handle_to_acl_index(handle);
2529 if (idx == MAX_L2CAP_LINKS) {
2530 BTM_TRACE_ERROR("%s: can't find acl for handle: 0x%04d", __func__, handle);
2531 return;
2532 }
2533
2534 STREAM_TO_ARRAY(btm_cb.acl_db[idx].peer_le_features, p, BD_FEATURES_LEN);
2535
2536 btsnd_hcic_rmt_ver_req(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002537}
2538
2539/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002540 *
2541 * Function btm_ble_write_adv_enable_complete
2542 *
2543 * Description This function process the write adv enable command complete.
2544 *
2545 * Returns void
2546 *
2547 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002548void btm_ble_write_adv_enable_complete(uint8_t* p) {
2549 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002550
Myles Watson911d1ae2016-11-28 16:44:40 -08002551 /* if write adv enable/disbale not succeed */
2552 if (*p != HCI_SUCCESS) {
2553 /* toggle back the adv mode */
2554 p_cb->adv_mode = !p_cb->adv_mode;
2555 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002556}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002557
2558/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002559 *
2560 * Function btm_ble_dir_adv_tout
2561 *
2562 * Description when directed adv time out
2563 *
2564 * Returns void
2565 *
2566 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002567void btm_ble_dir_adv_tout(void) {
2568 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002569
Myles Watson911d1ae2016-11-28 16:44:40 -08002570 /* make device fall back into undirected adv mode by default */
2571 btm_cb.ble_ctr_cb.inq_var.directed_conn = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002572}
2573
2574/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002575 *
2576 * Function btm_ble_set_topology_mask
2577 *
2578 * Description set BLE topology mask
2579 *
2580 * Returns true is request is allowed, false otherwise.
2581 *
2582 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002583bool btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
2584 request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2585 btm_cb.ble_ctr_cb.cur_states |= (request_state_mask & BTM_BLE_STATE_ALL_MASK);
2586 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08002587}
2588
2589/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002590 *
2591 * Function btm_ble_clear_topology_mask
2592 *
2593 * Description Clear BLE topology bit mask
2594 *
2595 * Returns true is request is allowed, false otherwise.
2596 *
2597 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002598bool btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
2599 request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2600 btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
2601 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08002602}
2603
2604/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002605 *
2606 * Function btm_ble_update_link_topology_mask
2607 *
2608 * Description This function update the link topology mask
2609 *
2610 * Returns void
2611 *
2612 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002613void btm_ble_update_link_topology_mask(uint8_t link_role, bool increase) {
2614 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_CONN_MASK);
Satya Calloji444a8da2015-03-06 10:38:22 -08002615
Myles Watson911d1ae2016-11-28 16:44:40 -08002616 if (increase)
2617 btm_cb.ble_ctr_cb.link_count[link_role]++;
2618 else if (btm_cb.ble_ctr_cb.link_count[link_role] > 0)
2619 btm_cb.ble_ctr_cb.link_count[link_role]--;
Satya Calloji444a8da2015-03-06 10:38:22 -08002620
Myles Watson911d1ae2016-11-28 16:44:40 -08002621 if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_MASTER])
2622 btm_ble_set_topology_mask(BTM_BLE_STATE_MASTER_BIT);
Satya Calloji444a8da2015-03-06 10:38:22 -08002623
Myles Watson911d1ae2016-11-28 16:44:40 -08002624 if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_SLAVE])
2625 btm_ble_set_topology_mask(BTM_BLE_STATE_SLAVE_BIT);
Satya Calloji444a8da2015-03-06 10:38:22 -08002626
Myles Watson911d1ae2016-11-28 16:44:40 -08002627 if (link_role == HCI_ROLE_SLAVE && increase) {
2628 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2629 /* make device fall back into undirected adv mode by default */
2630 btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
2631 /* clear all adv states */
2632 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2633 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002634}
2635
2636/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002637 *
2638 * Function btm_ble_update_mode_operation
2639 *
Myles Watson9ca07092016-11-28 16:41:53 -08002640 * Description This function update the GAP role operation when a link
2641 * status is updated.
Myles Watsonee96a3c2016-11-23 14:49:54 -08002642 *
2643 * Returns void
2644 *
2645 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002646void btm_ble_update_mode_operation(uint8_t link_role, BD_ADDR bd_addr,
2647 uint8_t status) {
2648 if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
2649 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2650 /* make device fall back into undirected adv mode by default */
2651 btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
2652 /* clear all adv states */
2653 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2654 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002655
Myles Watson911d1ae2016-11-28 16:44:40 -08002656 if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE) {
2657 btm_ble_set_connectability(btm_cb.btm_inq_vars.connectable_mode |
2658 btm_cb.ble_ctr_cb.inq_var.connectable_mode);
2659 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002660
Myles Watson911d1ae2016-11-28 16:44:40 -08002661 /* when no connection is attempted, and controller is not rejecting last
2662 request
2663 due to resource limitation, start next direct connection or background
2664 connection
2665 now in order */
2666 if (btm_ble_get_conn_st() == BLE_CONN_IDLE &&
2667 status != HCI_ERR_HOST_REJECT_RESOURCES &&
Emil Lenngrenaf7f49c2016-12-12 18:47:12 +00002668 status != HCI_ERR_MAX_NUM_OF_CONNECTIONS &&
Myles Watson911d1ae2016-11-28 16:44:40 -08002669 !btm_send_pending_direct_conn()) {
2670 btm_ble_resume_bg_conn();
2671 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002672}
2673
2674/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002675 *
2676 * Function btm_ble_init
2677 *
2678 * Description Initialize the control block variable values.
2679 *
2680 * Returns void
2681 *
2682 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002683void btm_ble_init(void) {
2684 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002685
Myles Watson911d1ae2016-11-28 16:44:40 -08002686 BTM_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002687
Myles Watson911d1ae2016-11-28 16:44:40 -08002688 alarm_free(p_cb->observer_timer);
2689 alarm_free(p_cb->inq_var.fast_adv_timer);
2690 memset(p_cb, 0, sizeof(tBTM_BLE_CB));
2691 memset(&(btm_cb.cmn_ble_vsc_cb), 0, sizeof(tBTM_BLE_VSC_CB));
2692 btm_cb.cmn_ble_vsc_cb.values_read = false;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002693
Myles Watson911d1ae2016-11-28 16:44:40 -08002694 p_cb->observer_timer = alarm_new("btm_ble.observer_timer");
2695 p_cb->cur_states = 0;
2696 p_cb->conn_pending_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002697
Myles Watson911d1ae2016-11-28 16:44:40 -08002698 p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2699 p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
2700 p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
2701 p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
2702 p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
2703 p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
2704 p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
2705 p_cb->inq_var.fast_adv_timer = alarm_new("btm_ble_inq.fast_adv_timer");
2706 p_cb->inq_var.inquiry_timer = alarm_new("btm_ble_inq.inquiry_timer");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002707
Myles Watson911d1ae2016-11-28 16:44:40 -08002708 /* for background connection, reset connection params to be undefined */
2709 p_cb->scan_int = p_cb->scan_win = BTM_BLE_SCAN_PARAM_UNDEF;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002710
Myles Watson911d1ae2016-11-28 16:44:40 -08002711 p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
Satya Callojic4e25962014-05-10 23:46:24 -07002712
Myles Watson911d1ae2016-11-28 16:44:40 -08002713 p_cb->addr_mgnt_cb.refresh_raddr_timer =
2714 alarm_new("btm_ble_addr.refresh_raddr_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002715
Marie Janssend19e0782016-07-15 12:48:27 -07002716#if (BLE_VND_INCLUDED == FALSE)
Myles Watson911d1ae2016-11-28 16:44:40 -08002717 btm_ble_adv_filter_init();
Satya Calloji1a9247a2014-06-05 13:15:15 -07002718#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002719}
2720
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002721/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002722 *
2723 * Function btm_ble_topology_check
2724 *
Myles Watson9ca07092016-11-28 16:41:53 -08002725 * Description check to see requested state is supported. One state check
2726 * at a time is supported
Myles Watsonee96a3c2016-11-23 14:49:54 -08002727 *
2728 * Returns true is request is allowed, false otherwise.
2729 *
2730 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002731bool btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask) {
2732 bool rt = false;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002733
Myles Watson911d1ae2016-11-28 16:44:40 -08002734 uint8_t state_offset = 0;
2735 uint16_t cur_states = btm_cb.ble_ctr_cb.cur_states;
2736 uint8_t mask, offset;
2737 uint8_t request_state = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002738
Myles Watson911d1ae2016-11-28 16:44:40 -08002739 /* check only one bit is set and within valid range */
2740 if (request_state_mask == BTM_BLE_STATE_INVALID ||
2741 request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
2742 (request_state_mask & (request_state_mask - 1)) != 0) {
2743 BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002744 return rt;
Myles Watson911d1ae2016-11-28 16:44:40 -08002745 }
2746
2747 while (request_state_mask) {
2748 request_state_mask >>= 1;
2749 request_state++;
2750 }
2751
2752 /* check if the requested state is supported or not */
2753 mask = btm_le_state_combo_tbl[0][request_state - 1][0];
2754 offset = btm_le_state_combo_tbl[0][request_state - 1][1];
2755
2756 const uint8_t* ble_supported_states =
2757 controller_get_interface()->get_ble_supported_states();
2758
2759 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, mask, offset)) {
2760 BTM_TRACE_ERROR("state requested not supported: %d", request_state);
2761 return rt;
2762 }
2763
2764 rt = true;
2765 /* make sure currently active states are all supported in conjunction with the
2766 requested
2767 state. If the bit in table is not set, the combination is not supported */
2768 while (cur_states != 0) {
2769 if (cur_states & 0x01) {
2770 mask = btm_le_state_combo_tbl[request_state][state_offset][0];
2771 offset = btm_le_state_combo_tbl[request_state][state_offset][1];
2772
2773 if (mask != 0 && offset != 0) {
2774 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, mask, offset)) {
2775 rt = false;
2776 break;
2777 }
2778 }
2779 }
2780 cur_states >>= 1;
2781 state_offset++;
2782 }
2783 return rt;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002784}