xref: /aosp_15_r20/system/chre/apps/nearby/location/lbs/contexthub/nanoapps/nearby/tracker_filter.h (revision 84e339476a462649f82315436d70fd732297a399)
1 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_TRACKER_FILTER_H_
2 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_TRACKER_FILTER_H_
3 
4 #include <stdbool.h>
5 
6 #include <cstdint>
7 
8 #include "chre_api/chre.h"
9 #include "location/lbs/contexthub/nanoapps/nearby/byte_array.h"
10 #include "location/lbs/contexthub/nanoapps/nearby/proto/nearby_extension.nanopb.h"
11 #include "location/lbs/contexthub/nanoapps/nearby/timer.h"
12 #include "location/lbs/contexthub/nanoapps/nearby/tracker_storage.h"
13 #include "third_party/contexthub/chre/util/include/chre/util/dynamic_vector.h"
14 
15 namespace nearby {
16 
17 struct TrackerScanFilterConfig {
18   chre::DynamicVector<chreBleGenericFilter> hardware_filters;
19   int8_t rssi_threshold = CHRE_BLE_RSSI_THRESHOLD_NONE;
20   // Active interval for tracker scan filter. The tracker scan filter is enabled
21   // at the beginning of the active interval and disabled at the end of the
22   // active window. This creates a toggle effect for the tracker scan filter and
23   // reduces the BLE scan power consumption. If the interval and window are not
24   // set by host, the default values are 0, and the tracker scan filter is
25   // always enabled.
26   uint32_t active_interval_ms;
27   // Active window for tracker scan filter.
28   uint32_t active_window_ms;
29 };
30 
31 class TrackerFilter {
32  public:
33   // Updates scan filter and batch configurations.
34   // Returns generic_filters, which can be used to restart BLE scan through
35   // BleScanner::UpdateTrackerFilters() and BleScanner::Restart(). Refers to
36   // AppManager::HandleExtTrackerFilterConfig. Regarding to the ownership of
37   // hardware filters, generic_filters are saved in two places - TrackerFilter
38   // and BleScanner. TrackerFilter uses the saved generic_filters for matching
39   // advertisements and BleScanner uses the saved generic_filters for
40   // reconfiguring scan configuration whenever hardsare scan filters are
41   // updated. If config_response->result is not CHREX_NEARBY_RESULT_OK, the
42   // returned generic_filters should be ignored.
43   void Update(const chreHostEndpointInfo &host_info,
44               const nearby_extension_ExtConfigRequest_TrackerFilterConfig
45                   &filter_config,
46               chre::DynamicVector<chreBleGenericFilter> *generic_filters,
47               nearby_extension_ExtConfigResponse *config_response);
48 
49   // Matches BLE advertisements and pushes the matched advertisements to
50   // tracker storage.
51   void MatchAndSave(
52       const chre::DynamicVector<chreBleAdvertisingReport> &ble_adv_reports,
53       TrackerStorage &tracker_storage);
54 
55   // Whether tracker filter is empty. Currently, we're checking only hardware
56   // scan filters used for tracker filter.
IsEmpty()57   bool IsEmpty() const {
58     return scan_filter_config_.hardware_filters.empty();
59   }
60 
61   // Returns host end point for tracker filter.
GetHostEndPoint()62   uint16_t GetHostEndPoint() const {
63     return host_info_.hostEndpointId;
64   }
65 
66   // Returns batch configuration for tracker filter.
GetBatchConfig()67   TrackerBatchConfig GetBatchConfig() const {
68     return batch_config_;
69   }
70 
71   // Encodes a single tracker report into data_buf.
72   static bool EncodeTrackerReport(TrackerReport &tracker_report,
73                                   ByteArray data_buf, size_t *encoded_size);
74 
75   // Sets tracker scan filter active state.
SetActiveState()76   void SetActiveState() {
77     is_active_ = true;
78   }
79 
80   // Clears tracker scan filter active state.
ClearActiveState()81   void ClearActiveState() {
82     is_active_ = false;
83   }
84 
85   // Returns whether tracker scan filter is active.
IsActive()86   bool IsActive() {
87     return is_active_;
88   }
89 
GetActiveIntervalTimer()90   Timer &GetActiveIntervalTimer() {
91     return active_interval_timer_;
92   }
93 
GetActiveWindowTimer()94   Timer &GetActiveWindowTimer() {
95     return active_window_timer_;
96   }
97 
98  private:
99   TrackerScanFilterConfig scan_filter_config_;
100   TrackerBatchConfig batch_config_;
101   chreHostEndpointInfo host_info_;
102   // whether the tracker scan filter is active.
103   bool is_active_ = false;
104 
105   // Configures tracker scan filter active state.
106   void ConfigureActiveState();
107 
108   // Configures tracker scan filter control timers when updating scan filter and
109   // batch configurations.
110   void ConfigureScanControlTimers();
111 
112   // Timer for tracker scan filter active interval.
113   Timer active_interval_timer_ = Timer(false);
114   // Timer for tracker scan filter active window.
115   Timer active_window_timer_ = Timer(true);
116 };
117 
118 }  // namespace nearby
119 
120 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_TRACKER_FILTER_H_
121