blob: 20984769cb176cba1b94fc78c5021a01ffc34c61 [file] [log] [blame]
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -07001/******************************************************************************
2 *
3 * Copyright (C) 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 "btcore/include/bdaddr.h"
32#include "btif_common.h"
33#include "btif_util.h"
34
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070035#include <hardware/bt_gatt.h>
36
37#include "bta_api.h"
Jakub Pawlowski83f1d962016-12-14 09:49:38 -080038#include "bta_closure_api.h"
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070039#include "bta_gatt_api.h"
40#include "btif_config.h"
41#include "btif_dm.h"
42#include "btif_gatt.h"
43#include "btif_gatt_util.h"
44#include "btif_storage.h"
45#include "osi/include/log.h"
46#include "vendor_api.h"
47
48using base::Bind;
49using base::Owned;
50using std::vector;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -080051using RegisterCallback = BleScannerInterface::RegisterCallback;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070052
53extern bt_status_t do_in_jni_thread(const base::Closure& task);
54extern const btgatt_callbacks_t* bt_gatt_callbacks;
55
Myles Watson90088882016-11-15 16:33:22 -080056#define SCAN_CBACK_IN_JNI(P_CBACK, ...) \
57 do { \
58 if (bt_gatt_callbacks && bt_gatt_callbacks->scanner->P_CBACK) { \
59 BTIF_TRACE_API("HAL bt_gatt_callbacks->client->%s", #P_CBACK); \
60 do_in_jni_thread( \
61 Bind(bt_gatt_callbacks->scanner->P_CBACK, __VA_ARGS__)); \
62 } else { \
63 ASSERTC(0, "Callback is NULL", 0); \
64 } \
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070065 } while (0)
66
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070067namespace std {
68template <>
69struct hash<bt_bdaddr_t> {
70 size_t operator()(const bt_bdaddr_t& f) const {
71 return f.address[0] + f.address[1] + f.address[2] + f.address[3] +
72 f.address[4] + f.address[5];
73 }
74};
75
76template <>
77struct equal_to<bt_bdaddr_t> {
78 size_t operator()(const bt_bdaddr_t& x, const bt_bdaddr_t& y) const {
79 return memcmp(x.address, y.address, BD_ADDR_LEN);
80 }
81};
82}
83
84namespace {
85
86std::unordered_set<bt_bdaddr_t> p_dev_cb;
87
88void btif_gattc_add_remote_bdaddr(BD_ADDR p_bda, uint8_t addr_type) {
89 bt_bdaddr_t bd_addr;
90 memcpy(bd_addr.address, p_bda, BD_ADDR_LEN);
91 p_dev_cb.insert(bd_addr);
92}
93
94bool btif_gattc_find_bdaddr(BD_ADDR p_bda) {
95 bt_bdaddr_t bd_addr;
96 memcpy(bd_addr.address, p_bda, BD_ADDR_LEN);
97 return (p_dev_cb.count(bd_addr) != 0);
98}
99
100void btif_gattc_init_dev_cb(void) { p_dev_cb.clear(); }
101
102btgattc_error_t btif_gattc_translate_btm_status(tBTM_STATUS status) {
103 switch (status) {
104 case BTM_SUCCESS:
105 case BTM_SUCCESS_NO_SECURITY:
106 return BT_GATTC_COMMAND_SUCCESS;
107
108 case BTM_CMD_STARTED:
109 return BT_GATTC_COMMAND_STARTED;
110
111 case BTM_BUSY:
112 return BT_GATTC_COMMAND_BUSY;
113
114 case BTM_CMD_STORED:
115 return BT_GATTC_COMMAND_STORED;
116
117 case BTM_NO_RESOURCES:
118 return BT_GATTC_NO_RESOURCES;
119
120 case BTM_MODE_UNSUPPORTED:
121 case BTM_WRONG_MODE:
122 case BTM_MODE4_LEVEL4_NOT_SUPPORTED:
123 return BT_GATTC_MODE_UNSUPPORTED;
124
125 case BTM_ILLEGAL_VALUE:
126 case BTM_SCO_BAD_LENGTH:
127 return BT_GATTC_ILLEGAL_VALUE;
128
129 case BTM_UNKNOWN_ADDR:
130 return BT_GATTC_UNKNOWN_ADDR;
131
132 case BTM_DEVICE_TIMEOUT:
133 return BT_GATTC_DEVICE_TIMEOUT;
134
135 case BTM_FAILED_ON_SECURITY:
136 case BTM_REPEATED_ATTEMPTS:
137 case BTM_NOT_AUTHORIZED:
138 return BT_GATTC_SECURITY_ERROR;
139
140 case BTM_DEV_RESET:
141 case BTM_ILLEGAL_ACTION:
142 return BT_GATTC_INCORRECT_STATE;
143
144 case BTM_BAD_VALUE_RET:
145 return BT_GATTC_INVALID_CONTROLLER_OUTPUT;
146
147 case BTM_DELAY_CHECK:
148 return BT_GATTC_DELAYED_ENCRYPTION_CHECK;
149
150 case BTM_ERR_PROCESSING:
151 default:
152 return BT_GATTC_ERR_PROCESSING;
153 }
154}
155
156void btif_gatts_upstreams_evt(uint16_t event, char* p_param) {
157 LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event);
158
159 tBTA_GATTC* p_data = (tBTA_GATTC*)p_param;
160 switch (event) {
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700161 case BTA_GATTC_DEREG_EVT:
162 break;
163
164 case BTA_GATTC_SEARCH_CMPL_EVT: {
165 HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb,
166 p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
167 break;
168 }
169
170 default:
Jakub Pawlowski221e9bf2016-12-15 14:35:15 -0800171 LOG_DEBUG(LOG_TAG, "%s: Unhandled event (%d)", __func__, event);
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700172 break;
173 }
174}
175
176void bta_gatts_cback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
177 bt_status_t status =
178 btif_transfer_context(btif_gatts_upstreams_evt, (uint16_t)event,
179 (char*)p_data, sizeof(tBTA_GATTC), NULL);
180 ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
181}
182
183void bta_scan_param_setup_cb(tGATT_IF client_if, tBTM_STATUS status) {
184 SCAN_CBACK_IN_JNI(scan_parameter_setup_completed_cb, client_if,
Myles Watson90088882016-11-15 16:33:22 -0800185 btif_gattc_translate_btm_status(status));
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700186}
187
Jakub Pawlowski1febda92016-12-30 06:38:29 -0800188void bta_scan_filt_cfg_cb(uint8_t filt_type, uint8_t client_if,
189 tBTM_BLE_PF_AVBL_SPACE avbl_space,
190 tBTM_BLE_PF_ACTION action, tBTA_STATUS status) {
191 SCAN_CBACK_IN_JNI(scan_filter_cfg_cb, action, client_if, status, filt_type,
Myles Watson90088882016-11-15 16:33:22 -0800192 avbl_space);
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700193}
194
Jakub Pawlowski1febda92016-12-30 06:38:29 -0800195void bta_scan_filt_param_setup_cb(tBTM_BLE_REF_VALUE ref_value,
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800196 tBTM_BLE_PF_AVBL_SPACE avbl_space,
Jakub Pawlowski1febda92016-12-30 06:38:29 -0800197 uint8_t action_type, tBTA_STATUS status) {
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700198 SCAN_CBACK_IN_JNI(scan_filter_param_cb, action_type, ref_value, status,
Myles Watson90088882016-11-15 16:33:22 -0800199 avbl_space);
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700200}
201
Jakub Pawlowski1febda92016-12-30 06:38:29 -0800202void bta_scan_filt_status_cb(tBTM_BLE_REF_VALUE ref_value, uint8_t action,
203 tBTA_STATUS status) {
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700204 SCAN_CBACK_IN_JNI(scan_filter_status_cb, action, ref_value, status);
205}
206
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800207void bta_batch_scan_threshold_cb(tBTM_BLE_REF_VALUE ref_value) {
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700208 SCAN_CBACK_IN_JNI(batchscan_threshold_cb, ref_value);
209}
210
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800211void bta_batch_scan_reports_cb(int client_id, tBTA_STATUS status,
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700212 uint8_t report_format, uint8_t num_records,
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800213 std::vector<uint8_t> data) {
214 SCAN_CBACK_IN_JNI(batchscan_reports_cb, client_id, status, report_format,
215 num_records, std::move(data));
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700216}
217
218void bta_scan_results_cb_impl(bt_bdaddr_t bd_addr, tBT_DEVICE_TYPE device_type,
219 int8_t rssi, uint8_t addr_type,
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -0800220 uint16_t ble_evt_type, uint8_t ble_primary_phy,
221 uint8_t ble_secondary_phy,
222 uint8_t ble_advertising_sid, int8_t ble_tx_power,
223 uint16_t ble_periodic_adv_int,
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700224 vector<uint8_t> value) {
225 uint8_t remote_name_len;
226 const uint8_t* p_eir_remote_name = NULL;
227 bt_device_type_t dev_type;
228 bt_property_t properties;
229
230 p_eir_remote_name = BTM_CheckEirData(
231 value.data(), BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
232
233 if (p_eir_remote_name == NULL) {
234 p_eir_remote_name = BTM_CheckEirData(
235 value.data(), BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
236 }
237
238 if ((addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name)) {
239 if (!btif_gattc_find_bdaddr(bd_addr.address)) {
240 btif_gattc_add_remote_bdaddr(bd_addr.address, addr_type);
241
242 if (p_eir_remote_name) {
243 bt_bdname_t bdname;
244 memcpy(bdname.name, p_eir_remote_name, remote_name_len);
245 bdname.name[remote_name_len] = '\0';
246
247 LOG_VERBOSE(LOG_TAG, "%s BLE device name=%s len=%d dev_type=%d",
248 __func__, bdname.name, remote_name_len, device_type);
249 btif_dm_update_ble_remote_properties(bd_addr.address, bdname.name,
250 device_type);
251 }
252 }
253 }
254
255 dev_type = (bt_device_type_t)device_type;
256 BTIF_STORAGE_FILL_PROPERTY(&properties, BT_PROPERTY_TYPE_OF_DEVICE,
257 sizeof(dev_type), &dev_type);
258 btif_storage_set_remote_device_property(&(bd_addr), &properties);
259
260 btif_storage_set_remote_addr_type(&bd_addr, addr_type);
261
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -0800262 HAL_CBACK(bt_gatt_callbacks, scanner->scan_result_cb, ble_evt_type, addr_type,
263 &bd_addr, ble_primary_phy, ble_secondary_phy, ble_advertising_sid,
264 ble_tx_power, rssi, ble_periodic_adv_int, std::move(value));
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700265}
266
267void bta_scan_results_cb(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH* p_data) {
268 uint8_t len;
269
270 if (event == BTA_DM_INQ_CMPL_EVT) {
271 BTIF_TRACE_DEBUG("%s BLE observe complete. Num Resp %d", __func__,
272 p_data->inq_cmpl.num_resps);
273 return;
274 }
275
276 if (event != BTA_DM_INQ_RES_EVT) {
277 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event);
278 return;
279 }
280
281 vector<uint8_t> value(BTGATT_MAX_ATTR_LEN);
282 if (p_data->inq_res.p_eir) {
283 value.insert(value.begin(), p_data->inq_res.p_eir,
284 p_data->inq_res.p_eir + 62);
285
286 if (BTM_CheckEirData(p_data->inq_res.p_eir,
287 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &len)) {
288 p_data->inq_res.remt_name_not_required = true;
289 }
290 }
291
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -0800292 tBTA_DM_INQ_RES* r = &p_data->inq_res;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700293 bt_bdaddr_t bdaddr;
Jakub Pawlowski7de0f9b2017-01-27 08:06:20 -0800294 bdcpy(bdaddr.address, r->bd_addr);
295 do_in_jni_thread(Bind(bta_scan_results_cb_impl, bdaddr, r->device_type,
296 r->rssi, r->ble_addr_type, r->ble_evt_type,
297 r->ble_primary_phy, r->ble_secondary_phy,
298 r->ble_advertising_sid, r->ble_tx_power,
299 r->ble_periodic_adv_int, std::move(value)));
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700300}
301
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800302void bta_track_adv_event_cb(tBTM_BLE_TRACK_ADV_DATA* p_track_adv_data) {
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700303 btgatt_track_adv_info_t* btif_scan_track_cb = new btgatt_track_adv_info_t;
304
305 BTIF_TRACE_DEBUG("%s", __func__);
306 btif_gatt_move_track_adv_data(btif_scan_track_cb,
307 (btgatt_track_adv_info_t*)p_track_adv_data);
308
309 SCAN_CBACK_IN_JNI(track_adv_event_cb, Owned(btif_scan_track_cb));
310}
311
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800312class BleScannerInterfaceImpl : public BleScannerInterface {
313 ~BleScannerInterfaceImpl(){};
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700314
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800315 void RegisterScanner(RegisterCallback cb) override {
316 do_in_bta_thread(
317 FROM_HERE,
318 Bind(
319 [](RegisterCallback cb) {
320 BTA_GATTC_AppRegister(
321 bta_gatts_cback,
322 base::Bind(
323 [](RegisterCallback cb, uint8_t client_id,
324 uint8_t status) {
325 do_in_jni_thread(base::Bind(cb, client_id, status));
326 },
327 std::move(cb)));
328 },
329 std::move(cb)));
330 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700331
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800332 void Unregister(int scanner_id) override {
333 do_in_bta_thread(FROM_HERE, Bind(&BTA_GATTC_AppDeregister, scanner_id));
334 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700335
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800336 void Scan(bool start) override {
337 if (!start) {
338 do_in_bta_thread(FROM_HERE, Bind(&BTA_DmBleObserve, false, 0, nullptr));
339 return;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700340 }
341
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800342 btif_gattc_init_dev_cb();
343 do_in_bta_thread(FROM_HERE,
344 Bind(&BTA_DmBleObserve, true, 0,
345 (tBTA_DM_SEARCH_CBACK*)bta_scan_results_cb));
346 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700347
Jakub Pawlowski70984322016-12-16 10:43:49 -0800348 void ScanFilterParamSetup(
349 uint8_t client_if, uint8_t action, uint8_t filt_index,
350 std::unique_ptr<btgatt_filt_param_setup_t> filt_param) override {
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800351 BTIF_TRACE_DEBUG("%s", __func__);
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700352
Jakub Pawlowski2c1522e2017-01-03 06:36:19 -0800353 if (filt_param && filt_param->dely_mode == 1) {
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800354 do_in_bta_thread(
355 FROM_HERE, base::Bind(BTM_BleTrackAdvertiser, bta_track_adv_event_cb,
356 client_if));
Jakub Pawlowski2c1522e2017-01-03 06:36:19 -0800357 }
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800358
Jakub Pawlowskiad6afdf2017-01-04 06:50:46 -0800359 do_in_bta_thread(
360 FROM_HERE,
361 base::Bind(&BTM_BleAdvFilterParamSetup, action, filt_index,
362 base::Passed(&filt_param),
363 base::Bind(&bta_scan_filt_param_setup_cb, client_if)));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800364 }
365
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800366 void ScanFilterAddRemove(int client_if, int action, int filt_type,
367 int filt_index, int company_id, int company_id_mask,
368 const bt_uuid_t* p_uuid,
369 const bt_uuid_t* p_uuid_mask,
370 const bt_bdaddr_t* bd_addr, char addr_type,
371 vector<uint8_t> data,
372 vector<uint8_t> mask) override {
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800373 BTIF_TRACE_DEBUG("%s, %d, %d", __func__, action, filt_type);
374
375 /* If data is passed, both mask and data have to be the same length */
376 if (data.size() != mask.size() && data.size() != 0 && mask.size() != 0)
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800377 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800378
379 switch (filt_type) {
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800380 case BTM_BLE_PF_ADDR_FILTER: {
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800381 tBLE_BD_ADDR target_addr;
382 bdcpy(target_addr.bda, bd_addr->address);
383 target_addr.type = addr_type;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800384
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800385 do_in_bta_thread(
386 FROM_HERE,
387 base::Bind(&BTM_LE_PF_addr_filter, action, filt_index,
388 std::move(target_addr),
389 Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
390 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800391 }
392
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800393 case BTM_BLE_PF_SRVC_DATA:
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800394 do_in_bta_thread(FROM_HERE,
395 base::Bind(&BTM_LE_PF_srvc_data, action, filt_index));
396 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800397
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800398 case BTM_BLE_PF_SRVC_UUID:
399 case BTM_BLE_PF_SRVC_SOL_UUID: {
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800400 tBT_UUID bt_uuid;
401 btif_to_bta_uuid(&bt_uuid, p_uuid);
402
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800403 if (p_uuid_mask == NULL) {
404 do_in_bta_thread(
405 FROM_HERE,
406 base::Bind(&BTM_LE_PF_uuid_filter, action, filt_index, filt_type,
407 bt_uuid, BTM_BLE_PF_LOGIC_AND, nullptr,
408 Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
409 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800410 }
411
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800412 tBTM_BLE_PF_COND_MASK* mask = new tBTM_BLE_PF_COND_MASK;
413 btif_to_bta_uuid_mask(mask, p_uuid_mask, p_uuid);
414 do_in_bta_thread(
415 FROM_HERE,
416 base::Bind(&BTM_LE_PF_uuid_filter, action, filt_index, filt_type,
417 bt_uuid, BTM_BLE_PF_LOGIC_AND, base::Owned(mask),
418 Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
419 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800420 }
421
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800422 case BTM_BLE_PF_LOCAL_NAME: {
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800423 do_in_bta_thread(
424 FROM_HERE, base::Bind(&BTM_LE_PF_local_name, action, filt_index,
425 std::move(data), Bind(&bta_scan_filt_cfg_cb,
426 filt_type, client_if)));
427 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800428 }
429
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800430 case BTM_BLE_PF_MANU_DATA: {
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800431 do_in_bta_thread(
432 FROM_HERE,
433 base::Bind(&BTM_LE_PF_manu_data, action, filt_index, company_id,
434 company_id_mask, std::move(data), std::move(mask),
435 Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
436 return;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700437 }
438
Jakub Pawlowskie3960652016-12-29 01:23:52 -0800439 case BTM_BLE_PF_SRVC_DATA_PATTERN: {
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800440 do_in_bta_thread(
441 FROM_HERE,
442 base::Bind(&BTM_LE_PF_srvc_data_pattern, action, filt_index,
443 std::move(data), std::move(mask),
444 Bind(&bta_scan_filt_cfg_cb, filt_type, client_if)));
445 return;
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800446 }
447
448 default:
449 LOG_ERROR(LOG_TAG, "%s: Unknown filter type (%d)!", __func__, action);
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800450 return;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700451 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700452 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700453
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800454 void ScanFilterClear(int client_if, int filter_index) override {
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800455 BTIF_TRACE_DEBUG("%s: filter_index: %d", __func__, filter_index);
Jakub Pawlowski2ffd8da2017-01-04 07:58:37 -0800456 do_in_bta_thread(FROM_HERE,
457 base::Bind(&BTM_LE_PF_clear, filter_index,
458 Bind(&bta_scan_filt_cfg_cb, BTM_BLE_PF_TYPE_ALL,
459 client_if)));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800460 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700461
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800462 void ScanFilterEnable(int client_if, bool enable) override {
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800463 BTIF_TRACE_DEBUG("%s: enable: %d", __func__, enable);
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700464
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800465 uint8_t action = enable ? 1 : 0;
Jakub Pawlowski2c1522e2017-01-03 06:36:19 -0800466 do_in_bta_thread(
467 FROM_HERE, base::Bind(&BTM_BleEnableDisableFilterFeature, action,
468 base::Bind(&bta_scan_filt_status_cb, client_if)));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800469 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700470
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800471 void SetScanParameters(int client_if, int scan_interval,
472 int scan_window) override {
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800473 do_in_bta_thread(
474 FROM_HERE,
475 base::Bind(&BTM_BleSetScanParams, client_if, scan_interval, scan_window,
476 BTM_BLE_SCAN_MODE_ACTI, bta_scan_param_setup_cb));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800477 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700478
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800479 void BatchscanConfigStorage(int client_if, int batch_scan_full_max,
480 int batch_scan_trunc_max,
481 int batch_scan_notify_threshold) override {
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800482 base::Callback<void(uint8_t /* status */)> cb = base::Bind(
483 [](int client_if, uint8_t status) {
484 SCAN_CBACK_IN_JNI(batchscan_cfg_storage_cb, client_if, status);
485 },
486 client_if);
487
488 do_in_bta_thread(
489 FROM_HERE,
490 base::Bind(&BTM_BleSetStorageConfig, (uint8_t)batch_scan_full_max,
491 (uint8_t)batch_scan_trunc_max,
492 (uint8_t)batch_scan_notify_threshold, cb,
493 bta_batch_scan_threshold_cb, (tBTM_BLE_REF_VALUE)client_if));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800494 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700495
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800496 void BatchscanEnable(int client_if, int scan_mode, int scan_interval,
497 int scan_window, int addr_type,
498 int discard_rule) override {
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800499 auto cb = base::Bind(
500 [](int client_if, uint8_t status) {
501 SCAN_CBACK_IN_JNI(batchscan_enb_disable_cb, 1, client_if, status);
502 },
503 client_if);
504
505 do_in_bta_thread(
506 FROM_HERE, base::Bind(&BTM_BleEnableBatchScan, scan_mode, scan_interval,
507 scan_window, discard_rule, addr_type, cb));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800508 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700509
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800510 void BatchscanDisable(int client_if) override {
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800511 auto cb = base::Bind(
512 [](int client_if, uint8_t status) {
513 SCAN_CBACK_IN_JNI(batchscan_enb_disable_cb, 1, client_if, status);
514 },
515 client_if);
516
517 do_in_bta_thread(FROM_HERE, base::Bind(&BTM_BleDisableBatchScan, cb));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800518 }
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700519
Jakub Pawlowski808650d2016-12-17 17:08:15 -0800520 void BatchscanReadReports(int client_if, int scan_mode) override {
Jakub Pawlowskieab36512017-01-05 01:34:53 -0800521 do_in_bta_thread(FROM_HERE,
522 base::Bind(&BTM_BleReadScanReports, (uint8_t)scan_mode,
523 Bind(bta_batch_scan_reports_cb, client_if)));
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800524 }
525};
526
527BleScannerInterface* btLeScannerInstance = nullptr;
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700528
Myles Watson90088882016-11-15 16:33:22 -0800529} // namespace
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -0700530
Jakub Pawlowski83f1d962016-12-14 09:49:38 -0800531BleScannerInterface* get_ble_scanner_instance() {
532 if (btLeScannerInstance == nullptr)
533 btLeScannerInstance = new BleScannerInterfaceImpl();
534
535 return btLeScannerInstance;
536}