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