Remove the USB code that we will not be porting
Test: TreeHugger (not built)
Bug: 68030162
Change-Id: Iab22212f32158c5cb5c7c8c9ae2b0b9868bec610
diff --git a/host/vadb/BUILD b/host/vadb/BUILD
deleted file mode 100644
index 26a5dd4..0000000
--- a/host/vadb/BUILD
+++ /dev/null
@@ -1,39 +0,0 @@
-cc_binary(
- name = "adbshell",
- srcs = [
- "adbshell.c",
- ],
-)
-
-cc_library(
- name = "vadb_lib",
- srcs = [
- "usb_cmd.h",
- "usb_cmd_attach.cpp",
- "usb_cmd_attach.h",
- "usb_cmd_control_transfer.cpp",
- "usb_cmd_control_transfer.h",
- "usb_cmd_data_transfer.cpp",
- "usb_cmd_data_transfer.h",
- "usb_cmd_device_list.cpp",
- "usb_cmd_device_list.h",
- "usb_cmd_heartbeat.cpp",
- "usb_cmd_heartbeat.h",
- "virtual_adb_client.cpp",
- "virtual_adb_client.h",
- "virtual_adb_server.cpp",
- "virtual_adb_server.h",
- ],
- visibility = ["//visibility:public"],
- linkopts = [
- "-ludev",
- ],
- deps = [
- "@cuttlefish//common/libs/fs",
- "@cuttlefish//common/libs/usbforward:protocol",
- "@gflags_repo//:gflags",
- "@glog_repo//:glog",
- "//host/vadb/usbip:usbip_lib",
- ],
-)
-
diff --git a/host/vadb/README.md b/host/vadb/README.md
deleted file mode 100644
index 432fa2e..0000000
--- a/host/vadb/README.md
+++ /dev/null
@@ -1,35 +0,0 @@
-# Virtual ADB
-
-VirtualADB serves the purpose of making Cuttlefish device available locally as a
-USB device. VirtualADB uses USB/IP protocol to forward USB gadget from
-Cuttlefish to localhost.
-
-## Requirements
-
-To compile the VirtualADB package you need to install:
-
-```
-sudo apt-get install libudev-dev
-```
-
-VirtualADB requires `vhci-hcd` kernel module to be loaded. Module is part of
-kernel extra modules package:
-
-```
-sudo apt-get install linux-image-extra-`uname -r`
-```
-
-## Usage
-
-VirtualADB uses currently `virtio channel` to communicate with usb forwarder on
-`cuttlefish`. The tool instruments kernel to attach remote USB device directly.
-To do that, it requires super-user privileges - primarily because it's adding a
-new device to your system.
-
-To start VirtualADB simply execute:
-
-```
-sudo vadb /path/to/usb_forwarder_socket
-```
-
-where `usb_forwarder_socket` is the socket used by usb forwarder to communicate.
diff --git a/host/vadb/adbshell.c b/host/vadb/adbshell.c
deleted file mode 100644
index 0fe58c0..0000000
--- a/host/vadb/adbshell.c
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Utility that uses an adb connection as the login shell. */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-int main(int argc, char* argv[]) {
- char** new_argv = malloc((argc + 5) * sizeof(char*));
- new_argv[0] = "/usr/bin/adb";
- new_argv[1] = "-s";
- new_argv[2] = "CUTTLEFISHAVD01";
- new_argv[3] = "shell";
- new_argv[4] = "/system/bin/sh";
-
- // Some important data is lost before this point, and there are
- // no great recovery options:
- // * ssh with no arguments comes in with 1 arg of -adbshell. The command
- // given above does the right thing if we don't invoke the shell.
- if (argc == 1) {
- new_argv[4] = 0;
- }
- // * simple shell commands come in with a -c and a single string. The
- // problem here is that adb doesn't preserve spaces, so we need
- // to do additional escaping. The best compromise seems to be to
- // throw double quotes around each string.
- for (int i = 1; i < argc; ++i) {
- size_t buf_size = strlen(argv[i]) + 4;
- new_argv[i + 4] = malloc(buf_size);
- snprintf(new_argv[i + 4], buf_size, "\"%s\"", argv[i]);
- }
- //
- // * scp seems to be pathologically broken when paths contain spaces.
- // spaces aren't properly escaped by gcloud, so scp will fail with
- // "scp: with ambiguous target." We might be able to fix this with
- // some creative parsing of the arguments, but that seems like
- // overkill.
- new_argv[argc + 4] = 0;
- execv(new_argv[0], new_argv);
- // This never should happen
- return 2;
-}
diff --git a/host/vadb/usb_cmd.h b/host/vadb/usb_cmd.h
deleted file mode 100644
index 9090f20..0000000
--- a/host/vadb/usb_cmd.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include "common/libs/fs/shared_fd.h"
-#include "common/libs/usbforward/protocol.h"
-
-namespace vadb {
-// USBCommand is an abstraction of a proxied USB command.
-// Instances of this object all share the following life cycle:
-// 1) A specific instance (COMMAND) is being created.
-// 2) Instance owner (OWNER) sends RequestHeader.
-// 3) OWNER calls COMMAND.OnRequest() to send any relevant, additional
-// information.
-// 4) OWNER queues COMMAND until response arrives.
-//
-// At this point instance owner can process next command in queue. Then,
-// eventually:
-//
-// 5) OWNER receives matching ResponseHeader.
-// 6) OWNER calls COMMAND.OnResponse(), supplying FD that carries additional
-// data.
-// 7) OWNER dequeues and deletes COMMAND.
-class USBCommand {
- public:
- USBCommand() = default;
- virtual ~USBCommand() = default;
-
- // Command returns a specific usbforward command ID associated with this
- // request.
- virtual usb_forward::Command Command() = 0;
-
- // OnRequest is called whenever additional data relevant to this command
- // (other than RequestHeader) should be sent.
- // Returns false, if communication with remote host failed (and should be
- // terminated).
- virtual bool OnRequest(const avd::SharedFD& data) = 0;
-
- // OnResponse is called whenever additional data relevant to this command
- // (other than ResponseHeader) should be received.
- // Returns false, if communication with remote host failed (and should be
- // terminated).
- virtual bool OnResponse(bool is_success, const avd::SharedFD& data) = 0;
-
- private:
- USBCommand(const USBCommand& other) = delete;
- USBCommand& operator=(const USBCommand& other) = delete;
-};
-
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_attach.cpp b/host/vadb/usb_cmd_attach.cpp
deleted file mode 100644
index 9c3dcda..0000000
--- a/host/vadb/usb_cmd_attach.cpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <glog/logging.h>
-
-#include "common/libs/usbforward/protocol.h"
-#include "host/vadb/usb_cmd_attach.h"
-
-namespace vadb {
-bool USBCmdAttach::OnRequest(const avd::SharedFD& fd) {
- if (fd->Write(&req_, sizeof(req_)) != sizeof(req_)) {
- LOG(ERROR) << "Short write: " << fd->StrError();
- return false;
- }
- return true;
-}
-
-bool USBCmdAttach::OnResponse(bool is_success, const avd::SharedFD& data) {
- if (!is_success) return false;
- LOG(INFO) << "Attach successful.";
- return true;
-}
-
-USBCmdAttach::USBCmdAttach(uint8_t bus_id, uint8_t dev_id) {
- req_.bus_id = bus_id;
- req_.dev_id = dev_id;
-}
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_attach.h b/host/vadb/usb_cmd_attach.h
deleted file mode 100644
index 7bb36c6..0000000
--- a/host/vadb/usb_cmd_attach.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include "host/vadb/usb_cmd.h"
-
-namespace vadb {
-// Request remote device attach (~open).
-class USBCmdAttach : public USBCommand {
- public:
- USBCmdAttach(uint8_t bus_id, uint8_t dev_id);
- ~USBCmdAttach() override = default;
-
- // Return usbforward command this instance is executing.
- usb_forward::Command Command() override { return usb_forward::CmdAttach; }
-
- // Send request body to the server.
- // Return false, if communication failed.
- bool OnRequest(const avd::SharedFD& data) override;
-
- // Receive response data from the server.
- // Return false, if communication failed.
- bool OnResponse(bool is_success, const avd::SharedFD& data) override;
-
- private:
- usb_forward::AttachRequest req_;
-
- USBCmdAttach(const USBCmdAttach& other) = delete;
- USBCmdAttach& operator=(const USBCmdAttach& other) = delete;
-};
-} // namespace vadb
\ No newline at end of file
diff --git a/host/vadb/usb_cmd_control_transfer.cpp b/host/vadb/usb_cmd_control_transfer.cpp
deleted file mode 100644
index 189c174..0000000
--- a/host/vadb/usb_cmd_control_transfer.cpp
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <glog/logging.h>
-
-#include "host/vadb/usb_cmd_control_transfer.h"
-
-namespace vadb {
-USBCmdControlTransfer::USBCmdControlTransfer(
- uint8_t bus_id, uint8_t dev_id, uint8_t type, uint8_t request,
- uint16_t value, uint16_t index, uint32_t timeout, std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback)
- : data_(std::move(data)), callback_(std::move(callback)) {
- req_.bus_id = bus_id;
- req_.dev_id = dev_id;
- req_.type = type;
- req_.cmd = request;
- req_.value = value;
- req_.index = index;
- req_.length = data_.size();
- req_.timeout = timeout;
-}
-
-bool USBCmdControlTransfer::OnRequest(const avd::SharedFD& fd) {
- if (fd->Write(&req_, sizeof(req_)) != sizeof(req_)) {
- LOG(ERROR) << "Short write: " << fd->StrError();
- return false;
- }
-
- if ((req_.type & 0x80) == 0) {
- if (data_.size() > 0) {
- if (fd->Write(data_.data(), data_.size()) != data_.size()) {
- LOG(ERROR) << "Short write: " << fd->StrError();
- return false;
- }
- }
- }
-
- return true;
-}
-
-bool USBCmdControlTransfer::OnResponse(bool is_success,
- const avd::SharedFD& fd) {
- if (!is_success) {
- callback_(false, std::move(data_));
- return true;
- }
-
- if (req_.type & 0x80) {
- int32_t len;
- if (fd->Read(&len, sizeof(len)) != sizeof(len)) {
- LOG(ERROR) << "Short read: " << fd->StrError();
- callback_(false, std::move(data_));
- return false;
- }
-
- if (len > 0) {
- data_.resize(len);
- if (fd->Read(data_.data(), len) != len) {
- LOG(ERROR) << "Short read: " << fd->StrError();
- callback_(false, std::move(data_));
- return false;
- }
- }
- }
-
- callback_(true, std::move(data_));
- return true;
-}
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_control_transfer.h b/host/vadb/usb_cmd_control_transfer.h
deleted file mode 100644
index c9ad2ac..0000000
--- a/host/vadb/usb_cmd_control_transfer.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <memory>
-
-#include <stdint.h>
-
-#include "common/libs/usbforward/protocol.h"
-#include "host/vadb/usb_cmd.h"
-#include "host/vadb/usbip/device.h"
-
-namespace vadb {
-// Execute control transfer.
-class USBCmdControlTransfer : public USBCommand {
- public:
- USBCmdControlTransfer(uint8_t bus_id, uint8_t dev_id, uint8_t type,
- uint8_t request, uint16_t value, uint16_t index,
- uint32_t timeout, std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback);
-
- ~USBCmdControlTransfer() override = default;
-
- // Return usbforward command this instance is executing.
- usb_forward::Command Command() override {
- return usb_forward::CmdControlTransfer;
- }
-
- // Send request body to the server.
- // Return false, if communication failed.
- bool OnRequest(const avd::SharedFD& data) override;
-
- // Receive response data from the server.
- // Return false, if communication failed.
- bool OnResponse(bool is_success, const avd::SharedFD& data) override;
-
- private:
- usb_forward::ControlTransfer req_;
- std::vector<uint8_t> data_;
- usbip::Device::AsyncTransferReadyCB callback_;
-
- USBCmdControlTransfer(const USBCmdControlTransfer& other) = delete;
- USBCmdControlTransfer& operator=(const USBCmdControlTransfer& other) = delete;
-};
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_data_transfer.cpp b/host/vadb/usb_cmd_data_transfer.cpp
deleted file mode 100644
index 3dcfacd..0000000
--- a/host/vadb/usb_cmd_data_transfer.cpp
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <glog/logging.h>
-
-#include "host/vadb/usb_cmd_data_transfer.h"
-
-namespace vadb {
-USBCmdDataTransfer::USBCmdDataTransfer(
- uint8_t bus_id, uint8_t dev_id, uint8_t endpoint, bool is_host_to_device,
- uint32_t deadline, std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback)
- : data_(std::move(data)), callback_(std::move(callback)) {
- req_.bus_id = bus_id;
- req_.dev_id = dev_id;
- req_.endpoint_id = endpoint;
- req_.is_host_to_device = is_host_to_device;
- req_.length = data_.size();
- req_.timeout = deadline;
-}
-
-bool USBCmdDataTransfer::OnRequest(const avd::SharedFD& fd) {
- if (fd->Write(&req_, sizeof(req_)) != sizeof(req_)) {
- LOG(ERROR) << "Short write: " << fd->StrError();
- return false;
- }
-
- if (req_.is_host_to_device && data_.size() > 0) {
- if (fd->Write(data_.data(), data_.size()) != data_.size()) {
- LOG(ERROR) << "Short write: " << fd->StrError();
- return false;
- }
- }
-
- return true;
-}
-
-bool USBCmdDataTransfer::OnResponse(bool is_success, const avd::SharedFD& fd) {
- if (!is_success) {
- callback_(false, std::move(data_));
- return true;
- }
-
- if (!req_.is_host_to_device) {
- int32_t len;
- if (fd->Read(&len, sizeof(len)) != sizeof(len)) {
- LOG(ERROR) << "Short read: " << fd->StrError();
- callback_(false, std::move(data_));
- return false;
- }
-
- if (len > 0) {
- data_.resize(len);
- int32_t got = 0;
- // Virtio sends data in 32k packets. We may have to do a few reads.
- while (got < len) {
- auto packetsize = fd->Read(&data_[got], len - got);
- got += packetsize;
-
- if (fd->GetErrno() != 0) {
- // This could, technically, also be a disconnect.
- LOG(ERROR) << "Read failed: " << fd->StrError();
- return false;
- } else if (packetsize == 0) {
- LOG(ERROR) << "Short read; remote end disconnected.";
- return false;
- }
- }
- }
- }
-
- callback_(true, std::move(data_));
- return true;
-}
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_data_transfer.h b/host/vadb/usb_cmd_data_transfer.h
deleted file mode 100644
index f07b0ab..0000000
--- a/host/vadb/usb_cmd_data_transfer.h
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <memory>
-#include <stdint.h>
-
-#include "common/libs/usbforward/protocol.h"
-#include "host/vadb/usb_cmd.h"
-#include "host/vadb/usbip/device.h"
-
-namespace vadb {
-// Execute control transfer.
-class USBCmdDataTransfer : public USBCommand {
- public:
- USBCmdDataTransfer(uint8_t bus_id, uint8_t dev_id, uint8_t endpoint,
- bool is_host_to_device, uint32_t timeout,
- std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback);
-
- ~USBCmdDataTransfer() override = default;
-
- // Return usbforward command this instance is executing.
- usb_forward::Command Command() override {
- return usb_forward::CmdDataTransfer;
- }
-
- // Send request body to the server.
- // Return false, if communication failed.
- bool OnRequest(const avd::SharedFD& data) override;
-
- // Receive response data from the server.
- // Return false, if communication failed.
- bool OnResponse(bool is_success, const avd::SharedFD& data) override;
-
- private:
- usb_forward::DataTransfer req_;
- std::vector<uint8_t> data_;
- usbip::Device::AsyncTransferReadyCB callback_;
-
- USBCmdDataTransfer(const USBCmdDataTransfer& other) = delete;
- USBCmdDataTransfer& operator=(const USBCmdDataTransfer& other) = delete;
-};
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_device_list.cpp b/host/vadb/usb_cmd_device_list.cpp
deleted file mode 100644
index 03ccb7c..0000000
--- a/host/vadb/usb_cmd_device_list.cpp
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <glog/logging.h>
-
-#include "host/vadb/usb_cmd_device_list.h"
-
-namespace vadb {
-bool USBCmdDeviceList::OnRequest(const avd::SharedFD& data) {
- LOG(INFO) << "Requesting device list from Cuttlefish...";
- // No action required.
- return true;
-}
-
-bool USBCmdDeviceList::OnResponse(bool is_success, const avd::SharedFD& fd) {
- // This should never happen. If this command fails, something is very wrong.
- if (!is_success) return false;
-
- int32_t count;
- if (fd->Read(&count, sizeof(count)) != sizeof(count)) {
- LOG(ERROR) << "Short read: " << fd->StrError();
- return false;
- }
-
- LOG(INFO) << "Device list completed with " << count << " devices.";
-
- while (count-- > 0) {
- usb_forward::DeviceInfo dev;
- std::vector<usb_forward::InterfaceInfo> ifaces;
-
- if (fd->Read(&dev, sizeof(dev)) != sizeof(dev)) {
- LOG(ERROR) << "Short read: " << fd->StrError();
- return false;
- }
-
- ifaces.resize(dev.num_interfaces);
- if (fd->Read(ifaces.data(),
- ifaces.size() * sizeof(usb_forward::InterfaceInfo)) !=
- ifaces.size() * sizeof(usb_forward::InterfaceInfo)) {
- LOG(ERROR) << "Short read: " << fd->StrError();
- return false;
- }
-
- LOG(INFO) << "Found remote device 0x" << std::hex << dev.vendor_id << ":"
- << dev.product_id;
-
- on_device_discovered_(dev, ifaces);
- }
-
- return true;
-}
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_device_list.h b/host/vadb/usb_cmd_device_list.h
deleted file mode 100644
index 911db81..0000000
--- a/host/vadb/usb_cmd_device_list.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <functional>
-#include <vector>
-#include "common/libs/usbforward/protocol.h"
-#include "host/vadb/usb_cmd.h"
-
-namespace vadb {
-// Request device list from remote host.
-class USBCmdDeviceList : public USBCommand {
- public:
- // DeviceDiscoveredCB is a callback function invoked for every new discovered
- // device.
- using DeviceDiscoveredCB =
- std::function<void(const usb_forward::DeviceInfo&,
- const std::vector<usb_forward::InterfaceInfo>&)>;
-
- USBCmdDeviceList(DeviceDiscoveredCB cb)
- : on_device_discovered_(std::move(cb)) {}
-
- ~USBCmdDeviceList() override = default;
-
- // Return usbforward command this instance is executing.
- usb_forward::Command Command() override { return usb_forward::CmdDeviceList; }
-
- // Send request body to the server.
- // Return false, if communication failed.
- bool OnRequest(const avd::SharedFD& data) override;
-
- // Receive response data from the server.
- // Return false, if communication failed.
- bool OnResponse(bool is_success, const avd::SharedFD& data) override;
-
- private:
- DeviceDiscoveredCB on_device_discovered_;
- USBCmdDeviceList(const USBCmdDeviceList& other) = delete;
- USBCmdDeviceList& operator=(const USBCmdDeviceList& other) = delete;
-};
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_heartbeat.cpp b/host/vadb/usb_cmd_heartbeat.cpp
deleted file mode 100644
index 8584de6..0000000
--- a/host/vadb/usb_cmd_heartbeat.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <glog/logging.h>
-
-#include "common/libs/usbforward/protocol.h"
-#include "host/vadb/usb_cmd_heartbeat.h"
-
-namespace vadb {
-bool USBCmdHeartbeat::OnRequest(const avd::SharedFD& fd) { return true; }
-
-bool USBCmdHeartbeat::OnResponse(bool is_success, const avd::SharedFD& data) {
- callback_(is_success);
- return true;
-}
-
-USBCmdHeartbeat::USBCmdHeartbeat(USBCmdHeartbeat::HeartbeatResultCB callback)
- : callback_(callback) {}
-} // namespace vadb
diff --git a/host/vadb/usb_cmd_heartbeat.h b/host/vadb/usb_cmd_heartbeat.h
deleted file mode 100644
index 894c5f2..0000000
--- a/host/vadb/usb_cmd_heartbeat.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include "host/vadb/usb_cmd.h"
-
-namespace vadb {
-// Request remote device attach (~open).
-class USBCmdHeartbeat : public USBCommand {
- public:
- // Heartbeat result callback receives a boolean argument indicating whether
- // remote device is ready to be attached.
- using HeartbeatResultCB = std::function<void(bool)>;
-
- USBCmdHeartbeat(HeartbeatResultCB callback);
- ~USBCmdHeartbeat() override = default;
-
- // Return usbforward command this instance is executing.
- usb_forward::Command Command() override { return usb_forward::CmdHeartbeat; }
-
- // Send request body to the server.
- // Return false, if communication failed.
- bool OnRequest(const avd::SharedFD& data) override;
-
- // Receive response data from the server.
- // Return false, if communication failed.
- bool OnResponse(bool is_success, const avd::SharedFD& data) override;
-
- private:
- HeartbeatResultCB callback_;
-
- USBCmdHeartbeat(const USBCmdHeartbeat& other) = delete;
- USBCmdHeartbeat& operator=(const USBCmdHeartbeat& other) = delete;
-};
-} // namespace vadb
diff --git a/host/vadb/usbip/BUILD b/host/vadb/usbip/BUILD
deleted file mode 100644
index dcbd6fc..0000000
--- a/host/vadb/usbip/BUILD
+++ /dev/null
@@ -1,33 +0,0 @@
-cc_library(
- name = "usbip_lib",
- srcs = [
- "client.cpp",
- "client.h",
- "device.h",
- "device_pool.cpp",
- "device_pool.h",
- "messages.cpp",
- "messages.h",
- "server.cpp",
- "server.h",
- "vhci_instrument.cpp",
- "vhci_instrument.h",
- ],
- hdrs = [
- "client.h",
- "device.h",
- "device_pool.h",
- "messages.h",
- "server.h",
- ],
- deps = [
- "@cuttlefish//common/libs/fs",
- "@gflags_repo//:gflags",
- "@glog_repo//:glog",
- ],
- linkopts = [
- "-ludev",
- ],
- visibility = [ "//visibility:public" ]
-)
-
diff --git a/host/vadb/usbip/README.md b/host/vadb/usbip/README.md
deleted file mode 100644
index e652352..0000000
--- a/host/vadb/usbip/README.md
+++ /dev/null
@@ -1,36 +0,0 @@
-# USB/IP server library
-
-This folder contains set of classes and structures that constitute basic USB/IP
-server.
-
-Protocol used in this library is defined as part of
-[Linux kernel documentation](https://www.kernel.org/doc/Documentation/usb/usbip_protocol.txt).
-
-## Structure
-
-### [`vadb::usbip::Device`](./device.h)[](#Device)
-
-Structure describing individual device accessible over USB/IP protocol.
-
-### [`vadb::usbip::DevicePool`](./device_pool.h)[](#DevicePool)
-
-DevicePool holds a set of [Devices](#Device) that can be enumerated and
-accessed by clients of this Server.
-
-### [`vadb::usbip::Server`](./server.h)
-
-Purpose of this class is to start a new listening socket and accept incoming
-USB/IP connections & requests.
-
-### [`vadb::usbip::Client`](./client.h)
-
-Client class represents individual USB/IP connection. Client enables remote
-USB/IP client to enumerate and access devices registered in
-[DevicePool](#DevicePool).
-
-### [`USB/IP Messages`](./messages.h)
-
-This file contains structures and enum values defined by the USB/IP protocol.
-All definitions found there have been collected from
-[Linux kernel documentation](https://www.kernel.org/doc/Documentation/usb/usbip_protocol.txt)
-.
diff --git a/host/vadb/usbip/client.cpp b/host/vadb/usbip/client.cpp
deleted file mode 100644
index 317e9e4..0000000
--- a/host/vadb/usbip/client.cpp
+++ /dev/null
@@ -1,211 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "host/vadb/usbip/client.h"
-
-#include <glog/logging.h>
-#include <iostream>
-
-#include "host/vadb/usbip/device.h"
-#include "host/vadb/usbip/messages.h"
-
-namespace vadb {
-namespace usbip {
-
-void Client::BeforeSelect(avd::SharedFDSet* fd_read) const {
- fd_read->Set(fd_);
-}
-
-bool Client::AfterSelect(const avd::SharedFDSet& fd_read) {
- if (fd_read.IsSet(fd_)) return HandleIncomingMessage();
- return true;
-}
-
-// Handle incoming COMMAND.
-//
-// Read next CMD from client channel.
-// Returns false, if connection should be dropped.
-bool Client::HandleIncomingMessage() {
- CmdHeader hdr;
- if (!RecvUSBIPMsg(fd_, &hdr)) {
- LOG(ERROR) << "Could not read command header: " << fd_->StrError();
- return false;
- }
-
- // And the protocol, again.
- switch (hdr.command) {
- case kUsbIpCmdReqSubmit:
- return HandleSubmitCmd(hdr);
-
- case kUsbIpCmdReqUnlink:
- return HandleUnlinkCmd(hdr);
-
- default:
- LOG(ERROR) << "Unsupported command requested: " << hdr.command;
- return false;
- }
-}
-
-// Handle incoming SUBMIT COMMAND.
-//
-// Execute command on specified USB device.
-// Returns false, if connection should be dropped.
-bool Client::HandleSubmitCmd(const CmdHeader& cmd) {
- CmdReqSubmit req;
- if (!RecvUSBIPMsg(fd_, &req)) {
- LOG(ERROR) << "Could not read submit command: " << fd_->StrError();
- return false;
- }
-
- uint32_t seq_num = cmd.seq_num;
-
- // Reserve buffer for data in or out.
- std::vector<uint8_t> payload;
- int payload_length = req.transfer_buffer_length;
- payload.resize(payload_length);
-
- bool is_host_to_device = cmd.direction == kUsbIpDirectionOut;
- // Control requests are quite easy to detect; if setup is all '0's, then we're
- // doing a data transfer, otherwise it's a control transfer.
- // We only check for cmd and type fields here, as combination 0/0 of these
- // fields is already invalid (cmd == GET_STATUS, type = WRITE).
- bool is_control_request = !(req.setup.cmd == 0 && req.setup.type == 0);
-
- // Find requested device and execute command.
- auto device = pool_.GetDevice({cmd.bus_num, cmd.dev_num});
- if (device) {
- // Read data to be sent to device, if specified.
- if (is_host_to_device && payload_length) {
- int32_t got = 0;
- // Make sure we read everything.
- while (got < payload.size()) {
- auto read =
- fd_->Recv(&payload[got], payload.size() - got, MSG_NOSIGNAL);
- if (fd_->GetErrno() != 0) {
- LOG(ERROR) << "Client disconnected: " << fd_->StrError();
- return false;
- } else if (!read) {
- LOG(ERROR) << "Short read; client likely disconnected.";
- return false;
- }
- got += read;
- }
- }
-
- // If setup structure of request is initialized then we need to execute
- // control transfer. Otherwise, this is a plain data exchange.
- bool send_success = false;
- if (is_control_request) {
- send_success = device->handle_control_transfer(
- req.setup, req.deadline_interval, std::move(payload),
- [this, seq_num, is_host_to_device](bool is_success,
- std::vector<uint8_t> data) {
- HandleAsyncDataReady(seq_num, is_success, is_host_to_device,
- std::move(data));
- });
- } else {
- send_success = device->handle_data_transfer(
- cmd.endpoint, is_host_to_device, req.deadline_interval,
- std::move(payload),
- [this, seq_num, is_host_to_device](bool is_success,
- std::vector<uint8_t> data) {
- HandleAsyncDataReady(seq_num, is_success, is_host_to_device,
- std::move(data));
- });
- }
-
- // Simply fail if couldn't execute command.
- if (!send_success) {
- HandleAsyncDataReady(seq_num, false, is_host_to_device,
- std::vector<uint8_t>());
- }
- }
- return true;
-}
-
-void Client::HandleAsyncDataReady(uint32_t seq_num, bool is_success,
- bool is_host_to_device,
- std::vector<uint8_t> data) {
- // Response template.
- // - in header, host doesn't care about anything else except for command type
- // and sequence number.
- // - in body, report status == !OK unless we completed everything
- // successfully.
- CmdHeader rephdr{};
- rephdr.command = kUsbIpCmdRepSubmit;
- rephdr.seq_num = seq_num;
-
- CmdRepSubmit rep{};
- rep.status = is_success ? 0 : 1;
- rep.actual_length = data.size();
-
- // Data out.
- if (!SendUSBIPMsg(fd_, rephdr)) {
- LOG(ERROR) << "Failed to send response header: " << fd_->StrError();
- return;
- }
-
- if (!SendUSBIPMsg(fd_, rep)) {
- LOG(ERROR) << "Failed to send response body: " << fd_->StrError();
- return;
- }
-
- if (!is_host_to_device && data.size() > 0) {
- if (fd_->Send(data.data(), data.size(), MSG_NOSIGNAL) != data.size()) {
- LOG(ERROR) << "Failed to send response payload: " << fd_->StrError();
- return;
- }
- }
-}
-
-// Handle incoming UNLINK COMMAND.
-//
-// Unlink removes command specified via seq_num from a list of commands to be
-// executed.
-// We don't schedule commands for execution, so technically every UNLINK will
-// come in late.
-// Returns false, if connection should be dropped.
-bool Client::HandleUnlinkCmd(const CmdHeader& cmd) {
- CmdReqUnlink req;
- if (!RecvUSBIPMsg(fd_, &req)) {
- LOG(ERROR) << "Could not read unlink command: " << fd_->StrError();
- return false;
- }
- LOG(INFO) << "Client requested to unlink previously submitted command: "
- << req.seq_num;
-
- CmdHeader rephdr{};
- rephdr.command = kUsbIpCmdRepUnlink;
- rephdr.seq_num = cmd.seq_num;
-
- // Technically we do not schedule commands for execution, so we cannot
- // de-queue commands, either. Indicate this by sending status != ok.
- CmdRepUnlink rep;
- rep.status = 1;
-
- if (!SendUSBIPMsg(fd_, rephdr)) {
- LOG(ERROR) << "Could not send unlink command header: " << fd_->StrError();
- return false;
- }
-
- if (!SendUSBIPMsg(fd_, rep)) {
- LOG(ERROR) << "Could not send unlink command data: " << fd_->StrError();
- return false;
- }
- return true;
-}
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/client.h b/host/vadb/usbip/client.h
deleted file mode 100644
index 577524b..0000000
--- a/host/vadb/usbip/client.h
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include "common/libs/fs/shared_fd.h"
-#include "common/libs/fs/shared_select.h"
-#include "host/vadb/usbip/device_pool.h"
-#include "host/vadb/usbip/messages.h"
-
-namespace vadb {
-namespace usbip {
-
-// Represents USB/IP client, or individual connection to our USB/IP server.
-// Multiple clients are allowed, even if practically we anticipate only one
-// connection at the time.
-class Client final {
- public:
- Client(const DevicePool& pool, const avd::SharedFD& fd)
- : pool_(pool), fd_(fd) {}
-
- ~Client() {}
-
- // BeforeSelect is Called right before Select() to populate interesting
- // SharedFDs.
- void BeforeSelect(avd::SharedFDSet* fd_read) const;
-
- // AfterSelect is Called right after Select() to detect and respond to changes
- // on affected SharedFDs.
- // Return value indicates whether this client is still valid.
- bool AfterSelect(const avd::SharedFDSet& fd_read);
-
- private:
- // Respond to message from remote client.
- // Returns false, if client violated protocol or disconnected, indicating,
- // that this instance should no longer be used.
- bool HandleIncomingMessage();
-
- // Execute command on USB device.
- // Returns false, if connection should be dropped.
- bool HandleSubmitCmd(const CmdHeader& hdr);
-
- // HandleAsyncDataReady is called asynchronously once previously submitted
- // data transfer (control or bulk) has completed (or failed).
- void HandleAsyncDataReady(uint32_t seq_num, bool is_success,
- bool is_host_to_device, std::vector<uint8_t> data);
-
- // Unlink previously submitted message from device queue.
- // Returns false, if connection should be dropped.
- bool HandleUnlinkCmd(const CmdHeader& hdr);
-
- const DevicePool& pool_;
- avd::SharedFD fd_;
-
- Client(const Client&) = delete;
- Client& operator=(const Client&) = delete;
-};
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/device.h b/host/vadb/usbip/device.h
deleted file mode 100644
index 9764ed2..0000000
--- a/host/vadb/usbip/device.h
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <cstdint>
-#include <functional>
-#include <map>
-#include <memory>
-#include <vector>
-
-#include "host/vadb/usbip/messages.h"
-
-namespace vadb {
-namespace usbip {
-
-// The device descriptor of a USB device represents a USB device that is
-// available for import.
-class Device {
- public:
- // AsyncTransferReadyCB specifies a signature of a function that will be
- // called upon transfer completion (whether successful or failed). Parameters
- // supplied to the function are:
- // - operation status, indicated by boolean flag (true = success),
- // - vector containing transferred data (and actual size).
- using AsyncTransferReadyCB = std::function<void(bool, std::vector<uint8_t>)>;
-
- // Interface provides minimal description of device's interface.
- struct Interface {
- uint8_t iface_class;
- uint8_t iface_subclass;
- uint8_t iface_protocol;
- };
-
- // vendor_id and product_id identify device manufacturer and type.
- // dev_version describes device version (as BCD).
- uint16_t vendor_id;
- uint16_t product_id;
- uint16_t dev_version;
-
- // Class, Subclass and Protocol define device type.
- uint8_t dev_class;
- uint8_t dev_subclass;
- uint8_t dev_protocol;
-
- // Speed indicates device speed (see libusb_speed).
- uint8_t speed;
-
- // ConfigurationsCount and ConfigurationNumber describe total number of device
- // configurations and currently activated device configuration.
- size_t configurations_count;
- size_t configuration_number;
-
- // Interfaces returns a collection of device interfaces.
- std::vector<Interface> interfaces;
-
- // Attach request handler.
- std::function<bool()> handle_attach;
-
- // Device control request dispatcher.
- std::function<bool(const CmdRequest& request, uint32_t deadline,
- std::vector<uint8_t> data, AsyncTransferReadyCB callback)>
- handle_control_transfer;
-
- // Device data request dispatcher.
- std::function<bool(uint8_t endpoint, bool is_host_to_device,
- uint32_t deadline, std::vector<uint8_t> data,
- AsyncTransferReadyCB callback)>
- handle_data_transfer;
-};
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/device_pool.cpp b/host/vadb/usbip/device_pool.cpp
deleted file mode 100644
index a068be2..0000000
--- a/host/vadb/usbip/device_pool.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "host/vadb/usbip/device_pool.h"
-
-#include <glog/logging.h>
-
-namespace vadb {
-namespace usbip {
-
-void DevicePool::AddDevice(BusDevNumber bdn, std::unique_ptr<Device> device) {
- devices_[bdn] = std::move(device);
-}
-
-Device* DevicePool::GetDevice(BusDevNumber bus_id) const {
- auto iter = devices_.find(bus_id);
- if (iter == devices_.end()) return nullptr;
- return iter->second.get();
-}
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/device_pool.h b/host/vadb/usbip/device_pool.h
deleted file mode 100644
index 4a7c637..0000000
--- a/host/vadb/usbip/device_pool.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <map>
-#include <string>
-
-#include "host/vadb/usbip/device.h"
-
-namespace vadb {
-namespace usbip {
-// Container for all virtual USB/IP devices.
-// Stores devices by virtual BUS ID.
-class DevicePool {
- public:
- // BusDevNumber is a pair uniquely identifying bus and device.
- struct BusDevNumber {
- uint16_t bus_number;
- uint16_t dev_number;
-
- bool operator<(BusDevNumber other) const {
- return (bus_number << 16 | dev_number) <
- (other.bus_number << 16 | other.dev_number);
- }
- };
-
- // Internal container type.
- using MapType = std::map<BusDevNumber, std::unique_ptr<Device>>;
-
- DevicePool() = default;
- virtual ~DevicePool() = default;
-
- // Add new device associated with virtual BUS ID.
- void AddDevice(BusDevNumber bus_id, std::unique_ptr<Device> device);
-
- // Get device associated with supplied virtual bus/device number.
- Device* GetDevice(BusDevNumber bus_dev_num) const;
-
- // Get total number of USB/IP devices.
- size_t Size() const { return devices_.size(); }
-
- MapType::const_iterator begin() const { return devices_.cbegin(); }
- MapType::const_iterator end() const { return devices_.cend(); }
-
- private:
- MapType devices_;
-
- DevicePool(const DevicePool&) = delete;
- DevicePool& operator=(const DevicePool&) = delete;
-};
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/messages.cpp b/host/vadb/usbip/messages.cpp
deleted file mode 100644
index 0c57023..0000000
--- a/host/vadb/usbip/messages.cpp
+++ /dev/null
@@ -1,192 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "host/vadb/usbip/messages.h"
-
-#include <netinet/in.h>
-#include <iostream>
-
-#include <glog/logging.h>
-
-namespace vadb {
-namespace usbip {
-namespace {
-// Basic sanity checking.
-// We're using CmdHeader + CmdReq/Rep in case any of the fields is moved between
-// structures.
-constexpr int kUsbIpCmdLength = 48;
-
-static_assert(sizeof(CmdHeader) + sizeof(CmdReqSubmit) == kUsbIpCmdLength,
- "USB/IP command + header must be exactly 48 bytes.");
-static_assert(sizeof(CmdHeader) + sizeof(CmdRepSubmit) == kUsbIpCmdLength,
- "USB/IP command + header must be exactly 48 bytes.");
-static_assert(sizeof(CmdHeader) + sizeof(CmdReqUnlink) == kUsbIpCmdLength,
- "USB/IP command + header must be exactly 48 bytes.");
-static_assert(sizeof(CmdHeader) + sizeof(CmdRepUnlink) == kUsbIpCmdLength,
- "USB/IP command + header must be exactly 48 bytes.");
-} // namespace
-
-// NetToHost and HostToNet are used to reduce risk of copy/paste errors and to
-// provide uniform method of converting messages between different endian types.
-namespace internal {
-
-template <>
-void NetToHost(uint32_t* t) {
- *t = ntohl(*t);
-}
-
-template <>
-void NetToHost(Command* t) {
- *t = static_cast<Command>(ntohl(*t));
-}
-
-template <>
-void NetToHost(Direction* t) {
- *t = static_cast<Direction>(ntohl(*t));
-}
-
-template <>
-void NetToHost(uint16_t* t) {
- *t = ntohs(*t);
-}
-
-template <>
-void NetToHost(CmdHeader* t) {
- NetToHost(&t->command);
- NetToHost(&t->seq_num);
- NetToHost(&t->bus_num);
- NetToHost(&t->dev_num);
- NetToHost(&t->direction);
- NetToHost(&t->endpoint);
-}
-
-template <>
-void NetToHost(CmdReqSubmit* t) {
- NetToHost(&t->transfer_flags);
- NetToHost(&t->transfer_buffer_length);
- NetToHost(&t->start_frame);
- NetToHost(&t->number_of_packets);
- NetToHost(&t->deadline_interval);
-}
-
-template <>
-void NetToHost(CmdReqUnlink* t) {
- NetToHost(&t->seq_num);
-}
-
-template <>
-void HostToNet(uint32_t* t) {
- *t = htonl(*t);
-}
-
-template <>
-void HostToNet(Command* t) {
- *t = static_cast<Command>(htonl(*t));
-}
-
-template <>
-void HostToNet(Direction* t) {
- *t = static_cast<Direction>(htonl(*t));
-}
-
-template <>
-void HostToNet(uint16_t* t) {
- *t = htons(*t);
-}
-
-template <>
-void HostToNet(CmdHeader* t) {
- HostToNet(&t->command);
- HostToNet(&t->seq_num);
- HostToNet(&t->bus_num);
- HostToNet(&t->dev_num);
- HostToNet(&t->direction);
- HostToNet(&t->endpoint);
-}
-
-template <>
-void HostToNet(CmdRepSubmit* t) {
- HostToNet(&t->status);
- HostToNet(&t->actual_length);
- HostToNet(&t->start_frame);
- HostToNet(&t->number_of_packets);
- HostToNet(&t->error_count);
-}
-
-template <>
-void HostToNet(CmdRepUnlink* t) {
- HostToNet(&t->status);
-}
-
-} // namespace internal
-
-std::ostream& operator<<(std::ostream& out, const CmdHeader& header) {
- out << "CmdHeader\n";
- out << "\t\tcmd:\t" << header.command << '\n';
- out << "\t\tseq#:\t" << header.seq_num << '\n';
- out << "\t\tbus#:\t0x" << header.bus_num << '\n';
- out << "\t\tdev#:\t0x" << header.dev_num << '\n';
- out << "\t\tdir:\t" << (header.direction ? "in" : "out") << '\n';
- out << "\t\tendpt:\t" << header.endpoint << "\n";
- return out;
-}
-
-std::ostream& operator<<(std::ostream& out, const CmdRequest& setup) {
- out << "Request\n";
- out << "\t\t\ttype:\t" << std::hex << int(setup.type) << '\n';
- out << "\t\t\treq:\t" << int(setup.cmd) << std::dec << '\n';
- out << "\t\t\tval:\t" << setup.value << '\n';
- out << "\t\t\tidx:\t" << setup.index << '\n';
- out << "\t\t\tlen:\t" << setup.length << '\n';
- return out;
-}
-
-std::ostream& operator<<(std::ostream& out, const CmdReqSubmit& submit) {
- out << "CmdReqSubmit\n";
- out << "\t\ttr_flg:\t" << std::hex << submit.transfer_flags << std::dec
- << '\n';
- out << "\t\ttr_len:\t" << submit.transfer_buffer_length << '\n';
- out << "\t\tstart:\t" << submit.start_frame << '\n';
- out << "\t\tpktcnt:\t" << submit.number_of_packets << '\n';
- out << "\t\tttl:\t" << submit.deadline_interval << '\n';
- out << "\t\tsetup:\t" << submit.setup << '\n';
- return out;
-}
-
-std::ostream& operator<<(std::ostream& out, const CmdRepSubmit& submit) {
- out << "CmdRepSubmit\n";
- out << "\t\tstatus:\t" << submit.status << '\n';
- out << "\t\tlen:\t" << submit.actual_length << '\n';
- out << "\t\tstart:\t" << submit.start_frame << '\n';
- out << "\t\tpktcnt:\t" << submit.number_of_packets << '\n';
- out << "\t\terrors:\t" << submit.error_count << '\n';
- out << "\t\tsetup:\t" << submit.setup << '\n';
- return out;
-}
-
-std::ostream& operator<<(std::ostream& out, const CmdReqUnlink& unlink) {
- out << "CmdReqUnlink\n";
- out << "\t\tseq#:\t" << unlink.seq_num << '\n';
- return out;
-}
-
-std::ostream& operator<<(std::ostream& out, const CmdRepUnlink& unlink) {
- out << "CmdRepUnlink\n";
- out << "\t\tstatus:\t" << unlink.status << '\n';
- return out;
-}
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/messages.h b/host/vadb/usbip/messages.h
deleted file mode 100644
index 6bf8651..0000000
--- a/host/vadb/usbip/messages.h
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <glog/logging.h>
-#include <stdint.h>
-
-#include "common/libs/fs/shared_fd.h"
-
-// Requests and constants below are defined in kernel documentation file:
-// https://www.kernel.org/doc/Documentation/usb/usbip_protocol.txt
-namespace vadb {
-namespace usbip {
-namespace internal {
-// Rotate endianness of the data to match protocol.
-template <typename T>
-void HostToNet(T* data);
-template <typename T>
-void NetToHost(T* data);
-} // namespace internal
-
-// Send message to USB/IP client.
-// Accept data by value and modify it to match net endian locally.
-// Returns true, if message was sent successfully.
-template <typename T>
-bool SendUSBIPMsg(const avd::SharedFD& fd, T data) {
- internal::HostToNet(&data);
- return fd->Send(&data, sizeof(T), MSG_NOSIGNAL) == sizeof(T);
-}
-
-// Receive message from USB/IP client.
-// After message is received, it's updated to match host endian.
-// Returns true, if message was received successfully.
-template <typename T>
-bool RecvUSBIPMsg(const avd::SharedFD& fd, T* data) {
- bool res = fd->Recv(data, sizeof(T), MSG_NOSIGNAL) == sizeof(T);
- if (res) {
- internal::NetToHost(data);
- }
- return res;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-// COMMANDS
-////////////////////////////////////////////////////////////////////////////////
-
-// Command numbers. Commands are valid only once USB device is attached.
-enum Command : uint32_t {
- kUsbIpCmdReqSubmit = 1, // Submit request
- kUsbIpCmdReqUnlink = 2, // Unlink request
- kUsbIpCmdRepSubmit = 3, // Submit response
- kUsbIpCmdRepUnlink = 4, // Unlink response
-};
-
-// Direction of data flow.
-enum Direction : uint32_t {
- kUsbIpDirectionOut = 0,
- kUsbIpDirectionIn = 1,
-};
-
-// Setup structure is explained in great detail here:
-// - http://www.beyondlogic.org/usbnutshell/usb6.shtml
-// - http://www.usbmadesimple.co.uk/ums_4.htm
-struct CmdRequest {
- uint8_t type;
- uint8_t cmd;
- uint16_t value;
- uint16_t index;
- uint16_t length;
-} __attribute__((packed));
-
-// CmdHeader precedes any command request or response body.
-struct CmdHeader {
- Command command;
- uint32_t seq_num;
- uint16_t bus_num;
- uint16_t dev_num;
- Direction direction;
- uint32_t endpoint; // valid values: 0-15
-} __attribute__((packed));
-
-// Command data for submitting an USB request.
-struct CmdReqSubmit {
- uint32_t transfer_flags;
- uint32_t transfer_buffer_length;
- uint32_t start_frame;
- uint32_t number_of_packets;
- uint32_t deadline_interval;
- CmdRequest setup;
-} __attribute__((packed));
-
-// Command response for submitting an USB request.
-struct CmdRepSubmit {
- uint32_t status; // 0 = success.
- uint32_t actual_length;
- uint32_t start_frame;
- uint32_t number_of_packets;
- uint32_t error_count;
- CmdRequest setup;
-} __attribute__((packed));
-
-// Unlink USB request.
-struct CmdReqUnlink {
- uint32_t seq_num;
- uint32_t reserved[6];
-} __attribute__((packed));
-
-// Unlink USB response.
-struct CmdRepUnlink {
- uint32_t status;
- uint32_t reserved[6];
-} __attribute__((packed));
-
-// Diagnostics.
-std::ostream& operator<<(std::ostream& out, const CmdHeader& header);
-std::ostream& operator<<(std::ostream& out, const CmdReqSubmit& data);
-std::ostream& operator<<(std::ostream& out, const CmdRepSubmit& data);
-std::ostream& operator<<(std::ostream& out, const CmdReqUnlink& data);
-std::ostream& operator<<(std::ostream& out, const CmdRepUnlink& data);
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/server.cpp b/host/vadb/usbip/server.cpp
deleted file mode 100644
index ceb70e8..0000000
--- a/host/vadb/usbip/server.cpp
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "host/vadb/usbip/server.h"
-
-#include <glog/logging.h>
-#include <netinet/in.h>
-#include "common/libs/fs/shared_select.h"
-
-using avd::SharedFD;
-
-namespace vadb {
-namespace usbip {
-Server::Server(const std::string& name, const DevicePool& devices)
- : name_{name}, device_pool_{devices} {}
-
-bool Server::Init() { return CreateServerSocket(); }
-
-// Open new listening server socket.
-// Returns false, if listening socket could not be created.
-bool Server::CreateServerSocket() {
- LOG(INFO) << "Starting server socket: " << name_;
-
- server_ = SharedFD::SocketLocalServer(name_.c_str(), true, SOCK_STREAM, 0700);
- if (!server_->IsOpen()) {
- LOG(ERROR) << "Could not create socket: " << server_->StrError();
- return false;
- }
- return true;
-}
-
-void Server::BeforeSelect(avd::SharedFDSet* fd_read) const {
- fd_read->Set(server_);
- for (const auto& client : clients_) client.BeforeSelect(fd_read);
-}
-
-void Server::AfterSelect(const avd::SharedFDSet& fd_read) {
- if (fd_read.IsSet(server_)) HandleIncomingConnection();
-
- for (auto iter = clients_.begin(); iter != clients_.end();) {
- if (!iter->AfterSelect(fd_read)) {
- // If client conversation failed, hang up.
- iter = clients_.erase(iter);
- continue;
- }
- ++iter;
- }
-}
-
-// Accept new USB/IP connection. Add it to client pool.
-void Server::HandleIncomingConnection() {
- SharedFD client = SharedFD::Accept(*server_, nullptr, nullptr);
- if (!client->IsOpen()) {
- LOG(ERROR) << "Client connection failed: " << client->StrError();
- return;
- }
-
- clients_.emplace_back(device_pool_, client);
-}
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/server.h b/host/vadb/usbip/server.h
deleted file mode 100644
index 06b5e30..0000000
--- a/host/vadb/usbip/server.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <list>
-#include <string>
-
-#include "common/libs/fs/shared_fd.h"
-#include "host/vadb/usbip/client.h"
-#include "host/vadb/usbip/device_pool.h"
-
-namespace vadb {
-namespace usbip {
-
-class Server final {
- public:
- Server(const std::string& name, const DevicePool& device_pool);
- ~Server() = default;
-
- // Initialize this instance of Server.
- // Returns true, if initialization was successful.
- bool Init();
-
- // BeforeSelect is Called right before Select() to populate interesting
- // SharedFDs.
- void BeforeSelect(avd::SharedFDSet* fd_read) const;
-
- // AfterSelect is Called right after Select() to detect and respond to changes
- // on affected SharedFDs.
- void AfterSelect(const avd::SharedFDSet& fd_read);
-
- private:
- // Create USBIP server socket.
- // Returns true, if socket was successfully created.
- bool CreateServerSocket();
-
- // Handle new client connection.
- // New clients will be appended to clients_ list.
- void HandleIncomingConnection();
-
- std::string name_;
- avd::SharedFD server_;
- std::list<Client> clients_;
-
- const DevicePool& device_pool_;
-
- Server(const Server&) = delete;
- Server& operator=(const Server&) = delete;
-};
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/vhci_instrument.cpp b/host/vadb/usbip/vhci_instrument.cpp
deleted file mode 100644
index 8fb2600..0000000
--- a/host/vadb/usbip/vhci_instrument.cpp
+++ /dev/null
@@ -1,262 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <errno.h>
-#include <string.h>
-
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <sys/socket.h>
-
-#include <glog/logging.h>
-#include <fstream>
-#include <sstream>
-#include "common/libs/fs/shared_select.h"
-
-#include "common/libs/fs/shared_fd.h"
-#include "host/vadb/usbip/vhci_instrument.h"
-
-namespace vadb {
-namespace usbip {
-namespace {
-// Device ID is specified as a concatenated pair of BUS and DEVICE id.
-// Since we only export one device and our server doesn't care much about
-// its number, we use the default value of BUS=1 and DEVICE=1.
-// This can be set to something else and should still work, as long as
-// numbers are valid in USB sense.
-constexpr uint32_t kDefaultDeviceID = (1 << 16) | 1;
-
-// Request Highspeed configuration. Superspeed isn't supported by vhci.
-// Supported configurations are:
-// 4 -> wireless
-// 3 -> highspeed
-// 2 -> full speed
-// 1 -> low speed
-// Please refer to the Kernel source tree in the following locations:
-// include/uapi/linux/usb/ch9.h
-// drivers/usb/usbip/vhci_sysfs.c
-constexpr uint32_t kDefaultDeviceSpeed = 3;
-
-// Subsystem and device type where VHCI driver is located.
-// These values can usually be found after loading vhci-hcd module here:
-// /sys/devices/platform/vhci_hcd/modalias
-constexpr char kVHCISubsystem[] = "platform";
-constexpr char kVHCIDevType[] = "vhci_hcd";
-
-// Control messages.
-// Attach tells thread to attach remote device.
-// Detach tells thread to detach remote device.
-using ControlMsgType = uint8_t;
-constexpr ControlMsgType kControlAttach = 'A';
-constexpr ControlMsgType kControlDetach = 'D';
-constexpr ControlMsgType kControlExit = 'E';
-
-// Used with EPOLL as epoll_data to determine event type.
-enum EpollEventType {
- kControlEvent,
- kVHCIEvent,
-};
-
-// Port status values deducted from /sys/devices/platform/vhci_hcd/status
-enum {
- // kVHCIPortFree indicates the port is not currently in use.
- kVHCIStatusPortFree = 4
-};
-} // anonymous namespace
-
-VHCIInstrument::VHCIInstrument(const std::string& name)
- : udev_(nullptr, [](udev* u) { udev_unref(u); }),
- vhci_device_(nullptr,
- [](udev_device* device) { udev_device_unref(device); }),
- name_(name) {}
-
-VHCIInstrument::~VHCIInstrument() {
- control_write_end_->Write(&kControlExit, sizeof(kControlExit));
- attach_thread_->join();
-}
-
-bool VHCIInstrument::Init() {
- avd::SharedFD::Pipe(&control_read_end_, &control_write_end_);
-
- udev_.reset(udev_new());
- CHECK(udev_) << "Could not create libudev context.";
-
- vhci_device_.reset(udev_device_new_from_subsystem_sysname(
- udev_.get(), kVHCISubsystem, kVHCIDevType));
- if (!vhci_device_) {
- LOG(ERROR) << "VHCI not available. Is the driver loaded?";
- LOG(ERROR) << "Try: sudo modprobe vhci_hcd";
- LOG(ERROR) << "The driver is part of linux-image-extra-`uname -r` package";
- return false;
- }
-
- syspath_ = udev_device_get_syspath(vhci_device_.get());
-
- if (!FindFreePort()) {
- LOG(ERROR) << "It appears all your VHCI ports are currently occupied.";
- LOG(ERROR) << "New VHCI device cannot be registered unless one of the "
- << "ports is freed.";
- return false;
- }
-
- attach_thread_.reset(new std::thread([this]() { AttachThread(); }));
- return true;
-}
-
-bool VHCIInstrument::FindFreePort() {
- std::ifstream stat(syspath_ + "/status");
- int port;
- int status;
- std::string everything_else;
-
- if (!stat.is_open()) {
- LOG(ERROR) << "Could not open usb-ip status file.";
- return false;
- }
-
- // Skip past the header line.
- std::getline(stat, everything_else);
-
- while (stat.rdstate() == std::ios_base::goodbit) {
- stat >> port >> status;
- std::getline(stat, everything_else);
- if (status == kVHCIStatusPortFree) {
- port_ = port;
- LOG(INFO) << "Using VHCI port " << port_;
- return true;
- }
- }
- return false;
-}
-
-void VHCIInstrument::TriggerAttach() {
- control_write_end_->Write(&kControlAttach, sizeof(kControlAttach));
-}
-
-void VHCIInstrument::TriggerDetach() {
- control_write_end_->Write(&kControlDetach, sizeof(kControlDetach));
-}
-
-void VHCIInstrument::AttachThread() {
- avd::SharedFD epoll = avd::SharedFD::Epoll();
- // Trigger attach upon start.
- bool want_attach = true;
- // Operation is pending on read.
- bool is_pending = false;
-
- epoll_event control_event;
- control_event.events = EPOLLIN;
- control_event.data.u64 = kControlEvent;
- epoll_event vhci_event;
- vhci_event.events = EPOLLRDHUP | EPOLLONESHOT;
- vhci_event.data.u64 = kVHCIEvent;
-
- epoll->EpollCtl(EPOLL_CTL_ADD, control_read_end_, &control_event);
- while (true) {
- if (vhci_socket_->IsOpen()) {
- epoll->EpollCtl(EPOLL_CTL_ADD, vhci_socket_, &vhci_event);
- }
-
- epoll_event found_event{};
- ControlMsgType request_type;
-
- if (epoll->EpollWait(&found_event, 1, 1000)) {
- switch (found_event.data.u64) {
- case kControlEvent:
- control_read_end_->Read(&request_type, sizeof(request_type));
- is_pending = true;
- want_attach = request_type == kControlAttach;
- LOG(INFO) << (want_attach ? "Attach" : "Detach") << " triggered.";
- break;
- case kVHCIEvent:
- vhci_socket_ = avd::SharedFD();
- // Only re-establish VHCI if it was already established before.
- is_pending = want_attach;
- // Do not immediately fall into attach cycle. It will likely complete
- // before VHCI finishes deregistering this callback.
- continue;
- }
- }
-
- // Make an attempt to re-attach. If successful, clear pending attach flag.
- if (is_pending) {
- if (want_attach && Attach()) {
- is_pending = false;
- } else if (!want_attach && Detach()) {
- is_pending = false;
- } else {
- LOG(INFO) << (want_attach ? "Attach" : "Detach") << " unsuccessful. "
- << "Will re-try.";
- sleep(1);
- }
- }
- }
-}
-
-bool VHCIInstrument::Detach() {
- std::stringstream result;
- result << port_;
- std::ofstream detach(syspath_ + "/detach");
-
- if (!detach.is_open()) {
- LOG(WARNING) << "Could not open VHCI detach file.";
- return false;
- }
- detach << result.str();
- return detach.rdstate() == std::ios_base::goodbit;
-}
-
-bool VHCIInstrument::Attach() {
- if (!vhci_socket_->IsOpen()) {
- vhci_socket_ =
- avd::SharedFD::SocketLocalClient(name_.c_str(), true, SOCK_STREAM);
- if (!vhci_socket_->IsOpen()) return false;
- }
-
- int sys_fd = vhci_socket_->UNMANAGED_Dup();
- bool success = false;
-
- {
- std::stringstream result;
- result << port_ << ' ' << sys_fd << ' ' << kDefaultDeviceID << ' '
- << kDefaultDeviceSpeed;
- std::string path = syspath_ + "/attach";
- std::ofstream attach(path);
-
- if (!attach.is_open()) {
- LOG(WARNING) << "Could not open VHCI attach file " << path << " ("
- << strerror(errno) << ")";
- close(sys_fd);
- return false;
- }
- attach << result.str();
-
- // It is unclear whether duplicate FD should remain open or not. There are
- // cases supporting both assumptions, likely related to kernel version.
- // Kernel 4.10 is having problems communicating with USB/IP server if the
- // socket is closed after it's passed to kernel. It is a clear indication that
- // the kernel requires the socket to be kept open.
- success = attach.rdstate() == std::ios_base::goodbit;
- // Make sure everything was written and flushed. This happens when we close
- // the ofstream attach.
- }
-
- close(sys_fd);
- return success;
-}
-
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/usbip/vhci_instrument.h b/host/vadb/usbip/vhci_instrument.h
deleted file mode 100644
index 7f54cde..0000000
--- a/host/vadb/usbip/vhci_instrument.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <memory>
-#include <string>
-#include <thread>
-
-#include <libudev.h>
-
-#include "common/libs/fs/shared_fd.h"
-
-namespace vadb {
-namespace usbip {
-// VHCIInstrument class configures VHCI-HCD on local kernel.
-class VHCIInstrument {
- public:
- VHCIInstrument(const std::string& name);
- virtual ~VHCIInstrument();
-
- // Init opens vhci-hcd driver and allocates port to which remote USB device
- // will be attached.
- // Returns false, if vhci-hcd driver could not be opened, or if no free port
- // was found.
- bool Init();
-
- // TriggerAttach tells underlying thread to make attempt to re-attach USB
- // device.
- void TriggerAttach();
-
- // TriggerDetach tells underlying thread to disconnect remote USB device.
- void TriggerDetach();
-
- private:
- // Attach makes an attempt to configure VHCI to enable virtual USB device.
- // Returns true, if configuration attempt was successful.
- bool Attach();
-
- // Detach disconnects virtual USB device.
- // Returns true, if attempt was successful.
- bool Detach();
-
- // AttachThread is a background thread that responds to configuration
- // requests.
- void AttachThread();
- bool FindFreePort();
-
- private:
- std::unique_ptr<udev, void(*)(udev*)> udev_;
- std::unique_ptr<udev_device, void(*)(udev_device*)> vhci_device_;
- std::string name_;
- std::unique_ptr<std::thread> attach_thread_;
- std::string syspath_;
- avd::SharedFD control_write_end_;
- avd::SharedFD control_read_end_;
- avd::SharedFD vhci_socket_;
- int port_;
-
- VHCIInstrument(const VHCIInstrument& other) = delete;
- VHCIInstrument& operator=(const VHCIInstrument& other) = delete;
-};
-} // namespace usbip
-} // namespace vadb
diff --git a/host/vadb/virtual_adb_client.cpp b/host/vadb/virtual_adb_client.cpp
deleted file mode 100644
index b165287..0000000
--- a/host/vadb/virtual_adb_client.cpp
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <algorithm>
-#include <memory>
-#include "common/libs/fs/shared_select.h"
-#include "host/vadb/usb_cmd_attach.h"
-#include "host/vadb/usb_cmd_control_transfer.h"
-#include "host/vadb/usb_cmd_data_transfer.h"
-#include "host/vadb/usb_cmd_device_list.h"
-#include "host/vadb/usb_cmd_heartbeat.h"
-#include "host/vadb/virtual_adb_client.h"
-
-namespace vadb {
-namespace {
-constexpr int kHeartbeatTimeoutSeconds = 3;
-} // namespace
-
-VirtualADBClient::VirtualADBClient(usbip::DevicePool* pool, avd::SharedFD fd,
- const std::string& usbip_socket_name)
- : pool_{pool}, fd_{fd}, vhci_{usbip_socket_name} {
- CHECK(vhci_.Init());
- timer_ = avd::SharedFD::TimerFD(CLOCK_MONOTONIC, 0);
- SendHeartbeat();
-}
-
-void VirtualADBClient::RegisterDevice(
- const usb_forward::DeviceInfo& dev,
- const std::vector<usb_forward::InterfaceInfo>& ifaces) {
- auto d = std::unique_ptr<usbip::Device>(new usbip::Device);
- d->vendor_id = dev.vendor_id;
- d->product_id = dev.product_id;
- d->dev_version = dev.dev_version;
- d->dev_class = dev.dev_class;
- d->dev_subclass = dev.dev_subclass;
- d->dev_protocol = dev.dev_protocol;
- d->speed = dev.speed;
- d->configurations_count = dev.num_configurations;
- d->configuration_number = dev.cur_configuration;
-
- for (const auto& iface : ifaces) {
- d->interfaces.push_back(usbip::Device::Interface{
- iface.if_class, iface.if_subclass, iface.if_protocol});
- }
-
- uint8_t bus_id = dev.bus_id;
- uint8_t dev_id = dev.dev_id;
-
- d->handle_attach = [this, bus_id, dev_id]() -> bool {
- return HandleAttach(bus_id, dev_id);
- };
-
- d->handle_control_transfer =
- [this, bus_id, dev_id](
- const usbip::CmdRequest& r, uint32_t deadline,
- std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback) -> bool {
- return HandleDeviceControlRequest(bus_id, dev_id, r, deadline,
- std::move(data), std::move(callback));
- };
-
- d->handle_data_transfer =
- [this, bus_id, dev_id](
- uint8_t endpoint, bool is_host_to_device, uint32_t deadline,
- std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback) -> bool {
- return HandleDeviceDataRequest(bus_id, dev_id, endpoint, is_host_to_device,
- deadline, std::move(data),
- std::move(callback));
- };
-
- pool_->AddDevice(usbip::DevicePool::BusDevNumber{bus_id, dev_id},
- std::move(d));
-
- // Attach this device.
- HandleAttach(bus_id, dev_id);
-}
-
-bool VirtualADBClient::PopulateRemoteDevices() {
- return ExecuteCommand(std::unique_ptr<USBCommand>(new USBCmdDeviceList(
- [this](const usb_forward::DeviceInfo& info,
- const std::vector<usb_forward::InterfaceInfo>& ifaces) {
- RegisterDevice(info, ifaces);
- })));
-}
-
-bool VirtualADBClient::HandleDeviceControlRequest(
- uint8_t bus_id, uint8_t dev_id, const usbip::CmdRequest& r,
- uint32_t timeout, std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback) {
- return ExecuteCommand(std::unique_ptr<USBCommand>(new USBCmdControlTransfer(
- bus_id, dev_id, r.type, r.cmd, r.value, r.index, timeout, std::move(data),
- std::move(callback))));
-}
-
-bool VirtualADBClient::HandleDeviceDataRequest(
- uint8_t bus_id, uint8_t dev_id, uint8_t endpoint, bool is_host_to_device,
- uint32_t deadline, std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback) {
- return ExecuteCommand(std::unique_ptr<USBCommand>(
- new USBCmdDataTransfer(bus_id, dev_id, endpoint, is_host_to_device,
- deadline, std::move(data), std::move(callback))));
-}
-
-bool VirtualADBClient::HandleAttach(uint8_t bus_id, uint8_t dev_id) {
- return ExecuteCommand(
- std::unique_ptr<USBCommand>(new USBCmdAttach(bus_id, dev_id)));
-}
-
-bool VirtualADBClient::SendHeartbeat() {
- VLOG(1) << "Sending heartbeat...";
- struct itimerspec spec {};
- spec.it_value.tv_sec = kHeartbeatTimeoutSeconds;
- timer_->TimerSet(0, &spec, nullptr);
-
- heartbeat_tag_ = tag_;
-
- return ExecuteCommand(std::unique_ptr<USBCommand>(
- new USBCmdHeartbeat([this](bool success) { HandleHeartbeat(success); })));
-}
-
-void VirtualADBClient::HandleHeartbeat(bool is_ready) {
- VLOG(1) << "Remote server status: " << is_ready;
- if (is_ready && !is_remote_server_ready_) {
- LOG(INFO) << "Remote server is now ready.";
- PopulateRemoteDevices();
- vhci_.TriggerAttach();
- } else if (is_remote_server_ready_ && !is_ready) {
- vhci_.TriggerDetach();
- LOG(WARNING) << "Remote server connection lost.";
- // It makes perfect sense to cancel all outstanding USB requests, as device
- // is not going to answer any of these anyway.
- for (const auto& pair : commands_) {
- pair.second->OnResponse(false, fd_);
- }
- commands_.clear();
- }
- is_remote_server_ready_ = is_ready;
-}
-
-bool VirtualADBClient::HandleHeartbeatTimeout() {
- uint64_t timer_result;
- timer_->Read(&timer_result, sizeof(timer_result));
-
- auto iter = commands_.find(heartbeat_tag_);
- if (iter != commands_.end()) {
- // Make sure to erase the value from list of commands prior to running
- // callback. Particularly important for heartbeat, which cancels all
- // outstanding USB commands (including self, if found), if device goes
- // away (eg. reboots).
- auto command = std::move(iter->second);
- commands_.erase(iter);
- command->OnResponse(false, fd_);
- }
-
- return SendHeartbeat();
-}
-
-bool VirtualADBClient::ExecuteCommand(std::unique_ptr<USBCommand> cmd) {
- uint32_t this_tag = tag_;
- tag_++;
- usb_forward::RequestHeader hdr{cmd->Command(), this_tag};
- if (fd_->Write(&hdr, sizeof(hdr)) != sizeof(hdr)) {
- LOG(ERROR) << "Could not contact USB Forwarder: " << fd_->StrError();
- return false;
- }
-
- if (!cmd->OnRequest(fd_)) return false;
-
- commands_[this_tag] = std::move(cmd);
- return true;
-}
-
-// BeforeSelect is Called right before Select() to populate interesting
-// SharedFDs.
-void VirtualADBClient::BeforeSelect(avd::SharedFDSet* fd_read) const {
- fd_read->Set(fd_);
- fd_read->Set(timer_);
-}
-
-// AfterSelect is Called right after Select() to detect and respond to changes
-// on affected SharedFDs.
-// Return value indicates whether this client is still valid.
-bool VirtualADBClient::AfterSelect(const avd::SharedFDSet& fd_read) {
- if (fd_read.IsSet(timer_)) {
- HandleHeartbeatTimeout();
- }
- if (fd_read.IsSet(fd_)) {
- usb_forward::ResponseHeader rhdr;
- if (fd_->Read(&rhdr, sizeof(rhdr)) != sizeof(rhdr)) {
- LOG(ERROR) << "Could not read from USB Forwarder: " << fd_->StrError();
- // TODO(ender): it is very likely the connection has been dropped by QEmu.
- // Should we cancel all pending commands now?
- return false;
- }
-
- auto iter = commands_.find(rhdr.tag);
- if (iter == commands_.end()) {
- // This is likely a late heartbeat response, but could very well be any of
- // the remaining commands.
- LOG(INFO) << "Received response for discarded tag " << rhdr.tag;
- } else {
- // Make sure to erase the value from list of commands prior to running
- // callback. Particularly important for heartbeat, which cancels all
- // outstanding USB commands (including self, if found), if device goes
- // away (eg. reboots).
- auto command = std::move(iter->second);
- commands_.erase(iter);
- command->OnResponse(rhdr.status == usb_forward::StatusSuccess, fd_);
- }
- }
-
- return true;
-}
-
-} // namespace vadb
diff --git a/host/vadb/virtual_adb_client.h b/host/vadb/virtual_adb_client.h
deleted file mode 100644
index 8fae2d7..0000000
--- a/host/vadb/virtual_adb_client.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <string>
-
-#include "common/libs/fs/shared_fd.h"
-#include "common/libs/fs/shared_select.h"
-#include "common/libs/usbforward/protocol.h"
-#include "host/vadb/usb_cmd.h"
-#include "host/vadb/usbip/device.h"
-#include "host/vadb/usbip/device_pool.h"
-#include "host/vadb/usbip/messages.h"
-#include "host/vadb/usbip/vhci_instrument.h"
-
-namespace vadb {
-// VirtualADBClient is a companion class for USBForwarder, running on
-// Cuttlefish. VirtualADBClient collects list of available USB devices from
-// Cuttlefish and makes them available to USB/IP.
-//
-// Purpose of this class is to connect to USBForwarder and make access to
-// remote USB devices possible with help of USB/IP protocol.
-class VirtualADBClient {
- public:
- VirtualADBClient(usbip::DevicePool* pool, avd::SharedFD fd,
- const std::string& usbip_socket_name);
-
- virtual ~VirtualADBClient() = default;
-
- // Query remote server; populate available USB devices.
- bool PopulateRemoteDevices();
-
- // BeforeSelect is Called right before Select() to populate interesting
- // SharedFDs.
- void BeforeSelect(avd::SharedFDSet* fd_read) const;
-
- // AfterSelect is Called right after Select() to detect and respond to changes
- // on affected SharedFDs.
- // Return value indicates whether this client is still valid.
- bool AfterSelect(const avd::SharedFDSet& fd_read);
-
- private:
- // Register new device in a device pool.
- void RegisterDevice(const usb_forward::DeviceInfo& dev,
- const std::vector<usb_forward::InterfaceInfo>& ifaces);
-
- // Request attach remote USB device.
- bool HandleAttach(uint8_t bus_id, uint8_t dev_id);
-
- // Execute control request on remote device.
- bool HandleDeviceControlRequest(uint8_t bus_id, uint8_t dev_id,
- const usbip::CmdRequest& r, uint32_t deadline,
- std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback);
-
- // Execute data request on remote device.
- bool HandleDeviceDataRequest(uint8_t bus_id, uint8_t dev_id, uint8_t endpoint,
- bool is_host_to_device, uint32_t deadline,
- std::vector<uint8_t> data,
- usbip::Device::AsyncTransferReadyCB callback);
-
- // Send new heartbeat request and arm the heartbeat timer.
- bool SendHeartbeat();
-
- // Heartbeat handler receives response to heartbeat request.
- // Supplied argument indicates, whether remote server is ready to export USB
- // gadget.
- void HandleHeartbeat(bool is_ready);
-
- // Heartbeat timeout detects situation where heartbeat did not receive
- // matching response. This could be a direct result of device reset.
- bool HandleHeartbeatTimeout();
-
- // ExecuteCommand creates command header and executes supplied USBCommand.
- // If execution was successful, command will be stored internally until
- // response arrives.
- bool ExecuteCommand(std::unique_ptr<USBCommand> cmd);
-
- usbip::DevicePool* pool_;
- avd::SharedFD fd_;
- avd::SharedFD timer_;
- usbip::VHCIInstrument vhci_;
- bool is_remote_server_ready_ = false;
-
- uint32_t tag_ = 0;
- // Assign an 'invalid' tag as previously sent heartbeat command. This will
- // prevent heartbeat timeout handler from finding a command if none was sent.
- uint32_t heartbeat_tag_ = ~0;
- std::map<uint32_t, std::unique_ptr<USBCommand>> commands_;
-
- VirtualADBClient(const VirtualADBClient& other) = delete;
- VirtualADBClient& operator=(const VirtualADBClient& other) = delete;
-};
-
-} // namespace vadb
diff --git a/host/vadb/virtual_adb_server.cpp b/host/vadb/virtual_adb_server.cpp
deleted file mode 100644
index bf39bb3..0000000
--- a/host/vadb/virtual_adb_server.cpp
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "host/vadb/virtual_adb_server.h"
-
-namespace vadb {
-
-bool VirtualADBServer::Init() {
- LOG(INFO) << "Starting server socket: " << name_;
-
- server_ =
- avd::SharedFD::SocketLocalServer(name_.c_str(), false, SOCK_STREAM, 0666);
- if (!server_->IsOpen()) {
- LOG(ERROR) << "Could not create socket: " << server_->StrError();
- return false;
- }
- return true;
-}
-
-void VirtualADBServer::BeforeSelect(avd::SharedFDSet* fd_read) const {
- fd_read->Set(server_);
- for (const auto& client : clients_) client.BeforeSelect(fd_read);
-}
-
-void VirtualADBServer::AfterSelect(const avd::SharedFDSet& fd_read) {
- if (fd_read.IsSet(server_)) HandleIncomingConnection();
-
- for (auto iter = clients_.begin(); iter != clients_.end();) {
- if (!iter->AfterSelect(fd_read)) {
- // If client conversation failed, hang up.
- iter = clients_.erase(iter);
- continue;
- }
- ++iter;
- }
-}
-
-// Accept new QEmu connection. Add it to client pool.
-// Typically we will have no more than one QEmu connection, but the nature
-// of server requires proper handling nonetheless.
-void VirtualADBServer::HandleIncomingConnection() {
- avd::SharedFD client = avd::SharedFD::Accept(*server_, nullptr, nullptr);
- if (!client->IsOpen()) {
- LOG(ERROR) << "Client connection failed: " << client->StrError();
- return;
- }
-
- clients_.emplace_back(&pool_, client, usbip_name_);
-}
-
-} // namespace vadb
diff --git a/host/vadb/virtual_adb_server.h b/host/vadb/virtual_adb_server.h
deleted file mode 100644
index 91f6611..0000000
--- a/host/vadb/virtual_adb_server.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <list>
-#include <string>
-
-#include "common/libs/fs/shared_fd.h"
-#include "host/vadb/usbip/device_pool.h"
-#include "host/vadb/virtual_adb_client.h"
-
-namespace vadb {
-// VirtualADBServer manages incoming VirtualUSB/ADB connections from QEmu.
-class VirtualADBServer {
- public:
- VirtualADBServer(const std::string& usb_socket_name,
- const std::string& usbip_socket_name)
- : name_(usb_socket_name), usbip_name_(usbip_socket_name) {}
-
- ~VirtualADBServer() = default;
-
- // Initialize this instance of Server.
- // Returns true, if initialization was successful.
- bool Init();
-
- // Pool of USB devices available to export.
- const usbip::DevicePool& Pool() const { return pool_; };
-
- // BeforeSelect is Called right before Select() to populate interesting
- // SharedFDs.
- void BeforeSelect(avd::SharedFDSet* fd_read) const;
-
- // AfterSelect is Called right after Select() to detect and respond to changes
- // on affected SharedFDs.
- void AfterSelect(const avd::SharedFDSet& fd_read);
-
- private:
- void HandleIncomingConnection();
-
- usbip::DevicePool pool_;
- std::string name_;
- std::string usbip_name_;
- avd::SharedFD server_;
- std::list<VirtualADBClient> clients_;
-
- VirtualADBServer(const VirtualADBServer&) = delete;
- VirtualADBServer& operator=(const VirtualADBServer&) = delete;
-};
-
-} // namespace vadb