handle-winusb.h
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 #pragma once
5 
6 #include "win/win-helpers.h"
7 #include "types.h"
8 
9 #include <winusb.h>
10 #include <chrono>
11 
12 namespace librealsense
13 {
14  namespace platform
15  {
16  static usb_status winusb_status_to_rs(DWORD sts)
17  {
18  switch (sts)
19  {
20  //TODO:MK
21  case ERROR_INVALID_CATEGORY: return RS2_USB_STATUS_IO;
22  case ERROR_BROKEN_PIPE: return RS2_USB_STATUS_PIPE;
23  case ERROR_ACCESS_DENIED: return RS2_USB_STATUS_ACCESS;
24  case ERROR_DRIVE_LOCKED: return RS2_USB_STATUS_BUSY;
25  case ERROR_SEM_TIMEOUT: return RS2_USB_STATUS_TIMEOUT;
26  default: return RS2_USB_STATUS_OTHER;
27  }
28  }
29 
31  {
32  public:
33  usb_status open(const std::wstring& path, uint32_t timeout_ms = 1000)
34  {
36  do
37  {
38  _device_handle = CreateFile(path.c_str(),
39  GENERIC_READ | GENERIC_WRITE,
40  FILE_SHARE_READ | FILE_SHARE_WRITE,
41  NULL,
42  OPEN_EXISTING,
43  FILE_FLAG_OVERLAPPED,
44  NULL);
46  auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
47  if (elapsed > timeout_ms)
48  break;
49  std::this_thread::sleep_for(std::chrono::milliseconds(5));
50  } while (_device_handle == INVALID_HANDLE_VALUE);
51 
52  if (_device_handle == INVALID_HANDLE_VALUE)
53  {
54  auto lastResult = GetLastError();
55  LOG_ERROR("CreateFile failed, error: " << lastResult);
56  return winusb_status_to_rs(lastResult);
57  }
58 
59  WINUSB_INTERFACE_HANDLE ah;
60  if (WinUsb_Initialize(_device_handle, &ah) == FALSE)
61  {
62  auto lastResult = GetLastError();
63  LOG_ERROR("WinUsb_Initialize failed, error: " << lastResult);
64  return winusb_status_to_rs(lastResult);
65  }
66  USB_INTERFACE_DESCRIPTOR desc;
67  if (!WinUsb_QueryInterfaceSettings(ah, 0, &desc)) {
68  throw winapi_error("WinUsb action failed, last error: " + GetLastError());
69  }
70  _handles[desc.bInterfaceNumber] = ah;
71  _descriptors[desc.bInterfaceNumber] = desc;
72 
73  for (UCHAR interface_number = 0; true; interface_number++) {
74  WINUSB_INTERFACE_HANDLE h;
75  USB_INTERFACE_DESCRIPTOR descriptor;
76 
77  if (!WinUsb_GetAssociatedInterface(ah, interface_number, &h)) {
78  auto error = GetLastError();
79  if (error != ERROR_NO_MORE_ITEMS)
80  throw winapi_error("WinUsb action failed, last error: " + error);
81  break;
82  }
83 
84  if (!WinUsb_QueryInterfaceSettings(h, 0, &descriptor)) {
85  throw winapi_error("WinUsb action failed, last error: " + GetLastError());
86  }
87  _handles[descriptor.bInterfaceNumber] = h;
88  _descriptors[descriptor.bInterfaceNumber] = descriptor;
89  }
91  }
92 
94  {
95  for (auto&& h : _handles)
96  {
97  WinUsb_Free(h.second);
98  }
99  if (_device_handle != INVALID_HANDLE_VALUE)
100  CloseHandle(_device_handle);
101  }
102 
103  const HANDLE get_device_handle() { return _device_handle; }
104  const std::map<int, WINUSB_INTERFACE_HANDLE> get_handles() { return _handles; }
105  const std::map<int, USB_INTERFACE_DESCRIPTOR> get_descriptors() { return _descriptors; }
106 
107  const WINUSB_INTERFACE_HANDLE get_first_interface() { return _handles.begin()->second; }
108  const WINUSB_INTERFACE_HANDLE get_interface_handle(uint8_t interface_number)
109  {
110  auto it = _handles.find(interface_number);
111  if (it == _handles.end())
112  throw std::runtime_error("get_interface_handle failed, interface not found");
113  return it->second;
114  }
115  private:
116  HANDLE _device_handle = INVALID_HANDLE_VALUE;
117  std::map<int,WINUSB_INTERFACE_HANDLE> _handles;
118  std::map<int, USB_INTERFACE_DESCRIPTOR> _descriptors;
119  };
120  }
121 }
std::map< int, USB_INTERFACE_DESCRIPTOR > _descriptors
GLuint GLuint end
usb_status open(const std::wstring &path, uint32_t timeout_ms=1000)
Definition: handle-winusb.h:33
GLsizei const GLchar *const * path
Definition: glext.h:4276
std::map< int, WINUSB_INTERFACE_HANDLE > _handles
static usb_status winusb_status_to_rs(DWORD sts)
Definition: handle-winusb.h:16
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
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()
enum librealsense::platform::_usb_status usb_status
unsigned int uint32_t
Definition: stdint.h:80
GLuint start
const WINUSB_INTERFACE_HANDLE get_interface_handle(uint8_t interface_number)
#define LOG_ERROR(...)
Definition: src/types.h:242
const WINUSB_INTERFACE_HANDLE get_first_interface()
static auto it
GLint GLsizei count
#define NULL
Definition: tinycthread.c:47
#define FALSE
Definition: tinycthread.c:53


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