• 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 #include <base/bind.h>
26 #include <base/logging.h>
27 
28 #include <cstdint>
29 #include <unordered_map>
30 
31 #include "device/include/controller.h"
32 #include "main/shim/acl_api.h"
33 #include "main/shim/shim.h"
34 #include "stack/btm/btm_dev.h"
35 #include "stack/btm/btm_int_types.h"
36 #include "stack/btm/security_device_record.h"
37 #include "stack/include/bt_types.h"
38 #include "types/raw_address.h"
39 
40 extern tBTM_CB btm_cb;
41 
42 extern void btm_ble_create_conn_cancel();
43 
44 namespace {
45 
46 
47 }  // namespace
48 
49 // Unfortunately (for now?) we have to maintain a copy of the device acceptlist
50 // on the host to determine if a device is pending to be connected or not. This
51 // controls whether the host should keep trying to scan for acceptlisted
52 // peripherals or not.
53 // TODO: Move all of this to controller/le/background_list or similar?
54 struct BackgroundConnection {
55   RawAddress address;
56   uint8_t addr_type;
57   bool in_controller_wl;
58   uint8_t addr_type_in_wl;
59   bool pending_removal;
60 };
61 
62 struct BgConnHash {
operator ()BgConnHash63   std::size_t operator()(const RawAddress& x) const {
64     const uint8_t* a = x.address;
65     return a[0] ^ (a[1] << 8) ^ (a[2] << 16) ^ (a[3] << 24) ^ a[4] ^
66            (a[5] << 8);
67   }
68 };
69 
70 static std::unordered_map<RawAddress, BackgroundConnection, BgConnHash>
71     background_connections;
72 
convert_to_address_with_type(const RawAddress & bd_addr,const tBTM_SEC_DEV_REC * p_dev_rec)73 const tBLE_BD_ADDR convert_to_address_with_type(
74     const RawAddress& bd_addr, const tBTM_SEC_DEV_REC* p_dev_rec) {
75   if (p_dev_rec == nullptr || !p_dev_rec->is_device_type_has_ble()) {
76     return {
77         .type = BLE_ADDR_PUBLIC,
78         .bda = bd_addr,
79     };
80   }
81 
82   if (p_dev_rec->ble.identity_address_with_type.bda.IsEmpty()) {
83     return {
84         .type = p_dev_rec->ble.AddressType(),
85         .bda = bd_addr,
86     };
87   } else {
88     return p_dev_rec->ble.identity_address_with_type;
89   }
90 }
91 
92 /*******************************************************************************
93  *
94  * Function         btm_update_scanner_filter_policy
95  *
96  * Description      This function updates the filter policy of scanner
97  ******************************************************************************/
btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy)98 void btm_update_scanner_filter_policy(tBTM_BLE_SFP scan_policy) {
99   tBTM_BLE_INQ_CB* p_inq = &btm_cb.ble_ctr_cb.inq_var;
100 
101   uint32_t scan_interval =
102       !p_inq->scan_interval ? BTM_BLE_GAP_DISC_SCAN_INT : p_inq->scan_interval;
103   uint32_t scan_window =
104       !p_inq->scan_window ? BTM_BLE_GAP_DISC_SCAN_WIN : p_inq->scan_window;
105 
106   BTM_TRACE_EVENT("%s", __func__);
107 
108   p_inq->sfp = scan_policy;
109   p_inq->scan_type = p_inq->scan_type == BTM_BLE_SCAN_MODE_NONE
110                          ? BTM_BLE_SCAN_MODE_ACTI
111                          : p_inq->scan_type;
112 
113   btm_send_hci_set_scan_params(
114       p_inq->scan_type, (uint16_t)scan_interval, (uint16_t)scan_window,
115       btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type, scan_policy);
116 }
117 
118 /*******************************************************************************
119  *
120  * Function         btm_ble_suspend_bg_conn
121  *
122  * Description      This function is to suspend an active background connection
123  *                  procedure.
124  *
125  * Parameters       none.
126  *
127  * Returns          none.
128  *
129  ******************************************************************************/
btm_ble_suspend_bg_conn(void)130 bool btm_ble_suspend_bg_conn(void) {
131     LOG_DEBUG("Gd acl_manager handles sync of background connections");
132     return true;
133 }
134 
135 /*******************************************************************************
136  *
137  * Function         btm_ble_resume_bg_conn
138  *
139  * Description      This function is to resume a background auto connection
140  *                  procedure.
141  *
142  * Parameters       none.
143  *
144  * Returns          none.
145  *
146  ******************************************************************************/
btm_ble_resume_bg_conn(void)147 bool btm_ble_resume_bg_conn(void) {
148     LOG_DEBUG("Gd acl_manager handles sync of background connections");
149     return true;
150 }
151 
BTM_BackgroundConnectAddressKnown(const RawAddress & address)152 bool BTM_BackgroundConnectAddressKnown(const RawAddress& address) {
153   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
154 
155   //  not a known device, or a classic device, we assume public address
156   if (p_dev_rec == NULL || (p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) == 0)
157     return true;
158 
159   // bonded device with identity address known
160   if (!p_dev_rec->ble.identity_address_with_type.bda.IsEmpty()) {
161     return true;
162   }
163 
164   // Public address, Random Static, or Random Non-Resolvable Address known
165   if (p_dev_rec->ble.AddressType() == BLE_ADDR_PUBLIC ||
166       !BTM_BLE_IS_RESOLVE_BDA(address)) {
167     return true;
168   }
169 
170   // Only Resolvable Private Address (RPA) is known, we don't allow it into
171   // the background connection procedure.
172   return false;
173 }
174 
BTM_SetLeConnectionModeToFast()175 bool BTM_SetLeConnectionModeToFast() {
176   VLOG(2) << __func__;
177   tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
178   if ((p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF &&
179        p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ||
180       (p_cb->scan_int == BTM_BLE_SCAN_SLOW_INT_1 &&
181        p_cb->scan_win == BTM_BLE_SCAN_SLOW_WIN_1)) {
182     p_cb->scan_int = BTM_BLE_SCAN_FAST_INT;
183     p_cb->scan_win = BTM_BLE_SCAN_FAST_WIN;
184     return true;
185   }
186   return false;
187 }
188 
BTM_SetLeConnectionModeToSlow()189 void BTM_SetLeConnectionModeToSlow() {
190   VLOG(2) << __func__;
191   tBTM_BLE_CB* p_cb = &btm_cb.ble_ctr_cb;
192   if ((p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF &&
193        p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ||
194       (p_cb->scan_int == BTM_BLE_SCAN_FAST_INT &&
195        p_cb->scan_win == BTM_BLE_SCAN_FAST_WIN)) {
196     p_cb->scan_int = BTM_BLE_SCAN_SLOW_INT_1;
197     p_cb->scan_win = BTM_BLE_SCAN_SLOW_WIN_1;
198   }
199 }
200 
201 /** Adds the device into acceptlist. Returns false if acceptlist is full and
202  * device can't be added, true otherwise. */
BTM_AcceptlistAdd(const RawAddress & address)203 bool BTM_AcceptlistAdd(const RawAddress& address) {
204   if (!controller_get_interface()->supports_ble()) {
205     LOG_WARN("Controller does not support Le");
206     return false;
207   }
208 
209   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
210   if (p_dev_rec != NULL && p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
211     p_dev_rec->ble.in_controller_list |= BTM_ACCEPTLIST_BIT;
212   }
213 
214   return bluetooth::shim::ACL_AcceptLeConnectionFrom(
215       convert_to_address_with_type(address, p_dev_rec),
216       /* is_direct */ false);
217 }
218 
219 /** Removes the device from acceptlist */
BTM_AcceptlistRemove(const RawAddress & address)220 void BTM_AcceptlistRemove(const RawAddress& address) {
221   if (!controller_get_interface()->supports_ble()) {
222     LOG_WARN("Controller does not support Le");
223     return;
224   }
225 
226   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
227   if (p_dev_rec != NULL && p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
228     p_dev_rec->ble.in_controller_list &= ~BTM_ACCEPTLIST_BIT;
229   }
230 
231   bluetooth::shim::ACL_IgnoreLeConnectionFrom(
232       convert_to_address_with_type(address, p_dev_rec));
233   return;
234 }
235 
236 /** Clear the acceptlist, end any pending acceptlist connections */
BTM_AcceptlistClear()237 void BTM_AcceptlistClear() {
238   if (!controller_get_interface()->supports_ble()) {
239     LOG_WARN("Controller does not support Le");
240     return;
241   }
242   bluetooth::shim::ACL_IgnoreAllLeConnections();
243 }
244