1 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_TRACKER_STORAGE_H_ 2 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_TRACKER_STORAGE_H_ 3 4 #include <cstddef> 5 #include <cstdint> 6 #include <utility> 7 8 #include "chre_api/chre.h" 9 #include "third_party/contexthub/chre/util/include/chre/util/dynamic_vector.h" 10 #include "third_party/contexthub/chre/util/include/chre/util/unique_ptr.h" 11 12 namespace nearby { 13 14 // The callback interface for tracker storage events. 15 class TrackerStorageCallbackInterface { 16 public: 17 virtual ~TrackerStorageCallbackInterface() = default; 18 19 // Is called when sending a batch storage full event. 20 virtual void OnTrackerStorageFullEvent() = 0; 21 }; 22 23 struct TrackerBatchConfig { 24 // Minimum sampling interval to update tracker history. 25 uint32_t sample_interval_ms = {60000}; 26 // Maximum number of tracker reports that can be stored in storage. 27 uint32_t max_tracker_count = {30}; 28 // Notification threshold of the number of tracker reports, which should be 29 // equal to or smaller than max_tracker_count. 30 uint32_t notify_threshold_tracker_count = {28}; 31 // Maximum number of tracker histories that can be stored in tracker report. 32 uint32_t max_history_count = {20}; 33 // Timeout for tracker history to be considered lost. 34 uint32_t lost_timeout_ms = {60000}; 35 // Time based threshold for opportunistic flush of tracker reports. 36 uint32_t opportunistic_flush_threshold_time_ms = {4294967295}; 37 }; 38 39 enum class TrackerState { 40 kPresent, 41 kAbsent, 42 }; 43 44 struct TrackerHistory { 45 // Constructor to set the current time and default values. TrackerHistoryTrackerHistory46 explicit TrackerHistory(uint32_t current_time_ms) 47 : found_count(1), 48 first_found_time_ms(current_time_ms), 49 last_found_time_ms(current_time_ms), 50 last_radio_discovery_time_ms(current_time_ms), 51 lost_time_ms(0), 52 state(TrackerState::kPresent) {} 53 // The number of times the tracker report was found at each sampling interval 54 // when in the Present state. 55 uint32_t found_count; 56 // The time when the tracker report was first discovered when it was not in 57 // the present state, and the time when the tracker history was created. 58 uint32_t first_found_time_ms; 59 // The most recent time when the tracker report was discovered for each 60 // sampling period in the Present state. 61 uint32_t last_found_time_ms; 62 // The most recent time when the tracker report was discovered by the LE 63 // radio, regardless of the sampling period or the tracker state. 64 uint32_t last_radio_discovery_time_ms; 65 // The time at which the tracker report was lost. Only valid when the tracker 66 // state is Absent. 67 uint32_t lost_time_ms; 68 // The latest state of the tracker history. 69 TrackerState state; 70 }; 71 72 struct TrackerReport { 73 // Default constructor. 74 TrackerReport() = default; 75 76 // Move constructor. TrackerReportTrackerReport77 TrackerReport(TrackerReport &&other) { 78 if (&other == this) { 79 return; 80 } 81 header = other.header; 82 data = std::move(other.data); 83 historian = std::move(other.historian); 84 } 85 // Header of advertisement for the key report. 86 chreBleAdvertisingReport header; 87 // Data of advertisement for the key report. 88 chre::UniquePtr<uint8_t[]> data; 89 // Tracker history for the key report. 90 chre::DynamicVector<TrackerHistory> historian; 91 }; 92 93 class TrackerStorage { 94 public: 95 // Constructs tracker storage. 96 TrackerStorage() = default; 97 98 // Adds advertise report to tracker storage. 99 void Push(const chreBleAdvertisingReport &report, 100 const TrackerBatchConfig &config); 101 102 // Updates the tracker history for present and absent trackers in the storage. 103 void Refresh(const TrackerBatchConfig &config); 104 105 // Clears tracker storage. Clear()106 void Clear() { 107 tracker_reports_.clear(); 108 } 109 110 // Return tracker batch reports in storage. GetBatchReports()111 chre::DynamicVector<TrackerReport> &GetBatchReports() { 112 return tracker_reports_; 113 } 114 115 // Sets tracker storage event callback. SetCallback(TrackerStorageCallbackInterface * callback)116 void SetCallback(TrackerStorageCallbackInterface *callback) { 117 callback_ = callback; 118 } 119 120 private: 121 // Default size reserved for tracker history in creating a new tracker report. 122 static constexpr size_t kDefaultTrackerHistorySize = 2; 123 124 // Tracker batch reports. 125 // TODO(b/341757839): Optimize tracker storage memory using 126 // chre::SegmentedQueue to minimize heap fragmentation. 127 chre::DynamicVector<TrackerReport> tracker_reports_; 128 129 // Tracker storage event callback. 130 TrackerStorageCallbackInterface *callback_ = nullptr; 131 132 // Updates tracker report in pushing advertisement. 133 void UpdateTrackerReport(TrackerReport &tracker_report, 134 const TrackerBatchConfig &config, 135 const chreBleAdvertisingReport &report); 136 137 // Adds a new tracker report to tracker storage. 138 void AddTrackerReport(const chreBleAdvertisingReport &report, 139 const TrackerBatchConfig &config); 140 141 // Adds or updates advertising data for tracker report. 142 // For a newly added tracker report, it will allocate memory for advertising 143 // data, and copy the advertising data from the advertising report. 144 // For an existing tracker report, it will check if the advertising data is 145 // different from the previous one. If the length is the same but the payload 146 // is different, it will update the tracker report by copying the advertising 147 // data from the advertising report. If the length is different, it will 148 // update the tracker report by re-allocating memory for advertising data, and 149 // copying the advertising data from the advertising report. 150 // If the advertising data is the same as the previous one, it will not do 151 // anything. 152 void AddOrUpdateAdvertisingData(TrackerReport &tracker_report, 153 const chreBleAdvertisingReport &report); 154 155 // Returns whether advertising address is same. 156 bool IsEqualAddress(const TrackerReport &tracker_report, 157 const chreBleAdvertisingReport &report) const; 158 159 // Returns current time in milliseconds. 160 uint32_t GetCurrentTimeMs() const; 161 }; 162 163 } // namespace nearby 164 165 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_TRACKER_STORAGE_H_ 166