• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2019 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 #define UCI_HEADER_SIZE 4
19 #define UCI_MAX_PAYLOAD_SIZE 255
20 #define UCI_MAX_PACKET_SIZE (UCI_HEADER_SIZE + UCI_MAX_PAYLOAD_SIZE)
21 
22 constexpr const size_t kBufferSize = UCI_MAX_PACKET_SIZE * 2;
23 
24 namespace cuttlefish {
25 namespace {
26 
27 class UwbConnector : public CommandSource {
28  public:
INJECT(UwbConnector (const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance))29   INJECT(UwbConnector(const CuttlefishConfig& config,
30                             const CuttlefishConfig::InstanceSpecific& instance))
31       : config_(config), instance_(instance) {}
32 
33   // CommandSource
Commands()34   Result<std::vector<MonitorCommand>> Commands() override {
35     Command command(HostBinaryPath("tcp_connector"));
36     command.AddParameter("-fifo_out=", fifos_[0]);
37     command.AddParameter("-fifo_in=", fifos_[1]);
38     command.AddParameter("-data_port=", config_.pica_uci_port());
39     command.AddParameter("-buffer_size=", kBufferSize);
40     std::vector<MonitorCommand> commands;
41     commands.emplace_back(std::move(command));
42     return commands;
43   }
44 
45   // SetupFeature
Name() const46   std::string Name() const override { return "UwbConnector"; }
Enabled() const47   bool Enabled() const override { return config_.enable_host_uwb_connector(); }
48 
49  private:
Dependencies() const50   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()51   Result<void> ResultSetup() {
52     std::vector<std::string> fifo_paths = {
53         instance_.PerInstanceInternalPath("uwb_fifo_vm.in"),
54         instance_.PerInstanceInternalPath("uwb_fifo_vm.out"),
55     };
56     for (const auto& path : fifo_paths) {
57       unlink(path.c_str());
58       CF_EXPECT(mkfifo(path.c_str(), 0660) == 0, "Could not create " << path);
59       auto fd = SharedFD::Open(path, O_RDWR);
60       CF_EXPECT(fd->IsOpen(),
61                 "Could not open " << path << ": " << fd->StrError());
62       fifos_.push_back(fd);
63     }
64     return {};
65   }
66 
67  private:
68   const CuttlefishConfig& config_;
69   const CuttlefishConfig::InstanceSpecific& instance_;
70   std::vector<SharedFD> fifos_;
71 };
72 
73 }  // namespace
74 
75 fruit::Component<fruit::Required<const CuttlefishConfig,
76                                  const CuttlefishConfig::InstanceSpecific>>
UwbConnectorComponent()77 UwbConnectorComponent() {
78   return fruit::createComponent()
79       .addMultibinding<CommandSource, UwbConnector>()
80       .addMultibinding<SetupFeature, UwbConnector>();
81 }
82 
83 }  // namespace cuttlefish
84