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/bind.h>
22 #include <base/threading/thread.h>
23 #include <hardware/bluetooth.h>
24 #include <stdio.h>
25 #include <unordered_set>
26 
27 #include "btif/include/btif_common.h"
28 #include "gd/hci/address.h"
29 #include "gd/hci/le_scanning_manager.h"
30 #include "gd/storage/device.h"
31 #include "gd/storage/le_device.h"
32 #include "gd/storage/storage_module.h"
33 #include "main/shim/entry.h"
34 #include "main/shim/helpers.h"
35 #include "main/shim/shim.h"
36 
37 #include "advertise_data_parser.h"
38 #include "stack/btm/btm_int_types.h"
39 
40 using bluetooth::ToRawAddress;
41 using bluetooth::ToGdAddress;
42 
43 extern void btm_ble_process_adv_pkt_cont_for_inquiry(
44     uint16_t event_type, uint8_t address_type, const RawAddress& raw_address,
45     uint8_t primary_phy, uint8_t secondary_phy, uint8_t advertising_sid,
46     int8_t tx_power, int8_t rssi, uint16_t periodic_adv_int,
47     std::vector<uint8_t> advertising_data);
48 
49 extern void btif_dm_update_ble_remote_properties(const RawAddress& bd_addr,
50                                                  BD_NAME bd_name,
51                                                  tBT_DEVICE_TYPE dev_type);
52 
53 class BleScannerInterfaceImpl : public BleScannerInterface,
54                                 public bluetooth::hci::ScanningCallback {
55  public:
~BleScannerInterfaceImpl()56   ~BleScannerInterfaceImpl() override{};
57 
Init()58   void Init() {
59     LOG_INFO("init BleScannerInterfaceImpl");
60     bluetooth::shim::GetScanning()->RegisterScanningCallback(this);
61   }
62 
63   /** Registers a scanner with the stack */
RegisterScanner(const bluetooth::Uuid & uuid,RegisterCallback)64   void RegisterScanner(const bluetooth::Uuid& uuid, RegisterCallback) {
65     LOG(INFO) << __func__ << " in shim layer";
66     auto app_uuid = bluetooth::hci::Uuid::From128BitBE(uuid.To128BitBE());
67     bluetooth::shim::GetScanning()->RegisterScanner(app_uuid);
68   }
69 
70   /** Unregister a scanner from the stack */
Unregister(int scanner_id)71   void Unregister(int scanner_id) {
72     LOG(INFO) << __func__ << " in shim layer, scanner_id:" << scanner_id;
73     bluetooth::shim::GetScanning()->Unregister(scanner_id);
74   }
75 
76   /** Start or stop LE device scanning */
Scan(bool start)77   void Scan(bool start) {
78     LOG(INFO) << __func__ << " in shim layer";
79     bluetooth::shim::GetScanning()->Scan(start);
80     init_address_cache();
81   }
82 
83   /** 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)84   void ScanFilterParamSetup(
85       uint8_t client_if, uint8_t action, uint8_t filter_index,
86       std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
87       FilterParamSetupCallback cb) {
88     LOG(INFO) << __func__ << " in shim layer";
89 
90     auto apcf_action = static_cast<bluetooth::hci::ApcfAction>(action);
91     bluetooth::hci::AdvertisingFilterParameter advertising_filter_parameter;
92 
93     if (filt_param != nullptr) {
94       if (filt_param && filt_param->dely_mode == 1) {
95         bluetooth::shim::GetScanning()->TrackAdvertiser(client_if);
96       }
97       advertising_filter_parameter.feature_selection = filt_param->feat_seln;
98       advertising_filter_parameter.list_logic_type =
99           filt_param->list_logic_type;
100       advertising_filter_parameter.filter_logic_type =
101           filt_param->filt_logic_type;
102       advertising_filter_parameter.rssi_high_thresh =
103           filt_param->rssi_high_thres;
104       advertising_filter_parameter.delivery_mode =
105           static_cast<bluetooth::hci::DeliveryMode>(filt_param->dely_mode);
106       if (filt_param && filt_param->dely_mode == 1) {
107         advertising_filter_parameter.onfound_timeout =
108             filt_param->found_timeout;
109         advertising_filter_parameter.onfound_timeout_cnt =
110             filt_param->found_timeout_cnt;
111         advertising_filter_parameter.rssi_low_thres =
112             filt_param->rssi_low_thres;
113         advertising_filter_parameter.onlost_timeout = filt_param->lost_timeout;
114         advertising_filter_parameter.num_of_tracking_entries =
115             filt_param->num_of_tracking_entries;
116       }
117     }
118 
119     bluetooth::shim::GetScanning()->ScanFilterParameterSetup(
120         apcf_action, filter_index, advertising_filter_parameter);
121     // TODO refactor callback mechanism
122     do_in_jni_thread(FROM_HERE,
123                      base::Bind(cb, 0, 0, btm_status_value(BTM_SUCCESS)));
124   }
125 
126   /** Configure a scan filter condition  */
ScanFilterAdd(int filter_index,std::vector<ApcfCommand> filters,FilterConfigCallback cb)127   void ScanFilterAdd(int filter_index, std::vector<ApcfCommand> filters,
128                      FilterConfigCallback cb) {
129     LOG(INFO) << __func__ << " in shim layer";
130     std::vector<bluetooth::hci::AdvertisingPacketContentFilterCommand>
131         new_filters = {};
132     for (size_t i = 0; i < filters.size(); i++) {
133       bluetooth::hci::AdvertisingPacketContentFilterCommand command{};
134       if (!parse_filter_command(command, filters[i])) {
135         LOG_ERROR("invalid apcf command");
136         return;
137       }
138       new_filters.push_back(command);
139     }
140     bluetooth::shim::GetScanning()->ScanFilterAdd(filter_index, new_filters);
141     do_in_jni_thread(FROM_HERE,
142                      base::Bind(cb, 0, 0, 0, btm_status_value(BTM_SUCCESS)));
143   }
144 
145   /** Clear all scan filter conditions for specific filter index*/
ScanFilterClear(int filter_index,FilterConfigCallback cb)146   void ScanFilterClear(int filter_index, FilterConfigCallback cb) {
147     LOG(INFO) << __func__ << " in shim layer";
148     // This function doesn't used in java layer
149   }
150 
151   /** Enable / disable scan filter feature*/
ScanFilterEnable(bool enable,EnableCallback cb)152   void ScanFilterEnable(bool enable, EnableCallback cb) {
153     LOG(INFO) << __func__ << " in shim layer";
154     bluetooth::shim::GetScanning()->ScanFilterEnable(enable);
155 
156     uint8_t action = enable ? 1 : 0;
157     do_in_jni_thread(FROM_HERE,
158                      base::Bind(cb, action, btm_status_value(BTM_SUCCESS)));
159   }
160 
161   /** Sets the LE scan interval and window in units of N*0.625 msec */
SetScanParameters(int scan_interval,int scan_window,Callback cb)162   void SetScanParameters(int scan_interval, int scan_window, Callback cb) {
163     LOG(INFO) << __func__ << " in shim layer";
164     // use active scan
165     auto scan_type = static_cast<bluetooth::hci::LeScanType>(0x01);
166     bluetooth::shim::GetScanning()->SetScanParameters(scan_type, scan_interval,
167                                                       scan_window);
168     do_in_jni_thread(FROM_HERE, base::Bind(cb, 0));
169   }
170 
171   /* 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)172   void BatchscanConfigStorage(int client_if, int batch_scan_full_max,
173                               int batch_scan_trunc_max,
174                               int batch_scan_notify_threshold, Callback cb) {
175     LOG(INFO) << __func__ << " in shim layer";
176     bluetooth::shim::GetScanning()->BatchScanConifgStorage(
177         batch_scan_full_max, batch_scan_trunc_max, batch_scan_notify_threshold,
178         client_if);
179     do_in_jni_thread(FROM_HERE, base::Bind(cb, btm_status_value(BTM_SUCCESS)));
180   }
181 
182   /* Enable batchscan */
BatchscanEnable(int scan_mode,int scan_interval,int scan_window,int addr_type,int discard_rule,Callback cb)183   virtual void BatchscanEnable(int scan_mode, int scan_interval,
184                                int scan_window, int addr_type, int discard_rule,
185                                Callback cb) {
186     LOG(INFO) << __func__ << " in shim layer";
187     auto batch_scan_mode =
188         static_cast<bluetooth::hci::BatchScanMode>(scan_mode);
189     auto batch_scan_discard_rule =
190         static_cast<bluetooth::hci::BatchScanDiscardRule>(discard_rule);
191     bluetooth::shim::GetScanning()->BatchScanEnable(
192         batch_scan_mode, scan_window, scan_interval, batch_scan_discard_rule);
193     do_in_jni_thread(FROM_HERE, base::Bind(cb, btm_status_value(BTM_SUCCESS)));
194   }
195 
196   /* Disable batchscan */
BatchscanDisable(Callback cb)197   virtual void BatchscanDisable(Callback cb) {
198     LOG(INFO) << __func__ << " in shim layer";
199     bluetooth::shim::GetScanning()->BatchScanDisable();
200     do_in_jni_thread(FROM_HERE, base::Bind(cb, btm_status_value(BTM_SUCCESS)));
201   }
202 
203   /* Read out batchscan reports */
BatchscanReadReports(int client_if,int scan_mode)204   void BatchscanReadReports(int client_if, int scan_mode) {
205     LOG(INFO) << __func__ << " in shim layer";
206     auto batch_scan_mode =
207         static_cast<bluetooth::hci::BatchScanMode>(scan_mode);
208     auto scanner_id = static_cast<bluetooth::hci::ScannerId>(client_if);
209     bluetooth::shim::GetScanning()->BatchScanReadReport(scanner_id,
210                                                         batch_scan_mode);
211   }
212 
StartSync(uint8_t sid,RawAddress address,uint16_t skip,uint16_t timeout,StartSyncCb start_cb,SyncReportCb report_cb,SyncLostCb lost_cb)213   void StartSync(uint8_t sid, RawAddress address, uint16_t skip,
214                  uint16_t timeout, StartSyncCb start_cb, SyncReportCb report_cb,
215                  SyncLostCb lost_cb) {
216     LOG(INFO) << __func__ << " in shim layer";
217     // This function doesn't implement in the old stack
218   }
219 
StopSync(uint16_t handle)220   void StopSync(uint16_t handle) {
221     LOG(INFO) << __func__ << " in shim layer";
222     // This function doesn't implement in the old stack
223   }
224 
RegisterCallbacks(ScanningCallbacks * callbacks)225   void RegisterCallbacks(ScanningCallbacks* callbacks) {
226     LOG(INFO) << __func__ << " in shim layer";
227     scanning_callbacks_ = callbacks;
228   }
229 
OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,bluetooth::hci::ScannerId scanner_id,ScanningStatus status)230   void OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,
231                            bluetooth::hci::ScannerId scanner_id,
232                            ScanningStatus status) {
233     auto uuid = bluetooth::Uuid::From128BitBE(app_uuid.To128BitBE());
234     do_in_jni_thread(FROM_HERE,
235                      base::Bind(&ScanningCallbacks::OnScannerRegistered,
236                                 base::Unretained(scanning_callbacks_), uuid,
237                                 scanner_id, status));
238   }
239 
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)240   void OnScanResult(uint16_t event_type, uint8_t address_type,
241                     bluetooth::hci::Address address, uint8_t primary_phy,
242                     uint8_t secondary_phy, uint8_t advertising_sid,
243                     int8_t tx_power, int8_t rssi,
244                     uint16_t periodic_advertising_interval,
245                     std::vector<uint8_t> advertising_data) {
246     RawAddress raw_address = ToRawAddress(address);
247 
248     do_in_jni_thread(
249         FROM_HERE,
250         base::BindOnce(&BleScannerInterfaceImpl::handle_remote_properties,
251                        base::Unretained(this), raw_address, address_type,
252                        advertising_data));
253 
254     do_in_jni_thread(
255         FROM_HERE,
256         base::BindOnce(&ScanningCallbacks::OnScanResult,
257                        base::Unretained(scanning_callbacks_), event_type,
258                        address_type, raw_address, primary_phy, secondary_phy,
259                        advertising_sid, tx_power, rssi,
260                        periodic_advertising_interval, advertising_data));
261 
262     // TODO: Remove when StartInquiry in GD part implemented
263     btm_ble_process_adv_pkt_cont_for_inquiry(
264         event_type, address_type, raw_address, primary_phy, secondary_phy,
265         advertising_sid, tx_power, rssi, periodic_advertising_interval,
266         advertising_data);
267   }
268 
OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info)269   void OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo
270                                on_found_on_lost_info) {
271     AdvertisingTrackInfo track_info = {};
272     RawAddress raw_address =
273         ToRawAddress(on_found_on_lost_info.advertiser_address);
274     track_info.advertiser_address = raw_address;
275     track_info.advertiser_address_type =
276         on_found_on_lost_info.advertiser_address_type;
277     track_info.scanner_id = on_found_on_lost_info.scanner_id;
278     track_info.filter_index = on_found_on_lost_info.filter_index;
279     track_info.advertiser_state = on_found_on_lost_info.advertiser_state;
280     track_info.advertiser_info_present =
281         static_cast<uint8_t>(on_found_on_lost_info.advertiser_info_present);
282     if (on_found_on_lost_info.advertiser_info_present ==
283         bluetooth::hci::AdvtInfoPresent::ADVT_INFO_PRESENT) {
284       track_info.tx_power = on_found_on_lost_info.tx_power;
285       track_info.rssi = on_found_on_lost_info.rssi;
286       track_info.time_stamp = on_found_on_lost_info.time_stamp;
287       auto adv_data = on_found_on_lost_info.adv_packet;
288       track_info.adv_packet_len = (uint8_t)adv_data.size();
289       track_info.adv_packet.reserve(adv_data.size());
290       track_info.adv_packet.insert(track_info.adv_packet.end(),
291                                    adv_data.begin(), adv_data.end());
292       auto scan_rsp_data = on_found_on_lost_info.scan_response;
293       track_info.scan_response_len = (uint8_t)scan_rsp_data.size();
294       track_info.scan_response.reserve(adv_data.size());
295       track_info.scan_response.insert(track_info.scan_response.end(),
296                                       scan_rsp_data.begin(),
297                                       scan_rsp_data.end());
298     }
299     do_in_jni_thread(
300         FROM_HERE,
301         base::BindOnce(&ScanningCallbacks::OnTrackAdvFoundLost,
302                        base::Unretained(scanning_callbacks_), track_info));
303   }
304 
OnBatchScanReports(int client_if,int status,int report_format,int num_records,std::vector<uint8_t> data)305   void OnBatchScanReports(int client_if, int status, int report_format,
306                           int num_records, std::vector<uint8_t> data) {
307     do_in_jni_thread(
308         FROM_HERE,
309         base::BindOnce(&ScanningCallbacks::OnBatchScanReports,
310                        base::Unretained(scanning_callbacks_), client_if, status,
311                        report_format, num_records, data));
312   }
313 
OnBatchScanThresholdCrossed(int client_if)314   void OnBatchScanThresholdCrossed(int client_if) {
315     do_in_jni_thread(
316         FROM_HERE,
317         base::BindOnce(&ScanningCallbacks::OnBatchScanThresholdCrossed,
318                        base::Unretained(scanning_callbacks_), client_if));
319   }
320 
OnTimeout()321   void OnTimeout() {}
322 
OnFilterEnable(bluetooth::hci::Enable enable,uint8_t status)323   void OnFilterEnable(bluetooth::hci::Enable enable, uint8_t status){};
324 
OnFilterParamSetup(uint8_t available_spaces,bluetooth::hci::ApcfAction action,uint8_t status)325   void OnFilterParamSetup(uint8_t available_spaces,
326                           bluetooth::hci::ApcfAction action, uint8_t status){};
327 
OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type,uint8_t available_spaces,bluetooth::hci::ApcfAction action,uint8_t status)328   void OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type,
329                               uint8_t available_spaces,
330                               bluetooth::hci::ApcfAction action,
331                               uint8_t status){};
332 
333   ScanningCallbacks* scanning_callbacks_;
334 
335  private:
parse_filter_command(bluetooth::hci::AdvertisingPacketContentFilterCommand & advertising_packet_content_filter_command,ApcfCommand apcf_command)336   bool parse_filter_command(
337       bluetooth::hci::AdvertisingPacketContentFilterCommand&
338           advertising_packet_content_filter_command,
339       ApcfCommand apcf_command) {
340     advertising_packet_content_filter_command.filter_type =
341         static_cast<bluetooth::hci::ApcfFilterType>(apcf_command.type);
342     bluetooth::hci::Address address = ToGdAddress(apcf_command.address);
343     advertising_packet_content_filter_command.address = address;
344     advertising_packet_content_filter_command.application_address_type =
345         static_cast<bluetooth::hci::ApcfApplicationAddressType>(
346             apcf_command.addr_type);
347 
348     if (!apcf_command.uuid.IsEmpty()) {
349       uint8_t uuid_len = apcf_command.uuid.GetShortestRepresentationSize();
350       switch (uuid_len) {
351         case bluetooth::Uuid::kNumBytes16: {
352           advertising_packet_content_filter_command.uuid =
353               bluetooth::hci::Uuid::From16Bit(apcf_command.uuid.As16Bit());
354         } break;
355         case bluetooth::Uuid::kNumBytes32: {
356           advertising_packet_content_filter_command.uuid =
357               bluetooth::hci::Uuid::From32Bit(apcf_command.uuid.As32Bit());
358         } break;
359         case bluetooth::Uuid::kNumBytes128: {
360           advertising_packet_content_filter_command.uuid =
361               bluetooth::hci::Uuid::From128BitBE(
362                   apcf_command.uuid.To128BitBE());
363         } break;
364         default:
365           LOG_WARN("illegal UUID length %d", (uint16_t)uuid_len);
366           return false;
367       }
368     }
369 
370     if (!apcf_command.uuid_mask.IsEmpty()) {
371       uint8_t uuid_len = apcf_command.uuid.GetShortestRepresentationSize();
372       switch (uuid_len) {
373         case bluetooth::Uuid::kNumBytes16: {
374           advertising_packet_content_filter_command.uuid_mask =
375               bluetooth::hci::Uuid::From16Bit(apcf_command.uuid_mask.As16Bit());
376         } break;
377         case bluetooth::Uuid::kNumBytes32: {
378           advertising_packet_content_filter_command.uuid_mask =
379               bluetooth::hci::Uuid::From32Bit(apcf_command.uuid_mask.As32Bit());
380         } break;
381         case bluetooth::Uuid::kNumBytes128: {
382           advertising_packet_content_filter_command.uuid_mask =
383               bluetooth::hci::Uuid::From128BitBE(
384                   apcf_command.uuid_mask.To128BitBE());
385         } break;
386         default:
387           LOG_WARN("illegal UUID length %d", (uint16_t)uuid_len);
388           return false;
389       }
390     }
391 
392     advertising_packet_content_filter_command.name.assign(
393         apcf_command.name.begin(), apcf_command.name.end());
394     advertising_packet_content_filter_command.company = apcf_command.company;
395     advertising_packet_content_filter_command.company_mask =
396         apcf_command.company_mask;
397     advertising_packet_content_filter_command.data.assign(
398         apcf_command.data.begin(), apcf_command.data.end());
399     advertising_packet_content_filter_command.data_mask.assign(
400         apcf_command.data_mask.begin(), apcf_command.data_mask.end());
401     return true;
402   }
403 
handle_remote_properties(RawAddress bd_addr,tBLE_ADDR_TYPE addr_type,std::vector<uint8_t> advertising_data)404   void handle_remote_properties(RawAddress bd_addr, tBLE_ADDR_TYPE addr_type,
405                                 std::vector<uint8_t> advertising_data) {
406     // skip anonymous advertisment
407     if (addr_type == BLE_ADDR_ANONYMOUS) {
408       return;
409     }
410 
411     auto device_type = bluetooth::hci::DeviceType::LE;
412     uint8_t flag_len;
413     const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType(
414         advertising_data, BTM_BLE_AD_TYPE_FLAG, &flag_len);
415     if (p_flag != NULL && flag_len != 0) {
416       if ((BTM_BLE_BREDR_NOT_SPT & *p_flag) == 0) {
417         device_type = bluetooth::hci::DeviceType::DUAL;
418       }
419     }
420 
421     uint8_t remote_name_len;
422     const uint8_t* p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
423         advertising_data, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
424 
425     if (p_eir_remote_name == NULL) {
426       p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
427           advertising_data, BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
428     }
429 
430     // update device name
431     if ((addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name)) {
432       if (!find_address_cache(bd_addr)) {
433         add_address_cache(bd_addr);
434 
435         if (p_eir_remote_name) {
436           if (remote_name_len > BD_NAME_LEN + 1 ||
437               (remote_name_len == BD_NAME_LEN + 1 &&
438                p_eir_remote_name[BD_NAME_LEN] != '\0')) {
439             LOG_INFO("%s dropping invalid packet - device name too long: %d",
440                      __func__, remote_name_len);
441             return;
442           }
443 
444           bt_bdname_t bdname;
445           memcpy(bdname.name, p_eir_remote_name, remote_name_len);
446           if (remote_name_len < BD_NAME_LEN + 1)
447             bdname.name[remote_name_len] = '\0';
448 
449           btif_dm_update_ble_remote_properties(bd_addr, bdname.name,
450                                                device_type);
451         }
452       }
453     }
454     if (!bluetooth::shim::is_gd_stack_started_up()) {
455       LOG_WARN("Gd stack is stopped, return");
456       return;
457     }
458     auto* storage_module = bluetooth::shim::GetStorage();
459     bluetooth::hci::Address address = ToGdAddress(bd_addr);
460 
461     // update device type
462     auto mutation = storage_module->Modify();
463     bluetooth::storage::Device device =
464         storage_module->GetDeviceByLegacyKey(address);
465     mutation.Add(device.SetDeviceType(device_type));
466     mutation.Commit();
467 
468     // update address type
469     auto mutation2 = storage_module->Modify();
470     bluetooth::storage::LeDevice le_device = device.Le();
471     mutation2.Add(
472         le_device.SetAddressType((bluetooth::hci::AddressType)addr_type));
473     mutation2.Commit();
474   }
475 
add_address_cache(const RawAddress & p_bda)476   void add_address_cache(const RawAddress& p_bda) {
477     // Remove the oldest entries
478     while (remote_bdaddr_cache_.size() >= remote_bdaddr_cache_max_size_) {
479       const RawAddress& raw_address = remote_bdaddr_cache_ordered_.front();
480       remote_bdaddr_cache_.erase(raw_address);
481       remote_bdaddr_cache_ordered_.pop();
482     }
483     remote_bdaddr_cache_.insert(p_bda);
484     remote_bdaddr_cache_ordered_.push(p_bda);
485   }
486 
find_address_cache(const RawAddress & p_bda)487   bool find_address_cache(const RawAddress& p_bda) {
488     return (remote_bdaddr_cache_.find(p_bda) != remote_bdaddr_cache_.end());
489   }
490 
init_address_cache(void)491   void init_address_cache(void) {
492     remote_bdaddr_cache_.clear();
493     remote_bdaddr_cache_ordered_ = {};
494   }
495 
496   // all access to this variable should be done on the jni thread
497   std::set<RawAddress> remote_bdaddr_cache_;
498   std::queue<RawAddress> remote_bdaddr_cache_ordered_;
499   const size_t remote_bdaddr_cache_max_size_ = 1024;
500 };
501 
502 BleScannerInterfaceImpl* bt_le_scanner_instance = nullptr;
503 
get_ble_scanner_instance()504 BleScannerInterface* bluetooth::shim::get_ble_scanner_instance() {
505   if (bt_le_scanner_instance == nullptr) {
506     bt_le_scanner_instance = new BleScannerInterfaceImpl();
507   }
508   return bt_le_scanner_instance;
509 }
510 
init_scanning_manager()511 void bluetooth::shim::init_scanning_manager() {
512   bt_le_scanner_instance->Init();
513 }
514