• 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_request_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/mutex.h"
28 #include "chre/util/dynamic_vector.h"
29 #include "chre/util/non_copyable.h"
30 #include "chre/util/singleton.h"
31 #include "chre/util/unique_ptr.h"
32 
33 namespace chre {
34 
35 //! An identifier for a system callback, which is mapped into a CHRE event type
36 //! in the user-defined range.
37 enum class SystemCallbackType : uint16_t {
38   FirstCallbackType = CHRE_EVENT_FIRST_USER_VALUE,
39 
40   MessageToHostComplete,
41   WifiScanMonitorStateChange,
42   WifiRequestScanResponse,
43   WifiHandleScanEvent,
44   NanoappListResponse,
45   SensorLastEventUpdate,
46   FinishLoadingNanoapp,
47 };
48 
49 //! The function signature of a system callback mirrors the CHRE event free
50 //! callback to allow it to use the same event infrastructure.
51 typedef chreEventCompleteFunction SystemCallbackFunction;
52 
53 /**
54  * A class that keeps track of all event loops in the system. This class
55  * represents the top-level object in CHRE. It will own all resources that are
56  * shared by all event loops.
57  */
58 class EventLoopManager : public NonCopyable {
59  public:
60    /**
61     * Validates that a CHRE API is invoked from a valid nanoapp context and
62     * returns a pointer to the currently executing nanoapp. This should be
63     * called by most CHRE API methods that require accessing details about the
64     * event loop or the nanoapp itself. If the current event loop or nanoapp are
65     * null, this is an assertion error.
66     *
67     * @param functionName The name of the CHRE API. This should be __func__.
68     * @return A pointer to the currently executing nanoapp or null if outside
69     *         the context of a nanoapp.
70     */
71   static Nanoapp *validateChreApiCall(const char *functionName);
72 
73   /**
74    * Constructs an event loop and returns a pointer to it. The event loop is not
75    * started by this method.
76    *
77    * @return A pointer to an event loop. If creation fails, nullptr is returned.
78    */
79   EventLoop *createEventLoop();
80 
81   /**
82    * Leverages the event queue mechanism to schedule a CHRE system callback to
83    * be invoked at some point in the future from within the context of the
84    * "main" EventLoop. Which EventLoop is considered to be the "main" one is
85    * currently not specified, but it is required to be exactly one EventLoop
86    * that does not change at runtime.
87    *
88    * This function is safe to call from any thread.
89    *
90    * @param type An identifier for the callback, which is passed through to the
91    *        callback as a uint16_t, and can also be useful for debugging
92    * @param data Arbitrary data to provide to the callback
93    * @param callback Function to invoke from within the
94    */
95   bool deferCallback(SystemCallbackType type, void *data,
96                      SystemCallbackFunction *callback);
97 
98   /**
99    * Search all event loops to look up the instance ID associated with a Nanoapp
100    * via its app ID, and optionally the EventLoop that is hosting it.
101    *
102    * Note that this function makes the assumption that there is only one
103    * instance of a given appId executing within CHRE at any given time, i.e. the
104    * mapping between appId and instanceId is 1:1. This assumption holds due to
105    * restrictions imposed in v1.0 of the context hub HAL, but is not guaranteed
106    * for all future versions.
107    *
108    * This function is safe to call from any thread.
109    *
110    * @param appId The nanoapp ID to search for
111    * @param instanceId On success, will be populated with the instanceId
112    *        associated with the app. Must not be null.
113    * @param eventLoop If not null, will be populated with a pointer to the
114    *        EventLoop where this nanoapp executes
115    * @return true if an app with the given ID was found
116    */
117   bool findNanoappInstanceIdByAppId(uint64_t appId, uint32_t *instanceId,
118                                     EventLoop **eventLoop = nullptr);
119 
120   /**
121    * Search all event loops to find a nanoapp with a given instance ID.
122    *
123    * This function is safe to call from any thread.
124    *
125    * @param instanceId The nanoapp instance ID to search for.
126    * @return a pointer to the found nanoapp or nullptr.
127    */
128   Nanoapp *findNanoappByInstanceId(uint32_t instanceId,
129                                    EventLoop **eventLoop = nullptr);
130 
131   /**
132    * Returns a guaranteed unique instance identifier to associate with a newly
133    * constructed nanoapp.
134    *
135    * @return a unique instance ID
136    */
137   uint32_t getNextInstanceId();
138 
139   /**
140    * Posts an event to all event loops owned by this event loop manager. This
141    * method is thread-safe and is used to post events that all event loops would
142    * be interested in, such as sensor event data.
143    *
144    * @param The type of data being posted.
145    * @param The data being posted.
146    * @param The callback to invoke when the event is no longer needed.
147    * @param The instance ID of the sender of this event.
148    * @param The instance ID of the destination of this event.
149    * @return true if the event was successfully sent to all nanoapps targeted
150    *         by the instance ID (ie: for broadcast, all nanoapps were sent the
151    *         event).
152    */
153   bool postEvent(uint16_t eventType, void *eventData,
154                  chreEventCompleteFunction *freeCallback,
155                  uint32_t senderInstanceId = kSystemInstanceId,
156                  uint32_t targetInstanceId = kBroadcastInstanceId);
157 
158   /**
159    * @return A reference to the GNSS request manager. This allows interacting
160    *         with the platform GNSS subsystem and manages requests from various
161    *         nanoapps.
162    */
163   GnssRequestManager& getGnssRequestManager();
164 
165   /**
166    * @return A reference to the host communications manager that enables
167    *         transferring arbitrary data between the host processor and CHRE.
168    */
169   HostCommsManager& getHostCommsManager();
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    */
176   SensorRequestManager& getSensorRequestManager();
177 
178   /**
179    * @return Returns a reference to the wifi request manager. This allows
180    *         interacting with the platform wifi subsystem and manages the
181    *         requests from various nanoapps.
182    */
183   WifiRequestManager& getWifiRequestManager();
184 
185   /**
186    * @return A reference to the WWAN request manager. This allows interacting
187    *         with the platform WWAN subsystem and manages requests from various
188    *         nanoapps.
189    */
190   WwanRequestManager& getWwanRequestManager();
191 
192  private:
193   //! The mutex used to ensure that postEvent() completes for all event loops
194   //! before another thread can start posting an event. This ensures consistency
195   //! of event order between event loops.
196   Mutex mMutex;
197 
198   //! The instance ID that was previously generated by getNextInstanceId()
199   uint32_t mLastInstanceId = kSystemInstanceId;
200 
201   //! The list of event loops managed by this event loop manager. The EventLoops
202   //! are stored in UniquePtr because they are large objects. They do not
203   //! provide an implementation of the move constructor so it is best left to
204   //! allocate each event loop and manage the pointers to those event loops.
205   DynamicVector<UniquePtr<EventLoop>> mEventLoops;
206 
207   //! The GnssRequestManager that handles requests for all nanoapps. This
208   //! manages the state of the GNSS subsystem that the runtime subscribes to.
209   GnssRequestManager mGnssRequestManager;
210 
211   //! Handles communications with the host processor
212   HostCommsManager mHostCommsManager;
213 
214   //! The SensorRequestManager that handles requests for all nanoapps. This
215   //! manages the state of all sensors that runtime subscribes to.
216   SensorRequestManager mSensorRequestManager;
217 
218   //! The WifiRequestManager that handles requests for nanoapps. This manages
219   //! the state of the wifi subsystem that the runtime subscribes to.
220   WifiRequestManager mWifiRequestManager;
221 
222   //! The WwanRequestManager that handles requests for nanoapps. This manages
223   //! the state of the WWAN subsystem that the runtime subscribes to.
224   WwanRequestManager mWwanRequestManager;
225 };
226 
227 //! Provide an alias to the EventLoopManager singleton.
228 typedef Singleton<EventLoopManager> EventLoopManagerSingleton;
229 
230 }  // namespace chre
231 
232 #endif  // CHRE_CORE_EVENT_LOOP_MANAGER_H_
233