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_H_ 18 #define _CHRE_H_ 19 20 /** 21 * @file 22 * This header file includes all the headers which combine to fully define the 23 * interface for the Context Hub Runtime Environment (CHRE). This interface is 24 * of interest to both implementers of CHREs and authors of nanoapps. The API 25 * documentation attempts to address concerns of both. 26 * 27 * See individual header files for API details, and general comments below 28 * for overall platform information. 29 */ 30 31 #include <chre/audio.h> 32 #include <chre/common.h> 33 #include <chre/event.h> 34 #include <chre/gnss.h> 35 #include <chre/nanoapp.h> 36 #include <chre/re.h> 37 #include <chre/sensor.h> 38 #include <chre/toolchain.h> 39 #include <chre/version.h> 40 #include <chre/wifi.h> 41 #include <chre/wwan.h> 42 43 /** 44 * @mainpage 45 * CHRE is the Context Hub Runtime Environment. CHRE is used in Android to run 46 * contextual applications, called nanoapps, in a low-power processing domain 47 * other than the applications processor that runs Android itself. The CHRE 48 * API, documented herein, is the common interface exposed to nanoapps for any 49 * compatible CHRE implementation. The CHRE API provides the ability for 50 * creating nanoapps that are code-compatible across different CHRE 51 * implementations and underlying platforms. Refer to the following sections for 52 * a discussion on some important details of CHRE that aren't explicitly exposed 53 * in the API itself. 54 * 55 * @section entry_points Entry points 56 * 57 * The following entry points are used to bind a nanoapp to the CHRE system, and 58 * all three must be implemented by any nanoapp (see chre/nanoapp.h): 59 * - nanoappStart: initialization 60 * - nanoappHandleEvent: hook for event-driven processing 61 * - nanoappEnd: graceful teardown 62 * 63 * The CHRE implementation must also ensure that it performs these functions 64 * prior to invoking nanoappStart, or after nanoappEnd returns: 65 * - bss section zeroed out (prior to nanoappStart) 66 * - static variables initialized (prior to nanoappStart) 67 * - global C++ constructors called (prior to nanoappStart) 68 * - global C++ destructors called (after nanoappEnd) 69 * 70 * @section threading Threading model 71 * 72 * A CHRE implementation is free to choose among many different 73 * threading models, including a single-threaded system or a multi-threaded 74 * system with preemption. The current platform definition is agnostic to this 75 * underlying choice. However, the CHRE implementation must ensure that time 76 * spent executing within a nanoapp does not significantly degrade or otherwise 77 * interfere with other functions of the system in which CHRE is implemented, 78 * especially latency-sensitive tasks such as sensor event delivery to the AP. 79 * In other words, it must ensure that these functions can either occur in 80 * parallel or preempt a nanoapp's execution. The current version of the API 81 * does not specify whether the implementation allows for CPU sharing between 82 * nanoapps on a more granular level than the handling of individual events [1]. 83 * In any case, event ordering from the perspective of an individual nanoapp 84 * must be FIFO, but the CHRE implementation may choose to violate total 85 * ordering of events across all nanoapps to achieve more fair resource sharing, 86 * but this is not required. 87 * 88 * This version of the CHRE API does require that all nanoapps are treated as 89 * non-reentrant, meaning that only one instance of program flow can be inside 90 * an individual nanoapp at any given time. That is, any of the functions of 91 * the nanoapp, including the entry points and all other callbacks, cannot be 92 * invoked if a previous invocation to the same or any other function in the 93 * nanoapp has not completed yet. 94 * 95 * For example, if a nanoapp is currently in nanoappHandleEvent(), the CHRE is 96 * not allowed to call nanoappHandleEvent() again, or to call a memory freeing 97 * callback. Similarly, if a nanoapp is currently in a memory freeing 98 * callback, the CHRE is not allowed to call nanoappHandleEvent(), or invoke 99 * another memory freeing callback. 100 * 101 * There are two exceptions to this rule: If an invocation of chreSendEvent() 102 * fails (returns 'false'), it is allowed to immediately invoke the memory 103 * freeing callback passed into that function. This is a rare case, and one 104 * where otherwise a CHRE implementation is likely to leak memory. Similarly, 105 * chreSendMessageToHost() is allowed to invoke the memory freeing callback 106 * directly, whether it returns 'true' or 'false'. This is because the CHRE 107 * implementation may copy the message data to its own buffer, and therefore 108 * wouldn't need the nanoapp-supplied buffer after chreSendMessageToHost() 109 * returns. 110 * 111 * For a nanoapp author, this means no thought needs to be given to 112 * synchronization issues with global objects, as they will, by definition, 113 * only be accessed by a single thread at once. 114 * 115 * [1]: Note to CHRE implementers: A future version of the CHRE platform may 116 * require multi-threading with preemption. This is mentioned as a heads up, 117 * and to allow implementors deciding between implementation approaches to 118 * make the most informed choice. 119 * 120 * @section timing Timing 121 * 122 * Nanoapps should expect to be running on a highly constrained system, with 123 * little memory and little CPU. Any single nanoapp should expect to 124 * be one of several nanoapps on the system, which also share the CPU with the 125 * CHRE and possibly other services as well. 126 * 127 * Thus, a nanoapp needs to be efficient in its memory and CPU usage. 128 * Also, as noted in the Threading Model section, a CHRE implementation may 129 * be single threaded. As a result, all methods invoked in a nanoapp 130 * (like nanoappStart, nanoappHandleEvent, memory free callbacks, etc.) 131 * must run "quickly". "Quickly" is difficult to define, as there is a 132 * diversity of Context Hub hardware. Nanoapp authors are strongly recommended 133 * to limit their application to consuming no more than 1 second of CPU time 134 * prior to returning control to the CHRE implementation. A CHRE implementation 135 * may consider a nanoapp as unresponsive if it spends more time than this to 136 * process a single event, and take corrective action. 137 * 138 * A nanoapp may have the need to occasionally perform a large block of 139 * calculations that exceeds the 1 second guidance. The recommended approach in 140 * this case is to split up the large block of calculations into smaller 141 * batches. In one call into the nanoapp, the nanoapp can perform the first 142 * batch, and then set a timer or send an event (chreSendEvent()) to itself 143 * indicating which batch should be done next. This will allow the nanoapp to 144 * perform the entire calculation over time, without monopolizing system 145 * resources. 146 * 147 * @section floats Floating point support 148 * 149 * The C type 'float' is used in this API, and thus a CHRE implementation 150 * is required to support 'float's. 151 * 152 * Support of the C types 'double' and 'long double' is optional for a 153 * CHRE implementation. Note that if a CHRE decides to support them, unlike 154 * 'float' support, there is no requirement that this support is particularly 155 * efficient. So nanoapp authors should be aware this may be inefficient. 156 * 157 * If a CHRE implementation chooses not to support 'double' or 158 * 'long double', then the build toolchain setup provided needs to set 159 * the preprocessor define CHRE_NO_DOUBLE_SUPPORT. 160 * 161 * @section compat CHRE and Nanoapp compatibility 162 * 163 * CHRE implementations must make affordances to maintain binary compatibility 164 * across minor revisions of the API version (e.g. v1.1 to v1.2). This applies 165 * to both running a nanoapp compiled for a newer version of the API on a CHRE 166 * implementation built against an older version (backwards compatibility), and 167 * vice versa (forwards compatibility). API changes that are acceptable in 168 * minor version changes that may require special measures to ensure binary 169 * compatibility include: addition of new functions; addition of arguments to 170 * existing functions when the default value used for nanoapps compiled against 171 * the old version is well-defined and does not affect existing functionality; 172 * and addition of fields to existing structures, even when this induces a 173 * binary layout change (this should be made rare via judicious use of reserved 174 * fields). API changes that must only occur alongside a major version change 175 * and are therefore not compatible include: removal of any function, argument, 176 * field in a data structure, or mandatory functional behavior that a nanoapp 177 * may depend on; any change in the interpretation of an existing data structure 178 * field that alters the way it was defined previously (changing the units of a 179 * field would fall under this, but appropriating a previously reserved field 180 * for some new functionality would not); and any change in functionality or 181 * expected behavior that conflicts with the previous definition. 182 * 183 * Note that the CHRE API only specifies the software interface between a 184 * nanoapp and the CHRE system - the binary interface (ABI) between nanoapp and 185 * CHRE is necessarily implementation-dependent. Therefore, the recommended 186 * approach to accomplish binary compatibility is to build a Nanoapp Support 187 * Library (NSL) that is specific to the CHRE implementation into the nanoapp 188 * binary, and use it to handle ABI details in a way that ensures compatibility. 189 * In addition, to accomplish forwards compatibility, the CHRE implementation is 190 * expected to recognize the CHRE API version that a nanoapp is targeting and 191 * engage compatibility behaviors where necessary. 192 * 193 * By definition, major API version changes (e.g. v1.1 to v2.0) break 194 * compatibility. Therefore, a CHRE implementation must not attempt to load a 195 * nanoapp that is targeting a newer major API version. 196 */ 197 198 #endif /* _CHRE_H_ */ 199 200