blob: 9630a846a499428b0b76c16deb9bed08fac7f192 [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
Jakub Pawlowski0c683232017-02-24 09:49:59 -080027#include <base/bind.h>
Jakub Pawlowski119f32a2017-02-16 11:56:56 -080028#include <base/callback.h>
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080029#include <base/strings/string_number_conversions.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <stddef.h>
Myles Watson911d1ae2016-11-28 16:44:40 -080031#include <stdio.h>
32#include <string.h>
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080033#include <list>
34#include <vector>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035
36#include "bt_types.h"
Mike J. Chen5cd8bff2014-01-31 18:16:59 -080037#include "bt_utils.h"
Wei Wang21015a42014-10-03 10:58:03 -070038#include "btm_ble_api.h"
Marie Janssen49a86702015-07-08 11:48:57 -070039#include "btm_int.h"
Wei Wang21015a42014-10-03 10:58:03 -070040#include "btu.h"
Chris Manton79ecab52014-10-31 14:54:51 -070041#include "device/include/controller.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include "gap_api.h"
Marie Janssen49a86702015-07-08 11:48:57 -070043#include "hcimsgs.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070044#include "osi/include/osi.h"
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -070045
Jakub Pawlowski103b2c42017-04-28 14:59:46 -070046#include "advertise_data_parser.h"
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070047#include "btm_ble_int.h"
Marie Janssen49a86702015-07-08 11:48:57 -070048#include "gatt_int.h"
49#include "gattdefs.h"
50#include "l2c_int.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080051#include "osi/include/log.h"
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -070052
Myles Watson911d1ae2016-11-28 16:44:40 -080053#define BTM_BLE_NAME_SHORT 0x01
54#define BTM_BLE_NAME_CMPL 0x02
The Android Open Source Project5738f832012-12-12 16:00:35 -080055
Myles Watson911d1ae2016-11-28 16:44:40 -080056#define BTM_BLE_FILTER_TARGET_UNKNOWN 0xff
57#define BTM_BLE_POLICY_UNKNOWN 0xff
The Android Open Source Project5738f832012-12-12 16:00:35 -080058
Myles Watson911d1ae2016-11-28 16:44:40 -080059#define BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS (30 * 1000)
60#define MIN_ADV_LENGTH 2
Satya Calloji935324a2015-03-31 13:24:32 -070061#define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE 9
62
Myles Watson911d1ae2016-11-28 16:44:40 -080063extern fixed_queue_t* btu_general_alarm_queue;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080064
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080065namespace {
66
67class AdvertisingCache {
68 public:
69 /* Set the data to |data| for device |addr_type, addr| */
Jakub Pawlowskia484a882017-06-24 17:30:18 -070070 const std::vector<uint8_t>& Set(uint8_t addr_type, const RawAddress& addr,
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080071 std::vector<uint8_t> data) {
72 auto it = Find(addr_type, addr);
73 if (it != items.end()) {
74 it->data = std::move(data);
75 return it->data;
76 }
77
78 if (items.size() > cache_max) {
79 items.pop_back();
80 }
81
82 items.emplace_front(addr_type, addr, std::move(data));
83 return items.front().data;
84 }
85
86 /* Append |data| for device |addr_type, addr| */
Jakub Pawlowskia484a882017-06-24 17:30:18 -070087 const std::vector<uint8_t>& Append(uint8_t addr_type, const RawAddress& addr,
Jakub Pawlowski0595ca02017-02-07 12:15:06 -080088 std::vector<uint8_t> data) {
89 auto it = Find(addr_type, addr);
90 if (it != items.end()) {
91 it->data.insert(it->data.end(), data.begin(), data.end());
92 return it->data;
93 }
94
95 if (items.size() > cache_max) {
96 items.pop_back();
97 }
98
99 items.emplace_front(addr_type, addr, std::move(data));
100 return items.front().data;
101 }
102
103 /* Clear data for device |addr_type, addr| */
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700104 void Clear(uint8_t addr_type, const RawAddress& addr) {
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800105 auto it = Find(addr_type, addr);
106 if (it != items.end()) {
107 items.erase(it);
108 }
109 }
110
111 private:
112 struct Item {
113 uint8_t addr_type;
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700114 RawAddress addr;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800115 std::vector<uint8_t> data;
116
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700117 Item(uint8_t addr_type, const RawAddress& addr, std::vector<uint8_t> data)
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700118 : addr_type(addr_type), addr(addr), data(data) {}
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800119 };
120
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700121 std::list<Item>::iterator Find(uint8_t addr_type, const RawAddress& addr) {
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800122 for (auto it = items.begin(); it != items.end(); it++) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700123 if (it->addr_type == addr_type && it->addr == addr) {
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800124 return it;
125 }
126 }
127 return items.end();
128 }
129
130 /* we keep maximum 7 devices in the cache */
131 const size_t cache_max = 7;
132 std::list<Item> items;
133};
134
135/* Devices in this cache are waiting for eiter scan response, or chained packets
136 * on secondary channel */
137AdvertisingCache cache;
138
139} // namespace
140
Marie Janssend19e0782016-07-15 12:48:27 -0700141#if (BLE_VND_INCLUDED == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800142static tBTM_BLE_CTRL_FEATURES_CBACK* p_ctrl_le_feature_rd_cmpl_cback = NULL;
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +0100143#endif
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700144
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800146 * Local functions
147 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700148static void btm_ble_update_adv_flag(uint8_t flag);
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -0800149static void btm_ble_process_adv_pkt_cont(
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700150 uint16_t evt_type, uint8_t addr_type, const RawAddress& bda,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700151 uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
152 int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
153 uint8_t* data);
Myles Watson911d1ae2016-11-28 16:44:40 -0800154static uint8_t btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB* p_cb,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700155 RawAddress& p_peer_addr_ptr,
Myles Watson911d1ae2016-11-28 16:44:40 -0800156 tBLE_ADDR_TYPE* p_peer_addr_type,
157 tBLE_ADDR_TYPE* p_own_addr_type);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700158static void btm_ble_stop_observe(void);
Myles Watson911d1ae2016-11-28 16:44:40 -0800159static void btm_ble_fast_adv_timer_timeout(void* data);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800160static void btm_ble_start_slow_adv(void);
Myles Watson911d1ae2016-11-28 16:44:40 -0800161static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(void* data);
162static void btm_ble_inquiry_timer_timeout(void* data);
163static void btm_ble_observer_timer_timeout(void* data);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -0800164
Myles Watson911d1ae2016-11-28 16:44:40 -0800165#define BTM_BLE_INQ_RESULT 0x01
166#define BTM_BLE_OBS_RESULT 0x02
The Android Open Source Project5738f832012-12-12 16:00:35 -0800167
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -0800168bool ble_evt_type_is_connectable(uint16_t evt_type) {
169 return evt_type & (1 << BLE_EVT_CONNECTABLE_BIT);
170}
171
172bool ble_evt_type_is_scannable(uint16_t evt_type) {
173 return evt_type & (1 << BLE_EVT_SCANNABLE_BIT);
174}
175
176bool ble_evt_type_is_directed(uint16_t evt_type) {
177 return evt_type & (1 << BLE_EVT_DIRECTED_BIT);
178}
179
180bool ble_evt_type_is_scan_resp(uint16_t evt_type) {
181 return evt_type & (1 << BLE_EVT_SCAN_RESPONSE_BIT);
182}
183
184bool ble_evt_type_is_legacy(uint16_t evt_type) {
185 return evt_type & (1 << BLE_EVT_LEGACY_BIT);
186}
187
Jakub Pawlowski0595ca02017-02-07 12:15:06 -0800188uint8_t ble_evt_type_data_status(uint16_t evt_type) {
189 return (evt_type >> 5) & 3;
190}
191
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700192constexpr uint8_t UNSUPPORTED = 255;
193
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700194/* LE states combo bit to check */
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700195const uint8_t btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX] = {
196 {
197 /* single state support */
198 HCI_LE_STATES_CONN_ADV_BIT, /* conn_adv */
199 HCI_LE_STATES_INIT_BIT, /* init */
200 HCI_LE_STATES_INIT_BIT, /* master */
201 HCI_LE_STATES_SLAVE_BIT, /* slave */
202 UNSUPPORTED, /* todo: lo du dir adv, not covered ? */
203 HCI_LE_STATES_HI_DUTY_DIR_ADV_BIT, /* hi duty dir adv */
204 HCI_LE_STATES_NON_CONN_ADV_BIT, /* non connectable adv */
205 HCI_LE_STATES_PASS_SCAN_BIT, /* passive scan */
206 HCI_LE_STATES_ACTIVE_SCAN_BIT, /* active scan */
207 HCI_LE_STATES_SCAN_ADV_BIT /* scanable adv */
208 },
209 {
210 /* conn_adv =0 */
211 UNSUPPORTED, /* conn_adv */
212 HCI_LE_STATES_CONN_ADV_INIT_BIT, /* init: 32 */
213 HCI_LE_STATES_CONN_ADV_MASTER_BIT, /* master: 35 */
214 HCI_LE_STATES_CONN_ADV_SLAVE_BIT, /* slave: 38,*/
215 UNSUPPORTED, /* lo du dir adv */
216 UNSUPPORTED, /* hi duty dir adv */
217 UNSUPPORTED, /* non connectable adv */
218 HCI_LE_STATES_CONN_ADV_PASS_SCAN_BIT, /* passive scan */
219 HCI_LE_STATES_CONN_ADV_ACTIVE_SCAN_BIT, /* active scan */
220 UNSUPPORTED /* scanable adv */
221 },
222 {
223 /* init */
224 HCI_LE_STATES_CONN_ADV_INIT_BIT, /* conn_adv: 32 */
225 UNSUPPORTED, /* init */
226 HCI_LE_STATES_INIT_MASTER_BIT, /* master 28 */
227 HCI_LE_STATES_INIT_MASTER_SLAVE_BIT, /* slave 41 */
228 HCI_LE_STATES_LO_DUTY_DIR_ADV_INIT_BIT, /* lo du dir adv 34 */
229 HCI_LE_STATES_HI_DUTY_DIR_ADV_INIT_BIT, /* hi duty dir adv 33 */
230 HCI_LE_STATES_NON_CONN_INIT_BIT, /* non connectable adv */
231 HCI_LE_STATES_PASS_SCAN_INIT_BIT, /* passive scan */
232 HCI_LE_STATES_ACTIVE_SCAN_INIT_BIT, /* active scan */
233 HCI_LE_STATES_SCAN_ADV_INIT_BIT /* scanable adv */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700234
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700235 },
236 {
237 /* master */
238 HCI_LE_STATES_CONN_ADV_MASTER_BIT, /* conn_adv: 35 */
239 HCI_LE_STATES_INIT_MASTER_BIT, /* init 28 */
240 HCI_LE_STATES_INIT_MASTER_BIT, /* master 28 */
241 HCI_LE_STATES_CONN_ADV_INIT_BIT, /* slave: 32 */
242 HCI_LE_STATES_LO_DUTY_DIR_ADV_MASTER_BIT, /* lo duty cycle adv 37 */
243 HCI_LE_STATES_HI_DUTY_DIR_ADV_MASTER_BIT, /* hi duty cycle adv 36 */
244 HCI_LE_STATES_NON_CONN_ADV_MASTER_BIT, /* non connectable adv*/
245 HCI_LE_STATES_PASS_SCAN_MASTER_BIT, /* passive scan */
246 HCI_LE_STATES_ACTIVE_SCAN_MASTER_BIT, /* active scan */
247 HCI_LE_STATES_SCAN_ADV_MASTER_BIT /* scanable adv */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700248
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700249 },
250 {
251 /* slave */
252 HCI_LE_STATES_CONN_ADV_SLAVE_BIT, /* conn_adv: 38,*/
253 HCI_LE_STATES_INIT_MASTER_SLAVE_BIT, /* init 41 */
254 HCI_LE_STATES_INIT_MASTER_SLAVE_BIT, /* master 41 */
255 HCI_LE_STATES_CONN_ADV_SLAVE_BIT, /* slave: 38,*/
256 HCI_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_BIT, /* lo duty cycle adv 40 */
257 HCI_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_BIT, /* hi duty cycle adv 39 */
258 HCI_LE_STATES_NON_CONN_ADV_SLAVE_BIT, /* non connectable adv */
259 HCI_LE_STATES_PASS_SCAN_SLAVE_BIT, /* passive scan */
260 HCI_LE_STATES_ACTIVE_SCAN_SLAVE_BIT, /* active scan */
261 HCI_LE_STATES_SCAN_ADV_SLAVE_BIT /* scanable adv */
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700262
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700263 },
264 {
265 /* lo duty cycle adv */
266 UNSUPPORTED, /* conn_adv: 38,*/
267 HCI_LE_STATES_LO_DUTY_DIR_ADV_INIT_BIT, /* init 34 */
268 HCI_LE_STATES_LO_DUTY_DIR_ADV_MASTER_BIT, /* master 37 */
269 HCI_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_BIT, /* slave: 40 */
270 UNSUPPORTED, /* lo duty cycle adv 40 */
271 UNSUPPORTED, /* hi duty cycle adv 39 */
272 UNSUPPORTED, /* non connectable adv */
273 UNSUPPORTED, /* TODO: passive scan, not covered? */
274 UNSUPPORTED, /* TODO: active scan, not covered? */
275 UNSUPPORTED /* scanable adv */
276 },
277 {
278 /* hi duty cycle adv */
279 UNSUPPORTED, /* conn_adv: 38,*/
280 HCI_LE_STATES_HI_DUTY_DIR_ADV_INIT_BIT, /* init 33 */
281 HCI_LE_STATES_HI_DUTY_DIR_ADV_MASTER_BIT, /* master 36 */
282 HCI_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_BIT, /* slave: 39*/
283 UNSUPPORTED, /* lo duty cycle adv 40 */
284 UNSUPPORTED, /* hi duty cycle adv 39 */
285 UNSUPPORTED, /* non connectable adv */
286 HCI_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_BIT, /* passive scan */
287 HCI_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_BIT, /* active scan */
288 UNSUPPORTED /* scanable adv */
289 },
290 {
291 /* non connectable adv */
292 UNSUPPORTED, /* conn_adv: */
293 HCI_LE_STATES_NON_CONN_INIT_BIT, /* init */
294 HCI_LE_STATES_NON_CONN_ADV_MASTER_BIT, /* master */
295 HCI_LE_STATES_NON_CONN_ADV_SLAVE_BIT, /* slave: */
296 UNSUPPORTED, /* lo duty cycle adv */
297 UNSUPPORTED, /* hi duty cycle adv */
298 UNSUPPORTED, /* non connectable adv */
299 HCI_LE_STATES_NON_CONN_ADV_PASS_SCAN_BIT, /* passive scan */
300 HCI_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_BIT, /* active scan */
301 UNSUPPORTED /* scanable adv */
302 },
303 {
304 /* passive scan */
305 HCI_LE_STATES_CONN_ADV_PASS_SCAN_BIT, /* conn_adv: */
306 HCI_LE_STATES_PASS_SCAN_INIT_BIT, /* init */
307 HCI_LE_STATES_PASS_SCAN_MASTER_BIT, /* master */
308 HCI_LE_STATES_PASS_SCAN_SLAVE_BIT, /* slave: */
309 UNSUPPORTED, /* lo duty cycle adv */
310 HCI_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_BIT, /* hi duty cycle adv */
311 HCI_LE_STATES_NON_CONN_ADV_PASS_SCAN_BIT, /* non connectable adv */
312 UNSUPPORTED, /* passive scan */
313 UNSUPPORTED, /* active scan */
314 HCI_LE_STATES_SCAN_ADV_PASS_SCAN_BIT /* scanable adv */
315 },
316 {
317 /* active scan */
318 HCI_LE_STATES_CONN_ADV_ACTIVE_SCAN_BIT, /* conn_adv: */
319 HCI_LE_STATES_ACTIVE_SCAN_INIT_BIT, /* init */
320 HCI_LE_STATES_ACTIVE_SCAN_MASTER_BIT, /* master */
321 HCI_LE_STATES_ACTIVE_SCAN_SLAVE_BIT, /* slave: */
322 UNSUPPORTED, /* lo duty cycle adv */
323 HCI_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_BIT, /* hi duty cycle adv */
324 HCI_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_BIT, /* non connectable adv */
325 UNSUPPORTED, /* TODO: passive scan */
326 UNSUPPORTED, /* TODO: active scan */
327 HCI_LE_STATES_SCAN_ADV_ACTIVE_SCAN_BIT /* scanable adv */
328 },
329 {
330 /* scanable adv */
331 UNSUPPORTED, /* conn_adv: */
332 HCI_LE_STATES_SCAN_ADV_INIT_BIT, /* init */
333 HCI_LE_STATES_SCAN_ADV_MASTER_BIT, /* master */
334 HCI_LE_STATES_SCAN_ADV_SLAVE_BIT, /* slave: */
335 UNSUPPORTED, /* lo duty cycle adv */
336 UNSUPPORTED, /* hi duty cycle adv */
337 UNSUPPORTED, /* non connectable adv */
338 HCI_LE_STATES_SCAN_ADV_PASS_SCAN_BIT, /* passive scan */
339 HCI_LE_STATES_SCAN_ADV_ACTIVE_SCAN_BIT, /* active scan */
340 UNSUPPORTED /* scanable adv */
341 }};
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700342
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700343/* check LE combo state supported */
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700344inline bool BTM_LE_STATES_SUPPORTED(const uint8_t* x, uint8_t bit_num) {
345 uint8_t mask = 1 << (bit_num % 8);
346 uint8_t offset = bit_num / 8;
347 return ((x)[offset] & mask);
348}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700349
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800350/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800351 *
352 * Function BTM_BleUpdateAdvFilterPolicy
353 *
354 * Description This function update the filter policy of advertiser.
355 *
356 * Parameter adv_policy: advertising filter policy
357 *
358 * Return void
359 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800360void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) {
361 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
362 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700363 RawAddress adv_address = RawAddress::kEmpty;
Myles Watson911d1ae2016-11-28 16:44:40 -0800364 uint8_t adv_mode = p_cb->adv_mode;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800365
Myles Watson911d1ae2016-11-28 16:44:40 -0800366 BTM_TRACE_EVENT("BTM_BleUpdateAdvFilterPolicy");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800367
Myles Watson911d1ae2016-11-28 16:44:40 -0800368 if (!controller_get_interface()->supports_ble()) return;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700369
Myles Watson911d1ae2016-11-28 16:44:40 -0800370 if (p_cb->afp != adv_policy) {
371 p_cb->afp = adv_policy;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800372
Myles Watson911d1ae2016-11-28 16:44:40 -0800373 /* if adv active, stop and restart */
374 btm_ble_stop_adv();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800375
Myles Watson911d1ae2016-11-28 16:44:40 -0800376 if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
377 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700378 p_cb, adv_address, &init_addr_type, &p_cb->adv_addr_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800379
Myles Watson911d1ae2016-11-28 16:44:40 -0800380 btsnd_hcic_ble_write_adv_params(
381 (uint16_t)(p_cb->adv_interval_min ? p_cb->adv_interval_min
382 : BTM_BLE_GAP_ADV_SLOW_INT),
383 (uint16_t)(p_cb->adv_interval_max ? p_cb->adv_interval_max
384 : BTM_BLE_GAP_ADV_SLOW_INT),
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700385 p_cb->evt_type, p_cb->adv_addr_type, init_addr_type, adv_address,
Myles Watson911d1ae2016-11-28 16:44:40 -0800386 p_cb->adv_chnl_map, p_cb->afp);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800387
Myles Watson911d1ae2016-11-28 16:44:40 -0800388 if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
389 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800390}
Satya Calloji935324a2015-03-31 13:24:32 -0700391
392/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800393 *
Myles Watsonee96a3c2016-11-23 14:49:54 -0800394 * Function BTM_BleObserve
395 *
396 * Description This procedure keep the device listening for advertising
397 * events from a broadcast device.
398 *
399 * Parameters start: start or stop observe.
400 * white_list: use white list in observer mode or not.
401 *
402 * Returns void
403 *
404 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800405tBTM_STATUS BTM_BleObserve(bool start, uint8_t duration,
406 tBTM_INQ_RESULTS_CB* p_results_cb,
407 tBTM_CMPL_CB* p_cmpl_cb) {
408 tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
409 tBTM_STATUS status = BTM_WRONG_MODE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800410
Myles Watson911d1ae2016-11-28 16:44:40 -0800411 uint32_t scan_interval =
412 !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
413 uint32_t scan_window =
414 !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
Satya Calloji935324a2015-03-31 13:24:32 -0700415
Myles Watson911d1ae2016-11-28 16:44:40 -0800416 BTM_TRACE_EVENT("%s : scan_type:%d, %d, %d", __func__,
417 btm_cb.btm_inq_vars.scan_type, p_inq->scan_interval,
418 p_inq->scan_window);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800419
Myles Watson911d1ae2016-11-28 16:44:40 -0800420 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700421
Myles Watson911d1ae2016-11-28 16:44:40 -0800422 if (start) {
423 /* shared inquiry database, do not allow observe if any inquiry is active */
424 if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
425 BTM_TRACE_ERROR("%s Observe Already Active", __func__);
426 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800427 }
428
Myles Watson911d1ae2016-11-28 16:44:40 -0800429 btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
430 btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
431 status = BTM_CMD_STARTED;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700432
Myles Watson911d1ae2016-11-28 16:44:40 -0800433 /* scan is not started */
434 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
435 /* allow config of scan type */
436 p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE)
437 ? BTM_BLE_SCAN_MODE_ACTI
438 : p_inq->scan_type;
439/* assume observe always not using white list */
440#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == true)
441 /* enable resolving list */
442 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
443#endif
444
Jakub Pawlowskidefbb912017-02-10 15:24:00 -0800445 btm_send_hci_set_scan_params(
446 p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window,
447 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, BTM_BLE_DEFAULT_SFP);
Myles Watson911d1ae2016-11-28 16:44:40 -0800448
449 p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
450 status = btm_ble_start_scan();
451 }
452
453 if (status == BTM_CMD_STARTED) {
454 btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
455 if (duration != 0) {
456 /* start observer timer */
457 period_ms_t duration_ms = duration * 1000;
458 alarm_set_on_queue(btm_cb.ble_ctr_cb.observer_timer, duration_ms,
459 btm_ble_observer_timer_timeout, NULL,
460 btu_general_alarm_queue);
461 }
462 }
463 } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
464 status = BTM_CMD_STARTED;
465 btm_ble_stop_observe();
466 } else {
467 BTM_TRACE_ERROR("%s Observe not active", __func__);
468 }
469
470 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800471}
472
Marie Janssend19e0782016-07-15 12:48:27 -0700473#if (BLE_VND_INCLUDED == TRUE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800474/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800475 *
476 * Function btm_vsc_brcm_features_complete
477 *
478 * Description Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
479 *
480 * Returns void
481 *
482 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800483static void btm_ble_vendor_capability_vsc_cmpl_cback(
484 tBTM_VSC_CMPL* p_vcs_cplt_params) {
485 uint8_t status = 0xFF;
486 uint8_t* p;
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700487
Myles Watson911d1ae2016-11-28 16:44:40 -0800488 BTM_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700489
Myles Watson911d1ae2016-11-28 16:44:40 -0800490 /* Check status of command complete event */
491 if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) &&
492 (p_vcs_cplt_params->param_len > 0)) {
493 p = p_vcs_cplt_params->p_param_buf;
494 STREAM_TO_UINT8(status, p);
495 }
496
497 if (status == HCI_SUCCESS) {
498 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
499 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
500 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
501 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
502 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.filter_support, p);
503 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_filter, p);
504 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.energy_support, p);
505
506 if (p_vcs_cplt_params->param_len >
507 BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE) {
508 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.version_supported, p);
509 } else {
510 btm_cb.cmn_ble_vsc_cb.version_supported =
511 BTM_VSC_CHIP_CAPABILITY_L_VERSION;
Wei Wanga6ce7752014-05-20 06:30:32 +0000512 }
513
Myles Watson911d1ae2016-11-28 16:44:40 -0800514 if (btm_cb.cmn_ble_vsc_cb.version_supported >=
515 BTM_VSC_CHIP_CAPABILITY_M_VERSION) {
516 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.total_trackable_advertisers, p);
517 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.extended_scan_support, p);
518 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.debug_logging_supported, p);
Wei Wanga6ce7752014-05-20 06:30:32 +0000519 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800520 btm_cb.cmn_ble_vsc_cb.values_read = true;
521 }
Satya Callojic4e25962014-05-10 23:46:24 -0700522
Myles Watson911d1ae2016-11-28 16:44:40 -0800523 BTM_TRACE_DEBUG(
524 "%s: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d, ext_scan=%d", __func__,
525 status, btm_cb.cmn_ble_vsc_cb.max_irk_list_sz,
526 btm_cb.cmn_ble_vsc_cb.adv_inst_max, btm_cb.cmn_ble_vsc_cb.rpa_offloading,
527 btm_cb.cmn_ble_vsc_cb.energy_support,
528 btm_cb.cmn_ble_vsc_cb.extended_scan_support);
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700529
Jakub Pawlowski22552502016-12-22 03:13:00 -0800530 btm_ble_adv_init();
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700531
Myles Watson911d1ae2016-11-28 16:44:40 -0800532 if (btm_cb.cmn_ble_vsc_cb.max_filter > 0) btm_ble_adv_filter_init();
Satya Callojid5aa2472014-09-23 18:27:09 -0700533
Marie Janssend19e0782016-07-15 12:48:27 -0700534#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800535 /* VS capability included and non-4.2 device */
536 if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0 &&
537 controller_get_interface()->get_ble_resolving_list_max_size() == 0)
538 btm_ble_resolving_list_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
539#endif /* (BLE_PRIVACY_SPT == TRUE) */
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700540
Myles Watson911d1ae2016-11-28 16:44:40 -0800541 if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_init();
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700542
Myles Watson911d1ae2016-11-28 16:44:40 -0800543 if (p_ctrl_le_feature_rd_cmpl_cback != NULL)
544 p_ctrl_le_feature_rd_cmpl_cback(status);
Satya Calloji935324a2015-03-31 13:24:32 -0700545}
Myles Watson911d1ae2016-11-28 16:44:40 -0800546#endif /* (BLE_VND_INCLUDED == TRUE) */
Wei Wanga6ce7752014-05-20 06:30:32 +0000547
548/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800549 *
550 * Function BTM_BleGetVendorCapabilities
551 *
552 * Description This function reads local LE features
553 *
554 * Parameters p_cmn_vsc_cb : Locala LE capability structure
555 *
556 * Returns void
557 *
558 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800559extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB* p_cmn_vsc_cb) {
560 BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700561
Myles Watson911d1ae2016-11-28 16:44:40 -0800562 if (NULL != p_cmn_vsc_cb) {
563 *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
564 }
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700565}
566
567/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800568 *
569 * Function BTM_BleReadControllerFeatures
570 *
571 * Description Reads BLE specific controller features
572 *
Myles Watson9ca07092016-11-28 16:41:53 -0800573 * Parameters: tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when
574 * features are read
Myles Watsonee96a3c2016-11-23 14:49:54 -0800575 *
576 * Returns void
577 *
578 ******************************************************************************/
Myles Watsond628a062016-10-27 10:02:37 -0700579#if (BLE_VND_INCLUDED == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800580extern void BTM_BleReadControllerFeatures(
581 tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {
582 if (true == btm_cb.cmn_ble_vsc_cb.values_read) return;
Satya Calloji3f24f462014-09-16 22:44:43 -0700583
Myles Watson911d1ae2016-11-28 16:44:40 -0800584 BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700585
Myles Watson911d1ae2016-11-28 16:44:40 -0800586 p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
587 BTM_VendorSpecificCommand(HCI_BLE_VENDOR_CAP_OCF, 0, NULL,
588 btm_ble_vendor_capability_vsc_cmpl_cback);
Wei Wanga6ce7752014-05-20 06:30:32 +0000589}
Myles Watsond628a062016-10-27 10:02:37 -0700590#else
Myles Watson911d1ae2016-11-28 16:44:40 -0800591extern void BTM_BleReadControllerFeatures(
592 UNUSED_ATTR tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {}
Myles Watsond628a062016-10-27 10:02:37 -0700593#endif
Wei Wanga6ce7752014-05-20 06:30:32 +0000594
595/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800596 *
597 * Function BTM_BleEnableMixedPrivacyMode
598 *
599 * Description This function is called to enabled Mixed mode if privacy 1.2
600 * is applicable in controller.
601 *
602 * Parameters mixed_on: mixed mode to be used or not.
603 *
604 * Returns void
605 *
606 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800607void BTM_BleEnableMixedPrivacyMode(bool mixed_on) {
Marie Janssend19e0782016-07-15 12:48:27 -0700608#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800609 btm_cb.ble_ctr_cb.mixed_mode = mixed_on;
Satya Calloji444a8da2015-03-06 10:38:22 -0800610
Myles Watson911d1ae2016-11-28 16:44:40 -0800611/* TODO: send VSC to enabled mixed mode */
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612#endif
613}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800614
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700615/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800616 *
617 * Function BTM_BleConfigPrivacy
618 *
619 * Description This function is called to enable or disable the privacy in
620 * LE channel of the local device.
621 *
622 * Parameters privacy_mode: privacy mode on or off.
623 *
624 * Returns bool privacy mode set success; otherwise failed.
625 *
626 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800627bool BTM_BleConfigPrivacy(bool privacy_mode) {
Marie Janssend19e0782016-07-15 12:48:27 -0700628#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800629 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700630
Myles Watson911d1ae2016-11-28 16:44:40 -0800631 BTM_TRACE_EVENT("%s", __func__);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700632
Myles Watson911d1ae2016-11-28 16:44:40 -0800633 /* if LE is not supported, return error */
634 if (!controller_get_interface()->supports_ble()) return false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800635
Myles Watson911d1ae2016-11-28 16:44:40 -0800636 uint8_t addr_resolution = 0;
637 if (!privacy_mode) /* if privacy disabled, always use public address */
638 {
639 p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
640 p_cb->privacy_mode = BTM_PRIVACY_NONE;
641 } else /* privacy is turned on*/
642 {
643 /* always set host random address, used when privacy 1.1 or priavcy 1.2 is
644 * disabled */
645 p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
Jakub Pawlowski0c683232017-02-24 09:49:59 -0800646 btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700647
Myles Watson911d1ae2016-11-28 16:44:40 -0800648 /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private
649 * address in controller */
650 if (controller_get_interface()->supports_ble_privacy()) {
651 addr_resolution = 1;
652 /* check vendor specific capability */
653 p_cb->privacy_mode =
654 btm_cb.ble_ctr_cb.mixed_mode ? BTM_PRIVACY_MIXED : BTM_PRIVACY_1_2;
655 } else /* 4.1/4.0 controller */
656 p_cb->privacy_mode = BTM_PRIVACY_1_1;
657 }
Satya Calloji444a8da2015-03-06 10:38:22 -0800658
Myles Watson911d1ae2016-11-28 16:44:40 -0800659 GAP_BleAttrDBUpdate(GATT_UUID_GAP_CENTRAL_ADDR_RESOL,
660 (tGAP_BLE_ATTR_VALUE*)&addr_resolution);
Satya Calloji444a8da2015-03-06 10:38:22 -0800661
Myles Watson911d1ae2016-11-28 16:44:40 -0800662 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800663#else
Myles Watson911d1ae2016-11-28 16:44:40 -0800664 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800665#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700666}
667
Ganesh Ganapathi Batta8d416912014-05-30 16:28:00 -0700668/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800669 *
670 * Function BTM_BleMaxMultiAdvInstanceCount
671 *
Myles Watson9ca07092016-11-28 16:41:53 -0800672 * Description Returns max number of multi adv instances supported by
673 * controller
Myles Watsonee96a3c2016-11-23 14:49:54 -0800674 *
675 * Returns Max multi adv instance count
676 *
677 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800678extern uint8_t BTM_BleMaxMultiAdvInstanceCount(void) {
679 return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX
680 ? btm_cb.cmn_ble_vsc_cb.adv_inst_max
681 : BTM_BLE_MULTI_ADV_MAX;
Prerepa Viswanadham16fe0822014-08-07 11:38:06 -0700682}
683
The Android Open Source Project5738f832012-12-12 16:00:35 -0800684/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800685 *
686 * Function BTM_BleLocalPrivacyEnabled
687 *
688 * Description Checks if local device supports private address
689 *
690 * Returns Return true if local privacy is enabled else false
691 *
692 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800693bool BTM_BleLocalPrivacyEnabled(void) {
Marie Janssend19e0782016-07-15 12:48:27 -0700694#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800695 return (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE);
Sharvil Nanavati26112632014-06-07 00:56:14 -0700696#else
Myles Watson911d1ae2016-11-28 16:44:40 -0800697 return false;
Sharvil Nanavati26112632014-06-07 00:56:14 -0700698#endif
699}
700
Jakub Pawlowski83211b02016-12-07 11:25:15 -0800701/**
702 * Set BLE connectable mode to auto connect
703 */
704void BTM_BleStartAutoConn() {
705 BTM_TRACE_EVENT("%s", __func__);
706 if (!controller_get_interface()->supports_ble()) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800707
Jakub Pawlowski83211b02016-12-07 11:25:15 -0800708 if (btm_cb.ble_ctr_cb.bg_conn_type != BTM_BLE_CONN_AUTO) {
709 btm_ble_start_auto_conn(true);
710 btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_AUTO;
Myles Watson911d1ae2016-11-28 16:44:40 -0800711 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800712}
713
714/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800715 *
716 * Function BTM_BleClearBgConnDev
717 *
718 * Description This function is called to clear the whitelist,
719 * end any pending whitelist connections,
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700720 * and reset the local bg device list.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800721 *
722 * Parameters void
723 *
724 * Returns void
725 *
726 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800727void BTM_BleClearBgConnDev(void) {
728 btm_ble_start_auto_conn(false);
729 btm_ble_clear_white_list();
730 gatt_reset_bgdev_list();
Nitin Arora021e17a2014-01-29 19:18:39 -0800731}
732
733/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800734 *
735 * Function BTM_BleUpdateBgConnDev
736 *
737 * Description This function is called to add or remove a device into/from
738 * background connection procedure. The background connection
Jakub Pawlowski5e43c802017-06-26 17:22:35 -0700739 * procedure is decided by the background connection type, it
740 * can be auto connection, or selective connection.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800741 *
742 * Parameters add_remove: true to add; false to remove.
743 * remote_bda: device address to add/remove.
744 *
745 * Returns void
746 *
747 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700748bool BTM_BleUpdateBgConnDev(bool add_remove, const RawAddress& remote_bda) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800749 BTM_TRACE_EVENT("%s() add=%d", __func__, add_remove);
750 return btm_update_dev_to_white_list(add_remove, remote_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751}
752
753/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800754 *
755 * Function BTM_BleSetConnectableMode
756 *
757 * Description This function is called to set BLE connectable mode for a
758 * peripheral device.
759 *
Myles Watson9ca07092016-11-28 16:41:53 -0800760 * Parameters conn_mode: directed connectable mode, or non-directed. It
761 * can be BTM_BLE_CONNECT_EVT,
762 * BTM_BLE_CONNECT_DIR_EVT or
Myles Watsonee96a3c2016-11-23 14:49:54 -0800763 * BTM_BLE_CONNECT_LO_DUTY_DIR_EVT
764 *
765 * Returns BTM_ILLEGAL_VALUE if controller does not support BLE.
766 * BTM_SUCCESS is status set successfully; otherwise failure.
767 *
768 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800769tBTM_STATUS BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode) {
770 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800771
Myles Watson911d1ae2016-11-28 16:44:40 -0800772 BTM_TRACE_EVENT("%s connectable_mode = %d ", __func__, connectable_mode);
773 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774
Myles Watson911d1ae2016-11-28 16:44:40 -0800775 p_cb->directed_conn = connectable_mode;
776 return btm_ble_set_connectability(p_cb->connectable_mode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800777}
778
Marie Janssend19e0782016-07-15 12:48:27 -0700779#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800780static bool is_resolving_list_bit_set(void* data, void* context) {
781 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800782
Myles Watson911d1ae2016-11-28 16:44:40 -0800783 if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) != 0)
784 return false;
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800785
Myles Watson911d1ae2016-11-28 16:44:40 -0800786 return true;
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800787}
Pavlin Radoslavova9ea43b2016-02-15 11:47:37 -0800788#endif
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800789
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800790/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800791 *
792 * Function btm_set_conn_mode_adv_init_addr
793 *
Myles Watson9ca07092016-11-28 16:41:53 -0800794 * Description set initator address type and local address type based on
795 * adv mode.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800796 *
797 *
798 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800799static uint8_t btm_set_conn_mode_adv_init_addr(
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700800 tBTM_BLE_INQ_CB* p_cb, RawAddress& p_peer_addr_ptr,
Myles Watson911d1ae2016-11-28 16:44:40 -0800801 tBLE_ADDR_TYPE* p_peer_addr_type, tBLE_ADDR_TYPE* p_own_addr_type) {
802 uint8_t evt_type;
Marie Janssend19e0782016-07-15 12:48:27 -0700803#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800804 tBTM_SEC_DEV_REC* p_dev_rec;
Pavlin Radoslavova9ea43b2016-02-15 11:47:37 -0800805#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800806
Myles Watson911d1ae2016-11-28 16:44:40 -0800807 evt_type =
808 (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE)
809 ? ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT)
810 : BTM_BLE_CONNECT_EVT;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700811
Myles Watson911d1ae2016-11-28 16:44:40 -0800812 if (evt_type == BTM_BLE_CONNECT_EVT) {
813 evt_type = p_cb->directed_conn;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700814
Myles Watson911d1ae2016-11-28 16:44:40 -0800815 if (p_cb->directed_conn == BTM_BLE_CONNECT_DIR_EVT ||
816 p_cb->directed_conn == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT) {
Marie Janssend19e0782016-07-15 12:48:27 -0700817#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -0800818 /* for privacy 1.2, convert peer address as static, own address set as ID
819 * addr */
820 if (btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 ||
821 btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
822 /* only do so for bonded device */
Jakub Pawlowski78b81c62017-06-16 13:55:52 -0700823 if ((p_dev_rec = btm_find_or_alloc_dev(p_cb->direct_bda.bda)) != NULL &&
Myles Watson911d1ae2016-11-28 16:44:40 -0800824 p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
825 btm_ble_enable_resolving_list(BTM_BLE_RL_ADV);
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700826 p_peer_addr_ptr = p_dev_rec->ble.static_addr;
Myles Watson911d1ae2016-11-28 16:44:40 -0800827 *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
828 *p_own_addr_type = BLE_ADDR_RANDOM_ID;
829 return evt_type;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700830 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800831 /* otherwise fall though as normal directed adv */
832 else {
833 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
Jakub Pawlowskicac784d2016-02-01 11:53:36 -0800834 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800835 }
836#endif
837 /* direct adv mode does not have privacy, if privacy is not enabled */
838 *p_peer_addr_type = p_cb->direct_bda.type;
Jakub Pawlowski78b81c62017-06-16 13:55:52 -0700839 p_peer_addr_ptr = p_cb->direct_bda.bda;
Myles Watson911d1ae2016-11-28 16:44:40 -0800840 return evt_type;
Satya Calloji444a8da2015-03-06 10:38:22 -0800841 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800842 }
843
844/* undirect adv mode or non-connectable mode*/
845#if (BLE_PRIVACY_SPT == TRUE)
846 /* when privacy 1.2 privacy only mode is used, or mixed mode */
847 if ((btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 &&
848 p_cb->afp != AP_SCAN_CONN_ALL) ||
849 btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
850 list_node_t* n =
851 list_foreach(btm_cb.sec_dev_rec, is_resolving_list_bit_set, NULL);
852 if (n) {
853 /* if enhanced privacy is required, set Identity address and matching IRK
854 * peer */
855 tBTM_SEC_DEV_REC* p_dev_rec =
856 static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
Jakub Pawlowskic2276b02017-06-09 16:00:25 -0700857 p_peer_addr_ptr = p_dev_rec->ble.static_addr;
Myles Watson911d1ae2016-11-28 16:44:40 -0800858 *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
859
860 *p_own_addr_type = BLE_ADDR_RANDOM_ID;
861 } else {
862 /* resolving list is empty, not enabled */
863 *p_own_addr_type = BLE_ADDR_RANDOM;
Satya Calloji444a8da2015-03-06 10:38:22 -0800864 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800865 }
866 /* privacy 1.1, or privacy 1.2, general discoverable/connectable mode, disable
867 privacy in */
868 /* controller fall back to host based privacy */
869 else if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
870 *p_own_addr_type = BLE_ADDR_RANDOM;
871 }
Satya Calloji444a8da2015-03-06 10:38:22 -0800872#endif
873
Myles Watson911d1ae2016-11-28 16:44:40 -0800874 /* if no privacy,do not set any peer address,*/
875 /* local address type go by global privacy setting */
876 return evt_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800877}
878
879/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800880 *
881 * Function BTM_BleSetAdvParams
882 *
883 * Description This function is called to set advertising parameters.
884 *
885 * Parameters adv_int_min: minimum advertising interval
886 * adv_int_max: maximum advertising interval
887 * p_dir_bda: connectable direct initiator's LE device address
888 * chnl_map: advertising channel map.
889 *
890 * Returns void
891 *
892 ******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700893tBTM_STATUS BTM_BleSetAdvParams(uint16_t adv_int_min, uint16_t adv_int_max,
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700894 const RawAddress& p_dir_bda,
Myles Watson911d1ae2016-11-28 16:44:40 -0800895 tBTM_BLE_ADV_CHNL_MAP chnl_map) {
896 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
897 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
898 tBTM_STATUS status = BTM_SUCCESS;
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700899 RawAddress address = RawAddress::kEmpty;
Myles Watson911d1ae2016-11-28 16:44:40 -0800900 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
901 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
902 uint8_t adv_mode = p_cb->adv_mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800903
Myles Watson911d1ae2016-11-28 16:44:40 -0800904 BTM_TRACE_EVENT("BTM_BleSetAdvParams");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800905
Myles Watson911d1ae2016-11-28 16:44:40 -0800906 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
Andre Eisenbach3aa60542013-03-22 18:00:51 -0700907
Myles Watson911d1ae2016-11-28 16:44:40 -0800908 if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN,
909 BTM_BLE_ADV_INT_MAX) ||
910 !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN,
911 BTM_BLE_ADV_INT_MAX)) {
912 return BTM_ILLEGAL_VALUE;
913 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800914
Myles Watson911d1ae2016-11-28 16:44:40 -0800915 p_cb->adv_interval_min = adv_int_min;
916 p_cb->adv_interval_max = adv_int_max;
917 p_cb->adv_chnl_map = chnl_map;
Jakub Pawlowski78b81c62017-06-16 13:55:52 -0700918 p_cb->direct_bda.bda = p_dir_bda;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800919
Myles Watson911d1ae2016-11-28 16:44:40 -0800920 BTM_TRACE_EVENT("update params for an active adv");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921
Myles Watson911d1ae2016-11-28 16:44:40 -0800922 btm_ble_stop_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923
Myles Watson911d1ae2016-11-28 16:44:40 -0800924 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700925 p_cb, address, &init_addr_type, &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
Myles Watson911d1ae2016-11-28 16:44:40 -0800927 /* update adv params */
928 btsnd_hcic_ble_write_adv_params(
929 p_cb->adv_interval_min, p_cb->adv_interval_max, p_cb->evt_type,
Jakub Pawlowskib707f442017-07-03 15:39:36 -0700930 own_addr_type, init_addr_type, address, p_cb->adv_chnl_map, p_cb->afp);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
Myles Watson911d1ae2016-11-28 16:44:40 -0800932 if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800933
Myles Watson911d1ae2016-11-28 16:44:40 -0800934 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800935}
936
Jakub Pawlowski119f32a2017-02-16 11:56:56 -0800937/**
938 * This function is called to set scan parameters. |cb| is called with operation
939 * status
940 **/
941void BTM_BleSetScanParams(uint32_t scan_interval, uint32_t scan_window,
942 tBLE_SCAN_MODE scan_mode,
943 base::Callback<void(uint8_t)> cb) {
Myles Watson911d1ae2016-11-28 16:44:40 -0800944 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
945 uint32_t max_scan_interval;
946 uint32_t max_scan_window;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947
Myles Watson911d1ae2016-11-28 16:44:40 -0800948 BTM_TRACE_EVENT("%s", __func__);
949 if (!controller_get_interface()->supports_ble()) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800950
Myles Watson911d1ae2016-11-28 16:44:40 -0800951 /* If not supporting extended scan support, use the older range for checking
952 */
953 if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
954 max_scan_interval = BTM_BLE_SCAN_INT_MAX;
955 max_scan_window = BTM_BLE_SCAN_WIN_MAX;
956 } else {
957 /* If supporting extended scan support, use the new extended range for
958 * checking */
959 max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX;
960 max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX;
961 }
Satya Calloji935324a2015-03-31 13:24:32 -0700962
Myles Watson911d1ae2016-11-28 16:44:40 -0800963 if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN,
964 max_scan_interval) &&
965 BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN,
966 max_scan_window) &&
967 (scan_mode == BTM_BLE_SCAN_MODE_ACTI ||
968 scan_mode == BTM_BLE_SCAN_MODE_PASS)) {
969 p_cb->scan_type = scan_mode;
970 p_cb->scan_interval = scan_interval;
971 p_cb->scan_window = scan_window;
Satya Calloji935324a2015-03-31 13:24:32 -0700972
Jakub Pawlowski119f32a2017-02-16 11:56:56 -0800973 cb.Run(BTM_SUCCESS);
Myles Watson911d1ae2016-11-28 16:44:40 -0800974 } else {
Jakub Pawlowski119f32a2017-02-16 11:56:56 -0800975 cb.Run(BTM_ILLEGAL_VALUE);
Satya Calloji935324a2015-03-31 13:24:32 -0700976
Myles Watson911d1ae2016-11-28 16:44:40 -0800977 BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
978 scan_interval, scan_window);
979 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980}
981
982/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800983 *
984 * Function BTM_BleWriteScanRsp
985 *
986 * Description This function is called to write LE scan response.
987 *
988 * Parameters: p_scan_rsp: scan response information.
989 *
990 * Returns void
991 *
992 ******************************************************************************/
Jakub Pawlowski9eaf7762016-10-04 19:30:09 -0700993void BTM_BleWriteScanRsp(uint8_t* data, uint8_t length,
Myles Watson911d1ae2016-11-28 16:44:40 -0800994 tBTM_BLE_ADV_DATA_CMPL_CBACK* p_adv_data_cback) {
995 BTM_TRACE_EVENT("%s: length: %d", __func__, length);
996 if (!controller_get_interface()->supports_ble()) {
997 p_adv_data_cback(BTM_ILLEGAL_VALUE);
998 return;
999 }
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001000
Myles Watson911d1ae2016-11-28 16:44:40 -08001001 btsnd_hcic_ble_set_scan_rsp_data(length, data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002
Myles Watson911d1ae2016-11-28 16:44:40 -08001003 if (length != 0)
1004 btm_cb.ble_ctr_cb.inq_var.scan_rsp = true;
1005 else
1006 btm_cb.ble_ctr_cb.inq_var.scan_rsp = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001007
Myles Watson911d1ae2016-11-28 16:44:40 -08001008 p_adv_data_cback(BTM_SUCCESS);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001009}
1010
The Android Open Source Project5738f832012-12-12 16:00:35 -08001011/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001012 *
1013 * Function BTM__BLEReadDiscoverability
1014 *
Myles Watson9ca07092016-11-28 16:41:53 -08001015 * Description This function is called to read the current LE
1016 * discoverability mode of the device.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001017 *
1018 * Returns BTM_BLE_NON_DISCOVERABLE ,BTM_BLE_LIMITED_DISCOVERABLE or
1019 * BTM_BLE_GENRAL_DISCOVERABLE
1020 *
1021 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001022uint16_t BTM_BleReadDiscoverability() {
1023 BTM_TRACE_API("%s", __func__);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001024
Myles Watson911d1ae2016-11-28 16:44:40 -08001025 return (btm_cb.ble_ctr_cb.inq_var.discoverable_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001026}
1027
1028/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001029 *
1030 * Function BTM__BLEReadConnectability
1031 *
Myles Watson9ca07092016-11-28 16:41:53 -08001032 * Description This function is called to read the current LE
1033 * connectability mode of the device.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001034 *
1035 * Returns BTM_BLE_NON_CONNECTABLE or BTM_BLE_CONNECTABLE
1036 *
1037 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001038uint16_t BTM_BleReadConnectability() {
1039 BTM_TRACE_API("%s", __func__);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001040
Myles Watson911d1ae2016-11-28 16:44:40 -08001041 return (btm_cb.ble_ctr_cb.inq_var.connectable_mode);
VenkatRaghavan VijayaRaghavan76356ae2015-04-21 11:32:29 -07001042}
1043
1044/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001045 *
1046 * Function btm_ble_select_adv_interval
1047 *
1048 * Description select adv interval based on device mode
1049 *
1050 * Returns void
1051 *
1052 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001053void btm_ble_select_adv_interval(tBTM_BLE_INQ_CB* p_cb, uint8_t evt_type,
1054 uint16_t* p_adv_int_min,
1055 uint16_t* p_adv_int_max) {
1056 if (p_cb->adv_interval_min && p_cb->adv_interval_max) {
1057 *p_adv_int_min = p_cb->adv_interval_min;
1058 *p_adv_int_max = p_cb->adv_interval_max;
1059 } else {
1060 switch (evt_type) {
1061 case BTM_BLE_CONNECT_EVT:
1062 case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
1063 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
1064 break;
1065
1066 case BTM_BLE_NON_CONNECT_EVT:
1067 case BTM_BLE_DISCOVER_EVT:
1068 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
1069 break;
1070
1071 /* connectable directed event */
1072 case BTM_BLE_CONNECT_DIR_EVT:
1073 *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
1074 *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
1075 break;
1076
1077 default:
1078 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
1079 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001080 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001081 }
1082 return;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001083}
Satya Calloji444a8da2015-03-06 10:38:22 -08001084
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001085/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001086 *
1087 * Function btm_ble_update_dmt_flag_bits
1088 *
Myles Watson9ca07092016-11-28 16:41:53 -08001089 * Description Obtain updated adv flag value based on connect and
1090 * discoverability mode. Also, setup DMT support value in the
1091 * flag based on whether the controller supports both LE and
1092 * BR/EDR.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001093 *
1094 * Parameters: flag_value (Input / Output) - flag value
1095 * connect_mode (Input) - Connect mode value
1096 * disc_mode (Input) - discoverability mode
1097 *
1098 * Returns void
1099 *
1100 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001101void btm_ble_update_dmt_flag_bits(uint8_t* adv_flag_value,
1102 const uint16_t connect_mode,
1103 const uint16_t disc_mode) {
1104 /* BR/EDR non-discoverable , non-connectable */
1105 if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
1106 (connect_mode & BTM_CONNECTABLE_MASK) == 0)
1107 *adv_flag_value |= BTM_BLE_BREDR_NOT_SPT;
1108 else
1109 *adv_flag_value &= ~BTM_BLE_BREDR_NOT_SPT;
Satya Calloji7cf7da92015-02-05 13:12:15 -08001110
Myles Watson911d1ae2016-11-28 16:44:40 -08001111 /* if local controller support, mark both controller and host support in flag
1112 */
1113 if (controller_get_interface()->supports_simultaneous_le_bredr())
1114 *adv_flag_value |= (BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
1115 else
1116 *adv_flag_value &= ~(BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
Satya Calloji7cf7da92015-02-05 13:12:15 -08001117}
1118
1119/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001120 *
1121 * Function btm_ble_set_adv_flag
1122 *
1123 * Description Set adv flag in adv data.
1124 *
1125 * Parameters: connect_mode (Input)- Connect mode value
1126 * disc_mode (Input) - discoverability mode
1127 *
1128 * Returns void
1129 *
1130 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001131void btm_ble_set_adv_flag(uint16_t connect_mode, uint16_t disc_mode) {
1132 uint8_t flag = 0, old_flag = 0;
1133 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 -07001134
Myles Watson911d1ae2016-11-28 16:44:40 -08001135 if (p_adv_data->p_flags != NULL) flag = old_flag = *(p_adv_data->p_flags);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001136
Myles Watson911d1ae2016-11-28 16:44:40 -08001137 btm_ble_update_dmt_flag_bits(&flag, connect_mode, disc_mode);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001138
Myles Watson911d1ae2016-11-28 16:44:40 -08001139 LOG_DEBUG(LOG_TAG, "disc_mode %04x", disc_mode);
1140 /* update discoverable flag */
1141 if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE) {
1142 flag &= ~BTM_BLE_GEN_DISC_FLAG;
1143 flag |= BTM_BLE_LIMIT_DISC_FLAG;
1144 } else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE) {
1145 flag |= BTM_BLE_GEN_DISC_FLAG;
1146 flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
1147 } else /* remove all discoverable flags */
1148 {
1149 flag &= ~(BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG);
1150 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001151
Myles Watson911d1ae2016-11-28 16:44:40 -08001152 if (flag != old_flag) {
1153 btm_ble_update_adv_flag(flag);
1154 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001155}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001156/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001157 *
1158 * Function btm_ble_set_discoverability
1159 *
1160 * Description This function is called to set BLE discoverable mode.
1161 *
1162 * Parameters: combined_mode: discoverability mode.
1163 *
1164 * Returns BTM_SUCCESS is status set successfully; otherwise failure.
1165 *
1166 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001167tBTM_STATUS btm_ble_set_discoverability(uint16_t combined_mode) {
1168 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1169 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
1170 uint16_t mode = (combined_mode & BTM_BLE_DISCOVERABLE_MASK);
1171 uint8_t new_mode = BTM_BLE_ADV_ENABLE;
1172 uint8_t evt_type;
1173 tBTM_STATUS status = BTM_SUCCESS;
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001174 RawAddress address = RawAddress::kEmpty;
Myles Watson911d1ae2016-11-28 16:44:40 -08001175 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC,
1176 own_addr_type = p_addr_cb->own_addr_type;
1177 uint16_t adv_int_min, adv_int_max;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001178
Myles Watson911d1ae2016-11-28 16:44:40 -08001179 BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
1180 combined_mode);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001181
Myles Watson911d1ae2016-11-28 16:44:40 -08001182 /*** Check mode parameter ***/
1183 if (mode > BTM_BLE_MAX_DISCOVERABLE) return (BTM_ILLEGAL_VALUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001184
Myles Watson911d1ae2016-11-28 16:44:40 -08001185 p_cb->discoverable_mode = mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001186
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001187 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, address, &init_addr_type,
Myles Watson911d1ae2016-11-28 16:44:40 -08001188 &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001189
Myles Watson911d1ae2016-11-28 16:44:40 -08001190 if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE &&
1191 mode == BTM_BLE_NON_DISCOVERABLE)
1192 new_mode = BTM_BLE_ADV_DISABLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001193
Myles Watson911d1ae2016-11-28 16:44:40 -08001194 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 -08001195
Myles Watson911d1ae2016-11-28 16:44:40 -08001196 alarm_cancel(p_cb->fast_adv_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197
Myles Watson911d1ae2016-11-28 16:44:40 -08001198 /* update adv params if start advertising */
1199 BTM_TRACE_EVENT("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type,
1200 p_cb->evt_type);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001201
Myles Watson911d1ae2016-11-28 16:44:40 -08001202 if (new_mode == BTM_BLE_ADV_ENABLE) {
1203 btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, combined_mode);
1204
1205 if (evt_type != p_cb->evt_type || p_cb->adv_addr_type != own_addr_type ||
1206 !p_cb->fast_adv_on) {
1207 btm_ble_stop_adv();
1208
1209 /* update adv params */
1210 btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001211 own_addr_type, init_addr_type, address,
Myles Watson911d1ae2016-11-28 16:44:40 -08001212 p_cb->adv_chnl_map, p_cb->afp);
1213 p_cb->evt_type = evt_type;
1214 p_cb->adv_addr_type = own_addr_type;
1215 }
1216 }
1217
1218 if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode) {
Satya Calloji444a8da2015-03-06 10:38:22 -08001219 if (new_mode == BTM_BLE_ADV_ENABLE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001220 status = btm_ble_start_adv();
Satya Calloji70b95982015-04-23 23:39:49 -07001221 else
Myles Watson911d1ae2016-11-28 16:44:40 -08001222 status = btm_ble_stop_adv();
1223 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001224
Myles Watson911d1ae2016-11-28 16:44:40 -08001225 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
1226 p_cb->fast_adv_on = true;
1227 /* start initial GAP mode adv timer */
1228 alarm_set_on_queue(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
1229 btm_ble_fast_adv_timer_timeout, NULL,
1230 btu_general_alarm_queue);
1231 } else {
1232#if (BLE_PRIVACY_SPT == TRUE)
1233 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
1234#endif
1235 }
1236
1237 /* set up stop advertising timer */
1238 if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE) {
1239 BTM_TRACE_EVENT("start timer for limited disc mode duration=%d ms",
1240 BTM_BLE_GAP_LIM_TIMEOUT_MS);
1241 /* start Tgap(lim_timeout) */
1242 alarm_set_on_queue(p_cb->inquiry_timer, BTM_BLE_GAP_LIM_TIMEOUT_MS,
1243 btm_ble_inquiry_timer_gap_limited_discovery_timeout,
1244 NULL, btu_general_alarm_queue);
1245 }
1246 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001247}
1248
1249/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001250 *
1251 * Function btm_ble_set_connectability
1252 *
1253 * Description This function is called to set BLE connectability mode.
1254 *
1255 * Parameters: combined_mode: connectability mode.
1256 *
1257 * Returns BTM_SUCCESS is status set successfully; otherwise failure.
1258 *
1259 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001260tBTM_STATUS btm_ble_set_connectability(uint16_t combined_mode) {
1261 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1262 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
1263 uint16_t mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
1264 uint8_t new_mode = BTM_BLE_ADV_ENABLE;
1265 uint8_t evt_type;
1266 tBTM_STATUS status = BTM_SUCCESS;
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001267 RawAddress address = RawAddress::kEmpty;
Myles Watson911d1ae2016-11-28 16:44:40 -08001268 tBLE_ADDR_TYPE peer_addr_type = BLE_ADDR_PUBLIC,
1269 own_addr_type = p_addr_cb->own_addr_type;
1270 uint16_t adv_int_min, adv_int_max;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001271
Myles Watson911d1ae2016-11-28 16:44:40 -08001272 BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
1273 combined_mode);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001274
Myles Watson911d1ae2016-11-28 16:44:40 -08001275 /*** Check mode parameter ***/
1276 if (mode > BTM_BLE_MAX_CONNECTABLE) return (BTM_ILLEGAL_VALUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001277
Myles Watson911d1ae2016-11-28 16:44:40 -08001278 p_cb->connectable_mode = mode;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001279
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001280 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, address, &peer_addr_type,
Myles Watson911d1ae2016-11-28 16:44:40 -08001281 &own_addr_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001282
Myles Watson911d1ae2016-11-28 16:44:40 -08001283 if (mode == BTM_BLE_NON_CONNECTABLE &&
1284 p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
1285 new_mode = BTM_BLE_ADV_DISABLE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001286
Myles Watson911d1ae2016-11-28 16:44:40 -08001287 btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001288
Myles Watson911d1ae2016-11-28 16:44:40 -08001289 alarm_cancel(p_cb->fast_adv_timer);
1290 /* update adv params if needed */
1291 if (new_mode == BTM_BLE_ADV_ENABLE) {
1292 btm_ble_set_adv_flag(combined_mode, btm_cb.btm_inq_vars.discoverable_mode);
1293 if (p_cb->evt_type != evt_type ||
1294 p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on) {
1295 btm_ble_stop_adv();
1296
1297 btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001298 own_addr_type, peer_addr_type, address,
Myles Watson911d1ae2016-11-28 16:44:40 -08001299 p_cb->adv_chnl_map, p_cb->afp);
1300 p_cb->evt_type = evt_type;
1301 p_cb->adv_addr_type = own_addr_type;
1302 }
1303 }
1304
1305 /* update advertising mode */
1306 if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode) {
Satya Calloji444a8da2015-03-06 10:38:22 -08001307 if (new_mode == BTM_BLE_ADV_ENABLE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001308 status = btm_ble_start_adv();
Satya Calloji70b95982015-04-23 23:39:49 -07001309 else
Myles Watson911d1ae2016-11-28 16:44:40 -08001310 status = btm_ble_stop_adv();
1311 }
1312
1313 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
1314 p_cb->fast_adv_on = true;
1315 /* start initial GAP mode adv timer */
1316 alarm_set_on_queue(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
1317 btm_ble_fast_adv_timer_timeout, NULL,
1318 btu_general_alarm_queue);
1319 } else {
Marie Janssend19e0782016-07-15 12:48:27 -07001320#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001321 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
Andre Eisenbacha021a122015-05-20 23:55:13 -07001322#endif
Myles Watson911d1ae2016-11-28 16:44:40 -08001323 }
1324 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001325}
1326
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001327void btm_send_hci_scan_enable(uint8_t enable, uint8_t filter_duplicates) {
1328 if (controller_get_interface()->supports_ble_extended_advertising()) {
1329 btsnd_hcic_ble_set_extended_scan_enable(enable, filter_duplicates, 0x0000,
1330 0x0000);
1331 } else {
1332 btsnd_hcic_ble_set_scan_enable(enable, filter_duplicates);
1333 }
1334}
1335
1336void btm_send_hci_set_scan_params(uint8_t scan_type, uint16_t scan_int,
1337 uint16_t scan_win, uint8_t addr_type_own,
1338 uint8_t scan_filter_policy) {
1339 if (controller_get_interface()->supports_ble_extended_advertising()) {
1340 scanning_phy_cfg phy_cfg;
1341 phy_cfg.scan_type = scan_type;
1342 phy_cfg.scan_int = scan_int;
1343 phy_cfg.scan_win = scan_win;
1344
1345 btsnd_hcic_ble_set_extended_scan_params(addr_type_own, scan_filter_policy,
1346 1, &phy_cfg);
1347 } else {
1348 btsnd_hcic_ble_set_scan_params(scan_type, scan_int, scan_win, addr_type_own,
1349 scan_filter_policy);
1350 }
1351}
1352
The Android Open Source Project5738f832012-12-12 16:00:35 -08001353/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001354 *
1355 * Function btm_ble_start_inquiry
1356 *
1357 * Description This function is called to start BLE inquiry procedure.
Myles Watson9ca07092016-11-28 16:41:53 -08001358 * If the duration is zero, the periodic inquiry mode is
1359 * cancelled.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001360 *
1361 * Parameters: mode - GENERAL or LIMITED inquiry
1362 * p_inq_params - pointer to the BLE inquiry parameter.
Myles Watson9ca07092016-11-28 16:41:53 -08001363 * p_results_cb - callback returning pointer to results
1364 * (tBTM_INQ_RESULTS)
Myles Watsonee96a3c2016-11-23 14:49:54 -08001365 * p_cmpl_cb - callback indicating the end of an inquiry
1366 *
1367 *
1368 *
1369 * Returns BTM_CMD_STARTED if successfully started
1370 * BTM_NO_RESOURCES if could not allocate a message buffer
1371 * BTM_BUSY - if an inquiry is already active
1372 *
1373 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001374tBTM_STATUS btm_ble_start_inquiry(uint8_t mode, uint8_t duration) {
1375 tBTM_STATUS status = BTM_CMD_STARTED;
1376 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
1377 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378
Myles Watson911d1ae2016-11-28 16:44:40 -08001379 BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x",
1380 mode, btm_cb.btm_inq_vars.inq_active);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001381
Myles Watson911d1ae2016-11-28 16:44:40 -08001382 /* if selective connection is active, or inquiry is already active, reject it
1383 */
Jakub Pawlowski83211b02016-12-07 11:25:15 -08001384 if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001385 BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
1386 return (BTM_BUSY);
1387 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388
Myles Watson911d1ae2016-11-28 16:44:40 -08001389 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001390 btm_send_hci_set_scan_params(
Myles Watson911d1ae2016-11-28 16:44:40 -08001391 BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
1392 BTM_BLE_LOW_LATENCY_SCAN_WIN,
1393 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
Marie Janssend19e0782016-07-15 12:48:27 -07001394#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08001395 /* enable IRK list */
1396 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
Wei Wanged534e32014-05-20 06:30:13 +00001397#endif
Myles Watson911d1ae2016-11-28 16:44:40 -08001398 p_ble_cb->inq_var.scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
1399 status = btm_ble_start_scan();
1400 } else if ((p_ble_cb->inq_var.scan_interval !=
1401 BTM_BLE_LOW_LATENCY_SCAN_INT) ||
1402 (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
1403 BTM_TRACE_DEBUG("%s, restart LE scan with low latency scan params",
1404 __func__);
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001405 btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
1406 btm_send_hci_set_scan_params(
Myles Watson911d1ae2016-11-28 16:44:40 -08001407 BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
1408 BTM_BLE_LOW_LATENCY_SCAN_WIN,
1409 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08001410 btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE);
Myles Watson911d1ae2016-11-28 16:44:40 -08001411 }
1412
1413 if (status == BTM_CMD_STARTED) {
1414 p_inq->inq_active |= mode;
1415 p_ble_cb->scan_activity |= mode;
1416
1417 BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x",
1418 p_inq->inq_active);
1419
1420 if (duration != 0) {
1421 /* start inquiry timer */
1422 period_ms_t duration_ms = duration * 1000;
1423 alarm_set_on_queue(p_ble_cb->inq_var.inquiry_timer, duration_ms,
1424 btm_ble_inquiry_timer_timeout, NULL,
1425 btu_general_alarm_queue);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001426 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001427 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001428
Myles Watson911d1ae2016-11-28 16:44:40 -08001429 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001430}
1431
1432/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001433 *
1434 * Function btm_ble_read_remote_name_cmpl
1435 *
1436 * Description This function is called when BLE remote name is received.
1437 *
1438 * Returns void
1439 *
1440 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001441void btm_ble_read_remote_name_cmpl(bool status, const RawAddress& bda,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001442 uint16_t length, char* p_name) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001443 uint8_t hci_status = HCI_SUCCESS;
1444 BD_NAME bd_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001445
Myles Watson911d1ae2016-11-28 16:44:40 -08001446 memset(bd_name, 0, (BD_NAME_LEN + 1));
1447 if (length > BD_NAME_LEN) {
1448 length = BD_NAME_LEN;
1449 }
1450 memcpy((uint8_t*)bd_name, p_name, length);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001451
Myles Watson911d1ae2016-11-28 16:44:40 -08001452 if ((!status) || (length == 0)) {
1453 hci_status = HCI_ERR_HOST_TIMEOUT;
1454 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001455
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001456 btm_process_remote_name(&bda, bd_name, length + 1, hci_status);
1457 btm_sec_rmt_name_request_complete(&bda, (uint8_t*)p_name, hci_status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001458}
1459
1460/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001461 *
1462 * Function btm_ble_read_remote_name
1463 *
1464 * Description This function read remote LE device name using GATT read
1465 * procedure.
1466 *
1467 * Parameters: None.
1468 *
1469 * Returns void
1470 *
1471 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001472tBTM_STATUS btm_ble_read_remote_name(const RawAddress& remote_bda,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001473 tBTM_CMPL_CB* p_cb) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001474 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001475
Myles Watson911d1ae2016-11-28 16:44:40 -08001476 if (!controller_get_interface()->supports_ble()) return BTM_ERR_PROCESSING;
Andre Eisenbach3aa60542013-03-22 18:00:51 -07001477
Jack He8bf22852017-02-16 03:06:15 -05001478 tINQ_DB_ENT* p_i = btm_inq_db_find(remote_bda);
1479 if (p_i && !ble_evt_type_is_connectable(p_i->inq_info.results.ble_evt_type)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001480 BTM_TRACE_DEBUG("name request to non-connectable device failed.");
1481 return BTM_ERR_PROCESSING;
1482 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001483
Myles Watson911d1ae2016-11-28 16:44:40 -08001484 /* read remote device name using GATT procedure */
1485 if (p_inq->remname_active) return BTM_BUSY;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001486
Myles Watson911d1ae2016-11-28 16:44:40 -08001487 if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
1488 return BTM_BUSY;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001489
Myles Watson911d1ae2016-11-28 16:44:40 -08001490 p_inq->p_remname_cmpl_cb = p_cb;
1491 p_inq->remname_active = true;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001492 p_inq->remname_bda = remote_bda;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001493
Myles Watson911d1ae2016-11-28 16:44:40 -08001494 alarm_set_on_queue(p_inq->remote_name_timer, BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS,
1495 btm_inq_remote_name_timer_timeout, NULL,
1496 btu_general_alarm_queue);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001497
Myles Watson911d1ae2016-11-28 16:44:40 -08001498 return BTM_CMD_STARTED;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001499}
1500
1501/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001502 *
1503 * Function btm_ble_cancel_remote_name
1504 *
1505 * Description This function cancel read remote LE device name.
1506 *
1507 * Parameters: None.
1508 *
1509 * Returns void
1510 *
1511 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001512bool btm_ble_cancel_remote_name(const RawAddress& remote_bda) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001513 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
1514 bool status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001515
Myles Watson911d1ae2016-11-28 16:44:40 -08001516 status = GAP_BleCancelReadPeerDevName(remote_bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001517
Myles Watson911d1ae2016-11-28 16:44:40 -08001518 p_inq->remname_active = false;
Jakub Pawlowskib707f442017-07-03 15:39:36 -07001519 p_inq->remname_bda = RawAddress::kEmpty;
Myles Watson911d1ae2016-11-28 16:44:40 -08001520 alarm_cancel(p_inq->remote_name_timer);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001521
Myles Watson911d1ae2016-11-28 16:44:40 -08001522 return status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001523}
1524
1525/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001526 *
1527 * Function btm_ble_update_adv_flag
1528 *
Myles Watson9ca07092016-11-28 16:41:53 -08001529 * Description This function update the limited discoverable flag in the
1530 * adv data.
Myles Watsonee96a3c2016-11-23 14:49:54 -08001531 *
1532 * Parameters: None.
1533 *
1534 * Returns void
1535 *
1536 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001537static void btm_ble_update_adv_flag(uint8_t flag) {
1538 tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1539 uint8_t* p;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001540
Myles Watson911d1ae2016-11-28 16:44:40 -08001541 BTM_TRACE_DEBUG("btm_ble_update_adv_flag new=0x%x", flag);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001542
Myles Watson911d1ae2016-11-28 16:44:40 -08001543 if (p_adv_data->p_flags != NULL) {
1544 BTM_TRACE_DEBUG("btm_ble_update_adv_flag old=0x%x", *p_adv_data->p_flags);
1545 *p_adv_data->p_flags = flag;
1546 } else /* no FLAGS in ADV data*/
1547 {
1548 p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
1549 /* need 3 bytes space to stuff in the flags, if not */
1550 /* erase all written data, just for flags */
1551 if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3) {
1552 p = p_adv_data->p_pad = p_adv_data->ad_data;
1553 memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001554 }
1555
Myles Watson911d1ae2016-11-28 16:44:40 -08001556 *p++ = 2;
1557 *p++ = BTM_BLE_AD_TYPE_FLAG;
1558 p_adv_data->p_flags = p;
1559 *p++ = flag;
1560 p_adv_data->p_pad = p;
1561 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001562
Myles Watson911d1ae2016-11-28 16:44:40 -08001563 btsnd_hcic_ble_set_adv_data(
1564 (uint8_t)(p_adv_data->p_pad - p_adv_data->ad_data), p_adv_data->ad_data);
1565 p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001566}
1567
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001568/**
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001569 * Check ADV flag to make sure device is discoverable and match the search
1570 * condition
1571 */
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001572uint8_t btm_ble_is_discoverable(const RawAddress& bda,
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08001573 std::vector<uint8_t> const& adv_data) {
1574 uint8_t flag = 0, rt = 0;
Myles Watson911d1ae2016-11-28 16:44:40 -08001575 uint8_t data_len;
1576 tBTM_INQ_PARMS* p_cond = &btm_cb.btm_inq_vars.inqparms;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001577
Myles Watson911d1ae2016-11-28 16:44:40 -08001578 /* for observer, always "discoverable */
1579 if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
1580 rt |= BTM_BLE_OBS_RESULT;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001581
Myles Watson911d1ae2016-11-28 16:44:40 -08001582 /* does not match filter condition */
1583 if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001584 bda != p_cond->filter_cond.bdaddr_cond) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001585 BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07001586 return rt;
Myles Watson911d1ae2016-11-28 16:44:40 -08001587 }
1588
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08001589 if (!adv_data.empty()) {
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07001590 const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType(
1591 adv_data, BTM_BLE_AD_TYPE_FLAG, &data_len);
Myles Watson911d1ae2016-11-28 16:44:40 -08001592 if (p_flag != NULL) {
1593 flag = *p_flag;
1594
1595 if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
1596 (flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) {
1597 BTM_TRACE_DEBUG("Find Generable Discoverable device");
1598 rt |= BTM_BLE_INQ_RESULT;
1599 }
1600
1601 else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
1602 (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0) {
1603 BTM_TRACE_DEBUG("Find limited discoverable device");
1604 rt |= BTM_BLE_INQ_RESULT;
1605 }
1606 }
1607 }
1608 return rt;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001609}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001610
Myles Watson911d1ae2016-11-28 16:44:40 -08001611static void btm_ble_appearance_to_cod(uint16_t appearance, uint8_t* dev_class) {
1612 dev_class[0] = 0;
Adam Hampsone6c74502014-05-30 15:07:08 -07001613
Myles Watson911d1ae2016-11-28 16:44:40 -08001614 switch (appearance) {
1615 case BTM_BLE_APPEARANCE_GENERIC_PHONE:
1616 dev_class[1] = BTM_COD_MAJOR_PHONE;
1617 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1618 break;
1619 case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
1620 dev_class[1] = BTM_COD_MAJOR_COMPUTER;
1621 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1622 break;
1623 case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
1624 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1625 dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
1626 break;
1627 case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
1628 case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
1629 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1630 dev_class[2] = BTM_COD_MINOR_THERMOMETER;
1631 break;
1632 case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
1633 case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
1634 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1635 dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
1636 break;
1637 case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
1638 case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
1639 case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
1640 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1641 dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
1642 break;
1643 case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
1644 case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
1645 case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
1646 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1647 dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
1648 break;
1649 case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
1650 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1651 dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
1652 break;
1653 case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
1654 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1655 dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
1656 break;
1657 case BTM_BLE_APPEARANCE_GENERIC_WALKING:
1658 case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
1659 case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
1660 case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
1661 dev_class[1] = BTM_COD_MAJOR_HEALTH;
1662 dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
1663 break;
1664 case BTM_BLE_APPEARANCE_GENERIC_WATCH:
1665 case BTM_BLE_APPEARANCE_SPORTS_WATCH:
1666 dev_class[1] = BTM_COD_MAJOR_WEARABLE;
1667 dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
1668 break;
1669 case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
1670 dev_class[1] = BTM_COD_MAJOR_WEARABLE;
1671 dev_class[2] = BTM_COD_MINOR_GLASSES;
1672 break;
1673 case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
1674 dev_class[1] = BTM_COD_MAJOR_IMAGING;
1675 dev_class[2] = BTM_COD_MINOR_DISPLAY;
1676 break;
1677 case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
1678 dev_class[1] = BTM_COD_MAJOR_AUDIO;
1679 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1680 break;
1681 case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
1682 case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
1683 case BTM_BLE_APPEARANCE_GENERIC_HID:
1684 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1685 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1686 break;
1687 case BTM_BLE_APPEARANCE_HID_KEYBOARD:
1688 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1689 dev_class[2] = BTM_COD_MINOR_KEYBOARD;
1690 break;
1691 case BTM_BLE_APPEARANCE_HID_MOUSE:
1692 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1693 dev_class[2] = BTM_COD_MINOR_POINTING;
1694 break;
1695 case BTM_BLE_APPEARANCE_HID_JOYSTICK:
1696 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1697 dev_class[2] = BTM_COD_MINOR_JOYSTICK;
1698 break;
1699 case BTM_BLE_APPEARANCE_HID_GAMEPAD:
1700 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1701 dev_class[2] = BTM_COD_MINOR_GAMEPAD;
1702 break;
1703 case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
1704 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1705 dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
1706 break;
1707 case BTM_BLE_APPEARANCE_HID_CARD_READER:
1708 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1709 dev_class[2] = BTM_COD_MINOR_CARD_READER;
1710 break;
1711 case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
1712 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1713 dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
1714 break;
1715 case BTM_BLE_APPEARANCE_UKNOWN:
1716 case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
1717 case BTM_BLE_APPEARANCE_GENERIC_TAG:
1718 case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
1719 case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
1720 case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
1721 case BTM_BLE_APPEARANCE_CYCLING_SPEED:
1722 case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
1723 case BTM_BLE_APPEARANCE_CYCLING_POWER:
1724 case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
1725 case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
1726 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
1727 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
1728 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
1729 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
1730 default:
1731 dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
1732 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1733 };
Adam Hampsone6c74502014-05-30 15:07:08 -07001734}
1735
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001736/**
1737 * Update adv packet information into inquiry result.
1738 */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001739void btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001740 const RawAddress& bda, uint16_t evt_type,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001741 uint8_t primary_phy, uint8_t secondary_phy,
1742 uint8_t advertising_sid, int8_t tx_power,
1743 int8_t rssi, uint16_t periodic_adv_int,
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08001744 std::vector<uint8_t> const& data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001745 tBTM_INQ_RESULTS* p_cur = &p_i->inq_info.results;
1746 uint8_t len;
Myles Watson911d1ae2016-11-28 16:44:40 -08001747 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001748
Myles Watson911d1ae2016-11-28 16:44:40 -08001749 /* Save the info */
1750 p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
1751 p_cur->ble_addr_type = addr_type;
1752 p_cur->rssi = rssi;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08001753 p_cur->ble_primary_phy = primary_phy;
1754 p_cur->ble_secondary_phy = secondary_phy;
1755 p_cur->ble_advertising_sid = advertising_sid;
1756 p_cur->ble_tx_power = tx_power;
1757 p_cur->ble_periodic_adv_int = periodic_adv_int;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001758
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001759 if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
1760 ble_evt_type_is_scannable(evt_type) &&
1761 !ble_evt_type_is_scan_resp(evt_type)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001762 p_i->scan_rsp = false;
Myles Watson911d1ae2016-11-28 16:44:40 -08001763 } else
1764 p_i->scan_rsp = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001765
Myles Watson911d1ae2016-11-28 16:44:40 -08001766 if (p_i->inq_count != p_inq->inq_counter)
1767 p_cur->device_type = BT_DEVICE_TYPE_BLE;
1768 else
1769 p_cur->device_type |= BT_DEVICE_TYPE_BLE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001770
Myles Watson911d1ae2016-11-28 16:44:40 -08001771 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 -08001772
Myles Watson911d1ae2016-11-28 16:44:40 -08001773 p_i->inq_count = p_inq->inq_counter; /* Mark entry for current inquiry */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001774
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08001775 if (!data.empty()) {
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07001776 const uint8_t* p_flag =
1777 AdvertiseDataParser::GetFieldByType(data, BTM_BLE_AD_TYPE_FLAG, &len);
Myles Watson911d1ae2016-11-28 16:44:40 -08001778 if (p_flag != NULL) p_cur->flag = *p_flag;
1779 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001780
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08001781 if (!data.empty()) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001782 /* Check to see the BLE device has the Appearance UUID in the advertising
1783 * data. If it does
1784 * then try to convert the appearance value to a class of device value
1785 * Bluedroid can use.
1786 * Otherwise fall back to trying to infer if it is a HID device based on the
1787 * service class.
1788 */
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07001789 const uint8_t* p_uuid16 = AdvertiseDataParser::GetFieldByType(
1790 data, BTM_BLE_AD_TYPE_APPEARANCE, &len);
Myles Watson911d1ae2016-11-28 16:44:40 -08001791 if (p_uuid16 && len == 2) {
1792 btm_ble_appearance_to_cod((uint16_t)p_uuid16[0] | (p_uuid16[1] << 8),
1793 p_cur->dev_class);
1794 } else {
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07001795 p_uuid16 = AdvertiseDataParser::GetFieldByType(
1796 data, BTM_BLE_AD_TYPE_16SRV_CMPL, &len);
Myles Watson911d1ae2016-11-28 16:44:40 -08001797 if (p_uuid16 != NULL) {
1798 uint8_t i;
1799 for (i = 0; i + 2 <= len; i = i + 2) {
1800 /* if this BLE device support HID over LE, set HID Major in class of
1801 * device */
1802 if ((p_uuid16[i] | (p_uuid16[i + 1] << 8)) == UUID_SERVCLASS_LE_HID) {
1803 p_cur->dev_class[0] = 0;
1804 p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1805 p_cur->dev_class[2] = 0;
1806 break;
1807 }
Adam Hampsone6c74502014-05-30 15:07:08 -07001808 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001809 }
Zhihai Xu1237ee32013-11-26 18:18:29 -08001810 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001811 }
Zhihai Xu1237ee32013-11-26 18:18:29 -08001812
Myles Watson911d1ae2016-11-28 16:44:40 -08001813 /* if BR/EDR not supported is not set, assume is a DUMO device */
1814 if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001815 !ble_evt_type_is_directed(evt_type)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08001816 if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) {
1817 BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
1818 p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
1819 } else {
1820 BTM_TRACE_DEBUG("Random address, treating device as LE only");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001821 }
Myles Watson911d1ae2016-11-28 16:44:40 -08001822 } else {
1823 BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
1824 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001825}
1826
1827/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08001828 *
1829 * Function btm_clear_all_pending_le_entry
1830 *
1831 * Description This function is called to clear all LE pending entry in
1832 * inquiry database.
1833 *
1834 * Returns void
1835 *
1836 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08001837void btm_clear_all_pending_le_entry(void) {
1838 uint16_t xx;
1839 tINQ_DB_ENT* p_ent = btm_cb.btm_inq_vars.inq_db;
Satya Calloji444a8da2015-03-06 10:38:22 -08001840
Myles Watson911d1ae2016-11-28 16:44:40 -08001841 for (xx = 0; xx < BTM_INQ_DB_SIZE; xx++, p_ent++) {
1842 /* mark all pending LE entry as unused if an LE only device has scan
1843 * response outstanding */
1844 if ((p_ent->in_use) &&
1845 (p_ent->inq_info.results.device_type == BT_DEVICE_TYPE_BLE) &&
1846 !p_ent->scan_rsp)
1847 p_ent->in_use = false;
1848 }
Satya Calloji444a8da2015-03-06 10:38:22 -08001849}
1850
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001851void btm_ble_process_adv_addr(RawAddress& bda, uint8_t addr_type) {
Jakub Pawlowski801db302016-12-12 16:22:56 -08001852#if (BLE_PRIVACY_SPT == TRUE)
1853 /* map address to security record */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001854 bool match = btm_identity_addr_to_random_pseudo(&bda, &addr_type, false);
Jakub Pawlowski801db302016-12-12 16:22:56 -08001855
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001856 VLOG(1) << __func__ << ": bda=" << bda;
Jakub Pawlowski801db302016-12-12 16:22:56 -08001857 /* always do RRA resolution on host */
1858 if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) {
1859 tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda);
1860 if (match_rec) {
1861 match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001862 match_rec->ble.cur_rand_addr = bda;
Jakub Pawlowski801db302016-12-12 16:22:56 -08001863
1864 if (btm_ble_init_pseudo_addr(match_rec, bda)) {
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001865 bda = match_rec->bd_addr;
Jakub Pawlowski801db302016-12-12 16:22:56 -08001866 } else {
1867 // Assign the original address to be the current report address
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07001868 bda = match_rec->ble.pseudo_addr;
Jakub Pawlowski801db302016-12-12 16:22:56 -08001869 }
1870 }
1871 }
1872#endif
1873}
1874
1875/**
1876 * This function is called when extended advertising report event is received .
1877 * It updates the inquiry database. If the inquiry database is full, the oldest
1878 * entry is discarded.
1879 */
1880void btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) {
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001881 RawAddress bda, direct_address;
Jakub Pawlowski801db302016-12-12 16:22:56 -08001882 uint8_t* p = data;
1883 uint8_t addr_type, num_reports, pkt_data_len, primary_phy, secondary_phy,
1884 advertising_sid;
1885 int8_t rssi, tx_power;
1886 uint16_t event_type, periodic_adv_int, direct_address_type;
1887
1888 /* Only process the results if the inquiry is still active */
1889 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
1890
1891 /* Extract the number of reports in this event. */
1892 STREAM_TO_UINT8(num_reports, p);
1893
1894 while (num_reports--) {
1895 if (p > data + data_len) {
1896 // TODO(jpawlowski): we should crash the stack here
1897 BTM_TRACE_ERROR(
1898 "Malformed LE Extended Advertising Report Event from controller - "
1899 "can't loop the data");
1900 return;
1901 }
1902
1903 /* Extract inquiry results */
1904 STREAM_TO_UINT16(event_type, p);
1905 STREAM_TO_UINT8(addr_type, p);
Jakub Pawlowskib8a477e2017-06-16 15:16:15 -07001906 STREAM_TO_BDADDR(bda, p);
Jakub Pawlowski801db302016-12-12 16:22:56 -08001907 STREAM_TO_UINT8(primary_phy, p);
1908 STREAM_TO_UINT8(secondary_phy, p);
1909 STREAM_TO_UINT8(advertising_sid, p);
1910 STREAM_TO_INT8(tx_power, p);
1911 STREAM_TO_INT8(rssi, p);
1912 STREAM_TO_UINT16(periodic_adv_int, p);
1913 STREAM_TO_UINT8(direct_address_type, p);
Jakub Pawlowskib8a477e2017-06-16 15:16:15 -07001914 STREAM_TO_BDADDR(direct_address, p);
Jakub Pawlowski801db302016-12-12 16:22:56 -08001915 STREAM_TO_UINT8(pkt_data_len, p);
1916
1917 uint8_t* pkt_data = p;
1918 p += pkt_data_len; /* Advance to the the next packet*/
1919
1920 if (rssi >= 21 && rssi <= 126) {
1921 BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
1922 pkt_data_len, rssi);
1923 }
1924
Jakub Pawlowski801db302016-12-12 16:22:56 -08001925 btm_ble_process_adv_addr(bda, addr_type);
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08001926 btm_ble_process_adv_pkt_cont(event_type, addr_type, bda, primary_phy,
1927 secondary_phy, advertising_sid, tx_power, rssi,
1928 periodic_adv_int, pkt_data_len, pkt_data);
Jakub Pawlowski801db302016-12-12 16:22:56 -08001929 }
1930}
1931
1932/**
1933 * This function is called when advertising report event is received. It updates
1934 * the inquiry database. If the inquiry database is full, the oldest entry is
1935 * discarded.
1936 */
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001937void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
Jakub Pawlowskia484a882017-06-24 17:30:18 -07001938 RawAddress bda;
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001939 uint8_t* p = data;
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001940 uint8_t legacy_evt_type, addr_type, num_reports, pkt_data_len;
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001941 int8_t rssi;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001942
Myles Watson911d1ae2016-11-28 16:44:40 -08001943 /* Only process the results if the inquiry is still active */
1944 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
Satya Calloji444a8da2015-03-06 10:38:22 -08001945
Myles Watson911d1ae2016-11-28 16:44:40 -08001946 /* Extract the number of reports in this event. */
1947 STREAM_TO_UINT8(num_reports, p);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001948
Myles Watson911d1ae2016-11-28 16:44:40 -08001949 while (num_reports--) {
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001950 if (p > data + data_len) {
1951 // TODO(jpawlowski): we should crash the stack here
1952 BTM_TRACE_ERROR("Malformed LE Advertising Report Event from controller");
1953 return;
1954 }
1955
Myles Watson911d1ae2016-11-28 16:44:40 -08001956 /* Extract inquiry results */
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001957 STREAM_TO_UINT8(legacy_evt_type, p);
Myles Watson911d1ae2016-11-28 16:44:40 -08001958 STREAM_TO_UINT8(addr_type, p);
Jakub Pawlowskib8a477e2017-06-16 15:16:15 -07001959 STREAM_TO_BDADDR(bda, p);
Jakub Pawlowski9e300562016-12-07 10:54:44 -08001960 STREAM_TO_UINT8(pkt_data_len, p);
1961
1962 uint8_t* pkt_data = p;
1963 p += pkt_data_len; /* Advance to the the rssi byte */
1964
1965 STREAM_TO_INT8(rssi, p);
1966
1967 if (rssi >= 21 && rssi <= 126) {
1968 BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
1969 pkt_data_len, rssi);
1970 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001971
Jakub Pawlowski801db302016-12-12 16:22:56 -08001972 btm_ble_process_adv_addr(bda, addr_type);
Jakub Pawlowski9462c5d2016-12-06 15:40:58 -08001973
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08001974 uint16_t event_type;
1975 if (legacy_evt_type == 0x00) { // ADV_IND;
1976 event_type = 0x0013;
1977 } else if (legacy_evt_type == 0x01) { // ADV_DIRECT_IND;
1978 event_type = 0x0015;
1979 } else if (legacy_evt_type == 0x02) { // ADV_SCAN_IND;
1980 event_type = 0x0012;
1981 } else if (legacy_evt_type == 0x03) { // ADV_NONCONN_IND;
1982 event_type = 0x0010;
1983 } else if (legacy_evt_type == 0x04) { // SCAN_RSP;
1984 // We can't distinguish between "SCAN_RSP to an ADV_IND", and "SCAN_RSP to
1985 // an ADV_SCAN_IND", so always return "SCAN_RSP to an ADV_IND"
1986 event_type = 0x001B;
1987 } else {
1988 BTM_TRACE_ERROR(
1989 "Malformed LE Advertising Report Event - unsupported "
1990 "legacy_event_type 0x%02x",
1991 legacy_evt_type);
1992 return;
1993 }
1994
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08001995 btm_ble_process_adv_pkt_cont(
1996 event_type, addr_type, bda, PHY_LE_1M, PHY_LE_NO_PACKET, NO_ADI_PRESENT,
1997 TX_POWER_NOT_PRESENT, rssi, 0x00 /* no periodic adv */, pkt_data_len,
1998 pkt_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08001999 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002000}
2001
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08002002/**
2003 * This function is called after random address resolution is done, and proceed
2004 * to process adv packet.
2005 */
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08002006static void btm_ble_process_adv_pkt_cont(
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002007 uint16_t evt_type, uint8_t addr_type, const RawAddress& bda,
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002008 uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
2009 int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
2010 uint8_t* data) {
Myles Watson911d1ae2016-11-28 16:44:40 -08002011 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
Myles Watson911d1ae2016-11-28 16:44:40 -08002012 bool update = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002013
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002014 std::vector<uint8_t> tmp;
2015 if (data_len != 0) tmp.insert(tmp.begin(), data, data + data_len);
2016
2017 bool is_scannable = ble_evt_type_is_scannable(evt_type);
2018 bool is_scan_resp = ble_evt_type_is_scan_resp(evt_type);
2019
2020 // We might have send scan request to this device before, but didn't get the
2021 // response. In such case make sure data is put at start, not appended to
2022 // already existing data.
2023 bool is_start =
2024 ble_evt_type_is_legacy(evt_type) && is_scannable && !is_scan_resp;
2025 std::vector<uint8_t> const& adv_data =
2026 is_start ? cache.Set(addr_type, bda, std::move(tmp))
2027 : cache.Append(addr_type, bda, std::move(tmp));
2028
2029 bool data_complete = (ble_evt_type_data_status(evt_type) != 0x01);
2030
2031 if (!data_complete) {
2032 // If we didn't receive whole adv data yet, don't report the device.
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002033 DVLOG(1) << "Data not complete yet, waiting for more " << bda;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002034 return;
2035 }
2036
2037 bool is_active_scan =
2038 btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI;
2039 if (is_active_scan && is_scannable && !is_scan_resp) {
2040 // If we didn't receive scan response yet, don't report the device.
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002041 DVLOG(1) << " Waiting for scan response " << bda;
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002042 return;
2043 }
2044
Jakub Pawlowski103b2c42017-04-28 14:59:46 -07002045 if (!AdvertiseDataParser::IsValid(adv_data)) {
2046 DVLOG(1) << __func__ << "Dropping bad advertisement packet: "
2047 << base::HexEncode(adv_data.data(), adv_data.size());
2048 return;
2049 }
2050
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002051 tINQ_DB_ENT* p_i = btm_inq_db_find(bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002052
Myles Watson911d1ae2016-11-28 16:44:40 -08002053 /* Check if this address has already been processed for this inquiry */
2054 if (btm_inq_find_bdaddr(bda)) {
2055 /* never been report as an LE device */
2056 if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
2057 /* scan repsonse to be updated */
2058 (!p_i->scan_rsp))) {
2059 update = true;
2060 } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
2061 update = false;
2062 } else {
2063 /* if yes, skip it */
2064 return; /* assumption: one result per event */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002065 }
Myles Watson911d1ae2016-11-28 16:44:40 -08002066 }
2067 /* If existing entry, use that, else get a new one (possibly reusing the
2068 * oldest) */
2069 if (p_i == NULL) {
2070 p_i = btm_inq_db_new(bda);
2071 if (p_i != NULL) {
2072 p_inq->inq_cmpl_info.num_resp++;
2073 } else
2074 return;
2075 } else if (p_i->inq_count !=
2076 p_inq->inq_counter) /* first time seen in this inquiry */
2077 {
2078 p_inq->inq_cmpl_info.num_resp++;
2079 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002080
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002081 /* update the LE device information in inquiry database */
2082 btm_ble_update_inq_result(p_i, addr_type, bda, evt_type, primary_phy,
2083 secondary_phy, advertising_sid, tx_power, rssi,
2084 periodic_adv_int, adv_data);
2085
2086 uint8_t result = btm_ble_is_discoverable(bda, adv_data);
Myles Watson911d1ae2016-11-28 16:44:40 -08002087 if (result == 0) {
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002088 cache.Clear(addr_type, bda);
Myles Watson911d1ae2016-11-28 16:44:40 -08002089 LOG_WARN(LOG_TAG,
Jakub Pawlowski9e300562016-12-07 10:54:44 -08002090 "%s device no longer discoverable, discarding advertising packet",
Myles Watson911d1ae2016-11-28 16:44:40 -08002091 __func__);
2092 return;
2093 }
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002094
Myles Watson911d1ae2016-11-28 16:44:40 -08002095 if (!update) result &= ~BTM_BLE_INQ_RESULT;
2096 /* If the number of responses found and limited, issue a cancel inquiry */
2097 if (p_inq->inqparms.max_resps &&
2098 p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps) {
2099 /* new device */
2100 if (p_i == NULL ||
2101 /* assume a DUMO device, BR/EDR inquiry is always active */
2102 (p_i &&
2103 (p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ==
2104 BT_DEVICE_TYPE_BLE &&
2105 p_i->scan_rsp)) {
2106 BTM_TRACE_WARNING(
2107 "INQ RES: Extra Response Received...cancelling inquiry..");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002108
Myles Watson911d1ae2016-11-28 16:44:40 -08002109 /* if is non-periodic inquiry active, cancel now */
2110 if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
2111 (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
2112 btsnd_hcic_inq_cancel();
The Android Open Source Project5738f832012-12-12 16:00:35 -08002113
Myles Watson911d1ae2016-11-28 16:44:40 -08002114 btm_ble_stop_inquiry();
The Android Open Source Project5738f832012-12-12 16:00:35 -08002115
Myles Watson911d1ae2016-11-28 16:44:40 -08002116 btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002117 }
Myles Watson911d1ae2016-11-28 16:44:40 -08002118 }
Jakub Pawlowski83211b02016-12-07 11:25:15 -08002119
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -08002120 tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb;
Jakub Pawlowski83211b02016-12-07 11:25:15 -08002121 if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {
2122 (p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002123 const_cast<uint8_t*>(adv_data.data()), adv_data.size());
Jakub Pawlowski83211b02016-12-07 11:25:15 -08002124 }
Jakub Pawlowskid64bf4f2017-01-27 05:53:07 -08002125
2126 tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
Jakub Pawlowski83211b02016-12-07 11:25:15 -08002127 if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
2128 (p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002129 const_cast<uint8_t*>(adv_data.data()), adv_data.size());
Myles Watson911d1ae2016-11-28 16:44:40 -08002130 }
Jakub Pawlowski0595ca02017-02-07 12:15:06 -08002131
2132 cache.Clear(addr_type, bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002133}
2134
Jakub Pawlowskieafd45d2017-03-22 19:00:47 -07002135void btm_ble_process_phy_update_pkt(uint8_t len, uint8_t* data) {
2136 uint8_t status, tx_phy, rx_phy;
2137 uint16_t handle;
2138
2139 LOG_ASSERT(len == 5);
2140 uint8_t* p = data;
2141 STREAM_TO_UINT8(status, p);
2142 STREAM_TO_UINT16(handle, p);
2143 handle = handle & 0x0FFF;
2144 STREAM_TO_UINT8(tx_phy, p);
2145 STREAM_TO_UINT8(rx_phy, p);
2146
2147 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
2148 if (!p_dev_rec) {
2149 BTM_TRACE_WARNING("%s: No Device Found!", __func__);
2150 return;
2151 }
2152
2153 tGATT_TCB* p_tcb =
2154 gatt_find_tcb_by_addr(p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE);
2155 if (p_tcb == NULL) return;
2156
2157 gatt_notify_phy_updated(p_tcb, tx_phy, rx_phy, status);
2158}
2159
The Android Open Source Project5738f832012-12-12 16:00:35 -08002160/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002161 *
2162 * Function btm_ble_start_scan
2163 *
2164 * Description Start the BLE scan.
2165 *
2166 * Returns void
2167 *
2168 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002169tBTM_STATUS btm_ble_start_scan(void) {
2170 tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
Myles Watson911d1ae2016-11-28 16:44:40 -08002171 /* start scan, disable duplicate filtering */
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08002172 btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, p_inq->scan_duplicate_filter);
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002173
Myles Watson911d1ae2016-11-28 16:44:40 -08002174 if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI)
2175 btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
2176 else
2177 btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002178
Myles Watson911d1ae2016-11-28 16:44:40 -08002179 return BTM_CMD_STARTED;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002180}
2181
2182/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002183 *
2184 * Function btm_ble_stop_scan
2185 *
2186 * Description Stop the BLE scan.
2187 *
2188 * Returns void
2189 *
2190 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002191void btm_ble_stop_scan(void) {
2192 BTM_TRACE_EVENT("btm_ble_stop_scan ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002193
Jakub Pawlowski4d05ea52017-02-23 13:55:02 -08002194 if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI)
2195 btm_ble_clear_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
2196 else
2197 btm_ble_clear_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
2198
Myles Watson911d1ae2016-11-28 16:44:40 -08002199 /* Clear the inquiry callback if set */
2200 btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002201
Myles Watson911d1ae2016-11-28 16:44:40 -08002202 /* stop discovery now */
Jakub Pawlowski9df2a552016-12-02 11:34:06 -08002203 btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002204
Myles Watson911d1ae2016-11-28 16:44:40 -08002205 btm_update_scanner_filter_policy(SP_ADV_ALL);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002206}
2207/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002208 *
2209 * Function btm_ble_stop_inquiry
2210 *
2211 * Description Stop the BLE Inquiry.
2212 *
2213 * Returns void
2214 *
2215 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002216void btm_ble_stop_inquiry(void) {
2217 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
2218 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002219
Myles Watson911d1ae2016-11-28 16:44:40 -08002220 alarm_cancel(p_ble_cb->inq_var.inquiry_timer);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002221
Myles Watson911d1ae2016-11-28 16:44:40 -08002222 p_ble_cb->scan_activity &= ~BTM_BLE_INQUIRY_MASK;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002223
Myles Watson911d1ae2016-11-28 16:44:40 -08002224 /* If no more scan activity, stop LE scan now */
2225 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
2226 btm_ble_stop_scan();
2227 else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) ||
2228 (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
2229 BTM_TRACE_DEBUG("%s: setting default params for ongoing observe", __func__);
2230 btm_ble_stop_scan();
2231 btm_ble_start_scan();
2232 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002233
Myles Watson911d1ae2016-11-28 16:44:40 -08002234 /* If we have a callback registered for inquiry complete, call it */
2235 BTM_TRACE_DEBUG("BTM Inq Compl Callback: status 0x%02x, num results %d",
2236 p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002237
Myles Watson911d1ae2016-11-28 16:44:40 -08002238 btm_process_inq_complete(
2239 HCI_SUCCESS, (uint8_t)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002240}
2241
2242/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002243 *
2244 * Function btm_ble_stop_observe
2245 *
2246 * Description Stop the BLE Observe.
2247 *
2248 * Returns void
2249 *
2250 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002251static void btm_ble_stop_observe(void) {
2252 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
2253 tBTM_CMPL_CB* p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002254
Myles Watson911d1ae2016-11-28 16:44:40 -08002255 alarm_cancel(p_ble_cb->observer_timer);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002256
Myles Watson911d1ae2016-11-28 16:44:40 -08002257 p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002258
Myles Watson911d1ae2016-11-28 16:44:40 -08002259 p_ble_cb->p_obs_results_cb = NULL;
2260 p_ble_cb->p_obs_cmpl_cb = NULL;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002261
Myles Watson911d1ae2016-11-28 16:44:40 -08002262 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_stop_scan();
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002263
Myles Watson911d1ae2016-11-28 16:44:40 -08002264 if (p_obs_cb)
2265 (p_obs_cb)((tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002266}
2267/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002268 *
2269 * Function btm_ble_adv_states_operation
2270 *
2271 * Description Set or clear adv states in topology mask
2272 *
2273 * Returns operation status. true if sucessful, false otherwise.
2274 *
2275 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002276typedef bool(BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
2277static bool btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR* p_handler,
2278 uint8_t adv_evt) {
2279 bool rt = false;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002280
Myles Watson911d1ae2016-11-28 16:44:40 -08002281 switch (adv_evt) {
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002282 case BTM_BLE_CONNECT_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002283 rt = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
2284 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002285
Myles Watson911d1ae2016-11-28 16:44:40 -08002286 case BTM_BLE_NON_CONNECT_EVT:
2287 rt = (*p_handler)(BTM_BLE_STATE_NON_CONN_ADV_BIT);
2288 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002289 case BTM_BLE_CONNECT_DIR_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002290 rt = (*p_handler)(BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
2291 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002292
2293 case BTM_BLE_DISCOVER_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002294 rt = (*p_handler)(BTM_BLE_STATE_SCAN_ADV_BIT);
2295 break;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002296
Satya Calloji444a8da2015-03-06 10:38:22 -08002297 case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
Myles Watson911d1ae2016-11-28 16:44:40 -08002298 rt = (*p_handler)(BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT);
2299 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08002300
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002301 default:
Myles Watson911d1ae2016-11-28 16:44:40 -08002302 BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
2303 break;
2304 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002305
Myles Watson911d1ae2016-11-28 16:44:40 -08002306 return rt;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002307}
2308
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002309/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002310 *
2311 * Function btm_ble_start_adv
2312 *
2313 * Description start the BLE advertising.
2314 *
2315 * Returns void
2316 *
2317 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002318tBTM_STATUS btm_ble_start_adv(void) {
2319 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002320
Myles Watson911d1ae2016-11-28 16:44:40 -08002321 if (!btm_ble_adv_states_operation(btm_ble_topology_check, p_cb->evt_type))
2322 return BTM_WRONG_MODE;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002323
Marie Janssend19e0782016-07-15 12:48:27 -07002324#if (BLE_PRIVACY_SPT == TRUE)
Myles Watson911d1ae2016-11-28 16:44:40 -08002325 /* To relax resolving list, always have resolving list enabled, unless
2326 * directed adv */
2327 if (p_cb->evt_type != BTM_BLE_CONNECT_LO_DUTY_DIR_EVT &&
2328 p_cb->evt_type != BTM_BLE_CONNECT_DIR_EVT)
2329 /* enable resolving list is desired */
2330 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_ADV);
Satya Calloji444a8da2015-03-06 10:38:22 -08002331#endif
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002332
Myles Watson911d1ae2016-11-28 16:44:40 -08002333 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_ENABLE);
2334 p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
2335 btm_ble_adv_states_operation(btm_ble_set_topology_mask, p_cb->evt_type);
2336 return BTM_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002337}
Satya Calloji444a8da2015-03-06 10:38:22 -08002338
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002339/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002340 *
2341 * Function btm_ble_stop_adv
2342 *
2343 * Description Stop the BLE advertising.
2344 *
2345 * Returns void
2346 *
2347 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002348tBTM_STATUS btm_ble_stop_adv(void) {
2349 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002350
Myles Watson911d1ae2016-11-28 16:44:40 -08002351 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2352 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_DISABLE);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002353
Myles Watson911d1ae2016-11-28 16:44:40 -08002354 p_cb->fast_adv_on = false;
2355 p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
Jakub Pawlowskib6ab9b32016-10-10 09:35:13 -07002356
Myles Watson911d1ae2016-11-28 16:44:40 -08002357 /* clear all adv states */
2358 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2359 }
2360 return BTM_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002361}
2362
Myles Watson911d1ae2016-11-28 16:44:40 -08002363static void btm_ble_fast_adv_timer_timeout(UNUSED_ATTR void* data) {
2364 /* fast adv is completed, fall back to slow adv interval */
2365 btm_ble_start_slow_adv();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002366}
2367
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002368/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002369 *
2370 * Function btm_ble_start_slow_adv
2371 *
2372 * Description Restart adv with slow adv interval
2373 *
2374 * Returns void
2375 *
2376 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002377static void btm_ble_start_slow_adv(void) {
2378 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002379
Myles Watson911d1ae2016-11-28 16:44:40 -08002380 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2381 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
Jakub Pawlowskib707f442017-07-03 15:39:36 -07002382 RawAddress address = RawAddress::kEmpty;
Myles Watson911d1ae2016-11-28 16:44:40 -08002383 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
2384 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
Satya Calloji444a8da2015-03-06 10:38:22 -08002385
Myles Watson911d1ae2016-11-28 16:44:40 -08002386 btm_ble_stop_adv();
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002387
Myles Watson911d1ae2016-11-28 16:44:40 -08002388 p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
Jakub Pawlowskib707f442017-07-03 15:39:36 -07002389 p_cb, address, &init_addr_type, &own_addr_type);
Satya Calloji444a8da2015-03-06 10:38:22 -08002390
Myles Watson911d1ae2016-11-28 16:44:40 -08002391 /* slow adv mode never goes into directed adv */
Jakub Pawlowskib707f442017-07-03 15:39:36 -07002392 btsnd_hcic_ble_write_adv_params(
2393 BTM_BLE_GAP_ADV_SLOW_INT, BTM_BLE_GAP_ADV_SLOW_INT, p_cb->evt_type,
2394 own_addr_type, init_addr_type, address, p_cb->adv_chnl_map, p_cb->afp);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002395
Myles Watson911d1ae2016-11-28 16:44:40 -08002396 btm_ble_start_adv();
2397 }
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002398}
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002399
Myles Watson911d1ae2016-11-28 16:44:40 -08002400static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(
2401 UNUSED_ATTR void* data) {
2402 /* lim_timeout expired, limited discovery should exit now */
2403 btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
2404 btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode,
2405 btm_cb.btm_inq_vars.discoverable_mode);
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002406}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002407
Myles Watson911d1ae2016-11-28 16:44:40 -08002408static void btm_ble_inquiry_timer_timeout(UNUSED_ATTR void* data) {
2409 btm_ble_stop_inquiry();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002410}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002411
Myles Watson911d1ae2016-11-28 16:44:40 -08002412static void btm_ble_observer_timer_timeout(UNUSED_ATTR void* data) {
2413 btm_ble_stop_observe();
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002414}
The Android Open Source Project5738f832012-12-12 16:00:35 -08002415
Myles Watson911d1ae2016-11-28 16:44:40 -08002416void btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) {
2417 if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
2418 /* refresh the random addr */
Jakub Pawlowski0c683232017-02-24 09:49:59 -08002419 btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
Myles Watson911d1ae2016-11-28 16:44:40 -08002420 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002421}
2422
The Android Open Source Project5738f832012-12-12 16:00:35 -08002423/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002424 *
2425 * Function btm_ble_read_remote_features_complete
2426 *
2427 * Description This function is called when the command complete message
Myles Watson9ca07092016-11-28 16:41:53 -08002428 * is received from the HCI for the read LE remote feature
2429 * supported complete event.
Myles Watsonee96a3c2016-11-23 14:49:54 -08002430 *
2431 * Returns void
2432 *
2433 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002434void btm_ble_read_remote_features_complete(uint8_t* p) {
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302435 BTM_TRACE_EVENT("%s", __func__);
2436
Myles Watson911d1ae2016-11-28 16:44:40 -08002437 uint16_t handle;
2438 uint8_t status;
Myles Watson911d1ae2016-11-28 16:44:40 -08002439 STREAM_TO_UINT8(status, p);
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302440 STREAM_TO_UINT16(handle, p);
2441 handle = handle & 0x0FFF; // only 12 bits meaningful
Satya Calloji444a8da2015-03-06 10:38:22 -08002442
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302443 if (status != HCI_SUCCESS) {
Jakub Pawlowski341b6db2017-04-24 06:34:11 -07002444 BTM_TRACE_ERROR("%s: failed for handle: 0x%04d, status 0x%02x", __func__,
2445 handle, status);
2446 if (status != HCI_ERR_UNSUPPORTED_REM_FEATURE) return;
Myles Watson911d1ae2016-11-28 16:44:40 -08002447 }
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302448
2449 int idx = btm_handle_to_acl_index(handle);
2450 if (idx == MAX_L2CAP_LINKS) {
2451 BTM_TRACE_ERROR("%s: can't find acl for handle: 0x%04d", __func__, handle);
2452 return;
2453 }
2454
Jakub Pawlowski341b6db2017-04-24 06:34:11 -07002455 if (status == HCI_SUCCESS) {
2456 STREAM_TO_ARRAY(btm_cb.acl_db[idx].peer_le_features, p, BD_FEATURES_LEN);
2457 }
Nagarjuna Kristam86d5b562016-12-13 14:30:45 +05302458
2459 btsnd_hcic_rmt_ver_req(handle);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002460}
2461
2462/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002463 *
2464 * Function btm_ble_write_adv_enable_complete
2465 *
2466 * Description This function process the write adv enable command complete.
2467 *
2468 * Returns void
2469 *
2470 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002471void btm_ble_write_adv_enable_complete(uint8_t* p) {
2472 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002473
Myles Watson911d1ae2016-11-28 16:44:40 -08002474 /* if write adv enable/disbale not succeed */
2475 if (*p != HCI_SUCCESS) {
2476 /* toggle back the adv mode */
2477 p_cb->adv_mode = !p_cb->adv_mode;
2478 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002479}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002480
2481/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002482 *
2483 * Function btm_ble_dir_adv_tout
2484 *
2485 * Description when directed adv time out
2486 *
2487 * Returns void
2488 *
2489 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002490void btm_ble_dir_adv_tout(void) {
2491 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002492
Myles Watson911d1ae2016-11-28 16:44:40 -08002493 /* make device fall back into undirected adv mode by default */
2494 btm_cb.ble_ctr_cb.inq_var.directed_conn = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002495}
2496
2497/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002498 *
2499 * Function btm_ble_set_topology_mask
2500 *
2501 * Description set BLE topology mask
2502 *
2503 * Returns true is request is allowed, false otherwise.
2504 *
2505 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002506bool btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
2507 request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2508 btm_cb.ble_ctr_cb.cur_states |= (request_state_mask & BTM_BLE_STATE_ALL_MASK);
2509 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08002510}
2511
2512/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002513 *
2514 * Function btm_ble_clear_topology_mask
2515 *
2516 * Description Clear BLE topology bit mask
2517 *
2518 * Returns true is request is allowed, false otherwise.
2519 *
2520 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002521bool btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
2522 request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2523 btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
2524 return true;
Satya Calloji444a8da2015-03-06 10:38:22 -08002525}
2526
2527/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002528 *
2529 * Function btm_ble_update_link_topology_mask
2530 *
2531 * Description This function update the link topology mask
2532 *
2533 * Returns void
2534 *
2535 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002536void btm_ble_update_link_topology_mask(uint8_t link_role, bool increase) {
2537 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_CONN_MASK);
Satya Calloji444a8da2015-03-06 10:38:22 -08002538
Myles Watson911d1ae2016-11-28 16:44:40 -08002539 if (increase)
2540 btm_cb.ble_ctr_cb.link_count[link_role]++;
2541 else if (btm_cb.ble_ctr_cb.link_count[link_role] > 0)
2542 btm_cb.ble_ctr_cb.link_count[link_role]--;
Satya Calloji444a8da2015-03-06 10:38:22 -08002543
Myles Watson911d1ae2016-11-28 16:44:40 -08002544 if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_MASTER])
2545 btm_ble_set_topology_mask(BTM_BLE_STATE_MASTER_BIT);
Satya Calloji444a8da2015-03-06 10:38:22 -08002546
Myles Watson911d1ae2016-11-28 16:44:40 -08002547 if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_SLAVE])
2548 btm_ble_set_topology_mask(BTM_BLE_STATE_SLAVE_BIT);
Satya Calloji444a8da2015-03-06 10:38:22 -08002549
Myles Watson911d1ae2016-11-28 16:44:40 -08002550 if (link_role == HCI_ROLE_SLAVE && increase) {
2551 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2552 /* make device fall back into undirected adv mode by default */
2553 btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
2554 /* clear all adv states */
2555 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2556 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002557}
2558
2559/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002560 *
2561 * Function btm_ble_update_mode_operation
2562 *
Myles Watson9ca07092016-11-28 16:41:53 -08002563 * Description This function update the GAP role operation when a link
2564 * status is updated.
Myles Watsonee96a3c2016-11-23 14:49:54 -08002565 *
2566 * Returns void
2567 *
2568 ******************************************************************************/
Jakub Pawlowskia484a882017-06-24 17:30:18 -07002569void btm_ble_update_mode_operation(uint8_t link_role, const RawAddress* bd_addr,
2570 uint8_t status) {
Myles Watson911d1ae2016-11-28 16:44:40 -08002571 if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
2572 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2573 /* make device fall back into undirected adv mode by default */
2574 btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
2575 /* clear all adv states */
2576 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2577 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002578
Myles Watson911d1ae2016-11-28 16:44:40 -08002579 if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE) {
2580 btm_ble_set_connectability(btm_cb.btm_inq_vars.connectable_mode |
2581 btm_cb.ble_ctr_cb.inq_var.connectable_mode);
2582 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002583
Emil Lenngren8451ad02016-12-19 19:21:19 +00002584 /* in case of disconnected, we must cancel bgconn and restart
2585 in order to add back device to white list in order to reconnect */
Jakub Pawlowskic2276b02017-06-09 16:00:25 -07002586 btm_ble_bgconn_cancel_if_disconnected(*bd_addr);
Emil Lenngren8451ad02016-12-19 19:21:19 +00002587
Myles Watson911d1ae2016-11-28 16:44:40 -08002588 /* when no connection is attempted, and controller is not rejecting last
2589 request
2590 due to resource limitation, start next direct connection or background
2591 connection
2592 now in order */
2593 if (btm_ble_get_conn_st() == BLE_CONN_IDLE &&
2594 status != HCI_ERR_HOST_REJECT_RESOURCES &&
Emil Lenngrenaf7f49c2016-12-12 18:47:12 +00002595 status != HCI_ERR_MAX_NUM_OF_CONNECTIONS &&
Myles Watson911d1ae2016-11-28 16:44:40 -08002596 !btm_send_pending_direct_conn()) {
2597 btm_ble_resume_bg_conn();
2598 }
Satya Calloji444a8da2015-03-06 10:38:22 -08002599}
2600
2601/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002602 *
2603 * Function btm_ble_init
2604 *
2605 * Description Initialize the control block variable values.
2606 *
2607 * Returns void
2608 *
2609 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002610void btm_ble_init(void) {
2611 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002612
Myles Watson911d1ae2016-11-28 16:44:40 -08002613 BTM_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002614
Myles Watson911d1ae2016-11-28 16:44:40 -08002615 alarm_free(p_cb->observer_timer);
2616 alarm_free(p_cb->inq_var.fast_adv_timer);
2617 memset(p_cb, 0, sizeof(tBTM_BLE_CB));
2618 memset(&(btm_cb.cmn_ble_vsc_cb), 0, sizeof(tBTM_BLE_VSC_CB));
2619 btm_cb.cmn_ble_vsc_cb.values_read = false;
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002620
Myles Watson911d1ae2016-11-28 16:44:40 -08002621 p_cb->observer_timer = alarm_new("btm_ble.observer_timer");
2622 p_cb->cur_states = 0;
2623 p_cb->conn_pending_q = fixed_queue_new(SIZE_MAX);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002624
Myles Watson911d1ae2016-11-28 16:44:40 -08002625 p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2626 p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
2627 p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
2628 p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
2629 p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
2630 p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
2631 p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
2632 p_cb->inq_var.fast_adv_timer = alarm_new("btm_ble_inq.fast_adv_timer");
2633 p_cb->inq_var.inquiry_timer = alarm_new("btm_ble_inq.inquiry_timer");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002634
Myles Watson911d1ae2016-11-28 16:44:40 -08002635 /* for background connection, reset connection params to be undefined */
2636 p_cb->scan_int = p_cb->scan_win = BTM_BLE_SCAN_PARAM_UNDEF;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002637
Myles Watson911d1ae2016-11-28 16:44:40 -08002638 p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
Satya Callojic4e25962014-05-10 23:46:24 -07002639
Myles Watson911d1ae2016-11-28 16:44:40 -08002640 p_cb->addr_mgnt_cb.refresh_raddr_timer =
2641 alarm_new("btm_ble_addr.refresh_raddr_timer");
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -08002642
Marie Janssend19e0782016-07-15 12:48:27 -07002643#if (BLE_VND_INCLUDED == FALSE)
Myles Watson911d1ae2016-11-28 16:44:40 -08002644 btm_ble_adv_filter_init();
Satya Calloji1a9247a2014-06-05 13:15:15 -07002645#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002646}
2647
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002648/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -08002649 *
2650 * Function btm_ble_topology_check
2651 *
Myles Watson9ca07092016-11-28 16:41:53 -08002652 * Description check to see requested state is supported. One state check
2653 * at a time is supported
Myles Watsonee96a3c2016-11-23 14:49:54 -08002654 *
2655 * Returns true is request is allowed, false otherwise.
2656 *
2657 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -08002658bool btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask) {
2659 bool rt = false;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002660
Myles Watson911d1ae2016-11-28 16:44:40 -08002661 uint8_t state_offset = 0;
2662 uint16_t cur_states = btm_cb.ble_ctr_cb.cur_states;
Myles Watson911d1ae2016-11-28 16:44:40 -08002663 uint8_t request_state = 0;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002664
Myles Watson911d1ae2016-11-28 16:44:40 -08002665 /* check only one bit is set and within valid range */
2666 if (request_state_mask == BTM_BLE_STATE_INVALID ||
2667 request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
2668 (request_state_mask & (request_state_mask - 1)) != 0) {
2669 BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002670 return rt;
Myles Watson911d1ae2016-11-28 16:44:40 -08002671 }
2672
2673 while (request_state_mask) {
2674 request_state_mask >>= 1;
2675 request_state++;
2676 }
2677
2678 /* check if the requested state is supported or not */
Jakub Pawlowski5e43c802017-06-26 17:22:35 -07002679 uint8_t bit_num = btm_le_state_combo_tbl[0][request_state - 1];
Myles Watson911d1ae2016-11-28 16:44:40 -08002680 const uint8_t* ble_supported_states =
2681 controller_get_interface()->get_ble_supported_states();
2682
Jakub Pawlowski5e43c802017-06-26 17:22:35 -07002683 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, bit_num)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08002684 BTM_TRACE_ERROR("state requested not supported: %d", request_state);
2685 return rt;
2686 }
2687
2688 rt = true;
2689 /* make sure currently active states are all supported in conjunction with the
Jakub Pawlowski5e43c802017-06-26 17:22:35 -07002690 requested state. If the bit in table is UNSUPPORTED, the combination is not
2691 supported */
Myles Watson911d1ae2016-11-28 16:44:40 -08002692 while (cur_states != 0) {
2693 if (cur_states & 0x01) {
Jakub Pawlowski5e43c802017-06-26 17:22:35 -07002694 uint8_t bit_num = btm_le_state_combo_tbl[request_state][state_offset];
2695 if (bit_num != UNSUPPORTED) {
2696 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, bit_num)) {
Myles Watson911d1ae2016-11-28 16:44:40 -08002697 rt = false;
2698 break;
2699 }
2700 }
2701 }
2702 cur_states >>= 1;
2703 state_offset++;
2704 }
2705 return rt;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -07002706}