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