1 /* 2 * Copyright (C) 2016 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 CHRE_CORE_EVENT_LOOP_MANAGER_H_ 18 #define CHRE_CORE_EVENT_LOOP_MANAGER_H_ 19 20 #include "chre_api/chre/event.h" 21 #include "chre/core/event_loop.h" 22 #include "chre/core/event_loop_common.h" 23 #include "chre/core/gnss_manager.h" 24 #include "chre/core/host_comms_manager.h" 25 #include "chre/core/sensor_request_manager.h" 26 #include "chre/core/wifi_request_manager.h" 27 #include "chre/core/wwan_request_manager.h" 28 #include "chre/platform/memory_manager.h" 29 #include "chre/platform/mutex.h" 30 #include "chre/util/fixed_size_vector.h" 31 #include "chre/util/non_copyable.h" 32 #include "chre/util/singleton.h" 33 #include "chre/util/unique_ptr.h" 34 35 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 36 #include "chre/core/audio_request_manager.h" 37 #endif // CHRE_AUDIO_SUPPORT_ENABLED 38 39 namespace chre { 40 41 /** 42 * A class that keeps track of all event loops in the system. This class 43 * represents the top-level object in CHRE. It will own all resources that are 44 * shared by all event loops. 45 */ 46 class EventLoopManager : public NonCopyable { 47 public: 48 /** 49 * Validates that a CHRE API is invoked from a valid nanoapp context and 50 * returns a pointer to the currently executing nanoapp. This should be 51 * called by most CHRE API methods that require accessing details about the 52 * event loop or the nanoapp itself. If the current event loop or nanoapp are 53 * null, this is an assertion error. 54 * 55 * @param functionName The name of the CHRE API. This should be __func__. 56 * @param eventLoop Optional output parameter, which will be populated with 57 * the EventLoop that is currently executing if this function is 58 * successful 59 * @return A pointer to the currently executing nanoapp or null if outside 60 * the context of a nanoapp. 61 */ 62 static Nanoapp *validateChreApiCall(const char *functionName); 63 64 /** 65 * Collect debugging information for this CHRE instance. Must only be called 66 * from the context of the main CHRE thread. 67 * 68 * @return Buffer containing debugging information stored in a null-terminated 69 * string allocated on the heap (possibly nullptr if the allocation 70 * failed) 71 */ 72 UniquePtr<char> debugDump(); 73 74 /** 75 * Leverages the event queue mechanism to schedule a CHRE system callback to 76 * be invoked at some point in the future from within the context of the 77 * "main" EventLoop. Which EventLoop is considered to be the "main" one is 78 * currently not specified, but it is required to be exactly one EventLoop 79 * that does not change at runtime. 80 * 81 * This function is safe to call from any thread. 82 * 83 * @param type An identifier for the callback, which is passed through to the 84 * callback as a uint16_t, and can also be useful for debugging 85 * @param data Arbitrary data to provide to the callback 86 * @param callback Function to invoke from within the main CHRE event loop 87 */ deferCallback(SystemCallbackType type,void * data,SystemCallbackFunction * callback)88 void deferCallback(SystemCallbackType type, void *data, 89 SystemCallbackFunction *callback) { 90 mEventLoop.postEvent(static_cast<uint16_t>(type), data, callback, 91 kSystemInstanceId, kSystemInstanceId); 92 } 93 94 /** 95 * Schedules a CHRE system callback to be invoked at some point in the future 96 * after a specified amount of time, in the context of the "main" CHRE 97 * EventLoop. 98 * 99 * This function is safe to call from any thread. 100 * 101 * @param type An identifier for the callback, which is passed through to the 102 * callback as a uint16_t, and can also be useful for debugging 103 * @param data Arbitrary data to provide to the callback 104 * @param callback Function to invoke from within the main CHRE event loop 105 * @param delay The delay to postpone posting the event 106 * @return TimerHandle of the requested timer. 107 * 108 * @see deferCallback 109 */ setDelayedCallback(SystemCallbackType type,void * data,SystemCallbackFunction * callback,Nanoseconds delay)110 TimerHandle setDelayedCallback(SystemCallbackType type, void *data, 111 SystemCallbackFunction *callback, 112 Nanoseconds delay) { 113 return mEventLoop.getTimerPool().setSystemTimer( 114 delay, callback, type, data); 115 } 116 117 /** 118 * Cancels a delayed callback previously scheduled by setDelayedCallback. 119 * 120 * This function is safe to call from any thread. 121 * 122 * @param timerHandle The TimerHandle returned by setDelayedCallback 123 * 124 * @return true if the callback was successfully cancelled 125 */ cancelDelayedCallback(TimerHandle timerHandle)126 bool cancelDelayedCallback(TimerHandle timerHandle) { 127 return mEventLoop.getTimerPool().cancelSystemTimer(timerHandle); 128 } 129 130 /** 131 * Returns a guaranteed unique instance identifier to associate with a newly 132 * constructed nanoapp. 133 * 134 * @return a unique instance ID 135 */ 136 uint32_t getNextInstanceId(); 137 138 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 139 /** 140 * @return A reference to the audio request manager. This allows interacting 141 * with the audio subsystem and manages requests from various 142 * nanoapps. 143 */ getAudioRequestManager()144 AudioRequestManager& getAudioRequestManager() { 145 return mAudioRequestManager; 146 } 147 #endif // CHRE_AUDIO_SUPPORT_ENABLED 148 149 /** 150 * @return The event loop managed by this event loop manager. 151 */ getEventLoop()152 EventLoop& getEventLoop() { 153 return mEventLoop; 154 } 155 156 #ifdef CHRE_GNSS_SUPPORT_ENABLED 157 /** 158 * @return A reference to the GNSS request manager. This allows interacting 159 * with the platform GNSS subsystem and manages requests from various 160 * nanoapps. 161 */ getGnssManager()162 GnssManager& getGnssManager() { 163 return mGnssManager; 164 } 165 #endif // CHRE_GNSS_SUPPORT_ENABLED 166 167 /** 168 * @return A reference to the host communications manager that enables 169 * transferring arbitrary data between the host processor and CHRE. 170 */ getHostCommsManager()171 HostCommsManager& getHostCommsManager() { 172 return mHostCommsManager; 173 } 174 175 /** 176 * @return Returns a reference to the sensor request manager. This allows 177 * interacting with the platform sensors and managing requests from 178 * various nanoapps. 179 */ getSensorRequestManager()180 SensorRequestManager& getSensorRequestManager() { 181 return mSensorRequestManager; 182 } 183 184 #ifdef CHRE_WIFI_SUPPORT_ENABLED 185 /** 186 * @return Returns a reference to the wifi request manager. This allows 187 * interacting with the platform wifi subsystem and manages the 188 * requests from various nanoapps. 189 */ getWifiRequestManager()190 WifiRequestManager& getWifiRequestManager() { 191 return mWifiRequestManager; 192 } 193 #endif // CHRE_WIFI_SUPPORT_ENABLED 194 195 #ifdef CHRE_WWAN_SUPPORT_ENABLED 196 /** 197 * @return A reference to the WWAN request manager. This allows interacting 198 * with the platform WWAN subsystem and manages requests from various 199 * nanoapps. 200 */ getWwanRequestManager()201 WwanRequestManager& getWwanRequestManager() { 202 return mWwanRequestManager; 203 } 204 #endif // CHRE_WWAN_SUPPORT_ENABLED 205 206 /** 207 * @return A reference to the memory manager. This allows central control of 208 * the heap space allocated by nanoapps. 209 */ getMemoryManager()210 MemoryManager& getMemoryManager() { 211 return mMemoryManager; 212 } 213 214 /** 215 * Performs second-stage initialization of things that are not necessarily 216 * required at construction time but need to be completed prior to executing 217 * any nanoapps. 218 */ 219 void lateInit(); 220 221 private: 222 //! The instance ID that was previously generated by getNextInstanceId() 223 uint32_t mLastInstanceId = kSystemInstanceId; 224 225 #ifdef CHRE_AUDIO_SUPPORT_ENABLED 226 //! The audio request manager handles requests for all nanoapps and manages 227 //! the state of the audio subsystem that the runtime subscribes to. 228 AudioRequestManager mAudioRequestManager; 229 #endif 230 231 //! The event loop managed by this event loop manager. 232 EventLoop mEventLoop; 233 234 #ifdef CHRE_GNSS_SUPPORT_ENABLED 235 //! The GnssManager that handles requests for all nanoapps. This manages the 236 //! state of the GNSS subsystem that the runtime subscribes to. 237 GnssManager mGnssManager; 238 #endif // CHRE_GNSS_SUPPORT_ENABLED 239 240 //! Handles communications with the host processor. 241 HostCommsManager mHostCommsManager; 242 243 //! The SensorRequestManager that handles requests for all nanoapps. This 244 //! manages the state of all sensors that runtime subscribes to. 245 SensorRequestManager mSensorRequestManager; 246 247 #ifdef CHRE_WIFI_SUPPORT_ENABLED 248 //! The WifiRequestManager that handles requests for nanoapps. This manages 249 //! the state of the wifi subsystem that the runtime subscribes to. 250 WifiRequestManager mWifiRequestManager; 251 #endif // CHRE_WIFI_SUPPORT_ENABLED 252 253 #ifdef CHRE_WWAN_SUPPORT_ENABLED 254 //! The WwanRequestManager that handles requests for nanoapps. This manages 255 //! the state of the WWAN subsystem that the runtime subscribes to. 256 WwanRequestManager mWwanRequestManager; 257 #endif // CHRE_WWAN_SUPPORT_ENABLED 258 259 //! The MemoryManager that handles malloc/free call from nanoapps and also 260 //! controls upper limits on the heap allocation amount. 261 MemoryManager mMemoryManager; 262 }; 263 264 //! Provide an alias to the EventLoopManager singleton. 265 typedef Singleton<EventLoopManager> EventLoopManagerSingleton; 266 267 //! Extern the explicit EventLoopManagerSingleton to force non-inline method 268 //! calls. This reduces codesize considerably. 269 extern template class Singleton<EventLoopManager>; 270 271 } // namespace chre 272 273 #endif // CHRE_CORE_EVENT_LOOP_MANAGER_H_ 274