• 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 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * The CHRE implementation is required to provide the following
35  * preprocessor defines via the build system.
36  *
37  * CHRE_MESSAGE_TO_HOST_MAX_SIZE: The maximum size, in bytes, allowed for
38  *     a message sent to chreSendMessageToHost().  This must be at least
39  *     CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE.
40  */
41 
42 #ifndef CHRE_MESSAGE_TO_HOST_MAX_SIZE
43 #error CHRE_MESSAGE_TO_HOST_MAX_SIZE must be defined by the Context Hub Runtime Environment implementation
44 #endif
45 
46 /**
47  * The minimum size, in bytes, any CHRE implementation will
48  * use for CHRE_MESSAGE_TO_HOST_MAX_SIZE.
49  */
50 #define CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE 128
51 
52 #if CHRE_MESSAGE_TO_HOST_MAX_SIZE < CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE
53 #error CHRE_MESSAGE_TO_HOST_MAX_SIZE is too small.
54 #endif
55 
56 /**
57  * The lowest numerical value legal for a user-defined event.
58  *
59  * The system reserves all event values from 0 to 0x7FFF, inclusive.
60  * User events may use any value in the range 0x8000 to 0xFFFF, inclusive.
61  *
62  * Note that the same event values might be used by different nanoapps
63  * for different meanings.  This is not a concern, as these values only
64  * have meaning when paired with the originating nanoapp.
65  */
66 #define CHRE_EVENT_FIRST_USER_VALUE  UINT16_C(0x8000)
67 
68 /**
69  * nanoappHandleEvent argument: struct chreMessageFromHostData
70  *
71  * The format of the 'message' part of this structure is left undefined,
72  * and it's up to the nanoapp and host to have an established protocol
73  * beforehand.
74  */
75 #define CHRE_EVENT_MESSAGE_FROM_HOST  UINT16_C(0x0001)
76 
77 /**
78  * nanoappHandleEvent argument: 'cookie' given to chreTimerSet() method.
79  *
80  * Indicates that a timer has elapsed, in accordance with how chreTimerSet() was
81  * invoked.
82  */
83 #define CHRE_EVENT_TIMER  UINT16_C(0x0002)
84 
85 /**
86  * nanoappHandleEvent argument: struct chreNanoappInfo
87  *
88  * Indicates that a nanoapp has successfully started (its nanoappStart()
89  * function has been called, and it returned true) and is able to receive events
90  * sent via chreSendEvent().  Note that this event is not sent for nanoapps that
91  * were started prior to the current nanoapp - use chreGetNanoappInfo() to
92  * determine if another nanoapp is already running.
93  *
94  * @see chreConfigureNanoappInfoEvents
95  * @since v1.1
96  */
97 #define CHRE_EVENT_NANOAPP_STARTED  UINT16_C(0x0003)
98 
99 /**
100  * nanoappHandleEvent argument: struct chreNanoappInfo
101  *
102  * Indicates that a nanoapp has stopped executing and is no longer able to
103  * receive events sent via chreSendEvent().  Any events sent prior to receiving
104  * this event are not guaranteed to have been delivered.
105  *
106  * @see chreConfigureNanoappInfoEvents
107  * @since v1.1
108  */
109 #define CHRE_EVENT_NANOAPP_STOPPED  UINT16_C(0x0004)
110 
111 /**
112  * nanoappHandleEvent argument: NULL
113  *
114  * Indicates that CHRE has observed the host wake from low-power sleep state.
115  *
116  * @see chreConfigureHostSleepStateEvents
117  * @since v1.2
118  */
119 #define CHRE_EVENT_HOST_AWAKE  UINT16_C(0x0005)
120 
121 /**
122  * nanoappHandleEvent argument: NULL
123  *
124  * Indicates that CHRE has observed the host enter low-power sleep state.
125  *
126  * @see chreConfigureHostSleepStateEvents
127  * @since v1.2
128  */
129 #define CHRE_EVENT_HOST_ASLEEP  UINT16_C(0x0006)
130 
131 /**
132  * First possible value for CHRE_EVENT_SENSOR events.
133  *
134  * This allows us to separately define our CHRE_EVENT_SENSOR_* events in
135  * chre/sensor.h, without fear of collision with other event values.
136  */
137 #define CHRE_EVENT_SENSOR_FIRST_EVENT  UINT16_C(0x0100)
138 
139 /**
140  * Last possible value for CHRE_EVENT_SENSOR events.
141  *
142  * This allows us to separately define our CHRE_EVENT_SENSOR_* events in
143  * chre/sensor.h, without fear of collision with other event values.
144  */
145 #define CHRE_EVENT_SENSOR_LAST_EVENT  UINT16_C(0x02FF)
146 
147 /**
148  * First event in the block reserved for GNSS. These events are defined in
149  * chre/gnss.h.
150  */
151 #define CHRE_EVENT_GNSS_FIRST_EVENT  UINT16_C(0x0300)
152 #define CHRE_EVENT_GNSS_LAST_EVENT   UINT16_C(0x030F)
153 
154 /**
155  * First event in the block reserved for WiFi. These events are defined in
156  * chre/wifi.h.
157  */
158 #define CHRE_EVENT_WIFI_FIRST_EVENT  UINT16_C(0x0310)
159 #define CHRE_EVENT_WIFI_LAST_EVENT   UINT16_C(0x031F)
160 
161 /**
162  * First event in the block reserved for WWAN. These events are defined in
163  * chre/wwan.h.
164  */
165 #define CHRE_EVENT_WWAN_FIRST_EVENT  UINT16_C(0x0320)
166 #define CHRE_EVENT_WWAN_LAST_EVENT   UINT16_C(0x032F)
167 
168 /**
169  * First event in the block reserved for audio. These events are defined in
170  * chre/audio.h.
171  */
172 
173 #define CHRE_EVENT_AUDIO_FIRST_EVENT UINT16_C(0x0330)
174 #define CHRE_EVENT_AUDIO_LAST_EVENT  UINT16_C(0x033F)
175 
176 /**
177  * First in the extended range of values dedicated for internal CHRE
178  * implementation usage.
179  *
180  * This range is semantically the same as the internal event range defined
181  * below, but has been extended to allow for more implementation-specific events
182  * to be used.
183  *
184  * @since v1.1
185  */
186 #define CHRE_EVENT_INTERNAL_EXTENDED_FIRST_EVENT  UINT16_C(0x7000)
187 
188 /**
189  * First in a range of values dedicated for internal CHRE implementation usage.
190  *
191  * If a CHRE wishes to use events internally, any values within this range
192  * are assured not to be taken by future CHRE API additions.
193  */
194 #define CHRE_EVENT_INTERNAL_FIRST_EVENT  UINT16_C(0x7E00)
195 
196 /**
197  * Last in a range of values dedicated for internal CHRE implementation usage.
198  *
199  * If a CHRE wishes to use events internally, any values within this range
200  * are assured not to be taken by future CHRE API additions.
201  */
202 #define CHRE_EVENT_INTERNAL_LAST_EVENT  UINT16_C(0x7FFF)
203 
204 /**
205  * A special value for the hostEndpoint argument in
206  * chreSendMessageToHostEndpoint() that indicates that the message should be
207  * delivered to all host endpoints.  This value will not be used in the
208  * hostEndpoint field of struct chreMessageFromHostData supplied with
209  * CHRE_EVENT_MESSAGE_FROM_HOST.
210  *
211  * @since v1.1
212  */
213 #define CHRE_HOST_ENDPOINT_BROADCAST  UINT16_C(0xFFFF)
214 
215 /**
216  * A special value for hostEndpoint in struct chreMessageFromHostData that
217  * indicates that a host endpoint is unknown or otherwise unspecified.  This
218  * value may be received in CHRE_EVENT_MESSAGE_FROM_HOST, but it is not valid to
219  * provide it to chreSendMessageToHostEndpoint().
220  *
221  * @since v1.1
222  */
223 #define CHRE_HOST_ENDPOINT_UNSPECIFIED  UINT16_C(0xFFFE)
224 
225 
226 /**
227  * Data provided with CHRE_EVENT_MESSAGE_FROM_HOST.
228  */
229 struct chreMessageFromHostData {
230     /**
231      * Message type supplied by the host.
232      *
233      * @note In CHRE API v1.0, support for forwarding this field from the host
234      * was not strictly required, and some implementations did not support it.
235      * However, its support is mandatory as of v1.1.
236      */
237     union {
238         /**
239          * The preferred name to use when referencing this field.
240          *
241          * @since v1.1
242          */
243         uint32_t messageType;
244 
245         /**
246          * @deprecated This is the name for the messageType field used in v1.0.
247          * Left to allow code to compile against both v1.0 and v1.1 of the API
248          * definition without needing to use #ifdefs. This will be removed in a
249          * future API update - use messageType instead.
250          */
251         uint32_t reservedMessageType;
252     };
253 
254     /**
255      * The size, in bytes of the following 'message'.
256      *
257      * This can be 0.
258      */
259     uint32_t messageSize;
260 
261     /**
262      * The message from the host.
263      *
264      * These contents are of a format that the host and nanoapp must have
265      * established beforehand.
266      *
267      * This data is 'messageSize' bytes in length.  Note that if 'messageSize'
268      * is 0, this might be NULL.
269      */
270     const void *message;
271 
272     /**
273      * An identifier for the host-side entity that sent this message.  Unless
274      * this is set to CHRE_HOST_ENDPOINT_UNSPECIFIED, it can be used in
275      * chreSendMessageToHostEndpoint() to send a directed reply that will only
276      * be received by the given entity on the host.  Endpoint identifiers are
277      * opaque values assigned at runtime, so they cannot be assumed to always
278      * describe a specific entity across restarts.
279      *
280      * If running on a CHRE API v1.0 implementation, this field will always be
281      * set to CHRE_HOST_ENDPOINT_UNSPECIFIED.
282      *
283      * @since v1.1
284      */
285     uint16_t hostEndpoint;
286 };
287 
288 /**
289  * Provides metadata for a nanoapp in the system.
290  */
291 struct chreNanoappInfo {
292     /**
293      * Nanoapp identifier. The convention for populating this value is to set
294      * the most significant 5 bytes to a value that uniquely identifies the
295      * vendor, and the lower 3 bytes identify the nanoapp.
296      */
297     uint64_t appId;
298 
299     /**
300      * Nanoapp version.  The semantics of this field are defined by the nanoapp,
301      * however nanoapps are recommended to follow the same scheme used for the
302      * CHRE version exposed in chreGetVersion().  That is, the most significant
303      * byte represents the major version, the next byte the minor version, and
304      * the lower two bytes the patch version.
305      */
306     uint32_t version;
307 
308     /**
309      * The instance ID of this nanoapp, which can be used in chreSendEvent() to
310      * address an event specifically to this nanoapp.  This identifier is
311      * guaranteed to be unique among all nanoapps in the system.
312      */
313     uint32_t instanceId;
314 };
315 
316 /**
317  * Callback which frees data associated with an event.
318  *
319  * This callback is (optionally) provided to the chreSendEvent() method as
320  * a means for freeing the event data and performing any other cleanup
321  * necessary when the event is completed.  When this callback is invoked,
322  * 'eventData' is no longer needed and can be released.
323  *
324  * @param eventType  The 'eventType' argument from chreSendEvent().
325  * @param eventData  The 'eventData' argument from chreSendEvent().
326  *
327  * @see chreSendEvent
328  */
329 typedef void (chreEventCompleteFunction)(uint16_t eventType, void *eventData);
330 
331 /**
332  * Callback which frees a message.
333  *
334  * This callback is (optionally) provided to the chreSendMessageToHost() method
335  * as a means for freeing the message.  When this callback is invoked,
336  * 'message' is no longer needed and can be released.  Note that this in
337  * no way assures that said message did or did not make it to the host, simply
338  * that this memory is no longer needed.
339  *
340  * @param message  The 'message' argument from chreSendMessageToHost().
341  * @param messageSize  The 'messageSize' argument from chreSendMessageToHost().
342  *
343  * @see chreSendMessageToHost
344  */
345 typedef void (chreMessageFreeFunction)(void *message, size_t messageSize);
346 
347 
348 /**
349  * Enqueue an event to be sent to another nanoapp.
350  *
351  * @param eventType  This is a user-defined event type, of at least the
352  *     value CHRE_EVENT_FIRST_USER_VALUE.  It is illegal to attempt to use any
353  *     of the CHRE_EVENT_* values reserved for the CHRE.
354  * @param eventData  A pointer value that will be understood by the receiving
355  *     app.  Note that NULL is perfectly acceptable.  It also is not required
356  *     that this be a valid pointer, although if this nanoapp is intended to
357  *     work on arbitrary CHRE implementations, then the size of a
358  *     pointer cannot be assumed to be a certain size.  Note that the caller
359  *     no longer owns this memory after the call.
360  * @param freeCallback  A pointer to a callback function.  After the lifetime
361  *     of 'eventData' is over (either through successful delivery or the event
362  *     being dropped), this callback will be invoked.  This argument is allowed
363  *     to be NULL, in which case no callback will be invoked.
364  * @param targetInstanceId  The ID of the instance we're delivering this event
365  *     to.  Note that this is allowed to be our own instance.  The instance ID
366  *     of a nanoapp can be retrieved by using chreGetNanoappInfoByInstanceId().
367  * @returns true if the event was enqueued, false otherwise.  Note that even
368  *     if this method returns 'false', the 'freeCallback' will be invoked,
369  *     if non-NULL.  Note in the 'false' case, the 'freeCallback' may be
370  *     invoked directly from within chreSendEvent(), so it's necessary
371  *     for nanoapp authors to avoid possible recursion with this.
372  *
373  * @see chreEventDataFreeFunction
374  */
375 bool chreSendEvent(uint16_t eventType, void *eventData,
376                    chreEventCompleteFunction *freeCallback,
377                    uint32_t targetInstanceId);
378 
379 /**
380  * Send a message to the host, using the broadcast endpoint
381  * CHRE_HOST_ENDPOINT_BROADCAST.  Refer to chreSendMessageToHostEndpoint() for
382  * further details.
383  *
384  * @see chreSendMessageToHostEndpoint
385  *
386  * @deprecated New code should use chreSendMessageToHostEndpoint() instead of
387  * this function.  A future update to the API may cause references to this
388  * function to produce a compiler warning.
389  */
390 bool chreSendMessageToHost(void *message, uint32_t messageSize,
391                            uint32_t messageType,
392                            chreMessageFreeFunction *freeCallback);
393 
394 /**
395  * Send a message to the host, waking it up if it is currently asleep.
396  *
397  * This message is by definition arbitrarily defined.  Since we're not
398  * just a passing a pointer to memory around the system, but need to copy
399  * this into various buffers to send it to the host, the CHRE
400  * implementation cannot be asked to support an arbitrarily large message
401  * size.  As a result, we have the CHRE implementation define
402  * CHRE_MESSAGE_TO_HOST_MAX_SIZE.
403  *
404  * CHRE_MESSAGE_TO_HOST_MAX_SIZE is not given a value by the Platform API.  The
405  * Platform API does define CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, and requires
406  * that CHRE_MESSAGE_TO_HOST_MAX_SIZE is at least that value.
407  *
408  * As a result, if your message sizes are all less than
409  * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, then you have no concerns on any
410  * CHRE implementation.  If your message sizes are larger, you'll need to
411  * come up with a strategy for splitting your message across several calls
412  * to this method.  As long as that strategy works for
413  * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, it will work across all CHRE
414  * implementations (although on some implementations less calls to this
415  * method may be necessary).
416  *
417  * @param message  Pointer to a block of memory to send to the host.
418  *     NULL is acceptable only if messageSize is 0.  If non-NULL, this
419  *     must be a legitimate pointer (that is, unlike chreSendEvent(), a small
420  *     integral value cannot be cast to a pointer for this).  Note that the
421  *     caller no longer owns this memory after the call.
422  * @param messageSize  The size, in bytes, of the given message.
423  *     This cannot exceed CHRE_MESSAGE_TO_HOST_MAX_SIZE.
424  * @param messageType  Message type sent to the app on the host.
425  *     NOTE: In CHRE API v1.0, support for forwarding this field to the host was
426  *     not strictly required, and some implementations did not support it.
427  *     However, its support is mandatory as of v1.1.
428  * @param hostEndpoint  An identifier for the intended recipient of the message,
429  *     or CHRE_HOST_ENDPOINT_BROADCAST if all registered endpoints on the host
430  *     should receive the message.  Endpoint identifiers are assigned on the
431  *     host side, and nanoapps may learn of the host endpoint ID of an intended
432  *     recipient via an initial message sent by the host.  This parameter is
433  *     always treated as CHRE_HOST_ENDPOINT_BROADCAST if running on a CHRE API
434  *     v1.0 implementation.
435  * @param freeCallback  A pointer to a callback function.  After the lifetime
436  *     of 'message' is over (which does not assure that 'message' made it to
437  *     the host, just that the transport layer no longer needs this memory),
438  *     this callback will be invoked.  This argument is allowed
439  *     to be NULL, in which case no callback will be invoked.
440  * @returns true if the message was accepted for transmission, false otherwise.
441  *     Note that even if this method returns 'false', the 'freeCallback' will
442  *     be invoked, if non-NULL.  In either case, the 'freeCallback' may be
443  *     invoked directly from within chreSendMessageToHost(), so it's necessary
444  *     for nanoapp authors to avoid possible recursion with this.
445  *
446  * @see chreMessageFreeFunction
447  *
448  * @since v1.1
449  */
450 bool chreSendMessageToHostEndpoint(void *message, size_t messageSize,
451                                    uint32_t messageType, uint16_t hostEndpoint,
452                                    chreMessageFreeFunction *freeCallback);
453 
454 /**
455  * Queries for information about a nanoapp running in the system.
456  *
457  * In the current API, appId is required to be unique, i.e. there cannot be two
458  * nanoapps running concurrently with the same appId.  If this restriction is
459  * removed in a future API version and multiple instances of the same appId are
460  * present, this function must always return the first app to start.
461  *
462  * @param appId Identifier for the nanoapp that the caller is requesting
463  *     information about.
464  * @param info Output parameter.  If this function returns true, this structure
465  *     will be populated with details of the specified nanoapp.
466  * @returns true if a nanoapp with the given ID is currently running, and the
467  *     supplied info parameter was populated with its information.
468  *
469  * @since v1.1
470  */
471 bool chreGetNanoappInfoByAppId(uint64_t appId, struct chreNanoappInfo *info);
472 
473 /**
474  * Queries for information about a nanoapp running in the system, using the
475  * runtime unique identifier.  This method can be used to get information about
476  * the sender of an event.
477  *
478  * @param instanceId
479  * @param info Output parameter.  If this function returns true, this structure
480  *     will be populated with details of the specified nanoapp.
481  * @returns true if a nanoapp with the given instance ID is currently running,
482  *     and the supplied info parameter was populated with its information.
483  *
484  * @since v1.1
485  */
486 bool chreGetNanoappInfoByInstanceId(uint32_t instanceId,
487                                     struct chreNanoappInfo *info);
488 
489 /**
490  * Configures whether this nanoapp will be notified when other nanoapps in the
491  * system start and stop, via CHRE_EVENT_NANOAPP_STARTED and
492  * CHRE_EVENT_NANOAPP_STOPPED.  These events are disabled by default, and if a
493  * nanoapp is not interested in interacting with other nanoapps, then it does
494  * not need to register for them.  However, if inter-nanoapp communication is
495  * desired, nanoapps are recommended to call this function from nanoappStart().
496  *
497  * If running on a CHRE platform that only supports v1.0 of the CHRE API, this
498  * function has no effect.
499  *
500  * @param enable true to enable these events, false to disable
501  *
502  * @see CHRE_EVENT_NANOAPP_STARTED
503  * @see CHRE_EVENT_NANOAPP_STOPPED
504  *
505  * @since v1.1
506  */
507 void chreConfigureNanoappInfoEvents(bool enable);
508 
509 /**
510  * Configures whether this nanoapp will be notified when the host (applications
511  * processor) transitions between wake and sleep, via CHRE_EVENT_HOST_AWAKE and
512  * CHRE_EVENT_HOST_ASLEEP.  As chreSendMessageToHostEndpoint() wakes the host if
513  * it is asleep, these events can be used to opportunistically send data to the
514  * host only when it wakes up for some other reason.  Note that this event is
515  * not instantaneous - there is an inherent delay in CHRE observing power state
516  * changes of the host processor, which may be significant depending on the
517  * implementation, especially in the wake to sleep direction.  Therefore,
518  * nanoapps are not guaranteed that messages sent to the host between AWAKE and
519  * ASLEEP events will not trigger a host wakeup.  However, implementations must
520  * ensure that the nominal wake-up notification latency is strictly less than
521  * the minimum wake-sleep time of the host processor.  Implementations are also
522  * encouraged to minimize this and related latencies where possible, to avoid
523  * unnecessary host wake-ups.
524  *
525  * These events are only sent on transitions, so the initial state will not be
526  * sent to the nanoapp as an event - use chreIsHostAwake().
527  *
528  * @param enable true to enable these events, false to disable
529  *
530  * @see CHRE_EVENT_HOST_AWAKE
531  * @see CHRE_EVENT_HOST_ASLEEP
532  *
533  * @since v1.2
534  */
535 void chreConfigureHostSleepStateEvents(bool enable);
536 
537 /**
538  * Retrieves the current sleep/wake state of the host (applications processor).
539  * Note that, as with the CHRE_EVENT_HOST_AWAKE and CHRE_EVENT_HOST_ASLEEP
540  * events, there is no guarantee that CHRE's view of the host processor's sleep
541  * state is instantaneous, and it may also change between querying the state and
542  * performing a host-waking action like sending a message to the host.
543  *
544  * @returns true if by CHRE's own estimation the host is currently awake,
545  *     false otherwise
546  *
547  * @since v1.2
548  */
549 bool chreIsHostAwake(void);
550 
551 #ifdef __cplusplus
552 }
553 #endif
554 
555 #endif  /* _CHRE_EVENT_H_ */
556 
557