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