• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 #include <string>
18 #include <thread>
19 
20 #include <gflags/gflags.h>
21 #include <glog/logging.h>
22 
23 #include "common/libs/fs/shared_fd.h"
24 #include "common/libs/fs/shared_select.h"
25 #include "host/libs/config/cuttlefish_config.h"
26 #include "host/commands/virtual_usb_manager/usbip/server.h"
27 #include "host/commands/virtual_usb_manager/vadb/virtual_adb_server.h"
28 
29 DEFINE_int32(
30     usb_v1_fd, -1,
31     "A file descriptor pointing to the USB v1 open socket or -1 to create it");
32 
main(int argc,char ** argv)33 int main(int argc, char** argv) {
34   ::android::base::InitLogging(argv, android::base::StderrLogger);
35   google::ParseCommandLineFlags(&argc, &argv, true);
36 
37   auto config = vsoc::CuttlefishConfig::Get();
38   if (!config) {
39     LOG(ERROR) << "Unable to get config object";
40     return 1;
41   }
42 
43   cvd::SharedFD usb_v1_server;
44 
45   if (FLAGS_usb_v1_fd < 0) {
46     auto socket_name = config->usb_v1_socket_name();
47     LOG(INFO) << "Starting server at " << socket_name;
48     usb_v1_server = cvd::SharedFD::SocketLocalServer(socket_name.c_str(), false,
49                                                      SOCK_STREAM, 0666);
50   } else {
51     usb_v1_server = cvd::SharedFD::Dup(FLAGS_usb_v1_fd);
52   }
53 
54   if (!usb_v1_server->IsOpen()) {
55     LOG(ERROR) << "Error openning USB v1 server: " << usb_v1_server->StrError();
56     return 2;
57   }
58 
59   vadb::VirtualADBServer adb_{usb_v1_server, config->vhci_port(),
60                               config->usb_ip_socket_name()};
61   vadb::usbip::Server usbip_{config->usb_ip_socket_name(), adb_.Pool()};
62 
63   CHECK(usbip_.Init()) << "Could not start USB/IP server";
64 
65   for (;;) {
66     cvd::SharedFDSet fd_read;
67     fd_read.Zero();
68 
69     adb_.BeforeSelect(&fd_read);
70     usbip_.BeforeSelect(&fd_read);
71 
72     int ret = cvd::Select(&fd_read, nullptr, nullptr, nullptr);
73     if (ret <= 0) continue;
74 
75     adb_.AfterSelect(fd_read);
76     usbip_.AfterSelect(fd_read);
77   }
78 
79   return 0;
80 }
81