device-winusb.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 #if (_MSC_FULL_VER < 180031101)
5 #error At least Visual Studio 2013 Update 4 is required to compile this backend
6 #endif
7 
8 #include "device-winusb.h"
9 #include "win/win-helpers.h"
10 
11 #include "endpoint-winusb.h"
12 #include "interface-winusb.h"
13 #include "types.h"
14 
15 #include <atlstr.h>
16 #include <Windows.h>
17 #include <Sddl.h>
18 #include <string>
19 #include <regex>
20 #include <sstream>
21 #include <mutex>
22 #include <usbspec.h>
23 
24 #include <SetupAPI.h>
25 
26 
27 #pragma comment(lib, "winusb.lib")
28 
29 namespace librealsense
30 {
31  namespace platform
32  {
33  USB_DEVICE_DESCRIPTOR get_device_descriptor(WINUSB_INTERFACE_HANDLE handle)
34  {
35  USB_DEVICE_DESCRIPTOR desc;
36  ULONG return_length = 0;
37  if (!WinUsb_GetDescriptor(handle, USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, (PUCHAR)&desc, sizeof(desc), &return_length))
38  {
39  throw winapi_error("WinUsb action failed, last error: " + GetLastError());
40  }
41  return desc;
42  }
43 
44  std::vector<uint8_t> get_device_descriptors(WINUSB_INTERFACE_HANDLE handle)
45  {
46  USB_CONFIGURATION_DESCRIPTOR desc;
47  ULONG return_length = 0;
48  if (!WinUsb_GetDescriptor(handle, USB_CONFIGURATION_DESCRIPTOR_TYPE, 0, 0, (PUCHAR)&desc, sizeof(desc), &return_length))
49  {
50  throw winapi_error("WinUsb action failed, last error: " + GetLastError());
51  }
52 
53  std::vector<uint8_t> rv(desc.wTotalLength);
54 
55  // Returns configuration descriptor - including all interface, endpoint, class-specific, and vendor-specific descriptors
56  if (!WinUsb_GetDescriptor(handle, USB_CONFIGURATION_DESCRIPTOR_TYPE, 0, 0, rv.data(), desc.wTotalLength, &return_length))
57  {
58  throw winapi_error("WinUsb action failed, last error: " + GetLastError());
59  }
60 
61  return rv;
62  }
63 
64  void usb_device_winusb::parse_descriptor(WINUSB_INTERFACE_HANDLE handle)
65  {
66  auto device_descriptor = get_device_descriptor(handle);
67  _info.conn_spec = (usb_spec)device_descriptor.bcdUSB;
68 
69  auto descriptors = get_device_descriptors(handle);
70 
71  for (int i = 0; i < descriptors.size(); )
72  {
73  auto l = descriptors[i];
74  auto dt = descriptors[i + 1];
75  usb_descriptor ud = { l, dt, std::vector<uint8_t>(l) };
76  memcpy(ud.data.data(), &descriptors[i], l);
77  _descriptors.push_back(ud);
78  i += l;
79  }
80  }
81 
82  std::vector<std::shared_ptr<usb_interface>> usb_device_winusb::query_device_interfaces(const std::wstring& path)
83  {
84  std::vector<std::shared_ptr<usb_interface>> rv;
86  auto sts = handle.open(path);
87  if (sts != RS2_USB_STATUS_SUCCESS)
88  throw winapi_error("WinUsb action failed, " + GetLastError());
89 
90  auto handles = handle.get_handles();
91  auto descriptors = handle.get_descriptors();
92 
93  for (auto&& i : handle.get_handles())
94  {
95  auto d = descriptors.at(i.first);
96  auto intf = std::make_shared<usb_interface_winusb>(i.second, d, path);
97  rv.push_back(intf);
98  }
99 
101 
102 
103  return rv;
104  }
105 
106  usb_device_winusb::usb_device_winusb(const usb_device_info& info, std::vector<std::wstring> devices_path)
107  : _info(info)
108  {
109  for (auto&& device_path : devices_path)
110  {
111  auto intfs = query_device_interfaces(device_path);
112  _interfaces.insert(_interfaces.end(), intfs.begin(), intfs.end());
113  }
114  }
115 
117  {
118  auto it = std::find_if(_interfaces.begin(), _interfaces.end(),
119  [interface_number](const rs_usb_interface& i) { return interface_number == i->get_number(); });
120  if (it == _interfaces.end())
121  return nullptr;
122  return *it;
123  }
124 
125  const std::shared_ptr<usb_messenger> usb_device_winusb::open(uint8_t interface_number)
126  {
127  auto i = get_interface(interface_number);
128  if (!i)
129  return nullptr;
130  auto intf = std::static_pointer_cast<usb_interface_winusb>(i);
131  auto dh = std::make_shared<handle_winusb>();
132  auto sts = dh->open(intf->get_device_path());
133  if (sts != RS2_USB_STATUS_SUCCESS)
134  return nullptr;
135  return std::make_shared<usb_messenger_winusb>(shared_from_this(), dh);
136  }
137  }
138 }
std::shared_ptr< usb_interface > rs_usb_interface
Definition: usb-interface.h:31
usb_status open(const std::wstring &path, uint32_t timeout_ms=1000)
Definition: handle-winusb.h:33
double dt
Definition: boing.c:106
GLuint64 GLenum void * handle
Definition: glext.h:7785
GLsizei const GLchar *const * path
Definition: glext.h:4276
USB_DEVICE_DESCRIPTOR get_device_descriptor(WINUSB_INTERFACE_HANDLE handle)
d
Definition: rmse.py:171
unsigned char uint8_t
Definition: stdint.h:78
const std::map< int, USB_INTERFACE_DESCRIPTOR > get_descriptors()
const std::map< int, WINUSB_INTERFACE_HANDLE > get_handles()
std::vector< rs_usb_interface > _interfaces
Definition: device-winusb.h:43
void parse_descriptor(WINUSB_INTERFACE_HANDLE handle)
virtual const rs_usb_messenger open(uint8_t interface_number) override
def info(name, value, persistent=False)
Definition: test.py:301
std::vector< usb_descriptor > _descriptors
Definition: device-winusb.h:44
virtual const rs_usb_interface get_interface(uint8_t interface_number) const override
const WINUSB_INTERFACE_HANDLE get_first_interface()
std::vector< uint8_t > get_device_descriptors(WINUSB_INTERFACE_HANDLE handle)
static auto it
usb_device_winusb(const usb_device_info &info, std::vector< std::wstring > devices_path)
std::vector< std::shared_ptr< usb_interface > > query_device_interfaces(const std::wstring &path)
int i


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