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 #include <string>
19 #include <unordered_set>
20 #include <utility>
21 #include <vector>
22 
23 #include <fruit/fruit.h>
24 
25 #include "common/libs/utils/result.h"
26 #include "host/libs/config/command_source.h"
27 #include "host/libs/config/known_paths.h"
28 
29 // Copied from net/bluetooth/hci.h
30 #define HCI_MAX_ACL_SIZE 1024
31 #define HCI_MAX_FRAME_SIZE (HCI_MAX_ACL_SIZE + 4)
32 
33 // Include H4 header byte, and reserve more buffer size in the case of excess
34 // packet.
35 constexpr const size_t kBufferSize = (HCI_MAX_FRAME_SIZE + 1) * 2;
36 
37 namespace cuttlefish {
38 namespace {
39 
40 class BluetoothConnector : public CommandSource {
41  public:
INJECT(BluetoothConnector (const CuttlefishConfig & config,const CuttlefishConfig::InstanceSpecific & instance))42   INJECT(BluetoothConnector(const CuttlefishConfig& config,
43                             const CuttlefishConfig::InstanceSpecific& instance))
44       : config_(config), instance_(instance) {}
45 
46   // CommandSource
Commands()47   Result<std::vector<MonitorCommand>> Commands() override {
48     Command command(TcpConnectorBinary());
49     command.AddParameter("-fifo_out=", fifos_[0]);
50     command.AddParameter("-fifo_in=", fifos_[1]);
51     command.AddParameter("-data_port=", config_.rootcanal_hci_port());
52     command.AddParameter("-buffer_size=", kBufferSize);
53     std::vector<MonitorCommand> commands;
54     commands.emplace_back(std::move(command));
55     return commands;
56   }
57 
58   // SetupFeature
Name() const59   std::string Name() const override { return "BluetoothConnector"; }
Enabled() const60   bool Enabled() const override { return config_.enable_host_bluetooth_connector(); }
61 
62  private:
Dependencies() const63   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()64   Result<void> ResultSetup() {
65     std::vector<std::string> fifo_paths = {
66         instance_.PerInstanceInternalPath("bt_fifo_vm.in"),
67         instance_.PerInstanceInternalPath("bt_fifo_vm.out"),
68     };
69     for (const auto& path : fifo_paths) {
70       unlink(path.c_str());
71       CF_EXPECT(mkfifo(path.c_str(), 0660) == 0, "Could not create " << path);
72       auto fd = SharedFD::Open(path, O_RDWR);
73       CF_EXPECT(fd->IsOpen(),
74                 "Could not open " << path << ": " << fd->StrError());
75       fifos_.push_back(fd);
76     }
77     return {};
78   }
79 
80  private:
81   const CuttlefishConfig& config_;
82   const CuttlefishConfig::InstanceSpecific& instance_;
83   std::vector<SharedFD> fifos_;
84 };
85 
86 }  // namespace
87 
88 fruit::Component<fruit::Required<const CuttlefishConfig,
89                                  const CuttlefishConfig::InstanceSpecific>>
BluetoothConnectorComponent()90 BluetoothConnectorComponent() {
91   return fruit::createComponent()
92       .addMultibinding<CommandSource, BluetoothConnector>()
93       .addMultibinding<SetupFeature, BluetoothConnector>();
94 }
95 
96 }  // namespace cuttlefish
97