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 * @see chrePublishRpcServices 313 * 314 * @since v1.8 315 */ 316 #define CHRE_MINIMUM_RPC_SERVICE_LIMIT UINT8_C(4) 317 318 /** 319 * Data provided with CHRE_EVENT_MESSAGE_FROM_HOST. 320 */ 321 struct chreMessageFromHostData { 322 /** 323 * Message type supplied by the host. 324 * 325 * @note In CHRE API v1.0, support for forwarding this field from the host 326 * was not strictly required, and some implementations did not support it. 327 * However, its support is mandatory as of v1.1. 328 */ 329 union { 330 /** 331 * The preferred name to use when referencing this field. 332 * 333 * @since v1.1 334 */ 335 uint32_t messageType; 336 337 /** 338 * @deprecated This is the name for the messageType field used in v1.0. 339 * Left to allow code to compile against both v1.0 and v1.1 of the API 340 * definition without needing to use #ifdefs. This will be removed in a 341 * future API update - use messageType instead. 342 */ 343 uint32_t reservedMessageType; 344 }; 345 346 /** 347 * The size, in bytes of the following 'message'. 348 * 349 * This can be 0. 350 */ 351 uint32_t messageSize; 352 353 /** 354 * The message from the host. 355 * 356 * These contents are of a format that the host and nanoapp must have 357 * established beforehand. 358 * 359 * This data is 'messageSize' bytes in length. Note that if 'messageSize' 360 * is 0, this might be NULL. 361 */ 362 const void *message; 363 364 /** 365 * An identifier for the host-side entity that sent this message. Unless 366 * this is set to CHRE_HOST_ENDPOINT_UNSPECIFIED, it can be used in 367 * chreSendMessageToHostEndpoint() to send a directed reply that will only 368 * be received by the given entity on the host. Endpoint identifiers are 369 * opaque values assigned at runtime, so they cannot be assumed to always 370 * describe a specific entity across restarts. 371 * 372 * If running on a CHRE API v1.0 implementation, this field will always be 373 * set to CHRE_HOST_ENDPOINT_UNSPECIFIED. 374 * 375 * @since v1.1 376 */ 377 uint16_t hostEndpoint; 378 }; 379 380 /** 381 * Provides metadata for a nanoapp in the system. 382 */ 383 struct chreNanoappInfo { 384 /** 385 * Nanoapp identifier. The convention for populating this value is to set 386 * the most significant 5 bytes to a value that uniquely identifies the 387 * vendor, and the lower 3 bytes identify the nanoapp. 388 */ 389 uint64_t appId; 390 391 /** 392 * Nanoapp version. The semantics of this field are defined by the nanoapp, 393 * however nanoapps are recommended to follow the same scheme used for the 394 * CHRE version exposed in chreGetVersion(). That is, the most significant 395 * byte represents the major version, the next byte the minor version, and 396 * the lower two bytes the patch version. 397 */ 398 uint32_t version; 399 400 /** 401 * The instance ID of this nanoapp, which can be used in chreSendEvent() to 402 * address an event specifically to this nanoapp. This identifier is 403 * guaranteed to be unique among all nanoapps in the system. 404 * 405 * As of CHRE API v1.6, instance ID is guaranteed to never be greater than 406 * UINT16_MAX. This allows for the instance ID be packed with other data 407 * inside a 32-bit integer (useful for RPC routing). 408 */ 409 uint32_t instanceId; 410 411 /** 412 * Reserved for future use. 413 * Always set to 0. 414 */ 415 uint8_t reserved[3]; 416 417 /** 418 * The number of RPC services exposed by this nanoapp. 419 * The service details are available in the rpcServices array. 420 * Must always be set to 0 when running on a CHRE implementation prior to 421 * v1.8 422 * 423 * @since v1.8 424 */ 425 uint8_t rpcServiceCount; 426 427 /* 428 * Array of RPC services published by this nanoapp. 429 * Services are published via chrePublishRpcServices. 430 * The array contains rpcServiceCount entries. 431 * 432 * The pointer is only valid when rpcServiceCount is greater than 0. 433 * 434 * @since v1.8 435 */ 436 const struct chreNanoappRpcService *rpcServices; 437 }; 438 439 /** 440 * The types of notification events that can be included in struct 441 * chreHostEndpointNotification. 442 * 443 * @defgroup HOST_ENDPOINT_NOTIFICATION_TYPE 444 * @{ 445 */ 446 #define HOST_ENDPOINT_NOTIFICATION_TYPE_DISCONNECT UINT8_C(0) 447 /** @} */ 448 449 /** 450 * Data provided in CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION. 451 */ 452 struct chreHostEndpointNotification { 453 /** 454 * The ID of the host endpoint that this notification is for. 455 */ 456 uint16_t hostEndpointId; 457 458 /** 459 * The type of notification this event represents, which should be 460 * one of the HOST_ENDPOINT_NOTIFICATION_TYPE_* values. 461 */ 462 uint8_t notificationType; 463 464 /** 465 * Reserved for future use, must be zero. 466 */ 467 uint8_t reserved; 468 }; 469 470 //! The maximum length of a host endpoint's name. 471 #define CHRE_MAX_ENDPOINT_NAME_LEN (51) 472 473 //! The maximum length of a host endpoint's tag. 474 #define CHRE_MAX_ENDPOINT_TAG_LEN (51) 475 476 /** 477 * The type of host endpoint that can be used in the hostEndpointType field 478 * of chreHostEndpointInfo. 479 * 480 * @since v1.6 481 * 482 * @defgroup CHRE_HOST_ENDPOINT_TYPE_ 483 * @{ 484 */ 485 486 //! The host endpoint is part of the Android system framework. 487 #define CHRE_HOST_ENDPOINT_TYPE_FRAMEWORK UINT8_C(0) 488 489 //! The host endpoint is an Android app. 490 #define CHRE_HOST_ENDPOINT_TYPE_APP UINT8_C(1) 491 492 //! The host endpoint is an Android native program. 493 #define CHRE_HOST_ENDPOINT_TYPE_NATIVE UINT8_C(2) 494 495 //! Values in the range [CHRE_HOST_ENDPOINT_TYPE_VENDOR_START, 496 //! CHRE_HOST_ENDPOINT_TYPE_VENDOR_END] can be a custom defined host endpoint 497 //! type for platform-specific vendor use. 498 #define CHRE_HOST_ENDPOINT_TYPE_VENDOR_START UINT8_C(128) 499 #define CHRE_HOST_ENDPOINT_TYPE_VENDOR_END UINT8_C(255) 500 501 /** @} */ 502 503 /** 504 * Provides metadata for a host endpoint. 505 * 506 * @since v1.6 507 */ 508 struct chreHostEndpointInfo { 509 //! The endpoint ID of this host. 510 uint16_t hostEndpointId; 511 512 //! The type of host endpoint, which must be set to one of the 513 //! CHRE_HOST_ENDPOINT_TYPE_* values or a value in the vendor-reserved 514 //! range. 515 uint8_t hostEndpointType; 516 517 //! Flag indicating if the packageName/endpointName field is valid. 518 uint8_t isNameValid : 1; 519 520 //! Flag indicating if the attributionTag/endpointTag field is valid. 521 uint8_t isTagValid : 1; 522 523 //! A union of null-terminated host name strings. 524 union { 525 //! The Android package name associated with this host, valid if the 526 //! hostEndpointType is CHRE_HOST_ENDPOINT_TYPE_APP or 527 //! CHRE_HOST_ENDPOINT_TYPE_FRAMEWORK. Refer to the Android documentation 528 //! for the package attribute in the app manifest. 529 char packageName[CHRE_MAX_ENDPOINT_NAME_LEN]; 530 531 //! A generic endpoint name that can be used for endpoints that 532 //! may not have a package name. 533 char endpointName[CHRE_MAX_ENDPOINT_NAME_LEN]; 534 }; 535 536 //! A union of null-terminated host tag strings for further identification. 537 union { 538 //! The attribution tag associated with this host that is used to audit 539 //! access to data, which can be valid if the hostEndpointType is 540 //! CHRE_HOST_ENDPOINT_TYPE_APP. Refer to the Android documentation 541 //! regarding data audit using attribution tags. 542 char attributionTag[CHRE_MAX_ENDPOINT_TAG_LEN]; 543 544 //! A generic endpoint tag that can be used for endpoints that 545 //! may not have an attribution tag. 546 char endpointTag[CHRE_MAX_ENDPOINT_TAG_LEN]; 547 }; 548 }; 549 550 /** 551 * An RPC service exposed by a nanoapp. 552 * 553 * The implementation of the RPC interface is not defined by the HAL, and is written 554 * at the messaging endpoint layers (Android app and/or CHRE nanoapp). NanoappRpcService 555 * contains the informational metadata to be consumed by the RPC interface layer. 556 */ 557 struct chreNanoappRpcService { 558 /** 559 * The unique 64-bit ID of an RPC service exposed by a nanoapp. Note that 560 * the uniqueness is only required within the nanoapp's domain (i.e. the 561 * combination of the nanoapp ID and service id must be unique). 562 */ 563 uint64_t id; 564 565 /** 566 * The software version of this service, which follows the sematic 567 * versioning scheme (see semver.org). It follows the format 568 * major.minor.patch, where major and minor versions take up one byte 569 * each, and the patch version takes up the final 2 bytes. 570 */ 571 uint32_t version; 572 }; 573 574 /** 575 * Callback which frees data associated with an event. 576 * 577 * This callback is (optionally) provided to the chreSendEvent() method as 578 * a means for freeing the event data and performing any other cleanup 579 * necessary when the event is completed. When this callback is invoked, 580 * 'eventData' is no longer needed and can be released. 581 * 582 * @param eventType The 'eventType' argument from chreSendEvent(). 583 * @param eventData The 'eventData' argument from chreSendEvent(). 584 * 585 * @see chreSendEvent 586 */ 587 typedef void (chreEventCompleteFunction)(uint16_t eventType, void *eventData); 588 589 /** 590 * Callback which frees a message. 591 * 592 * This callback is (optionally) provided to the chreSendMessageToHostEndpoint() 593 * method as a means for freeing the message. When this callback is invoked, 594 * 'message' is no longer needed and can be released. Note that this in 595 * no way assures that said message did or did not make it to the host, simply 596 * that this memory is no longer needed. 597 * 598 * @param message The 'message' argument from chreSendMessageToHostEndpoint(). 599 * @param messageSize The 'messageSize' argument from 600 * chreSendMessageToHostEndpoint(). 601 * 602 * @see chreSendMessageToHostEndpoint 603 */ 604 typedef void (chreMessageFreeFunction)(void *message, size_t messageSize); 605 606 607 /** 608 * Enqueue an event to be sent to another nanoapp. 609 * 610 * @param eventType This is a user-defined event type, of at least the 611 * value CHRE_EVENT_FIRST_USER_VALUE. It is illegal to attempt to use any 612 * of the CHRE_EVENT_* values reserved for the CHRE. 613 * @param eventData A pointer value that will be understood by the receiving 614 * app. Note that NULL is perfectly acceptable. It also is not required 615 * that this be a valid pointer, although if this nanoapp is intended to 616 * work on arbitrary CHRE implementations, then the size of a 617 * pointer cannot be assumed to be a certain size. Note that the caller 618 * no longer owns this memory after the call. 619 * @param freeCallback A pointer to a callback function. After the lifetime 620 * of 'eventData' is over (either through successful delivery or the event 621 * being dropped), this callback will be invoked. This argument is allowed 622 * to be NULL, in which case no callback will be invoked. 623 * @param targetInstanceId The ID of the instance we're delivering this event 624 * to. Note that this is allowed to be our own instance. The instance ID 625 * of a nanoapp can be retrieved by using chreGetNanoappInfoByInstanceId(). 626 * @return true if the event was enqueued, false otherwise. Note that even 627 * if this method returns 'false', the 'freeCallback' will be invoked, 628 * if non-NULL. Note in the 'false' case, the 'freeCallback' may be 629 * invoked directly from within chreSendEvent(), so it's necessary 630 * for nanoapp authors to avoid possible recursion with this. 631 * 632 * @see chreEventDataFreeFunction 633 */ 634 bool chreSendEvent(uint16_t eventType, void *eventData, 635 chreEventCompleteFunction *freeCallback, 636 uint32_t targetInstanceId); 637 638 /** 639 * Send a message to the host, using the broadcast endpoint 640 * CHRE_HOST_ENDPOINT_BROADCAST. Refer to chreSendMessageToHostEndpoint() for 641 * further details. 642 * 643 * @see chreSendMessageToHostEndpoint 644 * 645 * @deprecated New code should use chreSendMessageToHostEndpoint() instead of 646 * this function. A future update to the API may cause references to this 647 * function to produce a compiler warning. 648 */ 649 bool chreSendMessageToHost(void *message, uint32_t messageSize, 650 uint32_t messageType, 651 chreMessageFreeFunction *freeCallback) 652 CHRE_DEPRECATED("Use chreSendMessageToHostEndpoint instead"); 653 654 /** 655 * Send a message to the host, using CHRE_MESSAGE_PERMISSION_NONE for the 656 * associated message permissions. This method must only be used if no data 657 * provided by CHRE's audio, GNSS, WiFi, and WWAN APIs was used to produce the 658 * contents of the message being sent. Refer to chreSendMessageWithPermissions() 659 * for further details. 660 * 661 * @see chreSendMessageWithPermissions 662 * 663 * @since v1.1 664 */ 665 bool chreSendMessageToHostEndpoint(void *message, size_t messageSize, 666 uint32_t messageType, uint16_t hostEndpoint, 667 chreMessageFreeFunction *freeCallback); 668 669 /** 670 * Send a message to the host, waking it up if it is currently asleep. 671 * 672 * This message is by definition arbitrarily defined. Since we're not 673 * just a passing a pointer to memory around the system, but need to copy 674 * this into various buffers to send it to the host, the CHRE 675 * implementation cannot be asked to support an arbitrarily large message 676 * size. As a result, we have the CHRE implementation define 677 * CHRE_MESSAGE_TO_HOST_MAX_SIZE. 678 * 679 * CHRE_MESSAGE_TO_HOST_MAX_SIZE is not given a value by the Platform API. The 680 * Platform API does define CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, and requires 681 * that CHRE_MESSAGE_TO_HOST_MAX_SIZE is at least that value. 682 * 683 * As a result, if your message sizes are all less than 684 * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, then you have no concerns on any 685 * CHRE implementation. If your message sizes are larger, you'll need to 686 * come up with a strategy for splitting your message across several calls 687 * to this method. As long as that strategy works for 688 * CHRE_MESSAGE_TO_HOST_MINIMUM_MAX_SIZE, it will work across all CHRE 689 * implementations (although on some implementations less calls to this 690 * method may be necessary). 691 * 692 * When sending a message to the host, the ContextHub service will enforce 693 * the host client has been granted Android-level permissions corresponding to 694 * the ones the nanoapp declares it uses through CHRE_NANOAPP_USES_AUDIO, etc. 695 * In addition to this, the permissions bitmask provided as input to this method 696 * results in the Android framework using app-ops to verify and log access upon 697 * message delivery to an application. This is primarily useful for ensuring 698 * accurate attribution for messages generated using permission-controlled data. 699 * The bitmask declared by the nanoapp for this message must be a 700 * subset of the permissions it declared it would use at build time or the 701 * message will be rejected. 702 * 703 * Nanoapps must use this method if the data they are sending contains or was 704 * derived from any data sampled through CHRE's audio, GNSS, WiFi, or WWAN APIs. 705 * Additionally, if vendors add APIs to expose data that would be guarded by a 706 * permission in Android, vendors must support declaring a message permission 707 * through this method. 708 * 709 * @param message Pointer to a block of memory to send to the host. 710 * NULL is acceptable only if messageSize is 0. If non-NULL, this 711 * must be a legitimate pointer (that is, unlike chreSendEvent(), a small 712 * integral value cannot be cast to a pointer for this). Note that the 713 * caller no longer owns this memory after the call. 714 * @param messageSize The size, in bytes, of the given message. If this exceeds 715 * CHRE_MESSAGE_TO_HOST_MAX_SIZE, the message will be rejected. 716 * @param messageType Message type sent to the app on the host. 717 * NOTE: In CHRE API v1.0, support for forwarding this field to the host was 718 * not strictly required, and some implementations did not support it. 719 * However, its support is mandatory as of v1.1. 720 * @param hostEndpoint An identifier for the intended recipient of the message, 721 * or CHRE_HOST_ENDPOINT_BROADCAST if all registered endpoints on the host 722 * should receive the message. Endpoint identifiers are assigned on the 723 * host side, and nanoapps may learn of the host endpoint ID of an intended 724 * recipient via an initial message sent by the host. This parameter is 725 * always treated as CHRE_HOST_ENDPOINT_BROADCAST if running on a CHRE API 726 * v1.0 implementation. CHRE_HOST_ENDPOINT_BROADCAST isn't allowed to be 727 * specified if anything other than CHRE_MESSAGE_PERMISSION_NONE is given 728 * as messagePermissions since doing so would potentially attribute 729 * permissions usage to host clients that don't intend to consume the data. 730 * @param messagePermissions Bitmasked CHRE_MESSAGE_PERMISSION_ values that will 731 * be converted to corresponding Android-level permissions and attributed 732 * the host endpoint upon consumption of the message. 733 * @param freeCallback A pointer to a callback function. After the lifetime 734 * of 'message' is over (which does not assure that 'message' made it to 735 * the host, just that the transport layer no longer needs this memory), 736 * this callback will be invoked. This argument is allowed 737 * to be NULL, in which case no callback will be invoked. 738 * @return true if the message was accepted for transmission, false otherwise. 739 * Note that even if this method returns 'false', the 'freeCallback' will 740 * be invoked, if non-NULL. In either case, the 'freeCallback' may be 741 * invoked directly from within chreSendMessageToHostEndpoint(), so it's 742 * necessary for nanoapp authors to avoid possible recursion with this. 743 * 744 * @see chreMessageFreeFunction 745 * 746 * @since v1.5 747 */ 748 bool chreSendMessageWithPermissions(void *message, size_t messageSize, 749 uint32_t messageType, uint16_t hostEndpoint, 750 uint32_t messagePermissions, 751 chreMessageFreeFunction *freeCallback); 752 753 /** 754 * Queries for information about a nanoapp running in the system. 755 * 756 * In the current API, appId is required to be unique, i.e. there cannot be two 757 * nanoapps running concurrently with the same appId. If this restriction is 758 * removed in a future API version and multiple instances of the same appId are 759 * present, this function must always return the first app to start. 760 * 761 * @param appId Identifier for the nanoapp that the caller is requesting 762 * information about. 763 * @param info Output parameter. If this function returns true, this structure 764 * will be populated with details of the specified nanoapp. 765 * @return true if a nanoapp with the given ID is currently running, and the 766 * supplied info parameter was populated with its information. 767 * 768 * @since v1.1 769 */ 770 bool chreGetNanoappInfoByAppId(uint64_t appId, struct chreNanoappInfo *info); 771 772 /** 773 * Queries for information about a nanoapp running in the system, using the 774 * runtime unique identifier. This method can be used to get information about 775 * the sender of an event. 776 * 777 * @param instanceId 778 * @param info Output parameter. If this function returns true, this structure 779 * will be populated with details of the specified nanoapp. 780 * @return true if a nanoapp with the given instance ID is currently running, 781 * and the supplied info parameter was populated with its information. 782 * 783 * @since v1.1 784 */ 785 bool chreGetNanoappInfoByInstanceId(uint32_t instanceId, 786 struct chreNanoappInfo *info); 787 788 /** 789 * Configures whether this nanoapp will be notified when other nanoapps in the 790 * system start and stop, via CHRE_EVENT_NANOAPP_STARTED and 791 * CHRE_EVENT_NANOAPP_STOPPED. These events are disabled by default, and if a 792 * nanoapp is not interested in interacting with other nanoapps, then it does 793 * not need to register for them. However, if inter-nanoapp communication is 794 * desired, nanoapps are recommended to call this function from nanoappStart(). 795 * 796 * If running on a CHRE platform that only supports v1.0 of the CHRE API, this 797 * function has no effect. 798 * 799 * @param enable true to enable these events, false to disable 800 * 801 * @see CHRE_EVENT_NANOAPP_STARTED 802 * @see CHRE_EVENT_NANOAPP_STOPPED 803 * 804 * @since v1.1 805 */ 806 void chreConfigureNanoappInfoEvents(bool enable); 807 808 /** 809 * Configures whether this nanoapp will be notified when the host (applications 810 * processor) transitions between wake and sleep, via CHRE_EVENT_HOST_AWAKE and 811 * CHRE_EVENT_HOST_ASLEEP. As chreSendMessageToHostEndpoint() wakes the host if 812 * it is asleep, these events can be used to opportunistically send data to the 813 * host only when it wakes up for some other reason. Note that this event is 814 * not instantaneous - there is an inherent delay in CHRE observing power state 815 * changes of the host processor, which may be significant depending on the 816 * implementation, especially in the wake to sleep direction. Therefore, 817 * nanoapps are not guaranteed that messages sent to the host between AWAKE and 818 * ASLEEP events will not trigger a host wakeup. However, implementations must 819 * ensure that the nominal wake-up notification latency is strictly less than 820 * the minimum wake-sleep time of the host processor. Implementations are also 821 * encouraged to minimize this and related latencies where possible, to avoid 822 * unnecessary host wake-ups. 823 * 824 * These events are only sent on transitions, so the initial state will not be 825 * sent to the nanoapp as an event - use chreIsHostAwake(). 826 * 827 * @param enable true to enable these events, false to disable 828 * 829 * @see CHRE_EVENT_HOST_AWAKE 830 * @see CHRE_EVENT_HOST_ASLEEP 831 * 832 * @since v1.2 833 */ 834 void chreConfigureHostSleepStateEvents(bool enable); 835 836 /** 837 * Retrieves the current sleep/wake state of the host (applications processor). 838 * Note that, as with the CHRE_EVENT_HOST_AWAKE and CHRE_EVENT_HOST_ASLEEP 839 * events, there is no guarantee that CHRE's view of the host processor's sleep 840 * state is instantaneous, and it may also change between querying the state and 841 * performing a host-waking action like sending a message to the host. 842 * 843 * @return true if by CHRE's own estimation the host is currently awake, 844 * false otherwise 845 * 846 * @since v1.2 847 */ 848 bool chreIsHostAwake(void); 849 850 /** 851 * Configures whether this nanoapp will be notified when CHRE is collecting 852 * debug dumps, via CHRE_EVENT_DEBUG_DUMP. This event is disabled by default, 853 * and if a nanoapp is not interested in logging its debug data, then it does 854 * not need to register for it. 855 * 856 * @param enable true to enable receipt of this event, false to disable. 857 * 858 * @see CHRE_EVENT_DEBUG_DUMP 859 * @see chreDebugDumpLog 860 * 861 * @since v1.4 862 */ 863 void chreConfigureDebugDumpEvent(bool enable); 864 865 /** 866 * Configures whether this nanoapp will receive updates regarding a host 867 * endpoint that is connected with the Context Hub. 868 * 869 * If this API succeeds, the nanoapp will receive disconnection notifications, 870 * via the CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION event with an eventData of type 871 * chreHostEndpointNotification with its notificationType set to 872 * HOST_ENDPOINT_NOTIFICATION_TYPE_DISCONNECT, which can be invoked if the host 873 * has disconnected from the Context Hub either explicitly or implicitly (e.g. 874 * crashes). Nanoapps can use this notifications to clean up any resources 875 * associated with this host endpoint. 876 * 877 * @param hostEndpointId The host endpoint ID to configure notifications for. 878 * @param enable true to enable notifications. 879 * 880 * @return true on success 881 * 882 * @see chreMessageFromHostData 883 * @see chreHostEndpointNotification 884 * @see CHRE_EVENT_HOST_ENDPOINT_NOTIFICATION 885 * 886 * @since v1.6 887 */ 888 bool chreConfigureHostEndpointNotifications(uint16_t hostEndpointId, 889 bool enable); 890 891 /** 892 * Publishes RPC services from this nanoapp. 893 * 894 * When this API is invoked, the list of RPC services will be provided to 895 * host applications interacting with the nanoapp. 896 * 897 * This function must be invoked from nanoappStart(), to guarantee stable output 898 * of the list of RPC services supported by the nanoapp. 899 * 900 * Although nanoapps are recommended to only call this API once with all 901 * services it intends to publish, if it is called multiple times, each 902 * call will append to the list of published services. 903 * 904 * Starting in CHRE API v1.8, the implementation must allow for a nanoapp to 905 * publish at least CHRE_MINIMUM_RPC_SERVICE_LIMIT services and at most 906 * UINT8_MAX services. If calling this function would result in exceeding 907 * the limit, the services must not be published and it must return false. 908 * 909 * @param services A non-null pointer to the list of RPC services to publish. 910 * @param numServices The number of services to publish, i.e. the length of the 911 * services array. 912 * 913 * @return true if the publishing is successful. 914 * 915 * @since v1.6 916 */ 917 bool chrePublishRpcServices(struct chreNanoappRpcService *services, 918 size_t numServices); 919 920 /** 921 * Retrieves metadata for a given host endpoint ID. 922 * 923 * This API will provide metadata regarding an endpoint associated with a 924 * host endpoint ID. The nanoapp should use this API to determine more 925 * information about a host endpoint that has sent a message to the nanoapp, 926 * after receiving a chreMessageFromHostData (which includes the endpoint ID). 927 * 928 * If the given host endpoint ID is not associated with a valid host (or if the 929 * client has disconnected from the Android or CHRE framework, i.e. no longer 930 * able to send messages to CHRE), this method will return false and info will 931 * not be populated. 932 * 933 * @param hostEndpointId The endpoint ID of the host to get info for. 934 * @param info The non-null pointer to where the metadata will be stored. 935 * 936 * @return true if info has been successfully populated. 937 * 938 * @since v1.6 939 */ 940 bool chreGetHostEndpointInfo(uint16_t hostEndpointId, 941 struct chreHostEndpointInfo *info); 942 943 #ifdef __cplusplus 944 } 945 #endif 946 947 #endif /* _CHRE_EVENT_H_ */ 948 949