xref: /aosp_15_r20/system/chre/doc/nanoapp_clients.md (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker# Interacting with Nanoapps from Client Code
2*84e33947SAndroid Build Coastguard Worker
3*84e33947SAndroid Build Coastguard Worker[TOC]
4*84e33947SAndroid Build Coastguard Worker
5*84e33947SAndroid Build Coastguard WorkerCode that interacts with a nanoapp, for example within an Android app, is known
6*84e33947SAndroid Build Coastguard Workeras the *client* of the nanoapp. There are two ways to interact with nanoapps
7*84e33947SAndroid Build Coastguard Workerfrom the host (application processor): (1) Java (above the Context Hub HAL, from
8*84e33947SAndroid Build Coastguard Workerthe Android framework or an APK), and (2) native vendor code (beneath the
9*84e33947SAndroid Build Coastguard WorkerContext Hub HAL). Most clients, especially those with a UI component, should use
10*84e33947SAndroid Build Coastguard Workerthe Java method, unless a vendor-partition native implementation is required
11*84e33947SAndroid Build Coastguard Worker(e.g. if interacting with a nanoapp is used to implement a different HAL, or if
12*84e33947SAndroid Build Coastguard Workercommunication with other beneath-HAL modules is required).
13*84e33947SAndroid Build Coastguard Worker
14*84e33947SAndroid Build Coastguard WorkerInteraction between nanoapps and clients occur through a flexible message
15*84e33947SAndroid Build Coastguard Workerpassing interface. Refer to the Nanoapp Developer Guide for recommendations on
16*84e33947SAndroid Build Coastguard Workerhow to design a protocol for use with a nanoapp.
17*84e33947SAndroid Build Coastguard Worker
18*84e33947SAndroid Build Coastguard Worker## Java APIs
19*84e33947SAndroid Build Coastguard Worker
20*84e33947SAndroid Build Coastguard WorkerCHRE is exposed to Android apps holding the appropriate permissions through the
21*84e33947SAndroid Build Coastguard Worker[ContextHubManager](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/hardware/location/ContextHubManager.java)
22*84e33947SAndroid Build Coastguard Workerand the associated
23*84e33947SAndroid Build Coastguard Worker[ContextHubClient](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/hardware/location/ContextHubClient.java)
24*84e33947SAndroid Build Coastguard Workersystem APIs.
25*84e33947SAndroid Build Coastguard Worker
26*84e33947SAndroid Build Coastguard WorkerTo use the above APIs, your application must have access to the
27*84e33947SAndroid Build Coastguard Worker`ACCESS_CONTEXT_HUB` permission, which is restricted to `signature|privileged`.
28*84e33947SAndroid Build Coastguard WorkerThis permission must be declared in the app’s [Android
29*84e33947SAndroid Build Coastguard WorkerManifest](https://developer.android.com/guide/topics/manifest/uses-permission-element),
30*84e33947SAndroid Build Coastguard Workerand is only granted to APKs that are signed with the same key as the platform
31*84e33947SAndroid Build Coastguard Worker(“signature” scope) or are preinstalled in the privileged apps folder *and* are
32*84e33947SAndroid Build Coastguard Workerpresent on the [privileged permission
33*84e33947SAndroid Build Coastguard Workerallowlist](https://source.android.com/devices/tech/config/perms-allowlist).
34*84e33947SAndroid Build Coastguard Worker
35*84e33947SAndroid Build Coastguard WorkerThe recommended flow for Java nanoapp client code is as follows:
36*84e33947SAndroid Build Coastguard Worker
37*84e33947SAndroid Build Coastguard Worker1. Retrieve the ContextHubManager object via
38*84e33947SAndroid Build Coastguard Worker   `Context.getSystemService(ContextHubManager.class)` - this will produce a
39*84e33947SAndroid Build Coastguard Worker   valid handle if the device supports `FEATURE_CONTEXT_HUB` as indicated by
40*84e33947SAndroid Build Coastguard Worker   `PackageManager.hasSystemFeature()`
41*84e33947SAndroid Build Coastguard Worker2. Retrieve a reference to a Context Hub via
42*84e33947SAndroid Build Coastguard Worker   `ContextHubManager.getContextHubs()`
43*84e33947SAndroid Build Coastguard Worker3. Confirm that the nanoapp is loaded and retrieve its version number by calling
44*84e33947SAndroid Build Coastguard Worker   `ContextHubManager.queryNanoApps()`
45*84e33947SAndroid Build Coastguard Worker4. If the nanoapp was found, create a `ContextHubClient` object through
46*84e33947SAndroid Build Coastguard Worker   `ContextHubManager.createClient()`. This can be used to communicate with your
47*84e33947SAndroid Build Coastguard Worker   nanoapp, and receive notifications of system events like reset. Note that the
48*84e33947SAndroid Build Coastguard Worker   `createClient()` API supports two modes of operation, which define how events
49*84e33947SAndroid Build Coastguard Worker   and data are passed to the client: direct callback with
50*84e33947SAndroid Build Coastguard Worker   `ContextHubClientCallback` (requires a persistent process), or
51*84e33947SAndroid Build Coastguard Worker   `PendingIntent` with `ContextHubIntentEvent` (can start an app’s process when
52*84e33947SAndroid Build Coastguard Worker   an event occurs). To send messages to the nanoapp, use
53*84e33947SAndroid Build Coastguard Worker   `ContextHubClient.sendMessageToNanoApp()`.
54*84e33947SAndroid Build Coastguard Worker
55*84e33947SAndroid Build Coastguard Worker## Vendor Native
56*84e33947SAndroid Build Coastguard Worker
57*84e33947SAndroid Build Coastguard WorkerDepending on the details of the platform implementation, you may also be able to
58*84e33947SAndroid Build Coastguard Workerinteract with CHRE directly, beneath the Context Hub HAL, by using socket IPC as
59*84e33947SAndroid Build Coastguard Workerexposed by the CHRE daemon reference implementation. This approach has some
60*84e33947SAndroid Build Coastguard Workeradvantages, like being able to interact with system nanoapps that are not
61*84e33947SAndroid Build Coastguard Workerexposed at the Java level, and it can be used with other low-level beneath-HAL
62*84e33947SAndroid Build Coastguard Workercode. However, it is not suitable for use from native code within an Android
63*84e33947SAndroid Build Coastguard Workerapp.
64*84e33947SAndroid Build Coastguard Worker
65*84e33947SAndroid Build Coastguard WorkerSee `host/common/test/chre_test_client.cc` for an example of how to use this
66*84e33947SAndroid Build Coastguard Workerinterface. Note that SELinux configuration is generally required to allowlist
67*84e33947SAndroid Build Coastguard Workeraccess to the CHRE socket.
68*84e33947SAndroid Build Coastguard Worker
69