1 /****************************************************************************** 2 * 3 * Copyright (C) 2008-2014 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * 21 * This file contains functions for BLE GAP. 22 * 23 ******************************************************************************/ 24 25 #define LOG_TAG "bt_btm_ble" 26 27 #include <base/bind.h> 28 #include <base/callback.h> 29 #include <base/strings/string_number_conversions.h> 30 #include <stddef.h> 31 #include <stdio.h> 32 #include <string.h> 33 #include <list> 34 #include <vector> 35 36 #include "bt_types.h" 37 #include "bt_utils.h" 38 #include "btm_ble_api.h" 39 #include "btm_int.h" 40 #include "btu.h" 41 #include "device/include/controller.h" 42 #include "gap_api.h" 43 #include "hcimsgs.h" 44 #include "osi/include/osi.h" 45 46 #include "advertise_data_parser.h" 47 #include "btm_ble_int.h" 48 #include "gatt_int.h" 49 #include "gattdefs.h" 50 #include "l2c_int.h" 51 #include "osi/include/log.h" 52 53 #define BTM_BLE_NAME_SHORT 0x01 54 #define BTM_BLE_NAME_CMPL 0x02 55 56 #define BTM_BLE_FILTER_TARGET_UNKNOWN 0xff 57 #define BTM_BLE_POLICY_UNKNOWN 0xff 58 59 #define BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS (30 * 1000) 60 #define MIN_ADV_LENGTH 2 61 #define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE 9 62 63 extern fixed_queue_t* btu_general_alarm_queue; 64 65 namespace { 66 67 class AdvertisingCache { 68 public: 69 /* Set the data to |data| for device |addr_type, addr| */ 70 const std::vector<uint8_t>& Set(uint8_t addr_type, BD_ADDR addr, 71 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| */ 87 const std::vector<uint8_t>& Append(uint8_t addr_type, BD_ADDR addr, 88 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| */ 104 void Clear(uint8_t addr_type, BD_ADDR addr) { 105 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; 114 BD_ADDR addr; 115 std::vector<uint8_t> data; 116 117 Item(uint8_t addr_type, BD_ADDR addr, std::vector<uint8_t> data) 118 : addr_type(addr_type), data(data) { 119 memcpy(this->addr, addr, BD_ADDR_LEN); 120 } 121 }; 122 123 std::list<Item>::iterator Find(uint8_t addr_type, BD_ADDR addr) { 124 for (auto it = items.begin(); it != items.end(); it++) { 125 if (it->addr_type == addr_type && 126 memcmp(it->addr, addr, BD_ADDR_LEN) == 0) { 127 return it; 128 } 129 } 130 return items.end(); 131 } 132 133 /* we keep maximum 7 devices in the cache */ 134 const size_t cache_max = 7; 135 std::list<Item> items; 136 }; 137 138 /* Devices in this cache are waiting for eiter scan response, or chained packets 139 * on secondary channel */ 140 AdvertisingCache cache; 141 142 } // namespace 143 144 #if (BLE_VND_INCLUDED == TRUE) 145 static tBTM_BLE_CTRL_FEATURES_CBACK* p_ctrl_le_feature_rd_cmpl_cback = NULL; 146 #endif 147 148 /******************************************************************************* 149 * Local functions 150 ******************************************************************************/ 151 static void btm_ble_update_adv_flag(uint8_t flag); 152 static void btm_ble_process_adv_pkt_cont( 153 uint16_t evt_type, uint8_t addr_type, BD_ADDR bda, uint8_t primary_phy, 154 uint8_t secondary_phy, uint8_t advertising_sid, int8_t tx_power, 155 int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len, uint8_t* data); 156 static uint8_t btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB* p_cb, 157 BD_ADDR_PTR p_peer_addr_ptr, 158 tBLE_ADDR_TYPE* p_peer_addr_type, 159 tBLE_ADDR_TYPE* p_own_addr_type); 160 static void btm_ble_stop_observe(void); 161 static void btm_ble_fast_adv_timer_timeout(void* data); 162 static void btm_ble_start_slow_adv(void); 163 static void btm_ble_inquiry_timer_gap_limited_discovery_timeout(void* data); 164 static void btm_ble_inquiry_timer_timeout(void* data); 165 static void btm_ble_observer_timer_timeout(void* data); 166 167 #define BTM_BLE_INQ_RESULT 0x01 168 #define BTM_BLE_OBS_RESULT 0x02 169 170 bool ble_evt_type_is_connectable(uint16_t evt_type) { 171 return evt_type & (1 << BLE_EVT_CONNECTABLE_BIT); 172 } 173 174 bool ble_evt_type_is_scannable(uint16_t evt_type) { 175 return evt_type & (1 << BLE_EVT_SCANNABLE_BIT); 176 } 177 178 bool ble_evt_type_is_directed(uint16_t evt_type) { 179 return evt_type & (1 << BLE_EVT_DIRECTED_BIT); 180 } 181 182 bool ble_evt_type_is_scan_resp(uint16_t evt_type) { 183 return evt_type & (1 << BLE_EVT_SCAN_RESPONSE_BIT); 184 } 185 186 bool ble_evt_type_is_legacy(uint16_t evt_type) { 187 return evt_type & (1 << BLE_EVT_LEGACY_BIT); 188 } 189 190 uint8_t ble_evt_type_data_status(uint16_t evt_type) { 191 return (evt_type >> 5) & 3; 192 } 193 194 /* LE states combo bit to check */ 195 const uint8_t btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX][2] = 196 {{ 197 /* single state support */ 198 {HCI_SUPP_LE_STATES_CONN_ADV_MASK, 199 HCI_SUPP_LE_STATES_CONN_ADV_OFF}, /* conn_adv */ 200 {HCI_SUPP_LE_STATES_INIT_MASK, HCI_SUPP_LE_STATES_INIT_OFF}, /* init */ 201 {HCI_SUPP_LE_STATES_INIT_MASK, 202 HCI_SUPP_LE_STATES_INIT_OFF}, /* master */ 203 {HCI_SUPP_LE_STATES_SLAVE_MASK, 204 HCI_SUPP_LE_STATES_SLAVE_OFF}, /* slave */ 205 {0, 0}, /* todo: lo du dir adv, not covered ? */ 206 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASK, 207 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_OFF}, /* hi duty dir adv */ 208 {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASK, 209 HCI_SUPP_LE_STATES_NON_CONN_ADV_OFF}, /* non connectable adv */ 210 {HCI_SUPP_LE_STATES_PASS_SCAN_MASK, 211 HCI_SUPP_LE_STATES_PASS_SCAN_OFF}, /* passive scan */ 212 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASK, 213 HCI_SUPP_LE_STATES_ACTIVE_SCAN_OFF}, /* active scan */ 214 {HCI_SUPP_LE_STATES_SCAN_ADV_MASK, 215 HCI_SUPP_LE_STATESSCAN_ADV_OFF} /* scanable adv */ 216 }, 217 { 218 /* conn_adv =0 */ 219 {0, 0}, /* conn_adv */ 220 {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, 221 HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF}, /* init: 32 */ 222 {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, 223 HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF}, /* master: 35 */ 224 {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, 225 HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/ 226 {0, 0}, /* lo du dir adv */ 227 {0, 0}, /* hi duty dir adv */ 228 {0, 0}, /* non connectable adv */ 229 {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, 230 HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* passive scan */ 231 {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, 232 HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* active scan */ 233 {0, 0} /* scanable adv */ 234 }, 235 { 236 /* init */ 237 {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, 238 HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF}, /* conn_adv: 32 */ 239 {0, 0}, /* init */ 240 {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, 241 HCI_SUPP_LE_STATES_INIT_MASTER_OFF}, /* master 28 */ 242 {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, 243 HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* slave 41 */ 244 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, 245 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF}, /* lo du dir adv 34 */ 246 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, 247 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* hi duty dir adv 33 */ 248 {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, 249 HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* non connectable adv */ 250 {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, 251 HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* passive scan */ 252 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, 253 HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* active scan */ 254 {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, 255 HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF} /* scanable adv */ 256 257 }, 258 { 259 /* master */ 260 {HCI_SUPP_LE_STATES_CONN_ADV_MASTER_MASK, 261 HCI_SUPP_LE_STATES_CONN_ADV_MASTER_OFF}, /* conn_adv: 35 */ 262 {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, 263 HCI_SUPP_LE_STATES_INIT_MASTER_OFF}, /* init 28 */ 264 {HCI_SUPP_LE_STATES_INIT_MASTER_MASK, 265 HCI_SUPP_LE_STATES_INIT_MASTER_OFF}, /* master 28 */ 266 {HCI_SUPP_LE_STATES_CONN_ADV_INIT_MASK, 267 HCI_SUPP_LE_STATES_CONN_ADV_INIT_OFF}, /* slave: 32 */ 268 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, 269 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* lo duty cycle adv 270 37 */ 271 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, 272 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* hi duty cycle adv 273 36 */ 274 {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, 275 HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* non connectable adv 276 */ 277 {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, 278 HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* passive scan */ 279 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, 280 HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* active scan */ 281 {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, 282 HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF} /* scanable adv */ 283 284 }, 285 { 286 /* slave */ 287 {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, 288 HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* conn_adv: 38,*/ 289 {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, 290 HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* init 41 */ 291 {HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_MASK, 292 HCI_SUPP_LE_STATES_INIT_MASTER_SLAVE_OFF}, /* master 41 */ 293 {HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_MASK, 294 HCI_SUPP_LE_STATES_CONN_ADV_SLAVE_OFF}, /* slave: 38,*/ 295 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, 296 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* lo duty cycle adv 40 297 */ 298 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, 299 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF}, /* hi duty cycle adv 39 300 */ 301 {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, 302 HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF}, /* non connectable adv */ 303 {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, 304 HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF}, /* passive scan */ 305 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, 306 HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF}, /* active scan */ 307 {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, 308 HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF} /* scanable adv */ 309 310 }, 311 { 312 /* lo duty cycle adv */ 313 {0, 0}, /* conn_adv: 38,*/ 314 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_MASK, 315 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_INIT_OFF}, /* init 34 */ 316 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_MASK, 317 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_MASTER_OFF}, /* master 37 */ 318 {HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_MASK, 319 HCI_SUPP_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 40 */ 320 {0, 0}, /* lo duty cycle adv 40 */ 321 {0, 0}, /* hi duty cycle adv 39 */ 322 {0, 0}, /* non connectable adv */ 323 {0, 0}, /* TODO: passive scan, not covered? */ 324 {0, 0}, /* TODO: active scan, not covered? */ 325 {0, 0} /* scanable adv */ 326 }, 327 { 328 /* hi duty cycle adv */ 329 {0, 0}, /* conn_adv: 38,*/ 330 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_MASK, 331 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_INIT_OFF}, /* init 33 */ 332 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_MASK, 333 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_MASTER_OFF}, /* master 36 */ 334 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_MASK, 335 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_OFF}, /* slave: 39*/ 336 {0, 0}, /* lo duty cycle adv 40 */ 337 {0, 0}, /* hi duty cycle adv 39 */ 338 {0, 0}, /* non connectable adv */ 339 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, 340 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF}, /* passive scan */ 341 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, 342 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF}, /* active scan */ 343 {0, 0} /* scanable adv */ 344 }, 345 { 346 /* non connectable adv */ 347 {0, 0}, /* conn_adv: */ 348 {HCI_SUPP_LE_STATES_NON_CONN_INIT_MASK, 349 HCI_SUPP_LE_STATES_NON_CONN_INIT_OFF}, /* init */ 350 {HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_MASK, 351 HCI_SUPP_LE_STATES_NON_CONN_ADV_MASTER_OFF}, /* master */ 352 {HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_MASK, 353 HCI_SUPP_LE_STATES_NON_CONN_ADV_SLAVE_OFF}, /* slave: */ 354 {0, 0}, /* lo duty cycle adv */ 355 {0, 0}, /* hi duty cycle adv */ 356 {0, 0}, /* non connectable adv */ 357 {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, 358 HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF}, /* passive scan */ 359 {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, 360 HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF}, /* active scan */ 361 {0, 0} /* scanable adv */ 362 }, 363 { 364 /* passive scan */ 365 {HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_MASK, 366 HCI_SUPP_LE_STATES_CONN_ADV_PASS_SCAN_OFF}, /* conn_adv: */ 367 {HCI_SUPP_LE_STATES_PASS_SCAN_INIT_MASK, 368 HCI_SUPP_LE_STATES_PASS_SCAN_INIT_OFF}, /* init */ 369 {HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_MASK, 370 HCI_SUPP_LE_STATES_PASS_SCAN_MASTER_OFF}, /* master */ 371 {HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_MASK, 372 HCI_SUPP_LE_STATES_PASS_SCAN_SLAVE_OFF}, /* slave: */ 373 {0, 0}, /* lo duty cycle adv */ 374 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_MASK, 375 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_OFF}, /* hi duty cycle 376 adv */ 377 {HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_MASK, 378 HCI_SUPP_LE_STATES_NON_CONN_ADV_PASS_SCAN_OFF}, /* non connectable 379 adv */ 380 {0, 0}, /* passive scan */ 381 {0, 0}, /* active scan */ 382 {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, 383 HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF} /* scanable adv */ 384 }, 385 { 386 /* active scan */ 387 {HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_MASK, 388 HCI_SUPP_LE_STATES_CONN_ADV_ACTIVE_SCAN_OFF}, /* conn_adv: */ 389 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_MASK, 390 HCI_SUPP_LE_STATES_ACTIVE_SCAN_INIT_OFF}, /* init */ 391 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_MASK, 392 HCI_SUPP_LE_STATES_ACTIVE_SCAN_MASTER_OFF}, /* master */ 393 {HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_MASK, 394 HCI_SUPP_LE_STATES_ACTIVE_SCAN_SLAVE_OFF}, /* slave: */ 395 {0, 0}, /* lo duty cycle adv */ 396 {HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_MASK, 397 HCI_SUPP_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_OFF}, /* hi duty 398 cycle adv 399 */ 400 {HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_MASK, 401 HCI_SUPP_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_OFF}, /* non 402 connectable 403 adv */ 404 {0, 0}, /* TODO: passive scan */ 405 {0, 0}, /* TODO: active scan */ 406 {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, 407 HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF} /* scanable adv */ 408 }, 409 { 410 /* scanable adv */ 411 {0, 0}, /* conn_adv: */ 412 {HCI_SUPP_LE_STATES_SCAN_ADV_INIT_MASK, 413 HCI_SUPP_LE_STATES_SCAN_ADV_INIT_OFF}, /* init */ 414 {HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_MASK, 415 HCI_SUPP_LE_STATES_SCAN_ADV_MASTER_OFF}, /* master */ 416 {HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_MASK, 417 HCI_SUPP_LE_STATES_SCAN_ADV_SLAVE_OFF}, /* slave: */ 418 {0, 0}, /* lo duty cycle adv */ 419 {0, 0}, /* hi duty cycle adv */ 420 {0, 0}, /* non connectable adv */ 421 {HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_MASK, 422 HCI_SUPP_LE_STATES_SCAN_ADV_PASS_SCAN_OFF}, /* passive scan */ 423 {HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_MASK, 424 HCI_SUPP_LE_STATES_SCAN_ADV_ACTIVE_SCAN_OFF}, /* active scan */ 425 {0, 0} /* scanable adv */ 426 } 427 428 }; 429 /* check LE combo state supported */ 430 #define BTM_LE_STATES_SUPPORTED(x, y, z) ((x)[(z)] & (y)) 431 432 /******************************************************************************* 433 * 434 * Function BTM_BleUpdateAdvFilterPolicy 435 * 436 * Description This function update the filter policy of advertiser. 437 * 438 * Parameter adv_policy: advertising filter policy 439 * 440 * Return void 441 ******************************************************************************/ 442 void BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) { 443 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 444 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC; 445 BD_ADDR p_addr_ptr = {0}; 446 uint8_t adv_mode = p_cb->adv_mode; 447 448 BTM_TRACE_EVENT("BTM_BleUpdateAdvFilterPolicy"); 449 450 if (!controller_get_interface()->supports_ble()) return; 451 452 if (p_cb->afp != adv_policy) { 453 p_cb->afp = adv_policy; 454 455 /* if adv active, stop and restart */ 456 btm_ble_stop_adv(); 457 458 if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE) 459 p_cb->evt_type = btm_set_conn_mode_adv_init_addr( 460 p_cb, p_addr_ptr, &init_addr_type, &p_cb->adv_addr_type); 461 462 btsnd_hcic_ble_write_adv_params( 463 (uint16_t)(p_cb->adv_interval_min ? p_cb->adv_interval_min 464 : BTM_BLE_GAP_ADV_SLOW_INT), 465 (uint16_t)(p_cb->adv_interval_max ? p_cb->adv_interval_max 466 : BTM_BLE_GAP_ADV_SLOW_INT), 467 p_cb->evt_type, p_cb->adv_addr_type, init_addr_type, p_addr_ptr, 468 p_cb->adv_chnl_map, p_cb->afp); 469 470 if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv(); 471 } 472 } 473 474 /******************************************************************************* 475 * 476 * Function BTM_BleObserve 477 * 478 * Description This procedure keep the device listening for advertising 479 * events from a broadcast device. 480 * 481 * Parameters start: start or stop observe. 482 * white_list: use white list in observer mode or not. 483 * 484 * Returns void 485 * 486 ******************************************************************************/ 487 tBTM_STATUS BTM_BleObserve(bool start, uint8_t duration, 488 tBTM_INQ_RESULTS_CB* p_results_cb, 489 tBTM_CMPL_CB* p_cmpl_cb) { 490 tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var; 491 tBTM_STATUS status = BTM_WRONG_MODE; 492 493 uint32_t scan_interval = 494 !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval; 495 uint32_t scan_window = 496 !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window; 497 498 BTM_TRACE_EVENT("%s : scan_type:%d, %d, %d", __func__, 499 btm_cb.btm_inq_vars.scan_type, p_inq->scan_interval, 500 p_inq->scan_window); 501 502 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE; 503 504 if (start) { 505 /* shared inquiry database, do not allow observe if any inquiry is active */ 506 if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) { 507 BTM_TRACE_ERROR("%s Observe Already Active", __func__); 508 return status; 509 } 510 511 btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb; 512 btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb; 513 status = BTM_CMD_STARTED; 514 515 /* scan is not started */ 516 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) { 517 /* allow config of scan type */ 518 p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE) 519 ? BTM_BLE_SCAN_MODE_ACTI 520 : p_inq->scan_type; 521 /* assume observe always not using white list */ 522 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == true) 523 /* enable resolving list */ 524 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN); 525 #endif 526 527 btm_send_hci_set_scan_params( 528 p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window, 529 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, BTM_BLE_DEFAULT_SFP); 530 531 p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE; 532 status = btm_ble_start_scan(); 533 } 534 535 if (status == BTM_CMD_STARTED) { 536 btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE; 537 if (duration != 0) { 538 /* start observer timer */ 539 period_ms_t duration_ms = duration * 1000; 540 alarm_set_on_queue(btm_cb.ble_ctr_cb.observer_timer, duration_ms, 541 btm_ble_observer_timer_timeout, NULL, 542 btu_general_alarm_queue); 543 } 544 } 545 } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) { 546 status = BTM_CMD_STARTED; 547 btm_ble_stop_observe(); 548 } else { 549 BTM_TRACE_ERROR("%s Observe not active", __func__); 550 } 551 552 return status; 553 } 554 555 #if (BLE_VND_INCLUDED == TRUE) 556 /******************************************************************************* 557 * 558 * Function btm_vsc_brcm_features_complete 559 * 560 * Description Command Complete callback for HCI_BLE_VENDOR_CAP_OCF 561 * 562 * Returns void 563 * 564 ******************************************************************************/ 565 static void btm_ble_vendor_capability_vsc_cmpl_cback( 566 tBTM_VSC_CMPL* p_vcs_cplt_params) { 567 uint8_t status = 0xFF; 568 uint8_t* p; 569 570 BTM_TRACE_DEBUG("%s", __func__); 571 572 /* Check status of command complete event */ 573 if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) && 574 (p_vcs_cplt_params->param_len > 0)) { 575 p = p_vcs_cplt_params->p_param_buf; 576 STREAM_TO_UINT8(status, p); 577 } 578 579 if (status == HCI_SUCCESS) { 580 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.adv_inst_max, p); 581 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.rpa_offloading, p); 582 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p); 583 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p); 584 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.filter_support, p); 585 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_filter, p); 586 STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.energy_support, p); 587 588 if (p_vcs_cplt_params->param_len > 589 BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE) { 590 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.version_supported, p); 591 } else { 592 btm_cb.cmn_ble_vsc_cb.version_supported = 593 BTM_VSC_CHIP_CAPABILITY_L_VERSION; 594 } 595 596 if (btm_cb.cmn_ble_vsc_cb.version_supported >= 597 BTM_VSC_CHIP_CAPABILITY_M_VERSION) { 598 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.total_trackable_advertisers, p); 599 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.extended_scan_support, p); 600 STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.debug_logging_supported, p); 601 } 602 btm_cb.cmn_ble_vsc_cb.values_read = true; 603 } 604 605 BTM_TRACE_DEBUG( 606 "%s: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d, ext_scan=%d", __func__, 607 status, btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, 608 btm_cb.cmn_ble_vsc_cb.adv_inst_max, btm_cb.cmn_ble_vsc_cb.rpa_offloading, 609 btm_cb.cmn_ble_vsc_cb.energy_support, 610 btm_cb.cmn_ble_vsc_cb.extended_scan_support); 611 612 btm_ble_adv_init(); 613 614 if (btm_cb.cmn_ble_vsc_cb.max_filter > 0) btm_ble_adv_filter_init(); 615 616 #if (BLE_PRIVACY_SPT == TRUE) 617 /* VS capability included and non-4.2 device */ 618 if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0 && 619 controller_get_interface()->get_ble_resolving_list_max_size() == 0) 620 btm_ble_resolving_list_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz); 621 #endif /* (BLE_PRIVACY_SPT == TRUE) */ 622 623 if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_init(); 624 625 if (p_ctrl_le_feature_rd_cmpl_cback != NULL) 626 p_ctrl_le_feature_rd_cmpl_cback(status); 627 } 628 #endif /* (BLE_VND_INCLUDED == TRUE) */ 629 630 /******************************************************************************* 631 * 632 * Function BTM_BleGetVendorCapabilities 633 * 634 * Description This function reads local LE features 635 * 636 * Parameters p_cmn_vsc_cb : Locala LE capability structure 637 * 638 * Returns void 639 * 640 ******************************************************************************/ 641 extern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB* p_cmn_vsc_cb) { 642 BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities"); 643 644 if (NULL != p_cmn_vsc_cb) { 645 *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb; 646 } 647 } 648 649 /****************************************************************************** 650 * 651 * Function BTM_BleReadControllerFeatures 652 * 653 * Description Reads BLE specific controller features 654 * 655 * Parameters: tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when 656 * features are read 657 * 658 * Returns void 659 * 660 ******************************************************************************/ 661 #if (BLE_VND_INCLUDED == TRUE) 662 extern void BTM_BleReadControllerFeatures( 663 tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) { 664 if (true == btm_cb.cmn_ble_vsc_cb.values_read) return; 665 666 BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures"); 667 668 p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback; 669 BTM_VendorSpecificCommand(HCI_BLE_VENDOR_CAP_OCF, 0, NULL, 670 btm_ble_vendor_capability_vsc_cmpl_cback); 671 } 672 #else 673 extern void BTM_BleReadControllerFeatures( 674 UNUSED_ATTR tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {} 675 #endif 676 677 /******************************************************************************* 678 * 679 * Function BTM_BleEnableMixedPrivacyMode 680 * 681 * Description This function is called to enabled Mixed mode if privacy 1.2 682 * is applicable in controller. 683 * 684 * Parameters mixed_on: mixed mode to be used or not. 685 * 686 * Returns void 687 * 688 ******************************************************************************/ 689 void BTM_BleEnableMixedPrivacyMode(bool mixed_on) { 690 #if (BLE_PRIVACY_SPT == TRUE) 691 btm_cb.ble_ctr_cb.mixed_mode = mixed_on; 692 693 /* TODO: send VSC to enabled mixed mode */ 694 #endif 695 } 696 697 /******************************************************************************* 698 * 699 * Function BTM_BleConfigPrivacy 700 * 701 * Description This function is called to enable or disable the privacy in 702 * LE channel of the local device. 703 * 704 * Parameters privacy_mode: privacy mode on or off. 705 * 706 * Returns bool privacy mode set success; otherwise failed. 707 * 708 ******************************************************************************/ 709 bool BTM_BleConfigPrivacy(bool privacy_mode) { 710 #if (BLE_PRIVACY_SPT == TRUE) 711 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb; 712 713 BTM_TRACE_EVENT("%s", __func__); 714 715 /* if LE is not supported, return error */ 716 if (!controller_get_interface()->supports_ble()) return false; 717 718 uint8_t addr_resolution = 0; 719 if (!privacy_mode) /* if privacy disabled, always use public address */ 720 { 721 p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC; 722 p_cb->privacy_mode = BTM_PRIVACY_NONE; 723 } else /* privacy is turned on*/ 724 { 725 /* always set host random address, used when privacy 1.1 or priavcy 1.2 is 726 * disabled */ 727 p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM; 728 btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low)); 729 730 /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private 731 * address in controller */ 732 if (controller_get_interface()->supports_ble_privacy()) { 733 addr_resolution = 1; 734 /* check vendor specific capability */ 735 p_cb->privacy_mode = 736 btm_cb.ble_ctr_cb.mixed_mode ? BTM_PRIVACY_MIXED : BTM_PRIVACY_1_2; 737 } else /* 4.1/4.0 controller */ 738 p_cb->privacy_mode = BTM_PRIVACY_1_1; 739 } 740 741 GAP_BleAttrDBUpdate(GATT_UUID_GAP_CENTRAL_ADDR_RESOL, 742 (tGAP_BLE_ATTR_VALUE*)&addr_resolution); 743 744 return true; 745 #else 746 return false; 747 #endif 748 } 749 750 /******************************************************************************* 751 * 752 * Function BTM_BleMaxMultiAdvInstanceCount 753 * 754 * Description Returns max number of multi adv instances supported by 755 * controller 756 * 757 * Returns Max multi adv instance count 758 * 759 ******************************************************************************/ 760 extern uint8_t BTM_BleMaxMultiAdvInstanceCount(void) { 761 return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX 762 ? btm_cb.cmn_ble_vsc_cb.adv_inst_max 763 : BTM_BLE_MULTI_ADV_MAX; 764 } 765 766 /******************************************************************************* 767 * 768 * Function BTM_BleLocalPrivacyEnabled 769 * 770 * Description Checks if local device supports private address 771 * 772 * Returns Return true if local privacy is enabled else false 773 * 774 ******************************************************************************/ 775 bool BTM_BleLocalPrivacyEnabled(void) { 776 #if (BLE_PRIVACY_SPT == TRUE) 777 return (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE); 778 #else 779 return false; 780 #endif 781 } 782 783 /** 784 * Set BLE connectable mode to auto connect 785 */ 786 void BTM_BleStartAutoConn() { 787 BTM_TRACE_EVENT("%s", __func__); 788 if (!controller_get_interface()->supports_ble()) return; 789 790 if (btm_cb.ble_ctr_cb.bg_conn_type != BTM_BLE_CONN_AUTO) { 791 btm_ble_start_auto_conn(true); 792 btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_AUTO; 793 } 794 } 795 796 /******************************************************************************* 797 * 798 * Function BTM_BleClearBgConnDev 799 * 800 * Description This function is called to clear the whitelist, 801 * end any pending whitelist connections, 802 * and reset the local bg device list. 803 * 804 * Parameters void 805 * 806 * Returns void 807 * 808 ******************************************************************************/ 809 void BTM_BleClearBgConnDev(void) { 810 btm_ble_start_auto_conn(false); 811 btm_ble_clear_white_list(); 812 gatt_reset_bgdev_list(); 813 } 814 815 /******************************************************************************* 816 * 817 * Function BTM_BleUpdateBgConnDev 818 * 819 * Description This function is called to add or remove a device into/from 820 * background connection procedure. The background connection 821 * procedure is decided by the background connection type, it 822 *can be 823 * auto connection, or selective connection. 824 * 825 * Parameters add_remove: true to add; false to remove. 826 * remote_bda: device address to add/remove. 827 * 828 * Returns void 829 * 830 ******************************************************************************/ 831 bool BTM_BleUpdateBgConnDev(bool add_remove, BD_ADDR remote_bda) { 832 BTM_TRACE_EVENT("%s() add=%d", __func__, add_remove); 833 return btm_update_dev_to_white_list(add_remove, remote_bda); 834 } 835 836 /******************************************************************************* 837 * 838 * Function BTM_BleSetConnectableMode 839 * 840 * Description This function is called to set BLE connectable mode for a 841 * peripheral device. 842 * 843 * Parameters conn_mode: directed connectable mode, or non-directed. It 844 * can be BTM_BLE_CONNECT_EVT, 845 * BTM_BLE_CONNECT_DIR_EVT or 846 * BTM_BLE_CONNECT_LO_DUTY_DIR_EVT 847 * 848 * Returns BTM_ILLEGAL_VALUE if controller does not support BLE. 849 * BTM_SUCCESS is status set successfully; otherwise failure. 850 * 851 ******************************************************************************/ 852 tBTM_STATUS BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode) { 853 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 854 855 BTM_TRACE_EVENT("%s connectable_mode = %d ", __func__, connectable_mode); 856 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE; 857 858 p_cb->directed_conn = connectable_mode; 859 return btm_ble_set_connectability(p_cb->connectable_mode); 860 } 861 862 #if (BLE_PRIVACY_SPT == TRUE) 863 static bool is_resolving_list_bit_set(void* data, void* context) { 864 tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data); 865 866 if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) != 0) 867 return false; 868 869 return true; 870 } 871 #endif 872 873 /******************************************************************************* 874 * 875 * Function btm_set_conn_mode_adv_init_addr 876 * 877 * Description set initator address type and local address type based on 878 * adv mode. 879 * 880 * 881 ******************************************************************************/ 882 static uint8_t btm_set_conn_mode_adv_init_addr( 883 tBTM_BLE_INQ_CB* p_cb, BD_ADDR_PTR p_peer_addr_ptr, 884 tBLE_ADDR_TYPE* p_peer_addr_type, tBLE_ADDR_TYPE* p_own_addr_type) { 885 uint8_t evt_type; 886 #if (BLE_PRIVACY_SPT == TRUE) 887 tBTM_SEC_DEV_REC* p_dev_rec; 888 #endif 889 890 evt_type = 891 (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE) 892 ? ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT) 893 : BTM_BLE_CONNECT_EVT; 894 895 if (evt_type == BTM_BLE_CONNECT_EVT) { 896 evt_type = p_cb->directed_conn; 897 898 if (p_cb->directed_conn == BTM_BLE_CONNECT_DIR_EVT || 899 p_cb->directed_conn == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT) { 900 #if (BLE_PRIVACY_SPT == TRUE) 901 /* for privacy 1.2, convert peer address as static, own address set as ID 902 * addr */ 903 if (btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 || 904 btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) { 905 /* only do so for bonded device */ 906 if ((p_dev_rec = btm_find_or_alloc_dev(p_cb->direct_bda.bda)) != NULL && 907 p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) { 908 btm_ble_enable_resolving_list(BTM_BLE_RL_ADV); 909 memcpy(p_peer_addr_ptr, p_dev_rec->ble.static_addr, BD_ADDR_LEN); 910 *p_peer_addr_type = p_dev_rec->ble.static_addr_type; 911 *p_own_addr_type = BLE_ADDR_RANDOM_ID; 912 return evt_type; 913 } 914 /* otherwise fall though as normal directed adv */ 915 else { 916 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true); 917 } 918 } 919 #endif 920 /* direct adv mode does not have privacy, if privacy is not enabled */ 921 *p_peer_addr_type = p_cb->direct_bda.type; 922 memcpy(p_peer_addr_ptr, p_cb->direct_bda.bda, BD_ADDR_LEN); 923 return evt_type; 924 } 925 } 926 927 /* undirect adv mode or non-connectable mode*/ 928 #if (BLE_PRIVACY_SPT == TRUE) 929 /* when privacy 1.2 privacy only mode is used, or mixed mode */ 930 if ((btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 && 931 p_cb->afp != AP_SCAN_CONN_ALL) || 932 btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) { 933 list_node_t* n = 934 list_foreach(btm_cb.sec_dev_rec, is_resolving_list_bit_set, NULL); 935 if (n) { 936 /* if enhanced privacy is required, set Identity address and matching IRK 937 * peer */ 938 tBTM_SEC_DEV_REC* p_dev_rec = 939 static_cast<tBTM_SEC_DEV_REC*>(list_node(n)); 940 memcpy(p_peer_addr_ptr, p_dev_rec->ble.static_addr, BD_ADDR_LEN); 941 *p_peer_addr_type = p_dev_rec->ble.static_addr_type; 942 943 *p_own_addr_type = BLE_ADDR_RANDOM_ID; 944 } else { 945 /* resolving list is empty, not enabled */ 946 *p_own_addr_type = BLE_ADDR_RANDOM; 947 } 948 } 949 /* privacy 1.1, or privacy 1.2, general discoverable/connectable mode, disable 950 privacy in */ 951 /* controller fall back to host based privacy */ 952 else if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) { 953 *p_own_addr_type = BLE_ADDR_RANDOM; 954 } 955 #endif 956 957 /* if no privacy,do not set any peer address,*/ 958 /* local address type go by global privacy setting */ 959 return evt_type; 960 } 961 962 /******************************************************************************* 963 * 964 * Function BTM_BleSetAdvParams 965 * 966 * Description This function is called to set advertising parameters. 967 * 968 * Parameters adv_int_min: minimum advertising interval 969 * adv_int_max: maximum advertising interval 970 * p_dir_bda: connectable direct initiator's LE device address 971 * chnl_map: advertising channel map. 972 * 973 * Returns void 974 * 975 ******************************************************************************/ 976 tBTM_STATUS BTM_BleSetAdvParams(uint16_t adv_int_min, uint16_t adv_int_max, 977 tBLE_BD_ADDR* p_dir_bda, 978 tBTM_BLE_ADV_CHNL_MAP chnl_map) { 979 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; 980 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 981 tBTM_STATUS status = BTM_SUCCESS; 982 BD_ADDR p_addr_ptr = {0}; 983 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC; 984 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type; 985 uint8_t adv_mode = p_cb->adv_mode; 986 987 BTM_TRACE_EVENT("BTM_BleSetAdvParams"); 988 989 if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE; 990 991 if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN, 992 BTM_BLE_ADV_INT_MAX) || 993 !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN, 994 BTM_BLE_ADV_INT_MAX)) { 995 return BTM_ILLEGAL_VALUE; 996 } 997 998 p_cb->adv_interval_min = adv_int_min; 999 p_cb->adv_interval_max = adv_int_max; 1000 p_cb->adv_chnl_map = chnl_map; 1001 1002 if (p_dir_bda) { 1003 memcpy(&p_cb->direct_bda, p_dir_bda, sizeof(tBLE_BD_ADDR)); 1004 } 1005 1006 BTM_TRACE_EVENT("update params for an active adv"); 1007 1008 btm_ble_stop_adv(); 1009 1010 p_cb->evt_type = btm_set_conn_mode_adv_init_addr( 1011 p_cb, p_addr_ptr, &init_addr_type, &own_addr_type); 1012 1013 /* update adv params */ 1014 btsnd_hcic_ble_write_adv_params( 1015 p_cb->adv_interval_min, p_cb->adv_interval_max, p_cb->evt_type, 1016 own_addr_type, init_addr_type, p_addr_ptr, p_cb->adv_chnl_map, p_cb->afp); 1017 1018 if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv(); 1019 1020 return status; 1021 } 1022 1023 /** 1024 * This function is called to set scan parameters. |cb| is called with operation 1025 * status 1026 **/ 1027 void BTM_BleSetScanParams(uint32_t scan_interval, uint32_t scan_window, 1028 tBLE_SCAN_MODE scan_mode, 1029 base::Callback<void(uint8_t)> cb) { 1030 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 1031 uint32_t max_scan_interval; 1032 uint32_t max_scan_window; 1033 1034 BTM_TRACE_EVENT("%s", __func__); 1035 if (!controller_get_interface()->supports_ble()) return; 1036 1037 /* If not supporting extended scan support, use the older range for checking 1038 */ 1039 if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) { 1040 max_scan_interval = BTM_BLE_SCAN_INT_MAX; 1041 max_scan_window = BTM_BLE_SCAN_WIN_MAX; 1042 } else { 1043 /* If supporting extended scan support, use the new extended range for 1044 * checking */ 1045 max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX; 1046 max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX; 1047 } 1048 1049 if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN, 1050 max_scan_interval) && 1051 BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN, 1052 max_scan_window) && 1053 (scan_mode == BTM_BLE_SCAN_MODE_ACTI || 1054 scan_mode == BTM_BLE_SCAN_MODE_PASS)) { 1055 p_cb->scan_type = scan_mode; 1056 p_cb->scan_interval = scan_interval; 1057 p_cb->scan_window = scan_window; 1058 1059 cb.Run(BTM_SUCCESS); 1060 } else { 1061 cb.Run(BTM_ILLEGAL_VALUE); 1062 1063 BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d", 1064 scan_interval, scan_window); 1065 } 1066 } 1067 1068 /******************************************************************************* 1069 * 1070 * Function BTM_BleWriteScanRsp 1071 * 1072 * Description This function is called to write LE scan response. 1073 * 1074 * Parameters: p_scan_rsp: scan response information. 1075 * 1076 * Returns void 1077 * 1078 ******************************************************************************/ 1079 void BTM_BleWriteScanRsp(uint8_t* data, uint8_t length, 1080 tBTM_BLE_ADV_DATA_CMPL_CBACK* p_adv_data_cback) { 1081 BTM_TRACE_EVENT("%s: length: %d", __func__, length); 1082 if (!controller_get_interface()->supports_ble()) { 1083 p_adv_data_cback(BTM_ILLEGAL_VALUE); 1084 return; 1085 } 1086 1087 btsnd_hcic_ble_set_scan_rsp_data(length, data); 1088 1089 if (length != 0) 1090 btm_cb.ble_ctr_cb.inq_var.scan_rsp = true; 1091 else 1092 btm_cb.ble_ctr_cb.inq_var.scan_rsp = false; 1093 1094 p_adv_data_cback(BTM_SUCCESS); 1095 } 1096 1097 /******************************************************************************* 1098 * 1099 * Function BTM__BLEReadDiscoverability 1100 * 1101 * Description This function is called to read the current LE 1102 * discoverability mode of the device. 1103 * 1104 * Returns BTM_BLE_NON_DISCOVERABLE ,BTM_BLE_LIMITED_DISCOVERABLE or 1105 * BTM_BLE_GENRAL_DISCOVERABLE 1106 * 1107 ******************************************************************************/ 1108 uint16_t BTM_BleReadDiscoverability() { 1109 BTM_TRACE_API("%s", __func__); 1110 1111 return (btm_cb.ble_ctr_cb.inq_var.discoverable_mode); 1112 } 1113 1114 /******************************************************************************* 1115 * 1116 * Function BTM__BLEReadConnectability 1117 * 1118 * Description This function is called to read the current LE 1119 * connectability mode of the device. 1120 * 1121 * Returns BTM_BLE_NON_CONNECTABLE or BTM_BLE_CONNECTABLE 1122 * 1123 ******************************************************************************/ 1124 uint16_t BTM_BleReadConnectability() { 1125 BTM_TRACE_API("%s", __func__); 1126 1127 return (btm_cb.ble_ctr_cb.inq_var.connectable_mode); 1128 } 1129 1130 /******************************************************************************* 1131 * 1132 * Function btm_ble_select_adv_interval 1133 * 1134 * Description select adv interval based on device mode 1135 * 1136 * Returns void 1137 * 1138 ******************************************************************************/ 1139 void btm_ble_select_adv_interval(tBTM_BLE_INQ_CB* p_cb, uint8_t evt_type, 1140 uint16_t* p_adv_int_min, 1141 uint16_t* p_adv_int_max) { 1142 if (p_cb->adv_interval_min && p_cb->adv_interval_max) { 1143 *p_adv_int_min = p_cb->adv_interval_min; 1144 *p_adv_int_max = p_cb->adv_interval_max; 1145 } else { 1146 switch (evt_type) { 1147 case BTM_BLE_CONNECT_EVT: 1148 case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT: 1149 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1; 1150 break; 1151 1152 case BTM_BLE_NON_CONNECT_EVT: 1153 case BTM_BLE_DISCOVER_EVT: 1154 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2; 1155 break; 1156 1157 /* connectable directed event */ 1158 case BTM_BLE_CONNECT_DIR_EVT: 1159 *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT; 1160 *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT; 1161 break; 1162 1163 default: 1164 *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT; 1165 break; 1166 } 1167 } 1168 return; 1169 } 1170 1171 /******************************************************************************* 1172 * 1173 * Function btm_ble_update_dmt_flag_bits 1174 * 1175 * Description Obtain updated adv flag value based on connect and 1176 * discoverability mode. Also, setup DMT support value in the 1177 * flag based on whether the controller supports both LE and 1178 * BR/EDR. 1179 * 1180 * Parameters: flag_value (Input / Output) - flag value 1181 * connect_mode (Input) - Connect mode value 1182 * disc_mode (Input) - discoverability mode 1183 * 1184 * Returns void 1185 * 1186 ******************************************************************************/ 1187 void btm_ble_update_dmt_flag_bits(uint8_t* adv_flag_value, 1188 const uint16_t connect_mode, 1189 const uint16_t disc_mode) { 1190 /* BR/EDR non-discoverable , non-connectable */ 1191 if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 && 1192 (connect_mode & BTM_CONNECTABLE_MASK) == 0) 1193 *adv_flag_value |= BTM_BLE_BREDR_NOT_SPT; 1194 else 1195 *adv_flag_value &= ~BTM_BLE_BREDR_NOT_SPT; 1196 1197 /* if local controller support, mark both controller and host support in flag 1198 */ 1199 if (controller_get_interface()->supports_simultaneous_le_bredr()) 1200 *adv_flag_value |= (BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT); 1201 else 1202 *adv_flag_value &= ~(BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT); 1203 } 1204 1205 /******************************************************************************* 1206 * 1207 * Function btm_ble_set_adv_flag 1208 * 1209 * Description Set adv flag in adv data. 1210 * 1211 * Parameters: connect_mode (Input)- Connect mode value 1212 * disc_mode (Input) - discoverability mode 1213 * 1214 * Returns void 1215 * 1216 ******************************************************************************/ 1217 void btm_ble_set_adv_flag(uint16_t connect_mode, uint16_t disc_mode) { 1218 uint8_t flag = 0, old_flag = 0; 1219 tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data; 1220 1221 if (p_adv_data->p_flags != NULL) flag = old_flag = *(p_adv_data->p_flags); 1222 1223 btm_ble_update_dmt_flag_bits(&flag, connect_mode, disc_mode); 1224 1225 LOG_DEBUG(LOG_TAG, "disc_mode %04x", disc_mode); 1226 /* update discoverable flag */ 1227 if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE) { 1228 flag &= ~BTM_BLE_GEN_DISC_FLAG; 1229 flag |= BTM_BLE_LIMIT_DISC_FLAG; 1230 } else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE) { 1231 flag |= BTM_BLE_GEN_DISC_FLAG; 1232 flag &= ~BTM_BLE_LIMIT_DISC_FLAG; 1233 } else /* remove all discoverable flags */ 1234 { 1235 flag &= ~(BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG); 1236 } 1237 1238 if (flag != old_flag) { 1239 btm_ble_update_adv_flag(flag); 1240 } 1241 } 1242 /******************************************************************************* 1243 * 1244 * Function btm_ble_set_discoverability 1245 * 1246 * Description This function is called to set BLE discoverable mode. 1247 * 1248 * Parameters: combined_mode: discoverability mode. 1249 * 1250 * Returns BTM_SUCCESS is status set successfully; otherwise failure. 1251 * 1252 ******************************************************************************/ 1253 tBTM_STATUS btm_ble_set_discoverability(uint16_t combined_mode) { 1254 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; 1255 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 1256 uint16_t mode = (combined_mode & BTM_BLE_DISCOVERABLE_MASK); 1257 uint8_t new_mode = BTM_BLE_ADV_ENABLE; 1258 uint8_t evt_type; 1259 tBTM_STATUS status = BTM_SUCCESS; 1260 BD_ADDR p_addr_ptr = {0}; 1261 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC, 1262 own_addr_type = p_addr_cb->own_addr_type; 1263 uint16_t adv_int_min, adv_int_max; 1264 1265 BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode, 1266 combined_mode); 1267 1268 /*** Check mode parameter ***/ 1269 if (mode > BTM_BLE_MAX_DISCOVERABLE) return (BTM_ILLEGAL_VALUE); 1270 1271 p_cb->discoverable_mode = mode; 1272 1273 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &init_addr_type, 1274 &own_addr_type); 1275 1276 if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE && 1277 mode == BTM_BLE_NON_DISCOVERABLE) 1278 new_mode = BTM_BLE_ADV_DISABLE; 1279 1280 btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max); 1281 1282 alarm_cancel(p_cb->fast_adv_timer); 1283 1284 /* update adv params if start advertising */ 1285 BTM_TRACE_EVENT("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type, 1286 p_cb->evt_type); 1287 1288 if (new_mode == BTM_BLE_ADV_ENABLE) { 1289 btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, combined_mode); 1290 1291 if (evt_type != p_cb->evt_type || p_cb->adv_addr_type != own_addr_type || 1292 !p_cb->fast_adv_on) { 1293 btm_ble_stop_adv(); 1294 1295 /* update adv params */ 1296 btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type, 1297 own_addr_type, init_addr_type, p_addr_ptr, 1298 p_cb->adv_chnl_map, p_cb->afp); 1299 p_cb->evt_type = evt_type; 1300 p_cb->adv_addr_type = own_addr_type; 1301 } 1302 } 1303 1304 if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode) { 1305 if (new_mode == BTM_BLE_ADV_ENABLE) 1306 status = btm_ble_start_adv(); 1307 else 1308 status = btm_ble_stop_adv(); 1309 } 1310 1311 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) { 1312 p_cb->fast_adv_on = true; 1313 /* start initial GAP mode adv timer */ 1314 alarm_set_on_queue(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS, 1315 btm_ble_fast_adv_timer_timeout, NULL, 1316 btu_general_alarm_queue); 1317 } else { 1318 #if (BLE_PRIVACY_SPT == TRUE) 1319 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true); 1320 #endif 1321 } 1322 1323 /* set up stop advertising timer */ 1324 if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE) { 1325 BTM_TRACE_EVENT("start timer for limited disc mode duration=%d ms", 1326 BTM_BLE_GAP_LIM_TIMEOUT_MS); 1327 /* start Tgap(lim_timeout) */ 1328 alarm_set_on_queue(p_cb->inquiry_timer, BTM_BLE_GAP_LIM_TIMEOUT_MS, 1329 btm_ble_inquiry_timer_gap_limited_discovery_timeout, 1330 NULL, btu_general_alarm_queue); 1331 } 1332 return status; 1333 } 1334 1335 /******************************************************************************* 1336 * 1337 * Function btm_ble_set_connectability 1338 * 1339 * Description This function is called to set BLE connectability mode. 1340 * 1341 * Parameters: combined_mode: connectability mode. 1342 * 1343 * Returns BTM_SUCCESS is status set successfully; otherwise failure. 1344 * 1345 ******************************************************************************/ 1346 tBTM_STATUS btm_ble_set_connectability(uint16_t combined_mode) { 1347 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; 1348 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 1349 uint16_t mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK); 1350 uint8_t new_mode = BTM_BLE_ADV_ENABLE; 1351 uint8_t evt_type; 1352 tBTM_STATUS status = BTM_SUCCESS; 1353 BD_ADDR p_addr_ptr = {0}; 1354 tBLE_ADDR_TYPE peer_addr_type = BLE_ADDR_PUBLIC, 1355 own_addr_type = p_addr_cb->own_addr_type; 1356 uint16_t adv_int_min, adv_int_max; 1357 1358 BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode, 1359 combined_mode); 1360 1361 /*** Check mode parameter ***/ 1362 if (mode > BTM_BLE_MAX_CONNECTABLE) return (BTM_ILLEGAL_VALUE); 1363 1364 p_cb->connectable_mode = mode; 1365 1366 evt_type = btm_set_conn_mode_adv_init_addr(p_cb, p_addr_ptr, &peer_addr_type, 1367 &own_addr_type); 1368 1369 if (mode == BTM_BLE_NON_CONNECTABLE && 1370 p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE) 1371 new_mode = BTM_BLE_ADV_DISABLE; 1372 1373 btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max); 1374 1375 alarm_cancel(p_cb->fast_adv_timer); 1376 /* update adv params if needed */ 1377 if (new_mode == BTM_BLE_ADV_ENABLE) { 1378 btm_ble_set_adv_flag(combined_mode, btm_cb.btm_inq_vars.discoverable_mode); 1379 if (p_cb->evt_type != evt_type || 1380 p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on) { 1381 btm_ble_stop_adv(); 1382 1383 btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type, 1384 own_addr_type, peer_addr_type, p_addr_ptr, 1385 p_cb->adv_chnl_map, p_cb->afp); 1386 p_cb->evt_type = evt_type; 1387 p_cb->adv_addr_type = own_addr_type; 1388 } 1389 } 1390 1391 /* update advertising mode */ 1392 if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode) { 1393 if (new_mode == BTM_BLE_ADV_ENABLE) 1394 status = btm_ble_start_adv(); 1395 else 1396 status = btm_ble_stop_adv(); 1397 } 1398 1399 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) { 1400 p_cb->fast_adv_on = true; 1401 /* start initial GAP mode adv timer */ 1402 alarm_set_on_queue(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS, 1403 btm_ble_fast_adv_timer_timeout, NULL, 1404 btu_general_alarm_queue); 1405 } else { 1406 #if (BLE_PRIVACY_SPT == TRUE) 1407 btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true); 1408 #endif 1409 } 1410 return status; 1411 } 1412 1413 void btm_send_hci_scan_enable(uint8_t enable, uint8_t filter_duplicates) { 1414 if (controller_get_interface()->supports_ble_extended_advertising()) { 1415 btsnd_hcic_ble_set_extended_scan_enable(enable, filter_duplicates, 0x0000, 1416 0x0000); 1417 } else { 1418 btsnd_hcic_ble_set_scan_enable(enable, filter_duplicates); 1419 } 1420 } 1421 1422 void btm_send_hci_set_scan_params(uint8_t scan_type, uint16_t scan_int, 1423 uint16_t scan_win, uint8_t addr_type_own, 1424 uint8_t scan_filter_policy) { 1425 if (controller_get_interface()->supports_ble_extended_advertising()) { 1426 scanning_phy_cfg phy_cfg; 1427 phy_cfg.scan_type = scan_type; 1428 phy_cfg.scan_int = scan_int; 1429 phy_cfg.scan_win = scan_win; 1430 1431 btsnd_hcic_ble_set_extended_scan_params(addr_type_own, scan_filter_policy, 1432 1, &phy_cfg); 1433 } else { 1434 btsnd_hcic_ble_set_scan_params(scan_type, scan_int, scan_win, addr_type_own, 1435 scan_filter_policy); 1436 } 1437 } 1438 1439 /******************************************************************************* 1440 * 1441 * Function btm_ble_start_inquiry 1442 * 1443 * Description This function is called to start BLE inquiry procedure. 1444 * If the duration is zero, the periodic inquiry mode is 1445 * cancelled. 1446 * 1447 * Parameters: mode - GENERAL or LIMITED inquiry 1448 * p_inq_params - pointer to the BLE inquiry parameter. 1449 * p_results_cb - callback returning pointer to results 1450 * (tBTM_INQ_RESULTS) 1451 * p_cmpl_cb - callback indicating the end of an inquiry 1452 * 1453 * 1454 * 1455 * Returns BTM_CMD_STARTED if successfully started 1456 * BTM_NO_RESOURCES if could not allocate a message buffer 1457 * BTM_BUSY - if an inquiry is already active 1458 * 1459 ******************************************************************************/ 1460 tBTM_STATUS btm_ble_start_inquiry(uint8_t mode, uint8_t duration) { 1461 tBTM_STATUS status = BTM_CMD_STARTED; 1462 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb; 1463 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars; 1464 1465 BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x", 1466 mode, btm_cb.btm_inq_vars.inq_active); 1467 1468 /* if selective connection is active, or inquiry is already active, reject it 1469 */ 1470 if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity)) { 1471 BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry"); 1472 return (BTM_BUSY); 1473 } 1474 1475 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) { 1476 btm_send_hci_set_scan_params( 1477 BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT, 1478 BTM_BLE_LOW_LATENCY_SCAN_WIN, 1479 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL); 1480 #if (BLE_PRIVACY_SPT == TRUE) 1481 /* enable IRK list */ 1482 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN); 1483 #endif 1484 p_ble_cb->inq_var.scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE; 1485 status = btm_ble_start_scan(); 1486 } else if ((p_ble_cb->inq_var.scan_interval != 1487 BTM_BLE_LOW_LATENCY_SCAN_INT) || 1488 (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) { 1489 BTM_TRACE_DEBUG("%s, restart LE scan with low latency scan params", 1490 __func__); 1491 btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE); 1492 btm_send_hci_set_scan_params( 1493 BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT, 1494 BTM_BLE_LOW_LATENCY_SCAN_WIN, 1495 btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL); 1496 btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE); 1497 } 1498 1499 if (status == BTM_CMD_STARTED) { 1500 p_inq->inq_active |= mode; 1501 p_ble_cb->scan_activity |= mode; 1502 1503 BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x", 1504 p_inq->inq_active); 1505 1506 if (duration != 0) { 1507 /* start inquiry timer */ 1508 period_ms_t duration_ms = duration * 1000; 1509 alarm_set_on_queue(p_ble_cb->inq_var.inquiry_timer, duration_ms, 1510 btm_ble_inquiry_timer_timeout, NULL, 1511 btu_general_alarm_queue); 1512 } 1513 } 1514 1515 return status; 1516 } 1517 1518 /******************************************************************************* 1519 * 1520 * Function btm_ble_read_remote_name_cmpl 1521 * 1522 * Description This function is called when BLE remote name is received. 1523 * 1524 * Returns void 1525 * 1526 ******************************************************************************/ 1527 void btm_ble_read_remote_name_cmpl(bool status, BD_ADDR bda, uint16_t length, 1528 char* p_name) { 1529 uint8_t hci_status = HCI_SUCCESS; 1530 BD_NAME bd_name; 1531 1532 memset(bd_name, 0, (BD_NAME_LEN + 1)); 1533 if (length > BD_NAME_LEN) { 1534 length = BD_NAME_LEN; 1535 } 1536 memcpy((uint8_t*)bd_name, p_name, length); 1537 1538 if ((!status) || (length == 0)) { 1539 hci_status = HCI_ERR_HOST_TIMEOUT; 1540 } 1541 1542 btm_process_remote_name(bda, bd_name, length + 1, hci_status); 1543 btm_sec_rmt_name_request_complete(bda, (uint8_t*)p_name, hci_status); 1544 } 1545 1546 /******************************************************************************* 1547 * 1548 * Function btm_ble_read_remote_name 1549 * 1550 * Description This function read remote LE device name using GATT read 1551 * procedure. 1552 * 1553 * Parameters: None. 1554 * 1555 * Returns void 1556 * 1557 ******************************************************************************/ 1558 tBTM_STATUS btm_ble_read_remote_name(BD_ADDR remote_bda, tBTM_CMPL_CB* p_cb) { 1559 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars; 1560 1561 if (!controller_get_interface()->supports_ble()) return BTM_ERR_PROCESSING; 1562 1563 tINQ_DB_ENT* p_i = btm_inq_db_find(remote_bda); 1564 if (p_i && !ble_evt_type_is_connectable(p_i->inq_info.results.ble_evt_type)) { 1565 BTM_TRACE_DEBUG("name request to non-connectable device failed."); 1566 return BTM_ERR_PROCESSING; 1567 } 1568 1569 /* read remote device name using GATT procedure */ 1570 if (p_inq->remname_active) return BTM_BUSY; 1571 1572 if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl)) 1573 return BTM_BUSY; 1574 1575 p_inq->p_remname_cmpl_cb = p_cb; 1576 p_inq->remname_active = true; 1577 1578 memcpy(p_inq->remname_bda, remote_bda, BD_ADDR_LEN); 1579 1580 alarm_set_on_queue(p_inq->remote_name_timer, BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS, 1581 btm_inq_remote_name_timer_timeout, NULL, 1582 btu_general_alarm_queue); 1583 1584 return BTM_CMD_STARTED; 1585 } 1586 1587 /******************************************************************************* 1588 * 1589 * Function btm_ble_cancel_remote_name 1590 * 1591 * Description This function cancel read remote LE device name. 1592 * 1593 * Parameters: None. 1594 * 1595 * Returns void 1596 * 1597 ******************************************************************************/ 1598 bool btm_ble_cancel_remote_name(BD_ADDR remote_bda) { 1599 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars; 1600 bool status; 1601 1602 status = GAP_BleCancelReadPeerDevName(remote_bda); 1603 1604 p_inq->remname_active = false; 1605 memset(p_inq->remname_bda, 0, BD_ADDR_LEN); 1606 alarm_cancel(p_inq->remote_name_timer); 1607 1608 return status; 1609 } 1610 1611 /******************************************************************************* 1612 * 1613 * Function btm_ble_update_adv_flag 1614 * 1615 * Description This function update the limited discoverable flag in the 1616 * adv data. 1617 * 1618 * Parameters: None. 1619 * 1620 * Returns void 1621 * 1622 ******************************************************************************/ 1623 static void btm_ble_update_adv_flag(uint8_t flag) { 1624 tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data; 1625 uint8_t* p; 1626 1627 BTM_TRACE_DEBUG("btm_ble_update_adv_flag new=0x%x", flag); 1628 1629 if (p_adv_data->p_flags != NULL) { 1630 BTM_TRACE_DEBUG("btm_ble_update_adv_flag old=0x%x", *p_adv_data->p_flags); 1631 *p_adv_data->p_flags = flag; 1632 } else /* no FLAGS in ADV data*/ 1633 { 1634 p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad; 1635 /* need 3 bytes space to stuff in the flags, if not */ 1636 /* erase all written data, just for flags */ 1637 if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3) { 1638 p = p_adv_data->p_pad = p_adv_data->ad_data; 1639 memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN); 1640 } 1641 1642 *p++ = 2; 1643 *p++ = BTM_BLE_AD_TYPE_FLAG; 1644 p_adv_data->p_flags = p; 1645 *p++ = flag; 1646 p_adv_data->p_pad = p; 1647 } 1648 1649 btsnd_hcic_ble_set_adv_data( 1650 (uint8_t)(p_adv_data->p_pad - p_adv_data->ad_data), p_adv_data->ad_data); 1651 p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS; 1652 } 1653 1654 /** 1655 * Check ADV flag to make sure device is discoverable and match the search 1656 * condition 1657 */ 1658 uint8_t btm_ble_is_discoverable(BD_ADDR bda, 1659 std::vector<uint8_t> const& adv_data) { 1660 uint8_t flag = 0, rt = 0; 1661 uint8_t data_len; 1662 tBTM_INQ_PARMS* p_cond = &btm_cb.btm_inq_vars.inqparms; 1663 1664 /* for observer, always "discoverable */ 1665 if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) 1666 rt |= BTM_BLE_OBS_RESULT; 1667 1668 /* does not match filter condition */ 1669 if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR && 1670 memcmp(bda, p_cond->filter_cond.bdaddr_cond, BD_ADDR_LEN) != 0) { 1671 BTM_TRACE_DEBUG("BD ADDR does not meet filter condition"); 1672 return rt; 1673 } 1674 1675 if (!adv_data.empty()) { 1676 const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType( 1677 adv_data, BTM_BLE_AD_TYPE_FLAG, &data_len); 1678 if (p_flag != NULL) { 1679 flag = *p_flag; 1680 1681 if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) && 1682 (flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) { 1683 BTM_TRACE_DEBUG("Find Generable Discoverable device"); 1684 rt |= BTM_BLE_INQ_RESULT; 1685 } 1686 1687 else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY && 1688 (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0) { 1689 BTM_TRACE_DEBUG("Find limited discoverable device"); 1690 rt |= BTM_BLE_INQ_RESULT; 1691 } 1692 } 1693 } 1694 return rt; 1695 } 1696 1697 static void btm_ble_appearance_to_cod(uint16_t appearance, uint8_t* dev_class) { 1698 dev_class[0] = 0; 1699 1700 switch (appearance) { 1701 case BTM_BLE_APPEARANCE_GENERIC_PHONE: 1702 dev_class[1] = BTM_COD_MAJOR_PHONE; 1703 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED; 1704 break; 1705 case BTM_BLE_APPEARANCE_GENERIC_COMPUTER: 1706 dev_class[1] = BTM_COD_MAJOR_COMPUTER; 1707 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED; 1708 break; 1709 case BTM_BLE_APPEARANCE_GENERIC_REMOTE: 1710 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1711 dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL; 1712 break; 1713 case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER: 1714 case BTM_BLE_APPEARANCE_THERMOMETER_EAR: 1715 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1716 dev_class[2] = BTM_COD_MINOR_THERMOMETER; 1717 break; 1718 case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE: 1719 case BTM_BLE_APPEARANCE_HEART_RATE_BELT: 1720 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1721 dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR; 1722 break; 1723 case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE: 1724 case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM: 1725 case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST: 1726 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1727 dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR; 1728 break; 1729 case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER: 1730 case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP: 1731 case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST: 1732 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1733 dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER; 1734 break; 1735 case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE: 1736 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1737 dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER; 1738 break; 1739 case BTM_BLE_APPEARANCE_GENERIC_WEIGHT: 1740 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1741 dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE; 1742 break; 1743 case BTM_BLE_APPEARANCE_GENERIC_WALKING: 1744 case BTM_BLE_APPEARANCE_WALKING_IN_SHOE: 1745 case BTM_BLE_APPEARANCE_WALKING_ON_SHOE: 1746 case BTM_BLE_APPEARANCE_WALKING_ON_HIP: 1747 dev_class[1] = BTM_COD_MAJOR_HEALTH; 1748 dev_class[2] = BTM_COD_MINOR_STEP_COUNTER; 1749 break; 1750 case BTM_BLE_APPEARANCE_GENERIC_WATCH: 1751 case BTM_BLE_APPEARANCE_SPORTS_WATCH: 1752 dev_class[1] = BTM_COD_MAJOR_WEARABLE; 1753 dev_class[2] = BTM_COD_MINOR_WRIST_WATCH; 1754 break; 1755 case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES: 1756 dev_class[1] = BTM_COD_MAJOR_WEARABLE; 1757 dev_class[2] = BTM_COD_MINOR_GLASSES; 1758 break; 1759 case BTM_BLE_APPEARANCE_GENERIC_DISPLAY: 1760 dev_class[1] = BTM_COD_MAJOR_IMAGING; 1761 dev_class[2] = BTM_COD_MINOR_DISPLAY; 1762 break; 1763 case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER: 1764 dev_class[1] = BTM_COD_MAJOR_AUDIO; 1765 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED; 1766 break; 1767 case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER: 1768 case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER: 1769 case BTM_BLE_APPEARANCE_GENERIC_HID: 1770 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1771 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED; 1772 break; 1773 case BTM_BLE_APPEARANCE_HID_KEYBOARD: 1774 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1775 dev_class[2] = BTM_COD_MINOR_KEYBOARD; 1776 break; 1777 case BTM_BLE_APPEARANCE_HID_MOUSE: 1778 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1779 dev_class[2] = BTM_COD_MINOR_POINTING; 1780 break; 1781 case BTM_BLE_APPEARANCE_HID_JOYSTICK: 1782 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1783 dev_class[2] = BTM_COD_MINOR_JOYSTICK; 1784 break; 1785 case BTM_BLE_APPEARANCE_HID_GAMEPAD: 1786 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1787 dev_class[2] = BTM_COD_MINOR_GAMEPAD; 1788 break; 1789 case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET: 1790 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1791 dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET; 1792 break; 1793 case BTM_BLE_APPEARANCE_HID_CARD_READER: 1794 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1795 dev_class[2] = BTM_COD_MINOR_CARD_READER; 1796 break; 1797 case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN: 1798 dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1799 dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN; 1800 break; 1801 case BTM_BLE_APPEARANCE_UKNOWN: 1802 case BTM_BLE_APPEARANCE_GENERIC_CLOCK: 1803 case BTM_BLE_APPEARANCE_GENERIC_TAG: 1804 case BTM_BLE_APPEARANCE_GENERIC_KEYRING: 1805 case BTM_BLE_APPEARANCE_GENERIC_CYCLING: 1806 case BTM_BLE_APPEARANCE_CYCLING_COMPUTER: 1807 case BTM_BLE_APPEARANCE_CYCLING_SPEED: 1808 case BTM_BLE_APPEARANCE_CYCLING_CADENCE: 1809 case BTM_BLE_APPEARANCE_CYCLING_POWER: 1810 case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE: 1811 case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS: 1812 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION: 1813 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV: 1814 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD: 1815 case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV: 1816 default: 1817 dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED; 1818 dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED; 1819 }; 1820 } 1821 1822 /** 1823 * Update adv packet information into inquiry result. 1824 */ 1825 void btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type, BD_ADDR bda, 1826 uint16_t evt_type, uint8_t primary_phy, 1827 uint8_t secondary_phy, uint8_t advertising_sid, 1828 int8_t tx_power, int8_t rssi, 1829 uint16_t periodic_adv_int, 1830 std::vector<uint8_t> const& data) { 1831 tBTM_INQ_RESULTS* p_cur = &p_i->inq_info.results; 1832 uint8_t len; 1833 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars; 1834 1835 /* Save the info */ 1836 p_cur->inq_result_type = BTM_INQ_RESULT_BLE; 1837 p_cur->ble_addr_type = addr_type; 1838 p_cur->rssi = rssi; 1839 p_cur->ble_primary_phy = primary_phy; 1840 p_cur->ble_secondary_phy = secondary_phy; 1841 p_cur->ble_advertising_sid = advertising_sid; 1842 p_cur->ble_tx_power = tx_power; 1843 p_cur->ble_periodic_adv_int = periodic_adv_int; 1844 1845 if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI && 1846 ble_evt_type_is_scannable(evt_type) && 1847 !ble_evt_type_is_scan_resp(evt_type)) { 1848 p_i->scan_rsp = false; 1849 } else 1850 p_i->scan_rsp = true; 1851 1852 if (p_i->inq_count != p_inq->inq_counter) 1853 p_cur->device_type = BT_DEVICE_TYPE_BLE; 1854 else 1855 p_cur->device_type |= BT_DEVICE_TYPE_BLE; 1856 1857 if (evt_type != BTM_BLE_SCAN_RSP_EVT) p_cur->ble_evt_type = evt_type; 1858 1859 p_i->inq_count = p_inq->inq_counter; /* Mark entry for current inquiry */ 1860 1861 if (!data.empty()) { 1862 const uint8_t* p_flag = 1863 AdvertiseDataParser::GetFieldByType(data, BTM_BLE_AD_TYPE_FLAG, &len); 1864 if (p_flag != NULL) p_cur->flag = *p_flag; 1865 } 1866 1867 if (!data.empty()) { 1868 /* Check to see the BLE device has the Appearance UUID in the advertising 1869 * data. If it does 1870 * then try to convert the appearance value to a class of device value 1871 * Bluedroid can use. 1872 * Otherwise fall back to trying to infer if it is a HID device based on the 1873 * service class. 1874 */ 1875 const uint8_t* p_uuid16 = AdvertiseDataParser::GetFieldByType( 1876 data, BTM_BLE_AD_TYPE_APPEARANCE, &len); 1877 if (p_uuid16 && len == 2) { 1878 btm_ble_appearance_to_cod((uint16_t)p_uuid16[0] | (p_uuid16[1] << 8), 1879 p_cur->dev_class); 1880 } else { 1881 p_uuid16 = AdvertiseDataParser::GetFieldByType( 1882 data, BTM_BLE_AD_TYPE_16SRV_CMPL, &len); 1883 if (p_uuid16 != NULL) { 1884 uint8_t i; 1885 for (i = 0; i + 2 <= len; i = i + 2) { 1886 /* if this BLE device support HID over LE, set HID Major in class of 1887 * device */ 1888 if ((p_uuid16[i] | (p_uuid16[i + 1] << 8)) == UUID_SERVCLASS_LE_HID) { 1889 p_cur->dev_class[0] = 0; 1890 p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL; 1891 p_cur->dev_class[2] = 0; 1892 break; 1893 } 1894 } 1895 } 1896 } 1897 } 1898 1899 /* if BR/EDR not supported is not set, assume is a DUMO device */ 1900 if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 && 1901 !ble_evt_type_is_directed(evt_type)) { 1902 if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) { 1903 BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO"); 1904 p_cur->device_type |= BT_DEVICE_TYPE_DUMO; 1905 } else { 1906 BTM_TRACE_DEBUG("Random address, treating device as LE only"); 1907 } 1908 } else { 1909 BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device"); 1910 } 1911 } 1912 1913 /******************************************************************************* 1914 * 1915 * Function btm_clear_all_pending_le_entry 1916 * 1917 * Description This function is called to clear all LE pending entry in 1918 * inquiry database. 1919 * 1920 * Returns void 1921 * 1922 ******************************************************************************/ 1923 void btm_clear_all_pending_le_entry(void) { 1924 uint16_t xx; 1925 tINQ_DB_ENT* p_ent = btm_cb.btm_inq_vars.inq_db; 1926 1927 for (xx = 0; xx < BTM_INQ_DB_SIZE; xx++, p_ent++) { 1928 /* mark all pending LE entry as unused if an LE only device has scan 1929 * response outstanding */ 1930 if ((p_ent->in_use) && 1931 (p_ent->inq_info.results.device_type == BT_DEVICE_TYPE_BLE) && 1932 !p_ent->scan_rsp) 1933 p_ent->in_use = false; 1934 } 1935 } 1936 1937 void btm_ble_process_adv_addr(BD_ADDR bda, uint8_t addr_type) { 1938 #if (BLE_PRIVACY_SPT == TRUE) 1939 /* map address to security record */ 1940 bool match = btm_identity_addr_to_random_pseudo(bda, &addr_type, false); 1941 1942 BTM_TRACE_DEBUG("%s: bda= %0x:%0x:%0x:%0x:%0x:%0x", __func__, bda[0], bda[1], 1943 bda[2], bda[3], bda[4], bda[5]); 1944 /* always do RRA resolution on host */ 1945 if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) { 1946 tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda); 1947 if (match_rec) { 1948 match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA; 1949 memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN); 1950 1951 if (btm_ble_init_pseudo_addr(match_rec, bda)) { 1952 memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN); 1953 } else { 1954 // Assign the original address to be the current report address 1955 memcpy(bda, match_rec->ble.pseudo_addr, BD_ADDR_LEN); 1956 } 1957 } 1958 } 1959 #endif 1960 } 1961 1962 /** 1963 * This function is called when extended advertising report event is received . 1964 * It updates the inquiry database. If the inquiry database is full, the oldest 1965 * entry is discarded. 1966 */ 1967 void btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) { 1968 BD_ADDR bda, direct_address; 1969 uint8_t* p = data; 1970 uint8_t addr_type, num_reports, pkt_data_len, primary_phy, secondary_phy, 1971 advertising_sid; 1972 int8_t rssi, tx_power; 1973 uint16_t event_type, periodic_adv_int, direct_address_type; 1974 1975 /* Only process the results if the inquiry is still active */ 1976 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return; 1977 1978 /* Extract the number of reports in this event. */ 1979 STREAM_TO_UINT8(num_reports, p); 1980 1981 while (num_reports--) { 1982 if (p > data + data_len) { 1983 // TODO(jpawlowski): we should crash the stack here 1984 BTM_TRACE_ERROR( 1985 "Malformed LE Extended Advertising Report Event from controller - " 1986 "can't loop the data"); 1987 return; 1988 } 1989 1990 /* Extract inquiry results */ 1991 STREAM_TO_UINT16(event_type, p); 1992 STREAM_TO_UINT8(addr_type, p); 1993 STREAM_TO_BDADDR(bda, p); 1994 STREAM_TO_UINT8(primary_phy, p); 1995 STREAM_TO_UINT8(secondary_phy, p); 1996 STREAM_TO_UINT8(advertising_sid, p); 1997 STREAM_TO_INT8(tx_power, p); 1998 STREAM_TO_INT8(rssi, p); 1999 STREAM_TO_UINT16(periodic_adv_int, p); 2000 STREAM_TO_UINT8(direct_address_type, p); 2001 STREAM_TO_BDADDR(direct_address, p); 2002 STREAM_TO_UINT8(pkt_data_len, p); 2003 2004 uint8_t* pkt_data = p; 2005 p += pkt_data_len; /* Advance to the the next packet*/ 2006 2007 if (rssi >= 21 && rssi <= 126) { 2008 BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__, 2009 pkt_data_len, rssi); 2010 } 2011 2012 btm_ble_process_adv_addr(bda, addr_type); 2013 btm_ble_process_adv_pkt_cont(event_type, addr_type, bda, primary_phy, 2014 secondary_phy, advertising_sid, tx_power, rssi, 2015 periodic_adv_int, pkt_data_len, pkt_data); 2016 } 2017 } 2018 2019 /** 2020 * This function is called when advertising report event is received. It updates 2021 * the inquiry database. If the inquiry database is full, the oldest entry is 2022 * discarded. 2023 */ 2024 void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) { 2025 BD_ADDR bda; 2026 uint8_t* p = data; 2027 uint8_t legacy_evt_type, addr_type, num_reports, pkt_data_len; 2028 int8_t rssi; 2029 2030 /* Only process the results if the inquiry is still active */ 2031 if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return; 2032 2033 /* Extract the number of reports in this event. */ 2034 STREAM_TO_UINT8(num_reports, p); 2035 2036 while (num_reports--) { 2037 if (p > data + data_len) { 2038 // TODO(jpawlowski): we should crash the stack here 2039 BTM_TRACE_ERROR("Malformed LE Advertising Report Event from controller"); 2040 return; 2041 } 2042 2043 /* Extract inquiry results */ 2044 STREAM_TO_UINT8(legacy_evt_type, p); 2045 STREAM_TO_UINT8(addr_type, p); 2046 STREAM_TO_BDADDR(bda, p); 2047 STREAM_TO_UINT8(pkt_data_len, p); 2048 2049 uint8_t* pkt_data = p; 2050 p += pkt_data_len; /* Advance to the the rssi byte */ 2051 2052 STREAM_TO_INT8(rssi, p); 2053 2054 if (rssi >= 21 && rssi <= 126) { 2055 BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__, 2056 pkt_data_len, rssi); 2057 } 2058 2059 btm_ble_process_adv_addr(bda, addr_type); 2060 2061 uint16_t event_type; 2062 if (legacy_evt_type == 0x00) { // ADV_IND; 2063 event_type = 0x0013; 2064 } else if (legacy_evt_type == 0x01) { // ADV_DIRECT_IND; 2065 event_type = 0x0015; 2066 } else if (legacy_evt_type == 0x02) { // ADV_SCAN_IND; 2067 event_type = 0x0012; 2068 } else if (legacy_evt_type == 0x03) { // ADV_NONCONN_IND; 2069 event_type = 0x0010; 2070 } else if (legacy_evt_type == 0x04) { // SCAN_RSP; 2071 // We can't distinguish between "SCAN_RSP to an ADV_IND", and "SCAN_RSP to 2072 // an ADV_SCAN_IND", so always return "SCAN_RSP to an ADV_IND" 2073 event_type = 0x001B; 2074 } else { 2075 BTM_TRACE_ERROR( 2076 "Malformed LE Advertising Report Event - unsupported " 2077 "legacy_event_type 0x%02x", 2078 legacy_evt_type); 2079 return; 2080 } 2081 2082 btm_ble_process_adv_pkt_cont( 2083 event_type, addr_type, bda, PHY_LE_1M, PHY_LE_NO_PACKET, NO_ADI_PRESENT, 2084 TX_POWER_NOT_PRESENT, rssi, 0x00 /* no periodic adv */, pkt_data_len, 2085 pkt_data); 2086 } 2087 } 2088 2089 /** 2090 * This function is called after random address resolution is done, and proceed 2091 * to process adv packet. 2092 */ 2093 static void btm_ble_process_adv_pkt_cont( 2094 uint16_t evt_type, uint8_t addr_type, BD_ADDR bda, uint8_t primary_phy, 2095 uint8_t secondary_phy, uint8_t advertising_sid, int8_t tx_power, 2096 int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len, uint8_t* data) { 2097 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars; 2098 bool update = true; 2099 2100 std::vector<uint8_t> tmp; 2101 if (data_len != 0) tmp.insert(tmp.begin(), data, data + data_len); 2102 2103 bool is_scannable = ble_evt_type_is_scannable(evt_type); 2104 bool is_scan_resp = ble_evt_type_is_scan_resp(evt_type); 2105 2106 // We might have send scan request to this device before, but didn't get the 2107 // response. In such case make sure data is put at start, not appended to 2108 // already existing data. 2109 bool is_start = 2110 ble_evt_type_is_legacy(evt_type) && is_scannable && !is_scan_resp; 2111 std::vector<uint8_t> const& adv_data = 2112 is_start ? cache.Set(addr_type, bda, std::move(tmp)) 2113 : cache.Append(addr_type, bda, std::move(tmp)); 2114 2115 bool data_complete = (ble_evt_type_data_status(evt_type) != 0x01); 2116 2117 if (!data_complete) { 2118 // If we didn't receive whole adv data yet, don't report the device. 2119 DVLOG(1) << "Data not complete yet, waiting for more " 2120 << base::HexEncode(bda, BD_ADDR_LEN); 2121 return; 2122 } 2123 2124 bool is_active_scan = 2125 btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI; 2126 if (is_active_scan && is_scannable && !is_scan_resp) { 2127 // If we didn't receive scan response yet, don't report the device. 2128 DVLOG(1) << " Waiting for scan response " 2129 << base::HexEncode(bda, BD_ADDR_LEN); 2130 return; 2131 } 2132 2133 if (!AdvertiseDataParser::IsValid(adv_data)) { 2134 DVLOG(1) << __func__ << "Dropping bad advertisement packet: " 2135 << base::HexEncode(adv_data.data(), adv_data.size()); 2136 return; 2137 } 2138 2139 tINQ_DB_ENT* p_i = btm_inq_db_find(bda); 2140 2141 /* Check if this address has already been processed for this inquiry */ 2142 if (btm_inq_find_bdaddr(bda)) { 2143 /* never been report as an LE device */ 2144 if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) || 2145 /* scan repsonse to be updated */ 2146 (!p_i->scan_rsp))) { 2147 update = true; 2148 } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) { 2149 update = false; 2150 } else { 2151 /* if yes, skip it */ 2152 return; /* assumption: one result per event */ 2153 } 2154 } 2155 /* If existing entry, use that, else get a new one (possibly reusing the 2156 * oldest) */ 2157 if (p_i == NULL) { 2158 p_i = btm_inq_db_new(bda); 2159 if (p_i != NULL) { 2160 p_inq->inq_cmpl_info.num_resp++; 2161 } else 2162 return; 2163 } else if (p_i->inq_count != 2164 p_inq->inq_counter) /* first time seen in this inquiry */ 2165 { 2166 p_inq->inq_cmpl_info.num_resp++; 2167 } 2168 2169 /* update the LE device information in inquiry database */ 2170 btm_ble_update_inq_result(p_i, addr_type, bda, evt_type, primary_phy, 2171 secondary_phy, advertising_sid, tx_power, rssi, 2172 periodic_adv_int, adv_data); 2173 2174 uint8_t result = btm_ble_is_discoverable(bda, adv_data); 2175 if (result == 0) { 2176 cache.Clear(addr_type, bda); 2177 LOG_WARN(LOG_TAG, 2178 "%s device no longer discoverable, discarding advertising packet", 2179 __func__); 2180 return; 2181 } 2182 2183 if (!update) result &= ~BTM_BLE_INQ_RESULT; 2184 /* If the number of responses found and limited, issue a cancel inquiry */ 2185 if (p_inq->inqparms.max_resps && 2186 p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps) { 2187 /* new device */ 2188 if (p_i == NULL || 2189 /* assume a DUMO device, BR/EDR inquiry is always active */ 2190 (p_i && 2191 (p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) == 2192 BT_DEVICE_TYPE_BLE && 2193 p_i->scan_rsp)) { 2194 BTM_TRACE_WARNING( 2195 "INQ RES: Extra Response Received...cancelling inquiry.."); 2196 2197 /* if is non-periodic inquiry active, cancel now */ 2198 if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 && 2199 (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0) 2200 btsnd_hcic_inq_cancel(); 2201 2202 btm_ble_stop_inquiry(); 2203 2204 btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT); 2205 } 2206 } 2207 2208 tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb; 2209 if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) { 2210 (p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results, 2211 const_cast<uint8_t*>(adv_data.data()), adv_data.size()); 2212 } 2213 2214 tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb; 2215 if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) { 2216 (p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results, 2217 const_cast<uint8_t*>(adv_data.data()), adv_data.size()); 2218 } 2219 2220 cache.Clear(addr_type, bda); 2221 } 2222 2223 void btm_ble_process_phy_update_pkt(uint8_t len, uint8_t* data) { 2224 uint8_t status, tx_phy, rx_phy; 2225 uint16_t handle; 2226 2227 LOG_ASSERT(len == 5); 2228 uint8_t* p = data; 2229 STREAM_TO_UINT8(status, p); 2230 STREAM_TO_UINT16(handle, p); 2231 handle = handle & 0x0FFF; 2232 STREAM_TO_UINT8(tx_phy, p); 2233 STREAM_TO_UINT8(rx_phy, p); 2234 2235 tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle); 2236 if (!p_dev_rec) { 2237 BTM_TRACE_WARNING("%s: No Device Found!", __func__); 2238 return; 2239 } 2240 2241 tGATT_TCB* p_tcb = 2242 gatt_find_tcb_by_addr(p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE); 2243 if (p_tcb == NULL) return; 2244 2245 gatt_notify_phy_updated(p_tcb, tx_phy, rx_phy, status); 2246 } 2247 2248 /******************************************************************************* 2249 * 2250 * Function btm_ble_start_scan 2251 * 2252 * Description Start the BLE scan. 2253 * 2254 * Returns void 2255 * 2256 ******************************************************************************/ 2257 tBTM_STATUS btm_ble_start_scan(void) { 2258 tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var; 2259 /* start scan, disable duplicate filtering */ 2260 btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, p_inq->scan_duplicate_filter); 2261 2262 if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI) 2263 btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT); 2264 else 2265 btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT); 2266 2267 return BTM_CMD_STARTED; 2268 } 2269 2270 /******************************************************************************* 2271 * 2272 * Function btm_ble_stop_scan 2273 * 2274 * Description Stop the BLE scan. 2275 * 2276 * Returns void 2277 * 2278 ******************************************************************************/ 2279 void btm_ble_stop_scan(void) { 2280 BTM_TRACE_EVENT("btm_ble_stop_scan "); 2281 2282 if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI) 2283 btm_ble_clear_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT); 2284 else 2285 btm_ble_clear_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT); 2286 2287 /* Clear the inquiry callback if set */ 2288 btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE; 2289 2290 /* stop discovery now */ 2291 btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE); 2292 2293 btm_update_scanner_filter_policy(SP_ADV_ALL); 2294 } 2295 /******************************************************************************* 2296 * 2297 * Function btm_ble_stop_inquiry 2298 * 2299 * Description Stop the BLE Inquiry. 2300 * 2301 * Returns void 2302 * 2303 ******************************************************************************/ 2304 void btm_ble_stop_inquiry(void) { 2305 tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars; 2306 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb; 2307 2308 alarm_cancel(p_ble_cb->inq_var.inquiry_timer); 2309 2310 p_ble_cb->scan_activity &= ~BTM_BLE_INQUIRY_MASK; 2311 2312 /* If no more scan activity, stop LE scan now */ 2313 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) 2314 btm_ble_stop_scan(); 2315 else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) || 2316 (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) { 2317 BTM_TRACE_DEBUG("%s: setting default params for ongoing observe", __func__); 2318 btm_ble_stop_scan(); 2319 btm_ble_start_scan(); 2320 } 2321 2322 /* If we have a callback registered for inquiry complete, call it */ 2323 BTM_TRACE_DEBUG("BTM Inq Compl Callback: status 0x%02x, num results %d", 2324 p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp); 2325 2326 btm_process_inq_complete( 2327 HCI_SUCCESS, (uint8_t)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK)); 2328 } 2329 2330 /******************************************************************************* 2331 * 2332 * Function btm_ble_stop_observe 2333 * 2334 * Description Stop the BLE Observe. 2335 * 2336 * Returns void 2337 * 2338 ******************************************************************************/ 2339 static void btm_ble_stop_observe(void) { 2340 tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb; 2341 tBTM_CMPL_CB* p_obs_cb = p_ble_cb->p_obs_cmpl_cb; 2342 2343 alarm_cancel(p_ble_cb->observer_timer); 2344 2345 p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE; 2346 2347 p_ble_cb->p_obs_results_cb = NULL; 2348 p_ble_cb->p_obs_cmpl_cb = NULL; 2349 2350 if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_stop_scan(); 2351 2352 if (p_obs_cb) 2353 (p_obs_cb)((tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info); 2354 } 2355 /******************************************************************************* 2356 * 2357 * Function btm_ble_adv_states_operation 2358 * 2359 * Description Set or clear adv states in topology mask 2360 * 2361 * Returns operation status. true if sucessful, false otherwise. 2362 * 2363 ******************************************************************************/ 2364 typedef bool(BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK); 2365 static bool btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR* p_handler, 2366 uint8_t adv_evt) { 2367 bool rt = false; 2368 2369 switch (adv_evt) { 2370 case BTM_BLE_CONNECT_EVT: 2371 rt = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT); 2372 break; 2373 2374 case BTM_BLE_NON_CONNECT_EVT: 2375 rt = (*p_handler)(BTM_BLE_STATE_NON_CONN_ADV_BIT); 2376 break; 2377 case BTM_BLE_CONNECT_DIR_EVT: 2378 rt = (*p_handler)(BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT); 2379 break; 2380 2381 case BTM_BLE_DISCOVER_EVT: 2382 rt = (*p_handler)(BTM_BLE_STATE_SCAN_ADV_BIT); 2383 break; 2384 2385 case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT: 2386 rt = (*p_handler)(BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT); 2387 break; 2388 2389 default: 2390 BTM_TRACE_ERROR("unknown adv event : %d", adv_evt); 2391 break; 2392 } 2393 2394 return rt; 2395 } 2396 2397 /******************************************************************************* 2398 * 2399 * Function btm_ble_start_adv 2400 * 2401 * Description start the BLE advertising. 2402 * 2403 * Returns void 2404 * 2405 ******************************************************************************/ 2406 tBTM_STATUS btm_ble_start_adv(void) { 2407 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 2408 2409 if (!btm_ble_adv_states_operation(btm_ble_topology_check, p_cb->evt_type)) 2410 return BTM_WRONG_MODE; 2411 2412 #if (BLE_PRIVACY_SPT == TRUE) 2413 /* To relax resolving list, always have resolving list enabled, unless 2414 * directed adv */ 2415 if (p_cb->evt_type != BTM_BLE_CONNECT_LO_DUTY_DIR_EVT && 2416 p_cb->evt_type != BTM_BLE_CONNECT_DIR_EVT) 2417 /* enable resolving list is desired */ 2418 btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_ADV); 2419 #endif 2420 if (p_cb->afp != AP_SCAN_CONN_ALL) { 2421 btm_execute_wl_dev_operation(); 2422 btm_cb.ble_ctr_cb.wl_state |= BTM_BLE_WL_ADV; 2423 } 2424 2425 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_ENABLE); 2426 p_cb->adv_mode = BTM_BLE_ADV_ENABLE; 2427 btm_ble_adv_states_operation(btm_ble_set_topology_mask, p_cb->evt_type); 2428 return BTM_SUCCESS; 2429 } 2430 2431 /******************************************************************************* 2432 * 2433 * Function btm_ble_stop_adv 2434 * 2435 * Description Stop the BLE advertising. 2436 * 2437 * Returns void 2438 * 2439 ******************************************************************************/ 2440 tBTM_STATUS btm_ble_stop_adv(void) { 2441 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 2442 2443 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) { 2444 btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_DISABLE); 2445 2446 p_cb->fast_adv_on = false; 2447 p_cb->adv_mode = BTM_BLE_ADV_DISABLE; 2448 btm_cb.ble_ctr_cb.wl_state &= ~BTM_BLE_WL_ADV; 2449 2450 /* clear all adv states */ 2451 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK); 2452 } 2453 return BTM_SUCCESS; 2454 } 2455 2456 static void btm_ble_fast_adv_timer_timeout(UNUSED_ATTR void* data) { 2457 /* fast adv is completed, fall back to slow adv interval */ 2458 btm_ble_start_slow_adv(); 2459 } 2460 2461 /******************************************************************************* 2462 * 2463 * Function btm_ble_start_slow_adv 2464 * 2465 * Description Restart adv with slow adv interval 2466 * 2467 * Returns void 2468 * 2469 ******************************************************************************/ 2470 static void btm_ble_start_slow_adv(void) { 2471 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 2472 2473 if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) { 2474 tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; 2475 BD_ADDR p_addr_ptr = {0}; 2476 tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC; 2477 tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type; 2478 2479 btm_ble_stop_adv(); 2480 2481 p_cb->evt_type = btm_set_conn_mode_adv_init_addr( 2482 p_cb, p_addr_ptr, &init_addr_type, &own_addr_type); 2483 2484 /* slow adv mode never goes into directed adv */ 2485 btsnd_hcic_ble_write_adv_params(BTM_BLE_GAP_ADV_SLOW_INT, 2486 BTM_BLE_GAP_ADV_SLOW_INT, p_cb->evt_type, 2487 own_addr_type, init_addr_type, p_addr_ptr, 2488 p_cb->adv_chnl_map, p_cb->afp); 2489 2490 btm_ble_start_adv(); 2491 } 2492 } 2493 2494 static void btm_ble_inquiry_timer_gap_limited_discovery_timeout( 2495 UNUSED_ATTR void* data) { 2496 /* lim_timeout expired, limited discovery should exit now */ 2497 btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE; 2498 btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, 2499 btm_cb.btm_inq_vars.discoverable_mode); 2500 } 2501 2502 static void btm_ble_inquiry_timer_timeout(UNUSED_ATTR void* data) { 2503 btm_ble_stop_inquiry(); 2504 } 2505 2506 static void btm_ble_observer_timer_timeout(UNUSED_ATTR void* data) { 2507 btm_ble_stop_observe(); 2508 } 2509 2510 void btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) { 2511 if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) { 2512 /* refresh the random addr */ 2513 btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low)); 2514 } 2515 } 2516 2517 /******************************************************************************* 2518 * 2519 * Function btm_ble_read_remote_features_complete 2520 * 2521 * Description This function is called when the command complete message 2522 * is received from the HCI for the read LE remote feature 2523 * supported complete event. 2524 * 2525 * Returns void 2526 * 2527 ******************************************************************************/ 2528 void btm_ble_read_remote_features_complete(uint8_t* p) { 2529 BTM_TRACE_EVENT("%s", __func__); 2530 2531 uint16_t handle; 2532 uint8_t status; 2533 STREAM_TO_UINT8(status, p); 2534 STREAM_TO_UINT16(handle, p); 2535 handle = handle & 0x0FFF; // only 12 bits meaningful 2536 2537 if (status != HCI_SUCCESS) { 2538 BTM_TRACE_ERROR("%s: failed for handle: 0x%04d, status 0x%02x", __func__, 2539 handle, status); 2540 if (status != HCI_ERR_UNSUPPORTED_REM_FEATURE) return; 2541 } 2542 2543 int idx = btm_handle_to_acl_index(handle); 2544 if (idx == MAX_L2CAP_LINKS) { 2545 BTM_TRACE_ERROR("%s: can't find acl for handle: 0x%04d", __func__, handle); 2546 return; 2547 } 2548 2549 if (status == HCI_SUCCESS) { 2550 STREAM_TO_ARRAY(btm_cb.acl_db[idx].peer_le_features, p, BD_FEATURES_LEN); 2551 } 2552 2553 btsnd_hcic_rmt_ver_req(handle); 2554 } 2555 2556 /******************************************************************************* 2557 * 2558 * Function btm_ble_write_adv_enable_complete 2559 * 2560 * Description This function process the write adv enable command complete. 2561 * 2562 * Returns void 2563 * 2564 ******************************************************************************/ 2565 void btm_ble_write_adv_enable_complete(uint8_t* p) { 2566 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var; 2567 2568 /* if write adv enable/disbale not succeed */ 2569 if (*p != HCI_SUCCESS) { 2570 /* toggle back the adv mode */ 2571 p_cb->adv_mode = !p_cb->adv_mode; 2572 } 2573 } 2574 2575 /******************************************************************************* 2576 * 2577 * Function btm_ble_dir_adv_tout 2578 * 2579 * Description when directed adv time out 2580 * 2581 * Returns void 2582 * 2583 ******************************************************************************/ 2584 void btm_ble_dir_adv_tout(void) { 2585 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE; 2586 2587 /* make device fall back into undirected adv mode by default */ 2588 btm_cb.ble_ctr_cb.inq_var.directed_conn = false; 2589 } 2590 2591 /******************************************************************************* 2592 * 2593 * Function btm_ble_set_topology_mask 2594 * 2595 * Description set BLE topology mask 2596 * 2597 * Returns true is request is allowed, false otherwise. 2598 * 2599 ******************************************************************************/ 2600 bool btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) { 2601 request_state_mask &= BTM_BLE_STATE_ALL_MASK; 2602 btm_cb.ble_ctr_cb.cur_states |= (request_state_mask & BTM_BLE_STATE_ALL_MASK); 2603 return true; 2604 } 2605 2606 /******************************************************************************* 2607 * 2608 * Function btm_ble_clear_topology_mask 2609 * 2610 * Description Clear BLE topology bit mask 2611 * 2612 * Returns true is request is allowed, false otherwise. 2613 * 2614 ******************************************************************************/ 2615 bool btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) { 2616 request_state_mask &= BTM_BLE_STATE_ALL_MASK; 2617 btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask; 2618 return true; 2619 } 2620 2621 /******************************************************************************* 2622 * 2623 * Function btm_ble_update_link_topology_mask 2624 * 2625 * Description This function update the link topology mask 2626 * 2627 * Returns void 2628 * 2629 ******************************************************************************/ 2630 void btm_ble_update_link_topology_mask(uint8_t link_role, bool increase) { 2631 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_CONN_MASK); 2632 2633 if (increase) 2634 btm_cb.ble_ctr_cb.link_count[link_role]++; 2635 else if (btm_cb.ble_ctr_cb.link_count[link_role] > 0) 2636 btm_cb.ble_ctr_cb.link_count[link_role]--; 2637 2638 if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_MASTER]) 2639 btm_ble_set_topology_mask(BTM_BLE_STATE_MASTER_BIT); 2640 2641 if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_SLAVE]) 2642 btm_ble_set_topology_mask(BTM_BLE_STATE_SLAVE_BIT); 2643 2644 if (link_role == HCI_ROLE_SLAVE && increase) { 2645 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE; 2646 /* make device fall back into undirected adv mode by default */ 2647 btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT; 2648 /* clear all adv states */ 2649 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK); 2650 } 2651 } 2652 2653 /******************************************************************************* 2654 * 2655 * Function btm_ble_update_mode_operation 2656 * 2657 * Description This function update the GAP role operation when a link 2658 * status is updated. 2659 * 2660 * Returns void 2661 * 2662 ******************************************************************************/ 2663 void btm_ble_update_mode_operation(uint8_t link_role, BD_ADDR bd_addr, 2664 uint8_t status) { 2665 if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) { 2666 btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE; 2667 /* make device fall back into undirected adv mode by default */ 2668 btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT; 2669 /* clear all adv states */ 2670 btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK); 2671 } 2672 2673 if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE) { 2674 btm_ble_set_connectability(btm_cb.btm_inq_vars.connectable_mode | 2675 btm_cb.ble_ctr_cb.inq_var.connectable_mode); 2676 } 2677 2678 /* when no connection is attempted, and controller is not rejecting last 2679 request 2680 due to resource limitation, start next direct connection or background 2681 connection 2682 now in order */ 2683 if (btm_ble_get_conn_st() == BLE_CONN_IDLE && 2684 status != HCI_ERR_HOST_REJECT_RESOURCES && 2685 status != HCI_ERR_MAX_NUM_OF_CONNECTIONS && 2686 !btm_send_pending_direct_conn()) { 2687 btm_ble_resume_bg_conn(); 2688 } 2689 } 2690 2691 /******************************************************************************* 2692 * 2693 * Function btm_ble_init 2694 * 2695 * Description Initialize the control block variable values. 2696 * 2697 * Returns void 2698 * 2699 ******************************************************************************/ 2700 void btm_ble_init(void) { 2701 tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb; 2702 2703 BTM_TRACE_DEBUG("%s", __func__); 2704 2705 alarm_free(p_cb->observer_timer); 2706 alarm_free(p_cb->inq_var.fast_adv_timer); 2707 memset(p_cb, 0, sizeof(tBTM_BLE_CB)); 2708 memset(&(btm_cb.cmn_ble_vsc_cb), 0, sizeof(tBTM_BLE_VSC_CB)); 2709 btm_cb.cmn_ble_vsc_cb.values_read = false; 2710 2711 p_cb->observer_timer = alarm_new("btm_ble.observer_timer"); 2712 p_cb->cur_states = 0; 2713 p_cb->conn_pending_q = fixed_queue_new(SIZE_MAX); 2714 2715 p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE; 2716 p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE; 2717 p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP; 2718 p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP; 2719 p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP; 2720 p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE; 2721 p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE; 2722 p_cb->inq_var.fast_adv_timer = alarm_new("btm_ble_inq.fast_adv_timer"); 2723 p_cb->inq_var.inquiry_timer = alarm_new("btm_ble_inq.inquiry_timer"); 2724 2725 /* for background connection, reset connection params to be undefined */ 2726 p_cb->scan_int = p_cb->scan_win = BTM_BLE_SCAN_PARAM_UNDEF; 2727 2728 p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT; 2729 2730 p_cb->addr_mgnt_cb.refresh_raddr_timer = 2731 alarm_new("btm_ble_addr.refresh_raddr_timer"); 2732 2733 #if (BLE_VND_INCLUDED == FALSE) 2734 btm_ble_adv_filter_init(); 2735 #endif 2736 } 2737 2738 /******************************************************************************* 2739 * 2740 * Function btm_ble_topology_check 2741 * 2742 * Description check to see requested state is supported. One state check 2743 * at a time is supported 2744 * 2745 * Returns true is request is allowed, false otherwise. 2746 * 2747 ******************************************************************************/ 2748 bool btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask) { 2749 bool rt = false; 2750 2751 uint8_t state_offset = 0; 2752 uint16_t cur_states = btm_cb.ble_ctr_cb.cur_states; 2753 uint8_t mask, offset; 2754 uint8_t request_state = 0; 2755 2756 /* check only one bit is set and within valid range */ 2757 if (request_state_mask == BTM_BLE_STATE_INVALID || 2758 request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT || 2759 (request_state_mask & (request_state_mask - 1)) != 0) { 2760 BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask); 2761 return rt; 2762 } 2763 2764 while (request_state_mask) { 2765 request_state_mask >>= 1; 2766 request_state++; 2767 } 2768 2769 /* check if the requested state is supported or not */ 2770 mask = btm_le_state_combo_tbl[0][request_state - 1][0]; 2771 offset = btm_le_state_combo_tbl[0][request_state - 1][1]; 2772 2773 const uint8_t* ble_supported_states = 2774 controller_get_interface()->get_ble_supported_states(); 2775 2776 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, mask, offset)) { 2777 BTM_TRACE_ERROR("state requested not supported: %d", request_state); 2778 return rt; 2779 } 2780 2781 rt = true; 2782 /* make sure currently active states are all supported in conjunction with the 2783 requested 2784 state. If the bit in table is not set, the combination is not supported */ 2785 while (cur_states != 0) { 2786 if (cur_states & 0x01) { 2787 mask = btm_le_state_combo_tbl[request_state][state_offset][0]; 2788 offset = btm_le_state_combo_tbl[request_state][state_offset][1]; 2789 2790 if (mask != 0 && offset != 0) { 2791 if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, mask, offset)) { 2792 rt = false; 2793 break; 2794 } 2795 } 2796 } 2797 cur_states >>= 1; 2798 state_offset++; 2799 } 2800 return rt; 2801 } 2802