device-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 "device-libusb.h"
5 #include "types.h"
6 
7 
8 namespace librealsense
9 {
10  namespace platform
11  {
12  usb_device_libusb::usb_device_libusb(libusb_device* device, const libusb_device_descriptor& desc, const usb_device_info& info, std::shared_ptr<usb_context> context) :
13  _device(device), _usb_device_descriptor(desc), _info(info), _context(context)
14  {
15  usb_descriptor ud = {desc.bLength, desc.bDescriptorType, std::vector<uint8_t>(desc.bLength)};
16  memcpy(ud.data.data(), &desc, desc.bLength);
17  _descriptors.push_back(ud);
18 
19  for (uint8_t c = 0; c < desc.bNumConfigurations; ++c)
20  {
21  libusb_config_descriptor *config{};
22  auto ret = libusb_get_config_descriptor(device, c, &config);
23  if (LIBUSB_SUCCESS != ret)
24  {
25  LOG_WARNING("failed to read USB config descriptor: error = " << std::dec << ret);
26  continue;
27  }
28 
29  std::shared_ptr<usb_interface_libusb> curr_ctrl_intf;
30  for (uint8_t i = 0; i < config->bNumInterfaces; ++i)
31  {
32  auto inf = config->interface[i];
33  auto curr_inf = std::make_shared<usb_interface_libusb>(inf);
34  _interfaces.push_back(curr_inf);
35  switch (inf.altsetting->bInterfaceClass)
36  {
38  {
39  if(inf.altsetting->bInterfaceSubClass == RS2_USB_SUBCLASS_VIDEO_CONTROL)
40  curr_ctrl_intf = curr_inf;
41  if(inf.altsetting->bInterfaceSubClass == RS2_USB_SUBCLASS_VIDEO_STREAMING)
42  curr_ctrl_intf->add_associated_interface(curr_inf);
43  break;
44  }
45  default:
46  break;
47  }
48  for(int j = 0; j < inf.num_altsetting; j++)
49  {
50  auto d = inf.altsetting[j];
51  usb_descriptor ud = {d.bLength, d.bDescriptorType, std::vector<uint8_t>(d.bLength)};
52  memcpy(ud.data.data(), &d, d.bLength);
53  _descriptors.push_back(ud);
54  for(int k = 0; k < d.extra_length; )
55  {
56  auto l = d.extra[k];
57  auto dt = d.extra[k+1];
58  usb_descriptor ud = {l, dt, std::vector<uint8_t>(l)};
59  memcpy(ud.data.data(), &d.extra[k], l);
60  _descriptors.push_back(ud);
61  k += l;
62  }
63  }
64  }
65 
66  libusb_free_config_descriptor(config);
67  }
68  libusb_ref_device(_device);
69  }
70 
72  {
73  libusb_unref_device(_device);
74  }
75 
77  {
78  auto it = std::find_if(_interfaces.begin(), _interfaces.end(),
79  [interface_number](const rs_usb_interface& i) { return interface_number == i->get_number(); });
80  if (it == _interfaces.end())
81  return nullptr;
82  return *it;
83  }
84 
85  std::shared_ptr<handle_libusb> usb_device_libusb::get_handle(uint8_t interface_number)
86  {
87  try
88  {
89  auto i = get_interface(interface_number);
90  if (!i)
91  return nullptr;
92  auto intf = std::dynamic_pointer_cast<usb_interface_libusb>(i);
93  return std::make_shared<handle_libusb>(_context, _device, intf);
94  }
95  catch(const std::exception& e)
96  {
97  return nullptr;
98  }
99  }
100 
101  const std::shared_ptr<usb_messenger> usb_device_libusb::open(uint8_t interface_number)
102  {
103  auto h = get_handle(interface_number);
104  if(!h)
105  return nullptr;
106  return std::make_shared<usb_messenger_libusb>(shared_from_this(), h);
107  }
108  }
109 }
std::vector< std::shared_ptr< usb_interface > > _interfaces
Definition: device-libusb.h:29
std::shared_ptr< usb_interface > rs_usb_interface
Definition: usb-interface.h:31
#define LOG_WARNING(...)
Definition: src/types.h:241
double dt
Definition: boing.c:106
std::shared_ptr< handle_libusb > get_handle(uint8_t interface_number)
usb_device_libusb(libusb_device *device, const libusb_device_descriptor &desc, const usb_device_info &info, std::shared_ptr< usb_context > context)
virtual const rs_usb_messenger open(uint8_t interface_number) override
d
Definition: rmse.py:171
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
unsigned char uint8_t
Definition: stdint.h:78
e
Definition: rmse.py:177
def info(name, value, persistent=False)
Definition: test.py:301
virtual const rs_usb_interface get_interface(uint8_t interface_number) const override
const GLubyte * c
Definition: glext.h:12690
GLint j
static auto it
int i
std::shared_ptr< usb_context > _context
Definition: device-libusb.h:31
std::vector< usb_descriptor > _descriptors
Definition: device-libusb.h:30


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