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 "common/libs/fs/shared_fd.h" 19 #include "common/libs/fs/shared_select.h" 20 #include "host/commands/virtual_usb_manager/usbip/device_pool.h" 21 #include "host/commands/virtual_usb_manager/usbip/messages.h" 22 23 namespace vadb { 24 namespace usbip { 25 26 // Represents USB/IP client, or individual connection to our USB/IP server. 27 // Multiple clients are allowed, even if practically we anticipate only one 28 // connection at the time. 29 class Client final { 30 public: Client(const DevicePool & pool,const cvd::SharedFD & fd)31 Client(const DevicePool& pool, const cvd::SharedFD& fd) 32 : pool_(pool), fd_(fd) {} 33 ~Client()34 ~Client() {} 35 36 // BeforeSelect is Called right before Select() to populate interesting 37 // SharedFDs. 38 void BeforeSelect(cvd::SharedFDSet* fd_read) const; 39 40 // AfterSelect is Called right after Select() to detect and respond to changes 41 // on affected SharedFDs. 42 // Return value indicates whether this client is still valid. 43 bool AfterSelect(const cvd::SharedFDSet& fd_read); 44 45 private: 46 // Respond to message from remote client. 47 // Returns false, if client violated protocol or disconnected, indicating, 48 // that this instance should no longer be used. 49 bool HandleIncomingMessage(); 50 51 // Execute command on USB device. 52 // Returns false, if connection should be dropped. 53 bool HandleSubmitCmd(const CmdHeader& hdr); 54 55 // HandleAsyncDataReady is called asynchronously once previously submitted 56 // data transfer (control or bulk) has completed (or failed). 57 void HandleAsyncDataReady(uint32_t seq_num, bool is_success, 58 bool is_host_to_device, std::vector<uint8_t> data); 59 60 // Unlink previously submitted message from device queue. 61 // Returns false, if connection should be dropped. 62 bool HandleUnlinkCmd(const CmdHeader& hdr); 63 64 const DevicePool& pool_; 65 cvd::SharedFD fd_; 66 67 Client(const Client&) = delete; 68 Client& operator=(const Client&) = delete; 69 }; 70 71 } // namespace usbip 72 } // namespace vadb 73