1 /* 2 * Copyright (C) 2017 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_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_ 18 #define CHRE_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_ 19 20 #include <stdint.h> 21 22 #include "chre/util/system/napp_permissions.h" 23 #include "flatbuffers/flatbuffers.h" 24 25 namespace chre { 26 27 namespace fbs { 28 29 // Forward declaration of the ChreMessage enum defined in the generated 30 // FlatBuffers header file 31 enum class ChreMessage : uint8_t; 32 33 } // namespace fbs 34 35 //! On a message sent from CHRE, specifies that the host daemon should determine 36 //! which client to send the message to. Usually, this is all clients, but for a 37 //! message from a nanoapp, the host daemon can use the endpoint ID to determine 38 //! the destination client ID. 39 constexpr uint16_t kHostClientIdUnspecified = 0; 40 41 /** 42 * Functions that are shared between the CHRE and host to assist with 43 * communications between the two. Note that normally these functions are 44 * accessed through a derived class like chre::HostProtocolChre (CHRE-side) or 45 * android::chre:HostProtocolHost (host-side). 46 */ 47 class HostProtocolCommon { 48 public: 49 /** 50 * Encodes a message between a nanoapp and a host (in both directions) using 51 * the given FlatBufferBuilder and supplied parameters. 52 * Note that messagePermissions is only applicable to messages from a nanoapp 53 * to the host. 54 * 55 * @param builder A newly constructed FlatBufferBuilder that will be used to 56 * encode the message. It will be finalized before returning from this 57 * function. 58 * @param appId Nanoapp ID. 59 * @param messageType Type of message that was constructed. 60 * @param hostEndpoint The host endpoint the data was sent from or that should 61 * receive this message. 62 * @param messageData Pointer to message payload. Can be null if 63 * messageDataLen is zero. 64 * @param messageDataLen Size of messageData, in bytes. 65 * @param permissions List of Android permissions declared by the nanoapp or 66 * granted to the host. For from the nanoapp to the host messages, this 67 * must be a superset of messagePermissions. 68 * @param messagePermissions Used only for messages from the nanoapp to the 69 * host. Lists the Android permissions covering the contents of the 70 * message. These permissions are used to record and attribute access 71 * to permissions-controlled resources. 72 * @param wokeHost true if this message results in waking up the host. 73 * @param isReliable Whether the message is reliable. 74 * @param messageSequenceNumber The message sequence number to use for the 75 * reliable message status. 76 */ 77 static void encodeNanoappMessage( 78 flatbuffers::FlatBufferBuilder &builder, uint64_t appId, 79 uint32_t messageType, uint16_t hostEndpoint, const void *messageData, 80 size_t messageDataLen, 81 uint32_t permissions = 82 static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_ALL), 83 uint32_t messagePermissions = 84 static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_ALL), 85 bool wokeHost = false, bool isReliable = false, 86 uint32_t messageSequenceNumber = 0); 87 88 /** 89 * Encodes a message delivery status for use with reliable messages. 90 * 91 * @param builder A newly constructed FlatBufferBuilder that will be used to 92 * encode the message. It will be finalized before returning from this 93 * function. 94 * @param messageSequenceNumber The message sequence number. 95 * @param errorCode The error code. 96 */ 97 static void encodeMessageDeliveryStatus( 98 flatbuffers::FlatBufferBuilder &builder, uint32_t messageSequenceNumber, 99 uint8_t errorCode); 100 101 /** 102 * Adds a string to the provided builder as a byte vector. 103 * 104 * @param builder The builder to add the string to. 105 * @param str The string to add. 106 * @return The offset in the builder that the string is stored at. 107 */ 108 static flatbuffers::Offset<flatbuffers::Vector<int8_t>> addStringAsByteVector( 109 flatbuffers::FlatBufferBuilder &builder, const char *str); 110 111 /** 112 * Constructs the message container and finalizes the FlatBufferBuilder 113 * 114 * @param builder The FlatBufferBuilder that was used to construct the 115 * message prior to adding the container 116 * @param messageType Type of message that was constructed 117 * @param message Offset of the message to include (normally the return value 118 * of flatbuffers::Offset::Union() on the message offset) 119 * @param hostClientId The source/client ID of the host-side entity that 120 * sent/should receive this message. Leave unspecified (default 0) 121 * when constructing a message on the host, as this field will be 122 * set before the message is sent to CHRE. 123 */ 124 static void finalize(flatbuffers::FlatBufferBuilder &builder, 125 fbs::ChreMessage messageType, 126 flatbuffers::Offset<void> message, 127 uint16_t hostClientId = kHostClientIdUnspecified); 128 129 /** 130 * Verifies that the provided message contains a valid flatbuffers CHRE 131 * protocol message, 132 * 133 * @param message The message to validate. 134 * @param length The size of the message to validate. 135 * @return true if the message is valid, false otherwise. 136 */ 137 static bool verifyMessage(const void *message, size_t messageLen); 138 }; 139 140 } // namespace chre 141 142 #endif // CHRE_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_ 143