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/fragmented_load_transaction.h" 25 #include "chre_host/generated/host_messages_generated.h" 26 #include "flatbuffers/flatbuffers.h" 27 28 #include <vector> 29 30 namespace android { 31 namespace chre { 32 33 /** 34 * Checks that a string encapsulated as a byte vector is null-terminated, and 35 * if it is, returns a pointer to the vector's data. Otherwise returns null. 36 * 37 * This is similar to getStringFromByteVector in host_protocol_chre.h. Ensure 38 * that method's implementation is kept in sync with this. 39 * 40 * @param vec Target vector, can be null 41 * 42 * @return Pointer to the vector's data, or null 43 */ 44 const char *getStringFromByteVector(const std::vector<int8_t> &vec); 45 46 /** 47 * Calling code should provide an implementation of this interface to handle 48 * parsed results from decodeMessageFromChre(). 49 */ 50 class IChreMessageHandlers { 51 public: 52 virtual ~IChreMessageHandlers() = default; 53 handleNanoappMessage(const::chre::fbs::NanoappMessageT &)54 virtual void handleNanoappMessage( 55 const ::chre::fbs::NanoappMessageT & /*message*/){}; 56 handleHubInfoResponse(const::chre::fbs::HubInfoResponseT &)57 virtual void handleHubInfoResponse( 58 const ::chre::fbs::HubInfoResponseT & /*response*/){}; 59 handleNanoappListResponse(const::chre::fbs::NanoappListResponseT &)60 virtual void handleNanoappListResponse( 61 const ::chre::fbs::NanoappListResponseT & /*response*/){}; 62 handleLoadNanoappResponse(const::chre::fbs::LoadNanoappResponseT &)63 virtual void handleLoadNanoappResponse( 64 const ::chre::fbs::LoadNanoappResponseT & /*response*/){}; 65 handleUnloadNanoappResponse(const::chre::fbs::UnloadNanoappResponseT &)66 virtual void handleUnloadNanoappResponse( 67 const ::chre::fbs::UnloadNanoappResponseT & /*response*/){}; 68 handleDebugDumpData(const::chre::fbs::DebugDumpDataT &)69 virtual void handleDebugDumpData( 70 const ::chre::fbs::DebugDumpDataT & /*data*/){}; 71 handleDebugDumpResponse(const::chre::fbs::DebugDumpResponseT &)72 virtual void handleDebugDumpResponse( 73 const ::chre::fbs::DebugDumpResponseT & /*response*/){}; 74 handleSelfTestResponse(const::chre::fbs::SelfTestResponseT &)75 virtual void handleSelfTestResponse( 76 const ::chre::fbs::SelfTestResponseT & /*response*/){}; 77 }; 78 79 /** 80 * A set of helper methods that simplify the encode/decode of FlatBuffers 81 * messages used in communication with CHRE from the host. 82 */ 83 class HostProtocolHost : public ::chre::HostProtocolCommon { 84 public: 85 /** 86 * Decodes a message sent from CHRE and invokes the appropriate handler 87 * function in the provided interface implementation to handle the parsed 88 * result. 89 * 90 * @param message Buffer containing a complete FlatBuffers CHRE message 91 * @param messageLen Size of the message, in bytes 92 * @param handlers Set of callbacks to handle the parsed message. If this 93 * function returns success, then exactly one of these functions was 94 * called. 95 * 96 * @return true if the message was parsed successfully and passed to a handler 97 */ 98 static bool decodeMessageFromChre(const void *message, size_t messageLen, 99 IChreMessageHandlers &handlers); 100 101 /** 102 * Encodes a message requesting hub information from CHRE 103 * 104 * @param builder A newly constructed FlatBufferBuilder that will be used to 105 * construct the message 106 */ 107 static void encodeHubInfoRequest(flatbuffers::FlatBufferBuilder &builder); 108 109 /** 110 * Encodes a message sending boot debug configuration to CHRE 111 * 112 * @param builder A newly constructed FlatBufferBuilder that will be used to 113 * construct the message 114 */ 115 static void encodeDebugConfiguration(flatbuffers::FlatBufferBuilder &builder); 116 117 /** 118 * Encodes a message requesting to load a nanoapp specified by the included 119 * (possibly fragmented) binary payload and metadata. 120 * 121 * @param builder A newly constructed FlatBufferBuilder that will be used to 122 * construct the message 123 * @param request The FragmentedLoadRequest object with the binary and the 124 * metadata 125 * @param respondBeforeStart See LoadNanoappRequest.respond_before_start in 126 * flatbuffers message. 127 */ 128 static void encodeFragmentedLoadNanoappRequest( 129 flatbuffers::FlatBufferBuilder &builder, 130 const FragmentedLoadRequest &request, bool respondBeforeStart = false); 131 132 /** 133 * Encodes a message requesting the list of loaded nanoapps from CHRE 134 * 135 * @param builder A newly constructed FlatBufferBuilder that will be used to 136 * construct the message 137 */ 138 static void encodeNanoappListRequest(flatbuffers::FlatBufferBuilder &builder); 139 140 /** 141 * Encodes a message requesting to unload a nanoapp specified by app ID. 142 * 143 * @param builder A newly constructed FlatBufferBuilder that will be used to 144 * construct the message 145 * @param transactionId A transaction identifier to tie the subsequent 146 * response to this request 147 * @param appId Identifier for the app to unload 148 * @param allowSystemNanoappUnload Whether this request should be allowed to 149 * result in unloading a system nanoapp (e.g. requests from the context 150 * hub HAL should have set this to false, as system nanoapps are not 151 * expected to be managed through that HAL) 152 */ 153 static void encodeUnloadNanoappRequest( 154 flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId, 155 uint64_t appId, bool allowSystemNanoappUnload); 156 157 /** 158 * Encodes a message to send the AP timestamp to CHRE 159 * 160 * @param builder A newly constructed FlatBufferBuilder that will be used to 161 * construct the message 162 * @param offset The AP to SLPI offset in nanoseconds 163 */ 164 static void encodeTimeSyncMessage(flatbuffers::FlatBufferBuilder &builder, 165 int64_t offset); 166 167 /** 168 * Encodes a message requesting debugging information from CHRE 169 * 170 * @param builder A newly constructed FlatBufferBuilder that will be used to 171 * construct the message 172 */ 173 static void encodeDebugDumpRequest(flatbuffers::FlatBufferBuilder &builder); 174 175 /** 176 * Decodes the host client ID included in the message container 177 * 178 * @param message Buffer containing a complete FlatBuffers CHRE message 179 * @param messageLen Size of the message, in bytes 180 * @param hostClientId Output parameter that will be populated with the client 181 * ID included in the message on success 182 * 183 * @return true if the host client ID was successfully decoded from the 184 * message 185 */ 186 static bool extractHostClientIdAndType(const void *message, size_t messageLen, 187 uint16_t *hostClientId, 188 ::chre::fbs::ChreMessage *messageType); 189 190 /** 191 * Update the host client ID field in the MessageContainer. 192 * 193 * @param message Buffer containing a complete FlatBuffers CHRE message 194 * @param messageLen Size of the message, in bytes 195 * @param hostClientId The value to set the host client ID to 196 * 197 * @return true if the message was verified successfully, and we were able to 198 * modify the host client ID field 199 */ 200 static bool mutateHostClientId(void *message, size_t messageLen, 201 uint16_t hostClientId); 202 203 /** 204 * Encodes a message requesting to load a nanoapp specified by the included 205 * binary payload and metadata. 206 * 207 * @param builder A newly constructed FlatBufferBuilder that will be used to 208 * construct the message 209 */ 210 static void encodeLoadNanoappRequestForBinary( 211 flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId, 212 uint64_t appId, uint32_t appVersion, uint32_t appFlags, 213 uint32_t targetApiVersion, const std::vector<uint8_t> &nanoappBinary, 214 uint32_t fragmentId, size_t appTotalSizeBytes, bool respondBeforeStart); 215 216 /** 217 * Encodes a message requesting to load a nanoapp specified by the included 218 * binary filename and metadata. 219 * 220 * @param builder A newly constructed FlatBufferBuilder that will be used to 221 * construct the message 222 */ 223 static void encodeLoadNanoappRequestForFile( 224 flatbuffers::FlatBufferBuilder &builder, uint32_t transactionId, 225 uint64_t appId, uint32_t appVersion, uint32_t targetApiVersion, 226 const char *nanoappBinaryName); 227 228 /** 229 * Encodes a message notifying CHRE of a user setting change. 230 * 231 * @param builder A newly constructed FlatBufferBuilder that will be used to 232 * construct the message 233 * @param setting The setting value that the user changed 234 * @param newState The state that the user change the setting to. 235 */ 236 static void encodeSettingChangeNotification( 237 flatbuffers::FlatBufferBuilder &builder, ::chre::fbs::Setting setting, 238 ::chre::fbs::SettingState newState); 239 240 /** 241 * Encodes a message to request CHRE to perform a self test. 242 */ 243 static void encodeSelfTestRequest(flatbuffers::FlatBufferBuilder &builder); 244 245 /** 246 * Encodes a message notifying CHRE of a host endpoint that connected. 247 * 248 * @param builder A newly constructed FlatBufferBuilder that will be used to 249 * construct the message 250 * @param hostEndpointId The ID of the host endpoint that connected. 251 * @param type The CHRE_HOST_ENDPOINT_TYPE_* value that describes the 252 * endpoint. 253 * @param packageName The (optional) package name of the endpoint. 254 * @param attributionTag The (optional) attribution tag of the endpoint. 255 */ 256 static void encodeHostEndpointConnected( 257 flatbuffers::FlatBufferBuilder &builder, uint16_t hostEndpointId, 258 uint8_t type, const std::string &packageName, 259 const std::string &attributionTag); 260 261 /** 262 * Encodes a message notifying CHRE of a host endpoint that disconnected. 263 * 264 * @param builder A newly constructed FlatBufferBuilder that will be used to 265 * construct the message 266 * @param hostEndpointId The ID of the host endpoint that disconnected. 267 */ 268 static void encodeHostEndpointDisconnected( 269 flatbuffers::FlatBufferBuilder &builder, uint16_t hostEndpointId); 270 271 static void encodeNanconfigurationUpdate( 272 flatbuffers::FlatBufferBuilder &builder, bool nanEnabled); 273 }; 274 275 } // namespace chre 276 } // namespace android 277 278 #endif // CHRE_HOST_HOST_PROTOCOL_HOST_H_ 279