1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bt_shim_scanner"
18
19 #include "le_scanning_manager.h"
20
21 #include <base/functional/bind.h>
22 #include <base/logging.h>
23 #include <base/threading/thread.h>
24 #include <hardware/bluetooth.h>
25 #include <stdio.h>
26
27 #include <unordered_set>
28
29 #include "advertise_data_parser.h"
30 #include "btif/include/btif_common.h"
31 #include "hci/address.h"
32 #include "hci/le_scanning_manager.h"
33 #include "hci/msft.h"
34 #include "include/hardware/ble_scanner.h"
35 #include "main/shim/ble_scanner_interface_impl.h"
36 #include "main/shim/dumpsys.h"
37 #include "main/shim/entry.h"
38 #include "main/shim/helpers.h"
39 #include "main/shim/le_scanning_manager.h"
40 #include "main/shim/shim.h"
41 #include "stack/btm/btm_int_types.h"
42 #include "stack/include/btm_log_history.h"
43 #include "storage/device.h"
44 #include "storage/le_device.h"
45 #include "storage/storage_module.h"
46 #include "types/ble_address_with_type.h"
47 #include "types/bluetooth/uuid.h"
48 #include "types/raw_address.h"
49
50 using bluetooth::ToGdAddress;
51 using bluetooth::ToRawAddress;
52
53 extern tBTM_CB btm_cb;
54
55 namespace {
56 constexpr char kBtmLogTag[] = "SCAN";
57 constexpr uint16_t kAllowServiceDataFilter = 0x0040;
58 // Bit 8 for enable AD Type Check
59 constexpr uint16_t kAllowADTypeFilter = 0x100;
60 constexpr uint8_t kFilterLogicOr = 0x00;
61 constexpr uint8_t kFilterLogicAnd = 0x01;
62 constexpr uint8_t kLowestRssiValue = 129;
63 constexpr uint16_t kAllowAllFilter = 0x00;
64 constexpr uint16_t kListLogicOr = 0x01;
65
66 class DefaultScanningCallback : public ::ScanningCallbacks {
OnScannerRegistered(const bluetooth::Uuid app_uuid,uint8_t scanner_id,uint8_t status)67 void OnScannerRegistered(const bluetooth::Uuid app_uuid, uint8_t scanner_id,
68 uint8_t status) override {
69 LogUnused();
70 }
OnSetScannerParameterComplete(uint8_t scanner_id,uint8_t status)71 void OnSetScannerParameterComplete(uint8_t scanner_id,
72 uint8_t status) override {
73 LogUnused();
74 }
OnScanResult(uint16_t event_type,uint8_t address_type,RawAddress bda,uint8_t primary_phy,uint8_t secondary_phy,uint8_t advertising_sid,int8_t tx_power,int8_t rssi,uint16_t periodic_advertising_interval,std::vector<uint8_t> advertising_data)75 void OnScanResult(uint16_t event_type, uint8_t address_type, RawAddress bda,
76 uint8_t primary_phy, uint8_t secondary_phy,
77 uint8_t advertising_sid, int8_t tx_power, int8_t rssi,
78 uint16_t periodic_advertising_interval,
79 std::vector<uint8_t> advertising_data) override {
80 LogUnused();
81 }
OnTrackAdvFoundLost(AdvertisingTrackInfo advertising_track_info)82 void OnTrackAdvFoundLost(
83 AdvertisingTrackInfo advertising_track_info) override {
84 LogUnused();
85 }
OnBatchScanReports(int client_if,int status,int report_format,int num_records,std::vector<uint8_t> data)86 void OnBatchScanReports(int client_if, int status, int report_format,
87 int num_records, std::vector<uint8_t> data) override {
88 LogUnused();
89 }
OnBatchScanThresholdCrossed(int client_if)90 void OnBatchScanThresholdCrossed(int client_if) override { LogUnused(); }
OnPeriodicSyncStarted(int reg_id,uint8_t status,uint16_t sync_handle,uint8_t advertising_sid,uint8_t address_type,RawAddress address,uint8_t phy,uint16_t interval)91 void OnPeriodicSyncStarted(int reg_id, uint8_t status, uint16_t sync_handle,
92 uint8_t advertising_sid, uint8_t address_type,
93 RawAddress address, uint8_t phy,
94 uint16_t interval) override {
95 LogUnused();
96 };
OnPeriodicSyncReport(uint16_t sync_handle,int8_t tx_power,int8_t rssi,uint8_t status,std::vector<uint8_t> data)97 void OnPeriodicSyncReport(uint16_t sync_handle, int8_t tx_power, int8_t rssi,
98 uint8_t status,
99 std::vector<uint8_t> data) override {
100 LogUnused();
101 };
OnPeriodicSyncLost(uint16_t sync_handle)102 void OnPeriodicSyncLost(uint16_t sync_handle) override { LogUnused(); };
OnPeriodicSyncTransferred(int pa_source,uint8_t status,RawAddress address)103 void OnPeriodicSyncTransferred(int pa_source, uint8_t status,
104 RawAddress address) override {
105 LogUnused();
106 };
107
OnBigInfoReport(uint16_t sync_handle,bool encrypted)108 void OnBigInfoReport(uint16_t sync_handle, bool encrypted) override {LogUnused(); };
109
110 private:
LogUnused()111 static void LogUnused() {
112 LOG_WARN("BLE Scanning callbacks have not been registered");
113 }
114 } default_scanning_callback_;
115
116 } // namespace
117
118 ::ScanningCallbacks* bluetooth::shim::default_scanning_callback =
119 static_cast<::ScanningCallbacks*>(&default_scanning_callback_);
120 extern ::ScanningCallbacks* bluetooth::shim::default_scanning_callback;
121
122 extern tBTM_CB btm_cb;
123
124 void btm_ble_process_adv_pkt_cont_for_inquiry(
125 uint16_t event_type, tBLE_ADDR_TYPE address_type,
126 const RawAddress& raw_address, uint8_t primary_phy, uint8_t secondary_phy,
127 uint8_t advertising_sid, int8_t tx_power, int8_t rssi,
128 uint16_t periodic_adv_int, std::vector<uint8_t> advertising_data);
129
130 extern void btif_dm_update_ble_remote_properties(const RawAddress& bd_addr,
131 BD_NAME bd_name,
132 DEV_CLASS dev_class,
133 tBT_DEVICE_TYPE dev_type);
134
135 void btm_ble_process_adv_addr(RawAddress& raw_address,
136 tBLE_ADDR_TYPE* address_type);
137
138 extern bool btm_ble_get_appearance_as_cod(std::vector<uint8_t> const& data,
139 DEV_CLASS dev_class);
140
141 using bluetooth::shim::BleScannerInterfaceImpl;
142
Init()143 void BleScannerInterfaceImpl::Init() {
144 LOG_INFO("init BleScannerInterfaceImpl");
145 bluetooth::shim::GetScanning()->RegisterScanningCallback(this);
146
147 if (bluetooth::shim::GetMsftExtensionManager()) {
148 bluetooth::shim::GetMsftExtensionManager()->SetScanningCallback(this);
149 }
150 }
151
152 /** Registers a scanner with the stack */
RegisterScanner(const bluetooth::Uuid & uuid,RegisterCallback)153 void BleScannerInterfaceImpl::RegisterScanner(const bluetooth::Uuid& uuid,
154 RegisterCallback) {
155 LOG(INFO) << __func__ << " in shim layer";
156 auto app_uuid = bluetooth::hci::Uuid::From128BitBE(uuid.To128BitBE());
157 bluetooth::shim::GetScanning()->RegisterScanner(app_uuid);
158 }
159
160 /** Unregister a scanner from the stack */
Unregister(int scanner_id)161 void BleScannerInterfaceImpl::Unregister(int scanner_id) {
162 LOG(INFO) << __func__ << " in shim layer, scanner_id:" << scanner_id;
163 bluetooth::shim::GetScanning()->Unregister(scanner_id);
164 }
165
166 /** Start or stop LE device scanning */
Scan(bool start)167 void BleScannerInterfaceImpl::Scan(bool start) {
168 LOG(INFO) << __func__ << " in shim layer " << ((start) ? "started" : "stopped");
169 bluetooth::shim::GetScanning()->Scan(start);
170 if (start && !btm_cb.ble_ctr_cb.is_ble_observe_active()) {
171 btm_cb.neighbor.le_scan = {
172 .start_time_ms = timestamper_in_milliseconds.GetTimestamp(),
173 .results = 0,
174 };
175 BTM_LogHistory(kBtmLogTag, RawAddress::kEmpty, "Le scan started");
176 btm_cb.ble_ctr_cb.set_ble_observe_active();
177 } else if (!start && btm_cb.ble_ctr_cb.is_ble_observe_active()) {
178 // stopped
179 const unsigned long long duration_timestamp =
180 timestamper_in_milliseconds.GetTimestamp() -
181 btm_cb.neighbor.le_scan.start_time_ms;
182 BTM_LogHistory(kBtmLogTag, RawAddress::kEmpty, "Le scan stopped",
183 base::StringPrintf("duration_s:%6.3f results:%-3lu",
184 (double)duration_timestamp / 1000.0,
185 btm_cb.neighbor.le_scan.results));
186 btm_cb.ble_ctr_cb.reset_ble_observe();
187 btm_cb.neighbor.le_scan = {};
188 } else {
189 LOG_WARN("Invalid state: start:%d, current scan state: %d", start,
190 btm_cb.ble_ctr_cb.is_ble_observe_active());
191 return;
192 }
193
194 do_in_jni_thread(FROM_HERE,
195 base::Bind(&BleScannerInterfaceImpl::AddressCache::init,
196 base::Unretained(&address_cache_)));
197 }
198
199 /** Setup scan filter params */
ScanFilterParamSetup(uint8_t client_if,uint8_t action,uint8_t filter_index,std::unique_ptr<btgatt_filt_param_setup_t> filt_param,FilterParamSetupCallback cb)200 void BleScannerInterfaceImpl::ScanFilterParamSetup(
201 uint8_t client_if, uint8_t action, uint8_t filter_index,
202 std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
203 FilterParamSetupCallback cb) {
204 LOG(INFO) << __func__ << " in shim layer";
205
206 auto apcf_action = static_cast<bluetooth::hci::ApcfAction>(action);
207 bluetooth::hci::AdvertisingFilterParameter advertising_filter_parameter;
208
209 if (filt_param != nullptr) {
210 if (filt_param && filt_param->dely_mode == 1 &&
211 apcf_action == hci::ApcfAction::ADD) {
212 bluetooth::shim::GetScanning()->TrackAdvertiser(filter_index, client_if);
213 }
214 advertising_filter_parameter.feature_selection = filt_param->feat_seln;
215 advertising_filter_parameter.list_logic_type = filt_param->list_logic_type;
216 advertising_filter_parameter.filter_logic_type =
217 filt_param->filt_logic_type;
218 advertising_filter_parameter.rssi_high_thresh = filt_param->rssi_high_thres;
219 advertising_filter_parameter.delivery_mode =
220 static_cast<bluetooth::hci::DeliveryMode>(filt_param->dely_mode);
221 if (filt_param && filt_param->dely_mode == 1) {
222 advertising_filter_parameter.onfound_timeout = filt_param->found_timeout;
223 advertising_filter_parameter.onfound_timeout_cnt =
224 filt_param->found_timeout_cnt;
225 advertising_filter_parameter.rssi_low_thresh = filt_param->rssi_low_thres;
226 advertising_filter_parameter.onlost_timeout = filt_param->lost_timeout;
227 advertising_filter_parameter.num_of_tracking_entries =
228 filt_param->num_of_tracking_entries;
229 }
230 }
231
232 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
233 apcf_action, filter_index, advertising_filter_parameter);
234 // TODO refactor callback mechanism
235 do_in_jni_thread(FROM_HERE,
236 base::Bind(cb, 0, 0, btm_status_value(BTM_SUCCESS)));
237 }
238
239 /** Configure a scan filter condition */
ScanFilterAdd(int filter_index,std::vector<ApcfCommand> filters,FilterConfigCallback cb)240 void BleScannerInterfaceImpl::ScanFilterAdd(int filter_index,
241 std::vector<ApcfCommand> filters,
242 FilterConfigCallback cb) {
243 LOG(INFO) << __func__ << " in shim layer";
244 std::vector<bluetooth::hci::AdvertisingPacketContentFilterCommand>
245 new_filters = {};
246 for (size_t i = 0; i < filters.size(); i++) {
247 bluetooth::hci::AdvertisingPacketContentFilterCommand command{};
248 if (!parse_filter_command(command, filters[i])) {
249 LOG_ERROR("invalid apcf command");
250 return;
251 }
252 new_filters.push_back(command);
253 }
254 bluetooth::shim::GetScanning()->ScanFilterAdd(filter_index, new_filters);
255 do_in_jni_thread(FROM_HERE,
256 base::Bind(cb, 0, 0, 0, btm_status_value(BTM_SUCCESS)));
257 }
258
259 /** Clear all scan filter conditions for specific filter index*/
ScanFilterClear(int filter_index,FilterConfigCallback cb)260 void BleScannerInterfaceImpl::ScanFilterClear(int filter_index,
261 FilterConfigCallback cb) {
262 LOG(INFO) << __func__ << " in shim layer";
263 // This function doesn't used in java layer
264 }
265
266 /** Enable / disable scan filter feature*/
ScanFilterEnable(bool enable,EnableCallback cb)267 void BleScannerInterfaceImpl::ScanFilterEnable(bool enable, EnableCallback cb) {
268 LOG(INFO) << __func__ << " in shim layer";
269 bluetooth::shim::GetScanning()->ScanFilterEnable(enable);
270
271 uint8_t action = enable ? 1 : 0;
272 do_in_jni_thread(FROM_HERE,
273 base::Bind(cb, action, btm_status_value(BTM_SUCCESS)));
274 }
275
276 /** Is MSFT Extension supported? */
IsMsftSupported()277 bool BleScannerInterfaceImpl::IsMsftSupported() {
278 LOG_INFO("in shim layer");
279
280 return bluetooth::shim::GetMsftExtensionManager()->SupportsMsftExtensions();
281 }
282
283 /** Adds MSFT filter */
MsftAdvMonitorAdd(MsftAdvMonitor monitor,MsftAdvMonitorAddCallback cb)284 void BleScannerInterfaceImpl::MsftAdvMonitorAdd(MsftAdvMonitor monitor,
285 MsftAdvMonitorAddCallback cb) {
286 LOG_INFO("in shim layer");
287 msft_callbacks_.Add = cb;
288 bluetooth::shim::GetMsftExtensionManager()->MsftAdvMonitorAdd(
289 monitor, base::Bind(&BleScannerInterfaceImpl::OnMsftAdvMonitorAdd,
290 base::Unretained(this)));
291 }
292
293 /** Removes MSFT filter */
MsftAdvMonitorRemove(uint8_t monitor_handle,MsftAdvMonitorRemoveCallback cb)294 void BleScannerInterfaceImpl::MsftAdvMonitorRemove(
295 uint8_t monitor_handle, MsftAdvMonitorRemoveCallback cb) {
296 LOG_INFO("in shim layer");
297 msft_callbacks_.Remove = cb;
298 bluetooth::shim::GetMsftExtensionManager()->MsftAdvMonitorRemove(
299 monitor_handle,
300 base::Bind(&BleScannerInterfaceImpl::OnMsftAdvMonitorRemove,
301 base::Unretained(this)));
302 }
303
304 /** Enable / disable MSFT scan filter */
MsftAdvMonitorEnable(bool enable,MsftAdvMonitorEnableCallback cb)305 void BleScannerInterfaceImpl::MsftAdvMonitorEnable(
306 bool enable, MsftAdvMonitorEnableCallback cb) {
307 LOG_INFO("in shim layer");
308 msft_callbacks_.Enable = cb;
309 bluetooth::shim::GetMsftExtensionManager()->MsftAdvMonitorEnable(
310 enable, base::Bind(&BleScannerInterfaceImpl::OnMsftAdvMonitorEnable,
311 base::Unretained(this), enable));
312 }
313
314 /** Callback of adding MSFT filter */
OnMsftAdvMonitorAdd(uint8_t monitor_handle,bluetooth::hci::ErrorCode status)315 void BleScannerInterfaceImpl::OnMsftAdvMonitorAdd(
316 uint8_t monitor_handle, bluetooth::hci::ErrorCode status) {
317 LOG_INFO("in shim layer");
318 msft_callbacks_.Add.Run(monitor_handle, (uint8_t)status);
319 }
320
321 /** Callback of removing MSFT filter */
OnMsftAdvMonitorRemove(bluetooth::hci::ErrorCode status)322 void BleScannerInterfaceImpl::OnMsftAdvMonitorRemove(
323 bluetooth::hci::ErrorCode status) {
324 LOG_INFO("in shim layer");
325 msft_callbacks_.Remove.Run((uint8_t)status);
326 }
327
328 /** Callback of enabling / disabling MSFT scan filter */
OnMsftAdvMonitorEnable(bool enable,bluetooth::hci::ErrorCode status)329 void BleScannerInterfaceImpl::OnMsftAdvMonitorEnable(
330 bool enable, bluetooth::hci::ErrorCode status) {
331 LOG_INFO("in shim layer");
332
333 if (status == bluetooth::hci::ErrorCode::SUCCESS) {
334 bluetooth::shim::GetScanning()->SetScanFilterPolicy(
335 enable ? bluetooth::hci::LeScanningFilterPolicy::FILTER_ACCEPT_LIST_ONLY
336 : bluetooth::hci::LeScanningFilterPolicy::ACCEPT_ALL);
337 }
338
339 msft_callbacks_.Enable.Run((uint8_t)status);
340 }
341
342 /** Sets the LE scan interval and window in units of N*0.625 msec */
SetScanParameters(int scanner_id,int scan_interval,int scan_window,Callback cb)343 void BleScannerInterfaceImpl::SetScanParameters(int scanner_id,
344 int scan_interval,
345 int scan_window, Callback cb) {
346 LOG(INFO) << __func__ << " in shim layer";
347 tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
348 if (BTM_BLE_ISVALID_PARAM(scan_interval, BTM_BLE_SCAN_INT_MIN,
349 BTM_BLE_EXT_SCAN_INT_MAX) &&
350 BTM_BLE_ISVALID_PARAM(scan_window, BTM_BLE_SCAN_WIN_MIN,
351 BTM_BLE_EXT_SCAN_WIN_MAX)) {
352 p_cb->scan_type = BTM_BLE_SCAN_MODE_ACTI;
353 p_cb->scan_interval = scan_interval;
354 p_cb->scan_window = scan_window;
355 }
356
357 // use active scan
358 auto scan_type = static_cast<bluetooth::hci::LeScanType>(0x01);
359 bluetooth::shim::GetScanning()->SetScanParameters(scanner_id, scan_type,
360 scan_interval, scan_window);
361 }
362
363 /* Configure the batchscan storage */
BatchscanConfigStorage(int client_if,int batch_scan_full_max,int batch_scan_trunc_max,int batch_scan_notify_threshold,Callback cb)364 void BleScannerInterfaceImpl::BatchscanConfigStorage(
365 int client_if, int batch_scan_full_max, int batch_scan_trunc_max,
366 int batch_scan_notify_threshold, Callback cb) {
367 LOG(INFO) << __func__ << " in shim layer";
368 bluetooth::shim::GetScanning()->BatchScanConifgStorage(
369 batch_scan_full_max, batch_scan_trunc_max, batch_scan_notify_threshold,
370 client_if);
371 do_in_jni_thread(FROM_HERE, base::Bind(cb, btm_status_value(BTM_SUCCESS)));
372 }
373
374 /* Enable batchscan */
BatchscanEnable(int scan_mode,int scan_interval,int scan_window,int addr_type,int discard_rule,Callback cb)375 void BleScannerInterfaceImpl::BatchscanEnable(int scan_mode, int scan_interval,
376 int scan_window, int addr_type,
377 int discard_rule, Callback cb) {
378 LOG(INFO) << __func__ << " in shim layer";
379 auto batch_scan_mode = static_cast<bluetooth::hci::BatchScanMode>(scan_mode);
380 auto batch_scan_discard_rule =
381 static_cast<bluetooth::hci::BatchScanDiscardRule>(discard_rule);
382 bluetooth::shim::GetScanning()->BatchScanEnable(
383 batch_scan_mode, scan_window, scan_interval, batch_scan_discard_rule);
384 do_in_jni_thread(FROM_HERE, base::Bind(cb, btm_status_value(BTM_SUCCESS)));
385 }
386
387 /* Disable batchscan */
BatchscanDisable(Callback cb)388 void BleScannerInterfaceImpl::BatchscanDisable(Callback cb) {
389 LOG(INFO) << __func__ << " in shim layer";
390 bluetooth::shim::GetScanning()->BatchScanDisable();
391 do_in_jni_thread(FROM_HERE, base::Bind(cb, btm_status_value(BTM_SUCCESS)));
392 }
393
394 /* Read out batchscan reports */
BatchscanReadReports(int client_if,int scan_mode)395 void BleScannerInterfaceImpl::BatchscanReadReports(int client_if,
396 int scan_mode) {
397 LOG(INFO) << __func__ << " in shim layer";
398 auto batch_scan_mode = static_cast<bluetooth::hci::BatchScanMode>(scan_mode);
399 auto scanner_id = static_cast<bluetooth::hci::ScannerId>(client_if);
400 bluetooth::shim::GetScanning()->BatchScanReadReport(scanner_id,
401 batch_scan_mode);
402 }
403
404 bool btm_random_pseudo_to_identity_addr(RawAddress* random_pseudo,
405 tBLE_ADDR_TYPE* p_identity_addr_type);
406
407 bool btm_identity_addr_to_random_pseudo(RawAddress* bd_addr,
408 tBLE_ADDR_TYPE* p_addr_type,
409 bool refresh);
410
411 extern tACL_CONN* btm_acl_for_bda(const RawAddress& bd_addr,
412 tBT_TRANSPORT transport);
413
StartSync(uint8_t sid,RawAddress address,uint16_t skip,uint16_t timeout,int reg_id)414 void BleScannerInterfaceImpl::StartSync(uint8_t sid, RawAddress address,
415 uint16_t skip, uint16_t timeout,
416 int reg_id) {
417 LOG(INFO) << __func__ << " in shim layer";
418 tBLE_ADDR_TYPE address_type = BLE_ADDR_RANDOM;
419 tINQ_DB_ENT* p_i = btm_inq_db_find(address);
420 if (p_i) {
421 address_type = p_i->inq_info.results.ble_addr_type; // Random
422 }
423 btm_random_pseudo_to_identity_addr(&address, &address_type);
424 address_type &= ~BLE_ADDR_TYPE_ID_BIT;
425 bluetooth::shim::GetScanning()->StartSync(
426 sid, ToAddressWithType(address, address_type), skip, timeout, reg_id);
427 }
428
StopSync(uint16_t handle)429 void BleScannerInterfaceImpl::StopSync(uint16_t handle) {
430 LOG(INFO) << __func__ << " in shim layer";
431 bluetooth::shim::GetScanning()->StopSync(handle);
432 }
433
CancelCreateSync(uint8_t sid,RawAddress address)434 void BleScannerInterfaceImpl::CancelCreateSync(uint8_t sid,
435 RawAddress address) {
436 LOG(INFO) << __func__ << " in shim layer";
437 bluetooth::shim::GetScanning()->CancelCreateSync(sid, ToGdAddress(address));
438 }
439
TransferSync(RawAddress address,uint16_t service_data,uint16_t sync_handle,int pa_source)440 void BleScannerInterfaceImpl::TransferSync(RawAddress address,
441 uint16_t service_data,
442 uint16_t sync_handle,
443 int pa_source) {
444 LOG(INFO) << __func__ << " in shim layer";
445 tACL_CONN* p_acl = btm_acl_for_bda(address, BT_TRANSPORT_LE);
446 if (p_acl == NULL || !HCI_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECIPIENT(
447 p_acl->peer_le_features)) {
448 LOG_ERROR("[PAST] Remote doesn't support PAST");
449 scanning_callbacks_->OnPeriodicSyncTransferred(
450 pa_source, BTM_MODE_UNSUPPORTED, address);
451 return;
452 }
453 bluetooth::shim::GetScanning()->TransferSync(
454 ToGdAddress(address), service_data, sync_handle, pa_source);
455 }
456
TransferSetInfo(RawAddress address,uint16_t service_data,uint8_t adv_handle,int pa_source)457 void BleScannerInterfaceImpl::TransferSetInfo(RawAddress address,
458 uint16_t service_data,
459 uint8_t adv_handle,
460 int pa_source) {
461 LOG(INFO) << __func__ << " in shim layer";
462 tACL_CONN* p_acl = btm_acl_for_bda(address, BT_TRANSPORT_LE);
463 if (p_acl == NULL || !HCI_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECIPIENT(
464 p_acl->peer_le_features)) {
465 LOG_ERROR("[PAST] Remote doesn't support PAST");
466 scanning_callbacks_->OnPeriodicSyncTransferred(
467 pa_source, BTM_MODE_UNSUPPORTED, address);
468 return;
469 }
470 bluetooth::shim::GetScanning()->TransferSetInfo(
471 ToGdAddress(address), service_data, adv_handle, pa_source);
472 }
473
SyncTxParameters(RawAddress addr,uint8_t mode,uint16_t skip,uint16_t timeout,int reg_id)474 void BleScannerInterfaceImpl::SyncTxParameters(RawAddress addr, uint8_t mode,
475 uint16_t skip, uint16_t timeout,
476 int reg_id) {
477 LOG(INFO) << __func__ << " in shim layer";
478 bluetooth::shim::GetScanning()->SyncTxParameters(ToGdAddress(addr), mode,
479 skip, timeout, reg_id);
480 }
481
RegisterCallbacks(ScanningCallbacks * callbacks)482 void BleScannerInterfaceImpl::RegisterCallbacks(ScanningCallbacks* callbacks) {
483 LOG(INFO) << __func__ << " in shim layer";
484 scanning_callbacks_ = callbacks;
485 }
486
OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,bluetooth::hci::ScannerId scanner_id,ScanningStatus status)487 void BleScannerInterfaceImpl::OnScannerRegistered(
488 const bluetooth::hci::Uuid app_uuid, bluetooth::hci::ScannerId scanner_id,
489 ScanningStatus status) {
490 auto uuid = bluetooth::Uuid::From128BitBE(app_uuid.To128BitBE());
491 do_in_jni_thread(FROM_HERE,
492 base::Bind(&ScanningCallbacks::OnScannerRegistered,
493 base::Unretained(scanning_callbacks_), uuid,
494 scanner_id, status));
495 }
496
OnSetScannerParameterComplete(bluetooth::hci::ScannerId scanner_id,ScanningStatus status)497 void BleScannerInterfaceImpl::OnSetScannerParameterComplete(
498 bluetooth::hci::ScannerId scanner_id, ScanningStatus status) {
499 do_in_jni_thread(
500 FROM_HERE,
501 base::Bind(&ScanningCallbacks::OnSetScannerParameterComplete,
502 base::Unretained(scanning_callbacks_), scanner_id, status));
503 }
504
OnScanResult(uint16_t event_type,uint8_t address_type,bluetooth::hci::Address address,uint8_t primary_phy,uint8_t secondary_phy,uint8_t advertising_sid,int8_t tx_power,int8_t rssi,uint16_t periodic_advertising_interval,std::vector<uint8_t> advertising_data)505 void BleScannerInterfaceImpl::OnScanResult(
506 uint16_t event_type, uint8_t address_type, bluetooth::hci::Address address,
507 uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
508 int8_t tx_power, int8_t rssi, uint16_t periodic_advertising_interval,
509 std::vector<uint8_t> advertising_data) {
510 RawAddress raw_address = ToRawAddress(address);
511 tBLE_ADDR_TYPE ble_addr_type = to_ble_addr_type(address_type);
512
513 btm_cb.neighbor.le_scan.results++;
514 if (ble_addr_type != BLE_ADDR_ANONYMOUS) {
515 btm_ble_process_adv_addr(raw_address, &ble_addr_type);
516 }
517
518 do_in_jni_thread(
519 FROM_HERE,
520 base::BindOnce(&BleScannerInterfaceImpl::handle_remote_properties,
521 base::Unretained(this), raw_address, ble_addr_type,
522 advertising_data));
523
524 do_in_jni_thread(
525 FROM_HERE,
526 base::BindOnce(&ScanningCallbacks::OnScanResult,
527 base::Unretained(scanning_callbacks_), event_type,
528 static_cast<uint8_t>(address_type), raw_address,
529 primary_phy, secondary_phy, advertising_sid, tx_power,
530 rssi, periodic_advertising_interval, advertising_data));
531
532 // TODO: Remove when StartInquiry in GD part implemented
533 btm_ble_process_adv_pkt_cont_for_inquiry(
534 event_type, ble_addr_type, raw_address, primary_phy, secondary_phy,
535 advertising_sid, tx_power, rssi, periodic_advertising_interval,
536 advertising_data);
537 }
538
OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info)539 void BleScannerInterfaceImpl::OnTrackAdvFoundLost(
540 bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info) {
541 AdvertisingTrackInfo track_info = {};
542 RawAddress raw_address =
543 ToRawAddress(on_found_on_lost_info.advertiser_address);
544
545 if (on_found_on_lost_info.advertiser_address_type != BLE_ADDR_ANONYMOUS) {
546 btm_ble_process_adv_addr(raw_address,
547 &on_found_on_lost_info.advertiser_address_type);
548 }
549
550 track_info.monitor_handle = on_found_on_lost_info.monitor_handle;
551 track_info.advertiser_address = raw_address;
552 track_info.advertiser_address_type =
553 on_found_on_lost_info.advertiser_address_type;
554 track_info.scanner_id = on_found_on_lost_info.scanner_id;
555 track_info.filter_index = on_found_on_lost_info.filter_index;
556 track_info.advertiser_state = on_found_on_lost_info.advertiser_state;
557 track_info.advertiser_info_present =
558 static_cast<uint8_t>(on_found_on_lost_info.advertiser_info_present);
559 if (on_found_on_lost_info.advertiser_info_present ==
560 bluetooth::hci::AdvtInfoPresent::ADVT_INFO_PRESENT) {
561 track_info.tx_power = on_found_on_lost_info.tx_power;
562 track_info.rssi = on_found_on_lost_info.rssi;
563 track_info.time_stamp = on_found_on_lost_info.time_stamp;
564 auto adv_data = on_found_on_lost_info.adv_packet;
565 track_info.adv_packet_len = (uint8_t)adv_data.size();
566 track_info.adv_packet.reserve(adv_data.size());
567 track_info.adv_packet.insert(track_info.adv_packet.end(), adv_data.begin(),
568 adv_data.end());
569 auto scan_rsp_data = on_found_on_lost_info.scan_response;
570 track_info.scan_response_len = (uint8_t)scan_rsp_data.size();
571 track_info.scan_response.reserve(adv_data.size());
572 track_info.scan_response.insert(track_info.scan_response.end(),
573 scan_rsp_data.begin(), scan_rsp_data.end());
574 }
575 do_in_jni_thread(
576 FROM_HERE,
577 base::BindOnce(&ScanningCallbacks::OnTrackAdvFoundLost,
578 base::Unretained(scanning_callbacks_), track_info));
579 }
580
OnBatchScanReports(int client_if,int status,int report_format,int num_records,std::vector<uint8_t> data)581 void BleScannerInterfaceImpl::OnBatchScanReports(int client_if, int status,
582 int report_format,
583 int num_records,
584 std::vector<uint8_t> data) {
585 do_in_jni_thread(
586 FROM_HERE,
587 base::BindOnce(&ScanningCallbacks::OnBatchScanReports,
588 base::Unretained(scanning_callbacks_), client_if, status,
589 report_format, num_records, data));
590 }
591
OnBatchScanThresholdCrossed(int client_if)592 void BleScannerInterfaceImpl::OnBatchScanThresholdCrossed(int client_if) {
593 do_in_jni_thread(
594 FROM_HERE,
595 base::BindOnce(&ScanningCallbacks::OnBatchScanThresholdCrossed,
596 base::Unretained(scanning_callbacks_), client_if));
597 }
598
OnPeriodicSyncStarted(int reg_id,uint8_t status,uint16_t sync_handle,uint8_t advertising_sid,bluetooth::hci::AddressWithType address_with_type,uint8_t phy,uint16_t interval)599 void BleScannerInterfaceImpl::OnPeriodicSyncStarted(
600 int reg_id, uint8_t status, uint16_t sync_handle, uint8_t advertising_sid,
601 bluetooth::hci::AddressWithType address_with_type, uint8_t phy,
602 uint16_t interval) {
603 RawAddress raw_address = ToRawAddress(address_with_type.GetAddress());
604 tBLE_ADDR_TYPE ble_addr_type =
605 to_ble_addr_type((uint8_t)address_with_type.GetAddressType());
606 if (ble_addr_type & BLE_ADDR_TYPE_ID_BIT) {
607 btm_identity_addr_to_random_pseudo(&raw_address, &ble_addr_type, true);
608 }
609
610 do_in_jni_thread(FROM_HERE,
611 base::BindOnce(&ScanningCallbacks::OnPeriodicSyncStarted,
612 base::Unretained(scanning_callbacks_), reg_id,
613 status, sync_handle, advertising_sid,
614 static_cast<int>(ble_addr_type), raw_address,
615 phy, interval));
616 }
617
OnPeriodicSyncReport(uint16_t sync_handle,int8_t tx_power,int8_t rssi,uint8_t status,std::vector<uint8_t> data)618 void BleScannerInterfaceImpl::OnPeriodicSyncReport(uint16_t sync_handle,
619 int8_t tx_power, int8_t rssi,
620 uint8_t status,
621 std::vector<uint8_t> data) {
622 do_in_jni_thread(
623 FROM_HERE,
624 base::BindOnce(&ScanningCallbacks::OnPeriodicSyncReport,
625 base::Unretained(scanning_callbacks_), sync_handle,
626 tx_power, rssi, status, std::move(data)));
627 }
628
OnPeriodicSyncLost(uint16_t sync_handle)629 void BleScannerInterfaceImpl::OnPeriodicSyncLost(uint16_t sync_handle) {
630 do_in_jni_thread(
631 FROM_HERE,
632 base::BindOnce(&ScanningCallbacks::OnPeriodicSyncLost,
633 base::Unretained(scanning_callbacks_), sync_handle));
634 }
635
OnPeriodicSyncTransferred(int pa_source,uint8_t status,bluetooth::hci::Address address)636 void BleScannerInterfaceImpl::OnPeriodicSyncTransferred(
637 int pa_source, uint8_t status, bluetooth::hci::Address address) {
638 do_in_jni_thread(FROM_HERE,
639 base::BindOnce(&ScanningCallbacks::OnPeriodicSyncTransferred,
640 base::Unretained(scanning_callbacks_),
641 pa_source, status, ToRawAddress(address)));
642 }
643
OnBigInfoReport(uint16_t sync_handle,bool encrypted)644 void BleScannerInterfaceImpl::OnBigInfoReport(uint16_t sync_handle, bool encrypted) {
645 do_in_jni_thread(FROM_HERE,
646 base::BindOnce(&ScanningCallbacks::OnBigInfoReport,
647 base::Unretained(scanning_callbacks_), sync_handle, encrypted));
648 }
649
OnTimeout()650 void BleScannerInterfaceImpl::OnTimeout() {}
OnFilterEnable(bluetooth::hci::Enable enable,uint8_t status)651 void BleScannerInterfaceImpl::OnFilterEnable(bluetooth::hci::Enable enable,
652 uint8_t status) {}
OnFilterParamSetup(uint8_t available_spaces,bluetooth::hci::ApcfAction action,uint8_t status)653 void BleScannerInterfaceImpl::OnFilterParamSetup(
654 uint8_t available_spaces, bluetooth::hci::ApcfAction action,
655 uint8_t status) {}
OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type,uint8_t available_spaces,bluetooth::hci::ApcfAction action,uint8_t status)656 void BleScannerInterfaceImpl::OnFilterConfigCallback(
657 bluetooth::hci::ApcfFilterType filter_type, uint8_t available_spaces,
658 bluetooth::hci::ApcfAction action, uint8_t status) {}
659
parse_filter_command(bluetooth::hci::AdvertisingPacketContentFilterCommand & advertising_packet_content_filter_command,ApcfCommand apcf_command)660 bool BleScannerInterfaceImpl::parse_filter_command(
661 bluetooth::hci::AdvertisingPacketContentFilterCommand&
662 advertising_packet_content_filter_command,
663 ApcfCommand apcf_command) {
664 advertising_packet_content_filter_command.filter_type =
665 static_cast<bluetooth::hci::ApcfFilterType>(apcf_command.type);
666 bluetooth::hci::Address address = ToGdAddress(apcf_command.address);
667 advertising_packet_content_filter_command.address = address;
668 advertising_packet_content_filter_command.application_address_type =
669 static_cast<bluetooth::hci::ApcfApplicationAddressType>(
670 apcf_command.addr_type);
671
672 if (!apcf_command.uuid.IsEmpty()) {
673 uint8_t uuid_len = apcf_command.uuid.GetShortestRepresentationSize();
674 switch (uuid_len) {
675 case bluetooth::Uuid::kNumBytes16: {
676 advertising_packet_content_filter_command.uuid =
677 bluetooth::hci::Uuid::From16Bit(apcf_command.uuid.As16Bit());
678 } break;
679 case bluetooth::Uuid::kNumBytes32: {
680 advertising_packet_content_filter_command.uuid =
681 bluetooth::hci::Uuid::From32Bit(apcf_command.uuid.As32Bit());
682 } break;
683 case bluetooth::Uuid::kNumBytes128: {
684 advertising_packet_content_filter_command.uuid =
685 bluetooth::hci::Uuid::From128BitBE(apcf_command.uuid.To128BitBE());
686 } break;
687 default:
688 LOG_WARN("illegal UUID length %d", (uint16_t)uuid_len);
689 return false;
690 }
691 }
692
693 if (!apcf_command.uuid_mask.IsEmpty()) {
694 uint8_t uuid_len = apcf_command.uuid.GetShortestRepresentationSize();
695 switch (uuid_len) {
696 case bluetooth::Uuid::kNumBytes16: {
697 advertising_packet_content_filter_command.uuid_mask =
698 bluetooth::hci::Uuid::From16Bit(apcf_command.uuid_mask.As16Bit());
699 } break;
700 case bluetooth::Uuid::kNumBytes32: {
701 advertising_packet_content_filter_command.uuid_mask =
702 bluetooth::hci::Uuid::From32Bit(apcf_command.uuid_mask.As32Bit());
703 } break;
704 case bluetooth::Uuid::kNumBytes128: {
705 advertising_packet_content_filter_command.uuid_mask =
706 bluetooth::hci::Uuid::From128BitBE(
707 apcf_command.uuid_mask.To128BitBE());
708 } break;
709 default:
710 LOG_WARN("illegal UUID length %d", (uint16_t)uuid_len);
711 return false;
712 }
713 }
714
715 advertising_packet_content_filter_command.name.assign(
716 apcf_command.name.begin(), apcf_command.name.end());
717 advertising_packet_content_filter_command.company = apcf_command.company;
718 advertising_packet_content_filter_command.company_mask =
719 apcf_command.company_mask;
720 advertising_packet_content_filter_command.ad_type = apcf_command.ad_type;
721 advertising_packet_content_filter_command.org_id = apcf_command.org_id;
722 advertising_packet_content_filter_command.tds_flags = apcf_command.tds_flags;
723 advertising_packet_content_filter_command.tds_flags_mask =
724 apcf_command.tds_flags_mask;
725 advertising_packet_content_filter_command.meta_data_type =
726 static_cast<bluetooth::hci::ApcfMetaDataType>(
727 apcf_command.meta_data_type);
728 advertising_packet_content_filter_command.meta_data.assign(
729 apcf_command.meta_data.begin(), apcf_command.meta_data.end());
730 advertising_packet_content_filter_command.data.assign(
731 apcf_command.data.begin(), apcf_command.data.end());
732 advertising_packet_content_filter_command.data_mask.assign(
733 apcf_command.data_mask.begin(), apcf_command.data_mask.end());
734 advertising_packet_content_filter_command.irk = apcf_command.irk;
735 return true;
736 }
737
handle_remote_properties(RawAddress bd_addr,tBLE_ADDR_TYPE addr_type,std::vector<uint8_t> advertising_data)738 void BleScannerInterfaceImpl::handle_remote_properties(
739 RawAddress bd_addr, tBLE_ADDR_TYPE addr_type,
740 std::vector<uint8_t> advertising_data) {
741 if (!bluetooth::shim::is_gd_stack_started_up()) {
742 LOG_WARN("Gd stack is stopped, return");
743 return;
744 }
745
746 // skip anonymous advertisment
747 if (addr_type == BLE_ADDR_ANONYMOUS) {
748 return;
749 }
750
751 auto device_type = bluetooth::hci::DeviceType::LE;
752 uint8_t flag_len;
753 const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType(
754 advertising_data, BTM_BLE_AD_TYPE_FLAG, &flag_len);
755
756 if (p_flag != NULL && flag_len != 0) {
757 if ((BTM_BLE_BREDR_NOT_SPT & *p_flag) == 0) {
758 device_type = bluetooth::hci::DeviceType::DUAL;
759 }
760 }
761
762 uint8_t remote_name_len;
763 const uint8_t* p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
764 advertising_data, HCI_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
765
766 if (p_eir_remote_name == NULL) {
767 p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
768 advertising_data, HCI_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
769 }
770
771 bt_bdname_t bdname = {0};
772
773 // update device name
774 if (p_eir_remote_name) {
775 if (!address_cache_.find(bd_addr)) {
776 address_cache_.add(bd_addr);
777
778 if (p_eir_remote_name) {
779 if (remote_name_len > BD_NAME_LEN + 1 ||
780 (remote_name_len == BD_NAME_LEN + 1 &&
781 p_eir_remote_name[BD_NAME_LEN] != '\0')) {
782 LOG_INFO("%s dropping invalid packet - device name too long: %d",
783 __func__, remote_name_len);
784 return;
785 }
786
787 memcpy(bdname.name, p_eir_remote_name, remote_name_len);
788 if (remote_name_len < BD_NAME_LEN + 1)
789 bdname.name[remote_name_len] = '\0';
790 btif_dm_update_ble_remote_properties(bd_addr, bdname.name, NULL,
791 device_type);
792 }
793 }
794 }
795
796 DEV_CLASS dev_class;
797 if (btm_ble_get_appearance_as_cod(advertising_data, dev_class)) {
798 btif_dm_update_ble_remote_properties(bd_addr, bdname.name, dev_class,
799 device_type);
800 }
801
802 auto* storage_module = bluetooth::shim::GetStorage();
803 bluetooth::hci::Address address = ToGdAddress(bd_addr);
804
805 // update device type
806 auto mutation = storage_module->Modify();
807 bluetooth::storage::Device device =
808 storage_module->GetDeviceByLegacyKey(address);
809 mutation.Add(device.SetDeviceType(device_type));
810 mutation.Commit();
811
812 // update address type
813 auto mutation2 = storage_module->Modify();
814 bluetooth::storage::LeDevice le_device = device.Le();
815 mutation2.Add(
816 le_device.SetAddressType((bluetooth::hci::AddressType)addr_type));
817 mutation2.Commit();
818 }
819
add(const RawAddress & p_bda)820 void BleScannerInterfaceImpl::AddressCache::add(const RawAddress& p_bda) {
821 // Remove the oldest entries
822 while (remote_bdaddr_cache_.size() >= remote_bdaddr_cache_max_size_) {
823 const RawAddress& raw_address = remote_bdaddr_cache_ordered_.front();
824 remote_bdaddr_cache_.erase(raw_address);
825 remote_bdaddr_cache_ordered_.pop();
826 }
827 remote_bdaddr_cache_.insert(p_bda);
828 remote_bdaddr_cache_ordered_.push(p_bda);
829 }
830
find(const RawAddress & p_bda)831 bool BleScannerInterfaceImpl::AddressCache::find(const RawAddress& p_bda) {
832 return (remote_bdaddr_cache_.find(p_bda) != remote_bdaddr_cache_.end());
833 }
834
init(void)835 void BleScannerInterfaceImpl::AddressCache::init(void) {
836 remote_bdaddr_cache_.clear();
837 remote_bdaddr_cache_ordered_ = {};
838 }
839
840 BleScannerInterfaceImpl* bt_le_scanner_instance = nullptr;
841
get_ble_scanner_instance()842 BleScannerInterface* bluetooth::shim::get_ble_scanner_instance() {
843 if (bt_le_scanner_instance == nullptr) {
844 bt_le_scanner_instance = new BleScannerInterfaceImpl();
845 }
846 return bt_le_scanner_instance;
847 }
848
init_scanning_manager()849 void bluetooth::shim::init_scanning_manager() {
850 static_cast<BleScannerInterfaceImpl*>(
851 bluetooth::shim::get_ble_scanner_instance())
852 ->Init();
853 }
854
is_ad_type_filter_supported()855 bool bluetooth::shim::is_ad_type_filter_supported() {
856 return bluetooth::shim::GetScanning()->IsAdTypeFilterSupported();
857 }
858
set_ad_type_rsi_filter(bool enable)859 void bluetooth::shim::set_ad_type_rsi_filter(bool enable) {
860 bluetooth::hci::AdvertisingFilterParameter advertising_filter_parameter;
861 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
862 bluetooth::hci::ApcfAction::DELETE, 0x00, advertising_filter_parameter);
863 if (enable) {
864 std::vector<bluetooth::hci::AdvertisingPacketContentFilterCommand> filters =
865 {};
866 bluetooth::hci::AdvertisingPacketContentFilterCommand filter{};
867 filter.filter_type = bluetooth::hci::ApcfFilterType::AD_TYPE;
868 filter.ad_type = BTM_BLE_AD_TYPE_RSI;
869 filters.push_back(filter);
870 bluetooth::shim::GetScanning()->ScanFilterAdd(0x00, filters);
871
872 advertising_filter_parameter.delivery_mode =
873 bluetooth::hci::DeliveryMode::IMMEDIATE;
874 advertising_filter_parameter.feature_selection = kAllowADTypeFilter;
875 advertising_filter_parameter.list_logic_type = kAllowADTypeFilter;
876 advertising_filter_parameter.filter_logic_type = kFilterLogicOr;
877 advertising_filter_parameter.rssi_high_thresh = kLowestRssiValue;
878 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
879 bluetooth::hci::ApcfAction::ADD, 0x00, advertising_filter_parameter);
880 }
881 }
882
set_empty_filter(bool enable)883 void bluetooth::shim::set_empty_filter(bool enable) {
884 bluetooth::hci::AdvertisingFilterParameter advertising_filter_parameter;
885 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
886 bluetooth::hci::ApcfAction::DELETE, 0x00, advertising_filter_parameter);
887 if (enable) {
888 /* Add an allow-all filter on index 0 */
889 advertising_filter_parameter.delivery_mode =
890 bluetooth::hci::DeliveryMode::IMMEDIATE;
891 advertising_filter_parameter.feature_selection = kAllowAllFilter;
892 advertising_filter_parameter.list_logic_type = kListLogicOr;
893 advertising_filter_parameter.filter_logic_type = kFilterLogicOr;
894 advertising_filter_parameter.rssi_high_thresh = kLowestRssiValue;
895 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
896 bluetooth::hci::ApcfAction::ADD, 0x00, advertising_filter_parameter);
897 }
898 }
899
set_target_announcements_filter(bool enable)900 void bluetooth::shim::set_target_announcements_filter(bool enable) {
901 uint8_t filter_index = 0x03;
902
903 LOG_DEBUG(" enable %d", enable);
904
905 bluetooth::hci::AdvertisingFilterParameter advertising_filter_parameter = {};
906 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
907 bluetooth::hci::ApcfAction::DELETE, filter_index,
908 advertising_filter_parameter);
909
910 if (!enable) return;
911
912 advertising_filter_parameter.delivery_mode =
913 bluetooth::hci::DeliveryMode::IMMEDIATE;
914 advertising_filter_parameter.feature_selection = kAllowServiceDataFilter;
915 advertising_filter_parameter.list_logic_type = kListLogicOr;
916 advertising_filter_parameter.filter_logic_type = kFilterLogicAnd;
917 advertising_filter_parameter.rssi_high_thresh = kLowestRssiValue;
918
919 /* Add targeted announcements filter on index 4 */
920 std::vector<bluetooth::hci::AdvertisingPacketContentFilterCommand>
921 cap_bap_filter = {};
922
923 bluetooth::hci::AdvertisingPacketContentFilterCommand cap_filter{};
924 cap_filter.filter_type = bluetooth::hci::ApcfFilterType::SERVICE_DATA;
925 cap_filter.data = {0x53, 0x18, 0x01};
926 cap_filter.data_mask = {0x53, 0x18, 0xFF};
927 cap_bap_filter.push_back(cap_filter);
928
929 bluetooth::hci::AdvertisingPacketContentFilterCommand bap_filter{};
930 bap_filter.filter_type = bluetooth::hci::ApcfFilterType::SERVICE_DATA;
931 bap_filter.data = {0x4e, 0x18, 0x01};
932 bap_filter.data_mask = {0x4e, 0x18, 0xFF};
933
934 cap_bap_filter.push_back(bap_filter);
935 bluetooth::shim::GetScanning()->ScanFilterAdd(filter_index, cap_bap_filter);
936
937 bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
938 bluetooth::hci::ApcfAction::ADD, filter_index,
939 advertising_filter_parameter);
940 }
941