• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains functions for BLE acceptlist operation.
22  *
23  ******************************************************************************/
24 
25 #define LOG_TAG "ble_bgconn"
26 
27 #include "stack/btm/btm_ble_bgconn.h"
28 
29 #include <bluetooth/log.h>
30 
31 #include <cstdint>
32 #include <unordered_map>
33 
34 #include "hci/controller_interface.h"
35 #include "main/shim/acl_api.h"
36 #include "main/shim/entry.h"
37 #include "os/log.h"
38 #include "stack/btm/btm_ble_int.h"
39 #include "stack/btm/btm_dev.h"
40 #include "stack/btm/btm_int_types.h"
41 #include "types/raw_address.h"
42 
43 using namespace bluetooth;
44 
45 extern tBTM_CB btm_cb;
46 
47 // Unfortunately (for now?) we have to maintain a copy of the device acceptlist
48 // on the host to determine if a device is pending to be connected or not. This
49 // controls whether the host should keep trying to scan for acceptlisted
50 // peripherals or not.
51 // TODO: Move all of this to controller/le/background_list or similar?
52 struct BackgroundConnection {
53   RawAddress address;
54   uint8_t addr_type;
55   bool in_controller_wl;
56   uint8_t addr_type_in_wl;
57   bool pending_removal;
58 };
59 
60 struct BgConnHash {
operator ()BgConnHash61   std::size_t operator()(const RawAddress& x) const {
62     const uint8_t* a = x.address;
63     return a[0] ^ (a[1] << 8) ^ (a[2] << 16) ^ (a[3] << 24) ^ a[4] ^
64            (a[5] << 8);
65   }
66 };
67 
68 static std::unordered_map<RawAddress, BackgroundConnection, BgConnHash>
69     background_connections;
70 
71 /*******************************************************************************
72  *
73  * Function         btm_update_scanner_filter_policy
74  *
75  * Description      This function updates the filter policy of scanner
76  ******************************************************************************/
btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy)77 void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy) {
78   uint32_t scan_interval = !btm_cb.ble_ctr_cb.inq_var.scan_interval
79                                ? BTM_BLE_GAP_DISC_SCAN_INT
80                                : btm_cb.ble_ctr_cb.inq_var.scan_interval;
81   uint32_t scan_window = !btm_cb.ble_ctr_cb.inq_var.scan_window
82                              ? BTM_BLE_GAP_DISC_SCAN_WIN
83                              : btm_cb.ble_ctr_cb.inq_var.scan_window;
84   uint8_t scan_phy = !btm_cb.ble_ctr_cb.inq_var.scan_phy
85                          ? BTM_BLE_DEFAULT_PHYS
86                          : btm_cb.ble_ctr_cb.inq_var.scan_phy;
87 
88   log::verbose("");
89 
90   btm_cb.ble_ctr_cb.inq_var.sfp = scan_policy;
91   btm_cb.ble_ctr_cb.inq_var.scan_type =
92       btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_NONE
93           ? BTM_BLE_SCAN_MODE_ACTI
94           : btm_cb.ble_ctr_cb.inq_var.scan_type;
95 
96   btm_send_hci_set_scan_params(
97       btm_cb.ble_ctr_cb.inq_var.scan_type, (uint16_t)scan_interval,
98       (uint16_t)scan_window, (uint8_t)scan_phy,
99       btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, scan_policy);
100 }
101 
102 /** Adds the device into acceptlist. Returns false if acceptlist is full and
103  * device can't be added, true otherwise. */
BTM_AcceptlistAdd(const RawAddress & address)104 bool BTM_AcceptlistAdd(const RawAddress& address) {
105   return BTM_AcceptlistAdd(address, false);
106 }
107 
108 /** Adds the device into acceptlist and indicates whether to using direct
109  * connect parameters. Returns false if acceptlist is full and device can't
110  * be added, true otherwise. */
BTM_AcceptlistAdd(const RawAddress & address,bool is_direct)111 bool BTM_AcceptlistAdd(const RawAddress& address, bool is_direct) {
112   if (!bluetooth::shim::GetController()->SupportsBle()) {
113     log::warn("Controller does not support Le");
114     return false;
115   }
116 
117   return bluetooth::shim::ACL_AcceptLeConnectionFrom(
118       BTM_Sec_GetAddressWithType(address), is_direct);
119 }
120 
121 /** Removes the device from acceptlist */
BTM_AcceptlistRemove(const RawAddress & address)122 void BTM_AcceptlistRemove(const RawAddress& address) {
123   if (!bluetooth::shim::GetController()->SupportsBle()) {
124     log::warn("Controller does not support Le");
125     return;
126   }
127 
128   bluetooth::shim::ACL_IgnoreLeConnectionFrom(
129       BTM_Sec_GetAddressWithType(address));
130   return;
131 }
132 
133 /** Clear the acceptlist, end any pending acceptlist connections */
BTM_AcceptlistClear()134 void BTM_AcceptlistClear() {
135   if (!bluetooth::shim::GetController()->SupportsBle()) {
136     log::warn("Controller does not support Le");
137     return;
138   }
139   bluetooth::shim::ACL_IgnoreAllLeConnections();
140 }
141