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_APP_MANAGER_H_ 18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_APP_MANAGER_H_ 19 20 #include <cstdint> 21 22 #include "location/lbs/contexthub/nanoapps/nearby/proto/nearby_extension.nanopb.h" 23 #include "location/lbs/contexthub/nanoapps/nearby/tracker_filter.h" 24 #include "location/lbs/contexthub/nanoapps/nearby/tracker_storage.h" 25 #include "third_party/contexthub/chre/util/include/chre/util/dynamic_vector.h" 26 #include "third_party/nanopb/pb.h" 27 #ifdef NEARBY_PROFILE 28 #include <ash/profile.h> 29 #endif 30 31 #include <chre.h> 32 33 #include "location/lbs/contexthub/nanoapps/nearby/adv_report_cache.h" 34 #include "location/lbs/contexthub/nanoapps/nearby/ble_scanner.h" 35 #include "location/lbs/contexthub/nanoapps/nearby/filter.h" 36 #include "location/lbs/contexthub/nanoapps/nearby/filter_extension.h" 37 #include "third_party/contexthub/chre/util/include/chre/util/singleton.h" 38 #include "third_party/contexthub/chre/util/include/chre/util/time.h" 39 40 namespace nearby { 41 42 // AppManager handles events from CHRE as well messages with host. 43 class AppManager : public TrackerStorageCallbackInterface { 44 friend class AppManagerTest; 45 46 public: 47 AppManager(); 48 // Returns true if AppManager is initialized successfully. 49 bool IsInitialized(); 50 51 // Handles an event from CHRE. 52 void HandleEvent(uint32_t sender_instance_id, uint16_t event_type, 53 const void *event_data); 54 55 private: 56 // Handles a message from host. 57 void HandleMessageFromHost(const chreMessageFromHostData *event); 58 59 // Acknowledge a host's SET_FILTER_REQUEST to indicate success or failure. 60 void RespondHostSetFilterRequest(bool success); 61 62 // Handles config request from the host. 63 void HandleHostConfigRequest(const uint8_t *message, uint32_t message_size); 64 65 // Handles advertise reports to match filters. 66 // Advertise reports will be cleared at the end of this function. 67 void HandleMatchAdvReports(AdvReportCache &adv_reports); 68 69 // If encoded byte size for filter results is larger than output buffer, sends 70 // each filter result. Otherwise, sends filter results together. 71 void SendBulkFilterResultsToHost( 72 const chre::DynamicVector<nearby_BleFilterResult> &filter_results); 73 74 // Serializes filter_results into stream after encoding as BleFilterResults. 75 // Returns false if encoding fails. 76 void SendFilterResultsToHost( 77 const chre::DynamicVector<nearby_BleFilterResult> &filter_results); 78 79 // Serializes a filter_result into stream after encoding as BleFilterResults. 80 // Returns false if encoding fails. 81 void SendFilterResultToHost(const nearby_BleFilterResult &filter_result); 82 83 // Updates Filter extension with event. Returns true if event is sent 84 // from an OEM service. 85 bool UpdateFilterExtension(const chreMessageFromHostData *event); 86 87 // Updates BLE scan state to start or stop based on filter configurations. 88 void UpdateBleScanState(); 89 90 // Encodes filter results as BleFilterResults format. Returns true if encoding 91 // was successful. 92 static bool EncodeFilterResults( 93 const chre::DynamicVector<nearby_BleFilterResult> &filter_results, 94 pb_ostream_t *stream, size_t *msg_size); 95 96 // Encodes a filter result as BleFilterResults format. Returns true if 97 // encoding was successful. 98 static bool EncodeFilterResult(const nearby_BleFilterResult &filter_result, 99 pb_ostream_t *stream, size_t *msg_size); 100 101 // Gets the expected encoded message size of filter results, which is 102 // equivalent to msg_size output of EncodeFilterResults() 103 static bool GetEncodedSizeFromFilterResults( 104 const chre::DynamicVector<nearby_BleFilterResult> &filter_results, 105 size_t &encoded_size); 106 107 // Handles extended config request from the host. 108 void HandleHostExtConfigRequest(const chreMessageFromHostData *event); 109 110 // Handles extended filter config request from the host. 111 bool HandleExtFilterConfig( 112 const chreHostEndpointInfo &host_info, 113 const nearby_extension_ExtConfigRequest_FilterConfig &config, 114 nearby_extension_ExtConfigResponse *config_response); 115 116 // Handles extended service config request from the host. 117 bool HandleExtServiceConfig( 118 const chreHostEndpointInfo &host_info, 119 const nearby_extension_ExtConfigRequest_ServiceConfig &config, 120 nearby_extension_ExtConfigResponse *config_response); 121 122 // BLE scan keep alive timer callback. 123 void OnBleScanKeepAliveTimerCallback(); 124 125 // Handles host awake event. 126 void HandleHostAwakeEvent(); 127 128 // Handles timer event. 129 void HandleTimerEvent(const void *event_data); 130 131 // Handles tracker filter config request from the host. 132 bool HandleExtTrackerFilterConfig( 133 const chreHostEndpointInfo &host_info, 134 const nearby_extension_ExtConfigRequest_TrackerFilterConfig &config, 135 nearby_extension_ExtConfigResponse *config_response); 136 137 // Handles flush tracker reports request from the host. 138 void HandleExtFlushTrackerReports( 139 const chreHostEndpointInfo &host_info, 140 const nearby_extension_ExtConfigRequest_FlushTrackerReports &config, 141 nearby_extension_ExtConfigResponse *config_response); 142 143 // Sends a tracker storage full event to host. 144 void SendTrackerStorageFullEventToHost(); 145 146 // Sends tracker reports to host. 147 void SendTrackerReportsToHost( 148 chre::DynamicVector<TrackerReport> &tracker_reports); 149 150 // TrackerStorageCallbackInterface API called when sending a tracker 151 // storage full event to host. 152 void OnTrackerStorageFullEvent() override; 153 154 static void SendExtConfigResponseToHost( 155 uint32_t request_id, uint16_t host_end_point, 156 nearby_extension_ExtConfigResponse &config_response); 157 158 static void SendFilterExtensionResultToHost( 159 chre::DynamicVector<FilterExtensionResult> &filter_results); 160 161 static const char *GetExtConfigNameFromTag(pb_size_t config_tag); 162 // TODO(b/193756395): Find the optimal size or compute the size in runtime. 163 // Note: the nanopb API pb_get_encoded_size 164 // (https://jpa.kapsi.fi/nanopb/docs/reference.html#pb_get_encoded_size) 165 // can only get the encoded message size if the message does not contained 166 // repeated fields. Otherwise, the repeated fields require Callback field 167 // encoders, which need a pb_ostream_t to work with, while pb_ostream_t is 168 // initialized by a buffer with the size to be determined. 169 // It seems possible to compute a message size with repeated field by 170 // rehearsing the encoding without actually storing in memory. Explore to 171 // enhance nanopb API to extend pb_get_encoded_size for repeated fields. 172 static constexpr size_t kFilterResultsBufSize = 400; 173 static constexpr size_t kTrackerReportsBufSize = 800; 174 // Default value for Fast Pair cache to expire. 175 static constexpr uint64_t kFpFilterResultExpireTimeNanoSec = 176 #ifdef USE_SHORT_FP_CACHE_TO 177 3 * chre::kOneSecondInNanoseconds; 178 #else 179 5 * chre::kOneSecondInNanoseconds; 180 #endif 181 182 Filter filter_; 183 FilterExtension filter_extension_; 184 BleScanner ble_scanner_; 185 TrackerFilter tracker_filter_; 186 TrackerStorage tracker_storage_; 187 188 uint16_t host_endpoint_ = 0; 189 bool screen_on_ = false; 190 bool fp_screen_on_sent_ = false; 191 AdvReportCache adv_reports_cache_; 192 chre::DynamicVector<nearby_BleFilterResult> fp_filter_cache_results_; 193 chre::DynamicVector<FilterExtensionResult> 194 screen_on_filter_extension_results_; 195 uint64_t fp_filter_cache_time_nanosec_; 196 uint64_t fp_filter_cache_expire_nanosec_ = kFpFilterResultExpireTimeNanoSec; 197 uint64_t last_tracker_report_flush_time_nanosec_; 198 #ifdef NEARBY_PROFILE 199 ashProfileData profile_data_; 200 #endif 201 }; 202 203 // The singleton AppManager that will be initialized safely. 204 typedef chre::Singleton<AppManager> AppManagerSingleton; 205 206 } // namespace nearby 207 208 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_APP_MANAGER_H_ 209