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