15738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/******************************************************************************
25738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
3a6ce7751d84218c193eb90d390aef23217b1737eWei Wang *  Copyright (C) 2008-2014 Broadcom Corporation
45738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
55738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Licensed under the Apache License, Version 2.0 (the "License");
65738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  you may not use this file except in compliance with the License.
75738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  You may obtain a copy of the License at:
85738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
95738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  http://www.apache.org/licenses/LICENSE-2.0
105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
125738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
135738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  See the License for the specific language governing permissions and
155738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  limitations under the License.
165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ******************************************************************************/
185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/******************************************************************************
205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *  This file contains functions for BLE GAP.
225738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project *
235738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project ******************************************************************************/
245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2549a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#define LOG_TAG "bt_btm_ble"
2649a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen
270c683232e28ea616844bfa03d876a08df217ba13Jakub Pawlowski#include <base/bind.h>
28119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski#include <base/callback.h>
290595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski#include <base/strings/string_number_conversions.h>
305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include <stddef.h>
31911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#include <stdio.h>
32911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#include <string.h>
330595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski#include <list>
340595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski#include <vector>
355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
365738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "bt_types.h"
375cd8bff2dd0337cb52bf48f312e3d2d55a8882fbMike J. Chen#include "bt_utils.h"
3821015a484b3fa3d7aa7cdc291b30238089d14f4eWei Wang#include "btm_ble_api.h"
3949a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#include "btm_int.h"
4021015a484b3fa3d7aa7cdc291b30238089d14f4eWei Wang#include "btu.h"
4179ecab5d0418fde77e9afcdd451bd713af73e180Chris Manton#include "device/include/controller.h"
425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#include "gap_api.h"
4349a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#include "hcimsgs.h"
44d7ffd64accbd50a27289a388856e56244ccbb5daMyles Watson#include "osi/include/osi.h"
4516fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
46103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski#include "advertise_data_parser.h"
477fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta#include "btm_ble_int.h"
4849a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#include "gatt_int.h"
4949a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#include "gattdefs.h"
5049a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#include "l2c_int.h"
5144802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati#include "osi/include/log.h"
527fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
53911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_BLE_NAME_SHORT 0x01
54911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_BLE_NAME_CMPL 0x02
555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
56911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_BLE_FILTER_TARGET_UNKNOWN 0xff
57911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_BLE_POLICY_UNKNOWN 0xff
585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
59911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS (30 * 1000)
60911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define MIN_ADV_LENGTH 2
61935324abbe8706307297f09cf79bbcd4efb73367Satya Calloji#define BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE 9
62935324abbe8706307297f09cf79bbcd4efb73367Satya Calloji
630595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowskinamespace {
640595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
650595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowskiclass AdvertisingCache {
660595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski public:
670595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  /* Set the data to |data| for device |addr_type, addr| */
689e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski  const std::vector<uint8_t>& Set(uint8_t addr_type, const RawAddress& addr,
690595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                                  std::vector<uint8_t> data) {
700595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    auto it = Find(addr_type, addr);
710595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    if (it != items.end()) {
720595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      it->data = std::move(data);
730595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      return it->data;
740595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    }
750595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
760595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    if (items.size() > cache_max) {
770595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      items.pop_back();
780595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    }
790595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
800595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    items.emplace_front(addr_type, addr, std::move(data));
810595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    return items.front().data;
820595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  }
830595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
840595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  /* Append |data| for device |addr_type, addr| */
859e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski  const std::vector<uint8_t>& Append(uint8_t addr_type, const RawAddress& addr,
860595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                                     std::vector<uint8_t> data) {
870595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    auto it = Find(addr_type, addr);
880595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    if (it != items.end()) {
890595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      it->data.insert(it->data.end(), data.begin(), data.end());
900595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      return it->data;
910595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    }
920595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
930595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    if (items.size() > cache_max) {
940595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      items.pop_back();
950595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    }
960595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
970595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    items.emplace_front(addr_type, addr, std::move(data));
980595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    return items.front().data;
990595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  }
1000595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1010595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  /* Clear data for device |addr_type, addr| */
1029e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski  void Clear(uint8_t addr_type, const RawAddress& addr) {
1030595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    auto it = Find(addr_type, addr);
1040595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    if (it != items.end()) {
1050595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      items.erase(it);
1060595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    }
1070595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  }
1080595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1090595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski private:
1100595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  struct Item {
1110595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    uint8_t addr_type;
1129e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski    RawAddress addr;
1130595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    std::vector<uint8_t> data;
1140595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1159e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski    Item(uint8_t addr_type, const RawAddress& addr, std::vector<uint8_t> data)
116c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski        : addr_type(addr_type), addr(addr), data(data) {}
1170595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  };
1180595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1199e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski  std::list<Item>::iterator Find(uint8_t addr_type, const RawAddress& addr) {
1200595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    for (auto it = items.begin(); it != items.end(); it++) {
121c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski      if (it->addr_type == addr_type && it->addr == addr) {
1220595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski        return it;
1230595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      }
1240595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    }
1250595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    return items.end();
1260595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  }
1270595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1280595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  /* we keep maximum 7 devices in the cache */
1290595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  const size_t cache_max = 7;
1300595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  std::list<Item> items;
1310595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski};
1320595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1330595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski/* Devices in this cache are waiting for eiter scan response, or chained packets
1340595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski * on secondary channel */
1350595ca0018ac9980d1dd7e192301f8494a01f9cbJakub PawlowskiAdvertisingCache cache;
1360595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1370595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski}  // namespace
1380595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
139d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_VND_INCLUDED == TRUE)
140911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic tBTM_BLE_CTRL_FEATURES_CBACK* p_ctrl_le_feature_rd_cmpl_cback = NULL;
141104e3f238f500cacdb8afaccdc672dfc34eae7eeBernhard Rosenkränzer#endif
1428d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta
1435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
144ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *  Local functions
145ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
146d19e0785e662e640191a075eda07acce61c2aedaMarie Janssenstatic void btm_ble_update_adv_flag(uint8_t flag);
1477de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowskistatic void btm_ble_process_adv_pkt_cont(
1489e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski    uint16_t evt_type, uint8_t addr_type, const RawAddress& bda,
149c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
150c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
151c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    uint8_t* data);
152911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic uint8_t btm_set_conn_mode_adv_init_addr(tBTM_BLE_INQ_CB* p_cb,
1539e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski                                               RawAddress& p_peer_addr_ptr,
154911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                               tBLE_ADDR_TYPE* p_peer_addr_type,
155911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                               tBLE_ADDR_TYPE* p_own_addr_type);
1567fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Battastatic void btm_ble_stop_observe(void);
157911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_fast_adv_timer_timeout(void* data);
15878bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavovstatic void btm_ble_start_slow_adv(void);
159911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_inquiry_timer_gap_limited_discovery_timeout(void* data);
160911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_inquiry_timer_timeout(void* data);
161911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_observer_timer_timeout(void* data);
16278bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov
163911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_BLE_INQ_RESULT 0x01
164911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#define BTM_BLE_OBS_RESULT 0x02
1655738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
166d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowskibool ble_evt_type_is_connectable(uint16_t evt_type) {
167d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  return evt_type & (1 << BLE_EVT_CONNECTABLE_BIT);
168d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski}
169d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
170d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowskibool ble_evt_type_is_scannable(uint16_t evt_type) {
171d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  return evt_type & (1 << BLE_EVT_SCANNABLE_BIT);
172d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski}
173d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
174d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowskibool ble_evt_type_is_directed(uint16_t evt_type) {
175d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  return evt_type & (1 << BLE_EVT_DIRECTED_BIT);
176d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski}
177d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
178d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowskibool ble_evt_type_is_scan_resp(uint16_t evt_type) {
179d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  return evt_type & (1 << BLE_EVT_SCAN_RESPONSE_BIT);
180d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski}
181d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
182d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowskibool ble_evt_type_is_legacy(uint16_t evt_type) {
183d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  return evt_type & (1 << BLE_EVT_LEGACY_BIT);
184d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski}
185d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
1860595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowskiuint8_t ble_evt_type_data_status(uint16_t evt_type) {
1870595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  return (evt_type >> 5) & 3;
1880595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski}
1890595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
1905e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowskiconstexpr uint8_t UNSUPPORTED = 255;
1915e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski
1927fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/* LE states combo bit to check */
1935e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowskiconst uint8_t btm_le_state_combo_tbl[BTM_BLE_STATE_MAX][BTM_BLE_STATE_MAX] = {
1945e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
1955e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* single state support */
1965e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_BIT, /* conn_adv */
1975e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_BIT,     /* init */
1985e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_BIT,     /* master */
1995e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SLAVE_BIT,    /* slave */
2005e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                /* todo: lo du dir adv, not covered ? */
2015e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_BIT, /* hi duty dir adv */
2025e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_BIT,    /* non connectable adv */
2035e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_BIT,       /*  passive scan */
2045e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_BIT,     /*   active scan */
2055e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_BIT         /* scanable adv */
2065e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2075e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2085e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* conn_adv =0 */
2095e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* conn_adv */
2105e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_INIT_BIT,        /* init: 32 */
2115e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_MASTER_BIT,      /* master: 35 */
2125e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_SLAVE_BIT,       /* slave: 38,*/
2135e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* lo du dir adv */
2145e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* hi duty dir adv */
2155e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* non connectable adv */
2165e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_PASS_SCAN_BIT,   /*  passive scan */
2175e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_ACTIVE_SCAN_BIT, /*   active scan */
2185e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED                             /* scanable adv */
2195e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2205e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2215e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* init */
2225e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_INIT_BIT,        /* conn_adv: 32 */
2235e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* init */
2245e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_MASTER_BIT,          /* master 28 */
2255e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_MASTER_SLAVE_BIT,    /* slave 41 */
2265e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_LO_DUTY_DIR_ADV_INIT_BIT, /* lo du dir adv 34 */
2275e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_INIT_BIT, /* hi duty dir adv 33 */
2285e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_INIT_BIT,        /*  non connectable adv */
2295e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_INIT_BIT,       /* passive scan */
2305e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_INIT_BIT,     /*  active scan */
2315e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_INIT_BIT         /* scanable adv */
2325e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski
2335e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2345e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2355e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* master */
2365e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_MASTER_BIT,        /* conn_adv: 35 */
2375e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_MASTER_BIT,            /* init 28 */
2385e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_MASTER_BIT,            /* master 28 */
2395e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_INIT_BIT,          /* slave: 32 */
2405e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_LO_DUTY_DIR_ADV_MASTER_BIT, /* lo duty cycle adv 37 */
2415e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_MASTER_BIT, /* hi duty cycle adv 36 */
2425e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_MASTER_BIT,    /*  non connectable adv*/
2435e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_MASTER_BIT,       /*  passive scan */
2445e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_MASTER_BIT,     /*   active scan */
2455e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_MASTER_BIT         /*  scanable adv */
2465e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski
2475e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2485e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2495e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* slave */
2505e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_SLAVE_BIT,        /* conn_adv: 38,*/
2515e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_MASTER_SLAVE_BIT,     /* init 41 */
2525e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_INIT_MASTER_SLAVE_BIT,     /* master 41 */
2535e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_SLAVE_BIT,        /* slave: 38,*/
2545e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_BIT, /* lo duty cycle adv 40 */
2555e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_BIT, /* hi duty cycle adv 39 */
2565e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_SLAVE_BIT,    /* non connectable adv */
2575e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_SLAVE_BIT,       /* passive scan */
2585e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_SLAVE_BIT,     /*  active scan */
2595e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_SLAVE_BIT         /* scanable adv */
2605e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski
2615e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2625e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2635e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* lo duty cycle adv */
2645e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                              /* conn_adv: 38,*/
2655e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_LO_DUTY_DIR_ADV_INIT_BIT,   /* init 34 */
2665e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_LO_DUTY_DIR_ADV_MASTER_BIT, /* master 37 */
2675e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_LO_DUTY_DIR_ADV_SLAVE_BIT,  /* slave: 40 */
2685e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                              /* lo duty cycle adv 40 */
2695e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                              /* hi duty cycle adv 39 */
2705e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                              /*  non connectable adv */
2715e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED, /* TODO: passive scan, not covered? */
2725e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED, /* TODO:  active scan, not covered? */
2735e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED  /*  scanable adv */
2745e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2755e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2765e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* hi duty cycle adv */
2775e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* conn_adv: 38,*/
2785e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_INIT_BIT,      /* init 33 */
2795e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_MASTER_BIT,    /* master 36 */
2805e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_SLAVE_BIT,     /* slave: 39*/
2815e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* lo duty cycle adv 40 */
2825e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* hi duty cycle adv 39 */
2835e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* non connectable adv */
2845e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_BIT, /* passive scan */
2855e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_BIT, /* active scan */
2865e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED                                    /* scanable adv */
2875e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
2885e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
2895e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* non connectable adv */
2905e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                /* conn_adv: */
2915e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_INIT_BIT,            /* init  */
2925e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_MASTER_BIT,      /* master  */
2935e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_SLAVE_BIT,       /* slave: */
2945e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                /* lo duty cycle adv */
2955e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                /* hi duty cycle adv */
2965e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                /* non connectable adv */
2975e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_PASS_SCAN_BIT,   /* passive scan */
2985e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_BIT, /* active scan */
2995e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED                                 /* scanable adv */
3005e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
3015e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
3025e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* passive scan */
3035e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_PASS_SCAN_BIT,        /* conn_adv: */
3045e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_INIT_BIT,            /* init  */
3055e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_MASTER_BIT,          /* master  */
3065e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_PASS_SCAN_SLAVE_BIT,           /* slave: */
3075e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* lo duty cycle adv */
3085e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_PASS_SCAN_BIT, /* hi duty cycle adv */
3095e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_PASS_SCAN_BIT,    /* non connectable adv */
3105e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* passive scan */
3115e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                 /* active scan */
3125e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_PASS_SCAN_BIT         /* scanable adv */
3135e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
3145e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
3155e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* active scan */
3165e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_CONN_ADV_ACTIVE_SCAN_BIT,        /* conn_adv: */
3175e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_INIT_BIT,            /* init  */
3185e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_MASTER_BIT,          /* master  */
3195e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_ACTIVE_SCAN_SLAVE_BIT,           /* slave: */
3205e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                   /* lo duty cycle adv */
3215e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_HI_DUTY_DIR_ADV_ACTIVE_SCAN_BIT, /* hi duty cycle adv */
3225e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_NON_CONN_ADV_ACTIVE_SCAN_BIT, /*  non connectable adv */
3235e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                /* TODO: passive scan */
3245e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                                /* TODO:  active scan */
3255e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_ACTIVE_SCAN_BIT      /*  scanable adv */
3265e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    },
3275e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    {
3285e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        /* scanable adv */
3295e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* conn_adv: */
3305e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_INIT_BIT,        /* init  */
3315e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_MASTER_BIT,      /* master  */
3325e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_SLAVE_BIT,       /* slave: */
3335e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* lo duty cycle adv */
3345e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* hi duty cycle adv */
3355e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED,                            /* non connectable adv */
3365e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_PASS_SCAN_BIT,   /*  passive scan */
3375e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        HCI_LE_STATES_SCAN_ADV_ACTIVE_SCAN_BIT, /*  active scan */
3385e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        UNSUPPORTED                             /* scanable adv */
3395e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski    }};
3407fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
3417fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/* check LE combo state supported */
3425e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowskiinline bool BTM_LE_STATES_SUPPORTED(const uint8_t* x, uint8_t bit_num) {
3435e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski  uint8_t mask = 1 << (bit_num % 8);
3445e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski  uint8_t offset = bit_num / 8;
3455e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski  return ((x)[offset] & mask);
3465e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski}
3477fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
348ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
349ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
350ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleUpdateAdvFilterPolicy
351ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
352ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function update the filter policy of advertiser.
353ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
354ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameter        adv_policy: advertising filter policy
355ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
356ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Return           void
357ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
358911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid BTM_BleUpdateAdvFilterPolicy(tBTM_BLE_AFP adv_policy) {
359911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
360911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
361aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  RawAddress adv_address = RawAddress::kEmpty;
362911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t adv_mode = p_cb->adv_mode;
363ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
364911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("BTM_BleUpdateAdvFilterPolicy");
365911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
366911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return;
367911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
368911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->afp != adv_policy) {
369911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->afp = adv_policy;
370911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
371911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* if adv active, stop and restart */
372911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_stop_adv();
373911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
374911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_cb->connectable_mode & BTM_BLE_CONNECTABLE)
375911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
376aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski          p_cb, adv_address, &init_addr_type, &p_cb->adv_addr_type);
377911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
378911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btsnd_hcic_ble_write_adv_params(
379911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        (uint16_t)(p_cb->adv_interval_min ? p_cb->adv_interval_min
380911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                          : BTM_BLE_GAP_ADV_SLOW_INT),
381911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        (uint16_t)(p_cb->adv_interval_max ? p_cb->adv_interval_max
382911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                          : BTM_BLE_GAP_ADV_SLOW_INT),
383aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski        p_cb->evt_type, p_cb->adv_addr_type, init_addr_type, adv_address,
384911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        p_cb->adv_chnl_map, p_cb->afp);
385911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
386911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
387911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
388ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
389935324abbe8706307297f09cf79bbcd4efb73367Satya Calloji
390935324abbe8706307297f09cf79bbcd4efb73367Satya Calloji/*******************************************************************************
391ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
392ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleObserve
393ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
394ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This procedure keep the device listening for advertising
395ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  events from a broadcast device.
396ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
397ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       start: start or stop observe.
398ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  white_list: use white list in observer mode or not.
399ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
400ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
401ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
402ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
403911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS BTM_BleObserve(bool start, uint8_t duration,
404911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                           tBTM_INQ_RESULTS_CB* p_results_cb,
405911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                           tBTM_CMPL_CB* p_cmpl_cb) {
406911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
407911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_STATUS status = BTM_WRONG_MODE;
408911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
409911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint32_t scan_interval =
410911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
411911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint32_t scan_window =
412911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
413911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
414911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s : scan_type:%d, %d, %d", __func__,
415911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  btm_cb.btm_inq_vars.scan_type, p_inq->scan_interval,
416911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  p_inq->scan_window);
417911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
418911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
419911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
420911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (start) {
421911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* shared inquiry database, do not allow observe if any inquiry is active */
422911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
423911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      BTM_TRACE_ERROR("%s Observe Already Active", __func__);
424911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      return status;
425911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
426911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
427911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.p_obs_results_cb = p_results_cb;
428911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.p_obs_cmpl_cb = p_cmpl_cb;
429911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    status = BTM_CMD_STARTED;
430911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
431911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* scan is not started */
432911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
433911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* allow config of scan type */
434911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_inq->scan_type = (p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE)
435911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                             ? BTM_BLE_SCAN_MODE_ACTI
436911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                             : p_inq->scan_type;
437911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson/* assume observe always not using white list */
438911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == true)
439911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* enable resolving list */
440911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
441911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#endif
44270b9598970e5b18ba53c282319e68faea6eb5681Satya Calloji
443defbb91f06308e4056a9e9609cefb5011f86c94dJakub Pawlowski      btm_send_hci_set_scan_params(
444defbb91f06308e4056a9e9609cefb5011f86c94dJakub Pawlowski          p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window,
445defbb91f06308e4056a9e9609cefb5011f86c94dJakub Pawlowski          btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, BTM_BLE_DEFAULT_SFP);
446911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
447911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_inq->scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
448911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status = btm_ble_start_scan();
449911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
450911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
451911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (status == BTM_CMD_STARTED) {
452911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_cb.ble_ctr_cb.scan_activity |= BTM_LE_OBSERVE_ACTIVE;
453911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      if (duration != 0) {
454911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        /* start observer timer */
455911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        period_ms_t duration_ms = duration * 1000;
45647616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski        alarm_set_on_mloop(btm_cb.ble_ctr_cb.observer_timer, duration_ms,
45747616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski                           btm_ble_observer_timer_timeout, NULL);
458911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      }
4595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
460911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
461911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    status = BTM_CMD_STARTED;
462911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_stop_observe();
463911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
464911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_ERROR("%s Observe not active", __func__);
465911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
4665738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
467911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return status;
4685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
4695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
470d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_VND_INCLUDED == TRUE)
4715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
472ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
473ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_vsc_brcm_features_complete
474ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
475ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Command Complete callback for HCI_BLE_VENDOR_CAP_OCF
476ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
477ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
478ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
479ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
480911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_vendor_capability_vsc_cmpl_cback(
481911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    tBTM_VSC_CMPL* p_vcs_cplt_params) {
482911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t status = 0xFF;
483911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t* p;
484a6ce7751d84218c193eb90d390aef23217b1737eWei Wang
485911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("%s", __func__);
486935324abbe8706307297f09cf79bbcd4efb73367Satya Calloji
487911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* Check status of command complete event */
488911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if ((p_vcs_cplt_params->opcode == HCI_BLE_VENDOR_CAP_OCF) &&
489911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      (p_vcs_cplt_params->param_len > 0)) {
490911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p = p_vcs_cplt_params->p_param_buf;
491911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(status, p);
492911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
493911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
494911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (status == HCI_SUCCESS) {
495911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.adv_inst_max, p);
496911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.rpa_offloading, p);
497911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg, p);
498911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz, p);
499911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.filter_support, p);
500911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.max_filter, p);
501911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(btm_cb.cmn_ble_vsc_cb.energy_support, p);
502911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
503911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_vcs_cplt_params->param_len >
504911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_VSC_CHIP_CAPABILITY_RSP_LEN_L_RELEASE) {
505911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.version_supported, p);
506911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else {
507911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_cb.cmn_ble_vsc_cb.version_supported =
508911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          BTM_VSC_CHIP_CAPABILITY_L_VERSION;
509a6ce7751d84218c193eb90d390aef23217b1737eWei Wang    }
510c4e2596a112a0dd63ac681b1226548c0f95e828cSatya Calloji
511911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (btm_cb.cmn_ble_vsc_cb.version_supported >=
512911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_VSC_CHIP_CAPABILITY_M_VERSION) {
513911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.total_trackable_advertisers, p);
514911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.extended_scan_support, p);
515911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      STREAM_TO_UINT16(btm_cb.cmn_ble_vsc_cb.debug_logging_supported, p);
516911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
517911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.cmn_ble_vsc_cb.values_read = true;
518911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
51916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
520911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG(
521911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      "%s: stat=%d, irk=%d, ADV ins:%d, rpa=%d, ener=%d, ext_scan=%d", __func__,
522911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status, btm_cb.cmn_ble_vsc_cb.max_irk_list_sz,
523911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_cb.cmn_ble_vsc_cb.adv_inst_max, btm_cb.cmn_ble_vsc_cb.rpa_offloading,
524911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_cb.cmn_ble_vsc_cb.energy_support,
525911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_cb.cmn_ble_vsc_cb.extended_scan_support);
52616fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
5272255250d9bb5c8c1354fc2b4685523d028dcf6f9Jakub Pawlowski  btm_ble_adv_init();
528911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
529911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.cmn_ble_vsc_cb.max_filter > 0) btm_ble_adv_filter_init();
530d5aa24750c2202ce944560c2c27ead0214e8350dSatya Calloji
531d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
532911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* VS capability included and non-4.2 device */
533911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.cmn_ble_vsc_cb.max_irk_list_sz > 0 &&
534911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      controller_get_interface()->get_ble_resolving_list_max_size() == 0)
535911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_resolving_list_init(btm_cb.cmn_ble_vsc_cb.max_irk_list_sz);
536911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#endif /* (BLE_PRIVACY_SPT == TRUE) */
53716fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
538911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_init();
5398d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta
540911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_ctrl_le_feature_rd_cmpl_cback != NULL)
541911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_ctrl_le_feature_rd_cmpl_cback(status);
542935324abbe8706307297f09cf79bbcd4efb73367Satya Calloji}
543911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#endif /* (BLE_VND_INCLUDED == TRUE) */
544a6ce7751d84218c193eb90d390aef23217b1737eWei Wang
545a6ce7751d84218c193eb90d390aef23217b1737eWei Wang/*******************************************************************************
546ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
547ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleGetVendorCapabilities
548ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
549ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function reads local LE features
550ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
551ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       p_cmn_vsc_cb : Locala LE capability structure
552ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
553ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
554ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
555ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
556911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonextern void BTM_BleGetVendorCapabilities(tBTM_BLE_VSC_CB* p_cmn_vsc_cb) {
557911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("BTM_BleGetVendorCapabilities");
5588d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta
559911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (NULL != p_cmn_vsc_cb) {
560911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p_cmn_vsc_cb = btm_cb.cmn_ble_vsc_cb;
561911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
5628d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta}
5638d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta
5648d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta/******************************************************************************
565ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
566ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleReadControllerFeatures
567ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
568ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Reads BLE specific controller features
569ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
5709ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Parameters:      tBTM_BLE_CTRL_FEATURES_CBACK : Callback to notify when
5719ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  features are read
572ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
573ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
574ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
575ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
576d628a0695e73973e0bd54dd026bfed900b69f804Myles Watson#if (BLE_VND_INCLUDED == TRUE)
577911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonextern void BTM_BleReadControllerFeatures(
578911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {
579911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (true == btm_cb.cmn_ble_vsc_cb.values_read) return;
5803f24f460ec3efedde25723e6a4297efc10873cd2Satya Calloji
581911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("BTM_BleReadControllerFeatures");
5828d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta
583911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_ctrl_le_feature_rd_cmpl_cback = p_vsc_cback;
584911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_VendorSpecificCommand(HCI_BLE_VENDOR_CAP_OCF, 0, NULL,
585911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                            btm_ble_vendor_capability_vsc_cmpl_cback);
586d628a0695e73973e0bd54dd026bfed900b69f804Myles Watson}
587c4e2596a112a0dd63ac681b1226548c0f95e828cSatya Calloji#else
588911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonextern void BTM_BleReadControllerFeatures(
589911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    UNUSED_ATTR tBTM_BLE_CTRL_FEATURES_CBACK* p_vsc_cback) {}
590d628a0695e73973e0bd54dd026bfed900b69f804Myles Watson#endif
591a6ce7751d84218c193eb90d390aef23217b1737eWei Wang
592a6ce7751d84218c193eb90d390aef23217b1737eWei Wang/*******************************************************************************
593ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
594ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleEnableMixedPrivacyMode
595ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
596ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to enabled Mixed mode if privacy 1.2
597ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  is applicable in controller.
598ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
599ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       mixed_on:  mixed mode to be used or not.
600ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
601ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
602ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
603ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
604911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid BTM_BleEnableMixedPrivacyMode(bool mixed_on) {
605d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
606911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.ble_ctr_cb.mixed_mode = mixed_on;
607444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
608911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson/* TODO: send VSC to enabled mixed mode */
6095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project#endif
6105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
6115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
6127fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
613ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
614ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleConfigPrivacy
615ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
616ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to enable or disable the privacy in
617ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                   LE channel of the local device.
618ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
619ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       privacy_mode:  privacy mode on or off.
620ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
621ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          bool    privacy mode set success; otherwise failed.
622ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
623ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
624911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonbool BTM_BleConfigPrivacy(bool privacy_mode) {
625d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
626911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
627911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
628911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s", __func__);
629911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
630911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* if LE is not supported, return error */
631911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return false;
632911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
633911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t addr_resolution = 0;
634911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!privacy_mode) /* if privacy disabled, always use public address */
635911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  {
636911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_PUBLIC;
637911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->privacy_mode = BTM_PRIVACY_NONE;
638911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else /* privacy is turned on*/
639911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  {
640911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* always set host random address, used when privacy 1.1 or priavcy 1.2 is
641911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * disabled */
642911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->addr_mgnt_cb.own_addr_type = BLE_ADDR_RANDOM;
6430c683232e28ea616844bfa03d876a08df217ba13Jakub Pawlowski    btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
644911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
645911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* 4.2 controller only allow privacy 1.2 or mixed mode, resolvable private
646911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * address in controller */
647911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (controller_get_interface()->supports_ble_privacy()) {
648911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      addr_resolution = 1;
649911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* check vendor specific capability */
650911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->privacy_mode =
651911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          btm_cb.ble_ctr_cb.mixed_mode ? BTM_PRIVACY_MIXED : BTM_PRIVACY_1_2;
652911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else /* 4.1/4.0 controller */
653911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->privacy_mode = BTM_PRIVACY_1_1;
654911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
655911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
656911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  GAP_BleAttrDBUpdate(GATT_UUID_GAP_CENTRAL_ADDR_RESOL,
657911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                      (tGAP_BLE_ATTR_VALUE*)&addr_resolution);
658911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
659911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return true;
660444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji#else
661911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return false;
662444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji#endif
6637fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
6647fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
6658d41691e6b16ba945711e8c3846df1ec70fdb724Ganesh Ganapathi Batta/*******************************************************************************
666ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
667ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function          BTM_BleMaxMultiAdvInstanceCount
668ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
6699ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description        Returns max number of multi adv instances supported by
6709ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  controller
671ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
672ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          Max multi adv instance count
673ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
674ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
675911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonextern uint8_t BTM_BleMaxMultiAdvInstanceCount(void) {
676911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return btm_cb.cmn_ble_vsc_cb.adv_inst_max < BTM_BLE_MULTI_ADV_MAX
677911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson             ? btm_cb.cmn_ble_vsc_cb.adv_inst_max
678911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson             : BTM_BLE_MULTI_ADV_MAX;
67916fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham}
68016fe0822ccd3eb6f66d3afd9965d25a0a0cb4c30Prerepa Viswanadham
6815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
682ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
683ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleLocalPrivacyEnabled
684ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
685ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description        Checks if local device supports private address
686ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
687ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          Return true if local privacy is enabled else false
688ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
689ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
690911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonbool BTM_BleLocalPrivacyEnabled(void) {
691d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
692911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE);
69326112633d80098314048914c0be33442a0048fb7Sharvil Nanavati#else
694911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return false;
69526112633d80098314048914c0be33442a0048fb7Sharvil Nanavati#endif
69626112633d80098314048914c0be33442a0048fb7Sharvil Nanavati}
69726112633d80098314048914c0be33442a0048fb7Sharvil Nanavati
69883211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski/**
69983211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski * Set BLE connectable mode to auto connect
70083211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski */
70183211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowskivoid BTM_BleStartAutoConn() {
70283211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  BTM_TRACE_EVENT("%s", __func__);
70383211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  if (!controller_get_interface()->supports_ble()) return;
704911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
70583211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  if (btm_cb.ble_ctr_cb.bg_conn_type != BTM_BLE_CONN_AUTO) {
70683211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski    btm_ble_start_auto_conn(true);
70783211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski    btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_AUTO;
708911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
7095738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
7105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
7115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
712ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
713ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleClearBgConnDev
714ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
715ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to clear the whitelist,
716ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  end any pending whitelist connections,
7175e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski *                  and reset the local bg device list.
718ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
719ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       void
720ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
721ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
722ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
723ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
724911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid BTM_BleClearBgConnDev(void) {
725911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_start_auto_conn(false);
726911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_clear_white_list();
727911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  gatt_reset_bgdev_list();
728021e17a641e61e3f139717cd755bf6f9b9b9eb12Nitin Arora}
729021e17a641e61e3f139717cd755bf6f9b9b9eb12Nitin Arora
730021e17a641e61e3f139717cd755bf6f9b9b9eb12Nitin Arora/*******************************************************************************
731ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
732ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleUpdateBgConnDev
733ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
734ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to add or remove a device into/from
735ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  background connection procedure. The background connection
7365e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski *                  procedure is decided by the background connection type, it
7375e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski *                  can be auto connection, or selective connection.
738ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
739ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       add_remove: true to add; false to remove.
740ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  remote_bda: device address to add/remove.
741ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
742ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
743ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
744ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
7459e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowskibool BTM_BleUpdateBgConnDev(bool add_remove, const RawAddress& remote_bda) {
746911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s() add=%d", __func__, add_remove);
747911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return btm_update_dev_to_white_list(add_remove, remote_bda);
7485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
7495738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
7505738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
751ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
752ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleSetConnectableMode
753ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
754ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to set BLE connectable mode for a
755ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  peripheral device.
756ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
7579ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Parameters       conn_mode:  directed connectable mode, or non-directed. It
7589ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                              can be BTM_BLE_CONNECT_EVT,
7599ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                              BTM_BLE_CONNECT_DIR_EVT or
760ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                              BTM_BLE_CONNECT_LO_DUTY_DIR_EVT
761ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
762ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          BTM_ILLEGAL_VALUE if controller does not support BLE.
763ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  BTM_SUCCESS is status set successfully; otherwise failure.
764ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
765ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
766911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS BTM_BleSetConnectableMode(tBTM_BLE_CONN_MODE connectable_mode) {
767911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
7685738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
769911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s connectable_mode = %d ", __func__, connectable_mode);
770911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
7715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
772911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->directed_conn = connectable_mode;
773911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return btm_ble_set_connectability(p_cb->connectable_mode);
774ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
7755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
776d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
777911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic bool is_resolving_list_bit_set(void* data, void* context) {
778911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
779cac784d772db7f42dc93acafb18e5d71617c04f3Jakub Pawlowski
780911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) != 0)
781911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return false;
782cac784d772db7f42dc93acafb18e5d71617c04f3Jakub Pawlowski
783911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return true;
784cac784d772db7f42dc93acafb18e5d71617c04f3Jakub Pawlowski}
785a9ea43bb168d35b6bd73f995d1f87e50deaf69d4Pavlin Radoslavov#endif
786cac784d772db7f42dc93acafb18e5d71617c04f3Jakub Pawlowski
787ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
788ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
789ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_set_conn_mode_adv_init_addr
790ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
7919ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      set initator address type and local address type based on
7929ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  adv mode.
793ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
794ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
795ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
796911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic uint8_t btm_set_conn_mode_adv_init_addr(
7979e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski    tBTM_BLE_INQ_CB* p_cb, RawAddress& p_peer_addr_ptr,
798911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    tBLE_ADDR_TYPE* p_peer_addr_type, tBLE_ADDR_TYPE* p_own_addr_type) {
799911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t evt_type;
800d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
801911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_SEC_DEV_REC* p_dev_rec;
802a9ea43bb168d35b6bd73f995d1f87e50deaf69d4Pavlin Radoslavov#endif
8035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
804911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  evt_type =
805911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE)
806911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          ? ((p_cb->scan_rsp) ? BTM_BLE_DISCOVER_EVT : BTM_BLE_NON_CONNECT_EVT)
807911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          : BTM_BLE_CONNECT_EVT;
8087fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
809911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (evt_type == BTM_BLE_CONNECT_EVT) {
810911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    evt_type = p_cb->directed_conn;
811444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
812911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_cb->directed_conn == BTM_BLE_CONNECT_DIR_EVT ||
813911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        p_cb->directed_conn == BTM_BLE_CONNECT_LO_DUTY_DIR_EVT) {
814d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
815911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* for privacy 1.2, convert peer address as static, own address set as ID
816911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson       * addr */
817911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      if (btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 ||
818911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
819911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        /* only do so for bonded device */
82078b81c6db0aed32eb595ce4de7eb22fc24800058Jakub Pawlowski        if ((p_dev_rec = btm_find_or_alloc_dev(p_cb->direct_bda.bda)) != NULL &&
821911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson            p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
822911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          btm_ble_enable_resolving_list(BTM_BLE_RL_ADV);
823c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski          p_peer_addr_ptr = p_dev_rec->ble.static_addr;
824911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
825911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          *p_own_addr_type = BLE_ADDR_RANDOM_ID;
826911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          return evt_type;
827911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        }
828911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        /* otherwise fall though as normal directed adv */
829911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        else {
830911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
831444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji        }
832911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      }
833911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson#endif
834911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* direct adv mode does not have privacy, if privacy is not enabled  */
835911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      *p_peer_addr_type = p_cb->direct_bda.type;
83678b81c6db0aed32eb595ce4de7eb22fc24800058Jakub Pawlowski      p_peer_addr_ptr = p_cb->direct_bda.bda;
837911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      return evt_type;
8385738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
839911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
8407fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
841911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson/* undirect adv mode or non-connectable mode*/
842d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
843911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* when privacy 1.2 privacy only mode is used, or mixed mode */
844911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if ((btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_1_2 &&
845911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson       p_cb->afp != AP_SCAN_CONN_ALL) ||
846911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_MIXED) {
847911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    list_node_t* n =
848911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        list_foreach(btm_cb.sec_dev_rec, is_resolving_list_bit_set, NULL);
849911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (n) {
850911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* if enhanced privacy is required, set Identity address and matching IRK
851911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson       * peer */
852911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      tBTM_SEC_DEV_REC* p_dev_rec =
853911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          static_cast<tBTM_SEC_DEV_REC*>(list_node(n));
854c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski      p_peer_addr_ptr = p_dev_rec->ble.static_addr;
855911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      *p_peer_addr_type = p_dev_rec->ble.static_addr_type;
856911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
857911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      *p_own_addr_type = BLE_ADDR_RANDOM_ID;
858911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else {
859911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* resolving list is empty, not enabled */
860911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      *p_own_addr_type = BLE_ADDR_RANDOM;
861911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
862911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
863911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* privacy 1.1, or privacy 1.2, general discoverable/connectable mode, disable
864911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     privacy in */
865911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* controller fall back to host based privacy */
866911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else if (btm_cb.ble_ctr_cb.privacy_mode != BTM_PRIVACY_NONE) {
867911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p_own_addr_type = BLE_ADDR_RANDOM;
868911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
869444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji#endif
870ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
871911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* if no privacy,do not set any peer address,*/
872911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* local address type go by global privacy setting */
873911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return evt_type;
8745738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
8755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
8765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
877ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
878ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleSetAdvParams
879ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
880ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to set advertising parameters.
881ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
882ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters       adv_int_min: minimum advertising interval
883ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  adv_int_max: maximum advertising interval
884ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  p_dir_bda: connectable direct initiator's LE device address
885ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  chnl_map: advertising channel map.
886ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
887ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
888ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
889ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
890d19e0785e662e640191a075eda07acce61c2aedaMarie JanssentBTM_STATUS BTM_BleSetAdvParams(uint16_t adv_int_min, uint16_t adv_int_max,
8919e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski                                const RawAddress& p_dir_bda,
892911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                tBTM_BLE_ADV_CHNL_MAP chnl_map) {
893911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
894911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
895911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_STATUS status = BTM_SUCCESS;
896aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  RawAddress address = RawAddress::kEmpty;
897911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
898911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
899911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t adv_mode = p_cb->adv_mode;
9005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
901911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("BTM_BleSetAdvParams");
9025738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
903911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
9045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
905911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN,
906911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                             BTM_BLE_ADV_INT_MAX) ||
907911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN,
908911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                             BTM_BLE_ADV_INT_MAX)) {
909911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return BTM_ILLEGAL_VALUE;
910911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
9115738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
912911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->adv_interval_min = adv_int_min;
913911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->adv_interval_max = adv_int_max;
914911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->adv_chnl_map = chnl_map;
91578b81c6db0aed32eb595ce4de7eb22fc24800058Jakub Pawlowski  p_cb->direct_bda.bda = p_dir_bda;
9165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
917911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("update params for an active adv");
9185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
919911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_stop_adv();
9205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
921911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
922aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski      p_cb, address, &init_addr_type, &own_addr_type);
923911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
924911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* update adv params */
925911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btsnd_hcic_ble_write_adv_params(
926911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->adv_interval_min, p_cb->adv_interval_max, p_cb->evt_type,
927aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski      own_addr_type, init_addr_type, address, p_cb->adv_chnl_map, p_cb->afp);
928911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
929911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
930911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
931911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return status;
9325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
9335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
934119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski/**
935119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski * This function is called to set scan parameters. |cb| is called with operation
936119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski * status
937119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski **/
938119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowskivoid BTM_BleSetScanParams(uint32_t scan_interval, uint32_t scan_window,
939119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski                          tBLE_SCAN_MODE scan_mode,
940119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski                          base::Callback<void(uint8_t)> cb) {
941911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
942911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint32_t max_scan_interval;
943911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint32_t max_scan_window;
944911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
945911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s", __func__);
946911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return;
947911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
948911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* If not supporting extended scan support, use the older range for checking
949911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson   */
950911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.cmn_ble_vsc_cb.extended_scan_support == 0) {
951911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    max_scan_interval = BTM_BLE_SCAN_INT_MAX;
952911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    max_scan_window = BTM_BLE_SCAN_WIN_MAX;
953911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
954911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* If supporting extended scan support, use the new extended range for
955911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * checking */
956911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    max_scan_interval = BTM_BLE_EXT_SCAN_INT_MAX;
957911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    max_scan_window = BTM_BLE_EXT_SCAN_WIN_MAX;
958911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
959911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
960911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN,
961911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                            max_scan_interval) &&
962911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN,
963911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                            max_scan_window) &&
964911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      (scan_mode == BTM_BLE_SCAN_MODE_ACTI ||
965911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson       scan_mode == BTM_BLE_SCAN_MODE_PASS)) {
966911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->scan_type = scan_mode;
967911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->scan_interval = scan_interval;
968911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->scan_window = scan_window;
969911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
970119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski    cb.Run(BTM_SUCCESS);
971911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
972119f32a7e85801f15c30dd5e874dc4bff0311d54Jakub Pawlowski    cb.Run(BTM_ILLEGAL_VALUE);
973911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
974911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_ERROR("Illegal params: scan_interval = %d scan_window = %d",
975911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                    scan_interval, scan_window);
976911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
9775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
9785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
9795738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
980ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
981ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM_BleWriteScanRsp
982ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
983ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to write LE scan response.
984ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
985ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:      p_scan_rsp: scan response information.
986ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
987ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
988ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
989ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
9909eaf7768670389e0850e924e295a0c5738027e31Jakub Pawlowskivoid BTM_BleWriteScanRsp(uint8_t* data, uint8_t length,
991911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                         tBTM_BLE_ADV_DATA_CMPL_CBACK* p_adv_data_cback) {
992911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s: length: %d", __func__, length);
993911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) {
994911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_adv_data_cback(BTM_ILLEGAL_VALUE);
995911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return;
996911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
9973aa60544585b5fb0f35c1165539a4a859fc0f25cAndre Eisenbach
998911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btsnd_hcic_ble_set_scan_rsp_data(length, data);
9995738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1000911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (length != 0)
1001911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.inq_var.scan_rsp = true;
1002911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else
1003911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.inq_var.scan_rsp = false;
10045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1005911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_adv_data_cback(BTM_SUCCESS);
10065738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
10075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
10085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1009ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1010ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM__BLEReadDiscoverability
1011ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
10129ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      This function is called to read the current LE
10139ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  discoverability mode of the device.
1014ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1015ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          BTM_BLE_NON_DISCOVERABLE ,BTM_BLE_LIMITED_DISCOVERABLE or
1016ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                     BTM_BLE_GENRAL_DISCOVERABLE
1017ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1018ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1019911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonuint16_t BTM_BleReadDiscoverability() {
1020911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_API("%s", __func__);
102176356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan
1022911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return (btm_cb.ble_ctr_cb.inq_var.discoverable_mode);
102376356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan}
102476356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan
102576356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan/*******************************************************************************
1026ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1027ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         BTM__BLEReadConnectability
1028ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
10299ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      This function is called to read the current LE
10309ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  connectability mode of the device.
1031ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1032ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          BTM_BLE_NON_CONNECTABLE or BTM_BLE_CONNECTABLE
1033ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1034ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1035911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonuint16_t BTM_BleReadConnectability() {
1036911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_API("%s", __func__);
103776356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan
1038911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return (btm_cb.ble_ctr_cb.inq_var.connectable_mode);
103976356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan}
104076356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan
104176356aee883af67898ddc1aa3b628195f396d42aVenkatRaghavan VijayaRaghavan/*******************************************************************************
1042ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1043ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_select_adv_interval
1044ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1045ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      select adv interval based on device mode
1046ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1047ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1048ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1049ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1050911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_select_adv_interval(tBTM_BLE_INQ_CB* p_cb, uint8_t evt_type,
1051911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                 uint16_t* p_adv_int_min,
1052911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                 uint16_t* p_adv_int_max) {
1053911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->adv_interval_min && p_cb->adv_interval_max) {
1054911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p_adv_int_min = p_cb->adv_interval_min;
1055911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p_adv_int_max = p_cb->adv_interval_max;
1056911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
1057911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    switch (evt_type) {
1058911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      case BTM_BLE_CONNECT_EVT:
1059911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
1060911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_1;
1061911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        break;
10627fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
1063911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      case BTM_BLE_NON_CONNECT_EVT:
1064911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      case BTM_BLE_DISCOVER_EVT:
1065911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_FAST_INT_2;
1066911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        break;
1067444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
1068911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* connectable directed event */
1069911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      case BTM_BLE_CONNECT_DIR_EVT:
1070911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        *p_adv_int_min = BTM_BLE_GAP_ADV_DIR_MIN_INT;
1071911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        *p_adv_int_max = BTM_BLE_GAP_ADV_DIR_MAX_INT;
1072911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        break;
10737fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
1074911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      default:
1075911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        *p_adv_int_min = *p_adv_int_max = BTM_BLE_GAP_ADV_SLOW_INT;
1076911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        break;
10777fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    }
1078911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1079911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return;
10807fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
1081444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
10827fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
1083ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1084ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_update_dmt_flag_bits
1085ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
10869ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      Obtain updated adv flag value based on connect and
10879ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  discoverability mode. Also, setup DMT support value in the
10889ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  flag based on whether the controller supports both LE and
10899ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  BR/EDR.
1090ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1091ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:      flag_value (Input / Output) - flag value
1092ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  connect_mode (Input) - Connect mode value
1093ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  disc_mode (Input) - discoverability mode
1094ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1095ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1096ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1097ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1098911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_update_dmt_flag_bits(uint8_t* adv_flag_value,
1099911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                  const uint16_t connect_mode,
1100911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                  const uint16_t disc_mode) {
1101911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* BR/EDR non-discoverable , non-connectable */
1102911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if ((disc_mode & BTM_DISCOVERABLE_MASK) == 0 &&
1103911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      (connect_mode & BTM_CONNECTABLE_MASK) == 0)
1104911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *adv_flag_value |= BTM_BLE_BREDR_NOT_SPT;
1105911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else
1106911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *adv_flag_value &= ~BTM_BLE_BREDR_NOT_SPT;
1107911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1108911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* if local controller support, mark both controller and host support in flag
1109911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson   */
1110911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (controller_get_interface()->supports_simultaneous_le_bredr())
1111911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *adv_flag_value |= (BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
1112911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else
1113911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *adv_flag_value &= ~(BTM_BLE_DMT_CONTROLLER_SPT | BTM_BLE_DMT_HOST_SPT);
11147cf7da925d73600f108be6efcf9a542f14c93063Satya Calloji}
11157cf7da925d73600f108be6efcf9a542f14c93063Satya Calloji
11167cf7da925d73600f108be6efcf9a542f14c93063Satya Calloji/*******************************************************************************
1117ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1118ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_set_adv_flag
1119ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1120ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Set adv flag in adv data.
1121ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1122ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:      connect_mode (Input)- Connect mode value
1123ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  disc_mode (Input) - discoverability mode
1124ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1125ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1126ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1127ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1128911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_set_adv_flag(uint16_t connect_mode, uint16_t disc_mode) {
1129911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t flag = 0, old_flag = 0;
1130911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1131911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1132911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_adv_data->p_flags != NULL) flag = old_flag = *(p_adv_data->p_flags);
1133911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1134911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_update_dmt_flag_bits(&flag, connect_mode, disc_mode);
1135911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1136911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  LOG_DEBUG(LOG_TAG, "disc_mode %04x", disc_mode);
1137911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* update discoverable flag */
1138911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (disc_mode & BTM_BLE_LIMITED_DISCOVERABLE) {
1139911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    flag &= ~BTM_BLE_GEN_DISC_FLAG;
1140911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    flag |= BTM_BLE_LIMIT_DISC_FLAG;
1141911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else if (disc_mode & BTM_BLE_GENERAL_DISCOVERABLE) {
1142911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    flag |= BTM_BLE_GEN_DISC_FLAG;
1143911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    flag &= ~BTM_BLE_LIMIT_DISC_FLAG;
1144911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else /* remove all discoverable flags */
1145911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  {
1146911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    flag &= ~(BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG);
1147911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1148911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1149911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (flag != old_flag) {
1150911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_update_adv_flag(flag);
1151911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
11527fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
11535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1154ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1155ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_set_discoverability
1156ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1157ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to set BLE discoverable mode.
1158ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1159ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:      combined_mode: discoverability mode.
1160ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1161ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          BTM_SUCCESS is status set successfully; otherwise failure.
1162ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1163ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1164911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS btm_ble_set_discoverability(uint16_t combined_mode) {
1165911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1166911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
1167911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t mode = (combined_mode & BTM_BLE_DISCOVERABLE_MASK);
1168911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t new_mode = BTM_BLE_ADV_ENABLE;
1169911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t evt_type;
1170911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_STATUS status = BTM_SUCCESS;
1171aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  RawAddress address = RawAddress::kEmpty;
1172911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC,
1173911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                 own_addr_type = p_addr_cb->own_addr_type;
1174911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t adv_int_min, adv_int_max;
11755738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1176911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
1177911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  combined_mode);
11785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1179911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /*** Check mode parameter ***/
1180911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (mode > BTM_BLE_MAX_DISCOVERABLE) return (BTM_ILLEGAL_VALUE);
11815738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1182911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->discoverable_mode = mode;
11835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1184aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  evt_type = btm_set_conn_mode_adv_init_addr(p_cb, address, &init_addr_type,
1185911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                             &own_addr_type);
11865738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1187911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->connectable_mode == BTM_BLE_NON_CONNECTABLE &&
1188911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      mode == BTM_BLE_NON_DISCOVERABLE)
1189911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    new_mode = BTM_BLE_ADV_DISABLE;
11905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1191911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
11925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1193911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_cancel(p_cb->fast_adv_timer);
11945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1195911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* update adv params if start advertising */
1196911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("evt_type=0x%x p-cb->evt_type=0x%x ", evt_type,
1197911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  p_cb->evt_type);
11987fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
1199911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (new_mode == BTM_BLE_ADV_ENABLE) {
1200911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode, combined_mode);
1201ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
1202911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (evt_type != p_cb->evt_type || p_cb->adv_addr_type != own_addr_type ||
1203911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        !p_cb->fast_adv_on) {
1204911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_ble_stop_adv();
120570b9598970e5b18ba53c282319e68faea6eb5681Satya Calloji
1206911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* update adv params */
1207911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
1208aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski                                      own_addr_type, init_addr_type, address,
1209911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                      p_cb->adv_chnl_map, p_cb->afp);
1210911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->evt_type = evt_type;
1211911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->adv_addr_type = own_addr_type;
12127fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    }
1213911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1214911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1215911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (status == BTM_SUCCESS && p_cb->adv_mode != new_mode) {
1216911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (new_mode == BTM_BLE_ADV_ENABLE)
1217911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status = btm_ble_start_adv();
121870b9598970e5b18ba53c282319e68faea6eb5681Satya Calloji    else
1219911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status = btm_ble_stop_adv();
1220911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1221911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1222911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
1223911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->fast_adv_on = true;
1224911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* start initial GAP mode adv timer */
122547616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski    alarm_set_on_mloop(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
122647616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski                       btm_ble_fast_adv_timer_timeout, NULL);
1227911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
1228d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
1229911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
1230a021a12987c4257d137437c6de9438b3344c6f6bAndre Eisenbach#endif
1231911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
12325738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1233911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* set up stop advertising timer */
1234911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (status == BTM_SUCCESS && mode == BTM_BLE_LIMITED_DISCOVERABLE) {
1235911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_EVENT("start timer for limited disc mode duration=%d ms",
1236911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                    BTM_BLE_GAP_LIM_TIMEOUT_MS);
1237911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* start Tgap(lim_timeout) */
123847616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski    alarm_set_on_mloop(p_cb->inquiry_timer, BTM_BLE_GAP_LIM_TIMEOUT_MS,
1239911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                       btm_ble_inquiry_timer_gap_limited_discovery_timeout,
124047616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski                       NULL);
1241911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1242911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return status;
12435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
12445738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
12455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1246ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1247ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_set_connectability
1248ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1249ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to set BLE connectability mode.
1250ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1251ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:      combined_mode: connectability mode.
1252ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1253ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          BTM_SUCCESS is status set successfully; otherwise failure.
1254ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1255ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1256911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS btm_ble_set_connectability(uint16_t combined_mode) {
1257911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
1258911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
1259911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t mode = (combined_mode & BTM_BLE_CONNECTABLE_MASK);
1260911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t new_mode = BTM_BLE_ADV_ENABLE;
1261911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t evt_type;
1262911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_STATUS status = BTM_SUCCESS;
1263aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  RawAddress address = RawAddress::kEmpty;
1264911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBLE_ADDR_TYPE peer_addr_type = BLE_ADDR_PUBLIC,
1265911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                 own_addr_type = p_addr_cb->own_addr_type;
1266911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t adv_int_min, adv_int_max;
1267911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1268911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("%s mode=0x%0x combined_mode=0x%x", __func__, mode,
1269911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  combined_mode);
1270911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1271911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /*** Check mode parameter ***/
1272911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (mode > BTM_BLE_MAX_CONNECTABLE) return (BTM_ILLEGAL_VALUE);
1273911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1274911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->connectable_mode = mode;
1275911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1276aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  evt_type = btm_set_conn_mode_adv_init_addr(p_cb, address, &peer_addr_type,
1277911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                             &own_addr_type);
1278911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1279911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (mode == BTM_BLE_NON_CONNECTABLE &&
1280911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->discoverable_mode == BTM_BLE_NON_DISCOVERABLE)
1281911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    new_mode = BTM_BLE_ADV_DISABLE;
1282911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1283911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_select_adv_interval(p_cb, evt_type, &adv_int_min, &adv_int_max);
1284911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1285911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_cancel(p_cb->fast_adv_timer);
1286911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* update adv params if needed */
1287911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (new_mode == BTM_BLE_ADV_ENABLE) {
1288911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_adv_flag(combined_mode, btm_cb.btm_inq_vars.discoverable_mode);
1289911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_cb->evt_type != evt_type ||
1290911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        p_cb->adv_addr_type != p_addr_cb->own_addr_type || !p_cb->fast_adv_on) {
1291911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_ble_stop_adv();
1292911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1293911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btsnd_hcic_ble_write_adv_params(adv_int_min, adv_int_max, evt_type,
1294aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski                                      own_addr_type, peer_addr_type, address,
1295911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                      p_cb->adv_chnl_map, p_cb->afp);
1296911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->evt_type = evt_type;
1297911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->adv_addr_type = own_addr_type;
1298911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
1299911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1300911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1301911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* update advertising mode */
1302911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (status == BTM_SUCCESS && new_mode != p_cb->adv_mode) {
1303444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji    if (new_mode == BTM_BLE_ADV_ENABLE)
1304911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status = btm_ble_start_adv();
130570b9598970e5b18ba53c282319e68faea6eb5681Satya Calloji    else
1306911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status = btm_ble_stop_adv();
1307911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1308911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1309911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
1310911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->fast_adv_on = true;
1311911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* start initial GAP mode adv timer */
131247616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski    alarm_set_on_mloop(p_cb->fast_adv_timer, BTM_BLE_GAP_FAST_ADV_TIMEOUT_MS,
131347616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski                       btm_ble_fast_adv_timer_timeout, NULL);
1314911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
1315d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
1316911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, true);
1317a021a12987c4257d137437c6de9438b3344c6f6bAndre Eisenbach#endif
1318911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1319911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return status;
13205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
13215738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
13229df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowskivoid btm_send_hci_scan_enable(uint8_t enable, uint8_t filter_duplicates) {
13239df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  if (controller_get_interface()->supports_ble_extended_advertising()) {
13249df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btsnd_hcic_ble_set_extended_scan_enable(enable, filter_duplicates, 0x0000,
13259df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski                                            0x0000);
13269df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  } else {
13279df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btsnd_hcic_ble_set_scan_enable(enable, filter_duplicates);
13289df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  }
13299df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski}
13309df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski
13319df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowskivoid btm_send_hci_set_scan_params(uint8_t scan_type, uint16_t scan_int,
13329df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski                                  uint16_t scan_win, uint8_t addr_type_own,
13339df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski                                  uint8_t scan_filter_policy) {
13349df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  if (controller_get_interface()->supports_ble_extended_advertising()) {
13359df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    scanning_phy_cfg phy_cfg;
13369df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    phy_cfg.scan_type = scan_type;
13379df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    phy_cfg.scan_int = scan_int;
13389df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    phy_cfg.scan_win = scan_win;
13399df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski
13409df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btsnd_hcic_ble_set_extended_scan_params(addr_type_own, scan_filter_policy,
13419df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski                                            1, &phy_cfg);
13429df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  } else {
13439df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btsnd_hcic_ble_set_scan_params(scan_type, scan_int, scan_win, addr_type_own,
13449df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski                                   scan_filter_policy);
13459df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  }
13469df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski}
13479df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski
13485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1349ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1350ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_start_inquiry
1351ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1352ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to start BLE inquiry procedure.
13539ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  If the duration is zero, the periodic inquiry mode is
13549ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  cancelled.
1355ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1356ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:      mode - GENERAL or LIMITED inquiry
1357ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  p_inq_params - pointer to the BLE inquiry parameter.
13589ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  p_results_cb - callback returning pointer to results
13599ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                                 (tBTM_INQ_RESULTS)
1360ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  p_cmpl_cb - callback indicating the end of an inquiry
1361ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1362ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1363ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1364ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          BTM_CMD_STARTED if successfully started
1365ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  BTM_NO_RESOURCES if could not allocate a message buffer
1366ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  BTM_BUSY - if an inquiry is already active
1367ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1368ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1369911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS btm_ble_start_inquiry(uint8_t mode, uint8_t duration) {
1370911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_STATUS status = BTM_CMD_STARTED;
1371911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
1372911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
1373911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1374911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("btm_ble_start_inquiry: mode = %02x inq_active = 0x%02x",
1375911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  mode, btm_cb.btm_inq_vars.inq_active);
1376911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1377911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* if selective connection is active, or inquiry is already active, reject it
1378911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson   */
137983211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  if (BTM_BLE_IS_INQ_ACTIVE(p_ble_cb->scan_activity)) {
1380911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_ERROR("LE Inquiry is active, can not start inquiry");
1381911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return (BTM_BUSY);
1382911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1383911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1384911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
13859df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btm_send_hci_set_scan_params(
1386911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
1387911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_BLE_LOW_LATENCY_SCAN_WIN,
1388911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
1389d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
1390911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* enable IRK list */
1391911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_SCAN);
1392ed534e330bf63c72e778fc8daae09ddd63c395b2Wei Wang#endif
1393911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_ble_cb->inq_var.scan_duplicate_filter = BTM_BLE_DUPLICATE_DISABLE;
1394911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    status = btm_ble_start_scan();
1395911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else if ((p_ble_cb->inq_var.scan_interval !=
1396911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson              BTM_BLE_LOW_LATENCY_SCAN_INT) ||
1397911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson             (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
1398911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("%s, restart LE scan with low latency scan params",
1399911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                    __func__);
14009df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
14019df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btm_send_hci_set_scan_params(
1402911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_BLE_SCAN_MODE_ACTI, BTM_BLE_LOW_LATENCY_SCAN_INT,
1403911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_BLE_LOW_LATENCY_SCAN_WIN,
1404911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, SP_ADV_ALL);
14059df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski    btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, BTM_BLE_DUPLICATE_DISABLE);
1406911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1407911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1408911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (status == BTM_CMD_STARTED) {
1409911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_inq->inq_active |= mode;
1410911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_ble_cb->scan_activity |= mode;
1411911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1412911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("btm_ble_start_inquiry inq_active = 0x%02x",
1413911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                    p_inq->inq_active);
1414911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1415911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (duration != 0) {
1416911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* start inquiry timer */
1417911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      period_ms_t duration_ms = duration * 1000;
141847616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski      alarm_set_on_mloop(p_ble_cb->inq_var.inquiry_timer, duration_ms,
141947616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski                         btm_ble_inquiry_timer_timeout, NULL);
1420911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
1421911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1422911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1423911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return status;
14245738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
14255738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
14265738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1427ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1428ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_read_remote_name_cmpl
1429ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1430ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called when BLE remote name is received.
1431ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1432ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1433ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1434ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
14359e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowskivoid btm_ble_read_remote_name_cmpl(bool status, const RawAddress& bda,
1436c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski                                   uint16_t length, char* p_name) {
1437911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t hci_status = HCI_SUCCESS;
1438911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BD_NAME bd_name;
14395738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1440911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  memset(bd_name, 0, (BD_NAME_LEN + 1));
1441911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (length > BD_NAME_LEN) {
1442911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    length = BD_NAME_LEN;
1443911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1444911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  memcpy((uint8_t*)bd_name, p_name, length);
14455738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1446911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if ((!status) || (length == 0)) {
1447911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    hci_status = HCI_ERR_HOST_TIMEOUT;
1448911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1449911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1450c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski  btm_process_remote_name(&bda, bd_name, length + 1, hci_status);
1451c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski  btm_sec_rmt_name_request_complete(&bda, (uint8_t*)p_name, hci_status);
14525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
14535738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
14545738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1455ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1456ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_read_remote_name
1457ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1458ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function read remote LE device name using GATT read
1459ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  procedure.
1460ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1461ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:       None.
1462ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1463ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1464ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1465ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
14669e030fde05352ec4385d7baf6cc2af89e95e039cJakub PawlowskitBTM_STATUS btm_ble_read_remote_name(const RawAddress& remote_bda,
1467c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski                                     tBTM_CMPL_CB* p_cb) {
1468911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
14695738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1470911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!controller_get_interface()->supports_ble()) return BTM_ERR_PROCESSING;
14715738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
14728bf2285959ffb8960f1bf01499c15cfa1e2ccf16Jack He  tINQ_DB_ENT* p_i = btm_inq_db_find(remote_bda);
14738bf2285959ffb8960f1bf01499c15cfa1e2ccf16Jack He  if (p_i && !ble_evt_type_is_connectable(p_i->inq_info.results.ble_evt_type)) {
1474911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("name request to non-connectable device failed.");
1475911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return BTM_ERR_PROCESSING;
1476911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
14775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1478911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* read remote device name using GATT procedure */
1479911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_inq->remname_active) return BTM_BUSY;
14805738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1481911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!GAP_BleReadPeerDevName(remote_bda, btm_ble_read_remote_name_cmpl))
1482911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return BTM_BUSY;
14835738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1484911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_inq->p_remname_cmpl_cb = p_cb;
1485911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_inq->remname_active = true;
1486c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski  p_inq->remname_bda = remote_bda;
1487911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
148847616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski  alarm_set_on_mloop(p_inq->remote_name_timer, BTM_EXT_BLE_RMT_NAME_TIMEOUT_MS,
148947616530ceea2ea6432ffb35cd8a3fc0a56365b5Jakub Pawlowski                     btm_inq_remote_name_timer_timeout, NULL);
14905738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1491911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return BTM_CMD_STARTED;
14925738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
14935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
14945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1495ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1496ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_cancel_remote_name
1497ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1498ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function cancel read remote LE device name.
1499ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1500ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:       None.
1501ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1502ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1503ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1504ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
15059e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowskibool btm_ble_cancel_remote_name(const RawAddress& remote_bda) {
1506911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
1507911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  bool status;
15085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1509911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  status = GAP_BleCancelReadPeerDevName(remote_bda);
15105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1511911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_inq->remname_active = false;
1512aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski  p_inq->remname_bda = RawAddress::kEmpty;
1513911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_cancel(p_inq->remote_name_timer);
15145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1515911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return status;
15165738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
15175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
15185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1519ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1520ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_update_adv_flag
1521ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
15229ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      This function update the limited discoverable flag in the
15239ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  adv data.
1524ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1525ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Parameters:       None.
1526ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1527ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1528ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1529ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1530911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_update_adv_flag(uint8_t flag) {
1531911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_LOCAL_ADV_DATA* p_adv_data = &btm_cb.ble_ctr_cb.inq_var.adv_data;
1532911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t* p;
15335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1534911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("btm_ble_update_adv_flag new=0x%x", flag);
15355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1536911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_adv_data->p_flags != NULL) {
1537911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("btm_ble_update_adv_flag old=0x%x", *p_adv_data->p_flags);
1538911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p_adv_data->p_flags = flag;
1539911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else /* no FLAGS in ADV data*/
1540911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  {
1541911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p = (p_adv_data->p_pad == NULL) ? p_adv_data->ad_data : p_adv_data->p_pad;
1542911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* need 3 bytes space to stuff in the flags, if not */
1543911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* erase all written data, just for flags */
1544911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if ((BTM_BLE_AD_DATA_LEN - (p - p_adv_data->ad_data)) < 3) {
1545911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p = p_adv_data->p_pad = p_adv_data->ad_data;
1546911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      memset(p_adv_data->ad_data, 0, BTM_BLE_AD_DATA_LEN);
15475738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
15485738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1549911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p++ = 2;
1550911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p++ = BTM_BLE_AD_TYPE_FLAG;
1551911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_adv_data->p_flags = p;
1552911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    *p++ = flag;
1553911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_adv_data->p_pad = p;
1554911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
15555738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1556911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btsnd_hcic_ble_set_adv_data(
1557911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      (uint8_t)(p_adv_data->p_pad - p_adv_data->ad_data), p_adv_data->ad_data);
1558911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_adv_data->data_mask |= BTM_BLE_AD_BIT_FLAGS;
15595738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
15605738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1561d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski/**
1562d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski * Check ADV flag to make sure device is discoverable and match the search
1563d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski * condition
1564d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski */
15659e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowskiuint8_t btm_ble_is_discoverable(const RawAddress& bda,
15660595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                                std::vector<uint8_t> const& adv_data) {
15670595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  uint8_t flag = 0, rt = 0;
1568911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t data_len;
1569911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQ_PARMS* p_cond = &btm_cb.btm_inq_vars.inqparms;
1570911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1571911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* for observer, always "discoverable */
1572911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity))
1573911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    rt |= BTM_BLE_OBS_RESULT;
1574911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1575911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* does not match filter condition */
1576911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cond->filter_cond_type == BTM_FILTER_COND_BD_ADDR &&
1577c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski      bda != p_cond->filter_cond.bdaddr_cond) {
1578911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("BD ADDR does not meet filter condition");
15797fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    return rt;
1580911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1581911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
15820595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  if (!adv_data.empty()) {
1583103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski    const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType(
1584103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski        adv_data, BTM_BLE_AD_TYPE_FLAG, &data_len);
1585911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_flag != NULL) {
1586911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      flag = *p_flag;
1587911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1588911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&
1589911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          (flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) {
1590911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_TRACE_DEBUG("Find Generable Discoverable device");
1591911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        rt |= BTM_BLE_INQ_RESULT;
1592911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      }
1593911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1594911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      else if (btm_cb.btm_inq_vars.inq_active & BTM_BLE_LIMITED_INQUIRY &&
1595911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson               (flag & BTM_BLE_LIMIT_DISC_FLAG) != 0) {
1596911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        BTM_TRACE_DEBUG("Find limited discoverable device");
1597911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        rt |= BTM_BLE_INQ_RESULT;
1598911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      }
1599911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
1600911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1601911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return rt;
1602911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson}
1603911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1604911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_appearance_to_cod(uint16_t appearance, uint8_t* dev_class) {
1605911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  dev_class[0] = 0;
1606911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1607911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  switch (appearance) {
1608911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_PHONE:
1609911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PHONE;
1610911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1611911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1612911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_COMPUTER:
1613911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_COMPUTER;
1614911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1615911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1616911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_REMOTE:
1617911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1618911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_REMOTE_CONTROL;
1619911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1620911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_THERMOMETER:
1621911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_THERMOMETER_EAR:
1622911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1623911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_THERMOMETER;
1624911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1625911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_HEART_RATE:
1626911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HEART_RATE_BELT:
1627911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1628911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_HEART_PULSE_MONITOR;
1629911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1630911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_BLOOD_PRESSURE:
1631911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_ARM:
1632911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_BLOOD_PRESSURE_WRIST:
1633911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1634911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_BLOOD_MONITOR;
1635911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1636911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_PULSE_OXIMETER:
1637911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_PULSE_OXIMETER_FINGERTIP:
1638911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_PULSE_OXIMETER_WRIST:
1639911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1640911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_PULSE_OXIMETER;
1641911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1642911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_GLUCOSE:
1643911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1644911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_GLUCOSE_METER;
1645911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1646911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_WEIGHT:
1647911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1648911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_WEIGHING_SCALE;
1649911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1650911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_WALKING:
1651911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_WALKING_IN_SHOE:
1652911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_WALKING_ON_SHOE:
1653911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_WALKING_ON_HIP:
1654911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_HEALTH;
1655911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_STEP_COUNTER;
1656911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1657911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_WATCH:
1658911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_SPORTS_WATCH:
1659911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_WEARABLE;
1660911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_WRIST_WATCH;
1661911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1662911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_EYEGLASSES:
1663911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_WEARABLE;
1664911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_GLASSES;
1665911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1666911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_DISPLAY:
1667911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_IMAGING;
1668911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_DISPLAY;
1669911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1670911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_MEDIA_PLAYER:
1671911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_AUDIO;
1672911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1673911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1674911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_BARCODE_SCANNER:
1675911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_BARCODE_SCANNER:
1676911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_HID:
1677911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1678911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1679911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1680911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_KEYBOARD:
1681911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1682911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_KEYBOARD;
1683911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1684911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_MOUSE:
1685911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1686911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_POINTING;
1687911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1688911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_JOYSTICK:
1689911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1690911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_JOYSTICK;
1691911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1692911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_GAMEPAD:
1693911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1694911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_GAMEPAD;
1695911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1696911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_DIGITIZER_TABLET:
1697911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1698911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_DIGITIZING_TABLET;
1699911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1700911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_CARD_READER:
1701911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1702911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_CARD_READER;
1703911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1704911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_HID_DIGITAL_PEN:
1705911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1706911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_DIGITAL_PAN;
1707911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
1708911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_UKNOWN:
1709911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_CLOCK:
1710911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_TAG:
1711911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_KEYRING:
1712911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_CYCLING:
1713911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_CYCLING_COMPUTER:
1714911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_CYCLING_SPEED:
1715911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_CYCLING_CADENCE:
1716911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_CYCLING_POWER:
1717911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_CYCLING_SPEED_CADENCE:
1718911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_GENERIC_OUTDOOR_SPORTS:
1719911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION:
1720911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_AND_NAV:
1721911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD:
1722911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_APPEARANCE_OUTDOOR_SPORTS_LOCATION_POD_AND_NAV:
1723911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    default:
1724911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
1725911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      dev_class[2] = BTM_COD_MINOR_UNCLASSIFIED;
1726911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  };
1727e6c7450a2addbb856e68b66a726b71286f809b19Adam Hampson}
1728e6c7450a2addbb856e68b66a726b71286f809b19Adam Hampson
1729d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski/**
1730d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski * Update adv packet information into inquiry result.
1731d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski */
1732c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowskivoid btm_ble_update_inq_result(tINQ_DB_ENT* p_i, uint8_t addr_type,
17339e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski                               const RawAddress& bda, uint16_t evt_type,
1734c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski                               uint8_t primary_phy, uint8_t secondary_phy,
1735c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski                               uint8_t advertising_sid, int8_t tx_power,
1736c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski                               int8_t rssi, uint16_t periodic_adv_int,
17370595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                               std::vector<uint8_t> const& data) {
1738911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQ_RESULTS* p_cur = &p_i->inq_info.results;
1739911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t len;
1740911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
1741911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1742911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* Save the info */
1743911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cur->inq_result_type = BTM_INQ_RESULT_BLE;
1744911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cur->ble_addr_type = addr_type;
1745911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cur->rssi = rssi;
17467de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski  p_cur->ble_primary_phy = primary_phy;
17477de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski  p_cur->ble_secondary_phy = secondary_phy;
17487de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski  p_cur->ble_advertising_sid = advertising_sid;
17497de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski  p_cur->ble_tx_power = tx_power;
17507de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski  p_cur->ble_periodic_adv_int = periodic_adv_int;
1751911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1752d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI &&
1753d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      ble_evt_type_is_scannable(evt_type) &&
1754d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      !ble_evt_type_is_scan_resp(evt_type)) {
1755911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_i->scan_rsp = false;
1756911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else
1757911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_i->scan_rsp = true;
1758911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1759911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_i->inq_count != p_inq->inq_counter)
1760911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cur->device_type = BT_DEVICE_TYPE_BLE;
1761911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else
1762911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cur->device_type |= BT_DEVICE_TYPE_BLE;
1763911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1764911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (evt_type != BTM_BLE_SCAN_RSP_EVT) p_cur->ble_evt_type = evt_type;
1765911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1766911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_i->inq_count = p_inq->inq_counter; /* Mark entry for current inquiry */
1767911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
17680595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  if (!data.empty()) {
1769103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski    const uint8_t* p_flag =
1770103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski        AdvertiseDataParser::GetFieldByType(data, BTM_BLE_AD_TYPE_FLAG, &len);
1771911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_flag != NULL) p_cur->flag = *p_flag;
1772911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1773911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
17740595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  if (!data.empty()) {
1775911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* Check to see the BLE device has the Appearance UUID in the advertising
1776911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * data.  If it does
1777911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * then try to convert the appearance value to a class of device value
1778911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * Bluedroid can use.
1779911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * Otherwise fall back to trying to infer if it is a HID device based on the
1780911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * service class.
1781911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     */
1782103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski    const uint8_t* p_uuid16 = AdvertiseDataParser::GetFieldByType(
1783103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski        data, BTM_BLE_AD_TYPE_APPEARANCE, &len);
1784911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_uuid16 && len == 2) {
1785911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_ble_appearance_to_cod((uint16_t)p_uuid16[0] | (p_uuid16[1] << 8),
1786911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                p_cur->dev_class);
1787911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else {
1788103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski      p_uuid16 = AdvertiseDataParser::GetFieldByType(
1789103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski          data, BTM_BLE_AD_TYPE_16SRV_CMPL, &len);
1790911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      if (p_uuid16 != NULL) {
1791911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        uint8_t i;
1792911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        for (i = 0; i + 2 <= len; i = i + 2) {
1793911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          /* if this BLE device support HID over LE, set HID Major in class of
1794911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson           * device */
1795911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          if ((p_uuid16[i] | (p_uuid16[i + 1] << 8)) == UUID_SERVCLASS_LE_HID) {
1796911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson            p_cur->dev_class[0] = 0;
1797911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson            p_cur->dev_class[1] = BTM_COD_MAJOR_PERIPHERAL;
1798911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson            p_cur->dev_class[2] = 0;
1799911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson            break;
1800911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          }
18011237ee3c4bf3c8b65c2e83409db76669b1f1dcfdZhihai Xu        }
1802911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      }
18031237ee3c4bf3c8b65c2e83409db76669b1f1dcfdZhihai Xu    }
1804911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
18051237ee3c4bf3c8b65c2e83409db76669b1f1dcfdZhihai Xu
1806911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* if BR/EDR not supported is not set, assume is a DUMO device */
1807911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if ((p_cur->flag & BTM_BLE_BREDR_NOT_SPT) == 0 &&
1808d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      !ble_evt_type_is_directed(evt_type)) {
1809911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_cur->ble_addr_type != BLE_ADDR_RANDOM) {
1810911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      BTM_TRACE_DEBUG("BR/EDR NOT support bit not set, treat as DUMO");
1811911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cur->device_type |= BT_DEVICE_TYPE_DUMO;
1812911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else {
1813911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      BTM_TRACE_DEBUG("Random address, treating device as LE only");
18145738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project    }
1815911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else {
1816911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("BR/EDR NOT SUPPORT bit set, LE only device");
1817911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
18185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
18195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
18205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
1821ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1822ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_clear_all_pending_le_entry
1823ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1824ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called to clear all LE pending entry in
1825ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *                  inquiry database.
1826ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1827ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
1828ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
1829ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
1830911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_clear_all_pending_le_entry(void) {
1831911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t xx;
1832911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tINQ_DB_ENT* p_ent = btm_cb.btm_inq_vars.inq_db;
1833911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
1834911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  for (xx = 0; xx < BTM_INQ_DB_SIZE; xx++, p_ent++) {
1835911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* mark all pending LE entry as unused if an LE only device has scan
1836911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     * response outstanding */
1837911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if ((p_ent->in_use) &&
1838911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        (p_ent->inq_info.results.device_type == BT_DEVICE_TYPE_BLE) &&
1839911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        !p_ent->scan_rsp)
1840911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_ent->in_use = false;
1841911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
1842444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji}
1843444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
18449e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowskivoid btm_ble_process_adv_addr(RawAddress& bda, uint8_t addr_type) {
1845801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski#if (BLE_PRIVACY_SPT == TRUE)
1846801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  /* map address to security record */
1847c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski  bool match = btm_identity_addr_to_random_pseudo(&bda, &addr_type, false);
1848801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1849c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski  VLOG(1) << __func__ << ": bda=" << bda;
1850801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  /* always do RRA resolution on host */
1851801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  if (!match && BTM_BLE_IS_RESOLVE_BDA(bda)) {
1852801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    tBTM_SEC_DEV_REC* match_rec = btm_ble_resolve_random_addr(bda);
1853801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    if (match_rec) {
1854801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
1855c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski      match_rec->ble.cur_rand_addr = bda;
1856801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1857801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      if (btm_ble_init_pseudo_addr(match_rec, bda)) {
1858c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski        bda = match_rec->bd_addr;
1859801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      } else {
1860801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski        // Assign the original address to be the current report address
1861c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski        bda = match_rec->ble.pseudo_addr;
1862801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      }
1863801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    }
1864801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  }
1865801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski#endif
1866801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski}
1867801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1868801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski/**
1869801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski * This function is called when extended advertising report event is received .
1870801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski * It updates the inquiry database. If the inquiry database is full, the oldest
1871801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski * entry is discarded.
1872801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski */
1873801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowskivoid btm_ble_process_ext_adv_pkt(uint8_t data_len, uint8_t* data) {
18749e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski  RawAddress bda, direct_address;
1875801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  uint8_t* p = data;
1876801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  uint8_t addr_type, num_reports, pkt_data_len, primary_phy, secondary_phy,
1877801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      advertising_sid;
1878801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  int8_t rssi, tx_power;
1879801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  uint16_t event_type, periodic_adv_int, direct_address_type;
1880801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1881801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  /* Only process the results if the inquiry is still active */
1882801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
1883801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1884801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  /* Extract the number of reports in this event. */
1885801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  STREAM_TO_UINT8(num_reports, p);
1886801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1887801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  while (num_reports--) {
1888801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    if (p > data + data_len) {
1889801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      // TODO(jpawlowski): we should crash the stack here
1890801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      BTM_TRACE_ERROR(
1891801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski          "Malformed LE Extended Advertising Report Event from controller - "
1892801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski          "can't loop the data");
1893801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      return;
1894801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    }
1895801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1896801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    /* Extract inquiry results */
1897801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT16(event_type, p);
1898801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT8(addr_type, p);
1899b8a477e9ac51898b05cb328eda897d95f1ef3d02Jakub Pawlowski    STREAM_TO_BDADDR(bda, p);
1900801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT8(primary_phy, p);
1901801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT8(secondary_phy, p);
1902801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT8(advertising_sid, p);
1903801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_INT8(tx_power, p);
1904801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_INT8(rssi, p);
1905801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT16(periodic_adv_int, p);
1906801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT8(direct_address_type, p);
1907b8a477e9ac51898b05cb328eda897d95f1ef3d02Jakub Pawlowski    STREAM_TO_BDADDR(direct_address, p);
1908801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    STREAM_TO_UINT8(pkt_data_len, p);
1909801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1910801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    uint8_t* pkt_data = p;
1911801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    p += pkt_data_len; /* Advance to the the next packet*/
1912801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1913801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    if (rssi >= 21 && rssi <= 126) {
1914801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski      BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
1915801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski                      pkt_data_len, rssi);
1916801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    }
1917801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1918801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    btm_ble_process_adv_addr(bda, addr_type);
19197de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski    btm_ble_process_adv_pkt_cont(event_type, addr_type, bda, primary_phy,
19207de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski                                 secondary_phy, advertising_sid, tx_power, rssi,
19217de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski                                 periodic_adv_int, pkt_data_len, pkt_data);
1922801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski  }
1923801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski}
1924801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski
1925801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski/**
1926801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski * This function is called when advertising report event is received. It updates
1927801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski * the inquiry database. If the inquiry database is full, the oldest entry is
1928801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski * discarded.
1929801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski */
19309e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowskivoid btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data) {
19319e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski  RawAddress bda;
19329e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski  uint8_t* p = data;
1933d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  uint8_t legacy_evt_type, addr_type, num_reports, pkt_data_len;
19349e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski  int8_t rssi;
19355738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1936911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* Only process the results if the inquiry is still active */
1937911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!BTM_BLE_IS_SCAN_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) return;
1938444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
1939911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* Extract the number of reports in this event. */
1940911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  STREAM_TO_UINT8(num_reports, p);
19415738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1942911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  while (num_reports--) {
19439e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    if (p > data + data_len) {
19449e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski      // TODO(jpawlowski): we should crash the stack here
19459e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski      BTM_TRACE_ERROR("Malformed LE Advertising Report Event from controller");
19469e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski      return;
19479e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    }
19489e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski
1949911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* Extract inquiry results */
1950d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    STREAM_TO_UINT8(legacy_evt_type, p);
1951911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    STREAM_TO_UINT8(addr_type, p);
1952b8a477e9ac51898b05cb328eda897d95f1ef3d02Jakub Pawlowski    STREAM_TO_BDADDR(bda, p);
19539e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    STREAM_TO_UINT8(pkt_data_len, p);
19549e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski
19559e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    uint8_t* pkt_data = p;
19569e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    p += pkt_data_len; /* Advance to the the rssi byte */
19579e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski
19589e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    STREAM_TO_INT8(rssi, p);
19599e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski
19609e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    if (rssi >= 21 && rssi <= 126) {
19619e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski      BTM_TRACE_ERROR("%s: bad rssi value in advertising report: ", __func__,
19629e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski                      pkt_data_len, rssi);
19639e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski    }
19645738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1965801db30048049a63869587af4b89a6e5a4dabb9bJakub Pawlowski    btm_ble_process_adv_addr(bda, addr_type);
19669462c5dbd610e7b894008cec99f57e082de1abc5Jakub Pawlowski
1967d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    uint16_t event_type;
1968d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    if (legacy_evt_type == 0x00) {  // ADV_IND;
1969d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      event_type = 0x0013;
1970d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    } else if (legacy_evt_type == 0x01) {  // ADV_DIRECT_IND;
1971d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      event_type = 0x0015;
1972d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    } else if (legacy_evt_type == 0x02) {  // ADV_SCAN_IND;
1973d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      event_type = 0x0012;
1974d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    } else if (legacy_evt_type == 0x03) {  // ADV_NONCONN_IND;
1975d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      event_type = 0x0010;
1976d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    } else if (legacy_evt_type == 0x04) {  // SCAN_RSP;
1977d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      // We can't distinguish between "SCAN_RSP to an ADV_IND", and "SCAN_RSP to
1978d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      // an ADV_SCAN_IND", so always return "SCAN_RSP to an ADV_IND"
1979d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      event_type = 0x001B;
1980d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    } else {
1981d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      BTM_TRACE_ERROR(
1982d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski          "Malformed LE Advertising Report Event - unsupported "
1983d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski          "legacy_event_type 0x%02x",
1984d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski          legacy_evt_type);
1985d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski      return;
1986d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski    }
1987d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
19887de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski    btm_ble_process_adv_pkt_cont(
19897de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski        event_type, addr_type, bda, PHY_LE_1M, PHY_LE_NO_PACKET, NO_ADI_PRESENT,
19907de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski        TX_POWER_NOT_PRESENT, rssi, 0x00 /* no periodic adv */, pkt_data_len,
19917de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski        pkt_data);
1992911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
19935738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
19945738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
1995d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski/**
1996d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski * This function is called after random address resolution is done, and proceed
1997d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski * to process adv packet.
1998d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski */
19997de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowskistatic void btm_ble_process_adv_pkt_cont(
20009e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski    uint16_t evt_type, uint8_t addr_type, const RawAddress& bda,
2001c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
2002c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int, uint8_t data_len,
2003c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    uint8_t* data) {
2004911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
2005911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  bool update = true;
2006911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
20070595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  std::vector<uint8_t> tmp;
20080595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  if (data_len != 0) tmp.insert(tmp.begin(), data, data + data_len);
20090595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
20100595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  bool is_scannable = ble_evt_type_is_scannable(evt_type);
20110595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  bool is_scan_resp = ble_evt_type_is_scan_resp(evt_type);
20120595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
2013436058ee3f52419fde558068a77222a67153e40dJakub Pawlowski  bool is_start =
2014436058ee3f52419fde558068a77222a67153e40dJakub Pawlowski      ble_evt_type_is_legacy(evt_type) && is_scannable && !is_scan_resp;
2015436058ee3f52419fde558068a77222a67153e40dJakub Pawlowski
2016436058ee3f52419fde558068a77222a67153e40dJakub Pawlowski  if (is_start) AdvertiseDataParser::RemoveTrailingZeros(tmp);
2017436058ee3f52419fde558068a77222a67153e40dJakub Pawlowski
20180595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  // We might have send scan request to this device before, but didn't get the
20190595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  // response. In such case make sure data is put at start, not appended to
20200595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  // already existing data.
20210595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  std::vector<uint8_t> const& adv_data =
20220595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      is_start ? cache.Set(addr_type, bda, std::move(tmp))
20230595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski               : cache.Append(addr_type, bda, std::move(tmp));
20240595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
20250595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  bool data_complete = (ble_evt_type_data_status(evt_type) != 0x01);
20260595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
20270595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  if (!data_complete) {
20280595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    // If we didn't receive whole adv data yet, don't report the device.
2029c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    DVLOG(1) << "Data not complete yet, waiting for more " << bda;
20300595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    return;
20310595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  }
20320595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
20330595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  bool is_active_scan =
20340595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski      btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI;
20350595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  if (is_active_scan && is_scannable && !is_scan_resp) {
20360595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    // If we didn't receive scan response yet, don't report the device.
2037c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski    DVLOG(1) << " Waiting for scan response " << bda;
20380595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    return;
20390595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  }
20400595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
2041103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski  if (!AdvertiseDataParser::IsValid(adv_data)) {
2042103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski    DVLOG(1) << __func__ << "Dropping bad advertisement packet: "
2043103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski             << base::HexEncode(adv_data.data(), adv_data.size());
2044103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski    return;
2045103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski  }
2046103b2c44d3db6172a2dc9319e215eeece504316eJakub Pawlowski
20470595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  tINQ_DB_ENT* p_i = btm_inq_db_find(bda);
2048911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2049911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* Check if this address has already been processed for this inquiry */
2050911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_inq_find_bdaddr(bda)) {
2051911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* never been report as an LE device */
2052911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_i && (!(p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ||
2053911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                /* scan repsonse to be updated */
2054911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                (!p_i->scan_rsp))) {
2055911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      update = true;
2056911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else if (BTM_BLE_IS_OBS_ACTIVE(btm_cb.ble_ctr_cb.scan_activity)) {
2057911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      update = false;
2058911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else {
2059911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* if yes, skip it */
2060911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      return; /* assumption: one result per event */
2061911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
2062911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2063911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* If existing entry, use that, else get  a new one (possibly reusing the
2064911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson   * oldest) */
2065911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_i == NULL) {
2066911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_i = btm_inq_db_new(bda);
2067911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_i != NULL) {
2068911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_inq->inq_cmpl_info.num_resp++;
2069911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    } else
2070911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      return;
2071911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  } else if (p_i->inq_count !=
2072911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson             p_inq->inq_counter) /* first time seen in this inquiry */
2073911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  {
2074911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_inq->inq_cmpl_info.num_resp++;
2075911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
20760595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
2077911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* update the LE device information in inquiry database */
20780595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  btm_ble_update_inq_result(p_i, addr_type, bda, evt_type, primary_phy,
20790595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                            secondary_phy, advertising_sid, tx_power, rssi,
20800595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                            periodic_adv_int, adv_data);
2081911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
20820595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  uint8_t result = btm_ble_is_discoverable(bda, adv_data);
2083911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (result == 0) {
20840595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski    cache.Clear(addr_type, bda);
2085911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    LOG_WARN(LOG_TAG,
20869e3005684fe9fa592630d685e9cceb71cd27dfe7Jakub Pawlowski             "%s device no longer discoverable, discarding advertising packet",
2087911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson             __func__);
2088911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return;
2089911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
20900595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
2091911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!update) result &= ~BTM_BLE_INQ_RESULT;
2092911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* If the number of responses found and limited, issue a cancel inquiry */
2093911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_inq->inqparms.max_resps &&
2094911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_inq->inq_cmpl_info.num_resp == p_inq->inqparms.max_resps) {
2095911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* new device */
2096911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (p_i == NULL ||
2097911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        /* assume a DUMO device, BR/EDR inquiry is always active */
2098911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        (p_i &&
2099911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson         (p_i->inq_info.results.device_type & BT_DEVICE_TYPE_BLE) ==
2100911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson             BT_DEVICE_TYPE_BLE &&
2101911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson         p_i->scan_rsp)) {
2102911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      BTM_TRACE_WARNING(
2103911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          "INQ RES: Extra Response Received...cancelling inquiry..");
2104911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2105911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      /* if is non-periodic inquiry active, cancel now */
2106911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      if ((p_inq->inq_active & BTM_BR_INQ_ACTIVE_MASK) != 0 &&
2107911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          (p_inq->inq_active & BTM_PERIODIC_INQUIRY_ACTIVE) == 0)
2108911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson        btsnd_hcic_inq_cancel();
2109911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2110911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_ble_stop_inquiry();
2111911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2112911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      btm_acl_update_busy_level(BTM_BLI_INQ_DONE_EVT);
2113911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    }
2114911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
211583211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski
21167de0f9b78fb38fcb02cbcd3c1c7c005947b9e291Jakub Pawlowski  tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb;
211783211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {
211883211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski    (p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
21190595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                       const_cast<uint8_t*>(adv_data.data()), adv_data.size());
212083211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  }
2121d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski
2122d64bf4f6f8b34471b120330bf3be2b8b7aab0274Jakub Pawlowski  tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;
212383211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski  if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {
212483211b0ccceb53586ac651a4d67ded7443b2f222Jakub Pawlowski    (p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,
21250595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski                       const_cast<uint8_t*>(adv_data.data()), adv_data.size());
2126911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
21270595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski
21280595ca0018ac9980d1dd7e192301f8494a01f9cbJakub Pawlowski  cache.Clear(addr_type, bda);
21297fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
21307fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2131eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowskivoid btm_ble_process_phy_update_pkt(uint8_t len, uint8_t* data) {
2132eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  uint8_t status, tx_phy, rx_phy;
2133eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  uint16_t handle;
2134eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski
2135eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  LOG_ASSERT(len == 5);
2136eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  uint8_t* p = data;
2137eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  STREAM_TO_UINT8(status, p);
2138eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  STREAM_TO_UINT16(handle, p);
2139eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  handle = handle & 0x0FFF;
2140eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  STREAM_TO_UINT8(tx_phy, p);
2141eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  STREAM_TO_UINT8(rx_phy, p);
2142eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski
2143eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev_by_handle(handle);
2144eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  if (!p_dev_rec) {
2145eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski    BTM_TRACE_WARNING("%s: No Device Found!", __func__);
2146eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski    return;
2147eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  }
2148eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski
2149eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  tGATT_TCB* p_tcb =
2150eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski      gatt_find_tcb_by_addr(p_dev_rec->ble.pseudo_addr, BT_TRANSPORT_LE);
2151eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  if (p_tcb == NULL) return;
2152eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski
2153eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski  gatt_notify_phy_updated(p_tcb, tx_phy, rx_phy, status);
2154eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski}
2155eafd45d08653bb1621c82a2f3cf922a43a0b1bc5Jakub Pawlowski
21567fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
2157ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2158ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_start_scan
2159ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2160ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Start the BLE scan.
2161ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2162ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2163ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2164ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2165911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS btm_ble_start_scan(void) {
2166911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
2167911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* start scan, disable duplicate filtering */
21689df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  btm_send_hci_scan_enable(BTM_BLE_SCAN_ENABLE, p_inq->scan_duplicate_filter);
2169b6ab9b3af6b8793ff781e0359a78611726ab2300Jakub Pawlowski
2170911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_inq->scan_type == BTM_BLE_SCAN_MODE_ACTI)
2171911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
2172911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else
2173911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
2174b6ab9b3af6b8793ff781e0359a78611726ab2300Jakub Pawlowski
2175911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return BTM_CMD_STARTED;
21765738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
21775738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
21785738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
2179ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2180ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_stop_scan
2181ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2182ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Stop the BLE scan.
2183ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2184ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2185ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2186ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2187911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_stop_scan(void) {
2188911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_EVENT("btm_ble_stop_scan ");
21895738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
21904d05ea59a1b1d4ae42c7e9026a45f8ea35d6b398Jakub Pawlowski  if (btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI)
21914d05ea59a1b1d4ae42c7e9026a45f8ea35d6b398Jakub Pawlowski    btm_ble_clear_topology_mask(BTM_BLE_STATE_ACTIVE_SCAN_BIT);
21924d05ea59a1b1d4ae42c7e9026a45f8ea35d6b398Jakub Pawlowski  else
21934d05ea59a1b1d4ae42c7e9026a45f8ea35d6b398Jakub Pawlowski    btm_ble_clear_topology_mask(BTM_BLE_STATE_PASSIVE_SCAN_BIT);
21944d05ea59a1b1d4ae42c7e9026a45f8ea35d6b398Jakub Pawlowski
2195911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* Clear the inquiry callback if set */
2196911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.ble_ctr_cb.inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
21975738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2198911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* stop discovery now */
21999df2a552f7df99d79e1602781b37f6a6380fc71cJakub Pawlowski  btm_send_hci_scan_enable(BTM_BLE_SCAN_DISABLE, BTM_BLE_DUPLICATE_ENABLE);
22005738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2201911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_update_scanner_filter_policy(SP_ADV_ALL);
22027fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
22037fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
2204ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2205ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_stop_inquiry
2206ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2207ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Stop the BLE Inquiry.
2208ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2209ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2210ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2211ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2212911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_stop_inquiry(void) {
2213911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_INQUIRY_VAR_ST* p_inq = &btm_cb.btm_inq_vars;
2214911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
22157fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2216911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_cancel(p_ble_cb->inq_var.inquiry_timer);
22175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2218911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_ble_cb->scan_activity &= ~BTM_BLE_INQUIRY_MASK;
2219911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2220911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* If no more scan activity, stop LE scan now */
2221911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity))
2222911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_stop_scan();
2223911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else if ((p_ble_cb->inq_var.scan_interval != BTM_BLE_LOW_LATENCY_SCAN_INT) ||
2224911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson           (p_ble_cb->inq_var.scan_window != BTM_BLE_LOW_LATENCY_SCAN_WIN)) {
2225911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_DEBUG("%s: setting default params for ongoing observe", __func__);
2226911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_stop_scan();
2227911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_start_scan();
2228911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2229911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2230911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* If we have a callback registered for inquiry complete, call it */
2231911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("BTM Inq Compl Callback: status 0x%02x, num results %d",
2232911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                  p_inq->inq_cmpl_info.status, p_inq->inq_cmpl_info.num_resp);
2233911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2234911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_process_inq_complete(
2235911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      HCI_SUCCESS, (uint8_t)(p_inq->inqparms.mode & BTM_BLE_INQUIRY_MASK));
22367fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
22377fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
22387fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
2239ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2240ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_stop_observe
2241ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2242ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Stop the BLE Observe.
2243ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2244ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2245ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2246ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2247911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_stop_observe(void) {
2248911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_CB* p_ble_cb = &btm_cb.ble_ctr_cb;
2249911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_CMPL_CB* p_obs_cb = p_ble_cb->p_obs_cmpl_cb;
22507fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2251911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_cancel(p_ble_cb->observer_timer);
22525738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2253911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_ble_cb->scan_activity &= ~BTM_LE_OBSERVE_ACTIVE;
22547fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2255911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_ble_cb->p_obs_results_cb = NULL;
2256911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_ble_cb->p_obs_cmpl_cb = NULL;
22577fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2258911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) btm_ble_stop_scan();
22597fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2260911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_obs_cb)
2261911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    (p_obs_cb)((tBTM_INQUIRY_CMPL*)&btm_cb.btm_inq_vars.inq_cmpl_info);
22627fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
22637fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
2264ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2265ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_adv_states_operation
2266ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2267ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Set or clear adv states in topology mask
2268ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2269ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          operation status. true if sucessful, false otherwise.
2270ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2271ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2272911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsontypedef bool(BTM_TOPOLOGY_FUNC_PTR)(tBTM_BLE_STATE_MASK);
2273911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic bool btm_ble_adv_states_operation(BTM_TOPOLOGY_FUNC_PTR* p_handler,
2274911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                                         uint8_t adv_evt) {
2275911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  bool rt = false;
22767fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2277911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  switch (adv_evt) {
22787fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    case BTM_BLE_CONNECT_EVT:
2279911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      rt = (*p_handler)(BTM_BLE_STATE_CONN_ADV_BIT);
2280911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
22817fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2282911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    case BTM_BLE_NON_CONNECT_EVT:
2283911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      rt = (*p_handler)(BTM_BLE_STATE_NON_CONN_ADV_BIT);
2284911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
22857fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    case BTM_BLE_CONNECT_DIR_EVT:
2286911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      rt = (*p_handler)(BTM_BLE_STATE_HI_DUTY_DIR_ADV_BIT);
2287911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
22887fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
22897fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    case BTM_BLE_DISCOVER_EVT:
2290911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      rt = (*p_handler)(BTM_BLE_STATE_SCAN_ADV_BIT);
2291911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
22927fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2293444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji    case BTM_BLE_CONNECT_LO_DUTY_DIR_EVT:
2294911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      rt = (*p_handler)(BTM_BLE_STATE_LO_DUTY_DIR_ADV_BIT);
2295911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
2296444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
22977fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    default:
2298911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      BTM_TRACE_ERROR("unknown adv event : %d", adv_evt);
2299911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      break;
2300911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
23017fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2302911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return rt;
23035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
23045738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
23055738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
2306ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2307ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_start_adv
2308ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2309ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      start the BLE advertising.
2310ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2311ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2312ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2313ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2314911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS btm_ble_start_adv(void) {
2315911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
2316ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2317911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (!btm_ble_adv_states_operation(btm_ble_topology_check, p_cb->evt_type))
2318911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return BTM_WRONG_MODE;
23197fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2320d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_PRIVACY_SPT == TRUE)
2321911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* To relax resolving list,  always have resolving list enabled, unless
2322911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson   * directed adv */
2323911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->evt_type != BTM_BLE_CONNECT_LO_DUTY_DIR_EVT &&
2324911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      p_cb->evt_type != BTM_BLE_CONNECT_DIR_EVT)
2325911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* enable resolving list is desired */
2326911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_enable_resolving_list_for_platform(BTM_BLE_RL_ADV);
2327444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji#endif
23287fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2329911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_ENABLE);
2330911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->adv_mode = BTM_BLE_ADV_ENABLE;
2331911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_adv_states_operation(btm_ble_set_topology_mask, p_cb->evt_type);
2332911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return BTM_SUCCESS;
2333ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
2334444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2335ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
2336ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2337ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_stop_adv
2338ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2339ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Stop the BLE advertising.
2340ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2341ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2342ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2343ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2344911d1ae03efec2d54c3b1b605589d790d1745488Myles WatsontBTM_STATUS btm_ble_stop_adv(void) {
2345911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
23465738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2347911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2348911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btsnd_hcic_ble_set_adv_enable(BTM_BLE_ADV_DISABLE);
23497fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2350911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->fast_adv_on = false;
2351911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
2352b6ab9b3af6b8793ff781e0359a78611726ab2300Jakub Pawlowski
2353911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* clear all adv states */
2354911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2355911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2356911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return BTM_SUCCESS;
23577fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
23587fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2359911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_fast_adv_timer_timeout(UNUSED_ATTR void* data) {
2360911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* fast adv is completed, fall back to slow adv interval */
2361911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_start_slow_adv();
236278bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov}
236378bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov
23647fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
2365ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2366ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_start_slow_adv
2367ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2368ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Restart adv with slow adv interval
2369ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2370ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2371ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2372ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2373911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_start_slow_adv(void) {
2374911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
23757fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2376911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (p_cb->adv_mode == BTM_BLE_ADV_ENABLE) {
2377911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2378aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski    RawAddress address = RawAddress::kEmpty;
2379911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
2380911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
2381444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2382911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_stop_adv();
23837fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2384911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
2385aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski        p_cb, address, &init_addr_type, &own_addr_type);
2386444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2387911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* slow adv mode never goes into directed adv */
2388aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski    btsnd_hcic_ble_write_adv_params(
2389aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski        BTM_BLE_GAP_ADV_SLOW_INT, BTM_BLE_GAP_ADV_SLOW_INT, p_cb->evt_type,
2390aaa50649b95c0ab2cd84e67fab77596bbb24d095Jakub Pawlowski        own_addr_type, init_addr_type, address, p_cb->adv_chnl_map, p_cb->afp);
23917fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2392911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_start_adv();
2393911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
23947fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
23955738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2396911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_inquiry_timer_gap_limited_discovery_timeout(
2397911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    UNUSED_ATTR void* data) {
2398911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* lim_timeout expired, limited discovery should exit now */
2399911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.btm_inq_vars.discoverable_mode &= ~BTM_BLE_LIMITED_DISCOVERABLE;
2400911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_set_adv_flag(btm_cb.btm_inq_vars.connectable_mode,
2401911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                       btm_cb.btm_inq_vars.discoverable_mode);
240278bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov}
24035738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2404911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_inquiry_timer_timeout(UNUSED_ATTR void* data) {
2405911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_stop_inquiry();
240678bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov}
24075738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2408911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonstatic void btm_ble_observer_timer_timeout(UNUSED_ATTR void* data) {
2409911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_stop_observe();
241078bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov}
24117fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2412911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_refresh_raddr_timer_timeout(UNUSED_ATTR void* data) {
2413911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type == BLE_ADDR_RANDOM) {
2414911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* refresh the random addr */
24150c683232e28ea616844bfa03d876a08df217ba13Jakub Pawlowski    btm_gen_resolvable_private_addr(base::Bind(&btm_gen_resolve_paddr_low));
2416911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
24175738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
24185738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
24195738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
2420ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2421ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_read_remote_features_complete
2422ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2423ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function is called when the command complete message
24249ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  is received from the HCI for the read LE remote feature
24259ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  supported complete event.
2426ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2427ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2428ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2429ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2430911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_read_remote_features_complete(uint8_t* p) {
243186d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  BTM_TRACE_EVENT("%s", __func__);
243286d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam
2433911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t handle;
2434911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t status;
2435911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  STREAM_TO_UINT8(status, p);
243686d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  STREAM_TO_UINT16(handle, p);
243786d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  handle = handle & 0x0FFF;  // only 12 bits meaningful
2438911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
243986d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  if (status != HCI_SUCCESS) {
2440341b6db90d67e76ea9262c5e9f3ba5063cb93598Jakub Pawlowski    BTM_TRACE_ERROR("%s: failed for handle: 0x%04d, status 0x%02x", __func__,
2441341b6db90d67e76ea9262c5e9f3ba5063cb93598Jakub Pawlowski                    handle, status);
2442341b6db90d67e76ea9262c5e9f3ba5063cb93598Jakub Pawlowski    if (status != HCI_ERR_UNSUPPORTED_REM_FEATURE) return;
244386d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  }
2444911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
244586d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  int idx = btm_handle_to_acl_index(handle);
244686d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  if (idx == MAX_L2CAP_LINKS) {
244786d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam    BTM_TRACE_ERROR("%s: can't find acl for handle: 0x%04d", __func__, handle);
244886d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam    return;
2449911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
245086d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam
2451341b6db90d67e76ea9262c5e9f3ba5063cb93598Jakub Pawlowski  if (status == HCI_SUCCESS) {
2452341b6db90d67e76ea9262c5e9f3ba5063cb93598Jakub Pawlowski    STREAM_TO_ARRAY(btm_cb.acl_db[idx].peer_le_features, p, BD_FEATURES_LEN);
2453341b6db90d67e76ea9262c5e9f3ba5063cb93598Jakub Pawlowski  }
245486d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam
245586d5b5658c36f489f952d530d21b7306f036e117Nagarjuna Kristam  btsnd_hcic_rmt_ver_req(handle);
24565738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
24575738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
24585738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project/*******************************************************************************
2459ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2460ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_write_adv_enable_complete
2461ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2462ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function process the write adv enable command complete.
2463ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2464ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2465ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2466ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2467911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_write_adv_enable_complete(uint8_t* p) {
2468911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
2469911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2470911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* if write adv enable/disbale not succeed */
2471911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (*p != HCI_SUCCESS) {
2472911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* toggle back the adv mode */
2473911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    p_cb->adv_mode = !p_cb->adv_mode;
2474911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2475ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
2476ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2477ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
2478ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2479ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_dir_adv_tout
2480ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2481ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      when directed adv time out
2482ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2483ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2484ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2485ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2486911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_dir_adv_tout(void) {
2487911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2488ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2489911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* make device fall back into undirected adv mode by default */
2490911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.ble_ctr_cb.inq_var.directed_conn = false;
2491ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
2492ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2493ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
2494ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2495ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_set_topology_mask
2496ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2497ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      set BLE topology mask
2498ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2499ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          true is request is allowed, false otherwise.
2500ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2501ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2502911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonbool btm_ble_set_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
2503911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2504911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.ble_ctr_cb.cur_states |= (request_state_mask & BTM_BLE_STATE_ALL_MASK);
2505911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return true;
2506444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji}
2507444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2508444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji/*******************************************************************************
2509ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2510ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_clear_topology_mask
2511ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2512ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Clear BLE topology bit mask
2513ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2514ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          true is request is allowed, false otherwise.
2515ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2516ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2517911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonbool btm_ble_clear_topology_mask(tBTM_BLE_STATE_MASK request_state_mask) {
2518911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  request_state_mask &= BTM_BLE_STATE_ALL_MASK;
2519911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.ble_ctr_cb.cur_states &= ~request_state_mask;
2520911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return true;
2521444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji}
2522444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2523444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji/*******************************************************************************
2524ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2525ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_update_link_topology_mask
2526ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2527ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      This function update the link topology mask
2528ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2529ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2530ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2531ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2532911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_update_link_topology_mask(uint8_t link_role, bool increase) {
2533911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_CONN_MASK);
2534911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2535911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (increase)
2536911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.link_count[link_role]++;
2537911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  else if (btm_cb.ble_ctr_cb.link_count[link_role] > 0)
2538911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.link_count[link_role]--;
2539911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2540911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_MASTER])
2541911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_topology_mask(BTM_BLE_STATE_MASTER_BIT);
2542911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2543911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.ble_ctr_cb.link_count[HCI_ROLE_SLAVE])
2544911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_topology_mask(BTM_BLE_STATE_SLAVE_BIT);
2545911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2546911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (link_role == HCI_ROLE_SLAVE && increase) {
2547911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2548911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* make device fall back into undirected adv mode by default */
2549911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
2550911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* clear all adv states */
2551911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2552911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2553444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji}
2554444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2555444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji/*******************************************************************************
2556ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2557ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_update_mode_operation
2558ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
25599ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      This function update the GAP role operation when a link
25609ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  status is updated.
2561ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2562ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2563ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2564ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
25659e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowskivoid btm_ble_update_mode_operation(uint8_t link_role, const RawAddress* bd_addr,
25669e030fde05352ec4385d7baf6cc2af89e95e039cJakub Pawlowski                                   uint8_t status) {
2567911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT) {
2568911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2569911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* make device fall back into undirected adv mode by default */
2570911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_cb.ble_ctr_cb.inq_var.directed_conn = BTM_BLE_CONNECT_EVT;
2571911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    /* clear all adv states */
2572911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_clear_topology_mask(BTM_BLE_STATE_ALL_ADV_MASK);
2573911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2574911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2575911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_cb.ble_ctr_cb.inq_var.connectable_mode == BTM_BLE_CONNECTABLE) {
2576911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_set_connectability(btm_cb.btm_inq_vars.connectable_mode |
2577911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson                               btm_cb.ble_ctr_cb.inq_var.connectable_mode);
2578911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2579911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
25808451ad010e26562d4a4c7d0d135c70c4325ee6b5Emil Lenngren  /* in case of disconnected, we must cancel bgconn and restart
25818451ad010e26562d4a4c7d0d135c70c4325ee6b5Emil Lenngren     in order to add back device to white list in order to reconnect */
2582c2276b06572ab6fc1f900fbb1f41087e77d47e2aJakub Pawlowski  btm_ble_bgconn_cancel_if_disconnected(*bd_addr);
25838451ad010e26562d4a4c7d0d135c70c4325ee6b5Emil Lenngren
2584911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* when no connection is attempted, and controller is not rejecting last
2585911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     request
2586911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     due to resource limitation, start next direct connection or background
2587911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     connection
2588911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson     now in order */
2589911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (btm_ble_get_conn_st() == BLE_CONN_IDLE &&
2590911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      status != HCI_ERR_HOST_REJECT_RESOURCES &&
2591af7f49c96e97c0d4754e2eed1ee82a5253ba7d47Emil Lenngren      status != HCI_ERR_MAX_NUM_OF_CONNECTIONS &&
2592911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      !btm_send_pending_direct_conn()) {
2593911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    btm_ble_resume_bg_conn();
2594911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2595444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji}
2596444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji
2597444a8da807abaf5f9e813ce70c56a79160495fb3Satya Calloji/*******************************************************************************
2598ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2599ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_init
2600ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2601ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Description      Initialize the control block variable values.
2602ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2603ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          void
2604ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2605ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2606911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonvoid btm_ble_init(void) {
2607911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
26085738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2609911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  BTM_TRACE_DEBUG("%s", __func__);
26105738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2611911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_free(p_cb->observer_timer);
2612911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  alarm_free(p_cb->inq_var.fast_adv_timer);
2613911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  memset(p_cb, 0, sizeof(tBTM_BLE_CB));
2614911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  memset(&(btm_cb.cmn_ble_vsc_cb), 0, sizeof(tBTM_BLE_VSC_CB));
2615911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_cb.cmn_ble_vsc_cb.values_read = false;
261678bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov
2617911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->observer_timer = alarm_new("btm_ble.observer_timer");
2618911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->cur_states = 0;
2619911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->conn_pending_q = fixed_queue_new(SIZE_MAX);
26205738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2621911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
2622911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.scan_type = BTM_BLE_SCAN_MODE_NONE;
2623911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.adv_chnl_map = BTM_BLE_DEFAULT_ADV_CHNL_MAP;
2624911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.afp = BTM_BLE_DEFAULT_AFP;
2625911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.sfp = BTM_BLE_DEFAULT_SFP;
2626911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.connectable_mode = BTM_BLE_NON_CONNECTABLE;
2627911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.discoverable_mode = BTM_BLE_NON_DISCOVERABLE;
2628911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.fast_adv_timer = alarm_new("btm_ble_inq.fast_adv_timer");
2629911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.inquiry_timer = alarm_new("btm_ble_inq.inquiry_timer");
26305738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2631911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* for background connection, reset connection params to be undefined */
2632911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->scan_int = p_cb->scan_win = BTM_BLE_SCAN_PARAM_UNDEF;
26335738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
2634911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->inq_var.evt_type = BTM_BLE_NON_CONNECT_EVT;
2635c4e2596a112a0dd63ac681b1226548c0f95e828cSatya Calloji
2636911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  p_cb->addr_mgnt_cb.refresh_raddr_timer =
2637911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      alarm_new("btm_ble_addr.refresh_raddr_timer");
263878bcff79e1b1f0efce436b33bdd6da88745bfc8aPavlin Radoslavov
2639d19e0785e662e640191a075eda07acce61c2aedaMarie Janssen#if (BLE_VND_INCLUDED == FALSE)
2640911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  btm_ble_adv_filter_init();
26411a9247a1229aa65b3d1dce16426177bc5828e54dSatya Calloji#endif
26425738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project}
26435738f83aeb59361a0a2eda2460113f6dc919427The Android Open Source Project
26447fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta/*******************************************************************************
2645ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2646ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Function         btm_ble_topology_check
2647ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
26489ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson * Description      check to see requested state is supported. One state check
26499ca07091a1f07ea201cee0504dab6a1d7073d429Myles Watson *                  at a time is supported
2650ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2651ee96a3c60fca590d38025925c072d264e06493c4Myles Watson * Returns          true is request is allowed, false otherwise.
2652ee96a3c60fca590d38025925c072d264e06493c4Myles Watson *
2653ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
2654911d1ae03efec2d54c3b1b605589d790d1745488Myles Watsonbool btm_ble_topology_check(tBTM_BLE_STATE_MASK request_state_mask) {
2655911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  bool rt = false;
2656911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2657911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t state_offset = 0;
2658911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint16_t cur_states = btm_cb.ble_ctr_cb.cur_states;
2659911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  uint8_t request_state = 0;
2660911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2661911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* check only one bit is set and within valid range */
2662911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  if (request_state_mask == BTM_BLE_STATE_INVALID ||
2663911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      request_state_mask > BTM_BLE_STATE_SCAN_ADV_BIT ||
2664911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      (request_state_mask & (request_state_mask - 1)) != 0) {
2665911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_ERROR("illegal state requested: %d", request_state_mask);
2666911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return rt;
2667911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
26687fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2669911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  while (request_state_mask) {
2670911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    request_state_mask >>= 1;
2671911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    request_state++;
2672911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
26737fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
2674911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* check if the requested state is supported or not */
26755e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski  uint8_t bit_num = btm_le_state_combo_tbl[0][request_state - 1];
2676911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  const uint8_t* ble_supported_states =
2677911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      controller_get_interface()->get_ble_supported_states();
26787fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta
26795e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski  if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, bit_num)) {
2680911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    BTM_TRACE_ERROR("state requested not supported: %d", request_state);
2681911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    return rt;
2682911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2683911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson
2684911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  rt = true;
2685911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  /* make sure currently active states are all supported in conjunction with the
26865e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski     requested state. If the bit in table is UNSUPPORTED, the combination is not
26875e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski     supported */
2688911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  while (cur_states != 0) {
2689911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    if (cur_states & 0x01) {
26905e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski      uint8_t bit_num = btm_le_state_combo_tbl[request_state][state_offset];
26915e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski      if (bit_num != UNSUPPORTED) {
26925e43c8035c6ac5067e78728688bb7e9b72b0dcfdJakub Pawlowski        if (!BTM_LE_STATES_SUPPORTED(ble_supported_states, bit_num)) {
2693911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          rt = false;
2694911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson          break;
26957fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta        }
2696911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson      }
26977fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta    }
2698911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    cur_states >>= 1;
2699911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson    state_offset++;
2700911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  }
2701911d1ae03efec2d54c3b1b605589d790d1745488Myles Watson  return rt;
27027fa4fba6f59f97df00aff07dbe8fb21b114b3c2cGanesh Ganapathi Batta}
2703