Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 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 <memory> |
| 19 | #include <string> |
| 20 | #include <thread> |
| 21 | |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 22 | #include "common/libs/fs/shared_fd.h" |
| 23 | |
| 24 | namespace vadb { |
| 25 | namespace usbip { |
| 26 | // VHCIInstrument class configures VHCI-HCD on local kernel. |
| 27 | class VHCIInstrument { |
| 28 | public: |
Ryan Haining | dd0dcfe | 2018-05-16 13:55:20 -0700 | [diff] [blame] | 29 | VHCIInstrument(int port, const std::string& name); |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 30 | 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 Haining | dd0dcfe | 2018-05-16 13:55:20 -0700 | [diff] [blame] | 57 | bool VerifyPortIsFree() const; |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 60 | std::string name_; |
Ryan Haining | b8afc41 | 2018-04-07 09:49:28 -0700 | [diff] [blame] | 61 | std::thread attach_thread_; |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 62 | std::string syspath_; |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 63 | cvd::SharedFD control_write_end_; |
| 64 | cvd::SharedFD control_read_end_; |
| 65 | cvd::SharedFD vhci_socket_; |
Ryan Haining | dd0dcfe | 2018-05-16 13:55:20 -0700 | [diff] [blame] | 66 | int port_{}; |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 67 | |
| 68 | VHCIInstrument(const VHCIInstrument& other) = delete; |
| 69 | VHCIInstrument& operator=(const VHCIInstrument& other) = delete; |
| 70 | }; |
| 71 | } // namespace usbip |
| 72 | } // namespace vadb |