xref: /aosp_15_r20/system/chre/doc/porting_guide.md (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker# CHRE Framework Porting Guide
2*84e33947SAndroid Build Coastguard Worker
3*84e33947SAndroid Build Coastguard Worker[TOC]
4*84e33947SAndroid Build Coastguard Worker
5*84e33947SAndroid Build Coastguard WorkerCHRE achieves portability and extensibility across platforms by defining
6*84e33947SAndroid Build Coastguard Workerinterfaces that the platform needs to implement. These interfaces provide
7*84e33947SAndroid Build Coastguard Workerdependencies for the common CHRE code that are necessarily platform-specific.
8*84e33947SAndroid Build Coastguard WorkerAdditionally, platform code calls into common code to ferry events from
9*84e33947SAndroid Build Coastguard Workerunderlying subsystems to nanoapps.
10*84e33947SAndroid Build Coastguard Worker
11*84e33947SAndroid Build Coastguard WorkerThis section gives an overview of the steps one should take to add support for a
12*84e33947SAndroid Build Coastguard Workernew platform in the CHRE reference implementation.
13*84e33947SAndroid Build Coastguard Worker
14*84e33947SAndroid Build Coastguard Worker## Directory Structure
15*84e33947SAndroid Build Coastguard Worker
16*84e33947SAndroid Build Coastguard WorkerCHRE platform code can be broadly categorized as follows.
17*84e33947SAndroid Build Coastguard Worker
18*84e33947SAndroid Build Coastguard Worker### Platform Interfaces
19*84e33947SAndroid Build Coastguard Worker
20*84e33947SAndroid Build Coastguard WorkerFiles under `platform/include` serve as the interface between common code in
21*84e33947SAndroid Build Coastguard Worker`core/` and other platform-specific code in `platform/<platform_name>`.  These
22*84e33947SAndroid Build Coastguard Workerfiles are considered common and should not be modified for the sake of
23*84e33947SAndroid Build Coastguard Workersupporting an individual platform.
24*84e33947SAndroid Build Coastguard Worker
25*84e33947SAndroid Build Coastguard Worker### Shared Platform Code
26*84e33947SAndroid Build Coastguard Worker
27*84e33947SAndroid Build Coastguard WorkerLocated in `platform/shared/`, the code here is part of the platform layer’s
28*84e33947SAndroid Build Coastguard Workerresponsibilities, but is not necessarily specific to only one platform. In other
29*84e33947SAndroid Build Coastguard Workerwords, this code is likely to be re-used by multiple platforms, but it is not
30*84e33947SAndroid Build Coastguard Workerstrictly necessary for a given platform to use it.
31*84e33947SAndroid Build Coastguard Worker
32*84e33947SAndroid Build Coastguard Worker### Platform-specific Code
33*84e33947SAndroid Build Coastguard Worker
34*84e33947SAndroid Build Coastguard WorkerFiles under `platform/<platform_name>` are specific to the underlying software
35*84e33947SAndroid Build Coastguard Workerof a given platform, for example the APIs which are used to access functionality
36*84e33947SAndroid Build Coastguard Workerlike sensors, the operating system, etc. To permit code reuse, the CHRE platform
37*84e33947SAndroid Build Coastguard Workerlayer for a given device may be composed of files from multiple
38*84e33947SAndroid Build Coastguard Worker`<platform_name>` folders, for example if the same sensor framework is supported
39*84e33947SAndroid Build Coastguard Workeracross multiple OSes, there may be one folder that provides the components that
40*84e33947SAndroid Build Coastguard Workerare specific to just the OS.
41*84e33947SAndroid Build Coastguard Worker
42*84e33947SAndroid Build Coastguard WorkerPlatform-specific code can be further subdivided into:
43*84e33947SAndroid Build Coastguard Worker
44*84e33947SAndroid Build Coastguard Worker* **Source files**: normally included at
45*84e33947SAndroid Build Coastguard Worker  `platform/<platform_name>/<file_name>.cc`, but may appear in a subdirectory
46*84e33947SAndroid Build Coastguard Worker
47*84e33947SAndroid Build Coastguard Worker* **Headers which are includable by common code**: these are placed at
48*84e33947SAndroid Build Coastguard Worker  `platform/<platform_name>/include/chre/target_platform/<file_name>.h`, and are
49*84e33947SAndroid Build Coastguard Worker  included by *Platform Interfaces* found in `platform/include` and provide
50*84e33947SAndroid Build Coastguard Worker  inline or base class definitions, such as `mutex_base_impl.h` and
51*84e33947SAndroid Build Coastguard Worker  `platform_sensor_base.h` respectively, or required macros
52*84e33947SAndroid Build Coastguard Worker
53*84e33947SAndroid Build Coastguard Worker* **Fully platform-specific headers**: these typically appear at
54*84e33947SAndroid Build Coastguard Worker  `platform/<platform_name>/include/chre/platform/<platform_name>/<file_name>.h`
55*84e33947SAndroid Build Coastguard Worker  and may only be included by other platform-specific code
56*84e33947SAndroid Build Coastguard Worker
57*84e33947SAndroid Build Coastguard Worker## Open Sourcing
58*84e33947SAndroid Build Coastguard Worker
59*84e33947SAndroid Build Coastguard WorkerPartners who add support for a new platform are recommended to upstream their
60*84e33947SAndroid Build Coastguard Workercode to
61*84e33947SAndroid Build Coastguard Worker[AOSP](https://source.android.com/setup/contribute#contribute-to-the-code).
62*84e33947SAndroid Build Coastguard WorkerThis helps ensure that details of your platform are considered by the team that
63*84e33947SAndroid Build Coastguard Workermaintains the core framework, so any changes that break compilation are
64*84e33947SAndroid Build Coastguard Workeraddressed in a timely fashion, and enables you to receive useful code review
65*84e33947SAndroid Build Coastguard Workerfeedback to improve the quality of your CHRE implementation. Please reach out
66*84e33947SAndroid Build Coastguard Workervia your TAM to help organize this effort.
67*84e33947SAndroid Build Coastguard Worker
68*84e33947SAndroid Build Coastguard WorkerIf some parts of a platform’s CHRE implementation must be kept closed source,
69*84e33947SAndroid Build Coastguard Workerthen it is recommended to be kept in a separate Git project (under vendor/ in
70*84e33947SAndroid Build Coastguard Workerthe Android tree). This vendor-private code can be integrated with the main CHRE
71*84e33947SAndroid Build Coastguard Workerbuild system through the `CHRE_VARIANT_MK_INCLUDES` variable. See the build
72*84e33947SAndroid Build Coastguard Workersystem documentation for more details.
73*84e33947SAndroid Build Coastguard Worker
74*84e33947SAndroid Build Coastguard Worker## Recommended Steps for Porting CHRE
75*84e33947SAndroid Build Coastguard Worker
76*84e33947SAndroid Build Coastguard WorkerWhen starting to add support for a new platform in the CHRE framework, it’s
77*84e33947SAndroid Build Coastguard Workerrecommended to break the task into manageable chunks, to progressively add more
78*84e33947SAndroid Build Coastguard Workerfunctionality until the full desired feature set is achieved. An existing
79*84e33947SAndroid Build Coastguard Workerplatform implementation can be referenced to create empty stubs, and then
80*84e33947SAndroid Build Coastguard Workerproceed to add implementations piece by piece, testing along the way.
81*84e33947SAndroid Build Coastguard Worker
82*84e33947SAndroid Build Coastguard WorkerCHRE provides various test nanoapps in `apps/` that exercise a particular
83*84e33947SAndroid Build Coastguard Workerfeature that the platform provides. These are selectively compiled into the
84*84e33947SAndroid Build Coastguard Workerfirmware statically via a `static_nanoapps.cc` source file.
85*84e33947SAndroid Build Coastguard Worker
86*84e33947SAndroid Build Coastguard WorkerWith this in mind, it is recommended to follow this general approach:
87*84e33947SAndroid Build Coastguard Worker
88*84e33947SAndroid Build Coastguard Worker1. Create a new platform with only empty stubs, with optional features (like
89*84e33947SAndroid Build Coastguard Worker   `CHRE_GNSS_SUPPORT_ENABLED`) disabled at build-time
90*84e33947SAndroid Build Coastguard Worker
91*84e33947SAndroid Build Coastguard Worker2. Work on updating the build system to add a new build target and achieve
92*84e33947SAndroid Build Coastguard Worker   successful compilation and linking (see the build system documentation for
93*84e33947SAndroid Build Coastguard Worker   details)
94*84e33947SAndroid Build Coastguard Worker
95*84e33947SAndroid Build Coastguard Worker3. Implement base primitives from `platform/include`, including support for
96*84e33947SAndroid Build Coastguard Worker   mutexes, condition variables, atomics, time, timers, memory allocation, and
97*84e33947SAndroid Build Coastguard Worker   logging
98*84e33947SAndroid Build Coastguard Worker
99*84e33947SAndroid Build Coastguard Worker4. Add initialization code to start the CHRE thread
100*84e33947SAndroid Build Coastguard Worker
101*84e33947SAndroid Build Coastguard Worker5. Add static nanoapp initialization support (usually this is just a copy/paste
102*84e33947SAndroid Build Coastguard Worker   from another platform)
103*84e33947SAndroid Build Coastguard Worker
104*84e33947SAndroid Build Coastguard Worker6. Confirm that the ‘hello world’ nanoapp produces the expected log message (if
105*84e33947SAndroid Build Coastguard Worker   it does, huzzah!)
106*84e33947SAndroid Build Coastguard Worker
107*84e33947SAndroid Build Coastguard Worker7. Complete any remaining primitives, like assert
108*84e33947SAndroid Build Coastguard Worker
109*84e33947SAndroid Build Coastguard Worker8. Implement host link and the daemon/HAL on the host (AP) side, and validate it
110*84e33947SAndroid Build Coastguard Worker   with a combination of the message world nanoapp and the host-side test code
111*84e33947SAndroid Build Coastguard Worker   found at `host/common/test/chre_test_client.cc`
112*84e33947SAndroid Build Coastguard Worker
113*84e33947SAndroid Build Coastguard WorkerAt this stage, the core functionality has been enabled, and further steps should
114*84e33947SAndroid Build Coastguard Workerinclude enabling dynamic loading (described in its own section below), and the
115*84e33947SAndroid Build Coastguard Workerdesired optional feature areas, like sensors (potentially via their respective
116*84e33947SAndroid Build Coastguard WorkerPALs, described in the next section).
117*84e33947SAndroid Build Coastguard Worker
118*84e33947SAndroid Build Coastguard Worker## Implementing the Context Hub HAL
119*84e33947SAndroid Build Coastguard Worker
120*84e33947SAndroid Build Coastguard WorkerThe Context Hub HAL (found in the Android tree under
121*84e33947SAndroid Build Coastguard Worker`hardware/interfaces/contexthub`) defines the interface between Android and the
122*84e33947SAndroid Build Coastguard Workerunderlying CHRE implementation, but as CHRE is implemented on a different
123*84e33947SAndroid Build Coastguard Workerprocessor from the HAL, the HAL is mostly responsible for relaying messages to
124*84e33947SAndroid Build Coastguard WorkerCHRE. This project includes an implementation of the Context Hub HAL under
125*84e33947SAndroid Build Coastguard Worker`host/hal_generic` which pairs with the CHRE framework reference implementation.
126*84e33947SAndroid Build Coastguard WorkerIt converts between HAL API calls and serialized flatbuffers messages, using the
127*84e33947SAndroid Build Coastguard Workerhost messaging protocol defined under `platform/shared` (platform
128*84e33947SAndroid Build Coastguard Workerimplementations are able to choose a different protocol if desired, but would
129*84e33947SAndroid Build Coastguard Workerrequire a new HAL implementation), and passes the messages to and from the CHRE
130*84e33947SAndroid Build Coastguard Workerdaemon over a socket. The CHRE daemon is in turn responsible for communicating
131*84e33947SAndroid Build Coastguard Workerdirectly with CHRE, including common functionality like relaying messages to and
132*84e33947SAndroid Build Coastguard Workerfrom nanoapps, as well as device-specific functionality as needed. Some examples
133*84e33947SAndroid Build Coastguard Workerof CHRE functionality that are typically implemented with support from the CHRE
134*84e33947SAndroid Build Coastguard Workerdaemon include:
135*84e33947SAndroid Build Coastguard Worker
136*84e33947SAndroid Build Coastguard Worker* Loading preloaded nanoapps at startup
137*84e33947SAndroid Build Coastguard Worker* Passing log messages from CHRE into Android logcat
138*84e33947SAndroid Build Coastguard Worker* Determining the offset between `chreGetTime()` and Android’s
139*84e33947SAndroid Build Coastguard Worker  `SystemClock.elapsedRealtimeNanos()` for use with
140*84e33947SAndroid Build Coastguard Worker  `chreGetEstimatedHostTimeOffset()`
141*84e33947SAndroid Build Coastguard Worker* Coordination with the SoundTrigger HAL for audio functionality
142*84e33947SAndroid Build Coastguard Worker* Exposing CHRE functionality to other vendor-specific components (e.g. via
143*84e33947SAndroid Build Coastguard Worker  `chre::SocketClient`)
144*84e33947SAndroid Build Coastguard Worker
145*84e33947SAndroid Build Coastguard WorkerWhen adding support for a new platform, a new HAL implementation and/or daemon
146*84e33947SAndroid Build Coastguard Workerimplementation on the host side may be required. Refer to code in the `host/`
147*84e33947SAndroid Build Coastguard Workerdirectory for examples.
148*84e33947SAndroid Build Coastguard Worker
149*84e33947SAndroid Build Coastguard Worker## Implementing Optional Feature Areas (e.g. PALs)
150*84e33947SAndroid Build Coastguard Worker
151*84e33947SAndroid Build Coastguard WorkerCHRE provides groups of functionality called *feature areas* which are
152*84e33947SAndroid Build Coastguard Workerconsidered optional from the perspective of the CHRE API, but may be required to
153*84e33947SAndroid Build Coastguard Workersupport a desired nanoapp. CHRE feature areas include sensors, GNSS, audio, and
154*84e33947SAndroid Build Coastguard Workerothers. There are two ways by which this functionality can be exposed to the
155*84e33947SAndroid Build Coastguard Workercommon CHRE framework code: via the `Platform<Module>` C++ classes, or the C PAL
156*84e33947SAndroid Build Coastguard Worker(Platform Abstraction Layer) APIs. It may not be necessary to implement all of
157*84e33947SAndroid Build Coastguard Workerthe available feature areas, and they can instead be disabled if they won’t be
158*84e33947SAndroid Build Coastguard Workerimplemented.
159*84e33947SAndroid Build Coastguard Worker
160*84e33947SAndroid Build Coastguard WorkerThe Platform C++ Classes and PAL APIs have extensive documentation in their
161*84e33947SAndroid Build Coastguard Workerheader files, including details on requirements. Please refer to the headers for
162*84e33947SAndroid Build Coastguard Workerprecise implementation details.
163*84e33947SAndroid Build Coastguard Worker
164*84e33947SAndroid Build Coastguard Worker### Platform C++ Classes vs. PAL APIs
165*84e33947SAndroid Build Coastguard Worker
166*84e33947SAndroid Build Coastguard WorkerEach feature area includes one or more `Platform<Module>` classes which the
167*84e33947SAndroid Build Coastguard Workercommon framework code directly interacts with. These classes may be directly
168*84e33947SAndroid Build Coastguard Workerimplemented to provide the given functionality, or the shim to the PAL APIs
169*84e33947SAndroid Build Coastguard Workerincluded in the `shared` platform directory may be used. PALs provide a C API
170*84e33947SAndroid Build Coastguard Workerwhich is suitable for use as a binary interface, for example between two dynamic
171*84e33947SAndroid Build Coastguard Workermodules/libraries, and it also allows for the main CHRE to platform-specific
172*84e33947SAndroid Build Coastguard Workertranslation to be implemented in C, which may be preferable in some cases.
173*84e33947SAndroid Build Coastguard Worker
174*84e33947SAndroid Build Coastguard WorkerNote that the PAL APIs are binary-stable, in that it’s possible for the CHRE
175*84e33947SAndroid Build Coastguard Workerframework to work with a module that implements a different minor version of the
176*84e33947SAndroid Build Coastguard WorkerPAL API, full backwards compatibility (newer CHRE framework to older PAL) is not
177*84e33947SAndroid Build Coastguard Workerguaranteed, and may not be possible due to behavioral changes in the CHRE API.
178*84e33947SAndroid Build Coastguard WorkerWhile it is possible for a PAL implementation to simultaneously support multiple
179*84e33947SAndroid Build Coastguard Workerversions of the PAL API, it is generally recommended to ensure the PAL API
180*84e33947SAndroid Build Coastguard Workerversion matches between the framework and PAL module, unless the source control
181*84e33947SAndroid Build Coastguard Workerbenefits of a common PAL binary are desired.
182*84e33947SAndroid Build Coastguard Worker
183*84e33947SAndroid Build Coastguard WorkerThis level of compatibility is not provided for the C++ `Platform<Module>`
184*84e33947SAndroid Build Coastguard Workerclasses, as the CHRE framework may introduce changes that break compilation. If
185*84e33947SAndroid Build Coastguard Workera platform implementation is included in AOSP, then it is possible for the
186*84e33947SAndroid Build Coastguard Workerpotential impact to be evaluated and addressed early.
187*84e33947SAndroid Build Coastguard Worker
188*84e33947SAndroid Build Coastguard Worker### Disabling Feature Areas
189*84e33947SAndroid Build Coastguard Worker
190*84e33947SAndroid Build Coastguard WorkerIf a feature area is not supported, setting the make variable
191*84e33947SAndroid Build Coastguard Worker`CHRE_<name>_SUPPORT_ENABLED` to false in the variant makefile will avoid
192*84e33947SAndroid Build Coastguard Workerinclusion of common code for that feature area. Note that it must still be
193*84e33947SAndroid Build Coastguard Workerpossible for the associated CHRE APIs to be called by nanoapps without crashing
194*84e33947SAndroid Build Coastguard Worker- these functions must return an appropriate response indicating the lack of
195*84e33947SAndroid Build Coastguard Worker  support (refer to `platform/shared/chre_api_<name>.cc` for examples).
196*84e33947SAndroid Build Coastguard Worker
197*84e33947SAndroid Build Coastguard Worker### Implementing Platform C++ Classes
198*84e33947SAndroid Build Coastguard Worker
199*84e33947SAndroid Build Coastguard WorkerAs described in the CHRE Framework Overview section, CHRE abstracts common code
200*84e33947SAndroid Build Coastguard Workerfrom platform-specific code at compile time by inheriting through
201*84e33947SAndroid Build Coastguard Worker`Platform<Module>` and `Platform<Module>Base` classes. Platform-specific code
202*84e33947SAndroid Build Coastguard Workermay retrieve a reference to other objects in CHRE via
203*84e33947SAndroid Build Coastguard Worker`EventLoopManagerSingleton::get()`, which returns a pointer to the
204*84e33947SAndroid Build Coastguard Worker`EventLoopManager` object which contains all core system state. Refer to the
205*84e33947SAndroid Build Coastguard Worker`Platform<Module>` header file found in `platform/include`, and implementation
206*84e33947SAndroid Build Coastguard Workerexamples from other platforms for further details.
207*84e33947SAndroid Build Coastguard Worker
208*84e33947SAndroid Build Coastguard Worker### Implementing PALs
209*84e33947SAndroid Build Coastguard Worker
210*84e33947SAndroid Build Coastguard WorkerPAL implementations must only use the callback and system APIs provided in
211*84e33947SAndroid Build Coastguard Worker`open()` to call into the CHRE framework, as the other functions in the CHRE
212*84e33947SAndroid Build Coastguard Workerframework do not have a stable API.
213*84e33947SAndroid Build Coastguard Worker
214*84e33947SAndroid Build Coastguard WorkerIf a PAL implementation is provided as a dynamic module in binary form, it can
215*84e33947SAndroid Build Coastguard Workerbe linked into the CHRE framework at build time by adding it to
216*84e33947SAndroid Build Coastguard Worker`TARGET_SO_LATE_LIBS` in the build variant’s makefile - see the build system
217*84e33947SAndroid Build Coastguard Workerdocumentation for more details.
218*84e33947SAndroid Build Coastguard Worker
219*84e33947SAndroid Build Coastguard Worker#### Recommended Implementation flow
220*84e33947SAndroid Build Coastguard Worker
221*84e33947SAndroid Build Coastguard WorkerWhile there may be minor differences, most CHRE PAL [API][CHRE_PAL_DIR_URL]
222*84e33947SAndroid Build Coastguard Workerimplementations follow the following pattern:
223*84e33947SAndroid Build Coastguard Worker
224*84e33947SAndroid Build Coastguard Worker1. **Implement the Module API for CHRE**
225*84e33947SAndroid Build Coastguard Worker
226*84e33947SAndroid Build Coastguard WorkerCHRE provides a standardized structure containing various interfaces for
227*84e33947SAndroid Build Coastguard Workercalling into the vendor's closed source code in _pal/<feature>.h_. These
228*84e33947SAndroid Build Coastguard Workerfunctions must be implemented by the platform, and provided to CHRE when a call
229*84e33947SAndroid Build Coastguard Workerto _chrePal<feature>GetApi_ function is called. Functions to be implemented are
230*84e33947SAndroid Build Coastguard Workerof two broad categories:
231*84e33947SAndroid Build Coastguard Worker
232*84e33947SAndroid Build Coastguard Worker* _Access functions_
233*84e33947SAndroid Build Coastguard Worker
234*84e33947SAndroid Build Coastguard Worker      CHRE provides feature specific callbacks (see 2. below) to the PAL by
235*84e33947SAndroid Build Coastguard Worker      invoking the _open()_ function in the Module API provided above. The
236*84e33947SAndroid Build Coastguard Worker      structures returned by this function call must be stored somewhere by the
237*84e33947SAndroid Build Coastguard Worker      PAL, and used as necessary to call into the CHRE core. Typically, one or
238*84e33947SAndroid Build Coastguard Worker      more of these callbacks need to be invoked in response to a request from
239*84e33947SAndroid Build Coastguard Worker      CHRE using the Module API provided above.
240*84e33947SAndroid Build Coastguard Worker
241*84e33947SAndroid Build Coastguard Worker      The _close()_ function, when invoked by CHRE, indicates that CHRE is
242*84e33947SAndroid Build Coastguard Worker      shutting down. It is now the PAL's responsibility to perform cleanup,
243*84e33947SAndroid Build Coastguard Worker      cancel active requests, and not invoke any CHRE callbacks from this point
244*84e33947SAndroid Build Coastguard Worker      on.
245*84e33947SAndroid Build Coastguard Worker
246*84e33947SAndroid Build Coastguard Worker* _Request functions_
247*84e33947SAndroid Build Coastguard Worker
248*84e33947SAndroid Build Coastguard Worker      These are interfaces in the PAL API that are module specific, and used by
249*84e33947SAndroid Build Coastguard Worker      CHRE to call into the vendor PAL code.
250*84e33947SAndroid Build Coastguard Worker
251*84e33947SAndroid Build Coastguard Worker2. ** Use CHRE's callbacks **
252*84e33947SAndroid Build Coastguard Worker
253*84e33947SAndroid Build Coastguard WorkerCHRE provides a set of module specific callbacks by invoking the _open()_ access
254*84e33947SAndroid Build Coastguard Workerfunction provided by the Module API. It then starts performing control or data
255*84e33947SAndroid Build Coastguard Workerrequests as needed, by invoking the request functions in the Module API. The PAL
256*84e33947SAndroid Build Coastguard Workeris expected to process these requests, and invoke the appropriate CHRE provided
257*84e33947SAndroid Build Coastguard Workercallback in response. Note that some callbacks might require memory ownership
258*84e33947SAndroid Build Coastguard Workerto be held until explicitly released. For example, upon an audio request from
259*84e33947SAndroid Build Coastguard WorkerCHRE via a call to the `requestAudioDataEvent` audio PAL API, the platform
260*84e33947SAndroid Build Coastguard Workermight invoke the `audioDataEventCallback` when the audio data is ready and
261*84e33947SAndroid Build Coastguard Workerpackaged approapriately into a `chreAudioDataEvent` structure. The platform
262*84e33947SAndroid Build Coastguard Workermust ensure that the memory associated with this data structure remains in
263*84e33947SAndroid Build Coastguard Workercontext until CHRE explicitly releases it via a call to `releaseAudioDataEvent`.
264*84e33947SAndroid Build Coastguard Worker
265*84e33947SAndroid Build Coastguard WorkerPlease refer to the appropriate PAL documentation to ensure that such
266*84e33947SAndroid Build Coastguard Workerdependencies are understood early in the development phase.
267*84e33947SAndroid Build Coastguard Worker
268*84e33947SAndroid Build Coastguard Worker#### Reference implementations
269*84e33947SAndroid Build Coastguard Worker
270*84e33947SAndroid Build Coastguard WorkerPlease refer to the various reference implementations that are
271*84e33947SAndroid Build Coastguard Worker[available][CHRE_LINUX_DIR_URL] for CHRE PALS for the Linux platform. Note that
272*84e33947SAndroid Build Coastguard Workerthis implementation is meant to highlight the usage of the PAL APIs and
273*84e33947SAndroid Build Coastguard Workercallbacks, and might not necessarily port directly over into a resource
274*84e33947SAndroid Build Coastguard Workerconstrained embedded context.
275*84e33947SAndroid Build Coastguard Worker
276*84e33947SAndroid Build Coastguard Worker### PAL Verification
277*84e33947SAndroid Build Coastguard Worker
278*84e33947SAndroid Build Coastguard WorkerThere are several ways to test the PAL implementation beyond manual testing.
279*84e33947SAndroid Build Coastguard WorkerSome of them are listed below in increasing order of the amount of checks run by
280*84e33947SAndroid Build Coastguard Workerthe tests.
281*84e33947SAndroid Build Coastguard Worker
282*84e33947SAndroid Build Coastguard Worker1.  Use the FeatureWorld apps provided under the `apps` directory to exercise
283*84e33947SAndroid Build Coastguard Workerthe various PAL APIs and verify the CHRE API requirements are being met
284*84e33947SAndroid Build Coastguard Worker
285*84e33947SAndroid Build Coastguard Worker2. Assuming the platform PAL implementations utilize CHPP and can communicate
286*84e33947SAndroid Build Coastguard Workerfrom the host machine to the target chipset, execute `run_pal_impl_tests.sh` to
287*84e33947SAndroid Build Coastguard Workerrun basic consistency checks on the PAL
288*84e33947SAndroid Build Coastguard Worker
289*84e33947SAndroid Build Coastguard Worker3.  Execute tests (see Testing section for details)
290*84e33947SAndroid Build Coastguard Worker
291*84e33947SAndroid Build Coastguard Worker## Dynamic Loading Support
292*84e33947SAndroid Build Coastguard Worker
293*84e33947SAndroid Build Coastguard WorkerCHRE requires support for runtime loading and unloading of nanoapp binaries.
294*84e33947SAndroid Build Coastguard WorkerThere are several advantages to this approach:
295*84e33947SAndroid Build Coastguard Worker
296*84e33947SAndroid Build Coastguard Worker* Decouples nanoapp binaries from the underlying system - can maintain and
297*84e33947SAndroid Build Coastguard Worker  deploy a single nanoapp binary across multiple devices, even if they support
298*84e33947SAndroid Build Coastguard Worker  different versions of Android or the CHRE API
299*84e33947SAndroid Build Coastguard Worker
300*84e33947SAndroid Build Coastguard Worker* Makes it possible to update nanoapps without requiring a system reboot,
301*84e33947SAndroid Build Coastguard Worker  particularly on platforms where CHRE runs as part of a statically compiled
302*84e33947SAndroid Build Coastguard Worker  firmware
303*84e33947SAndroid Build Coastguard Worker
304*84e33947SAndroid Build Coastguard Worker* Enables advanced capabilities, like staged rollouts and targeted A/B testing
305*84e33947SAndroid Build Coastguard Worker
306*84e33947SAndroid Build Coastguard WorkerWhile dynamic loading is a responsibility of the platform implementation and may
307*84e33947SAndroid Build Coastguard Workeralready be a part of the underlying OS/system capabilities, the CHRE team is
308*84e33947SAndroid Build Coastguard Workerworking on a reference implementation for a future release. Please reach out via
309*84e33947SAndroid Build Coastguard Workeryour TAM if you are interested in integrating this reference code prior to its
310*84e33947SAndroid Build Coastguard Workerpublic release.
311*84e33947SAndroid Build Coastguard Worker
312*84e33947SAndroid Build Coastguard Worker[CHRE_PAL_DIR_URL]:  https://cs.android.com/android/platform/superproject/+/master:system/chre/pal/include/chre/pal/
313*84e33947SAndroid Build Coastguard Worker[CHRE_LINUX_DIR_URL]: https://cs.android.com/android/platform/superproject/+/master:system/chre/platform/linux/
314*84e33947SAndroid Build Coastguard Worker
315*84e33947SAndroid Build Coastguard Worker## Adding Context Hub support
316*84e33947SAndroid Build Coastguard Worker
317*84e33947SAndroid Build Coastguard WorkerOnce you have implemented the necessary pieces described previously, you are
318*84e33947SAndroid Build Coastguard Workernow ready to add the Context Hub support on the device! Here are the necessary
319*84e33947SAndroid Build Coastguard Workersteps to do this:
320*84e33947SAndroid Build Coastguard Worker
321*84e33947SAndroid Build Coastguard Worker1. Add the HAL implementation on the device
322*84e33947SAndroid Build Coastguard Worker
323*84e33947SAndroid Build Coastguard WorkerAdd the build target of the Context Hub HAL implementation to your device .mk
324*84e33947SAndroid Build Coastguard Workerfile. For example, if the default generic Context Hub HAL is being used, you
325*84e33947SAndroid Build Coastguard Workercan add the following:
326*84e33947SAndroid Build Coastguard Worker
327*84e33947SAndroid Build Coastguard Worker```
328*84e33947SAndroid Build Coastguard WorkerPRODUCT_PACKAGES += \
329*84e33947SAndroid Build Coastguard Worker    android.hardware.contexthub-service.generic
330*84e33947SAndroid Build Coastguard Worker```
331*84e33947SAndroid Build Coastguard Worker
332*84e33947SAndroid Build Coastguard Worker
333*84e33947SAndroid Build Coastguard WorkerCurrently, the generic Context Hub HAL relies on the CHRE daemon to communicate
334*84e33947SAndroid Build Coastguard Workerwith CHRE. If you are using one of our existing platforms, you can add one of
335*84e33947SAndroid Build Coastguard Workerthe following CHRE daemon build targets to your PRODUCT_PACKAGES as you did the
336*84e33947SAndroid Build Coastguard Workergeneric HAL above.
337*84e33947SAndroid Build Coastguard Worker
338*84e33947SAndroid Build Coastguard WorkerQualcomm target: `chre`\
339*84e33947SAndroid Build Coastguard WorkerExynos target: `chre_daemon_exynos`\
340*84e33947SAndroid Build Coastguard WorkerMediaTek target: `TBD`
341*84e33947SAndroid Build Coastguard Worker
342*84e33947SAndroid Build Coastguard WorkerOtherwise, you can look at those target definitions to define a new one for
343*84e33947SAndroid Build Coastguard Workeryour specific platform.
344*84e33947SAndroid Build Coastguard Worker
345*84e33947SAndroid Build Coastguard Worker2. Add the relevant SElinux policies for the device
346*84e33947SAndroid Build Coastguard Worker
347*84e33947SAndroid Build Coastguard WorkerResolve any missing SElinux violations by using the relevant tools such as
348*84e33947SAndroid Build Coastguard Workeraudit2allow, and updating the SElinux policies for your device. You may follow
349*84e33947SAndroid Build Coastguard Workerthe directions in [the official Android page](https://source.android.com/docs/security/features/selinux/validate)
350*84e33947SAndroid Build Coastguard Workerfor additional guidance.
351*84e33947SAndroid Build Coastguard Worker
352*84e33947SAndroid Build Coastguard Worker3. Add the Context Hub feature flag for the device
353*84e33947SAndroid Build Coastguard Worker
354*84e33947SAndroid Build Coastguard WorkerAdd the following in your device.mk file:
355*84e33947SAndroid Build Coastguard Worker
356*84e33947SAndroid Build Coastguard Worker```
357*84e33947SAndroid Build Coastguard WorkerPRODUCT_COPY_FILES += \
358*84e33947SAndroid Build Coastguard Worker    frameworks/native/data/etc/android.hardware.context_hub.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.context_hub.xml
359*84e33947SAndroid Build Coastguard Worker```
360*84e33947SAndroid Build Coastguard Worker
361*84e33947SAndroid Build Coastguard WorkerThe above change will enable the Context Hub Service on the device, and expects
362*84e33947SAndroid Build Coastguard Workerthat the Context Hub HAL comes up. If (1) and (2) are not performed, the device
363*84e33947SAndroid Build Coastguard Workermay fail to boot to the Android home screen.
364