• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _CHRE_EVENT_H_
18 #define _CHRE_EVENT_H_
19 
20 /**
21  * @file
22  * Context Hub Runtime Environment API dealing with events and messages.
23  */
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 
29 #include <chre/toolchain.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /**
36  * The CHRE implementation is required to provide the following preprocessor
37  * defines via the build system.
38  *
39  * CHRE_MESSAGE_TO_HOST_MAX_SIZE: The maximum size, in bytes, allowed for
40  *     a message sent to chreSendMessageToHostEndpoint().  This must be at least
41  *     CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE.
42  */
43 
44 #ifndef CHRE_MESSAGE_TO_HOST_MAX_SIZE
45 #error CHRE_MESSAGE_TO_HOST_MAX_SIZE must be defined by the CHRE implementation
46 #endif
47 
48 /**
49  * The minimum size, in bytes, any CHRE implementation will use for
50  * CHRE_MESSAGE_TO_HOST_MAX_SIZE is set to 1000 for v1.5+ CHRE implementations,
51  * and 128 for v1.0-v1.4 implementations (previously kept in
52  * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, which has been removed).
53  *
54  * All CHRE implementations supporting v1.5+ must support the raised limit of
55  * 1000 bytes, however a nanoapp compiled against v1.5 cannot assume this
56  * limit if there is a possibility their binary will run on a v1.4 or earlier
57  * implementation that had a lower limit. To allow for nanoapp compilation in
58  * these situations, CHRE_MESSAGE_TO_HOST_MAX_SIZE must be set to the minimum
59  * value the nanoapp may encounter, and CHRE_NANOAPP_SUPPORTS_PRE_V1_5 can be
60  * defined to skip the compile-time check.
61  */
62 #if (!defined(CHRE_NANOAPP_SUPPORTS_PRE_V1_5) && \
63      CHRE_MESSAGE_TO_HOST_MAX_SIZE < 1000) ||    \
64     (defined(CHRE_NANOAPP_SUPPORTS_PRE_V1_5) &&  \
65      CHRE_MESSAGE_TO_HOST_MAX_SIZE < 128)
66 #error CHRE_MESSAGE_TO_HOST_MAX_SIZE is too small.
67 #endif
68 
69 /**
70  * The lowest numerical value legal for a user-defined event.
71  *
72  * The system reserves all event values from 0 to 0x7FFF, inclusive.
73  * User events may use any value in the range 0x8000 to 0xFFFF, inclusive.
74  *
75  * Note that the same event values might be used by different nanoapps
76  * for different meanings.  This is not a concern, as these values only
77  * have meaning when paired with the originating nanoapp.
78  */
79 #define CHRE_EVENT_FIRST_USER_VALUE  UINT16_C(0x8000)
80 
81 /**
82  * nanoappHandleEvent argument: struct chreMessageFromHostData
83  *
84  * The format of the 'message' part of this structure is left undefined,
85  * and it's up to the nanoapp and host to have an established protocol
86  * beforehand.
87  */
88 #define CHRE_EVENT_MESSAGE_FROM_HOST  UINT16_C(0x0001)
89 
90 /**
91  * nanoappHandleEvent argument: 'cookie' given to chreTimerSet() method.
92  *
93  * Indicates that a timer has elapsed, in accordance with how chreTimerSet() was
94  * invoked.
95  */
96 #define CHRE_EVENT_TIMER  UINT16_C(0x0002)
97 
98 /**
99  * nanoappHandleEvent argument: struct chreNanoappInfo
100  *
101  * Indicates that a nanoapp has successfully started (its nanoappStart()
102  * function has been called, and it returned true) and is able to receive events
103  * sent via chreSendEvent().  Note that this event is not sent for nanoapps that
104  * were started prior to the current nanoapp - use chreGetNanoappInfo() to
105  * determine if another nanoapp is already running.
106  *
107  * @see chreConfigureNanoappInfoEvents
108  * @since v1.1
109  */
110 #define CHRE_EVENT_NANOAPP_STARTED  UINT16_C(0x0003)
111 
112 /**
113  * nanoappHandleEvent argument: struct chreNanoappInfo
114  *
115  * Indicates that a nanoapp has stopped executing and is no longer able to
116  * receive events sent via chreSendEvent().  Any events sent prior to receiving
117  * this event are not guaranteed to have been delivered.
118  *
119  * @see chreConfigureNanoappInfoEvents
120  * @since v1.1
121  */
122 #define CHRE_EVENT_NANOAPP_STOPPED  UINT16_C(0x0004)
123 
124 /**
125  * nanoappHandleEvent argument: NULL
126  *
127  * Indicates that CHRE has observed the host wake from low-power sleep state.
128  *
129  * @see chreConfigureHostSleepStateEvents
130  * @since v1.2
131  */
132 #define CHRE_EVENT_HOST_AWAKE  UINT16_C(0x0005)
133 
134 /**
135  * nanoappHandleEvent argument: NULL
136  *
137  * Indicates that CHRE has observed the host enter low-power sleep state.
138  *
139  * @see chreConfigureHostSleepStateEvents
140  * @since v1.2
141  */
142 #define CHRE_EVENT_HOST_ASLEEP  UINT16_C(0x0006)
143 
144 /**
145  * nanoappHandleEvent argument: NULL
146  *
147  * Indicates that CHRE is collecting debug dumps. Nanoapps can call
148  * chreDebugDumpLog() to log their debug data while handling this event.
149  *
150  * @see chreConfigureDebugDumpEvent
151  * @see chreDebugDumpLog
152  * @since v1.4
153  */
154 #define CHRE_EVENT_DEBUG_DUMP  UINT16_C(0x0007)
155 
156 /**
157  * nanoappHandleEvent argument: struct chreHostEndpointNotification
158  *
159  * Notifications event regarding a host endpoint.
160  *
161  * @see chreConfigureHostEndpointNotifications
162  * @since v1.6
163  */
164 #define CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION UINT16_C(0x0008)
165 
166 /**
167  * First possible value for CHRE_EVENT_SENSOR events.
168  *
169  * This allows us to separately define our CHRE_EVENT_SENSOR_* events in
170  * chre/sensor.h, without fear of collision with other event values.
171  */
172 #define CHRE_EVENT_SENSOR_FIRST_EVENT  UINT16_C(0x0100)
173 
174 /**
175  * Last possible value for CHRE_EVENT_SENSOR events.
176  *
177  * This allows us to separately define our CHRE_EVENT_SENSOR_* events in
178  * chre/sensor.h, without fear of collision with other event values.
179  */
180 #define CHRE_EVENT_SENSOR_LAST_EVENT  UINT16_C(0x02FF)
181 
182 /**
183  * First event in the block reserved for GNSS. These events are defined in
184  * chre/gnss.h.
185  */
186 #define CHRE_EVENT_GNSS_FIRST_EVENT  UINT16_C(0x0300)
187 #define CHRE_EVENT_GNSS_LAST_EVENT   UINT16_C(0x030F)
188 
189 /**
190  * First event in the block reserved for WiFi. These events are defined in
191  * chre/wifi.h.
192  */
193 #define CHRE_EVENT_WIFI_FIRST_EVENT  UINT16_C(0x0310)
194 #define CHRE_EVENT_WIFI_LAST_EVENT   UINT16_C(0x031F)
195 
196 /**
197  * First event in the block reserved for WWAN. These events are defined in
198  * chre/wwan.h.
199  */
200 #define CHRE_EVENT_WWAN_FIRST_EVENT  UINT16_C(0x0320)
201 #define CHRE_EVENT_WWAN_LAST_EVENT   UINT16_C(0x032F)
202 
203 /**
204  * First event in the block reserved for audio. These events are defined in
205  * chre/audio.h.
206  */
207 #define CHRE_EVENT_AUDIO_FIRST_EVENT UINT16_C(0x0330)
208 #define CHRE_EVENT_AUDIO_LAST_EVENT  UINT16_C(0x033F)
209 
210 /**
211  * First event in the block reserved for settings changed notifications.
212  * These events are defined in chre/user_settings.h
213  *
214  * @since v1.5
215  */
216 #define CHRE_EVENT_SETTING_CHANGED_FIRST_EVENT UINT16_C(0x340)
217 #define CHRE_EVENT_SETTING_CHANGED_LAST_EVENT  UINT16_C(0x34F)
218 
219 /**
220  * First event in the block reserved for Bluetooth LE. These events are defined
221  * in chre/ble.h.
222  */
223 #define CHRE_EVENT_BLE_FIRST_EVENT UINT16_C(0x0350)
224 #define CHRE_EVENT_BLE_LAST_EVENT  UINT16_C(0x035F)
225 
226 /**
227  * First in the extended range of values dedicated for internal CHRE
228  * implementation usage.
229  *
230  * This range is semantically the same as the internal event range defined
231  * below, but has been extended to allow for more implementation-specific events
232  * to be used.
233  *
234  * @since v1.1
235  */
236 #define CHRE_EVENT_INTERNAL_EXTENDED_FIRST_EVENT  UINT16_C(0x7000)
237 
238 /**
239  * First in a range of values dedicated for internal CHRE implementation usage.
240  *
241  * If a CHRE wishes to use events internally, any values within this range
242  * are assured not to be taken by future CHRE API additions.
243  */
244 #define CHRE_EVENT_INTERNAL_FIRST_EVENT  UINT16_C(0x7E00)
245 
246 /**
247  * Last in a range of values dedicated for internal CHRE implementation usage.
248  *
249  * If a CHRE wishes to use events internally, any values within this range
250  * are assured not to be taken by future CHRE API additions.
251  */
252 #define CHRE_EVENT_INTERNAL_LAST_EVENT  UINT16_C(0x7FFF)
253 
254 /**
255  * A special value for the hostEndpoint argument in
256  * chreSendMessageToHostEndpoint() that indicates that the message should be
257  * delivered to all host endpoints.  This value will not be used in the
258  * hostEndpoint field of struct chreMessageFromHostData supplied with
259  * CHRE_EVENT_MESSAGE_FROM_HOST.
260  *
261  * @since v1.1
262  */
263 #define CHRE_HOST_ENDPOINT_BROADCAST  UINT16_C(0xFFFF)
264 
265 /**
266  * A special value for hostEndpoint in struct chreMessageFromHostData that
267  * indicates that a host endpoint is unknown or otherwise unspecified.  This
268  * value may be received in CHRE_EVENT_MESSAGE_FROM_HOST, but it is not valid to
269  * provide it to chreSendMessageToHostEndpoint().
270  *
271  * @since v1.1
272  */
273 #define CHRE_HOST_ENDPOINT_UNSPECIFIED  UINT16_C(0xFFFE)
274 
275 /**
276  * Bitmask values that can be given as input to the messagePermissions parameter
277  * of chreSendMessageWithPermissions(). These values are typically used by
278  * nanoapps when they used data from the corresponding CHRE APIs to produce the
279  * message contents being sent and is used to attribute permissions usage on
280  * the Android side. See chreSendMessageWithPermissions() for more details on
281  * how these values are used when sending a message.
282  *
283  * Values in the range
284  * [CHRE_MESSAGE_PERMISSION_VENDOR_START, CHRE_MESSAGE_PERMISSION_VENDOR_END]
285  * are reserved for vendors to use when adding support for permission-gated APIs
286  * in their implementations.
287  *
288  * On the Android side, CHRE permissions are mapped as follows:
289  * - CHRE_MESSAGE_PERMISSION_AUDIO: android.permission.RECORD_AUDIO
290  * - CHRE_MESSAGE_PERMISSION_GNSS, CHRE_MESSAGE_PERMISSION_WIFI, and
291  *   CHRE_MESSAGE_PERMISSION_WWAN: android.permission.ACCESS_FINE_LOCATION, and
292  *   android.permissions.ACCESS_BACKGROUND_LOCATION
293  *
294  * @since v1.5
295  *
296  * @defgroup CHRE_MESSAGE_PERMISSION
297  * @{
298  */
299 
300 #define CHRE_MESSAGE_PERMISSION_NONE UINT32_C(0)
301 #define CHRE_MESSAGE_PERMISSION_AUDIO UINT32_C(1)
302 #define CHRE_MESSAGE_PERMISSION_GNSS (UINT32_C(1) << 1)
303 #define CHRE_MESSAGE_PERMISSION_WIFI (UINT32_C(1) << 2)
304 #define CHRE_MESSAGE_PERMISSION_WWAN (UINT32_C(1) << 3)
305 #define CHRE_MESSAGE_PERMISSION_BLE (UINT32_C(1) << 4)
306 #define CHRE_MESSAGE_PERMISSION_VENDOR_START (UINT32_C(1) << 24)
307 #define CHRE_MESSAGE_PERMISSION_VENDOR_END (UINT32_C(1) << 31)
308 
309 /** @} */
310 
311 /**
312  * Data provided with CHRE_EVENT_MESSAGE_FROM_HOST.
313  */
314 struct chreMessageFromHostData {
315     /**
316      * Message type supplied by the host.
317      *
318      * @note In CHRE API v1.0, support for forwarding this field from the host
319      * was not strictly required, and some implementations did not support it.
320      * However, its support is mandatory as of v1.1.
321      */
322     union {
323         /**
324          * The preferred name to use when referencing this field.
325          *
326          * @since v1.1
327          */
328         uint32_t messageType;
329 
330         /**
331          * @deprecated This is the name for the messageType field used in v1.0.
332          * Left to allow code to compile against both v1.0 and v1.1 of the API
333          * definition without needing to use #ifdefs. This will be removed in a
334          * future API update - use messageType instead.
335          */
336         uint32_t reservedMessageType;
337     };
338 
339     /**
340      * The size, in bytes of the following 'message'.
341      *
342      * This can be 0.
343      */
344     uint32_t messageSize;
345 
346     /**
347      * The message from the host.
348      *
349      * These contents are of a format that the host and nanoapp must have
350      * established beforehand.
351      *
352      * This data is 'messageSize' bytes in length.  Note that if 'messageSize'
353      * is 0, this might be NULL.
354      */
355     const void *message;
356 
357     /**
358      * An identifier for the host-side entity that sent this message.  Unless
359      * this is set to CHRE_HOST_ENDPOINT_UNSPECIFIED, it can be used in
360      * chreSendMessageToHostEndpoint() to send a directed reply that will only
361      * be received by the given entity on the host.  Endpoint identifiers are
362      * opaque values assigned at runtime, so they cannot be assumed to always
363      * describe a specific entity across restarts.
364      *
365      * If running on a CHRE API v1.0 implementation, this field will always be
366      * set to CHRE_HOST_ENDPOINT_UNSPECIFIED.
367      *
368      * @since v1.1
369      */
370     uint16_t hostEndpoint;
371 };
372 
373 /**
374  * Provides metadata for a nanoapp in the system.
375  */
376 struct chreNanoappInfo {
377     /**
378      * Nanoapp identifier. The convention for populating this value is to set
379      * the most significant 5 bytes to a value that uniquely identifies the
380      * vendor, and the lower 3 bytes identify the nanoapp.
381      */
382     uint64_t appId;
383 
384     /**
385      * Nanoapp version.  The semantics of this field are defined by the nanoapp,
386      * however nanoapps are recommended to follow the same scheme used for the
387      * CHRE version exposed in chreGetVersion().  That is, the most significant
388      * byte represents the major version, the next byte the minor version, and
389      * the lower two bytes the patch version.
390      */
391     uint32_t version;
392 
393     /**
394      * The instance ID of this nanoapp, which can be used in chreSendEvent() to
395      * address an event specifically to this nanoapp.  This identifier is
396      * guaranteed to be unique among all nanoapps in the system.
397      *
398      * @since v1.6
399      * Instance ID is guaranteed to never go beyond INT16_MAX. This helps the
400      * instance ID be packed into other information inside an int (useful for
401      * RPC routing).
402      */
403     uint32_t instanceId;
404 };
405 
406 /**
407  * The types of notification events that can be included in struct
408  * chreHostEndpointNotification.
409  *
410  * @defgroup HOST_ENDPOINT_NOTIFICATION_TYPE
411  * @{
412  */
413 #define HOST_ENDPOINT_NOTIFICATION_TYPE_DISCONNECT UINT8_C(0)
414 /** @} */
415 
416 /**
417  * Data provided in CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION.
418  */
419 struct chreHostEndpointNotification {
420     /**
421      * The ID of the host endpoint that this notification is for.
422      */
423     uint16_t hostEndpointId;
424 
425     /**
426      * The type of notification this event represents, which should be
427      * one of the HOST_ENDPOINT_NOTIFICATION_TYPE_* values.
428      */
429     uint8_t notificationType;
430 
431     /**
432      * Reserved for future use, must be zero.
433      */
434     uint8_t reserved;
435 };
436 
437 //! The maximum length of a host endpoint's name.
438 #define CHRE_MAX_ENDPOINT_NAME_LEN (51)
439 
440 //! The maximum length of a host endpoint's tag.
441 #define CHRE_MAX_ENDPOINT_TAG_LEN (51)
442 
443 /**
444  * The type of host endpoint that can be used in the hostEndpointType field
445  * of chreHostEndpointInfo.
446  *
447  * @since v1.6
448  *
449  * @defgroup CHRE_HOST_ENDPOINT_TYPE_
450  * @{
451  */
452 
453 //! The host endpoint is part of the Android system framework.
454 #define CHRE_HOST_ENDPOINT_TYPE_FRAMEWORK UINT8_C(0)
455 
456 //! The host endpoint is an Android app.
457 #define CHRE_HOST_ENDPOINT_TYPE_APP UINT8_C(1)
458 
459 //! Values in the range [CHRE_HOST_ENDPOINT_TYPE_VENDOR_START,
460 //! CHRE_HOST_ENDPOINT_TYPE_VENDOR_END] can be a custom defined host endpoint
461 //! type for platform-specific vendor use.
462 #define CHRE_HOST_ENDPOINT_TYPE_VENDOR_START UINT8_C(128)
463 #define CHRE_HOST_ENDPOINT_TYPE_VENDOR_END UINT8_C(255)
464 
465 /** @} */
466 
467 /**
468  * Provides metadata for a host endpoint.
469  *
470  * @since v1.6
471  */
472 struct chreHostEndpointInfo {
473     //! The endpoint ID of this host.
474     uint16_t hostEndpointId;
475 
476     //! The type of host endpoint, which must be set to one of the
477     //! CHRE_HOST_ENDPOINT_TYPE_* values or a value in the vendor-reserved
478     //! range.
479     uint8_t hostEndpointType;
480 
481     //! Flag indicating if the packageName/endpointName field is valid.
482     uint8_t isNameValid : 1;
483 
484     //! Flag indicating if the attributionTag/endpointTag field is valid.
485     uint8_t isTagValid : 1;
486 
487     //! A union of null-terminated host name strings.
488     union {
489         //! The Android package name associated with this host, valid if the
490         //! hostEndpointType is CHRE_HOST_ENDPOINT_TYPE_APP or
491         //! CHRE_HOST_ENDPOINT_TYPE_FRAMEWORK. Refer to the Android documentation
492         //! for the package attribute in the app manifest.
493         char packageName[CHRE_MAX_ENDPOINT_NAME_LEN];
494 
495         //! A generic endpoint name that can be used for endpoints that
496         //! may not have a package name.
497         char endpointName[CHRE_MAX_ENDPOINT_NAME_LEN];
498     };
499 
500     //! A union of null-terminated host tag strings for further identification.
501     union {
502         //! The attribution tag associated with this host that is used to audit
503         //! access to data, which can be valid if the hostEndpointType is
504         //! CHRE_HOST_ENDPOINT_TYPE_APP. Refer to the Android documentation
505         //! regarding data audit using attribution tags.
506         char attributionTag[CHRE_MAX_ENDPOINT_TAG_LEN];
507 
508         //! A generic endpoint tag that can be used for endpoints that
509         //! may not have an attribution tag.
510         char endpointTag[CHRE_MAX_ENDPOINT_TAG_LEN];
511     };
512 };
513 
514 /**
515  * An RPC service exposed by a nanoapp.
516  *
517  * The implementation of the RPC interface is not defined by the HAL, and is written
518  * at the messaging endpoint layers (Android app and/or CHRE nanoapp). NanoappRpcService
519  * contains the informational metadata to be consumed by the RPC interface layer.
520  */
521 struct chreNanoappRpcService {
522     /**
523      * The unique 64-bit ID of an RPC service exposed by a nanoapp. Note that
524      * the uniqueness is only required within the nanoapp's domain (i.e. the
525      * combination of the nanoapp ID and service id must be unique).
526      */
527     uint64_t id;
528 
529     /**
530      * The software version of this service, which follows the sematic
531      * versioning scheme (see semver.org). It follows the format
532      * major.minor.patch, where major and minor versions take up one byte
533      * each, and the patch version takes up the final 2 bytes.
534      */
535     uint32_t version;
536 };
537 
538 /**
539  * Callback which frees data associated with an event.
540  *
541  * This callback is (optionally) provided to the chreSendEvent() method as
542  * a means for freeing the event data and performing any other cleanup
543  * necessary when the event is completed.  When this callback is invoked,
544  * 'eventData' is no longer needed and can be released.
545  *
546  * @param eventType  The 'eventType' argument from chreSendEvent().
547  * @param eventData  The 'eventData' argument from chreSendEvent().
548  *
549  * @see chreSendEvent
550  */
551 typedef void (chreEventCompleteFunction)(uint16_t eventType, void *eventData);
552 
553 /**
554  * Callback which frees a message.
555  *
556  * This callback is (optionally) provided to the chreSendMessageToHostEndpoint()
557  * method as a means for freeing the message.  When this callback is invoked,
558  * 'message' is no longer needed and can be released.  Note that this in
559  * no way assures that said message did or did not make it to the host, simply
560  * that this memory is no longer needed.
561  *
562  * @param message  The 'message' argument from chreSendMessageToHostEndpoint().
563  * @param messageSize  The 'messageSize' argument from
564  *     chreSendMessageToHostEndpoint().
565  *
566  * @see chreSendMessageToHostEndpoint
567  */
568 typedef void (chreMessageFreeFunction)(void *message, size_t messageSize);
569 
570 
571 /**
572  * Enqueue an event to be sent to another nanoapp.
573  *
574  * @param eventType  This is a user-defined event type, of at least the
575  *     value CHRE_EVENT_FIRST_USER_VALUE.  It is illegal to attempt to use any
576  *     of the CHRE_EVENT_* values reserved for the CHRE.
577  * @param eventData  A pointer value that will be understood by the receiving
578  *     app.  Note that NULL is perfectly acceptable.  It also is not required
579  *     that this be a valid pointer, although if this nanoapp is intended to
580  *     work on arbitrary CHRE implementations, then the size of a
581  *     pointer cannot be assumed to be a certain size.  Note that the caller
582  *     no longer owns this memory after the call.
583  * @param freeCallback  A pointer to a callback function.  After the lifetime
584  *     of 'eventData' is over (either through successful delivery or the event
585  *     being dropped), this callback will be invoked.  This argument is allowed
586  *     to be NULL, in which case no callback will be invoked.
587  * @param targetInstanceId  The ID of the instance we're delivering this event
588  *     to.  Note that this is allowed to be our own instance.  The instance ID
589  *     of a nanoapp can be retrieved by using chreGetNanoappInfoByInstanceId().
590  * @return true if the event was enqueued, false otherwise.  Note that even
591  *     if this method returns 'false', the 'freeCallback' will be invoked,
592  *     if non-NULL.  Note in the 'false' case, the 'freeCallback' may be
593  *     invoked directly from within chreSendEvent(), so it's necessary
594  *     for nanoapp authors to avoid possible recursion with this.
595  *
596  * @see chreEventDataFreeFunction
597  */
598 bool chreSendEvent(uint16_t eventType, void *eventData,
599                    chreEventCompleteFunction *freeCallback,
600                    uint32_t targetInstanceId);
601 
602 /**
603  * Send a message to the host, using the broadcast endpoint
604  * CHRE_HOST_ENDPOINT_BROADCAST.  Refer to chreSendMessageToHostEndpoint() for
605  * further details.
606  *
607  * @see chreSendMessageToHostEndpoint
608  *
609  * @deprecated New code should use chreSendMessageToHostEndpoint() instead of
610  * this function.  A future update to the API may cause references to this
611  * function to produce a compiler warning.
612  */
613 bool chreSendMessageToHost(void *message, uint32_t messageSize,
614                            uint32_t messageType,
615                            chreMessageFreeFunction *freeCallback)
616     CHRE_DEPRECATED("Use chreSendMessageToHostEndpoint instead");
617 
618 /**
619  * Send a message to the host, using CHRE_MESSAGE_PERMISSION_NONE for the
620  * associated message permissions. This method must only be used if no data
621  * provided by CHRE's audio, GNSS, WiFi, and WWAN APIs was used to produce the
622  * contents of the message being sent. Refer to chreSendMessageWithPermissions()
623  * for further details.
624  *
625  * @see chreSendMessageWithPermissions
626  *
627  * @since v1.1
628  */
629 bool chreSendMessageToHostEndpoint(void *message, size_t messageSize,
630                                    uint32_t messageType, uint16_t hostEndpoint,
631                                    chreMessageFreeFunction *freeCallback);
632 
633 /**
634  * Send a message to the host, waking it up if it is currently asleep.
635  *
636  * This message is by definition arbitrarily defined.  Since we're not
637  * just a passing a pointer to memory around the system, but need to copy
638  * this into various buffers to send it to the host, the CHRE
639  * implementation cannot be asked to support an arbitrarily large message
640  * size.  As a result, we have the CHRE implementation define
641  * CHRE_MESSAGE_TO_HOST_MAX_SIZE.
642  *
643  * CHRE_MESSAGE_TO_HOST_MAX_SIZE is not given a value by the Platform API.  The
644  * Platform API does define CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, and requires
645  * that CHRE_MESSAGE_TO_HOST_MAX_SIZE is at least that value.
646  *
647  * As a result, if your message sizes are all less than
648  * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, then you have no concerns on any
649  * CHRE implementation.  If your message sizes are larger, you'll need to
650  * come up with a strategy for splitting your message across several calls
651  * to this method.  As long as that strategy works for
652  * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, it will work across all CHRE
653  * implementations (although on some implementations less calls to this
654  * method may be necessary).
655  *
656  * When sending a message to the host, the ContextHub service will enforce
657  * the host client has been granted Android-level permissions corresponding to
658  * the ones the nanoapp declares it uses through CHRE_NANOAPP_USES_AUDIO, etc.
659  * In addition to this, the permissions bitmask provided as input to this method
660  * results in the Android framework using app-ops to verify and log access upon
661  * message delivery to an application. This is primarily useful for ensuring
662  * accurate attribution for messages generated using permission-controlled data.
663  * The bitmask declared by the nanoapp for this message must be a
664  * subset of the permissions it declared it would use at build time or the
665  * message will be rejected.
666  *
667  * Nanoapps must use this method if the data they are sending contains or was
668  * derived from any data sampled through CHRE's audio, GNSS, WiFi, or WWAN APIs.
669  * Additionally, if vendors add APIs to expose data that would be guarded by a
670  * permission in Android, vendors must support declaring a message permission
671  * through this method.
672  *
673  * @param message  Pointer to a block of memory to send to the host.
674  *     NULL is acceptable only if messageSize is 0.  If non-NULL, this
675  *     must be a legitimate pointer (that is, unlike chreSendEvent(), a small
676  *     integral value cannot be cast to a pointer for this).  Note that the
677  *     caller no longer owns this memory after the call.
678  * @param messageSize  The size, in bytes, of the given message. If this exceeds
679  *     CHRE_MESSAGE_TO_HOST_MAX_SIZE, the message will be rejected.
680  * @param messageType  Message type sent to the app on the host.
681  *     NOTE: In CHRE API v1.0, support for forwarding this field to the host was
682  *     not strictly required, and some implementations did not support it.
683  *     However, its support is mandatory as of v1.1.
684  * @param hostEndpoint  An identifier for the intended recipient of the message,
685  *     or CHRE_HOST_ENDPOINT_BROADCAST if all registered endpoints on the host
686  *     should receive the message.  Endpoint identifiers are assigned on the
687  *     host side, and nanoapps may learn of the host endpoint ID of an intended
688  *     recipient via an initial message sent by the host.  This parameter is
689  *     always treated as CHRE_HOST_ENDPOINT_BROADCAST if running on a CHRE API
690  *     v1.0 implementation. CHRE_HOST_ENDPOINT_BROADCAST isn't allowed to be
691  *     specified if anything other than CHRE_MESSAGE_PERMISSION_NONE is given
692  *     as messagePermissions since doing so would potentially attribute
693  *     permissions usage to host clients that don't intend to consume the data.
694  * @param messagePermissions Bitmasked CHRE_MESSAGE_PERMISSION_ values that will
695  *     be converted to corresponding Android-level permissions and attributed
696  *     the host endpoint upon consumption of the message.
697  * @param freeCallback  A pointer to a callback function.  After the lifetime
698  *     of 'message' is over (which does not assure that 'message' made it to
699  *     the host, just that the transport layer no longer needs this memory),
700  *     this callback will be invoked.  This argument is allowed
701  *     to be NULL, in which case no callback will be invoked.
702  * @return true if the message was accepted for transmission, false otherwise.
703  *     Note that even if this method returns 'false', the 'freeCallback' will
704  *     be invoked, if non-NULL.  In either case, the 'freeCallback' may be
705  *     invoked directly from within chreSendMessageToHostEndpoint(), so it's
706  *     necessary for nanoapp authors to avoid possible recursion with this.
707  *
708  * @see chreMessageFreeFunction
709  *
710  * @since v1.5
711  */
712 bool chreSendMessageWithPermissions(void *message, size_t messageSize,
713                                     uint32_t messageType, uint16_t hostEndpoint,
714                                     uint32_t messagePermissions,
715                                     chreMessageFreeFunction *freeCallback);
716 
717 /**
718  * Queries for information about a nanoapp running in the system.
719  *
720  * In the current API, appId is required to be unique, i.e. there cannot be two
721  * nanoapps running concurrently with the same appId.  If this restriction is
722  * removed in a future API version and multiple instances of the same appId are
723  * present, this function must always return the first app to start.
724  *
725  * @param appId Identifier for the nanoapp that the caller is requesting
726  *     information about.
727  * @param info Output parameter.  If this function returns true, this structure
728  *     will be populated with details of the specified nanoapp.
729  * @return true if a nanoapp with the given ID is currently running, and the
730  *     supplied info parameter was populated with its information.
731  *
732  * @since v1.1
733  */
734 bool chreGetNanoappInfoByAppId(uint64_t appId, struct chreNanoappInfo *info);
735 
736 /**
737  * Queries for information about a nanoapp running in the system, using the
738  * runtime unique identifier.  This method can be used to get information about
739  * the sender of an event.
740  *
741  * @param instanceId
742  * @param info Output parameter.  If this function returns true, this structure
743  *     will be populated with details of the specified nanoapp.
744  * @return true if a nanoapp with the given instance ID is currently running,
745  *     and the supplied info parameter was populated with its information.
746  *
747  * @since v1.1
748  */
749 bool chreGetNanoappInfoByInstanceId(uint32_t instanceId,
750                                     struct chreNanoappInfo *info);
751 
752 /**
753  * Configures whether this nanoapp will be notified when other nanoapps in the
754  * system start and stop, via CHRE_EVENT_NANOAPP_STARTED and
755  * CHRE_EVENT_NANOAPP_STOPPED.  These events are disabled by default, and if a
756  * nanoapp is not interested in interacting with other nanoapps, then it does
757  * not need to register for them.  However, if inter-nanoapp communication is
758  * desired, nanoapps are recommended to call this function from nanoappStart().
759  *
760  * If running on a CHRE platform that only supports v1.0 of the CHRE API, this
761  * function has no effect.
762  *
763  * @param enable true to enable these events, false to disable
764  *
765  * @see CHRE_EVENT_NANOAPP_STARTED
766  * @see CHRE_EVENT_NANOAPP_STOPPED
767  *
768  * @since v1.1
769  */
770 void chreConfigureNanoappInfoEvents(bool enable);
771 
772 /**
773  * Configures whether this nanoapp will be notified when the host (applications
774  * processor) transitions between wake and sleep, via CHRE_EVENT_HOST_AWAKE and
775  * CHRE_EVENT_HOST_ASLEEP.  As chreSendMessageToHostEndpoint() wakes the host if
776  * it is asleep, these events can be used to opportunistically send data to the
777  * host only when it wakes up for some other reason.  Note that this event is
778  * not instantaneous - there is an inherent delay in CHRE observing power state
779  * changes of the host processor, which may be significant depending on the
780  * implementation, especially in the wake to sleep direction.  Therefore,
781  * nanoapps are not guaranteed that messages sent to the host between AWAKE and
782  * ASLEEP events will not trigger a host wakeup.  However, implementations must
783  * ensure that the nominal wake-up notification latency is strictly less than
784  * the minimum wake-sleep time of the host processor.  Implementations are also
785  * encouraged to minimize this and related latencies where possible, to avoid
786  * unnecessary host wake-ups.
787  *
788  * These events are only sent on transitions, so the initial state will not be
789  * sent to the nanoapp as an event - use chreIsHostAwake().
790  *
791  * @param enable true to enable these events, false to disable
792  *
793  * @see CHRE_EVENT_HOST_AWAKE
794  * @see CHRE_EVENT_HOST_ASLEEP
795  *
796  * @since v1.2
797  */
798 void chreConfigureHostSleepStateEvents(bool enable);
799 
800 /**
801  * Retrieves the current sleep/wake state of the host (applications processor).
802  * Note that, as with the CHRE_EVENT_HOST_AWAKE and CHRE_EVENT_HOST_ASLEEP
803  * events, there is no guarantee that CHRE's view of the host processor's sleep
804  * state is instantaneous, and it may also change between querying the state and
805  * performing a host-waking action like sending a message to the host.
806  *
807  * @return true if by CHRE's own estimation the host is currently awake,
808  *     false otherwise
809  *
810  * @since v1.2
811  */
812 bool chreIsHostAwake(void);
813 
814 /**
815  * Configures whether this nanoapp will be notified when CHRE is collecting
816  * debug dumps, via CHRE_EVENT_DEBUG_DUMP. This event is disabled by default,
817  * and if a nanoapp is not interested in logging its debug data, then it does
818  * not need to register for it.
819  *
820  * @param enable true to enable receipt of this event, false to disable.
821  *
822  * @see CHRE_EVENT_DEBUG_DUMP
823  * @see chreDebugDumpLog
824  *
825  * @since v1.4
826  */
827 void chreConfigureDebugDumpEvent(bool enable);
828 
829 /**
830  * Configures whether this nanoapp will receive updates regarding a host
831  * endpoint that is connected with the Context Hub.
832  *
833  * If this API succeeds, the nanoapp will receive disconnection notifications,
834  * via the CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION event with type
835  * HOST_ENDPOINT_NOTIFICATION_TYPE_DISCONNECT, which can be invoked if the host
836  * has disconnected from the Context Hub either explicitly or implicitly (e.g.
837  * crashes). Nanoapps can use this notifications to clean up any resources
838  * associated with this host endpoint.
839  *
840  * @param hostEndpointId The host endpoint ID to configure notifications for.
841  * @param enable true to enable notifications.
842  *
843  * @return true on success
844  *
845  * @see chreMessageFromHostData
846  * @see chreHostEndpointNotification
847  * @see CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION
848  *
849  * @since v1.6
850  */
851 bool chreConfigureHostEndpointNotifications(uint16_t hostEndpointId,
852                                             bool enable);
853 
854 /**
855  * Publishes an RPC service from this nanoapp.
856  *
857  * When this API is invoked, the list of RPC services will be provided to
858  * host applications interacting with the nanoapp.
859  *
860  * This function must be invoked from nanoappStart(), to guarantee stable output
861  * of the list of RPC services supported by the nanoapp.
862  *
863  * @param services A non-null pointer to the list of RPC services to publish.
864  * @param numServices The number of services to publish, i.e. the length of the
865  *   services array.
866  *
867  * @return true if the publishing is successful.
868  *
869  * @since v1.6
870  */
871 bool chrePublishRpcServices(struct chreNanoappRpcService *services,
872                             size_t numServices);
873 
874 /**
875  * Retrieves metadata for a given host endpoint ID.
876  *
877  * This API will provide metadata regarding an endpoint associated with a
878  * host endpoint ID. The nanoapp should use this API to determine more
879  * information about a host endpoint that has sent a message to the nanoapp,
880  * after receiving a chreMessageFromHostData (which includes the endpoint ID).
881  *
882  * If the given host endpoint ID is not associated with a valid host (or if the
883  * client has disconnected from the Android or CHRE framework, i.e. no longer
884  * able to send messages to CHRE), this method will return false and info will
885  * not be populated.
886  *
887  * @param hostEndpointId The endpoint ID of the host to get info for.
888  * @param info The non-null pointer to where the metadata will be stored.
889  *
890  * @return true if info has been successfully populated.
891  *
892  * @since v1.6
893  */
894 bool chreGetHostEndpointInfo(uint16_t hostEndpointId,
895                              struct chreHostEndpointInfo *info);
896 
897 #ifdef __cplusplus
898 }
899 #endif
900 
901 #endif  /* _CHRE_EVENT_H_ */
902 
903