Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 35 | #include <hardware/bt_gatt.h> |
| 36 | |
| 37 | #include "bta_api.h" |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 38 | #include "bta_closure_api.h" |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 39 | #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 | |
| 48 | using base::Bind; |
| 49 | using base::Owned; |
| 50 | using std::vector; |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 51 | using RegisterCallback = BleScannerInterface::RegisterCallback; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 52 | |
| 53 | extern bt_status_t do_in_jni_thread(const base::Closure& task); |
| 54 | extern const btgatt_callbacks_t* bt_gatt_callbacks; |
| 55 | |
Myles Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 56 | #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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 65 | } while (0) |
| 66 | |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 67 | namespace std { |
| 68 | template <> |
| 69 | struct 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 | |
| 76 | template <> |
| 77 | struct 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 | |
| 84 | namespace { |
| 85 | |
| 86 | std::unordered_set<bt_bdaddr_t> p_dev_cb; |
| 87 | |
| 88 | void 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 | |
| 94 | bool 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 | |
| 100 | void btif_gattc_init_dev_cb(void) { p_dev_cb.clear(); } |
| 101 | |
| 102 | btgattc_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 | |
| 156 | void 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 161 | 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 Pawlowski | 221e9bf | 2016-12-15 14:35:15 -0800 | [diff] [blame] | 171 | LOG_DEBUG(LOG_TAG, "%s: Unhandled event (%d)", __func__, event); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void 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 | |
| 183 | void 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 Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 185 | btif_gattc_translate_btm_status(status)); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jakub Pawlowski | 1febda9 | 2016-12-30 06:38:29 -0800 | [diff] [blame] | 188 | void 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 Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 192 | avbl_space); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Jakub Pawlowski | 1febda9 | 2016-12-30 06:38:29 -0800 | [diff] [blame] | 195 | void bta_scan_filt_param_setup_cb(tBTM_BLE_REF_VALUE ref_value, |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 196 | tBTM_BLE_PF_AVBL_SPACE avbl_space, |
Jakub Pawlowski | 1febda9 | 2016-12-30 06:38:29 -0800 | [diff] [blame] | 197 | uint8_t action_type, tBTA_STATUS status) { |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 198 | SCAN_CBACK_IN_JNI(scan_filter_param_cb, action_type, ref_value, status, |
Myles Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 199 | avbl_space); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jakub Pawlowski | 1febda9 | 2016-12-30 06:38:29 -0800 | [diff] [blame] | 202 | void bta_scan_filt_status_cb(tBTM_BLE_REF_VALUE ref_value, uint8_t action, |
| 203 | tBTA_STATUS status) { |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 204 | SCAN_CBACK_IN_JNI(scan_filter_status_cb, action, ref_value, status); |
| 205 | } |
| 206 | |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 207 | void bta_batch_scan_threshold_cb(tBTM_BLE_REF_VALUE ref_value) { |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 208 | SCAN_CBACK_IN_JNI(batchscan_threshold_cb, ref_value); |
| 209 | } |
| 210 | |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 211 | void bta_batch_scan_reports_cb(int client_id, tBTA_STATUS status, |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 212 | uint8_t report_format, uint8_t num_records, |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 213 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void bta_scan_results_cb_impl(bt_bdaddr_t bd_addr, tBT_DEVICE_TYPE device_type, |
| 219 | int8_t rssi, uint8_t addr_type, |
Jakub Pawlowski | 7de0f9b | 2017-01-27 08:06:20 -0800 | [diff] [blame^] | 220 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 224 | 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 Pawlowski | 7de0f9b | 2017-01-27 08:06:20 -0800 | [diff] [blame^] | 262 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void 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 Pawlowski | 7de0f9b | 2017-01-27 08:06:20 -0800 | [diff] [blame^] | 292 | tBTA_DM_INQ_RES* r = &p_data->inq_res; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 293 | bt_bdaddr_t bdaddr; |
Jakub Pawlowski | 7de0f9b | 2017-01-27 08:06:20 -0800 | [diff] [blame^] | 294 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 302 | void bta_track_adv_event_cb(tBTM_BLE_TRACK_ADV_DATA* p_track_adv_data) { |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 303 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 312 | class BleScannerInterfaceImpl : public BleScannerInterface { |
| 313 | ~BleScannerInterfaceImpl(){}; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 314 | |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 315 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 331 | |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 332 | void Unregister(int scanner_id) override { |
| 333 | do_in_bta_thread(FROM_HERE, Bind(&BTA_GATTC_AppDeregister, scanner_id)); |
| 334 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 335 | |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 336 | void Scan(bool start) override { |
| 337 | if (!start) { |
| 338 | do_in_bta_thread(FROM_HERE, Bind(&BTA_DmBleObserve, false, 0, nullptr)); |
| 339 | return; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 342 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 347 | |
Jakub Pawlowski | 7098432 | 2016-12-16 10:43:49 -0800 | [diff] [blame] | 348 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 351 | BTIF_TRACE_DEBUG("%s", __func__); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 352 | |
Jakub Pawlowski | 2c1522e | 2017-01-03 06:36:19 -0800 | [diff] [blame] | 353 | if (filt_param && filt_param->dely_mode == 1) { |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 354 | do_in_bta_thread( |
| 355 | FROM_HERE, base::Bind(BTM_BleTrackAdvertiser, bta_track_adv_event_cb, |
| 356 | client_if)); |
Jakub Pawlowski | 2c1522e | 2017-01-03 06:36:19 -0800 | [diff] [blame] | 357 | } |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 358 | |
Jakub Pawlowski | ad6afdf | 2017-01-04 06:50:46 -0800 | [diff] [blame] | 359 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 366 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 373 | 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 Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 377 | return; |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 378 | |
| 379 | switch (filt_type) { |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 380 | case BTM_BLE_PF_ADDR_FILTER: { |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 381 | tBLE_BD_ADDR target_addr; |
| 382 | bdcpy(target_addr.bda, bd_addr->address); |
| 383 | target_addr.type = addr_type; |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 384 | |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 385 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 393 | case BTM_BLE_PF_SRVC_DATA: |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 394 | do_in_bta_thread(FROM_HERE, |
| 395 | base::Bind(&BTM_LE_PF_srvc_data, action, filt_index)); |
| 396 | return; |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 397 | |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 398 | case BTM_BLE_PF_SRVC_UUID: |
| 399 | case BTM_BLE_PF_SRVC_SOL_UUID: { |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 400 | tBT_UUID bt_uuid; |
| 401 | btif_to_bta_uuid(&bt_uuid, p_uuid); |
| 402 | |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 403 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 412 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 422 | case BTM_BLE_PF_LOCAL_NAME: { |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 423 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 430 | case BTM_BLE_PF_MANU_DATA: { |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 431 | 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 Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Jakub Pawlowski | e396065 | 2016-12-29 01:23:52 -0800 | [diff] [blame] | 439 | case BTM_BLE_PF_SRVC_DATA_PATTERN: { |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 440 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | default: |
| 449 | LOG_ERROR(LOG_TAG, "%s: Unknown filter type (%d)!", __func__, action); |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 450 | return; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 451 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 452 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 453 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 454 | void ScanFilterClear(int client_if, int filter_index) override { |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 455 | BTIF_TRACE_DEBUG("%s: filter_index: %d", __func__, filter_index); |
Jakub Pawlowski | 2ffd8da | 2017-01-04 07:58:37 -0800 | [diff] [blame] | 456 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 460 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 461 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 462 | void ScanFilterEnable(int client_if, bool enable) override { |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 463 | BTIF_TRACE_DEBUG("%s: enable: %d", __func__, enable); |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 464 | |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 465 | uint8_t action = enable ? 1 : 0; |
Jakub Pawlowski | 2c1522e | 2017-01-03 06:36:19 -0800 | [diff] [blame] | 466 | do_in_bta_thread( |
| 467 | FROM_HERE, base::Bind(&BTM_BleEnableDisableFilterFeature, action, |
| 468 | base::Bind(&bta_scan_filt_status_cb, client_if))); |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 469 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 470 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 471 | void SetScanParameters(int client_if, int scan_interval, |
| 472 | int scan_window) override { |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 473 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 477 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 478 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 479 | void BatchscanConfigStorage(int client_if, int batch_scan_full_max, |
| 480 | int batch_scan_trunc_max, |
| 481 | int batch_scan_notify_threshold) override { |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 482 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 494 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 495 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 496 | void BatchscanEnable(int client_if, int scan_mode, int scan_interval, |
| 497 | int scan_window, int addr_type, |
| 498 | int discard_rule) override { |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 499 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 508 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 509 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 510 | void BatchscanDisable(int client_if) override { |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 511 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 518 | } |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 519 | |
Jakub Pawlowski | 808650d | 2016-12-17 17:08:15 -0800 | [diff] [blame] | 520 | void BatchscanReadReports(int client_if, int scan_mode) override { |
Jakub Pawlowski | eab3651 | 2017-01-05 01:34:53 -0800 | [diff] [blame] | 521 | 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 Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 524 | } |
| 525 | }; |
| 526 | |
| 527 | BleScannerInterface* btLeScannerInstance = nullptr; |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 528 | |
Myles Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 529 | } // namespace |
Jakub Pawlowski | c3f6a51 | 2016-10-27 11:49:40 -0700 | [diff] [blame] | 530 | |
Jakub Pawlowski | 83f1d96 | 2016-12-14 09:49:38 -0800 | [diff] [blame] | 531 | BleScannerInterface* get_ble_scanner_instance() { |
| 532 | if (btLeScannerInstance == nullptr) |
| 533 | btLeScannerInstance = new BleScannerInterfaceImpl(); |
| 534 | |
| 535 | return btLeScannerInstance; |
| 536 | } |