1 // 2 // Copyright (C) 2021 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 #pragma once 17 18 #include <cstdint> 19 #include <functional> 20 #include <optional> 21 #include <string> 22 #include <tuple> 23 24 #include "common/libs/confui/protocol.h" 25 #include "common/libs/fs/shared_fd.h" 26 27 namespace cuttlefish { 28 namespace confui { 29 namespace packet { 30 /* 31 * for communication between Confirmation UI guest and host. 32 * 33 * Payload is actually the header. When we send/recv, besides Payload, 34 * the "payload_length_" bytes should be additionally sent/recv'ed. 35 * 36 * The payload is assumed to be a text (e.g. char[N]) 37 * The WritePayload will create the string. When read, however, 38 * the receiver should parse it 39 * 40 * The format we use for confirmation UI is: 41 * session_id:type:contents 42 * 43 * e.g. GooglePay10354:start:my confirmaton message 44 */ 45 struct PayloadHeader { 46 std::uint32_t payload_length_; 47 }; 48 49 // PayloadHeader + the message actually being sent 50 using Payload = std::tuple<PayloadHeader, std::string>; 51 52 // msg will look like "334522:start:Hello I am Here!" 53 // this function returns 334522, start, "Hello I am Here!" 54 // if no session id is given, it is regarded as SESSION_ANY 55 ConfUiMessage PayloadToConfUiMessage(const std::string& str_to_parse); 56 57 std::optional<ConfUiMessage> RecvConfUiMsg(SharedFD fd); 58 bool SendAck(SharedFD fd, const std::string& session_id, const bool is_success, 59 const std::string& additional_info); 60 bool SendResponse(SharedFD fd, const std::string& session_id, 61 const std::string& additional_info); 62 // for HAL 63 bool SendCmd(SharedFD fd, const std::string& session_id, ConfUiCmd cmd, 64 const std::string& additional_info); 65 66 // this is for short messages 67 constexpr const ssize_t kMaxPayloadLength = 1000; 68 69 } // end of namespace packet 70 } // end of namespace confui 71 } // end of namespace cuttlefish 72