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 <map>
     19 #include <string>
     20 
     21 #include "host/libs/usbip/device.h"
     22 
     23 namespace vadb {
     24 namespace usbip {
     25 // Container for all virtual USB/IP devices.
     26 // Stores devices by virtual BUS ID.
     27 class DevicePool {
     28  public:
     29   // BusDevNumber is a pair uniquely identifying bus and device.
     30   struct BusDevNumber {
     31     uint16_t bus_number;
     32     uint16_t dev_number;
     33 
     34     bool operator<(BusDevNumber other) const {
     35       return (bus_number << 16 | dev_number) <
     36              (other.bus_number << 16 | other.dev_number);
     37     }
     38   };
     39 
     40   // Internal container type.
     41   using MapType = std::map<BusDevNumber, std::unique_ptr<Device>>;
     42 
     43   DevicePool() = default;
     44   virtual ~DevicePool() = default;
     45 
     46   // Add new device associated with virtual BUS ID.
     47   void AddDevice(BusDevNumber bus_id, std::unique_ptr<Device> device);
     48 
     49   // Get device associated with supplied virtual bus/device number.
     50   Device* GetDevice(BusDevNumber bus_dev_num) const;
     51 
     52   // Get total number of USB/IP devices.
     53   size_t Size() const { return devices_.size(); }
     54 
     55   MapType::const_iterator begin() const { return devices_.cbegin(); }
     56   MapType::const_iterator end() const { return devices_.cend(); }
     57 
     58  private:
     59   MapType devices_;
     60 
     61   DevicePool(const DevicePool&) = delete;
     62   DevicePool& operator=(const DevicePool&) = delete;
     63 };
     64 
     65 }  // namespace usbip
     66 }  // namespace vadb
     67