re.h revision 51116c21d9bdbe28cfea7d1439665cce0f75bd94
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_RE_H_ 18#define _CHRE_RE_H_ 19 20/** 21 * @file 22 * Some of the core Runtime Environment utilities of the Context Hub 23 * Runtime Environment. 24 * 25 * This includes functions for memory allocation, logging, and timers. 26 */ 27 28#include <stdarg.h> 29#include <stdbool.h> 30#include <stdint.h> 31#include <stdlib.h> 32 33#ifdef __cplusplus 34extern "C" { 35#endif 36 37/** 38 * The instance ID for the CHRE. 39 * 40 * This ID is used to identify events generated by the CHRE (as 41 * opposed to events generated by another nanoapp). 42 */ 43#define CHRE_INSTANCE_ID UINT32_C(0) 44 45/** 46 * A timer ID representing an invalid timer. 47 * 48 * This valid is returned by chreTimerSet() if a timer cannot be 49 * started. 50 */ 51#define CHRE_TIMER_INVALID UINT32_C(-1) 52 53 54/** 55 * Logging levels used to indicate severity level of logging messages. 56 * 57 * CHRE_LOG_ERROR: Something fatal has happened, i.e. something that will have 58 * user-visible consequences and won't be recoverable without explicitly 59 * deleting some data, uninstalling applications, wiping the data 60 * partitions or reflashing the entire phone (or worse). 61 * CHRE_LOG_WARN: Something that will have user-visible consequences but is 62 * likely to be recoverable without data loss by performing some explicit 63 * action, ranging from waiting or restarting an app all the way to 64 * re-downloading a new version of an application or rebooting the device. 65 * CHRE_LOG_INFO: Something interesting to most people happened, i.e. when a 66 * situation is detected that is likely to have widespread impact, though 67 * isn't necessarily an error. 68 * CHRE_LOG_DEBUG: Used to further note what is happening on the device that 69 * could be relevant to investigate and debug unexpected behaviors. You 70 * should log only what is needed to gather enough information about what 71 * is going on about your component. 72 * 73 * There is currently no API to turn on/off logging by level, but we anticipate 74 * adding such in future releases. 75 * 76 * @see chreLog 77 */ 78enum chreLogLevel { 79 CHRE_LOG_ERROR, 80 CHRE_LOG_WARN, 81 CHRE_LOG_INFO, 82 CHRE_LOG_DEBUG 83}; 84 85 86/** 87 * Get the application ID. 88 * 89 * The application ID is set by the loader of the nanoapp. This is not 90 * assured to be unique among all nanoapps running in the system. 91 * 92 * @returns The application ID. 93 */ 94uint64_t chreGetAppId(void); 95 96/** 97 * Get the instance ID. 98 * 99 * The instance ID is the CHRE handle to this nanoapp. This is assured 100 * to be unique among all nanoapps running in the system, and to be 101 * different from the CHRE_INSTANCE_ID. This is the ID used to communicate 102 * between nanoapps. 103 * 104 * @returns The instance ID 105 */ 106uint32_t chreGetInstanceId(void); 107 108/** 109 * A method for logging information about the system. 110 * 111 * A log entry can have a variety of levels (@see LogLevel). This function 112 * allows a variable number of arguments, in a printf-style format. 113 * 114 * A nanoapp needs to be able to rely upon consistent printf format 115 * recognition across any platform, and thus we establish formats which 116 * are required to be handled by every CHRE implementation. Some of the 117 * integral formats may seem obscure, but this API heavily uses types like 118 * uint32_t and uint16_t. The platform independent macros for those printf 119 * formats, like PRId32 or PRIx16, end up using some of these "obscure" 120 * formats on some platforms, and thus are required. 121 * 122 * For the initial N release, our emphasis is on correctly getting information 123 * into the log, and minimizing the requirements for CHRE implementations 124 * beyond that. We're not as concerned about how the information is visually 125 * displayed. As a result, there are a number of format sub-specifiers which 126 * are "OPTIONAL" for the N implementation. "OPTIONAL" in this context means 127 * that a CHRE implementation is allowed to essentially ignore the specifier, 128 * but it must understand the specifier enough in order to properly skip it. 129 * 130 * For a nanoapp author, an OPTIONAL format means you might not get exactly 131 * what you want on every CHRE implementation, but you will always get 132 * something sane. 133 * 134 * To be clearer, here's an example with the OPTIONAL 0-padding for integers 135 * for different hypothetical CHRE implementations. 136 * Compliant, chose to implement OPTIONAL format: 137 * chreLog(level, "%04x", 20) ==> "0014" 138 * Compliant, chose not to implement OPTIONAL format: 139 * chreLog(level, "%04x", 20) ==> "14" 140 * Non-compliant, discarded format because the '0' was assumed to be incorrect: 141 * chreLog(level, "%04x", 20) ==> "" 142 * 143 * Note that some of the OPTIONAL specifiers will probably become 144 * required in future APIs. 145 * 146 * We also have NOT_SUPPORTED specifiers. Nanoapp authors should not use any 147 * NOT_SUPPORTED specifiers, as unexpected things could happen on any given 148 * CHRE implementation. A CHRE implementation is allowed to support this 149 * (for example, when using shared code which already supports this), but 150 * nanoapp authors need to avoid these. 151 * 152 * 153 * Unless specifically noted as OPTIONAL or NOT_SUPPORTED, format 154 * (sub-)specifiers listed below are required. 155 * 156 * OPTIONAL format sub-specifiers: 157 * - '-' (left-justify within the given field width) 158 * - '+' (preceed the result with a '+' sign if it is positive) 159 * - ' ' (preceed the result with a blank space if no sign is going to be 160 * output) 161 * - '#' (For 'o', 'x' or 'X', preceed output with "0", "0x" or "0X", 162 * respectively. For floating point, unconditionally output a decimal 163 * point.) 164 * - '0' (left pad the number with zeroes instead of spaces when <width> 165 * needs padding) 166 * - <width> (A number representing the minimum number of characters to be 167 * output, left-padding with blank spaces if needed to meet the 168 * minimum) 169 * - '.'<precision> (A number which has different meaning depending on context.) 170 * - Integer context: Minimum number of digits to output, padding with 171 * leading zeros if needed to meet the minimum. 172 * - 'f' context: Number of digits to output after the decimal 173 * point (to the right of it). 174 * - 's' context: Maximum number of characters to output. 175 * 176 * Integral format specifiers: 177 * - 'd' (signed) 178 * - 'u' (unsigned) 179 * - 'o' (octal) 180 * - 'x' (hexadecimal, lower case) 181 * - 'X' (hexadecimal, upper case) 182 * 183 * Integral format sub-specifiers (as prefixes to an above integral format): 184 * - 'hh' (char) 185 * - 'h' (short) 186 * - 'l' (long) 187 * - 'll' (long long) 188 * - 'z' (size_t) 189 * - 't' (ptrdiff_t) 190 * 191 * Other format specifiers: 192 * - 'f' (floating point) 193 * - 'c' (character) 194 * - 's' (character string, terminated by '\0') 195 * - 'p' (pointer) 196 * - '%' (escaping the percent sign (i.e. "%%" becomes "%")) 197 * 198 * NOT_SUPPORTED specifiers: 199 * - 'n' (output nothing, but fill in a given pointer with the number 200 * of characters written so far) 201 * - '*' (indicates that the width/precision value comes from one of the 202 * arguments to the function) 203 * - 'e', 'E' (scientific notation output) 204 * - 'g', 'G' (Shortest floating point representation) 205 * 206 * @param level The severity level for this message. 207 * @param formatStr Either the entirety of the message, or a printf-style 208 * format string of the format documented above. 209 * @param ... A variable number of arguments necessary for the given 210 * 'formatStr' (there may be no additional arguments for some 'formatStr's). 211 */ 212void chreLog(enum chreLogLevel level, const char *formatStr, ...); 213 214/** 215 * Get the system time. 216 * 217 * This returns a time in nanoseconds in reference to some arbitrary 218 * time in the past. This method is only useful for determining timing 219 * between events on the system, and is not useful for determining 220 * any sort of absolute time. 221 * 222 * This value must always increase (and must never roll over). This 223 * value has no meaning across CHRE reboots. 224 * 225 * @returns The system time, in nanoseconds. 226 */ 227uint64_t chreGetTime(void); 228 229/** 230 * Retrieve CHRE's estimate of the time on the host, corresponding to the 231 * Android API SystemClock.elapsedRealtimeNanos(). 232 * 233 * A call to this function must not require waking up the host and should return 234 * quickly. The CHRE platform is expected to maintain the ability to estimate 235 * the host's clock using its own clock source, for example by applying an 236 * offset to the value it would return in chreGetTime(). 237 * 238 * @return An estimate of the current time on the host, accurate to within 239 * +/- 1.5 milliseconds. This estimate is not guaranteed to be 240 * monotonically increasing, and may move backwards as a result of receiving 241 * new information from the host. 242 * 243 * @since v1.1 244 */ 245uint64_t chreGetEstimatedHostTime(void); 246 247/** 248 * Set a timer. 249 * 250 * When the timer fires, nanoappHandleEvent will be invoked with 251 * CHRE_EVENT_TIMER and with the given 'cookie'. 252 * 253 * A CHRE implementation is required to provide at least 32 254 * timers. However, there's no assurance there will be any available 255 * for any given nanoapp (if it's loaded late, etc). 256 * 257 * @param duration Time, in nanoseconds, before the timer fires. 258 * @param cookie Argument that will be sent to nanoappHandleEvent upon the 259 * timer firing. This is allowed to be NULL and does not need to be 260 * a valid pointer (assuming the nanoappHandleEvent code is expecting such). 261 * @param oneShot If true, the timer will just fire once. If false, the 262 * timer will continue to refire every 'duration', until this timer is 263 * canceled (@see chreTimerCancel). 264 * 265 * @returns The timer ID. If the system is unable to set a timer 266 * (no more available timers, etc.) then CHRE_TIMER_INVALID will 267 * be returned. 268 * 269 * @see nanoappHandleEvent 270 */ 271uint32_t chreTimerSet(uint64_t duration, const void *cookie, bool oneShot); 272 273/** 274 * Cancel a timer. 275 * 276 * After this method returns, the CHRE assures there will be no more 277 * events sent from this timer, and any enqueued events from this timer 278 * will need to be evicted from the queue by the CHRE. 279 * 280 * @param timerId A timer ID obtained by this nanoapp via chreTimerSet(). 281 * @returns true if the timer was cancelled, false otherwise. We may 282 * fail to cancel the timer if it's a one shot which (just) fired, 283 * or if the given timer ID is not owned by the calling app. 284 */ 285bool chreTimerCancel(uint32_t timerId); 286 287/** 288 * Terminate this nanoapp. 289 * 290 * This takes effect immediately. 291 * 292 * The CHRE will no longer execute this nanoapp. The CHRE will not invoke 293 * nanoappEnd(), nor will it call any memory free callbacks in the nanoapp. 294 * 295 * The CHRE will unload/evict this nanoapp's code. 296 * 297 * @param abortCode A value indicating the reason for aborting. (Note that 298 * in this version of the API, there is no way for anyone to access this 299 * code, but future APIs may expose it.) 300 * @returns Never. This method does not return, as the CHRE stops nanoapp 301 * execution immediately. 302 */ 303void chreAbort(uint32_t abortCode); 304 305/** 306 * Allocate a given number of bytes from the system heap. 307 * 308 * The nanoapp is required to free this memory via chreHeapFree() prior to 309 * the nanoapp ending. 310 * 311 * While the CHRE implementation is required to free up heap resources of 312 * a nanoapp when unloading it, future requirements and tests focused on 313 * nanoapps themselves may check for memory leaks, and will require nanoapps 314 * to properly manage their heap resources. 315 * 316 * @param bytes The number of bytes requested. 317 * @returns A pointer to 'bytes' contiguous bytes of heap memory, or NULL 318 * if the allocation could not be performed. This pointer must be suitably 319 * aligned for any kind of variable. 320 * 321 * @see chreHeapFree. 322 */ 323void *chreHeapAlloc(uint32_t bytes); 324 325/** 326 * Free a heap allocation. 327 * 328 * This allocation must be from a value returned from a chreHeapAlloc() call 329 * made by this nanoapp. In other words, it is illegal to free memory 330 * allocated by another nanoapp (or the CHRE). 331 * 332 * @param ptr 'ptr' is required to be a value returned from chreHeapAlloc(). 333 * Note that since chreHeapAlloc can return NULL, CHRE 334 * implementations must safely handle 'ptr' being NULL. 335 * 336 * @see chreHeapAlloc. 337 */ 338void chreHeapFree(void *ptr); 339 340 341#ifdef __cplusplus 342} 343#endif 344 345#endif /* _CHRE_RE_H_ */ 346 347