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