1*84e33947SAndroid Build Coastguard Worker# Nanoapp Developer Guide 2*84e33947SAndroid Build Coastguard Worker 3*84e33947SAndroid Build Coastguard Worker[TOC] 4*84e33947SAndroid Build Coastguard Worker 5*84e33947SAndroid Build Coastguard WorkerSince CHRE is an open platform, anyone can write nanoapps. However, deploying to 6*84e33947SAndroid Build Coastguard Workera device requires cooperation of the device manufacturer, because CHRE is a 7*84e33947SAndroid Build Coastguard Workersecurity-sensitive trusted environment, similar to other low-level firmware, and 8*84e33947SAndroid Build Coastguard Workerit typically has tight resource constraints. This section assumes you have a 9*84e33947SAndroid Build Coastguard Workerbasic understanding of what a nanoapp is (if not, see the Nanoapp Overview 10*84e33947SAndroid Build Coastguard Workersection), and provides some simple instructions to help you get started with 11*84e33947SAndroid Build Coastguard Workerdeveloping your own nanoapp. 12*84e33947SAndroid Build Coastguard Worker 13*84e33947SAndroid Build Coastguard Worker## Getting Started 14*84e33947SAndroid Build Coastguard Worker 15*84e33947SAndroid Build Coastguard WorkerWhen starting a new nanoapp, it’s helpful to start with the skeleton of an 16*84e33947SAndroid Build Coastguard Workerexisting nanoapp. The simplest example can be found at `apps/hello_world`. Start 17*84e33947SAndroid Build Coastguard Workerby copying this folder to the location where you will develop the nanoapp - it 18*84e33947SAndroid Build Coastguard Workercan be outside of the `system/chre` project, for example in a vendor-specific 19*84e33947SAndroid Build Coastguard WorkerGit repository in the `vendor/` folder of the Android tree. 20*84e33947SAndroid Build Coastguard Worker 21*84e33947SAndroid Build Coastguard WorkerIf you don’t plan to use this nanoapp as a *static nanoapp* (see the Nanoapp 22*84e33947SAndroid Build Coastguard WorkerOverview for details), remove the `hello_world.mk` file and delete the code 23*84e33947SAndroid Build Coastguard Workerblocks wrapped in `#ifdef CHRE_NANOAPP_INTERNAL`. Rename the remaining files to 24*84e33947SAndroid Build Coastguard Workermatch your nanoapp. 25*84e33947SAndroid Build Coastguard Worker 26*84e33947SAndroid Build Coastguard Worker### Picking a Nanoapp ID 27*84e33947SAndroid Build Coastguard Worker 28*84e33947SAndroid Build Coastguard WorkerNanoapps are uniquely identified by a 64-bit number. The most significant 5 29*84e33947SAndroid Build Coastguard Workerbytes of this number are the vendor identifier, and the remaining bytes identify 30*84e33947SAndroid Build Coastguard Workerthe nanoapp within the vendor’s namespace. The vendor identifier is usually 31*84e33947SAndroid Build Coastguard Workerdevised from an ASCII representation of the vendor’s name, for example Google 32*84e33947SAndroid Build Coastguard Workeruses 0x476F6F676C (“Googl”). The remaining portion of the ID is typically just 33*84e33947SAndroid Build Coastguard Workeran incrementing value for each nanoapp. 34*84e33947SAndroid Build Coastguard Worker 35*84e33947SAndroid Build Coastguard WorkerRefer to `system/chre/chre_api/include/chre_api/chre/common.h` and 36*84e33947SAndroid Build Coastguard Worker`util/include/chre/util/nanoapp/app_id.h` for some examples and utilities. 37*84e33947SAndroid Build Coastguard Worker 38*84e33947SAndroid Build Coastguard WorkerBe sure to pick a unique nanoapp ID when creating a new nanoapp. 39*84e33947SAndroid Build Coastguard Worker 40*84e33947SAndroid Build Coastguard Worker### Picking a Language 41*84e33947SAndroid Build Coastguard Worker 42*84e33947SAndroid Build Coastguard WorkerCHRE guarantees support for nanoapps written in C11 or C++17, though not all 43*84e33947SAndroid Build Coastguard Workerstandard library functions are supported (see below for details). For a 44*84e33947SAndroid Build Coastguard Workerdevice-specific nanoapp, additional programming languages/versions *may* be 45*84e33947SAndroid Build Coastguard Workersupported, but this can impact portability. 46*84e33947SAndroid Build Coastguard Worker 47*84e33947SAndroid Build Coastguard Worker### Building the Nanoapp Binary 48*84e33947SAndroid Build Coastguard Worker 49*84e33947SAndroid Build Coastguard WorkerWhile it’s possible to build a nanoapp with a different build system, just as it 50*84e33947SAndroid Build Coastguard Workeris for the CHRE framework, it’s recommended to use the common build system 51*84e33947SAndroid Build Coastguard Workerincluded in this project, as it makes it easy to support a variety of target 52*84e33947SAndroid Build Coastguard Workerplatforms. The rest of this section assumes you are using the CHRE build system 53*84e33947SAndroid Build Coastguard Workerto create a non-static nanoapp. 54*84e33947SAndroid Build Coastguard Worker 55*84e33947SAndroid Build Coastguard WorkerUpdate the `Makefile` in your nanoapp’s directory to: 56*84e33947SAndroid Build Coastguard Worker 57*84e33947SAndroid Build Coastguard Worker* Define nanoapp metadata, including: 58*84e33947SAndroid Build Coastguard Worker * `NANOAPP_NAME`: sets the output filename of the binary 59*84e33947SAndroid Build Coastguard Worker * `NANOAPP_ID`: 64-bit identifier, in hexadecimal format 60*84e33947SAndroid Build Coastguard Worker * `NANOAPP_VERSION`: 32-bit version, in hexadecimal format (see versioning 61*84e33947SAndroid Build Coastguard Worker section below) 62*84e33947SAndroid Build Coastguard Worker * `NANOAPP_NAME_STRING`, `NANOAPP_VENDOR_STRING`: human-readable strings for 63*84e33947SAndroid Build Coastguard Worker the name of the nanoapp and vendor, respectively 64*84e33947SAndroid Build Coastguard Worker * `NANOAPP_IS_SYSTEM_NANOAPP`: 0 or 1 (see Nanoapp Overview) 65*84e33947SAndroid Build Coastguard Worker* Populate `COMMON_SRCS` with the C or C++ source files to compile 66*84e33947SAndroid Build Coastguard Worker* Populate `COMMON_CFLAGS` with compiler flags, like additional include paths 67*84e33947SAndroid Build Coastguard Worker* Include any additional `.mk` files for vendor extensions, etc. before `app.mk` 68*84e33947SAndroid Build Coastguard Worker 69*84e33947SAndroid Build Coastguard WorkerRefer to `build/nanoapp/app.mk` for full details. 70*84e33947SAndroid Build Coastguard Worker 71*84e33947SAndroid Build Coastguard WorkerThe nanoapp can then be built using a command like ``OPT_LEVEL=s make 72*84e33947SAndroid Build Coastguard Worker<build_target> -j`nproc` `` (see the CHRE Framework Build System section for 73*84e33947SAndroid Build Coastguard Workerdetails on build targets), which will produce build artifacts at 74*84e33947SAndroid Build Coastguard Worker`out/<build_target>/<nanoapp_name>.*`. 75*84e33947SAndroid Build Coastguard Worker 76*84e33947SAndroid Build Coastguard Worker### Loading onto a Device 77*84e33947SAndroid Build Coastguard Worker 78*84e33947SAndroid Build Coastguard WorkerExact steps to load a nanoapp binary can vary by device, but for developing a 79*84e33947SAndroid Build Coastguard Workerpreloaded nanoapp, this typically involves the following steps: 80*84e33947SAndroid Build Coastguard Worker 81*84e33947SAndroid Build Coastguard Worker* Perform any needed post-processing of the nanoapp binary to permit it to be 82*84e33947SAndroid Build Coastguard Worker loaded (such as signing with a development or production key) 83*84e33947SAndroid Build Coastguard Worker* Write the binary to the device’s storage (for example, using `adb push`) 84*84e33947SAndroid Build Coastguard Worker* Update `preloaded_nanoapps.json` or other configuration as needed, so that 85*84e33947SAndroid Build Coastguard Worker CHRE knows to load the new nanoapp 86*84e33947SAndroid Build Coastguard Worker* Restart CHRE to reload all nanoapps, including the new one 87*84e33947SAndroid Build Coastguard Worker 88*84e33947SAndroid Build Coastguard Worker## Nanoapp Versioning 89*84e33947SAndroid Build Coastguard Worker 90*84e33947SAndroid Build Coastguard WorkerWhile not strictly enforced, nanoapps are recommended to follow the convention 91*84e33947SAndroid Build Coastguard Workerof the CHRE framework and use [Semantic Versioning](http://semver.org). In the 92*84e33947SAndroid Build Coastguard Workercase of a nanoapp, the key versioned “API” is considered the interface between 93*84e33947SAndroid Build Coastguard Workerthe client and nanoapp. Nanoapp versions are represented as a 32-bit integer, 94*84e33947SAndroid Build Coastguard Workerwhere the most significant byte represents the major version, followed by one 95*84e33947SAndroid Build Coastguard Workerbyte for the minor version, and two bytes for the patch version. 96*84e33947SAndroid Build Coastguard Worker 97*84e33947SAndroid Build Coastguard Worker## Using the CHRE API 98*84e33947SAndroid Build Coastguard Worker 99*84e33947SAndroid Build Coastguard WorkerThe CHRE API is the key interface between each nanoapp and the underlying 100*84e33947SAndroid Build Coastguard Workersystem. Refer to the extensive API documentation in the header files at 101*84e33947SAndroid Build Coastguard Worker`chre_api/include`, as well as usage of the APIs by sample nanoapps. The CHRE 102*84e33947SAndroid Build Coastguard WorkerAPI is normally included via ` 103*84e33947SAndroid Build Coastguard Worker#include "chre_api/chre.h"`. 104*84e33947SAndroid Build Coastguard Worker## Utility Libraries 105*84e33947SAndroid Build Coastguard Worker 106*84e33947SAndroid Build Coastguard WorkerSome source and header files under `util` are specifically designed to aid in 107*84e33947SAndroid Build Coastguard Workernanoapp development, and others were initially created for use in the framework 108*84e33947SAndroid Build Coastguard Workerbut can be leveraged by nanoapps as well. In general, any source and header file 109*84e33947SAndroid Build Coastguard Workerthere that does **not** include a header from `chre/platform` (part of the 110*84e33947SAndroid Build Coastguard Workerinternal CHRE framework implementation details) may be used by a nanoapp, and 111*84e33947SAndroid Build Coastguard Workerfiles within a subdirectory called `nanoapp` are specifically targeted for use 112*84e33947SAndroid Build Coastguard Workerby nanoapps. 113*84e33947SAndroid Build Coastguard Worker 114*84e33947SAndroid Build Coastguard WorkerThis includes `util/include/chre/util/nanoapp/log.h` (meant to be included via 115*84e33947SAndroid Build Coastguard Worker`#include “chre/util/nanoapp/log.h”`), which provides macros like `LOGD` which 116*84e33947SAndroid Build Coastguard Workercan be conditionally compiled, include a configurable prefix to help identify 117*84e33947SAndroid Build Coastguard Workerthe sender, and suppress double promotion warnings. 118*84e33947SAndroid Build Coastguard Worker 119*84e33947SAndroid Build Coastguard WorkerThe utilities library also includes a number of container classes, which are 120*84e33947SAndroid Build Coastguard Workermeant to mimic the C++ standard library, but with a lightweight, CHRE-compatible 121*84e33947SAndroid Build Coastguard Workerimplementation. This includes: 122*84e33947SAndroid Build Coastguard Worker 123*84e33947SAndroid Build Coastguard Worker* `chre::DynamicVector`: an `std::vector` analogue 124*84e33947SAndroid Build Coastguard Worker* `chre::FixedSizeVector`: accessed like `std::vector`, but only uses statically 125*84e33947SAndroid Build Coastguard Worker allocated memory 126*84e33947SAndroid Build Coastguard Worker* `chre::ArrayQueue`: can be used as a circular buffer 127*84e33947SAndroid Build Coastguard Worker* `chre::UniquePtr`: an `std::unique_ptr` analogue 128*84e33947SAndroid Build Coastguard Worker* `chre::Optional`: an analogue to `std::optional` from C++17 129*84e33947SAndroid Build Coastguard Worker* `chre::Singleton`: a container for a statically allocated object with explicit 130*84e33947SAndroid Build Coastguard Worker initialization and deinitialization (e.g. enables construction of a global 131*84e33947SAndroid Build Coastguard Worker object to be deferred until `nanoappStart()`) 132*84e33947SAndroid Build Coastguard Worker 133*84e33947SAndroid Build Coastguard Worker## Interacting with the Host 134*84e33947SAndroid Build Coastguard Worker 135*84e33947SAndroid Build Coastguard WorkerNanoapps can interact with one or more clients on the host (applications 136*84e33947SAndroid Build Coastguard Workerprocessor) through a flexible binary message-passing interface. For simple 137*84e33947SAndroid Build Coastguard Workerinteractions in cases where the lowest memory footprint is desired, using only 138*84e33947SAndroid Build Coastguard Workerthe built-in message type field with no additional payload, or passing 139*84e33947SAndroid Build Coastguard Workerhand-rolled packed C-style structures (e.g. using Java’s ByteBuffer on the 140*84e33947SAndroid Build Coastguard Workerclient side) can work, though this approach can be error-prone. Using a 141*84e33947SAndroid Build Coastguard Workerwell-defined serialization format, such as Protocol Buffers (see the Using 142*84e33947SAndroid Build Coastguard WorkerNanoPB section below) or FlatBuffers, is usually a better choice. 143*84e33947SAndroid Build Coastguard Worker 144*84e33947SAndroid Build Coastguard WorkerThere are a few common tips to keep in mind when interacting with the host: 145*84e33947SAndroid Build Coastguard Worker 146*84e33947SAndroid Build Coastguard Worker1. Nanoapp binaries are usually updated independently from client code - watch 147*84e33947SAndroid Build Coastguard Workerout for compatibility issues arising from changes to the messaging protocol, and 148*84e33947SAndroid Build Coastguard Workeruse a serialization format like Protocol Buffers if possible. 149*84e33947SAndroid Build Coastguard Worker 150*84e33947SAndroid Build Coastguard Worker2. Nanoapp messages to the host always wake it up if it’s asleep. If this is not 151*84e33947SAndroid Build Coastguard Workerrequired, nanoapps are encouraged to batch their messages and opportunistically 152*84e33947SAndroid Build Coastguard Workersend when the host wakes up for another reason (see 153*84e33947SAndroid Build Coastguard Worker`chreConfigureHostSleepStateEvents()`). 154*84e33947SAndroid Build Coastguard Worker 155*84e33947SAndroid Build Coastguard Worker3. After calling `chreSendMessageToHostEndpoint()`, ownership of the memory 156*84e33947SAndroid Build Coastguard Workerassociated with the message payload is assigned to the framework. Do not modify 157*84e33947SAndroid Build Coastguard Workerit until the free callback is invoked. 158*84e33947SAndroid Build Coastguard Worker 159*84e33947SAndroid Build Coastguard Worker4. Nanoapp messaging should be unicast, unless broadcast messaging is strictly 160*84e33947SAndroid Build Coastguard Workernecessary. Design the messaging protocol such that the client initiates 161*84e33947SAndroid Build Coastguard Workercommunication, and save the host endpoint ID in the nanoapp to use when sending 162*84e33947SAndroid Build Coastguard Workerreplies. 163*84e33947SAndroid Build Coastguard Worker 164*84e33947SAndroid Build Coastguard Worker## Interacting with Other Nanoapps 165*84e33947SAndroid Build Coastguard Worker 166*84e33947SAndroid Build Coastguard WorkerWhile most nanoapps are only concerned with providing functionality for a single 167*84e33947SAndroid Build Coastguard Workerclient on the host, it is possible for a nanoapp to provide services to other 168*84e33947SAndroid Build Coastguard Workernanoapps within CHRE. Similar to how nanoapps communicate with the host by 169*84e33947SAndroid Build Coastguard Workerpassing *messages*, nanoapps can communicate with one another by passing 170*84e33947SAndroid Build Coastguard Worker*events* with arbitrary binary payload. Event IDs starting in the range 171*84e33947SAndroid Build Coastguard Worker`CHRE_EVENT_FIRST_USER_VALUE` are reserved for this purpose. 172*84e33947SAndroid Build Coastguard Worker 173*84e33947SAndroid Build Coastguard WorkerTypically a nanoapp creates a *nanoapp client library* which other nanoapps can 174*84e33947SAndroid Build Coastguard Workerinclude, which presents a simple, expressive API, and handles the implementation 175*84e33947SAndroid Build Coastguard Workerdetails of passing events to the target nanoapp, and interpreting incoming 176*84e33947SAndroid Build Coastguard Workermessages. 177*84e33947SAndroid Build Coastguard Worker 178*84e33947SAndroid Build Coastguard WorkerRefer to the functions defined in `chre/event.h` for more details. 179*84e33947SAndroid Build Coastguard Worker 180*84e33947SAndroid Build Coastguard Worker## Using TensorFlow Lite for Microcontrollers 181*84e33947SAndroid Build Coastguard Worker 182*84e33947SAndroid Build Coastguard WorkerMany nanoapps use machine learning techniques to accomplish their functionality. 183*84e33947SAndroid Build Coastguard WorkerThe CHRE build system has built-in support for integrating [TensorFlow Lite for 184*84e33947SAndroid Build Coastguard WorkerMicrocontrollers](https://www.tensorflow.org/lite/microcontrollers) (TFLM) into 185*84e33947SAndroid Build Coastguard Workera nanoapp. Sync the TFLM sources, set `TFLM_PATH`, and define `USE_TFLM=true` in 186*84e33947SAndroid Build Coastguard Workeryour Makefile - see `apps/tflm_demo/README` for details and an example nanoapp. 187*84e33947SAndroid Build Coastguard Worker 188*84e33947SAndroid Build Coastguard Worker## Using Nanopb 189*84e33947SAndroid Build Coastguard Worker 190*84e33947SAndroid Build Coastguard WorkerThe CHRE build system has integrated support for using 191*84e33947SAndroid Build Coastguard Worker[Nanopb](https://jpa.kapsi.fi/nanopb/) to provide support for [Protocol 192*84e33947SAndroid Build Coastguard WorkerBuffers](https://developers.google.com/protocol-buffers) in a nanoapp. To 193*84e33947SAndroid Build Coastguard Workerintegrate this into your nanoapp’s Makefile, first install and configure 194*84e33947SAndroid Build Coastguard Workerdependencies: 195*84e33947SAndroid Build Coastguard Worker 196*84e33947SAndroid Build Coastguard Worker* Sync the Nanopb source tree (e.g. from a release on GitHub), and define the 197*84e33947SAndroid Build Coastguard Worker `NANOPB_PREFIX` environment variable to its path 198*84e33947SAndroid Build Coastguard Worker* Download and install the protobuf compiler `protoc` and make it available in 199*84e33947SAndroid Build Coastguard Worker your `$PATH`, or set the `PROTOC` environment variable 200*84e33947SAndroid Build Coastguard Worker 201*84e33947SAndroid Build Coastguard WorkerThen in your nanoapp’s Makefile, populate `NANOPB_SRCS` with the desired 202*84e33947SAndroid Build Coastguard Worker`.proto` file(s). That’s it! Though some additional options/parameters are 203*84e33947SAndroid Build Coastguard Workeravailable - see `build/nanopb.mk` for details. 204*84e33947SAndroid Build Coastguard Worker 205*84e33947SAndroid Build Coastguard Worker## Nanoapp Development Best Practices 206*84e33947SAndroid Build Coastguard Worker 207*84e33947SAndroid Build Coastguard WorkerEven though CHRE aims to provide an environment for low-power and low-latency 208*84e33947SAndroid Build Coastguard Workercontextual signal processing, these two are often conflicting goals. In 209*84e33947SAndroid Build Coastguard Workeraddition, CHRE is usually implemented in a resource-constrained environment with 210*84e33947SAndroid Build Coastguard Workerlimited memory available. 211*84e33947SAndroid Build Coastguard Worker 212*84e33947SAndroid Build Coastguard WorkerAs it requires collaboration from all nanoapps to optimize their resource usage 213*84e33947SAndroid Build Coastguard Workerfor CHRE to live up to its promises, some best practices are provided here as 214*84e33947SAndroid Build Coastguard Workerguidance for nanoapp development. 215*84e33947SAndroid Build Coastguard Worker 216*84e33947SAndroid Build Coastguard Worker### Memory Efficiency 217*84e33947SAndroid Build Coastguard Worker 218*84e33947SAndroid Build Coastguard Worker#### Avoid dynamic heap allocations where possible 219*84e33947SAndroid Build Coastguard Worker 220*84e33947SAndroid Build Coastguard WorkerAs CHRE is designed in a resource-constrained environment, there is no guarantee 221*84e33947SAndroid Build Coastguard Workerruntime memory allocation will succeed. In addition, dynamic heap allocations 222*84e33947SAndroid Build Coastguard Workermake it difficult to estimate the memory usage in advance. Developers are 223*84e33947SAndroid Build Coastguard Workertherefore encouraged to use static allocations where possible. 224*84e33947SAndroid Build Coastguard Worker 225*84e33947SAndroid Build Coastguard Worker#### Be careful of stack usage 226*84e33947SAndroid Build Coastguard Worker 227*84e33947SAndroid Build Coastguard WorkerUnlike Linux’s default stack of 8MB that scales dynamically, CHRE only has a 228*84e33947SAndroid Build Coastguard Workerfixed stack of limited size (8KB is typical). Ensure you keep any allocations to 229*84e33947SAndroid Build Coastguard Workeran absolute minimum and any large allocations should go out of scope prior to 230*84e33947SAndroid Build Coastguard Workernavigating deeper into a stack. 231*84e33947SAndroid Build Coastguard Worker 232*84e33947SAndroid Build Coastguard Worker#### Prefer in-place algorithms 233*84e33947SAndroid Build Coastguard Worker 234*84e33947SAndroid Build Coastguard WorkerPrefer in-place algorithms over out-of-place ones where efficiency allows to 235*84e33947SAndroid Build Coastguard Workerminimize additional memory requirements. 236*84e33947SAndroid Build Coastguard Worker 237*84e33947SAndroid Build Coastguard Worker### Power Efficiency 238*84e33947SAndroid Build Coastguard Worker 239*84e33947SAndroid Build Coastguard Worker#### Be opportunistic when possible 240*84e33947SAndroid Build Coastguard Worker 241*84e33947SAndroid Build Coastguard WorkerExamples include: 242*84e33947SAndroid Build Coastguard Worker 243*84e33947SAndroid Build Coastguard Worker* If the host is asleep and doesn’t need to act on a nanoapp message 244*84e33947SAndroid Build Coastguard Worker immediately, buffer until it wakes up for another reason. 245*84e33947SAndroid Build Coastguard Worker* Make a WiFi on-demand scan request only if the WiFi scan monitor doesn’t 246*84e33947SAndroid Build Coastguard Worker provide a scan result in time. 247*84e33947SAndroid Build Coastguard Worker 248*84e33947SAndroid Build Coastguard Worker#### Batch data at the source where possible 249*84e33947SAndroid Build Coastguard Worker 250*84e33947SAndroid Build Coastguard WorkerBy batching data at the source, it reduces the data delivery frequency and helps 251*84e33947SAndroid Build Coastguard Workerkeep CHRE asleep and improve power efficiency. Clients should make data requests 252*84e33947SAndroid Build Coastguard Workerwith the longest batch interval that still meets the latency requirement. 253*84e33947SAndroid Build Coastguard WorkerExamples include: 254*84e33947SAndroid Build Coastguard Worker 255*84e33947SAndroid Build Coastguard Worker* Make a sensor data request with the longest ``latency`` possible. 256*84e33947SAndroid Build Coastguard Worker* Make an audio data request with the longest ``deliveryInterval`` possible. 257*84e33947SAndroid Build Coastguard Worker 258*84e33947SAndroid Build Coastguard Worker### Standard Library Usage 259*84e33947SAndroid Build Coastguard Worker 260*84e33947SAndroid Build Coastguard WorkerCHRE implementations are only required to support a subset of the standard C and 261*84e33947SAndroid Build Coastguard WorkerC++ libraries, as well as language features requiring run-time support. This 262*84e33947SAndroid Build Coastguard Workerlist is carefully considered to ensure memory usage and implementation 263*84e33947SAndroid Build Coastguard Workercomplexity are minimized. Following these principles, some features are 264*84e33947SAndroid Build Coastguard Workerexplicitly excluded due to their memory and/or extensive OS-level dependencies, 265*84e33947SAndroid Build Coastguard Workerand others because they are supplanted by more suitable CHRE-specific APIs. 266*84e33947SAndroid Build Coastguard WorkerWhile not meant to be an exhaustive list and some platforms may differ, the 267*84e33947SAndroid Build Coastguard Workerfollowing standard library features are not meant to be used by nanoapps: 268*84e33947SAndroid Build Coastguard Worker 269*84e33947SAndroid Build Coastguard Worker* C++ exceptions and run-time type information (RTTI) 270*84e33947SAndroid Build Coastguard Worker* Standard library multi-threading support, including C++ library headers 271*84e33947SAndroid Build Coastguard Worker` <thread>`, `<mutex>`, `<atomic>`, `<future>`, etc. 272*84e33947SAndroid Build Coastguard Worker* C and C++ Standard Input/Output libraries 273*84e33947SAndroid Build Coastguard Worker* C++ Standard Template Library (STL) 274*84e33947SAndroid Build Coastguard Worker* C++ Standard Regular Expressions library 275*84e33947SAndroid Build Coastguard Worker* Dynamic memory allocation (`malloc`, `calloc`, `realloc`, `free`), and 276*84e33947SAndroid Build Coastguard Worker libraries that inherently use dynamic allocation, such as `std::unique_ptr` 277*84e33947SAndroid Build Coastguard Worker* Localization and Unicode character support 278*84e33947SAndroid Build Coastguard Worker* Date and time libraries 279*84e33947SAndroid Build Coastguard Worker* Functions that modify normal program flow, including `<setjmp.h>`, 280*84e33947SAndroid Build Coastguard Worker `<signal.h>`, `abort`, `std::terminate`, etc. 281*84e33947SAndroid Build Coastguard Worker* Accessing the host environment, including `system`, `getenv`, etc. 282*84e33947SAndroid Build Coastguard Worker* POSIX or other libraries not included in the C11 or C++17 language standards 283*84e33947SAndroid Build Coastguard Worker 284*84e33947SAndroid Build Coastguard WorkerIn many cases, equivalent functionality is available from CHRE API functions 285*84e33947SAndroid Build Coastguard Workerand/or utility libraries. For example, `chreLog` may be used for debug logging, 286*84e33947SAndroid Build Coastguard Workerwhere a more traditional program might use `printf`. 287*84e33947SAndroid Build Coastguard Worker 288*84e33947SAndroid Build Coastguard Worker## Debugging 289*84e33947SAndroid Build Coastguard Worker 290*84e33947SAndroid Build Coastguard WorkerSimilar to the framework debugging methods, each has its nanoapp counterpart to 291*84e33947SAndroid Build Coastguard Workersupport nanoapp debugging through the framework. Please see the Framework 292*84e33947SAndroid Build Coastguard WorkerDebugging section for reference/context. 293*84e33947SAndroid Build Coastguard Worker 294*84e33947SAndroid Build Coastguard Worker### Logging 295*84e33947SAndroid Build Coastguard Worker 296*84e33947SAndroid Build Coastguard WorkerCHRE API `chreLog()` logs information into the system as part of the CHRE logs. 297*84e33947SAndroid Build Coastguard WorkerNormally this appears in logcat, but some platforms may route it to a different 298*84e33947SAndroid Build Coastguard Workerlogging system (a future version of the CHRE API is expected to make logcat 299*84e33947SAndroid Build Coastguard Workerlogging mandatory). 300*84e33947SAndroid Build Coastguard Worker 301*84e33947SAndroid Build Coastguard WorkerNanoapps are encouraged to `#include "chre/util/nanoapp/log.h"` and use the 302*84e33947SAndroid Build Coastguard Worker`LOGx()` macros defined therein, which requires these additional steps: 303*84e33947SAndroid Build Coastguard Worker 304*84e33947SAndroid Build Coastguard Worker* Define `LOG_TAG` to a short, human-readable identifier for your nanoapp, as 305*84e33947SAndroid Build Coastguard Worker this gets prepended to logs 306*84e33947SAndroid Build Coastguard Worker* Define `NANOAPP_MINIMUM_LOG_LEVEL` to a `CHRE_LOG_LEVEL_\*` value in your 307*84e33947SAndroid Build Coastguard Worker Makefile for compile time log level filtering - it’s recommended to use 308*84e33947SAndroid Build Coastguard Worker `CHRE_LOG_LEVEL_DEBUG` for development, and `CHRE_LOG_LEVEL_INFO` for release 309*84e33947SAndroid Build Coastguard Worker 310*84e33947SAndroid Build Coastguard WorkerSee also the Framework Debugging section for more general guidance on logging in 311*84e33947SAndroid Build Coastguard WorkerCHRE. 312*84e33947SAndroid Build Coastguard Worker 313*84e33947SAndroid Build Coastguard Worker### Debug Dump 314*84e33947SAndroid Build Coastguard Worker 315*84e33947SAndroid Build Coastguard WorkerWhen running on CHRE v1.4+, nanoapps can also append information to the CHRE 316*84e33947SAndroid Build Coastguard Workerframework debug dump. Nanoapps interested in using this capability should call 317*84e33947SAndroid Build Coastguard Worker`chreConfigureDebugDumpEvent(true)` in `nanoappStart()`, then when 318*84e33947SAndroid Build Coastguard Worker`CHRE_EVENT_DEBUG_DUMP` is received in `nanoappHandleEvent()`, use 319*84e33947SAndroid Build Coastguard Worker`chreDebugDumpLog()` to write human-readable output to the debug dump, which 320*84e33947SAndroid Build Coastguard Workerappears in bug reports under the Context Hub HAL debug section. In the reference 321*84e33947SAndroid Build Coastguard WorkerCHRE framework implementation, nanoapp debug dumps have the nanoapp name and ID 322*84e33947SAndroid Build Coastguard Workerautomatically prepended, for example: 323*84e33947SAndroid Build Coastguard Worker 324*84e33947SAndroid Build Coastguard Worker``` 325*84e33947SAndroid Build Coastguard WorkerNanoapp debug dumps: 326*84e33947SAndroid Build Coastguard Worker 327*84e33947SAndroid Build Coastguard Worker DebugDumpWorld 0x0123456789000011: 328*84e33947SAndroid Build Coastguard Worker Debug event count: 2 329*84e33947SAndroid Build Coastguard Worker Total dwell time: 92 us 330*84e33947SAndroid Build Coastguard Worker``` 331*84e33947SAndroid Build Coastguard Worker 332*84e33947SAndroid Build Coastguard WorkerRefer to the associated CHRE API documentation and Framework Debugging section 333*84e33947SAndroid Build Coastguard Workerfor more information. 334*84e33947SAndroid Build Coastguard Worker 335*84e33947SAndroid Build Coastguard Worker### CHRE_ASSERT 336*84e33947SAndroid Build Coastguard Worker 337*84e33947SAndroid Build Coastguard WorkerTo help catch programming errors or other unexpected conditions, nanoapps can 338*84e33947SAndroid Build Coastguard Workeruse the `CHRE_ASSERT` macro provided by `#include "chre/util/nanoapp/assert.h"`. 339*84e33947SAndroid Build Coastguard WorkerKeep in mind that if one nanoapp encounters an assertion failure, it most likely 340*84e33947SAndroid Build Coastguard Workerwill cause a reset of the processor where CHRE is running, impacting other 341*84e33947SAndroid Build Coastguard Workerfunctionality (though this can vary by platform). Therefore, assertions are only 342*84e33947SAndroid Build Coastguard Workerrecommended to be used during development. Define the `CHRE_ASSERTIONS_ENABLED` 343*84e33947SAndroid Build Coastguard Workervariable in your Makefile to `false` to disable assertions at compile time. 344