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