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