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 "flatbuffers/flatbuffers.h" 23 24 namespace chre { 25 26 namespace fbs { 27 28 // Forward declaration of the ChreMessage enum defined in the generated 29 // FlatBuffers header file 30 enum class ChreMessage : uint8_t; 31 32 } // namespace fbs 33 34 //! On a message sent from CHRE, specifies that the host daemon should determine 35 //! which client to send the message to. Usually, this is all clients, but for a 36 //! message from a nanoapp, the host daemon can use the endpoint ID to determine 37 //! the destination client ID. 38 constexpr uint16_t kHostClientIdUnspecified = 0; 39 40 /** 41 * Functions that are shared between the CHRE and host to assist with 42 * communications between the two. Note that normally these functions are 43 * accessed through a derived class like chre::HostProtocolChre (CHRE-side) or 44 * android::chre:HostProtocolHost (host-side). 45 */ 46 class HostProtocolCommon { 47 public: 48 /** 49 * Encodes a message to/from a nanoapp using the given FlatBufferBuilder and 50 * supplied parameters. 51 * 52 * @param builder A newly constructed FlatBufferBuilder that will be used to 53 * encode the message. It will be finalized before returning from this 54 * function. 55 */ 56 static void encodeNanoappMessage( 57 flatbuffers::FlatBufferBuilder& builder, uint64_t appId, 58 uint32_t messageType, uint16_t hostEndpoint, const void *messageData, 59 size_t messageDataLen); 60 61 /** 62 * Adds a string to the provided builder as a byte vector. 63 * 64 * @param builder The builder to add the string to. 65 * @param str The string to add. 66 * @return The offset in the builder that the string is stored at. 67 */ 68 static flatbuffers::Offset<flatbuffers::Vector<int8_t>> 69 addStringAsByteVector(flatbuffers::FlatBufferBuilder& builder, 70 const char *str); 71 72 /** 73 * Constructs the message container and finalizes the FlatBufferBuilder 74 * 75 * @param builder The FlatBufferBuilder that was used to construct the 76 * message prior to adding the container 77 * @param messageType Type of message that was constructed 78 * @param message Offset of the message to include (normally the return value 79 * of flatbuffers::Offset::Union() on the message offset) 80 * @param hostClientId The source/client ID of the host-side entity that 81 * sent/should receive this message. Leave unspecified (default 0) 82 * when constructing a message on the host, as this field will be 83 * set before the message is sent to CHRE. 84 */ 85 static void finalize( 86 flatbuffers::FlatBufferBuilder& builder, fbs::ChreMessage messageType, 87 flatbuffers::Offset<void> message, 88 uint16_t hostClientId = kHostClientIdUnspecified); 89 90 /** 91 * Verifies that the provided message contains a valid flatbuffers CHRE 92 * protocol message, 93 * 94 * @param message The message to validate. 95 * @param length The size of the message to validate. 96 * @return true if the message is valid, false otherwise. 97 */ 98 static bool verifyMessage(const void *message, size_t messageLen); 99 }; 100 101 } // namespace chre 102 103 #endif // CHRE_PLATFORM_SHARED_HOST_PROTOCOL_COMMON_H_ 104