1e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie/*
2e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Copyright (C) 2016 The Android Open Source Project
3e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie *
4e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Licensed under the Apache License, Version 2.0 (the "License");
5e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * you may not use this file except in compliance with the License.
6e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * You may obtain a copy of the License at
7e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie *
8e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie *      http://www.apache.org/licenses/LICENSE-2.0
9e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie *
10e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * Unless required by applicable law or agreed to in writing, software
11e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * distributed under the License is distributed on an "AS IS" BASIS,
12e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * See the License for the specific language governing permissions and
14e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie * limitations under the License.
15e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */
16e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
17e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#ifndef CHRE_CORE_EVENT_LOOP_H_
18e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#define CHRE_CORE_EVENT_LOOP_H_
19e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
20e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#include "chre/core/event.h"
21e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#include "chre/core/nanoapp.h"
22fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol#include "chre/core/timer_pool.h"
23977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie#include "chre/platform/mutex.h"
242de17c9c5c541d72248f8371dfb4d7e402dc93e8Andrew Rossignol#include "chre/platform/platform_nanoapp.h"
25ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro#include "chre/platform/power_control_manager.h"
26c03d7df169f13195652de29f4452830130a0c53fAndrew Rossignol#include "chre/util/dynamic_vector.h"
27022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol#include "chre/util/fixed_size_blocking_queue.h"
28e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#include "chre/util/non_copyable.h"
29a1d5686c7513f6b8a436efd9f554d8c2227a2291Brian Duddie#include "chre/util/synchronized_memory_pool.h"
302de17c9c5c541d72248f8371dfb4d7e402dc93e8Andrew Rossignol#include "chre/util/unique_ptr.h"
3162f187d9d736346275492e916f6001576b68bb00Brian Duddie#include "chre_api/chre/event.h"
32e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
33e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddienamespace chre {
34e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
35e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie/**
36977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * The EventLoop represents a single thread of execution that is shared among
37977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * zero or more nanoapps. As the name implies, the EventLoop is built around a
38977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie * loop that delivers events to the nanoapps managed within for processing.
39e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie */
40e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddieclass EventLoop : public NonCopyable {
41e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie public:
42fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol  /**
43fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol   * Setup the event loop.
44fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol   */
45fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol  EventLoop();
46fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol
47977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  /**
48a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * Synchronous callback used with forEachNanoapp
49a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   */
50a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie  typedef void (NanoappCallbackFunction)(const Nanoapp *nanoapp, void *data);
51a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie
52a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie  /**
53977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * Searches the set of nanoapps managed by this EventLoop for one with the
54977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * given app ID. If found, provides its instance ID, which can be used to send
55977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * events to the app.
56977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *
57977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * This function is safe to call from any thread.
58977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *
59977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @param appId The nanoapp identifier to search for.
60977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @param instanceId If this function returns true, will be populated with the
61977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *        instanceId associated with the given appId; otherwise unmodified.
62977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *        Must not be null.
63977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @return true if the given app ID was found and instanceId was populated
64977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   */
652fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie  bool findNanoappInstanceIdByAppId(uint64_t appId, uint32_t *instanceId) const;
66e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
67e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  /**
68a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * Iterates over the list of Nanoapps managed by this EventLoop, and invokes
69a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * the supplied callback for each one. This holds a lock if necessary, so it
70a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * is safe to call from any thread.
71a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   *
72a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * @param callback Function to invoke on each Nanoapp (synchronously)
73a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * @param data Arbitrary data to pass to the callback
74a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   */
75a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie  void forEachNanoapp(NanoappCallbackFunction *callback, void *data);
76a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie
77a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie  /**
7809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * Invokes a message to host free callback supplied by the given nanoapp
7909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * (identified by app ID). Ensures that the calling context is updated
8009a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * appropriately.
8109a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   *
8209a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * @param appId Identifies the nanoapp that sent this message and supplied the
8309a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   *        free callback
8409a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * @param freeFunction The non-null message free callback given by the nanoapp
8509a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * @param message Pointer to the message data
8609a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * @param messageSize Size of the message
8709a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   */
8809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie  void invokeMessageFreeFunction(
8909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie      uint64_t appId, chreMessageFreeFunction *freeFunction, void *message,
9009a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie      size_t messageSize);
9109a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie
9209a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie  /**
939d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * Invokes the Nanoapp's start callback, and if successful, adds it to the
949d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * set of Nanoapps managed by this EventLoop. This function must only be
959d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * called from the context of the thread that runs this event loop (i.e. from
969d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * the same thread that will call run() or from a callback invoked within
979d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * run()).
98e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   *
999d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * @param nanoapp The nanoapp that will be started. Upon success, this
1009d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   *        UniquePtr will become invalid, as the underlying Nanoapp instance
1019d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   *        will have been transferred to be managed by this EventLoop.
1029d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie   * @return true if the app was started successfully
103e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   */
1049d5b500a223ef73560f0dce38f50b809bde5dd0dBrian Duddie  bool startNanoapp(UniquePtr<Nanoapp>& nanoapp);
105e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
106e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  /**
10799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Stops and unloads a nanoapp identified by its instance ID. The end entry
10899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * point will be invoked, and the chre::Nanoapp instance will be destroyed.
10999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * After this function returns, all references to the Nanoapp instance are
11099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * invalidated.
111e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   *
11299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * @param instanceId The nanoapp's unique instance identifier
11399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * @param allowSystemNanoappUnload If false, this function will reject
11499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   *        attempts to unload a system nanoapp
11599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   *
11699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * @return true if the nanoapp with the given instance ID was found & unloaded
117e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   */
11899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  bool unloadNanoapp(uint32_t instanceId, bool allowSystemNanoappUnload);
119e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
120e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  /**
121977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * Executes the loop that blocks on the event queue and delivers received
122977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * events to nanoapps. Only returns after stop() is called (from another
123977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * context).
124e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   */
125e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  void run();
126e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
127e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  /**
128e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   * Signals the event loop currently executing in run() to exit gracefully at
129f80ea015d077330cfdbd619680b250915ffcf1d2Brian Duddie   * the next available opportunity. This function is thread-safe.
130e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   */
131e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  void stop();
132e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
1334927ee586656424c827920876673228fbdcf27c3Andrew Rossignol  /**
1344927ee586656424c827920876673228fbdcf27c3Andrew Rossignol   * Posts an event to a nanoapp that is currently running (or all nanoapps if
135042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie   * the target instance ID is kBroadcastInstanceId).
136042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie   *
137042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie   * This function is safe to call from any thread.
1384927ee586656424c827920876673228fbdcf27c3Andrew Rossignol   *
1395d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @param eventType The type of data being posted.
1405d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @param eventData The data being posted.
1415d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @param freeCallback The callback to invoke when the event is no longer
1425d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   *        needed.
1435d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @param senderInstanceId The instance ID of the sender of this event.
1445d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @param targetInstanceId The instance ID of the destination of this event.
1454927ee586656424c827920876673228fbdcf27c3Andrew Rossignol   *
146a12d05f5b1b8bd8d953a328758426b7ca8b5e6deBrian Duddie   * @return true if the event was successfully added to the queue. Note that
147a12d05f5b1b8bd8d953a328758426b7ca8b5e6deBrian Duddie   *         unlike chreSendEvent, this does *not* invoke the free callback if
148a12d05f5b1b8bd8d953a328758426b7ca8b5e6deBrian Duddie   *         it failed to post the event.
1495d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   *
1505d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @see chreSendEvent
1514927ee586656424c827920876673228fbdcf27c3Andrew Rossignol   */
152042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie  bool postEvent(uint16_t eventType, void *eventData,
1534927ee586656424c827920876673228fbdcf27c3Andrew Rossignol                 chreEventCompleteFunction *freeCallback,
1544927ee586656424c827920876673228fbdcf27c3Andrew Rossignol                 uint32_t senderInstanceId = kSystemInstanceId,
1554927ee586656424c827920876673228fbdcf27c3Andrew Rossignol                 uint32_t targetInstanceId = kBroadcastInstanceId);
156e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
157e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  /**
158e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   * Returns a pointer to the currently executing Nanoapp, or nullptr if none is
159977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * currently executing. Must only be called from within the thread context
160977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * associated with this EventLoop.
161e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   *
162977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @return the currently executing nanoapp, or nullptr
163e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie   */
164b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol  Nanoapp *getCurrentNanoapp() const {
165b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol    return mCurrentApp;
166b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol  }
167e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
168e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  /**
169a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * Gets the number of nanoapps currently associated with this event loop. Must
170a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * only be called within the context of this EventLoop.
171a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   *
172a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   * @return The number of nanoapps managed by this event loop
173a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie   */
174b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol  size_t getNanoappCount() const {
175b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol    return mNanoapps.size();
176b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol  }
177a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie
178a2f5add27054c406f36528cf8a5cbc6d4c79d246Brian Duddie  /**
179725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol   * Obtains the TimerPool associated with this event loop.
180725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol   *
181725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol   * @return The timer pool owned by this event loop.
182725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol   */
183b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol  TimerPool& getTimerPool() {
184b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol    return mTimerPool;
185b75315a3e185b4559d73c69eea3b30679239fac1Andrew Rossignol  }
186725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol
18730f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol  /**
18830f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   * Searches the set of nanoapps managed by this EventLoop for one with the
18930f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   * given instance ID.
19030f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   *
19130f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   * This function is safe to call from any thread.
19230f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   *
19330f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   * @param instanceId The nanoapp instance ID to search for.
19430f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   * @return a pointer to the found nanoapp or nullptr if no match was found.
19530f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol   */
1962fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie  Nanoapp *findNanoappByInstanceId(uint32_t instanceId) const;
19730f18903edc1dfe46e12b9989760ef7a56cb31d3Andrew Rossignol
19899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  /**
19962f187d9d736346275492e916f6001576b68bb00Brian Duddie   * Looks for an app with the given ID and if found, populates info with its
20062f187d9d736346275492e916f6001576b68bb00Brian Duddie   * metadata. Safe to call from any thread.
20162f187d9d736346275492e916f6001576b68bb00Brian Duddie   *
20262f187d9d736346275492e916f6001576b68bb00Brian Duddie   * @see chreGetNanoappInfoByAppId
20362f187d9d736346275492e916f6001576b68bb00Brian Duddie   */
20462f187d9d736346275492e916f6001576b68bb00Brian Duddie  bool populateNanoappInfoForAppId(uint64_t appId,
20562f187d9d736346275492e916f6001576b68bb00Brian Duddie                                   struct chreNanoappInfo *info) const;
20662f187d9d736346275492e916f6001576b68bb00Brian Duddie
20762f187d9d736346275492e916f6001576b68bb00Brian Duddie  /**
20862f187d9d736346275492e916f6001576b68bb00Brian Duddie   * Looks for an app with the given instance ID and if found, populates info
20962f187d9d736346275492e916f6001576b68bb00Brian Duddie   * with its metadata. Safe to call from any thread.
21062f187d9d736346275492e916f6001576b68bb00Brian Duddie   *
21162f187d9d736346275492e916f6001576b68bb00Brian Duddie   * @see chreGetNanoappInfoByInstanceId
21262f187d9d736346275492e916f6001576b68bb00Brian Duddie   */
21362f187d9d736346275492e916f6001576b68bb00Brian Duddie  bool populateNanoappInfoForInstanceId(uint32_t instanceId,
21462f187d9d736346275492e916f6001576b68bb00Brian Duddie                                        struct chreNanoappInfo *info) const;
21562f187d9d736346275492e916f6001576b68bb00Brian Duddie
21662f187d9d736346275492e916f6001576b68bb00Brian Duddie  /**
21799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * @return true if the current Nanoapp (or entire CHRE) is being unloaded, and
21899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   *         therefore it should not be allowed to send events or messages, etc.
21999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   */
2202fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie  bool currentNanoappIsStopping() const;
22199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie
222d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro  /**
223d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   * Prints state in a string buffer. Must only be called from the context of
224d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   * the main CHRE thread.
225d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   *
226d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   * @param buffer Pointer to the start of the buffer.
227d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   * @param bufferPos Pointer to buffer position to start the print (in-out).
228d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   * @param size Size of the buffer in bytes.
229d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   *
230d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   * @return true if entire log printed, false if overflow or error.
231d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro   */
232d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro  bool logStateToBuffer(char *buffer, size_t *bufferPos,
233d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro                        size_t bufferSize) const;
234d0530f420d8bdaee755ec2834af407424de195e0Arthur Ishiguro
235ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro
236ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  /**
237ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro   * Returns a reference to the power control manager. This allows power
238ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro   * controls from subsystems outside the event loops.
239ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro   */
240ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  PowerControlManager& getPowerControlManager() {
241ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro    return mPowerControlManager;
242ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  }
243ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro
244e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie private:
2454927ee586656424c827920876673228fbdcf27c3Andrew Rossignol  //! The maximum number of events that can be active in the system.
24688f21c7ca23d96f6ef568a06c98f3a5c4538140fAndrew Rossignol  static constexpr size_t kMaxEventCount = 96;
2474927ee586656424c827920876673228fbdcf27c3Andrew Rossignol
248022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol  //! The maximum number of events that are awaiting to be scheduled. These
249022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol  //! events are in a queue to be distributed to apps.
25088f21c7ca23d96f6ef568a06c98f3a5c4538140fAndrew Rossignol  static constexpr size_t kMaxUnscheduledEventCount = 96;
251022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol
2524927ee586656424c827920876673228fbdcf27c3Andrew Rossignol  //! The memory pool to allocate incoming events from.
253f7914d0fbadf2644c0c4096380c741bee2a026e0Andrew Rossignol  SynchronizedMemoryPool<Event, kMaxEventCount> mEventPool;
2544927ee586656424c827920876673228fbdcf27c3Andrew Rossignol
255fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol  //! The timer used schedule timed events for tasks running in this event loop.
256fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol  TimerPool mTimerPool;
257fbb213b072fec0a14f437ad55685c4d95eec829eAndrew Rossignol
258c03d7df169f13195652de29f4452830130a0c53fAndrew Rossignol  //! The list of nanoapps managed by this event loop.
2592de17c9c5c541d72248f8371dfb4d7e402dc93e8Andrew Rossignol  DynamicVector<UniquePtr<Nanoapp>> mNanoapps;
260725bef45ca127971b3ea798cd1f4e4b2c0d8fe9dAndrew Rossignol
261977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  //! This lock *must* be held whenever we:
262977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  //!   (1) make changes to the mNanoapps vector, or
263977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  //!   (2) read the mNanoapps vector from a thread other than the one
264977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  //!       associated with this EventLoop
265977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  //! It is not necessary to acquire the lock when reading mNanoapps from within
266977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  //! the thread context of this EventLoop.
2672fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie  mutable Mutex mNanoappsLock;
268977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie
269c03d7df169f13195652de29f4452830130a0c53fAndrew Rossignol  //! The blocking queue of incoming events from the system that have not been
27099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  //! distributed out to apps yet.
2711795db1e2282dac8454cf59d23314f70b6930f7bAndrew Rossignol  FixedSizeBlockingQueue<Event *, kMaxUnscheduledEventCount> mEvents;
272e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
273977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  // TODO: should probably be atomic to be fully correct
2748c56d08e149720fcb99e6ddc71289ebb5ee594e6Brian Duddie  volatile bool mRunning = true;
275e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
27699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  //! The nanoapp that is currently executing - must be set any time we call
27799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  //! into the nanoapp's entry points or callbacks
278e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  Nanoapp *mCurrentApp = nullptr;
279e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
28099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  //! Set to the nanoapp we are in the process of unloading in unloadNanoapp()
28199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  Nanoapp *mStoppingNanoapp = nullptr;
28299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie
283ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  //! The object which manages power related controls.
284ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  PowerControlManager mPowerControlManager;
285ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro
28688f21c7ca23d96f6ef568a06c98f3a5c4538140fAndrew Rossignol  //! The maximum number of events ever waiting in the event pool.
28788f21c7ca23d96f6ef568a06c98f3a5c4538140fAndrew Rossignol  size_t mMaxEventPoolUsage = 0;
28888f21c7ca23d96f6ef568a06c98f3a5c4538140fAndrew Rossignol
28999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  /**
29099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Do one round of Nanoapp event delivery, only considering events in
29199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Nanoapps' own queues (not mEvents).
29299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   *
29399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * @return true if there are more events pending in Nanoapps' own queues
29499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   */
29599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  bool deliverEvents();
29699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie
297977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  /**
2985d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * Delivers the next event pending in the Nanoapp's queue, and takes care of
2995d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * freeing events once they have been delivered to all nanoapps. Must only be
3005d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * called after confirming that the app has at least 1 pending event.
3015d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   *
3025d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   * @return true if the nanoapp has another event pending in its queue
3035d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   */
3045d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie  bool deliverNextEvent(const UniquePtr<Nanoapp>& app);
3055d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie
3065d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie  /**
30799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Given an event pulled from the main incoming event queue (mEvents), deliver
30899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * it to all Nanoapps that should receive the event, or free the event if
30999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * there are no valid recipients.
31099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   *
31199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * @param event The Event to distribute to Nanoapps
31299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   */
31399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  void distributeEvent(Event *event);
31499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie
31599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  /**
31699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Distribute all events pending in the inbound event queue. Note that this
31799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * function only guarantees that any events in the inbound queue at the time
31899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * it is called will be distributed to Nanoapp event queues - new events may
31999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * still be posted during or after this function call from other threads as
32099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * long as postEvent() will accept them.
32199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   */
32299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  void flushInboundEventQueue();
32399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie
32499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  /**
32599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Delivers events pending in Nanoapps' own queues until they are all empty.
32699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   */
32799f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  void flushNanoappEventQueues();
32899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie
32999f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  /**
330977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * Call after when an Event has been delivered to all intended recipients.
331977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * Invokes the event's free callback (if given) and releases resources.
332977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *
333977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @param event The event to be freed
334977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   */
335e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie  void freeEvent(Event *event);
336e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
337977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie  /**
33809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * Finds a Nanoapp with the given 64-bit appId.
33909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   *
3402fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie   * Only safe to call within this EventLoop's thread, or if mNanoappsLock is
3412fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie   * held.
34209a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   *
34309a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * @param appId Nanoapp ID
34409a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   * @return Pointer to Nanoapp instance in this EventLoop with the given app
34509a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   *         ID, or nullptr if not found
34609a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie   */
3472fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie  Nanoapp *lookupAppByAppId(uint64_t appId) const;
34809a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie
34909a4783208b5ba1e14d0f0102eb31e688caca28dBrian Duddie  /**
350977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * Finds a Nanoapp with the given instanceId.
351977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *
3522fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie   * Only safe to call within this EventLoop's thread, or if mNanoappsLock is
3532fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie   * held.
354977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   *
355977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @param instanceId Nanoapp instance identifier
356977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   * @return Nanoapp with the given instanceId, or nullptr if not found
357977321d9ad051d4d6519a5fa8c2ca914f4d1424fBrian Duddie   */
3582fce52a25d14fcdcba888f8ff7892dffe3f55abaBrian Duddie  Nanoapp *lookupAppByInstanceId(uint32_t instanceId) const;
3595d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie
3605d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie  /**
36165d679851a2c062c26f685afb450f17bce177d29Brian Duddie   * Sends an event with payload struct chreNanoappInfo populated from the given
36265d679851a2c062c26f685afb450f17bce177d29Brian Duddie   * Nanoapp instance to inform other nanoapps about it starting/stopping.
36365d679851a2c062c26f685afb450f17bce177d29Brian Duddie   *
36465d679851a2c062c26f685afb450f17bce177d29Brian Duddie   * @param eventType Should be one of CHRE_EVENT_NANOAPP_{STARTED, STOPPED}
36565d679851a2c062c26f685afb450f17bce177d29Brian Duddie   * @param nanoapp The nanoapp instance whose status has changed
36665d679851a2c062c26f685afb450f17bce177d29Brian Duddie   */
36765d679851a2c062c26f685afb450f17bce177d29Brian Duddie  void notifyAppStatusChange(uint16_t eventType, const Nanoapp& nanoapp);
36865d679851a2c062c26f685afb450f17bce177d29Brian Duddie
36965d679851a2c062c26f685afb450f17bce177d29Brian Duddie  /**
37099f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * Stops and unloads the Nanoapp at the given index in mNanoapps.
37199f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   *
37299f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * IMPORTANT: prior to calling this function, the event queues must be in a
37399f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * safe condition for removal of this nanoapp. This means that there must not
37499f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * be any pending events in this nanoapp's queue, and there must not be any
37599f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * outstanding events sent by this nanoapp, as they may reference the
37699f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie   * nanoapp's own memory (even if there is no free callback).
3775d9b2d68940f7dac47bf2fbad6f6069ed3b2faaaBrian Duddie   */
37899f01ac9bd6e351538cb8922ef15d9e838e7339aBrian Duddie  void unloadNanoappAtIndex(size_t index);
379e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie};
380e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
381e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie}  // namespace chre
382e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie
383e64f180233e64c40b56993cfea3696c5b4b16395Brian Duddie#endif  // CHRE_CORE_EVENT_LOOP_H_
384