1 /* 2 * Copyright 2023 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 #pragma once 18 19 #include <memory> 20 21 #include "common/libs/utils/result.h" 22 23 namespace cuttlefish { 24 namespace transport { 25 26 /** 27 * RawMessage - Header and raw byte payload for a serialized 28 * secure env message. 29 * 30 * @command: the command. 31 * @is_response: flag to mark message as a request/response. 32 * @payload_size: amount of payload data we're going to transfer. 33 * @payload: start of the serialized command specific payload. 34 */ 35 struct RawMessage { 36 uint32_t command : 31; 37 bool is_response : 1; 38 uint32_t payload_size; 39 uint8_t payload[0]; 40 }; 41 42 /** 43 * A destroyer for RawMessage instances created with 44 * CreateMessage. Wipes memory from the RawMessage 45 * instances. 46 */ 47 class MessageDestroyer { 48 public: 49 void operator()(RawMessage* ptr); 50 }; 51 52 /** An owning pointer for a RawMessage instance. */ 53 using ManagedMessage = std::unique_ptr<RawMessage, MessageDestroyer>; 54 55 /** 56 * Allocates memory for a RawMessage carrying a message of size 57 * `payload_size`. 58 */ 59 Result<ManagedMessage> CreateMessage(uint32_t command, bool is_response, 60 size_t payload_size); 61 Result<ManagedMessage> CreateMessage(uint32_t command, size_t payload_size); 62 63 /* 64 * Interface for communication channels that synchronously communicate 65 * HAL IPC/RPC calls. 66 */ 67 class Channel { 68 public: 69 virtual Result<void> SendRequest(RawMessage& message) = 0; 70 virtual Result<void> SendResponse(RawMessage& message) = 0; 71 virtual Result<ManagedMessage> ReceiveMessage() = 0; 72 virtual Result<int> WaitForMessage() = 0; ~Channel()73 virtual ~Channel() {} 74 }; 75 76 } // namespace transport 77 } // namespace cuttlefish