1 /*
2  * Copyright (C) 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 #ifndef GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H
17 #define GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H
18 
19 #include <memory>
20 
21 #include "include/hardware/ble_scanner.h"
22 #include "rust/cxx.h"
23 
24 namespace bluetooth {
25 namespace topshim {
26 namespace rust {
27 
28 struct RustApcfCommand;
29 struct RustMsftAdvMonitor;
30 
31 class BleScannerIntf : public ScanningCallbacks {
32  public:
BleScannerIntf(BleScannerInterface * scanner_intf)33   BleScannerIntf(BleScannerInterface* scanner_intf) : scanner_intf_(scanner_intf){};
34   ~BleScannerIntf() = default;
35 
36   // ScanningCallbacks overrides
37   void OnScannerRegistered(const bluetooth::Uuid app_uuid, uint8_t scannerId, uint8_t status) override;
38 
39   void OnSetScannerParameterComplete(uint8_t scannerId, uint8_t status) override;
40 
41   void OnScanResult(
42       uint16_t event_type,
43       uint8_t addr_type,
44       RawAddress bda,
45       uint8_t primary_phy,
46       uint8_t secondary_phy,
47       uint8_t advertising_sid,
48       int8_t tx_power,
49       int8_t rssi,
50       uint16_t periodic_adv_int,
51       std::vector<uint8_t> adv_data) override;
52 
53   void OnTrackAdvFoundLost(AdvertisingTrackInfo advertising_track_info) override;
54 
55   void OnBatchScanReports(
56       int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data) override;
57 
58   void OnBatchScanThresholdCrossed(int client_if) override;
59 
60   void OnPeriodicSyncStarted(
61       int reg_id,
62       uint8_t status,
63       uint16_t sync_handle,
64       uint8_t advertising_sid,
65       uint8_t address_type,
66       RawAddress address,
67       uint8_t phy,
68       uint16_t interval) override;
69   void OnPeriodicSyncReport(
70       uint16_t sync_handle, int8_t tx_power, int8_t rssi, uint8_t status, std::vector<uint8_t> data) override;
71   void OnPeriodicSyncLost(uint16_t sync_handle) override;
72   void OnPeriodicSyncTransferred(int pa_source, uint8_t status, RawAddress address) override;
73   void OnBigInfoReport(uint16_t sync_handle, bool encrypted) override;
74 
75   // Implementations of BleScannerInterface. These don't inherit from
76   // BleScannerInterface because the Rust FFI boundary requires some clever
77   // modifications.
78 
79   // Register a scanner for a Uuid. Response comes back via
80   // |OnRegisterCallback|.
81   void RegisterScanner(bluetooth::Uuid uuid);
82 
83   // Unregister a scanner with a |scanner_id|.
84   void Unregister(uint8_t scanner_id);
85 
86   // Start/Stop LE scanning.
87   void Scan(bool start);
88 
89   // Setup scan filter parameters. Get responses via
90   // |OnFilterParamSetupCallback|.
91   void ScanFilterParamSetup(
92       uint8_t scanner_id,
93       uint8_t action,
94       uint8_t filter_index,
95       btgatt_filt_param_setup_t filter_param);
96 
97   // Adds filters to given filter index. Gets responses via
98   // |OnFilterConfigCallback|.
99   void ScanFilterAdd(uint8_t filter_index, ::rust::Vec<RustApcfCommand> filters);
100 
101   // Clear scan filter conditions for a specific index.
102   void ScanFilterClear(uint8_t filter_index);
103 
104   // Enable/disable scan filter. Gets responses via |OnEnableCallback|.
105   void ScanFilterEnable(bool enable);
106 
107   // Is MSFT Extension supported?
108   bool IsMsftSupported();
109 
110   // Adds an MSFT filter. Gets responses via |OnMsftAdvMonitorAddCallback|.
111   void MsftAdvMonitorAdd(uint32_t call_id, const RustMsftAdvMonitor& monitor);
112 
113   // Removes a previously added MSFT scan filter.
114   void MsftAdvMonitorRemove(uint32_t call_id, uint8_t monitor_handle);
115 
116   // Enables or disables MSFT advertisement monitor.
117   void MsftAdvMonitorEnable(uint32_t call_id, bool enable);
118 
119   // Sets the LE scan interval and window in units of N * 0.625 msec. The result
120   // of this action is returned via |OnStatusCallback|.
121   void SetScanParameters(
122       uint8_t scanner_id,
123       uint8_t scan_type,
124       uint16_t scan_interval,
125       uint16_t scan_window,
126       uint8_t scan_phy);
127 
128   // Configure the batchscan storage and get a response via |OnStatusCallback|.
129   void BatchscanConfigStorage(
130       uint8_t scanner_id,
131       int32_t batch_scan_full_max,
132       int32_t batch_scan_trunc_max,
133       int32_t batch_scan_notify_threshold);
134 
135   // Enable batchscan. Gets responses via |OnStatusCallback| with scanner id
136   // = 0 (since multiple scanners can be registered).
137   void BatchscanEnable(
138       int32_t scan_mode, uint16_t scan_interval, uint16_t scan_window, int32_t addr_type, int32_t discard_rule);
139 
140   // Disable batchscan. Gets responses via |OnStatusCallback| with a scanner id
141   // = 0 (since multiple scanners can be registered).
142   void BatchscanDisable();
143 
144   // Read out batchscan report for a specific scanner. Gets responses via
145   // |ScanningCallbacks::OnBatchScanReports|.
146   void BatchscanReadReports(uint8_t scanner_id, int32_t scan_mode);
147 
148   // Start periodic sync. Gets responses via |OnStartSyncCb|. Periodic reports
149   // come via |OnSyncReportCb| and |OnSyncLostCb|.
150   void StartSync(uint8_t sid, RawAddress addr, uint16_t skip, uint16_t timeout);
151 
152   // Stop periodic sync.
153   void StopSync(uint16_t handle);
154 
155   // Cancel creating a periodic sync.
156   void CancelCreateSync(uint8_t sid, RawAddress addr);
157 
158   // Transfer sync data to target address. Gets responses via
159   // |OnSyncTransferCb|.
160   void TransferSync(RawAddress addr, uint16_t service_data, uint16_t sync_handle);
161 
162   // Transfer set info to target address. Gets responses via |OnSyncTransferCb|.
163   void TransferSetInfo(RawAddress addr, uint16_t service_data, uint8_t adv_handle);
164 
165   // Sync tx parameters to target address. Gets responses via |OnStartSyncCb|.
166   void SyncTxParameters(RawAddress addr, uint8_t mode, uint16_t skip, uint16_t timeout);
167 
168   // Register scanning callbacks to be dispatched to the Rust layer via static
169   // methods.
170   void RegisterCallbacks();
171 
172  private:
173   // The callback functions below will get base::Bind to the apis that need it
174   // and will call the same Rust function with all the parameters. Some of these
175   // callbacks don't have all the parameters coming back in the original
176   // callback and will need the values to be base::Bind at the callsite.
177 
178   void OnRegisterCallback(bluetooth::Uuid uuid, uint8_t scanner_id, uint8_t btm_status);
179   void OnStatusCallback(uint8_t scanner_id, uint8_t btm_status);
180   void OnEnableCallback(uint8_t action, uint8_t btm_status);
181   void OnFilterParamSetupCallback(uint8_t scanner_id, uint8_t avbl_space, uint8_t action_type, uint8_t btm_status);
182   void OnFilterConfigCallback(
183       uint8_t filt_index, uint8_t filt_type, uint8_t avbl_space, uint8_t action, uint8_t btm_status);
184 #if TARGET_FLOSS
185   void OnMsftAdvMonitorAddCallback(uint32_t call_id, uint8_t monitor_handle, uint8_t status);
186   void OnMsftAdvMonitorRemoveCallback(uint32_t call_id, uint8_t status);
187   void OnMsftAdvMonitorEnableCallback(uint32_t call_id, uint8_t status);
188 #endif
189 
190   BleScannerInterface* scanner_intf_;
191 };
192 
193 std::unique_ptr<BleScannerIntf> GetBleScannerIntf(const unsigned char* gatt_intf);
194 
195 }  // namespace rust
196 }  // namespace topshim
197 }  // namespace bluetooth
198 
199 #endif  // GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H
200