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