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