blob: 5e48f217f0795cc2b1e611f6ce900ef016fce156 [file] [log] [blame]
Greg Hartman92045f62017-10-31 17:12:24 -07001/*
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"
Jorge E. Moreirad8430662018-07-22 23:52:33 -070020#include "host/commands/virtual_usb_manager/usbip/device_pool.h"
21#include "host/commands/virtual_usb_manager/usbip/messages.h"
Greg Hartman92045f62017-10-31 17:12:24 -070022
23namespace vadb {
24namespace 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.
29class Client final {
30 public:
Greg Hartman153b1062017-11-11 12:09:21 -080031 Client(const DevicePool& pool, const cvd::SharedFD& fd)
Greg Hartman92045f62017-10-31 17:12:24 -070032 : pool_(pool), fd_(fd) {}
33
34 ~Client() {}
35
36 // BeforeSelect is Called right before Select() to populate interesting
37 // SharedFDs.
Greg Hartman153b1062017-11-11 12:09:21 -080038 void BeforeSelect(cvd::SharedFDSet* fd_read) const;
Greg Hartman92045f62017-10-31 17:12:24 -070039
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.
Greg Hartman153b1062017-11-11 12:09:21 -080043 bool AfterSelect(const cvd::SharedFDSet& fd_read);
Greg Hartman92045f62017-10-31 17:12:24 -070044
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_;
Greg Hartman153b1062017-11-11 12:09:21 -080065 cvd::SharedFD fd_;
Greg Hartman92045f62017-10-31 17:12:24 -070066
67 Client(const Client&) = delete;
68 Client& operator=(const Client&) = delete;
69};
70
71} // namespace usbip
72} // namespace vadb