• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <optional>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include <android-base/logging.h>
25 #include <android-base/result.h>
26 #include <build/version.h>
27 
28 #include "common/libs/fs/shared_fd.h"
29 #include "common/libs/utils/json.h"
30 #include "common/libs/utils/result.h"
31 #include "common/libs/utils/unix_sockets.h"
32 #include "cvd_server.pb.h"
33 #include "host/commands/cvd/types.h"
34 
35 namespace cuttlefish {
36 
37 struct OverrideFd {
38   std::optional<SharedFD> stdin_override_fd;
39   std::optional<SharedFD> stdout_override_fd;
40   std::optional<SharedFD> stderr_override_fd;
41 };
42 
43 class CvdClient {
44  public:
45   Result<void> ValidateServerVersion(const int num_retries = 1);
46   Result<void> StopCvdServer(bool clear);
47   Result<void> HandleAcloud(
48       const std::vector<std::string>& args,
49       const std::unordered_map<std::string, std::string>& env);
50   Result<cvd::Response> HandleCommand(
51       const std::vector<std::string>& args,
52       const std::unordered_map<std::string, std::string>& env,
53       const std::vector<std::string>& selector_args,
54       const OverrideFd& control_fds);
HandleCommand(const std::vector<std::string> & args,const std::unordered_map<std::string,std::string> & env,const std::vector<std::string> & selector_args)55   Result<cvd::Response> HandleCommand(
56       const std::vector<std::string>& args,
57       const std::unordered_map<std::string, std::string>& env,
58       const std::vector<std::string>& selector_args) {
59     auto response = CF_EXPECT(
60         HandleCommand(args, env, selector_args,
61                       OverrideFd{std::nullopt, std::nullopt, std::nullopt}));
62     return response;
63   }
64   Result<std::string> HandleVersion();
65   Result<cvd_common::Args> ValidSubcmdsList(const cvd_common::Envs& envs);
66 
67  private:
68   std::optional<UnixMessageSocket> server_;
69 
70   Result<void> SetServer(const SharedFD& server);
71   Result<cvd::Response> SendRequest(const cvd::Request& request,
72                                     const OverrideFd& new_control_fds = {},
73                                     std::optional<SharedFD> extra_fd = {});
74   Result<void> StartCvdServer();
75   Result<void> CheckStatus(const cvd::Status& status, const std::string& rpc);
76   Result<cvd::Version> GetServerVersion();
77 
78   Result<Json::Value> ListSubcommands(const cvd_common::Envs& envs);
79   static cvd::Version GetClientVersion();
80 };
81 
82 }  // end of namespace cuttlefish
83