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/commands/virtual_usb_manager/usbip/device_pool.h" 23 #include "host/commands/virtual_usb_manager/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(cvd::SharedFD usb_v1_socket, int vhci_port, 30 const std::string& usbip_socket_name) 31 : vhci_port_{vhci_port}, 32 usbip_name_(usbip_socket_name), 33 server_(usb_v1_socket) {} 34 35 ~VirtualADBServer() = default; 36 37 // Pool of USB devices available to export. 38 const usbip::DevicePool& Pool() const { return pool_; }; 39 40 // BeforeSelect is Called right before Select() to populate interesting 41 // SharedFDs. 42 void BeforeSelect(cvd::SharedFDSet* fd_read) const; 43 44 // AfterSelect is Called right after Select() to detect and respond to changes 45 // on affected SharedFDs. 46 void AfterSelect(const cvd::SharedFDSet& fd_read); 47 48 private: 49 void HandleIncomingConnection(); 50 51 usbip::DevicePool pool_; 52 int vhci_port_{}; 53 std::string usbip_name_; 54 cvd::SharedFD server_; 55 std::list<VirtualADBClient> clients_; 56 57 VirtualADBServer(const VirtualADBServer&) = delete; 58 VirtualADBServer& operator=(const VirtualADBServer&) = delete; 59 }; 60 61 } // namespace vadb 62