xref: /aosp_15_r20/system/chre/apps/nearby/location/lbs/contexthub/nanoapps/nearby/ble_scanner.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2023 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 LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_BLE_SCANNER_H_
18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_BLE_SCANNER_H_
19 
20 #include <chre.h>
21 
22 #include "third_party/contexthub/chre/util/include/chre/util/dynamic_vector.h"
23 #include "third_party/contexthub/chre/util/include/chre/util/time.h"
24 
25 namespace nearby {
26 
27 struct GenericFilters {
28   uint16_t end_point;
29   chre::DynamicVector<chreBleGenericFilter> filters;
30 
GenericFiltersGenericFilters31   explicit GenericFilters(uint16_t end_point) : end_point(end_point) {}
32 
33   friend bool operator==(const GenericFilters &c1, const GenericFilters &c2) {
34     return c1.end_point == c2.end_point;
35   }
36 
37   friend bool operator!=(const GenericFilters &c1, const GenericFilters &c2) {
38     return c1.end_point != c2.end_point;
39   }
40 };
41 
42 class BleScanner {
43  public:
44   // Default value for report delay of batch scan results in low latency mode.
45   static constexpr uint32_t kBatchScanReportDelayLowLatencyMilliSec = 0;
46 
47   // Default value for report delay of batch scan results in low power mode.
48   static constexpr uint32_t kBatchScanReportDelayLowPowerMilliSec = 3000;
49 
50   // Default value for BLE scan keep alive timer interval.
51   static constexpr uint64_t kKeepAliveTimerIntervalNanoSec =
52       60 * chre::kOneSecondInNanoseconds;
53 
54   // Constructs BLE Scanner and checks whether BLE batch scan is supported.
55   BleScanner();
56 
57   // Starts BLE scan. If scan already started, nothing happens.
58   void Start();
59 
60   // Stops BLE scan.
61   void Stop();
62 
63   // Flushes the batched scan results.
64   // Returns whether flush operation proceeds.
65   bool Flush();
66 
67   // Returns whether BLE batch scan is flushing.
IsFlushing()68   bool IsFlushing() {
69     return is_batch_flushing_;
70   }
71 
72   // Returns whether BLE scan is running.
isScanning()73   bool isScanning() {
74     return is_started_;
75   }
76 
77   // Returns true if BLE scan is available in the device.
isAvailable()78   bool isAvailable() {
79     return is_ble_scan_supported_;
80   }
81 
82   // Returns whether BLE batch scan is supported.
IsBatchSupported()83   bool IsBatchSupported() {
84     return is_batch_supported_;
85   }
86 
87   // Updates extended generic filters. Caller needs to call Restart() for the
88   // updated filters to be effective. Returns true for successful update.
89   bool UpdateFilters(
90       uint16_t host_end_point,
91       chre::DynamicVector<chreBleGenericFilter> *generic_filters);
92 
93   // Updates the tracker filters.
UpdateTrackerFilters(chre::DynamicVector<chreBleGenericFilter> & filters)94   void UpdateTrackerFilters(
95       chre::DynamicVector<chreBleGenericFilter> &filters) {
96     tracker_filters_ = std::move(filters);
97   }
98 
99   // Updates the report delay of batch scan
100   void UpdateBatchDelay(uint32_t delay_ms);
101 
102   // Handles an event from CHRE.
103   void HandleEvent(uint16_t event_type, const void *event_data);
104 
105   // Starts BLE scan. If scan already started, replacing the previous scan.
106   void Restart();
107 
108   // Sets default generic filters.
SetDefaultFilters()109   void SetDefaultFilters() {
110     is_default_generic_filter_enabled_ = true;
111   }
112 
113   // Clears default generic filters.
ClearDefaultFilters()114   void ClearDefaultFilters() {
115     is_default_generic_filter_enabled_ = false;
116   }
117 
118   // Sets tracker filters.
SetTrackerFilters()119   void SetTrackerFilters() {
120     is_tracker_filter_enabled_ = true;
121   }
122 
123   // Clears tracker filters.
ClearTrackerFilters()124   void ClearTrackerFilters() {
125     is_tracker_filter_enabled_ = false;
126   }
127 
128   // Returns whether the filter list contains the given filter.
129   bool ContainsFilter(const chre::DynamicVector<chreBleGenericFilter> &filters,
130                       const chreBleGenericFilter &src);
131 
132   // Starts BLE scan keep alive timer.
133   void StartKeepAliveTimer();
134 
135   // Stops BLE scan keep alive timer.
136   void StopKeepAliveTimer();
137 
138   // Sets BLE scan keep alive timer interval.
SetKeepAliveTimerInterval(uint64_t interval_ns)139   void SetKeepAliveTimerInterval(uint64_t interval_ns) {
140     keep_alive_timer_interval_ns_ = interval_ns;
141   }
142 
143  private:
144   // Whether BLE scan is started.
145   bool is_started_ = false;
146 
147   // Whether BLE scan is supported.
148   bool is_ble_scan_supported_ = true;
149 
150   // Whether BLE batch scan is supported.
151   bool is_batch_supported_ = false;
152 
153   // Whether BLE batch scan is flushing.
154   bool is_batch_flushing_ = false;
155 
156   // Whether default generic filter is enabled.
157   bool is_default_generic_filter_enabled_ = false;
158 
159   // Whether tracker filter is enabled.
160   bool is_tracker_filter_enabled_ = false;
161 
162   // Current report delay for BLE batch scan
163   uint32_t report_delay_ms_ = 0;
164 
165   // Current BLE scan mode
166   chreBleScanMode scan_mode_ = CHRE_BLE_SCAN_MODE_BACKGROUND;
167 
168   // Current BLE scan keep alive timer interval.
169   uint64_t keep_alive_timer_interval_ns_ = kKeepAliveTimerIntervalNanoSec;
170 
171   chre::DynamicVector<GenericFilters> generic_filters_list_;
172   chre::DynamicVector<chreBleGenericFilter> tracker_filters_;
173 };
174 
175 }  // namespace nearby
176 
177 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_BLE_SCANNER_H_
178