blob: f17393547f5df2a2f7ee5b2b3eabc868d92805f8 [file] [log] [blame]
Paul Kirth47451ce2020-07-13 17:42:08 +00001/*
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
29namespace cuttlefish {
30
Paul Kirthc246c032020-07-13 17:46:37 +000031constexpr char kDefaultLocation[] =
32 "/var/run/cuttlefish/cuttlefish_allocd.sock";
Paul Kirth47451ce2020-07-13 17:42:08 +000033
Paul Kirthc246c032020-07-13 17:46:37 +000034// Default flags for send and receive.
35static constexpr int kSendFlags = 0;
36static 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
Paul Kirth47451ce2020-07-13 17:42:08 +000042bool SendJsonMsg(cuttlefish::SharedFD client_socket, const Json::Value& resp);
43
Paul Kirthc246c032020-07-13 17:46:37 +000044/// 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
Paul Kirth47451ce2020-07-13 17:42:08 +000048std::optional<Json::Value> RecvJsonMsg(cuttlefish::SharedFD client_socket);
49
Paul Kirthc246c032020-07-13 17:46:37 +000050// Helper functions mapping between Enum types and std::string
51
52RequestType StrToReqTy(const std::string& req);
53
54std::string ReqTyToStr(RequestType req_ty);
55
56IfaceType StrToIfaceTy(const std::string& iface);
57
58std::string IfaceTyToStr(IfaceType iface);
59
60RequestStatus StrToStatus(const std::string& st);
61
62std::string StatusToStr(RequestStatus st);
63
Paul Kirth47451ce2020-07-13 17:42:08 +000064} // namespace cuttlefish