• 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_NANOAPP_H_
18 #define CHRE_CORE_NANOAPP_H_
19 
20 #include <cinttypes>
21 
22 #include "chre/core/event.h"
23 #include "chre/core/event_ref_queue.h"
24 #include "chre/platform/platform_nanoapp.h"
25 #include "chre/util/dynamic_vector.h"
26 
27 namespace chre {
28 
29 /**
30  * A class that tracks the state of a Nanoapp including incoming events and
31  * event registrations.
32  *
33  * Inheritance is used to separate the common interface with common
34  * implementation part (chre::Nanoapp) from the common interface with
35  * platform-specific implementation part (chre::PlatformNanoapp) from the purely
36  * platform-specific part (chre::PlatformNanoappBase). However, this inheritance
37  * relationship does *not* imply polymorphism, and this object must only be
38  * referred to via the most-derived type, i.e. chre::Nanoapp.
39  */
40 class Nanoapp : public PlatformNanoapp {
41  public:
42   /**
43    * @return uint32_t The globally unique identifier for this Nanoapp instance
44    */
45   uint32_t getInstanceId() const;
46 
47   /**
48    * Assigns an instance ID to this Nanoapp. This must be called prior to
49    * starting this nanoapp.
50    */
51   void setInstanceId(uint32_t instanceId);
52 
53   /**
54    * @return true if the nanoapp should receive broadcast events with the given
55    *         type
56    */
57   bool isRegisteredForBroadcastEvent(uint16_t eventType) const;
58 
59   /**
60    * Updates the Nanoapp's registration so that it will receive broadcast events
61    * with the given event ID.
62    *
63    * @return true if the event is newly registered
64    */
65   bool registerForBroadcastEvent(uint16_t eventId);
66 
67   /**
68    * Updates the Nanoapp's registration so that it will not receive broadcast
69    * events with the given event ID.
70    *
71    * @return true if the event was previously registered
72    */
73   bool unregisterForBroadcastEvent(uint16_t eventId);
74 
75   /**
76    * Adds an event to this nanoapp's queue of pending events.
77    *
78    * @param event
79    */
80   void postEvent(Event *event);
81 
82   /**
83    * Indicates whether there are any pending events in this apps queue.
84    *
85    * @return True indicating that there are events available to be processed.
86    */
87   bool hasPendingEvent();
88 
89   /**
90    * Sends the next event in the queue to the nanoapp and returns the processed
91    * event. The hasPendingEvent() method should be tested before invoking this.
92    *
93    * @return a pointer to the processed event.
94    */
95   Event *processNextEvent();
96 
97  private:
98   uint32_t mInstanceId = kInvalidInstanceId;
99 
100   //! The set of broadcast events that this app is registered for.
101   // TODO: Implement a set container and replace DynamicVector here. There may
102   // also be a better way of handling this (perhaps we map event type to apps
103   // who care about them).
104   DynamicVector<uint16_t> mRegisteredEvents;
105 
106   EventRefQueue mEventQueue;
107 };
108 
109 }
110 
111 #endif  // CHRE_CORE_NANOAPP_H_
112