enumerator-libusb.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include "usb/usb-enumerator.h"
5 #include "libusb/device-libusb.h"
6 #include "context-libusb.h"
7 #include "types.h"
8 
9 #include <libusb.h>
10 
11 namespace librealsense
12 {
13  namespace platform
14  {
16  {
17  auto usb_bus = std::to_string(libusb_get_bus_number(usb_device));
18 
19  // As per the USB 3.0 specs, the current maximum limit for the depth is 7.
20  const auto max_usb_depth = 8;
21  uint8_t usb_ports[max_usb_depth] = {};
22  std::stringstream port_path;
23  auto port_count = libusb_get_port_numbers(usb_device, usb_ports, max_usb_depth);
24  auto usb_dev = std::to_string(libusb_get_device_address(usb_device));
25  libusb_device_descriptor dev_desc;
26  auto r= libusb_get_device_descriptor(usb_device,&dev_desc);
27 
28  for (size_t i = 0; i < port_count; ++i)
29  {
30  port_path << std::to_string(usb_ports[i]) << (((i+1) < port_count)?".":"");
31  }
32 
33  return usb_bus + "-" + port_path.str() + "-" + usb_dev;
34  }
35 
36  std::vector<usb_device_info> get_subdevices(libusb_device* device, libusb_device_descriptor desc)
37  {
38  std::vector<usb_device_info> rv;
39  for (uint8_t c = 0; c < desc.bNumConfigurations; ++c)
40  {
41  libusb_config_descriptor *config = nullptr;
42  auto ret = libusb_get_config_descriptor(device, c, &config);
43  if (LIBUSB_SUCCESS != ret)
44  {
45  LOG_WARNING("failed to read USB config descriptor: error = " << std::dec << ret);
46  continue;
47  }
48 
49  for (uint8_t i = 0; i < config->bNumInterfaces; ++i)
50  {
51  auto inf = config->interface[i];
52 
53  //avoid publish streaming interfaces TODO:MK
54  if(inf.altsetting->bInterfaceSubClass == 2)
55  continue;
56  // when device is in DFU state, two USB devices are detected, one of RS2_USB_CLASS_VENDOR_SPECIFIC (255) class
57  // and the other of RS2_USB_CLASS_APPLICATION_SPECIFIC (254) class.
58  // in order to avoid listing two usb devices for a single physical device we ignore the application specific class
59  // https://www.usb.org/defined-class-codes#anchor_BaseClassFEh
60  if(inf.altsetting->bInterfaceClass == RS2_USB_CLASS_APPLICATION_SPECIFIC)
61  continue;
62 
64  auto path = get_device_path(device);
65  info.id = path;
66  info.unique_id = path;
67  info.conn_spec = usb_spec(desc.bcdUSB);
68  info.vid = desc.idVendor;
69  info.pid = desc.idProduct;
70  info.mi = i;
71  info.cls = usb_class(inf.altsetting->bInterfaceClass);
72  rv.push_back(info);
73  }
74 
75  libusb_free_config_descriptor(config);
76  }
77  return rv;
78  }
79 
80  std::vector<usb_device_info> usb_enumerator::query_devices_info()
81  {
82  std::vector<usb_device_info> rv;
83  auto ctx = std::make_shared<usb_context>();
84 
85  for (uint8_t idx = 0; idx < ctx->device_count(); ++idx)
86  {
87  auto device = ctx->get_device(idx);
88  if(device == nullptr)
89  continue;
90  libusb_device_descriptor desc{};
91 
92 
93  auto ret = libusb_get_device_descriptor(device, &desc);
94  if (LIBUSB_SUCCESS == ret)
95  {
96  auto sd = get_subdevices(device, desc);
97  rv.insert(rv.end(), sd.begin(), sd.end());
98  }
99  else
100  LOG_WARNING("failed to read USB device descriptor: error = " << std::dec << ret);
101  }
102  return rv;
103  }
104 
106  {
107  auto ctx = std::make_shared<usb_context>();
108 
109  for (uint8_t idx = 0; idx < ctx->device_count(); ++idx)
110  {
111  auto device = ctx->get_device(idx);
112  if(device == nullptr || get_device_path(device) != info.id)
113  continue;
114 
115  libusb_device_descriptor desc{};
116  auto ret = libusb_get_device_descriptor(device, &desc);
117  if (LIBUSB_SUCCESS == ret)
118  {
119  try
120  {
121  return std::make_shared<usb_device_libusb>(device, desc, info, ctx);
122  }
123  catch (std::exception e)
124  {
125  LOG_WARNING("failed to create usb device at index: %d" << idx);
126  }
127  }
128  else
129  LOG_WARNING("failed to read USB device descriptor: error = " << std::dec << ret);
130  }
131  return nullptr;
132  }
133  }
134 }
static rs_usb_device create_usb_device(const usb_device_info &info)
static std::vector< usb_device_info > query_devices_info()
std::string get_device_path(libusb_device *usb_device)
#define LOG_WARNING(...)
Definition: src/types.h:241
enum librealsense::platform::_usb_class usb_class
GLsizei const GLchar *const * path
Definition: glext.h:4276
GLsizei const GLchar *const * string
unsigned char uint8_t
Definition: stdint.h:78
e
Definition: rmse.py:177
def info(name, value, persistent=False)
Definition: test.py:301
std::vector< usb_device_info > get_subdevices(libusb_device *device, libusb_device_descriptor desc)
const GLubyte * c
Definition: glext.h:12690
GLdouble GLdouble r
int i
auto device
Definition: pyrs_net.cpp:17
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29
std::string to_string(T value)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14