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 */ 74 static void encodeNanoappMessage( 75 flatbuffers::FlatBufferBuilder &builder, uint64_t appId, 76 uint32_t messageType, uint16_t hostEndpoint, const void *messageData, 77 size_t messageDataLen, 78 uint32_t permissions = 79 static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_ALL), 80 uint32_t messagePermissions = 81 static_cast<uint32_t>(chre::NanoappPermissions::CHRE_PERMS_ALL), 82 bool wokeHost = false); 83 84 /** 85 * Adds a string to the provided builder as a byte vector. 86 * 87 * @param builder The builder to add the string to. 88 * @param str The string to add. 89 * @return The offset in the builder that the string is stored at. 90 */ 91 static flatbuffers::Offset<flatbuffers::Vector<int8_t>> addStringAsByteVector( 92 flatbuffers::FlatBufferBuilder &builder, const char *str); 93 94 /** 95 * Constructs the message container and finalizes the FlatBufferBuilder 96 * 97 * @param builder The FlatBufferBuilder that was used to construct the 98 * message prior to adding the container 99 * @param messageType Type of message that was constructed 100 * @param message Offset of the message to include (normally the return value 101 * of flatbuffers::Offset::Union() on the message offset) 102 * @param hostClientId The source/client ID of the host-side entity that 103 * sent/should receive this message. Leave unspecified (default 0) 104 * when constructing a message on the host, as this field will be 105 * set before the message is sent to CHRE. 106 */ 107 static void finalize(flatbuffers::FlatBufferBuilder &builder, 108 fbs::ChreMessage messageType, 109 flatbuffers::Offset<void> message, 110 uint16_t hostClientId = kHostClientIdUnspecified); 111 112 /** 113 * Verifies that the provided message contains a valid flatbuffers CHRE 114 * protocol message, 115 * 116 * @param message The message to validate. 117 * @param length The size of the message to validate. 118 * @return true if the message is valid, false otherwise. 119 */ 120 static bool verifyMessage(const void *message, size_t messageLen); 121 }; 122 123 } // namespace chre 124 125 #endif // CHRE_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_ 126