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 #pragma once 17 18 #include <string> 19 20 #include "common/libs/fs/shared_fd.h" 21 #include "common/libs/fs/shared_select.h" 22 #include "common/libs/usbforward/protocol.h" 23 #include "host/commands/virtual_usb_manager/vadb/usb_cmd.h" 24 #include "host/commands/virtual_usb_manager/usbip/device.h" 25 #include "host/commands/virtual_usb_manager/usbip/device_pool.h" 26 #include "host/commands/virtual_usb_manager/usbip/messages.h" 27 #include "host/commands/virtual_usb_manager/usbip/vhci_instrument.h" 28 29 namespace vadb { 30 // VirtualADBClient is a companion class for USBForwarder, running on 31 // Cuttlefish. VirtualADBClient collects list of available USB devices from 32 // Cuttlefish and makes them available to USB/IP. 33 // 34 // Purpose of this class is to connect to USBForwarder and make access to 35 // remote USB devices possible with help of USB/IP protocol. 36 class VirtualADBClient { 37 public: 38 VirtualADBClient(usbip::DevicePool* pool, cvd::SharedFD fd, int vhci_port, 39 const std::string& usbip_socket_name); 40 41 virtual ~VirtualADBClient() = default; 42 43 // Query remote server; populate available USB devices. 44 bool PopulateRemoteDevices(); 45 46 // BeforeSelect is Called right before Select() to populate interesting 47 // SharedFDs. 48 void BeforeSelect(cvd::SharedFDSet* fd_read) const; 49 50 // AfterSelect is Called right after Select() to detect and respond to changes 51 // on affected SharedFDs. 52 // Return value indicates whether this client is still valid. 53 bool AfterSelect(const cvd::SharedFDSet& fd_read); 54 55 private: 56 // Register new device in a device pool. 57 void RegisterDevice(const usb_forward::DeviceInfo& dev, 58 const std::vector<usb_forward::InterfaceInfo>& ifaces); 59 60 // Request attach remote USB device. 61 bool HandleAttach(uint8_t bus_id, uint8_t dev_id); 62 63 // Execute control request on remote device. 64 bool HandleDeviceControlRequest(uint8_t bus_id, uint8_t dev_id, 65 const usbip::CmdRequest& r, uint32_t deadline, 66 std::vector<uint8_t> data, 67 usbip::Device::AsyncTransferReadyCB callback); 68 69 // Execute data request on remote device. 70 bool HandleDeviceDataRequest(uint8_t bus_id, uint8_t dev_id, uint8_t endpoint, 71 bool is_host_to_device, uint32_t deadline, 72 std::vector<uint8_t> data, 73 usbip::Device::AsyncTransferReadyCB callback); 74 75 // Send new heartbeat request and arm the heartbeat timer. 76 bool SendHeartbeat(); 77 78 // Heartbeat handler receives response to heartbeat request. 79 // Supplied argument indicates, whether remote server is ready to export USB 80 // gadget. 81 void HandleHeartbeat(bool is_ready); 82 83 // Heartbeat timeout detects situation where heartbeat did not receive 84 // matching response. This could be a direct result of device reset. 85 bool HandleHeartbeatTimeout(); 86 87 // ExecuteCommand creates command header and executes supplied USBCommand. 88 // If execution was successful, command will be stored internally until 89 // response arrives. 90 bool ExecuteCommand(std::unique_ptr<USBCommand> cmd); 91 92 usbip::DevicePool* pool_; 93 cvd::SharedFD fd_; 94 cvd::SharedFD timer_; 95 usbip::VHCIInstrument vhci_; 96 bool is_remote_server_ready_ = false; 97 98 uint32_t tag_ = 0; 99 // Assign an 'invalid' tag as previously sent heartbeat command. This will 100 // prevent heartbeat timeout handler from finding a command if none was sent. 101 uint32_t heartbeat_tag_ = ~0; 102 std::map<uint32_t, std::unique_ptr<USBCommand>> commands_; 103 104 VirtualADBClient(const VirtualADBClient& other) = delete; 105 VirtualADBClient& operator=(const VirtualADBClient& other) = delete; 106 }; 107 108 } // namespace vadb 109