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