1 //
2 // Copyright (C) 2023 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/commands/run_cvd/launch/launch.h"
17 
18 #include <string>
19 #include <unordered_set>
20 #include <utility>
21 #include <vector>
22 
23 #include <fruit/fruit.h>
24 
25 #include "common/libs/utils/files.h"
26 #include "common/libs/utils/result.h"
27 #include "host/commands/run_cvd/launch/grpc_socket_creator.h"
28 #include "host/libs/config/command_source.h"
29 #include "host/libs/config/known_paths.h"
30 
31 namespace cuttlefish {
32 namespace {
33 
34 class OpenwrtControlServer : public CommandSource {
35  public:
INJECT(OpenwrtControlServer (const CuttlefishConfig & config,const CuttlefishConfig::EnvironmentSpecific & environment,GrpcSocketCreator & grpc_socket))36   INJECT(OpenwrtControlServer(
37       const CuttlefishConfig& config,
38       const CuttlefishConfig::EnvironmentSpecific& environment,
39       GrpcSocketCreator& grpc_socket))
40       : config_(config), environment_(environment), grpc_socket_(grpc_socket) {}
41 
42   // CommandSource
Commands()43   Result<std::vector<MonitorCommand>> Commands() override {
44     // TODO(b/288987294) Remove dependency to first_instance config when moving
45     // OpenWrt to run_env is completed.
46     auto first_instance = config_.Instances()[0];
47 
48     Command openwrt_control_server_cmd(OpenwrtControlServerBinary());
49     openwrt_control_server_cmd.AddParameter(
50         "--grpc_uds_path=", grpc_socket_.CreateGrpcSocket(Name()));
51     openwrt_control_server_cmd.AddParameter(
52         "--bridged_wifi_tap=",
53         std::to_string(first_instance.use_bridged_wifi_tap()));
54     openwrt_control_server_cmd.AddParameter("--webrtc_device_id=",
55                                             first_instance.webrtc_device_id());
56     openwrt_control_server_cmd.AddParameter("--launcher_log_path=",
57                                             first_instance.launcher_log_path());
58     openwrt_control_server_cmd.AddParameter(
59         "--openwrt_log_path=",
60         AbsolutePath(first_instance.PerInstanceLogPath("crosvm_openwrt.log")));
61 
62     std::vector<MonitorCommand> commands;
63     commands.emplace_back(std::move(openwrt_control_server_cmd));
64     return commands;
65   }
66 
67   // SetupFeature
Name() const68   std::string Name() const override { return "OpenwrtControlServer"; }
Enabled() const69   bool Enabled() const override { return environment_.enable_wifi(); }
70 
71  private:
Dependencies() const72   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()73   Result<void> ResultSetup() override { return {}; }
74 
75   const CuttlefishConfig& config_;
76   const CuttlefishConfig::EnvironmentSpecific& environment_;
77   GrpcSocketCreator& grpc_socket_;
78 };
79 
80 }  // namespace
81 
82 fruit::Component<fruit::Required<const CuttlefishConfig,
83                                  const CuttlefishConfig::EnvironmentSpecific,
84                                  GrpcSocketCreator>>
OpenwrtControlServerComponent()85 OpenwrtControlServerComponent() {
86   return fruit::createComponent()
87       .addMultibinding<CommandSource, OpenwrtControlServer>()
88       .addMultibinding<SetupFeature, OpenwrtControlServer>();
89 }
90 
91 }  // namespace cuttlefish
92