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 #include "host/libs/config/fastboot/fastboot.h"
17 
18 #include <utility>
19 #include <vector>
20 
21 #include "common/libs/utils/result.h"
22 #include "host/commands/kernel_log_monitor/utils.h"
23 #include "host/libs/config/command_source.h"
24 #include "host/libs/config/cuttlefish_config.h"
25 #include "host/libs/config/known_paths.h"
26 
27 namespace cuttlefish {
28 namespace {
29 
30 class FastbootProxy : public CommandSource, public KernelLogPipeConsumer {
31  public:
INJECT(FastbootProxy (const CuttlefishConfig::InstanceSpecific & instance,const FastbootConfig & fastboot_config,KernelLogPipeProvider & log_pipe_provider))32   INJECT(FastbootProxy(const CuttlefishConfig::InstanceSpecific& instance,
33                        const FastbootConfig& fastboot_config,
34                        KernelLogPipeProvider& log_pipe_provider))
35       : instance_(instance),
36         fastboot_config_(fastboot_config),
37         log_pipe_provider_(log_pipe_provider) {}
38 
Commands()39   Result<std::vector<MonitorCommand>> Commands() override {
40     const std::string ethernet_host = instance_.ethernet_ipv6() + "%" +
41                                       instance_.ethernet_bridge_name();
42 
43     Command tunnel(SocketVsockProxyBinary());
44     tunnel.AddParameter("--events_fd=", kernel_log_pipe_);
45     tunnel.AddParameter("--start_event_id=", monitor::Event::FastbootStarted);
46     tunnel.AddParameter("--stop_event_id=", monitor::Event::AdbdStarted);
47     tunnel.AddParameter("--server_type=", "tcp");
48     tunnel.AddParameter("--server_tcp_port=", instance_.fastboot_host_port());
49     tunnel.AddParameter("--client_type=", "tcp");
50     tunnel.AddParameter("--client_tcp_host=", ethernet_host);
51     tunnel.AddParameter("--client_tcp_port=", "5554");
52     tunnel.AddParameter("--label=", "fastboot");
53 
54     std::vector<MonitorCommand> commands;
55     commands.emplace_back(std::move(tunnel));
56     return commands;
57   }
58 
Name() const59   std::string Name() const override { return "FastbootProxy"; }
Enabled() const60   bool Enabled() const override {
61     return instance_.boot_flow() == CuttlefishConfig::InstanceSpecific::BootFlow::Android &&
62            fastboot_config_.ProxyFastboot();
63   }
64 
65  private:
Dependencies() const66   std::unordered_set<SetupFeature*> Dependencies() const override {
67     return {static_cast<SetupFeature*>(&log_pipe_provider_)};
68   }
69 
ResultSetup()70   Result<void> ResultSetup() override {
71     kernel_log_pipe_ = log_pipe_provider_.KernelLogPipe();
72     return {};
73   }
74 
75   const CuttlefishConfig::InstanceSpecific& instance_;
76   const FastbootConfig& fastboot_config_;
77   KernelLogPipeProvider& log_pipe_provider_;
78   SharedFD kernel_log_pipe_;
79 };
80 
81 }  // namespace
82 
83 fruit::Component<fruit::Required<KernelLogPipeProvider,
84                                  const CuttlefishConfig::InstanceSpecific,
85                                  const FastbootConfig>>
LaunchFastbootComponent()86 LaunchFastbootComponent() {
87   return fruit::createComponent()
88       .addMultibinding<CommandSource, FastbootProxy>()
89       .addMultibinding<KernelLogPipeConsumer, FastbootProxy>()
90       .addMultibinding<SetupFeature, FastbootProxy>();
91 }
92 
93 }  // namespace cuttlefish
94