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