enumerator-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 "usb/usb-enumerator.h"
9 #include "device-winusb.h"
10 #include "interface-winusb.h"
11 #include "win/win-helpers.h"
12 #include "types.h"
13 
14 #include <atlstr.h>
15 #include <Windows.h>
16 #include <Sddl.h>
17 #include <string>
18 #include <regex>
19 #include <sstream>
20 #include <mutex>
21 
22 #include <Cfgmgr32.h>
23 #include <SetupAPI.h>
24 
25 #pragma comment(lib, "winusb.lib")
26 
27 namespace librealsense
28 {
29  namespace platform
30  {
31  //https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/supported-usb-classes#microsoft-provided-usb-device-class-drivers
32  const std::map<std::string, usb_class> guids = {
33  {"{175695cd-30d9-4f87-8be3-5a8270f49a31}", RS2_USB_CLASS_VENDOR_SPECIFIC}, //Ivcam HWM
34  {"{a5dcbf10-6530-11d2-901f-00c04fb951ed}", RS2_USB_CLASS_UNSPECIFIED}, // for DFU
35  {"{ca3e7ab9-b4c3-4ae6-8251-579ef933890f}", RS2_USB_CLASS_VIDEO}, // UVC win 10
36  {"{08090549-ce78-41dc-a0fb-1bd66694bb0c}", RS2_USB_CLASS_VENDOR_SPECIFIC}, //HWM win 10
37  {"{68f2c451-0c22-415e-8293-7a903437e725}", RS2_USB_CLASS_VIDEO}, // UVC win 7
38  {"{ee390e5d-4f81-4543-a405-3686e712dc7b}", RS2_USB_CLASS_HID}, //HID win 7
39  {"{2f8549de-7dc3-4e5c-9821-d71ba00bec8c}", RS2_USB_CLASS_VENDOR_SPECIFIC}, //HWM win 7
40  };
41 
42  std::vector<std::wstring> query_by_interface(GUID guid)
43  {
44  std::vector<std::wstring> rv;
45 
46  CONFIGRET cr = CR_SUCCESS;
47  ULONG length = 0;
48 
49  do {
50  cr = CM_Get_Device_Interface_List_Size(&length, (LPGUID)&guid, NULL,
51  CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
52 
53  if (cr != CR_SUCCESS)
54  break;
55 
56  std::vector<WCHAR> device_list(length);
57  cr = CM_Get_Device_Interface_List((LPGUID)&guid, NULL, device_list.data(), length,
58  CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
59 
60  size_t offset = 0;
61  while (offset < length)
62  {
63  auto str = std::wstring((device_list.data() + offset));
64  if(!str.empty())
65  rv.push_back(str);
66  offset += str.size() + 1;
67  }
68  } while (cr == CR_BUFFER_SMALL);
69 
70  return rv;
71  }
72 
73  std::vector<std::wstring> query_by_interface(const std::string& guidStr)
74  {
75  GUID guid;
76  std::wstring guidWStr(guidStr.begin(), guidStr.end());
77  CHECK_HR(CLSIDFromString(guidWStr.c_str(), static_cast<LPCLSID>(&guid)));
78  return query_by_interface(guid);
79  }
80 
81  usb_device_info get_info(const std::wstring device_wstr)
82  {
83  usb_device_info rv{};
84  std::smatch matches;
85  std::string device_str(device_wstr.begin(), device_wstr.end());
86 
87  std::regex regex_camera_interface("\\b(.*VID_)(.*)(&PID_)(.*)(&MI_)(.*)(#.*&)(.*)(&.*)(&.*)(.*#)(.*)", std::regex_constants::icase);
88  std::regex regex_usb_interface("\\b(.*VID_)(.*)(&PID_)(.*)(#.*&)(.*)(&.*)(&.*)", std::regex_constants::icase);
89  std::regex regex_dfu_interface("\\b(.*VID_)(.*)(&PID_)(.*)(#)(.*)(#)(.*)", std::regex_constants::icase);
90 
91  if (std::regex_search(device_str, matches, regex_camera_interface) && matches.size() == 13)
92  {
93  rv.id = device_str;
94  std::stringstream vid; vid << std::hex << matches[2]; vid >> rv.vid;
95  std::stringstream pid; pid << std::hex << matches[4]; pid >> rv.pid;
96  std::stringstream mi; mi << std::hex << matches[6]; mi >> rv.mi;
97  std::stringstream uid; uid << std::hex << matches[8]; uid >> rv.unique_id;
98  auto it = guids.find(matches[12]);
99  rv.cls = it != guids.end() ? it->second : RS2_USB_CLASS_UNSPECIFIED;
100  return rv;
101  }
102  if (std::regex_search(device_str, matches, regex_usb_interface) && matches.size() == 9)
103  {
104  rv.id = device_str;
105  std::stringstream vid; vid << std::hex << matches[2]; vid >> rv.vid;
106  std::stringstream pid; pid << std::hex << matches[4]; pid >> rv.pid;
107  std::stringstream uid; uid << std::hex << matches[6]; uid >> rv.unique_id;
109  return rv;
110  }
111  if (std::regex_search(device_str, matches, regex_dfu_interface))
112  {
113  rv.id = device_str;
114  std::stringstream vid; vid << std::hex << matches[2]; vid >> rv.vid;
115  std::stringstream pid; pid << std::hex << matches[4]; pid >> rv.pid;
116  std::stringstream uid; uid << std::hex << matches[6]; uid >> rv.unique_id;
117  rv.cls = RS2_USB_CLASS_UNSPECIFIED;
118  return rv;
119  }
120 
121  return rv;
122  }
123 
124  std::vector<usb_device_info> usb_enumerator::query_devices_info()
125  {
126  std::vector<usb_device_info> rv;
127 
128  for (auto&& guid : guids)
129  {
130  for (auto&& id : query_by_interface(guid.first))
131  {
132  auto info = get_info(id.c_str());
133  if (info.vid == 0) //unsupported device
134  continue;
135  rv.push_back(info);
136  }
137  }
138  return rv;
139  }
140 
142  {
143  std::vector<std::wstring> devices_path;
144 
145  for (auto&& guid : guids)
146  {
147  for (auto&& id : query_by_interface(guid.first))
148  {
149  auto i = get_info(id.c_str());
150  if ((i.vid == info.vid) && (i.pid == info.pid) && (i.id == info.id))
151  devices_path.push_back(id);
152  }
153  }
154 
155  if (devices_path.size() == 0)
156  {
157  LOG_WARNING("failed to locate usb interfaces for device: " << info.id);
158  return nullptr;
159  }
160 
161  try
162  {
163  return std::make_shared<usb_device_winusb>(info, devices_path);
164  }
165  catch (std::exception e)
166  {
167  LOG_WARNING("failed to create usb device: " << info.id << ", error: " << e.what());
168  }
169  return nullptr;
170  }
171  }
172 }
static rs_usb_device create_usb_device(const usb_device_info &info)
static std::vector< usb_device_info > query_devices_info()
#define LOG_WARNING(...)
Definition: src/types.h:241
std::vector< std::wstring > query_by_interface(GUID guid)
matches
Definition: test-fg.py:19
#define CHECK_HR(x)
Definition: win-helpers.h:23
GLsizei const GLchar *const * string
e
Definition: rmse.py:177
def info(name, value, persistent=False)
Definition: test.py:301
usb_device_info get_info(const std::wstring device_wstr)
string cr
Definition: log.py:31
const std::map< std::string, usb_class > guids
static auto it
static const FGuid GUID
#define NULL
Definition: tinycthread.c:47
int i
GLenum GLuint GLenum GLsizei length
GLintptr offset
std::shared_ptr< usb_device > rs_usb_device
Definition: usb-device.h:29


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