1 /******************************************************************************
2 *
3 * Copyright 2016 The Android Open Source Project
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 #define LOG_TAG "bt_btif_scanner"
20
21 #include <base/bind.h>
22 #include <base/threading/thread.h>
23 #include <errno.h>
24 #include <hardware/bluetooth.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unordered_set>
29 #include "device/include/controller.h"
30
31 #include "btif_common.h"
32 #include "btif_util.h"
33
34 #include <hardware/bt_gatt.h>
35
36 #include "advertise_data_parser.h"
37 #include "bta_api.h"
38 #include "bta_gatt_api.h"
39 #include "btif_config.h"
40 #include "btif_dm.h"
41 #include "btif_gatt.h"
42 #include "btif_gatt_util.h"
43 #include "btif_storage.h"
44 #include "osi/include/log.h"
45 #include "stack/include/btu.h"
46 #include "vendor_api.h"
47
48 using base::Bind;
49 using base::Owned;
50 using std::vector;
51 using RegisterCallback = BleScannerInterface::RegisterCallback;
52
53 extern const btgatt_callbacks_t* bt_gatt_callbacks;
54
55 #define SCAN_CBACK_IN_JNI(P_CBACK, ...) \
56 do { \
57 if (bt_gatt_callbacks && bt_gatt_callbacks->scanner->P_CBACK) { \
58 BTIF_TRACE_API("HAL bt_gatt_callbacks->client->%s", #P_CBACK); \
59 do_in_jni_thread( \
60 Bind(bt_gatt_callbacks->scanner->P_CBACK, __VA_ARGS__)); \
61 } else { \
62 ASSERTC(0, "Callback is NULL", 0); \
63 } \
64 } while (0)
65
66 namespace {
67
68 // all access to this variable should be done on the jni thread
69 std::set<RawAddress> remote_bdaddr_cache;
70 std::queue<RawAddress> remote_bdaddr_cache_ordered;
71 const size_t remote_bdaddr_cache_max_size = 1024;
72
btif_address_cache_add(const RawAddress & p_bda,uint8_t addr_type)73 void btif_address_cache_add(const RawAddress& p_bda, uint8_t addr_type) {
74 // Remove the oldest entries
75 while (remote_bdaddr_cache.size() >= remote_bdaddr_cache_max_size) {
76 const RawAddress& raw_address = remote_bdaddr_cache_ordered.front();
77 remote_bdaddr_cache.erase(raw_address);
78 remote_bdaddr_cache_ordered.pop();
79 }
80 remote_bdaddr_cache.insert(p_bda);
81 remote_bdaddr_cache_ordered.push(p_bda);
82 }
83
btif_address_cache_find(const RawAddress & p_bda)84 bool btif_address_cache_find(const RawAddress& p_bda) {
85 return (remote_bdaddr_cache.find(p_bda) != remote_bdaddr_cache.end());
86 }
87
btif_address_cache_init(void)88 void btif_address_cache_init(void) {
89 remote_bdaddr_cache.clear();
90 remote_bdaddr_cache_ordered = {};
91 }
92
bta_batch_scan_threshold_cb(tBTM_BLE_REF_VALUE ref_value)93 void bta_batch_scan_threshold_cb(tBTM_BLE_REF_VALUE ref_value) {
94 SCAN_CBACK_IN_JNI(batchscan_threshold_cb, ref_value);
95 }
96
bta_batch_scan_reports_cb(int client_id,tBTA_STATUS status,uint8_t report_format,uint8_t num_records,std::vector<uint8_t> data)97 void bta_batch_scan_reports_cb(int client_id, tBTA_STATUS status,
98 uint8_t report_format, uint8_t num_records,
99 std::vector<uint8_t> data) {
100 SCAN_CBACK_IN_JNI(batchscan_reports_cb, client_id, status, report_format,
101 num_records, std::move(data));
102 }
103
bta_scan_results_cb_impl(RawAddress bd_addr,tBT_DEVICE_TYPE device_type,int8_t rssi,uint8_t addr_type,uint16_t ble_evt_type,uint8_t ble_primary_phy,uint8_t ble_secondary_phy,uint8_t ble_advertising_sid,int8_t ble_tx_power,uint16_t ble_periodic_adv_int,vector<uint8_t> value)104 void bta_scan_results_cb_impl(RawAddress bd_addr, tBT_DEVICE_TYPE device_type,
105 int8_t rssi, uint8_t addr_type,
106 uint16_t ble_evt_type, uint8_t ble_primary_phy,
107 uint8_t ble_secondary_phy,
108 uint8_t ble_advertising_sid, int8_t ble_tx_power,
109 uint16_t ble_periodic_adv_int,
110 vector<uint8_t> value) {
111 uint8_t remote_name_len;
112 bt_device_type_t dev_type;
113 bt_property_t properties;
114
115 const uint8_t* p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
116 value, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
117
118 if (p_eir_remote_name == NULL) {
119 p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
120 value, BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
121 }
122
123 if ((addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name)) {
124 if (!btif_address_cache_find(bd_addr)) {
125 btif_address_cache_add(bd_addr, addr_type);
126
127 if (p_eir_remote_name) {
128 if (remote_name_len > BD_NAME_LEN + 1 ||
129 (remote_name_len == BD_NAME_LEN + 1 &&
130 p_eir_remote_name[BD_NAME_LEN] != '\0')) {
131 LOG_INFO("%s dropping invalid packet - device name too long: %d",
132 __func__, remote_name_len);
133 return;
134 }
135
136 bt_bdname_t bdname;
137 memcpy(bdname.name, p_eir_remote_name, remote_name_len);
138 if (remote_name_len < BD_NAME_LEN + 1)
139 bdname.name[remote_name_len] = '\0';
140
141 LOG_VERBOSE("%s BLE device name=%s len=%d dev_type=%d", __func__,
142 bdname.name, remote_name_len, device_type);
143 btif_dm_update_ble_remote_properties(bd_addr, bdname.name, device_type);
144 }
145 }
146 }
147
148 dev_type = (bt_device_type_t)device_type;
149 BTIF_STORAGE_FILL_PROPERTY(&properties, BT_PROPERTY_TYPE_OF_DEVICE,
150 sizeof(dev_type), &dev_type);
151 btif_storage_set_remote_device_property(&(bd_addr), &properties);
152
153 btif_storage_set_remote_addr_type(&bd_addr, addr_type);
154 HAL_CBACK(bt_gatt_callbacks, scanner->scan_result_cb, ble_evt_type, addr_type,
155 &bd_addr, ble_primary_phy, ble_secondary_phy, ble_advertising_sid,
156 ble_tx_power, rssi, ble_periodic_adv_int, std::move(value));
157 }
158
bta_scan_results_cb(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)159 void bta_scan_results_cb(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p_data) {
160 uint8_t len;
161
162 if (event == BTA_DM_INQ_CMPL_EVT) {
163 BTIF_TRACE_DEBUG("%s BLE observe complete. Num Resp %d", __func__,
164 p_data->inq_cmpl.num_resps);
165 return;
166 }
167
168 if (event != BTA_DM_INQ_RES_EVT) {
169 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event);
170 return;
171 }
172
173 vector<uint8_t> value;
174 if (p_data->inq_res.p_eir) {
175 value.insert(value.begin(), p_data->inq_res.p_eir,
176 p_data->inq_res.p_eir + p_data->inq_res.eir_len);
177
178 if (AdvertiseDataParser::GetFieldByType(
179 value, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &len)) {
180 p_data->inq_res.remt_name_not_required = true;
181 }
182 }
183
184 tBTA_DM_INQ_RES* r = &p_data->inq_res;
185 do_in_jni_thread(Bind(bta_scan_results_cb_impl, r->bd_addr, r->device_type,
186 r->rssi, r->ble_addr_type, r->ble_evt_type,
187 r->ble_primary_phy, r->ble_secondary_phy,
188 r->ble_advertising_sid, r->ble_tx_power,
189 r->ble_periodic_adv_int, std::move(value)));
190 }
191
bta_track_adv_event_cb(tBTM_BLE_TRACK_ADV_DATA * p_track_adv_data)192 void bta_track_adv_event_cb(tBTM_BLE_TRACK_ADV_DATA* p_track_adv_data) {
193 btgatt_track_adv_info_t* btif_scan_track_cb = new btgatt_track_adv_info_t;
194
195 BTIF_TRACE_DEBUG("%s", __func__);
196 btif_gatt_move_track_adv_data(btif_scan_track_cb,
197 (btgatt_track_adv_info_t*)p_track_adv_data);
198
199 SCAN_CBACK_IN_JNI(track_adv_event_cb, Owned(btif_scan_track_cb));
200 }
201
bta_cback(tBTA_GATTC_EVT,tBTA_GATTC *)202 void bta_cback(tBTA_GATTC_EVT, tBTA_GATTC*) {}
203
204 class BleScannerInterfaceImpl : public BleScannerInterface {
~BleScannerInterfaceImpl()205 ~BleScannerInterfaceImpl() override{};
206
RegisterScanner(RegisterCallback cb)207 void RegisterScanner(RegisterCallback cb) override {
208 do_in_main_thread(FROM_HERE,
209 Bind(
210 [](RegisterCallback cb) {
211 BTA_GATTC_AppRegister(
212 bta_cback,
213 jni_thread_wrapper(FROM_HERE, std::move(cb)));
214 },
215 std::move(cb)));
216 }
217
Unregister(int scanner_id)218 void Unregister(int scanner_id) override {
219 do_in_main_thread(FROM_HERE, Bind(&BTA_GATTC_AppDeregister, scanner_id));
220 }
221
Scan(bool start)222 void Scan(bool start) override {
223 do_in_jni_thread(Bind(
224 [](bool start) {
225 if (!start) {
226 do_in_main_thread(FROM_HERE,
227 Bind(&BTA_DmBleObserve, false, 0, nullptr));
228 return;
229 }
230
231 btif_address_cache_init();
232 do_in_main_thread(
233 FROM_HERE, Bind(&BTA_DmBleObserve, true, 0, bta_scan_results_cb));
234 },
235 start));
236 }
237
ScanFilterParamSetup(uint8_t client_if,uint8_t action,uint8_t filt_index,std::unique_ptr<btgatt_filt_param_setup_t> filt_param,FilterParamSetupCallback cb)238 void ScanFilterParamSetup(
239 uint8_t client_if, uint8_t action, uint8_t filt_index,
240 std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
241 FilterParamSetupCallback cb) override {
242 BTIF_TRACE_DEBUG("%s", __func__);
243
244 if (filt_param && filt_param->dely_mode == 1) {
245 do_in_main_thread(
246 FROM_HERE, base::Bind(BTM_BleTrackAdvertiser, bta_track_adv_event_cb,
247 client_if));
248 }
249
250 do_in_main_thread(
251 FROM_HERE, base::Bind(&BTM_BleAdvFilterParamSetup, action, filt_index,
252 base::Passed(&filt_param),
253 jni_thread_wrapper(FROM_HERE, std::move(cb))));
254 }
255
ScanFilterAdd(int filter_index,std::vector<ApcfCommand> filters,FilterConfigCallback cb)256 void ScanFilterAdd(int filter_index, std::vector<ApcfCommand> filters,
257 FilterConfigCallback cb) override {
258 BTIF_TRACE_DEBUG("%s: %d", __func__, filter_index);
259
260 do_in_main_thread(
261 FROM_HERE,
262 base::Bind(
263 &BTM_LE_PF_set, filter_index, std::move(filters),
264 jni_thread_wrapper(
265 FROM_HERE,
266 Bind(std::move(cb),
267 0 /*TODO: this used to be filter type, unused ?*/))));
268 }
269
ScanFilterClear(int filter_index,FilterConfigCallback cb)270 void ScanFilterClear(int filter_index, FilterConfigCallback cb) override {
271 BTIF_TRACE_DEBUG("%s: filter_index: %d", __func__, filter_index);
272 do_in_main_thread(
273 FROM_HERE, base::Bind(&BTM_LE_PF_clear, filter_index,
274 jni_thread_wrapper(
275 FROM_HERE, Bind(cb, BTM_BLE_PF_TYPE_ALL))));
276 }
277
ScanFilterEnable(bool enable,EnableCallback cb)278 void ScanFilterEnable(bool enable, EnableCallback cb) override {
279 BTIF_TRACE_DEBUG("%s: enable: %d", __func__, enable);
280
281 uint8_t action = enable ? 1 : 0;
282 do_in_main_thread(FROM_HERE,
283 base::Bind(&BTM_BleEnableDisableFilterFeature, action,
284 jni_thread_wrapper(FROM_HERE, std::move(cb))));
285 }
286
SetScanParameters(int scan_interval,int scan_window,Callback cb)287 void SetScanParameters(int scan_interval, int scan_window,
288 Callback cb) override {
289 do_in_main_thread(
290 FROM_HERE, base::Bind(&BTM_BleSetScanParams, scan_interval, scan_window,
291 BTM_BLE_SCAN_MODE_ACTI,
292 jni_thread_wrapper(FROM_HERE, std::move(cb))));
293 }
294
BatchscanConfigStorage(int client_if,int batch_scan_full_max,int batch_scan_trunc_max,int batch_scan_notify_threshold,Callback cb)295 void BatchscanConfigStorage(int client_if, int batch_scan_full_max,
296 int batch_scan_trunc_max,
297 int batch_scan_notify_threshold,
298 Callback cb) override {
299 do_in_main_thread(
300 FROM_HERE,
301 base::Bind(&BTM_BleSetStorageConfig, (uint8_t)batch_scan_full_max,
302 (uint8_t)batch_scan_trunc_max,
303 (uint8_t)batch_scan_notify_threshold,
304 jni_thread_wrapper(FROM_HERE, cb),
305 bta_batch_scan_threshold_cb, (tBTM_BLE_REF_VALUE)client_if));
306 }
307
BatchscanEnable(int scan_mode,int scan_interval,int scan_window,int addr_type,int discard_rule,Callback cb)308 void BatchscanEnable(int scan_mode, int scan_interval, int scan_window,
309 int addr_type, int discard_rule, Callback cb) override {
310 do_in_main_thread(
311 FROM_HERE, base::Bind(&BTM_BleEnableBatchScan, scan_mode, scan_interval,
312 scan_window, discard_rule, addr_type,
313 jni_thread_wrapper(FROM_HERE, cb)));
314 }
315
BatchscanDisable(Callback cb)316 void BatchscanDisable(Callback cb) override {
317 do_in_main_thread(FROM_HERE, base::Bind(&BTM_BleDisableBatchScan,
318 jni_thread_wrapper(FROM_HERE, cb)));
319 }
320
BatchscanReadReports(int client_if,int scan_mode)321 void BatchscanReadReports(int client_if, int scan_mode) override {
322 do_in_main_thread(FROM_HERE,
323 base::Bind(&BTM_BleReadScanReports, (uint8_t)scan_mode,
324 Bind(bta_batch_scan_reports_cb, client_if)));
325 }
326
StartSync(uint8_t sid,RawAddress address,uint16_t skip,uint16_t timeout,StartSyncCb start_cb,SyncReportCb report_cb,SyncLostCb lost_cb)327 void StartSync(uint8_t sid, RawAddress address, uint16_t skip,
328 uint16_t timeout, StartSyncCb start_cb, SyncReportCb report_cb,
329 SyncLostCb lost_cb) override {}
330
StopSync(uint16_t handle)331 void StopSync(uint16_t handle) override {}
332 };
333
334 BleScannerInterface* btLeScannerInstance = nullptr;
335
336 } // namespace
337
get_ble_scanner_instance()338 BleScannerInterface* get_ble_scanner_instance() {
339 if (btLeScannerInstance == nullptr)
340 btLeScannerInstance = new BleScannerInterfaceImpl();
341
342 return btLeScannerInstance;
343 }
344