• 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 MsftCallbacks {
37  public:
38   using MsftAdvMonitorAddCallback =
39       base::Callback<void(uint8_t /* monitor_handle */, uint8_t /* status */)>;
40   using MsftAdvMonitorRemoveCallback =
41       base::Callback<void(uint8_t /* status */)>;
42   using MsftAdvMonitorEnableCallback =
43       base::Callback<void(uint8_t /* status */)>;
44 
45   MsftAdvMonitorAddCallback Add;
46   MsftAdvMonitorRemoveCallback Remove;
47   MsftAdvMonitorEnableCallback Enable;
48 };
49 
50 class BleScannerInterfaceImpl : public ::BleScannerInterface,
51                                 public bluetooth::hci::ScanningCallback {
52  public:
~BleScannerInterfaceImpl()53   ~BleScannerInterfaceImpl() override{};
54 
55   void Init();
56 
57   // ::BleScannerInterface
58   void RegisterScanner(const bluetooth::Uuid& uuid, RegisterCallback) override;
59   void Unregister(int scanner_id) override;
60   void Scan(bool start) override;
61   void ScanFilterParamSetup(
62       uint8_t client_if, uint8_t action, uint8_t filter_index,
63       std::unique_ptr<btgatt_filt_param_setup_t> filt_param,
64       FilterParamSetupCallback cb) override;
65   void ScanFilterAdd(int filter_index, std::vector<ApcfCommand> filters,
66                      FilterConfigCallback cb) override;
67   void ScanFilterClear(int filter_index, FilterConfigCallback cb) override;
68   void ScanFilterEnable(bool enable, EnableCallback cb) override;
69   bool IsMsftSupported() override;
70   void MsftAdvMonitorAdd(MsftAdvMonitor monitor,
71                          MsftAdvMonitorAddCallback cb) override;
72   void MsftAdvMonitorRemove(uint8_t monitor_handle,
73                             MsftAdvMonitorRemoveCallback cb) override;
74   void MsftAdvMonitorEnable(bool enable,
75                             MsftAdvMonitorEnableCallback cb) override;
76   void SetScanParameters(int scanner_id, int scan_interval, int scan_window,
77                          Callback cb) override;
78   void BatchscanConfigStorage(int client_if, int batch_scan_full_max,
79                               int batch_scan_trunc_max,
80                               int batch_scan_notify_threshold,
81                               Callback cb) override;
82   void BatchscanEnable(int scan_mode, int scan_interval, int scan_window,
83                        int addr_type, int discard_rule, Callback cb) override;
84   void BatchscanDisable(Callback cb) override;
85   void BatchscanReadReports(int client_if, int scan_mode) override;
86   void StartSync(uint8_t sid, RawAddress address, uint16_t skip,
87                  uint16_t timeout, int reg_id) override;
88   void StopSync(uint16_t handle) override;
89   void CancelCreateSync(uint8_t sid, RawAddress address) override;
90   void TransferSync(RawAddress address, uint16_t service_data,
91                     uint16_t sync_handle, int pa_source) override;
92   void TransferSetInfo(RawAddress address, uint16_t service_data,
93                        uint8_t adv_handle, int pa_source) override;
94   void SyncTxParameters(RawAddress addr, uint8_t mode, uint16_t skip,
95                         uint16_t timeout, int reg_id) override;
96 
97   // bluetooth::hci::ScanningCallback
98   void RegisterCallbacks(ScanningCallbacks* callbacks);
99   void OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,
100                            bluetooth::hci::ScannerId scanner_id,
101                            ScanningStatus status) override;
102   void OnSetScannerParameterComplete(bluetooth::hci::ScannerId scanner_id,
103                                      ScanningStatus status) override;
104   void OnScanResult(uint16_t event_type, uint8_t address_type,
105                     bluetooth::hci::Address address, uint8_t primary_phy,
106                     uint8_t secondary_phy, uint8_t advertising_sid,
107                     int8_t tx_power, int8_t rssi,
108                     uint16_t periodic_advertising_interval,
109                     std::vector<uint8_t> advertising_data) override;
110   void OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo
111                                on_found_on_lost_info) override;
112   void OnBatchScanReports(int client_if, int status, int report_format,
113                           int num_records, std::vector<uint8_t> data) override;
114   void OnBatchScanThresholdCrossed(int client_if) override;
115   void OnTimeout() override;
116   void OnFilterEnable(bluetooth::hci::Enable enable, uint8_t status) override;
117   void OnFilterParamSetup(uint8_t available_spaces,
118                           bluetooth::hci::ApcfAction action,
119                           uint8_t status) override;
120   void OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type,
121                               uint8_t available_spaces,
122                               bluetooth::hci::ApcfAction action,
123                               uint8_t status) override;
124   void OnPeriodicSyncStarted(int reg_id, uint8_t status, uint16_t sync_handle,
125                              uint8_t advertising_sid,
126                              bluetooth::hci::AddressWithType address_with_type,
127                              uint8_t phy, uint16_t interval) override;
128   void OnPeriodicSyncReport(uint16_t sync_handle, int8_t tx_power, int8_t rssi,
129                             uint8_t status, std::vector<uint8_t> data) override;
130   void OnPeriodicSyncLost(uint16_t sync_handle) override;
131   void OnPeriodicSyncTransferred(int pa_source, uint8_t status,
132                                  bluetooth::hci::Address address) override;
133   void OnBigInfoReport(uint16_t sync_handle, bool encrypted) override;
134 
135   ::ScanningCallbacks* scanning_callbacks_ = default_scanning_callback;
136   void OnMsftAdvMonitorAdd(uint8_t monitor_handle,
137                            bluetooth::hci::ErrorCode status);
138   void OnMsftAdvMonitorRemove(bluetooth::hci::ErrorCode status);
139   void OnMsftAdvMonitorEnable(bool enable, bluetooth::hci::ErrorCode status);
140   MsftCallbacks msft_callbacks_;
141 
142  private:
143   bool parse_filter_command(
144       bluetooth::hci::AdvertisingPacketContentFilterCommand&
145           advertising_packet_content_filter_command,
146       ApcfCommand apcf_command);
147   void handle_remote_properties(RawAddress bd_addr, tBLE_ADDR_TYPE addr_type,
148                                 std::vector<uint8_t> advertising_data);
149 
150   class AddressCache {
151    public:
152     void init(void);
153     void add(const RawAddress& p_bda);
154     bool find(const RawAddress& p_bda);
155 
156    private:
157     // all access to this variable should be done on the jni thread
158     std::set<RawAddress> remote_bdaddr_cache_;
159     std::queue<RawAddress> remote_bdaddr_cache_ordered_;
160     const size_t remote_bdaddr_cache_max_size_ = 1024;
161   } address_cache_;
162 };
163 
164 }  // namespace shim
165 }  // namespace bluetooth
166