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 RustGattFilterParam; 31 struct RustRawAddress; 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 77 // Implementations of BleScannerInterface. These don't inherit from 78 // BleScannerInterface because the Rust FFI boundary requires some clever 79 // modifications. 80 81 // Register a scanner for a Uuid. Response comes back via 82 // |OnRegisterCallback|. 83 void RegisterScanner(RustUuid uuid); 84 85 // Unregister a scanner with a |scanner_id|. 86 void Unregister(uint8_t scanner_id); 87 88 // Start/Stop LE scanning. 89 void Scan(bool start); 90 91 // Setup scan filter parameters. Get responses via 92 // |OnFilterParamSetupCallback|. 93 void ScanFilterParamSetup(uint8_t scanner_id, uint8_t action, uint8_t filter_index, RustGattFilterParam filter_param); 94 95 // Adds filters to given filter index. Gets responses via 96 // |OnFilterConfigCallback|. 97 void ScanFilterAdd(uint8_t filter_index, ::rust::Vec<RustApcfCommand> filters); 98 99 // Clear scan filter conditions for a specific index. 100 void ScanFilterClear(uint8_t filter_index); 101 102 // Enable/disable scan filter. Gets responses via |OnEnableCallback|. 103 void ScanFilterEnable(bool enable); 104 105 // Sets the LE scan interval and window in units of N * 0.625 msec. The result 106 // of this action is returned via |OnStatusCallback|. 107 void SetScanParameters(uint8_t scanner_id, uint16_t scan_interval, uint16_t scan_window); 108 109 // Configure the batchscan storage and get a response via |OnStatusCallback|. 110 void BatchscanConfigStorage( 111 uint8_t scanner_id, 112 int32_t batch_scan_full_max, 113 int32_t batch_scan_trunc_max, 114 int32_t batch_scan_notify_threshold); 115 116 // Enable batchscan. Gets responses via |OnStatusCallback| with scanner id 117 // = 0 (since multiple scanners can be registered). 118 void BatchscanEnable( 119 int32_t scan_mode, uint16_t scan_interval, uint16_t scan_window, int32_t addr_type, int32_t discard_rule); 120 121 // Disable batchscan. Gets responses via |OnStatusCallback| with a scanner id 122 // = 0 (since multiple scanners can be registered). 123 void BatchscanDisable(); 124 125 // Read out batchscan report for a specific scanner. Gets responses via 126 // |ScanningCallbacks::OnBatchScanReports|. 127 void BatchscanReadReports(uint8_t scanner_id, int32_t scan_mode); 128 129 // Start periodic sync. Gets responses via |OnStartSyncCb|. Periodic reports 130 // come via |OnSyncReportCb| and |OnSyncLostCb|. 131 void StartSync(uint8_t sid, RustRawAddress address, uint16_t skip, uint16_t timeout); 132 133 // Stop periodic sync. 134 void StopSync(uint16_t handle); 135 136 // Cancel creating a periodic sync. 137 void CancelCreateSync(uint8_t sid, RustRawAddress address); 138 139 // Transfer sync data to target address. Gets responses via 140 // |OnSyncTransferCb|. 141 void TransferSync(RustRawAddress address, uint16_t service_data, uint16_t sync_handle); 142 143 // Transfer set info to target address. Gets responses via |OnSyncTransferCb|. 144 void TransferSetInfo(RustRawAddress address, uint16_t service_data, uint8_t adv_handle); 145 146 // Sync tx parameters to target address. Gets responses via |OnStartSyncCb|. 147 void SyncTxParameters(RustRawAddress address, uint8_t mode, uint16_t skip, uint16_t timeout); 148 149 // Register scanning callbacks to be dispatched to the Rust layer via static 150 // methods. 151 void RegisterCallbacks(); 152 153 private: 154 // The callback functions below will get base::Bind to the apis that need it 155 // and will call the same Rust function with all the parameters. Some of these 156 // callbacks don't have all the parameters coming back in the original 157 // callback and will need the values to be base::Bind at the callsite. 158 159 void OnRegisterCallback(RustUuid uuid, uint8_t scanner_id, uint8_t btm_status); 160 void OnStatusCallback(uint8_t scanner_id, uint8_t btm_status); 161 void OnEnableCallback(uint8_t action, uint8_t btm_status); 162 void OnFilterParamSetupCallback(uint8_t scanner_id, uint8_t avbl_space, uint8_t action_type, uint8_t btm_status); 163 void OnFilterConfigCallback( 164 uint8_t filt_index, uint8_t filt_type, uint8_t avbl_space, uint8_t action, uint8_t btm_status); 165 166 BleScannerInterface* scanner_intf_; 167 }; 168 169 std::unique_ptr<BleScannerIntf> GetBleScannerIntf(const unsigned char* gatt_intf); 170 171 } // namespace rust 172 } // namespace topshim 173 } // namespace bluetooth 174 175 #endif // GD_RUST_TOPSHIM_GATT_GATT_BLE_SCANNER_SHIM_H 176