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_PLATFORM_PLATFORM_NANOAPP_H_
18 #define CHRE_PLATFORM_PLATFORM_NANOAPP_H_
19 
20 #include <cstdint>
21 #include <cstddef>
22 
23 #include "chre/util/non_copyable.h"
24 #include "chre/target_platform/platform_nanoapp_base.h"
25 
26 namespace chre {
27 
28 /**
29  * The common interface to Nanoapp functionality that has platform-specific
30  * implementation but must be supported for every platform.
31  */
32 class PlatformNanoapp : public PlatformNanoappBase, public NonCopyable {
33  public:
34   /**
35    * Calls the start function of the nanoapp. For dynamically loaded nanoapps,
36    * this must also result in calling through to any of the nanoapp's static
37    * global constructors/init functions, etc., prior to invoking the
38    * nanoappStart.
39    *
40    * @return true if the app was able to start successfully
41    *
42    * @see nanoappStart
43    */
44   bool start();
45 
46   /**
47    * Passes an event to the nanoapp.
48    *
49    * @see nanoappHandleEvent
50    */
51   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
52                    const void *eventData);
53 
54   /**
55    * Calls the nanoapp's end callback. For dynamically loaded nanoapps, this
56    * must also result in calling through to any of the nanoapp's static global
57    * destructors, atexit functions, etc., after nanoappEnd returns. This is only
58    * valid to call after start() has returned true.
59    *
60    * This function must leave the nanoapp in a state where it can be started
61    * again via start().
62    *
63    * @see nanoappEnd
64    */
65   void end();
66 
67   /**
68    * Retrieves the nanoapp's 64-bit identifier. This function must always return
69    * a valid identifier - either the one supplied by the host via the HAL (from
70    * the header), or the authoritative value inside the nanoapp binary if one
71    * exists. In the event that both are available and they do not match, the
72    * platform implementation must return false from start().
73    */
74   uint64_t getAppId() const;
75 
76   /**
77    * Retrieves the nanoapp's own version number. The same restrictions apply
78    * here as for getAppId().
79    *
80    * @see #getAppId
81    */
82   uint32_t getAppVersion() const;
83 
84   /**
85    * Retrieves the API version that this nanoapp was compiled against. This
86    * function must only be called while the nanoapp is running (i.e. between
87    * calls to start() and end()).
88    */
89   uint32_t getTargetApiVersion() const;
90 
91   /**
92    * Returns true if the nanoapp should not appear in the context hub HAL list
93    * of nanoapps, e.g. because it implements some device functionality purely
94    * beneath the HAL.
95    */
96   bool isSystemNanoapp() const;
97 
98   /**
99    * Prints state in a string buffer. Must only be called from the context of
100    * the main CHRE thread.
101    *
102    * @param buffer Pointer to the start of the buffer.
103    * @param bufferPos Pointer to buffer position to start the print (in-out).
104    * @param size Size of the buffer in bytes.
105    */
106   void logStateToBuffer(char *buffer, size_t *bufferPos,
107                         size_t bufferSize) const;
108 
109  protected:
110   /**
111    * PlatformNanoapp's constructor is protected, as it must only exist within
112    * the context of the derived class chre::Nanoapp.
113    */
114   PlatformNanoapp() = default;
115 
116   /**
117    * Unloads the nanoapp from memory.
118    */
119   ~PlatformNanoapp();
120 };
121 
122 }  // namespace chre
123 
124 #endif  // CHRE_PLATFORM_PLATFORM_NANOAPP_H_
125