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