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 <list> 19 #include <string> 20 21 #include "common/libs/fs/shared_fd.h" 22 #include "host/libs/usbip/device_pool.h" 23 #include "host/libs/vadb/virtual_adb_client.h" 24 25 namespace vadb { 26 // VirtualADBServer manages incoming VirtualUSB/ADB connections from QEmu. 27 class VirtualADBServer { 28 public: 29 VirtualADBServer(const std::string& usb_socket_name, int vhci_port, 30 const std::string& usbip_socket_name) 31 : name_(usb_socket_name), 32 vhci_port_{vhci_port}, 33 usbip_name_(usbip_socket_name) {} 34 35 ~VirtualADBServer() = default; 36 37 // Initialize this instance of Server. 38 // Returns true, if initialization was successful. 39 bool Init(); 40 41 // Pool of USB devices available to export. 42 const usbip::DevicePool& Pool() const { return pool_; }; 43 44 // BeforeSelect is Called right before Select() to populate interesting 45 // SharedFDs. 46 void BeforeSelect(cvd::SharedFDSet* fd_read) const; 47 48 // AfterSelect is Called right after Select() to detect and respond to changes 49 // on affected SharedFDs. 50 void AfterSelect(const cvd::SharedFDSet& fd_read); 51 52 private: 53 void HandleIncomingConnection(); 54 55 usbip::DevicePool pool_; 56 std::string name_; 57 int vhci_port_{}; 58 std::string usbip_name_; 59 cvd::SharedFD server_; 60 std::list<VirtualADBClient> clients_; 61 62 VirtualADBServer(const VirtualADBServer&) = delete; 63 VirtualADBServer& operator=(const VirtualADBServer&) = delete; 64 }; 65 66 } // namespace vadb 67