1 /* 2 * Copyright (C) 2020 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 <android-base/logging.h> 20 #include <json/json.h> 21 22 #include <optional> 23 #include <string> 24 25 #include "common/libs/fs/shared_fd.h" 26 #include "host/libs/allocd/request.h" 27 #include "host/libs/config/logging.h" 28 29 namespace cuttlefish { 30 31 constexpr char kDefaultLocation[] = 32 "/var/run/cuttlefish/cuttlefish_allocd.sock"; 33 34 // Default flags for send and receive. 35 static constexpr int kSendFlags = 0; 36 static constexpr int kRecvFlags = 0; 37 38 /// Sends a Json value over client_socket 39 /// 40 /// returns true if successfully sent the whole JSON object 41 /// returns false otherwise 42 bool SendJsonMsg(cuttlefish::SharedFD client_socket, const Json::Value& resp); 43 44 /// Receives a single Json value over client_socket 45 /// 46 /// The returned option will contain the JSON object when successful, 47 /// or an std::nullopt if an error is reported 48 std::optional<Json::Value> RecvJsonMsg(cuttlefish::SharedFD client_socket); 49 50 // Helper functions mapping between Enum types and std::string 51 52 RequestType StrToReqTy(const std::string& req); 53 54 std::string ReqTyToStr(RequestType req_ty); 55 56 IfaceType StrToIfaceTy(const std::string& iface); 57 58 std::string IfaceTyToStr(IfaceType iface); 59 60 RequestStatus StrToStatus(const std::string& st); 61 62 std::string StatusToStr(RequestStatus st); 63 64 } // namespace cuttlefish 65