• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 /**
18  * Gd shim layer to legacy le scanner
19  */
20 #pragma once
21 
22 #include <queue>
23 #include <set>
24 
25 #include "hci/le_scanning_callback.h"
26 #include "include/hardware/ble_scanner.h"
27 #include "types/ble_address_with_type.h"
28 #include "types/bluetooth/uuid.h"
29 #include "types/raw_address.h"
30 
31 namespace bluetooth {
32 namespace shim {
33 
34 extern ::ScanningCallbacks* default_scanning_callback;
35 
36 class BleScannerInterfaceImpl : public ::BleScannerInterface,
37                                 public bluetooth::hci::ScanningCallback {
38  public:
~BleScannerInterfaceImpl()39   ~BleScannerInterfaceImpl() override{};
40 
41   void Init();
42 
43   // ::BleScannerInterface
44   void RegisterScanner(const bluetooth::Uuid& uuid, RegisterCallback) override;
45   void Unregister(int scanner_id) override;
46   void Scan(bool start) override;
47   void ScanFilterParamSetup(
48       uint8_t client_if, uint8_t action, uint8_t filter_index,
49       std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
50       FilterParamSetupCallback cb) override;
51   void ScanFilterAdd(int filter_index, std::vector<ApcfCommand> filters,
52                      FilterConfigCallback cb) override;
53   void ScanFilterClear(int filter_index, FilterConfigCallback cb) override;
54   void ScanFilterEnable(bool enable, EnableCallback cb) override;
55   void SetScanParameters(int scanner_id, int scan_interval, int scan_window,
56                          Callback cb) override;
57   void BatchscanConfigStorage(int client_if, int batch_scan_full_max,
58                               int batch_scan_trunc_max,
59                               int batch_scan_notify_threshold,
60                               Callback cb) override;
61   void BatchscanEnable(int scan_mode, int scan_interval, int scan_window,
62                        int addr_type, int discard_rule, Callback cb) override;
63   void BatchscanDisable(Callback cb) override;
64   void BatchscanReadReports(int client_if, int scan_mode) override;
65   void StartSync(uint8_t sid, RawAddress address, uint16_t skip,
66                  uint16_t timeout, int reg_id) override;
67   void StopSync(uint16_t handle) override;
68   void CancelCreateSync(uint8_t sid, RawAddress address) override;
69   void TransferSync(RawAddress address, uint16_t service_data,
70                     uint16_t sync_handle, int pa_source) override;
71   void TransferSetInfo(RawAddress address, uint16_t service_data,
72                        uint8_t adv_handle, int pa_source) override;
73   void SyncTxParameters(RawAddress addr, uint8_t mode, uint16_t skip,
74                         uint16_t timeout, int reg_id) override;
75 
76   // bluetooth::hci::ScanningCallback
77   void RegisterCallbacks(ScanningCallbacks* callbacks);
78   void OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,
79                            bluetooth::hci::ScannerId scanner_id,
80                            ScanningStatus status) override;
81   void OnSetScannerParameterComplete(bluetooth::hci::ScannerId scanner_id,
82                                      ScanningStatus status) override;
83   void OnScanResult(uint16_t event_type, uint8_t address_type,
84                     bluetooth::hci::Address address, uint8_t primary_phy,
85                     uint8_t secondary_phy, uint8_t advertising_sid,
86                     int8_t tx_power, int8_t rssi,
87                     uint16_t periodic_advertising_interval,
88                     std::vector<uint8_t> advertising_data) override;
89   void OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo
90                                on_found_on_lost_info) override;
91   void OnBatchScanReports(int client_if, int status, int report_format,
92                           int num_records, std::vector<uint8_t> data) override;
93   void OnBatchScanThresholdCrossed(int client_if) override;
94   void OnTimeout() override;
95   void OnFilterEnable(bluetooth::hci::Enable enable, uint8_t status) override;
96   void OnFilterParamSetup(uint8_t available_spaces,
97                           bluetooth::hci::ApcfAction action,
98                           uint8_t status) override;
99   void OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type,
100                               uint8_t available_spaces,
101                               bluetooth::hci::ApcfAction action,
102                               uint8_t status) override;
103   void OnPeriodicSyncStarted(int reg_id, uint8_t status, uint16_t sync_handle,
104                              uint8_t advertising_sid,
105                              bluetooth::hci::AddressWithType address_with_type,
106                              uint8_t phy, uint16_t interval) override;
107   void OnPeriodicSyncReport(uint16_t sync_handle, int8_t tx_power, int8_t rssi,
108                             uint8_t status, std::vector<uint8_t> data) override;
109   void OnPeriodicSyncLost(uint16_t sync_handle) override;
110   void OnPeriodicSyncTransferred(int pa_source, uint8_t status,
111                                  bluetooth::hci::Address address) override;
112   ::ScanningCallbacks* scanning_callbacks_ = default_scanning_callback;
113 
114  private:
115   bool parse_filter_command(
116       bluetooth::hci::AdvertisingPacketContentFilterCommand&
117           advertising_packet_content_filter_command,
118       ApcfCommand apcf_command);
119   void handle_remote_properties(RawAddress bd_addr, tBLE_ADDR_TYPE addr_type,
120                                 std::vector<uint8_t> advertising_data);
121 
122   class AddressCache {
123    public:
124     void init(void);
125     void add(const RawAddress& p_bda);
126     bool find(const RawAddress& p_bda);
127 
128    private:
129     // all access to this variable should be done on the jni thread
130     std::set<RawAddress> remote_bdaddr_cache_;
131     std::queue<RawAddress> remote_bdaddr_cache_ordered_;
132     const size_t remote_bdaddr_cache_max_size_ = 1024;
133   } address_cache_;
134 };
135 
136 }  // namespace shim
137 }  // namespace bluetooth
138