Ocean
system/usb/Utilities.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_SYSTEM_USB_UTILITIES_H
9 #define META_OCEAN_SYSTEM_USB_UTILITIES_H
10 
11 #include "ocean/system/usb/USB.h"
12 
13 namespace Ocean
14 {
15 
16 namespace System
17 {
18 
19 namespace USB
20 {
21 
22 /**
23  * This class implements utility functions for USB devices.
24  * @ingroup systemusb
25  */
26 class OCEAN_SYSTEM_USB_EXPORT Utilities
27 {
28  public:
29 
30  /**
31  * Definition of individual device categories.
32  */
33  enum DeviceCategory : uint32_t
34  {
35  /// An unknown device category.
36  DC_UNKNOWN = 0u,
37  /// A device which contains a camera in some way.
38  DC_CAMERA = 1u << 0u
39  };
40 
41  protected:
42 
43  /**
44  * This class holds the relevant information of a USB product.
45  */
46  class Product
47  {
48  public:
49 
50  /**
51  * Default constructor.
52  */
53  Product() = default;
54 
55  /**
56  * Creates a new product object.
57  * @param deviceName The name of the product, must be valid
58  * @param deviceCategory The category of the product, must be valid
59  */
60  inline Product(std::string&& deviceName, const DeviceCategory deviceCategory);
61 
62  public:
63 
64  /// The name of the product.
65  std::string deviceName_;
66 
67  /// The category of the product.
68  DeviceCategory deviceCategory_ = DC_UNKNOWN;
69  };
70 
71  /**
72  * Definition of an unordered map mapping product ids to products.
73  */
74  using ProductMap = std::unordered_map<uint16_t, Product>;
75 
76  /**
77  * This class holds several products of a vendor.
78  */
79  class Vendor
80  {
81  public:
82 
83  /**
84  * Default constructor.
85  */
86  Vendor() = default;
87 
88  /**
89  * Creates a new vendor object.
90  * @param vendorName The name of the vendor, must be valid
91  * @param productMap The map mapping the ids of products (the vendor's product ids) to product objects, must be valid
92  */
93  inline Vendor(std::string&& vendorName, ProductMap&& productMap);
94 
95  public:
96 
97  /// The name of the vendor.
98  std::string vendorName_;
99 
100  /// The map with all known products of the vendor.
102  };
103 
104  /**
105  * Definition of an unordered map mapping vendor ids to vendor objects (a lookup table for USB devices).
106  */
107  using VendorMap = std::unordered_map<uint16_t, Vendor>;
108 
109  public:
110 
111  /**
112  * Lookups the name of a vendor id (if known).
113  * @return vendorId The id of the vendor, must be valid
114  * @return The name of the vendor, empty if the id is not stored in the lookup table
115  */
116  static std::string vendorName(const uint16_t vendorId);
117 
118  /**
119  * Lookups the name of a product id (if known).
120  * @param vendorId The id of the vendor, must be valid
121  * @param productId The id of the product, must be valid
122  * @return The name of the product, empty if not stored in the lookup table
123  */
124  static std::string productName(const uint16_t vendorId, const uint16_t productId);
125 
126  /**
127  * Lookups the device category of a device defined by it's vendor and product id.
128  * @param vendorId The id of the vendor, must be valid
129  * @param productId The id of the product, must be valid
130  * @return The device category of the product, DC_UNKNOWN if not stored in the lookup table
131  */
132  static DeviceCategory deviceCategory(const uint16_t vendorId, const uint16_t productId);
133 
134  /**
135  * Checks whether a device contains a specific category.
136  * In case the vendor id or product id is not stored in the lookup table, the device class is used as a backup.
137  * @param vendorId The id of the vendor, must be valid
138  * @param productId The id of the product, must be valid
139  * @param deviceClass The class of the device, must be valid
140  * @param deviceCategory The category to be checked, may be a combination of several categories
141  * @return True, if so
142  */
143  static bool doesDeviceContainCategory(const uint16_t vendorId, const uint16_t productId, const uint8_t deviceClass, const DeviceCategory deviceCategory);
144 
145  /**
146  * Checks whether a device contains a specific category.
147  * In case the vendor id or product id is not stored in the lookup table, the device class is used as a backup.
148  * @param deviceDescriptor The libusb device descriptor of the device, must be valid
149  * @param deviceCategory The category to be checked, may be a combination of several categories
150  * @return True, if so
151  */
152  static inline bool doesDeviceContainCategory(const libusb_device_descriptor& deviceDescriptor, const DeviceCategory deviceCategory);
153 
154  protected:
155 
156  /**
157  * Returns the lookup table with all known vendors.
158  * @return The vendor lookup table
159  */
160  static const VendorMap& vendorMap();
161 };
162 
163 inline Utilities::Product::Product(std::string&& deviceName, const DeviceCategory deviceCategory) :
164  deviceName_(std::move(deviceName)),
165  deviceCategory_(deviceCategory)
166 {
167  // nothing to do here
168 }
169 
170 inline Utilities::Vendor::Vendor(std::string&& vendorName, ProductMap&& productMap) :
171  vendorName_(std::move(vendorName)),
172  productMap_(std::move(productMap))
173 {
174  // nothing to do here
175 }
176 
177 inline bool Utilities::doesDeviceContainCategory(const libusb_device_descriptor& deviceDescriptor, const DeviceCategory deviceCategory)
178 {
179  return doesDeviceContainCategory(deviceDescriptor.idVendor, deviceDescriptor.idProduct, deviceDescriptor.bDeviceClass, deviceCategory);
180 }
181 
182 }
183 
184 }
185 
186 }
187 
188 #endif // META_OCEAN_SYSTEM_USB_UTILITIES_H
This class holds the relevant information of a USB product.
Definition: system/usb/Utilities.h:47
std::string deviceName_
The name of the product.
Definition: system/usb/Utilities.h:65
Product()=default
Default constructor.
This class holds several products of a vendor.
Definition: system/usb/Utilities.h:80
std::string vendorName_
The name of the vendor.
Definition: system/usb/Utilities.h:98
ProductMap productMap_
The map with all known products of the vendor.
Definition: system/usb/Utilities.h:101
Vendor()=default
Default constructor.
This class implements utility functions for USB devices.
Definition: system/usb/Utilities.h:27
DeviceCategory
Definition of individual device categories.
Definition: system/usb/Utilities.h:34
static DeviceCategory deviceCategory(const uint16_t vendorId, const uint16_t productId)
Lookups the device category of a device defined by it's vendor and product id.
static bool doesDeviceContainCategory(const uint16_t vendorId, const uint16_t productId, const uint8_t deviceClass, const DeviceCategory deviceCategory)
Checks whether a device contains a specific category.
static const VendorMap & vendorMap()
Returns the lookup table with all known vendors.
static std::string vendorName(const uint16_t vendorId)
Lookups the name of a vendor id (if known).
std::unordered_map< uint16_t, Product > ProductMap
Definition of an unordered map mapping product ids to products.
Definition: system/usb/Utilities.h:74
static std::string productName(const uint16_t vendorId, const uint16_t productId)
Lookups the name of a product id (if known).
std::unordered_map< uint16_t, Vendor > VendorMap
Definition of an unordered map mapping vendor ids to vendor objects (a lookup table for USB devices).
Definition: system/usb/Utilities.h:107
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15