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