xref: /aosp_15_r20/system/chre/doc/nanoapp_overview.md (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker*** aside
2*84e33947SAndroid Build Coastguard WorkerSee also: [Nanoapp Developer Guide](/doc/nanoapp_developer_guide.md) |
3*84e33947SAndroid Build Coastguard Worker[Interacting with Nanoapps](/doc/nanoapp_clients.md)
4*84e33947SAndroid Build Coastguard Worker***
5*84e33947SAndroid Build Coastguard Worker
6*84e33947SAndroid Build Coastguard Worker# Nanoapp Overview
7*84e33947SAndroid Build Coastguard Worker
8*84e33947SAndroid Build Coastguard Worker[TOC]
9*84e33947SAndroid Build Coastguard Worker
10*84e33947SAndroid Build Coastguard WorkerNanoapps are applications written in C or C++ which run in CHRE, to leverage
11*84e33947SAndroid Build Coastguard Workerlow-power hardware. Typically, nanoapps integrate with a component on the
12*84e33947SAndroid Build Coastguard WorkerAndroid side, such as a privileged APK, in order to provide complete end-to-end
13*84e33947SAndroid Build Coastguard Workerfunctionality of the target feature. This Android-side component is referred to
14*84e33947SAndroid Build Coastguard Workeras the nanoapp’s “client”. Since nanoapps are not limited to the same power
15*84e33947SAndroid Build Coastguard Workerconstraints that Android apps are, they can process inputs, like sensor data,
16*84e33947SAndroid Build Coastguard Workermuch more frequently while the device’s screen is off.
17*84e33947SAndroid Build Coastguard Worker
18*84e33947SAndroid Build Coastguard WorkerNanoapps are abstracted from the underlying platform details by the CHRE API,
19*84e33947SAndroid Build Coastguard Workerwhich is standardized across all CHRE implementations. This means that a nanoapp
20*84e33947SAndroid Build Coastguard Workeris *code compatible* across devices - its source code does not need to be
21*84e33947SAndroid Build Coastguard Workerchanged to run on different hardware, but it *may* need to be recompiled. The
22*84e33947SAndroid Build Coastguard WorkerCHRE API also provides binary compatibility guarantees across minor versions, so
23*84e33947SAndroid Build Coastguard Workera nanoapp does not need to be recompiled to run on a device that exposes a newer
24*84e33947SAndroid Build Coastguard Workeror older version of the CHRE API. These properties help provide for the maximum
25*84e33947SAndroid Build Coastguard Workerreuse of code across devices.
26*84e33947SAndroid Build Coastguard Worker
27*84e33947SAndroid Build Coastguard WorkerDue to system security and resource constraints of the platforms that CHRE
28*84e33947SAndroid Build Coastguard Workertargets, only device OEMs and their trusted partners are able to create
29*84e33947SAndroid Build Coastguard Workernanoapps. In other words, the system only runs nanoapps that possess a digital
30*84e33947SAndroid Build Coastguard Workersignature that is trusted in advance by the device manufacturer, and APKs must
31*84e33947SAndroid Build Coastguard Workerhold a special privileged/same-signature permission (`ACCESS_CONTEXT_HUB`) to be
32*84e33947SAndroid Build Coastguard Workerable to interact with nanoapps and the Context Hub in general. However, this
33*84e33947SAndroid Build Coastguard Workerdoes not mean that third-party APKs cannot benefit from CHRE - nanoapps can be
34*84e33947SAndroid Build Coastguard Workerused to power APIs available for use by any Android app.
35*84e33947SAndroid Build Coastguard Worker
36*84e33947SAndroid Build Coastguard Worker## Methods for Loading a Nanoapp
37*84e33947SAndroid Build Coastguard Worker
38*84e33947SAndroid Build Coastguard WorkerWhile nanoapps are nominally dynamically loadable modules, they can be loaded
39*84e33947SAndroid Build Coastguard Workerinto a device through a few methods, each of which has pros and cons elaborated
40*84e33947SAndroid Build Coastguard Workerbelow.
41*84e33947SAndroid Build Coastguard Worker
42*84e33947SAndroid Build Coastguard Worker### Static Nanoapps
43*84e33947SAndroid Build Coastguard Worker
44*84e33947SAndroid Build Coastguard WorkerStatic nanoapps are, as the name suggests, statically compiled into the CHRE
45*84e33947SAndroid Build Coastguard Workerframework binary. Static nanoapps are automatically initialized after the CHRE
46*84e33947SAndroid Build Coastguard Workerframework completes its initialization during the boot process.
47*84e33947SAndroid Build Coastguard Worker
48*84e33947SAndroid Build Coastguard WorkerStatic nanoapps typically aren’t used in production, because this monolithic
49*84e33947SAndroid Build Coastguard Workerapproach has downsides in terms of version control, updatability, etc., but it
50*84e33947SAndroid Build Coastguard Workercan be useful during CHRE development and bring-up of new devices, especially
51*84e33947SAndroid Build Coastguard Workerbefore dynamic loading functionality is enabled. For example, the FeatureWorld
52*84e33947SAndroid Build Coastguard Workernanoapps (described later) are typically built as static nanoapps.
53*84e33947SAndroid Build Coastguard Worker
54*84e33947SAndroid Build Coastguard WorkerStatic nanoapps are typically unconditionally compiled as part of the framework
55*84e33947SAndroid Build Coastguard Workerbuild (via `apps/apps.mk`), but then stripped out by the linker if unreferenced
56*84e33947SAndroid Build Coastguard Worker(using the `--gc-sections` option, or equivalent). Static nanoapps are
57*84e33947SAndroid Build Coastguard Workerreferenced only if their initialization function appears in the
58*84e33947SAndroid Build Coastguard Worker`kStaticNanoappList` array, which by default is empty, but can be overridden by
59*84e33947SAndroid Build Coastguard Workerthe device variant makefile, as in `variant/simulator/` for example.
60*84e33947SAndroid Build Coastguard Worker
61*84e33947SAndroid Build Coastguard WorkerSome boilerplate is needed to enable nanoapp to be built as a static nanoapp -
62*84e33947SAndroid Build Coastguard Workersee the code wrapped in `#ifdef CHRE_NANOAPP_INTERNAL` in
63*84e33947SAndroid Build Coastguard Worker`apps/hello_world/hello_world.cc`.
64*84e33947SAndroid Build Coastguard Worker
65*84e33947SAndroid Build Coastguard Worker### Preloaded Nanoapps
66*84e33947SAndroid Build Coastguard Worker
67*84e33947SAndroid Build Coastguard WorkerPreloaded nanoapps are built as a separate binary from the CHRE framework, but
68*84e33947SAndroid Build Coastguard Workerincluded in the vendor partition of an overall device image (hence they are
69*84e33947SAndroid Build Coastguard Worker“preloaded” onto the device). The binaries associated with a preloaded nanoapp
70*84e33947SAndroid Build Coastguard Worker(usually a `.so` and a `.napp_header` file) are checked in to the Android tree
71*84e33947SAndroid Build Coastguard Workeras a “prebuilt” binary, and integrated into the Android build system so as to
72*84e33947SAndroid Build Coastguard Workerappear in the resulting device image, for example using `$(BUILD_PREBUILT)` in
73*84e33947SAndroid Build Coastguard WorkerAndroid.mk, or `prebuilt_dsp` or `prebuilt_firmware` in Android.bp.
74*84e33947SAndroid Build Coastguard Worker
75*84e33947SAndroid Build Coastguard WorkerWhile the mechanism for loading prebuilt nanoapps is platform-specific, the CHRE
76*84e33947SAndroid Build Coastguard Workerframework generally follows these steps at boot time:
77*84e33947SAndroid Build Coastguard Worker
78*84e33947SAndroid Build Coastguard Worker1. When CHRE starts up, the CHRE daemon process running on the AP reads the
79*84e33947SAndroid Build Coastguard Worker   configuration file `/vendor/etc/chre/preloaded_nanoapps.json`, which contains
80*84e33947SAndroid Build Coastguard Worker   the list of nanoapps that should be automatically loaded.
81*84e33947SAndroid Build Coastguard Worker
82*84e33947SAndroid Build Coastguard Worker2. For each nanoapp in the JSON file, the CHRE daemon reads the `.napp_header`
83*84e33947SAndroid Build Coastguard Worker   from storage, and sends a message to CHRE requesting it to load the nanoapp.
84*84e33947SAndroid Build Coastguard Worker
85*84e33947SAndroid Build Coastguard Worker3. The platform layer of the CHRE framework handles the requests by loading,
86*84e33947SAndroid Build Coastguard Worker   authenticating, linking, and starting the nanoapp.
87*84e33947SAndroid Build Coastguard Worker
88*84e33947SAndroid Build Coastguard Worker4. CHRE initialization proceeds (it is important for all preloaded nanoapps to
89*84e33947SAndroid Build Coastguard Worker   be included at the first moment list query command can be processed, to
90*84e33947SAndroid Build Coastguard Worker   avoid race conditions leading to clients believing that a preloaded nanoapp
91*84e33947SAndroid Build Coastguard Worker   is missing).
92*84e33947SAndroid Build Coastguard Worker
93*84e33947SAndroid Build Coastguard WorkerThis path is most commonly used to deploy nanoapps to production, as the entire
94*84e33947SAndroid Build Coastguard Workerdevice software can be validated together without external dependencies, while
95*84e33947SAndroid Build Coastguard Workeralso preserving the ability to update nanoapps independent from other components
96*84e33947SAndroid Build Coastguard Workerin the system.
97*84e33947SAndroid Build Coastguard Worker
98*84e33947SAndroid Build Coastguard Worker### Fully Dynamic Nanoapps
99*84e33947SAndroid Build Coastguard Worker
100*84e33947SAndroid Build Coastguard WorkerAt the binary level, a preloaded nanoapp and fully dynamic nanoapp are
101*84e33947SAndroid Build Coastguard Workeridentical. The key difference is where they are stored and how they are
102*84e33947SAndroid Build Coastguard Workerinitially loaded into CHRE, and potentially how metadata is handled. In most
103*84e33947SAndroid Build Coastguard Workercases, preloaded nanoapps will use a separate `.napp_header` file with metadata
104*84e33947SAndroid Build Coastguard Workerand `.so` file for the actual binary, a fully dynamic nanoapp has the header
105*84e33947SAndroid Build Coastguard Workerprepended to the binary, and carries the `.napp` file type suffix. In other
106*84e33947SAndroid Build Coastguard Workerwords, the command `cat my_nanoapp.napp_header my_nanoapp.so > my_nanoapp.napp`
107*84e33947SAndroid Build Coastguard Workercan be used to create a fully dynamic nanoapp file from these components.
108*84e33947SAndroid Build Coastguard Worker
109*84e33947SAndroid Build Coastguard WorkerInstead of being stored on the device filesystem, fully dynamic nanoapps can be
110*84e33947SAndroid Build Coastguard Workerloaded at any time after initialization using the
111*84e33947SAndroid Build Coastguard Worker`ContextHubManager.loadNanoApp()` Java API. This allows nanoapps to be
112*84e33947SAndroid Build Coastguard Workerupdated/delivered by an APK, outside of a full Android system update (OTA).
113*84e33947SAndroid Build Coastguard Worker
114*84e33947SAndroid Build Coastguard WorkerThis mechanism is used to dynamically load and unload test nanoapps, but can
115*84e33947SAndroid Build Coastguard Workeralso be used for production nanoapps.
116*84e33947SAndroid Build Coastguard Worker
117*84e33947SAndroid Build Coastguard Worker## Other Nanoapp Types
118*84e33947SAndroid Build Coastguard Worker
119*84e33947SAndroid Build Coastguard WorkerSome platforms support loading nanoapps into multiple tiers of memory, for
120*84e33947SAndroid Build Coastguard Workerexample low-power tightly coupled memory (TCM, usually SRAM), versus a
121*84e33947SAndroid Build Coastguard Workerhigher-power but higher-capacity memory bank (such as DRAM). This distinction is
122*84e33947SAndroid Build Coastguard Workernormally made at the build target variant level.
123*84e33947SAndroid Build Coastguard Worker
124*84e33947SAndroid Build Coastguard WorkerCHRE also supports the concept of a *system nanoapp*, which is a nanoapp whose
125*84e33947SAndroid Build Coastguard Workerpurpose is to accomplish some low-level, device-specific functionality that is
126*84e33947SAndroid Build Coastguard Workerpurely beneath the HAL level. System nanoapps are therefore hidden from the
127*84e33947SAndroid Build Coastguard Workernanoapp list at the HAL. This property is controlled by setting the
128*84e33947SAndroid Build Coastguard Worker`NANOAPP_IS_SYSTEM_NANOAPP` variable in the nanoapp Makefile.
129*84e33947SAndroid Build Coastguard Worker
130*84e33947SAndroid Build Coastguard Worker## Example AOSP Nanoapps
131*84e33947SAndroid Build Coastguard Worker
132*84e33947SAndroid Build Coastguard WorkerSome basic nanoapps can be found in the `apps/` folder, which are used for test
133*84e33947SAndroid Build Coastguard Workerpurposes, as well as to demonstrate how to use the CHRE APIs.
134*84e33947SAndroid Build Coastguard Worker
135*84e33947SAndroid Build Coastguard Worker### FeatureWorld Nanoapps
136*84e33947SAndroid Build Coastguard Worker
137*84e33947SAndroid Build Coastguard WorkerThe *FeatureWorld* nanoapps each exercise a part of the CHRE API, and print
138*84e33947SAndroid Build Coastguard Workerresults/output to `chreLog`. An overview of a few of the key FeatureWorld
139*84e33947SAndroid Build Coastguard Workernanoapps is given below:
140*84e33947SAndroid Build Coastguard Worker
141*84e33947SAndroid Build Coastguard Worker* `hello_world`: While not technically a FeatureWorld nanoapp, it’s generally
142*84e33947SAndroid Build Coastguard Worker  the first nanoapp to be tried, and it simply outputs a log message when it
143*84e33947SAndroid Build Coastguard Worker  starts and ends, and upon any event received.
144*84e33947SAndroid Build Coastguard Worker
145*84e33947SAndroid Build Coastguard Worker* `message_world`: Exercises host messaging functionality. Typically used in
146*84e33947SAndroid Build Coastguard Worker  conjunction with `host/common/test/chre_test_client.cc` (see
147*84e33947SAndroid Build Coastguard Worker  `sendMessageToNanoapp()` in that file).
148*84e33947SAndroid Build Coastguard Worker
149*84e33947SAndroid Build Coastguard Worker* `sensor_world`: Enables sensors and prints the samples it receives. This
150*84e33947SAndroid Build Coastguard Worker  nanoapp is typically customized prior to executing, for example to control
151*84e33947SAndroid Build Coastguard Worker  which sensors it will enable. It also supports a “break it” mode which
152*84e33947SAndroid Build Coastguard Worker  stresses the system by enabling/disabling sensors frequently.
153*84e33947SAndroid Build Coastguard Worker
154*84e33947SAndroid Build Coastguard Worker* `host_awake_world`: Used to help validate functionality used for
155*84e33947SAndroid Build Coastguard Worker  opportunistically sending messages to the AP when it is awake.
156*84e33947SAndroid Build Coastguard Worker
157*84e33947SAndroid Build Coastguard Worker### Stress Test Nanoapps
158*84e33947SAndroid Build Coastguard Worker
159*84e33947SAndroid Build Coastguard WorkerThese nanoapps help stress test the CHRE framework. They include:
160*84e33947SAndroid Build Coastguard Worker
161*84e33947SAndroid Build Coastguard Worker* `audio_stress_test`: Repeatedly enables and disables an audio source,
162*84e33947SAndroid Build Coastguard Worker  verifying that it continues to provide data as expected.
163*84e33947SAndroid Build Coastguard Worker
164*84e33947SAndroid Build Coastguard Worker* `sensor_world`: Contains a “break it” mode which repeatedly enables, disables,
165*84e33947SAndroid Build Coastguard Worker  and reconfigures sensors.
166*84e33947SAndroid Build Coastguard Worker
167*84e33947SAndroid Build Coastguard Worker* `spammer`: Sends a constant stream of messages and events to stress test the
168*84e33947SAndroid Build Coastguard Worker  queueing system.
169*84e33947SAndroid Build Coastguard Worker
170*84e33947SAndroid Build Coastguard Worker* `unload_tester`: Used in conjunction with the spammer nanoapp to verify that
171*84e33947SAndroid Build Coastguard Worker  unloading a nanoapp with pending events/messages completes successfully. Note
172*84e33947SAndroid Build Coastguard Worker  that this nanoapp references internal framework functions (e.g.
173*84e33947SAndroid Build Coastguard Worker  `EventLoopManager::deferCallback()`) to accomplish its functionality, which is
174*84e33947SAndroid Build Coastguard Worker  generally only permissible for testing purposes.
175*84e33947SAndroid Build Coastguard Worker
176*84e33947SAndroid Build Coastguard Worker### Power Test
177*84e33947SAndroid Build Coastguard Worker
178*84e33947SAndroid Build Coastguard WorkerThe `power_test` nanoapp is intended to be used in conjunction with special
179*84e33947SAndroid Build Coastguard Workerhardware that directly measures the power usage of the system and/or its
180*84e33947SAndroid Build Coastguard Workercomponents. This nanoapp is intended to be used with its host-side client,
181*84e33947SAndroid Build Coastguard Worker`chre_power_test_client`, to create some activity at the CHRE API level which
182*84e33947SAndroid Build Coastguard Workercan then be measured. For example, running `./chre_power_test_client wifi enable
183*84e33947SAndroid Build Coastguard Worker5000000000` will configure the `power_test` nanoapp to request a WiFi scan every
184*84e33947SAndroid Build Coastguard Worker5 seconds - the power monitoring equipment can then be used to determine the
185*84e33947SAndroid Build Coastguard Workerpower cost of performing a WiFi scan from CHRE. Typically this is done after
186*84e33947SAndroid Build Coastguard Workerunloading all other nanoapps in the system (which can be done via
187*84e33947SAndroid Build Coastguard Worker`./chre_power_test_client unloadall`), and disabling all other functionality, to
188*84e33947SAndroid Build Coastguard Workerget a clean power trace of purely the functionality exercised by the
189*84e33947SAndroid Build Coastguard Worker`power_test` nanoapp.
190*84e33947SAndroid Build Coastguard Worker
191*84e33947SAndroid Build Coastguard WorkerRefer to `chre_power_test_client.cc` for more details, including a full listing
192*84e33947SAndroid Build Coastguard Workerof all supported commands.
193*84e33947SAndroid Build Coastguard Worker
194*84e33947SAndroid Build Coastguard Worker### Nanoapps Used with Java-based Test Suites
195*84e33947SAndroid Build Coastguard Worker
196*84e33947SAndroid Build Coastguard WorkerNanoapps under `apps/test` are associated with a test suite, for example Context
197*84e33947SAndroid Build Coastguard WorkerHub Qualification Test Suite (CHQTS), which is used to test that a given device
198*84e33947SAndroid Build Coastguard Workerupholds the requirements of the CHRE API. Much of the host-side Java code
199*84e33947SAndroid Build Coastguard Workerassociated with these nanoapps can be found in the `java/` folder.
200