1 /* 2 * Copyright (C) 2016 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 #ifndef ANDROID_INCLUDE_BLE_SCANNER_H 18 #define ANDROID_INCLUDE_BLE_SCANNER_H 19 20 #include <bluetooth/uuid.h> 21 #include <raw_address.h> 22 #include <stdint.h> 23 24 #include <memory> 25 #include <vector> 26 27 #include "bt_common_types.h" 28 #include "bt_gatt_client.h" 29 #include "bt_gatt_types.h" 30 31 /** Callback invoked when batchscan reports are obtained */ 32 typedef void (*batchscan_reports_callback)(int client_if, int status, 33 int report_format, int num_records, 34 std::vector<uint8_t> data); 35 36 /** Callback invoked when batchscan storage threshold limit is crossed */ 37 typedef void (*batchscan_threshold_callback)(int client_if); 38 39 /** Track ADV VSE callback invoked when tracked device is found or lost */ 40 typedef void (*track_adv_event_callback)( 41 btgatt_track_adv_info_t* p_track_adv_info); 42 43 /** Callback for scan results */ 44 typedef void (*scan_result_callback)(uint16_t event_type, uint8_t addr_type, 45 RawAddress* bda, uint8_t primary_phy, 46 uint8_t secondary_phy, 47 uint8_t advertising_sid, int8_t tx_power, 48 int8_t rssi, uint16_t periodic_adv_int, 49 std::vector<uint8_t> adv_data, 50 RawAddress* original_bda); 51 52 typedef struct { 53 scan_result_callback scan_result_cb; 54 batchscan_reports_callback batchscan_reports_cb; 55 batchscan_threshold_callback batchscan_threshold_cb; 56 track_adv_event_callback track_adv_event_cb; 57 } btgatt_scanner_callbacks_t; 58 59 class AdvertisingTrackInfo { 60 public: 61 // For MSFT-based advertisement monitor. 62 uint8_t monitor_handle; 63 uint8_t scanner_id; 64 uint8_t filter_index; 65 uint8_t advertiser_state; 66 uint8_t advertiser_info_present; 67 RawAddress advertiser_address; 68 uint8_t advertiser_address_type; 69 uint8_t tx_power; 70 int8_t rssi; 71 uint16_t time_stamp; 72 uint8_t adv_packet_len; 73 std::vector<uint8_t> adv_packet; 74 uint8_t scan_response_len; 75 std::vector<uint8_t> scan_response; 76 }; 77 78 /** 79 * LE Scanning related callbacks invoked from from the Bluetooth native stack 80 * All callbacks are invoked on the JNI thread 81 */ 82 class ScanningCallbacks { 83 public: 84 virtual ~ScanningCallbacks() = default; 85 virtual void OnScannerRegistered(const bluetooth::Uuid app_uuid, 86 uint8_t scannerId, uint8_t status) = 0; 87 virtual void OnSetScannerParameterComplete(uint8_t scannerId, 88 uint8_t status) = 0; 89 virtual void OnScanResult(uint16_t event_type, uint8_t addr_type, 90 RawAddress bda, uint8_t primary_phy, 91 uint8_t secondary_phy, uint8_t advertising_sid, 92 int8_t tx_power, int8_t rssi, 93 uint16_t periodic_adv_int, 94 std::vector<uint8_t> adv_data) = 0; 95 virtual void OnTrackAdvFoundLost( 96 AdvertisingTrackInfo advertising_track_info) = 0; 97 virtual void OnBatchScanReports(int client_if, int status, int report_format, 98 int num_records, 99 std::vector<uint8_t> data) = 0; 100 virtual void OnBatchScanThresholdCrossed(int client_if) = 0; 101 virtual void OnPeriodicSyncStarted(int reg_id, uint8_t status, 102 uint16_t sync_handle, 103 uint8_t advertising_sid, 104 uint8_t address_type, RawAddress address, 105 uint8_t phy, uint16_t interval) = 0; 106 virtual void OnPeriodicSyncReport(uint16_t sync_handle, int8_t tx_power, 107 int8_t rssi, uint8_t status, 108 std::vector<uint8_t> data) = 0; 109 virtual void OnPeriodicSyncLost(uint16_t sync_handle) = 0; 110 virtual void OnPeriodicSyncTransferred(int pa_source, uint8_t status, 111 RawAddress address) = 0; 112 virtual void OnBigInfoReport(uint16_t sync_handle, bool encrypted) = 0; 113 }; 114 115 class BleScannerInterface { 116 public: 117 virtual ~BleScannerInterface() = default; 118 119 using RegisterCallback = 120 base::Callback<void(uint8_t /* scanner_id */, uint8_t /* btm_status */)>; 121 122 using Callback = base::Callback<void(uint8_t /* btm_status */)>; 123 124 using EnableCallback = 125 base::Callback<void(uint8_t /* action */, uint8_t /* btm_status */)>; 126 127 using FilterParamSetupCallback = 128 base::Callback<void(uint8_t /* avbl_space */, uint8_t /* action_type */, 129 uint8_t /* btm_status */)>; 130 131 using FilterConfigCallback = 132 base::Callback<void(uint8_t /* filt_type */, uint8_t /* avbl_space */, 133 uint8_t /* action */, uint8_t /* btm_status */)>; 134 135 #if TARGET_FLOSS 136 using MsftAdvMonitorAddCallback = 137 base::Callback<void(uint8_t /* monitor_handle */, uint8_t /* status */)>; 138 139 using MsftAdvMonitorRemoveCallback = 140 base::Callback<void(uint8_t /* status */)>; 141 142 using MsftAdvMonitorEnableCallback = 143 base::Callback<void(uint8_t /* status */)>; 144 145 #endif 146 /** Registers a scanner with the stack */ 147 virtual void RegisterScanner(const bluetooth::Uuid& app_uuid, 148 RegisterCallback) = 0; 149 150 /** Unregister a scanner from the stack */ 151 virtual void Unregister(int scanner_id) = 0; 152 153 /** Start or stop LE device scanning */ 154 virtual void Scan(bool start) = 0; 155 156 /** Setup scan filter params */ 157 virtual void ScanFilterParamSetup( 158 uint8_t client_if, uint8_t action, uint8_t filt_index, 159 std::unique_ptr<btgatt_filt_param_setup_t> filt_param, 160 FilterParamSetupCallback cb) = 0; 161 162 /** Configure a scan filter condition */ 163 virtual void ScanFilterAdd(int filter_index, std::vector<ApcfCommand> filters, 164 FilterConfigCallback cb) = 0; 165 166 /** Clear all scan filter conditions for specific filter index*/ 167 virtual void ScanFilterClear(int filt_index, FilterConfigCallback cb) = 0; 168 169 /** Enable / disable scan filter feature*/ 170 virtual void ScanFilterEnable(bool enable, EnableCallback cb) = 0; 171 172 #if TARGET_FLOSS 173 /** Is MSFT Extension supported? */ 174 virtual bool IsMsftSupported() = 0; 175 176 /** Configures MSFT scan filter (advertisement monitor) */ 177 virtual void MsftAdvMonitorAdd(MsftAdvMonitor monitor, 178 MsftAdvMonitorAddCallback cb) = 0; 179 180 /** Removes previously added MSFT scan filter */ 181 virtual void MsftAdvMonitorRemove(uint8_t monitor_handle, 182 MsftAdvMonitorRemoveCallback cb) = 0; 183 184 /** Enable / disable MSFT scan filter feature */ 185 virtual void MsftAdvMonitorEnable(bool enable, 186 MsftAdvMonitorEnableCallback cb) = 0; 187 #endif 188 189 /** Sets the LE scan interval and window in units of N*0.625 msec */ 190 virtual void SetScanParameters(int scanner_id, uint8_t scan_type, 191 int scan_interval, int scan_window, 192 int scan_phy, Callback cb) = 0; 193 194 /* Configure the batchscan storage */ 195 virtual void BatchscanConfigStorage(int client_if, int batch_scan_full_max, 196 int batch_scan_trunc_max, 197 int batch_scan_notify_threshold, 198 Callback cb) = 0; 199 200 /* Enable batchscan */ 201 virtual void BatchscanEnable(int scan_mode, int scan_interval, 202 int scan_window, int addr_type, int discard_rule, 203 Callback cb) = 0; 204 205 /* Disable batchscan */ 206 virtual void BatchscanDisable(Callback cb) = 0; 207 208 /* Read out batchscan reports */ 209 virtual void BatchscanReadReports(int client_if, int scan_mode) = 0; 210 211 virtual void StartSync(uint8_t sid, RawAddress address, uint16_t skip, 212 uint16_t timeout, int reg_id) = 0; 213 virtual void StopSync(uint16_t handle) = 0; 214 215 virtual void RegisterCallbacks(ScanningCallbacks* callbacks) = 0; 216 217 virtual void CancelCreateSync(uint8_t sid, RawAddress address) = 0; 218 219 virtual void TransferSync(RawAddress address, uint16_t service_data, 220 uint16_t sync_handle, int pa_source) = 0; 221 virtual void TransferSetInfo(RawAddress address, uint16_t service_data, 222 uint8_t adv_handle, int pa_source) = 0; 223 virtual void SyncTxParameters(RawAddress addr, uint8_t mode, uint16_t skip, 224 uint16_t timeout, int reg_id) = 0; 225 }; 226 227 #endif /* ANDROID_INCLUDE_BLE_SCANNER_H */ 228