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) -> Result<void> {
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 CF_EXPECT(config_.SetModes(modes));
46 return {};
47 });
48 }
49
Name() const50 std::string Name() const override { return "AdbConfigFlagImpl"; }
51
Dependencies() const52 std::unordered_set<FlagFeature*> Dependencies() const override {
53 return {static_cast<FlagFeature*>(&config_flag_)};
54 }
55
Process(std::vector<std::string> & args)56 Result<void> Process(std::vector<std::string>& args) override {
57 // Defaults
58 config_.SetModes({AdbMode::VsockHalfTunnel});
59 bool run_adb_connector = !IsRunningInContainer();
60 Flag run_flag = GflagsCompatFlag("run_adb_connector", run_adb_connector);
61 CF_EXPECT(ConsumeFlags({run_flag, mode_flag_}, args),
62 "Failed to parse adb config flags");
63 config_.SetRunConnector(run_adb_connector);
64
65 auto adb_modes_check = config_.Modes();
66 adb_modes_check.erase(AdbMode::Unknown);
67 if (adb_modes_check.size() < 1) {
68 LOG(INFO) << "ADB not enabled";
69 }
70
71 return {};
72 }
WriteGflagsCompatHelpXml(std::ostream & out) const73 bool WriteGflagsCompatHelpXml(std::ostream& out) const override {
74 bool run = config_.RunConnector();
75 Flag run_flag = GflagsCompatFlag("run_adb_connector", run).Help(run_help);
76 return WriteGflagsCompatXml({run_flag, mode_flag_}, out);
77 }
78
79 private:
80 static constexpr char run_help[] =
81 "Maintain adb connection by sending 'adb connect' commands to the "
82 "server. Only relevant with -adb_mode=tunnel or vsock_tunnel.";
83 static constexpr char mode_help[] =
84 "Mode for ADB connection."
85 "'vsock_tunnel' for a TCP connection tunneled through vsock, "
86 "'native_vsock' for a direct connection to the guest ADB over "
87 "vsock, 'vsock_half_tunnel' for a TCP connection forwarded to "
88 "the guest ADB server, or a comma separated list of types as in "
89 "'native_vsock,vsock_half_tunnel'";
90
91 AdbConfig& config_;
92 ConfigFlag& config_flag_;
93 Flag mode_flag_;
94 };
95
96 } // namespace
97
98 fruit::Component<fruit::Required<AdbConfig, ConfigFlag>, AdbConfigFlag>
AdbConfigFlagComponent()99 AdbConfigFlagComponent() {
100 return fruit::createComponent()
101 .bind<AdbConfigFlag, AdbConfigFlagImpl>()
102 .addMultibinding<FlagFeature, AdbConfigFlag>();
103 }
104
105 } // namespace cuttlefish
106