blob: aa2f5d4496b3098ef74c9a4381d3f7c8d67a7d7e [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 <memory>
19#include <string>
20#include <thread>
21
Greg Hartman92045f62017-10-31 17:12:24 -070022#include "common/libs/fs/shared_fd.h"
23
24namespace vadb {
25namespace usbip {
26// VHCIInstrument class configures VHCI-HCD on local kernel.
27class VHCIInstrument {
28 public:
Ryan Hainingdd0dcfe2018-05-16 13:55:20 -070029 VHCIInstrument(int port, const std::string& name);
Greg Hartman92045f62017-10-31 17:12:24 -070030 virtual ~VHCIInstrument();
31
32 // Init opens vhci-hcd driver and allocates port to which remote USB device
33 // will be attached.
34 // Returns false, if vhci-hcd driver could not be opened, or if no free port
35 // was found.
36 bool Init();
37
38 // TriggerAttach tells underlying thread to make attempt to re-attach USB
39 // device.
40 void TriggerAttach();
41
42 // TriggerDetach tells underlying thread to disconnect remote USB device.
43 void TriggerDetach();
44
45 private:
46 // Attach makes an attempt to configure VHCI to enable virtual USB device.
47 // Returns true, if configuration attempt was successful.
48 bool Attach();
49
50 // Detach disconnects virtual USB device.
51 // Returns true, if attempt was successful.
52 bool Detach();
53
54 // AttachThread is a background thread that responds to configuration
55 // requests.
56 void AttachThread();
Ryan Hainingdd0dcfe2018-05-16 13:55:20 -070057 bool VerifyPortIsFree() const;
Greg Hartman92045f62017-10-31 17:12:24 -070058
59 private:
Greg Hartman92045f62017-10-31 17:12:24 -070060 std::string name_;
Ryan Hainingb8afc412018-04-07 09:49:28 -070061 std::thread attach_thread_;
Greg Hartman92045f62017-10-31 17:12:24 -070062 std::string syspath_;
Greg Hartman153b1062017-11-11 12:09:21 -080063 cvd::SharedFD control_write_end_;
64 cvd::SharedFD control_read_end_;
65 cvd::SharedFD vhci_socket_;
Ryan Hainingdd0dcfe2018-05-16 13:55:20 -070066 int port_{};
Greg Hartman92045f62017-10-31 17:12:24 -070067
68 VHCIInstrument(const VHCIInstrument& other) = delete;
69 VHCIInstrument& operator=(const VHCIInstrument& other) = delete;
70};
71} // namespace usbip
72} // namespace vadb