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