1 /*
2 * Copyright (C) 2018 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 #include "host/libs/config/adb/adb.h"
17
18 #include <android-base/strings.h>
19
20 #include "common/libs/utils/flag_parser.h"
21 #include "host/libs/config/config_flag.h"
22 #include "host/libs/config/feature.h"
23
24 namespace cuttlefish {
25 namespace {
26
27 class AdbConfigFlagImpl : public AdbConfigFlag {
28 public:
INJECT(AdbConfigFlagImpl (AdbConfig & config,ConfigFlag & config_flag))29 INJECT(AdbConfigFlagImpl(AdbConfig& config, ConfigFlag& config_flag))
30 : config_(config), config_flag_(config_flag) {
31 mode_flag_ = GflagsCompatFlag("adb_mode").Help(mode_help);
32 mode_flag_.Getter([this]() {
33 std::stringstream modes;
34 for (const auto& mode : config_.Modes()) {
35 modes << "," << AdbModeToString(mode);
36 }
37 return modes.str().substr(1); // First comma
38 });
39 mode_flag_.Setter([this](const FlagMatch& match) {
40 // TODO(schuffelen): Error on unknown types?
41 std::set<AdbMode> modes;
42 for (auto& mode : android::base::Split(match.value, ",")) {
43 modes.insert(StringToAdbMode(mode));
44 }
45 return config_.SetModes(modes);
46 });
47 }
48
Name() const49 std::string Name() const override { return "AdbConfigFlagImpl"; }
50
Dependencies() const51 std::unordered_set<FlagFeature*> Dependencies() const override {
52 return {static_cast<FlagFeature*>(&config_flag_)};
53 }
54
Process(std::vector<std::string> & args)55 bool Process(std::vector<std::string>& args) override {
56 // Defaults
57 config_.SetModes({AdbMode::VsockHalfTunnel});
58 bool run_adb_connector = !IsRunningInContainer();
59 Flag run_flag = GflagsCompatFlag("run_adb_connector", run_adb_connector);
60 if (!ParseFlags({run_flag, mode_flag_}, args)) {
61 LOG(ERROR) << "Failed to parse adb config flags";
62 return false;
63 }
64 config_.SetRunConnector(run_adb_connector);
65
66 auto adb_modes_check = config_.Modes();
67 adb_modes_check.erase(AdbMode::Unknown);
68 if (adb_modes_check.size() < 1) {
69 LOG(INFO) << "ADB not enabled";
70 }
71
72 return true;
73 }
WriteGflagsCompatHelpXml(std::ostream & out) const74 bool WriteGflagsCompatHelpXml(std::ostream& out) const override {
75 bool run = config_.RunConnector();
76 Flag run_flag = GflagsCompatFlag("run_adb_connector", run).Help(run_help);
77 return WriteGflagsCompatXml({run_flag, mode_flag_}, out);
78 }
79
80 private:
81 static constexpr char run_help[] =
82 "Maintain adb connection by sending 'adb connect' commands to the "
83 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel.";
84 static constexpr char mode_help[] =
85 "Mode for ADB connection."
86 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
87 "'native_vsock' for a direct connection to the guest ADB over "
88 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
89 "the guest ADB server, or a comma separated list of types as in "
90 "'native_vsock,vsock_half_tunnel'";
91
92 AdbConfig& config_;
93 ConfigFlag& config_flag_;
94 Flag mode_flag_;
95 };
96
97 } // namespace
98
99 fruit::Component<fruit::Required<AdbConfig, ConfigFlag>, AdbConfigFlag>
AdbConfigFlagComponent()100 AdbConfigFlagComponent() {
101 return fruit::createComponent()
102 .bind<AdbConfigFlag, AdbConfigFlagImpl>()
103 .addMultibinding<FlagFeature, AdbConfigFlag>();
104 }
105
106 } // namespace cuttlefish
107