Home | History | Annotate | Download | only in usbip
      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 
     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:
     29   VHCIInstrument(int port, const std::string& name);
     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();
     57   bool VerifyPortIsFree() const;
     58 
     59  private:
     60   std::string name_;
     61   std::thread attach_thread_;
     62   std::string syspath_;
     63   cvd::SharedFD control_write_end_;
     64   cvd::SharedFD control_read_end_;
     65   cvd::SharedFD vhci_socket_;
     66   int port_{};
     67 
     68   VHCIInstrument(const VHCIInstrument& other) = delete;
     69   VHCIInstrument& operator=(const VHCIInstrument& other) = delete;
     70 };
     71 }  // namespace usbip
     72 }  // namespace vadb
     73