xref: /aosp_15_r20/system/chre/doc/vendor_extensions.md (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker# Extending CHRE with Vendor-specific Functionality
2*84e33947SAndroid Build Coastguard Worker
3*84e33947SAndroid Build Coastguard Worker[TOC]
4*84e33947SAndroid Build Coastguard Worker
5*84e33947SAndroid Build Coastguard WorkerThe CHRE framework is easily extensible with no modifications to the core
6*84e33947SAndroid Build Coastguard Workerframework. Depending on the goals of the new API, one or more of the following
7*84e33947SAndroid Build Coastguard Workersteps must be performed. At a high-level, to add a new vendor-specific API to
8*84e33947SAndroid Build Coastguard WorkerCHRE, one must:
9*84e33947SAndroid Build Coastguard Worker
10*84e33947SAndroid Build Coastguard Worker1. Define new APIs in a header that can be referenced by both platform CHRE
11*84e33947SAndroid Build Coastguard Worker   framework code and vendor-specific nanoapps.
12*84e33947SAndroid Build Coastguard Worker
13*84e33947SAndroid Build Coastguard Worker2. Expose the new APIs from the framework to nanoapps, and connect them to a new
14*84e33947SAndroid Build Coastguard Worker   module to provide the desired functionality
15*84e33947SAndroid Build Coastguard Worker
16*84e33947SAndroid Build Coastguard Worker3. Integrate the new module with existing CHRE framework features, e.g. the
17*84e33947SAndroid Build Coastguard Worker   event subsystem, to provide complete functionality that fits within the
18*84e33947SAndroid Build Coastguard Worker   existing CHRE conventions
19*84e33947SAndroid Build Coastguard Worker
20*84e33947SAndroid Build Coastguard WorkerIt's best to refer to existing standard CHRE API feature areas, such as
21*84e33947SAndroid Build Coastguard Worker`chre/wifi.h` and `WifiRequestManager`, and follow a similar design where
22*84e33947SAndroid Build Coastguard Workerpossible.
23*84e33947SAndroid Build Coastguard Worker
24*84e33947SAndroid Build Coastguard Worker## Defining the API
25*84e33947SAndroid Build Coastguard Worker
26*84e33947SAndroid Build Coastguard WorkerTo prevent collision with future common CHRE API definitions, vendor extensions
27*84e33947SAndroid Build Coastguard Workermust not use the plain ‘chre’ prefix followed by a capitalized letter. Instead,
28*84e33947SAndroid Build Coastguard Workerit’s recommended to prefix the APIs with the vendor’s name as lowercase. For
29*84e33947SAndroid Build Coastguard Workerexample, if your company name is XYZ Semiconductor and you’re defining a new
30*84e33947SAndroid Build Coastguard Worker‘widget’ API, it’s recommended to use a naming scheme like
31*84e33947SAndroid Build Coastguard Worker`chrexyzWidget<FunctionName>()`, and included indirectly via `#include
32*84e33947SAndroid Build Coastguard Worker<chre_xyz.h>` or directly via `<chre_xyz/widget.h>`. The equivalent C++
33*84e33947SAndroid Build Coastguard Workernamespace would be `::chre::xyz`.
34*84e33947SAndroid Build Coastguard Worker
35*84e33947SAndroid Build Coastguard WorkerThere are reserved ranges for vendor/implementation-specific event types
36*84e33947SAndroid Build Coastguard Worker(starting from `CHRE_EVENT_INTERNAL_EXTENDED_FIRST_EVENT`), and other cases
37*84e33947SAndroid Build Coastguard Workerwhere vendors may wish or need to define a custom value in an existing field. To
38*84e33947SAndroid Build Coastguard Workerprevent collision with future versions of the CHRE API, vendor extensions must
39*84e33947SAndroid Build Coastguard Workeronly use values within vendor-reserved ranges. If you would like to add a new
40*84e33947SAndroid Build Coastguard Workervalue to an existing field for a vendor extension and a vendor-reserved range
41*84e33947SAndroid Build Coastguard Workerdoes not already exist, please reach out to the CHRE team for guidance -
42*84e33947SAndroid Build Coastguard Workersolutions may involve creating a new reserved range in the common CHRE API, or
43*84e33947SAndroid Build Coastguard Workerproviding advice on a different method of defining the API.
44*84e33947SAndroid Build Coastguard Worker
45*84e33947SAndroid Build Coastguard WorkerVendors can only add on to the CHRE API - existing APIs must not be changed. Do
46*84e33947SAndroid Build Coastguard Workernot modify core CHRE definitions, for example by adding on fields to common
47*84e33947SAndroid Build Coastguard Workerstructures, re-using event types, repurposing fields that are reserved for
48*84e33947SAndroid Build Coastguard Workerfuture use, etc.
49*84e33947SAndroid Build Coastguard Worker
50*84e33947SAndroid Build Coastguard WorkerIt’s recommended that any vendor extensions consider compatibility when
51*84e33947SAndroid Build Coastguard Workerdesigning it - see the Compatibility section for API design guidelines.
52*84e33947SAndroid Build Coastguard Worker
53*84e33947SAndroid Build Coastguard WorkerIf this API is intended to be open-sourced, it should be added to
54*84e33947SAndroid Build Coastguard Worker`platform/<platform_name>/extensions/include`. Otherwise, it’s suggested that
55*84e33947SAndroid Build Coastguard Workerthe API be placed outside of the CHRE tree, in a separate Git project under
56*84e33947SAndroid Build Coastguard Worker`vendor/` in the Android tree, to avoid potential conflicts when upgrading to a
57*84e33947SAndroid Build Coastguard Workernew version of CHRE.
58*84e33947SAndroid Build Coastguard Worker
59*84e33947SAndroid Build Coastguard Worker### Build Customization
60*84e33947SAndroid Build Coastguard Worker
61*84e33947SAndroid Build Coastguard WorkerAs part of the CHRE framework build system, the `CHRE_VARIANT_MK_INCLUDES`
62*84e33947SAndroid Build Coastguard Workerenvironment variable can be used to inject an external `.mk` file into the
63*84e33947SAndroid Build Coastguard Workertop-level build without any source code changes in the system/chre project.
64*84e33947SAndroid Build Coastguard WorkerAlternatively, if open sourcing, the `platform.mk` file should contain the
65*84e33947SAndroid Build Coastguard Workeradditions needed to support the new vendor API. Refer to the CHRE framework
66*84e33947SAndroid Build Coastguard Workerbuild documentation for further details.
67*84e33947SAndroid Build Coastguard Worker
68*84e33947SAndroid Build Coastguard WorkerTo expose the new functionality to nanoapps, it’s recommended to create a single
69*84e33947SAndroid Build Coastguard Worker`.mk` file that adds the necessary `COMMON_CFLAGS` entries (and potentially
70*84e33947SAndroid Build Coastguard Workerother build configuration). For example, create a `chrexyz.mk` file which
71*84e33947SAndroid Build Coastguard Workernanoapps should include in their Makefile prior to including
72*84e33947SAndroid Build Coastguard Worker`$(CHRE_PREFIX)/build/nanoapp/app.mk`.
73*84e33947SAndroid Build Coastguard Worker
74*84e33947SAndroid Build Coastguard Worker## Threading Model
75*84e33947SAndroid Build Coastguard Worker
76*84e33947SAndroid Build Coastguard WorkerInteractions with a nanoapp always happen from within the CHRE thread that runs
77*84e33947SAndroid Build Coastguard Workerthe EventLoop, so vendor extension code does not need to worry about race
78*84e33947SAndroid Build Coastguard Workerconditions due to multiple nanoapps calling into APIs, and likewise nanoapps do
79*84e33947SAndroid Build Coastguard Workernot need to worry about race conditions in its callbacks/handlers. However, it
80*84e33947SAndroid Build Coastguard Workeris common for a platform module to receive data in a callback on another thread.
81*84e33947SAndroid Build Coastguard WorkerIn that case, it is recommended to use `EventLoopManager::deferCallback()` to
82*84e33947SAndroid Build Coastguard Workerpass the incoming data to the CHRE thread for processing, as opposed to using
83*84e33947SAndroid Build Coastguard Workermutexes or other synchronization primitives, to avoid multithreading-related
84*84e33947SAndroid Build Coastguard Workerissues that can arise in rare conditions. Further, note that most of the core
85*84e33947SAndroid Build Coastguard WorkerCHRE functionality is only safe to call from within the CHRE thread (other than
86*84e33947SAndroid Build Coastguard Workerposting an event, or methods that are explicitly marked as thread-safe).
87*84e33947SAndroid Build Coastguard Worker
88*84e33947SAndroid Build Coastguard Worker## Initialization
89*84e33947SAndroid Build Coastguard Worker
90*84e33947SAndroid Build Coastguard WorkerSince the new API will not be part of the core framework, it won’t be attached
91*84e33947SAndroid Build Coastguard Workerto `EventLoopManager` or initialized as part of `chre::init()` or
92*84e33947SAndroid Build Coastguard Worker`EventLoopManagerSingleton::get()->lateInit()`, since vendor-extension APIs are
93*84e33947SAndroid Build Coastguard Workerby definition not part of the common code. Instead, a separate singleton object
94*84e33947SAndroid Build Coastguard Workershould be created, for example `chre::xyz::VendorExtensionManager`, and
95*84e33947SAndroid Build Coastguard Workerplatform-specific initialization code should invoke any necessary initialization
96*84e33947SAndroid Build Coastguard Worker**after** `chre::init` is called, but **before** loading any static nanoapps or
97*84e33947SAndroid Build Coastguard Workerinvoking `EventLoop::run()` to ensure that nanoapps don’t begin interacting with
98*84e33947SAndroid Build Coastguard Workerthe API before its state is ready.
99*84e33947SAndroid Build Coastguard Worker
100*84e33947SAndroid Build Coastguard Worker## Handling Nanoapp API Calls
101*84e33947SAndroid Build Coastguard Worker
102*84e33947SAndroid Build Coastguard WorkerCalls from a nanoapp into the CHRE framework first arrive in platform-specific
103*84e33947SAndroid Build Coastguard Workercode (refer to the Framework Overview documentation for details). The first step
104*84e33947SAndroid Build Coastguard Workeronce an API call reaches the framework is usually to call
105*84e33947SAndroid Build Coastguard Worker`EventLoopManager::validateChreApiCall(__func__)`. This fetches a pointer to the
106*84e33947SAndroid Build Coastguard Worker`Nanoapp` object associated with the nanoapp that invoked the API, which will
107*84e33947SAndroid Build Coastguard Workerfail if the API is called outside of the EventLoop thread context (see the
108*84e33947SAndroid Build Coastguard WorkerThreading Model above). From this point, the vendor extension singleton should
109*84e33947SAndroid Build Coastguard Workerbe used to invoke the appropriate functionality.
110*84e33947SAndroid Build Coastguard Worker
111*84e33947SAndroid Build Coastguard Worker## Sending Events to Nanoapps
112*84e33947SAndroid Build Coastguard Worker
113*84e33947SAndroid Build Coastguard WorkerVendor extension APIs that need to pass data to a nanoapp asynchronously should
114*84e33947SAndroid Build Coastguard Workeruse the event susbsystem, using the vendor-reserved event type range (starting
115*84e33947SAndroid Build Coastguard Workerat `CHRE_EVENT_INTERNAL_EXTENDED_FIRST_EVENT` and extending to
116*84e33947SAndroid Build Coastguard Worker`CHRE_EVENT_INTERNAL_LAST_EVENT`). Event types for a given vendor extension
117*84e33947SAndroid Build Coastguard Workershould be globally unique and stable over time.
118*84e33947SAndroid Build Coastguard Worker
119*84e33947SAndroid Build Coastguard WorkerSynchronous API calls that can potentially block for periods greater than a few
120*84e33947SAndroid Build Coastguard Workermilliseconds are discouraged, as these can prevent other nanoapps from
121*84e33947SAndroid Build Coastguard Workerexecuting, and/or cause the pending event queue to grow excessively during
122*84e33947SAndroid Build Coastguard Workerperiods of high activity. Refer to the GNSS and WWAN APIs for design patterns
123*84e33947SAndroid Build Coastguard Workerrelated to passing data to a nanoapp asynchronously, using custom event payloads
124*84e33947SAndroid Build Coastguard Workerand/or `chreAsyncResult`.
125*84e33947SAndroid Build Coastguard Worker
126*84e33947SAndroid Build Coastguard WorkerEvents can either be unicast to a nanoapp identified by its instance ID
127*84e33947SAndroid Build Coastguard Worker(`Nanoapp::getInstanceId()`), or broadcast to all nanoapps registered for the
128*84e33947SAndroid Build Coastguard Workergiven event type - see `Nanoapp::registerForBroadcastEvent()` and
129*84e33947SAndroid Build Coastguard Worker`Nanoapp::unregisterForBroadcastEvent()`.
130*84e33947SAndroid Build Coastguard Worker
131*84e33947SAndroid Build Coastguard WorkerUse `EventLoop::postEventOrDie()` or `EventLoop::postLowPriorityEventOrFree()`
132*84e33947SAndroid Build Coastguard Worker(via `EventLoopManagerSingleton::get()->getEventLoop()`) to pass events to
133*84e33947SAndroid Build Coastguard Workernanoapps, depending on what error handling is desired in the case that the event
134*84e33947SAndroid Build Coastguard Workercannot be posted to the queue. Any memory referenced by `eventData` must not be
135*84e33947SAndroid Build Coastguard Workermodified until `freeCallback` is invoked.
136