host_protocol_host.h revision 2f6b50aa8e01717948efd3024f6ab4f4825c0a18
1 2/* 3 * Copyright (C) 2017 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18#ifndef CHRE_HOST_HOST_PROTOCOL_HOST_H_ 19#define CHRE_HOST_HOST_PROTOCOL_HOST_H_ 20 21#include <stdint.h> 22 23#include "chre/platform/shared/host_protocol_common.h" 24#include "chre_host/host_messages_generated.h" 25#include "flatbuffers/flatbuffers.h" 26 27namespace android { 28namespace chre { 29 30/** 31 * Calling code should provide an implementation of this interface to handle 32 * parsed results from decodeMessageFromChre(). 33 */ 34class IChreMessageHandlers { 35 public: 36 virtual ~IChreMessageHandlers() = default; 37 38 virtual void handleNanoappMessage( 39 uint64_t appId, uint32_t messageType, uint16_t hostEndpoint, 40 const void *messageData, size_t messageDataLen) = 0; 41 42 virtual void handleHubInfoResponse( 43 const char *name, const char *vendor, 44 const char *toolchain, uint32_t legacyPlatformVersion, 45 uint32_t legacyToolchainVersion, float peakMips, float stoppedPower, 46 float sleepPower, float peakPower, uint32_t maxMessageLen, 47 uint64_t platformId, uint32_t version) = 0; 48 49 virtual void handleNanoappListResponse( 50 const ::chre::fbs::NanoappListResponseT& response) = 0; 51 52 virtual void handleLoadNanoappResponse( 53 const ::chre::fbs::LoadNanoappResponseT& response) = 0; 54 55 virtual void handleUnloadNanoappResponse( 56 const ::chre::fbs::UnloadNanoappResponseT& response) = 0; 57}; 58 59/** 60 * A set of helper methods that simplify the encode/decode of FlatBuffers 61 * messages used in communication with CHRE from the host. 62 */ 63class HostProtocolHost : public ::chre::HostProtocolCommon { 64 public: 65 /** 66 * Decodes a message sent from CHRE and invokes the appropriate handler 67 * function in the provided interface implementation to handle the parsed 68 * result. 69 * 70 * @param message Buffer containing a complete FlatBuffers CHRE message 71 * @param messageLen Size of the message, in bytes 72 * @param handlers Set of callbacks to handle the parsed message. If this 73 * function returns success, then exactly one of these functions was 74 * called. 75 * 76 * @return true if the message was parsed successfully and passed to a handler 77 */ 78 static bool decodeMessageFromChre(const void *message, size_t messageLen, 79 IChreMessageHandlers& handlers); 80 81 /** 82 * Encodes a message requesting hub information from CHRE 83 * 84 * @param builder A newly constructed FlatBufferBuilder that will be used to 85 * construct the message 86 */ 87 static void encodeHubInfoRequest(flatbuffers::FlatBufferBuilder& builder); 88 89 /** 90 * Encodes a message requesting to load a nanoapp specified by the included 91 * binary payload and metadata. 92 * 93 * @param builder A newly constructed FlatBufferBuilder that will be used to 94 * construct the message 95 */ 96 static void encodeLoadNanoappRequest( 97 flatbuffers::FlatBufferBuilder& builder, uint32_t transactionId, 98 uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion, 99 const std::vector<uint8_t>& nanoappBinary); 100 101 /** 102 * Encodes a message requesting the list of loaded nanoapps from CHRE 103 * 104 * @param builder A newly constructed FlatBufferBuilder that will be used to 105 * construct the message 106 */ 107 static void encodeNanoappListRequest(flatbuffers::FlatBufferBuilder& builder); 108 109 /** 110 * Encodes a message requesting to unload a nanoapp specified by app ID. 111 * 112 * @param builder A newly constructed FlatBufferBuilder that will be used to 113 * construct the message 114 * @param transactionId A transaction identifier to tie the subsequent 115 * response to this request 116 * @param appId Identifier for the app to unload 117 * @param allowSystemNanoappUnload Whether this request should be allowed to 118 * result in unloading a system nanoapp (e.g. requests from the context 119 * hub HAL should have set this to false, as system nanoapps are not 120 * expected to be managed through that HAL) 121 */ 122 static void encodeUnloadNanoappRequest( 123 flatbuffers::FlatBufferBuilder& builder, uint32_t transactionId, 124 uint64_t appId, bool allowSystemNanoappUnload); 125 126 /** 127 * Decodes the host client ID included in the message container 128 * 129 * @param message Buffer containing a complete FlatBuffers CHRE message 130 * @param messageLen Size of the message, in bytes 131 * @param hostClientId Output parameter that will be populated with the client 132 * ID included in the message on success 133 * 134 * @return true if the host client ID was successfully decoded from the 135 * message 136 */ 137 static bool extractHostClientIdAndType( 138 const void *message, size_t messageLen, uint16_t *hostClientId, 139 ::chre::fbs::ChreMessage *messageType); 140 141 /** 142 * Update the host client ID field in the MessageContainer. 143 * 144 * @param message Buffer containing a complete FlatBuffers CHRE message 145 * @param messageLen Size of the message, in bytes 146 * @param hostClientId The value to set the host client ID to 147 * 148 * @return true if the message was verified successfully, and we were able to 149 * modify the host client ID field 150 */ 151 static bool mutateHostClientId(void *message, size_t messageLen, 152 uint16_t hostClientId); 153}; 154 155} // namespace chre 156} // namespace android 157 158#endif // CHRE_HOST_HOST_PROTOCOL_HOST_H_ 159