Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
win-usb.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 #pragma once
4 
5 #include "../backend.h"
6 #include "win-helpers.h"
7 
8 #include <winusb.h>
9 #include <SetupAPI.h>
10 
11 #define HW_MONITOR_BUFFER_SIZE (1024)
12 
13 namespace librealsense
14 {
15  namespace platform
16  {
17  enum class pipe_direction
18  {
19  InPipe,
20  OutPipe
21  };
22 
24  {
25  public:
26  explicit usb_interface(HANDLE& deviceHandle);
27  ~usb_interface();
28 
29  void wait_for_async_op(OVERLAPPED &hOvl, ULONG &lengthTransferred, DWORD TimeOut, pipe_direction pipeDirection, bool* isExitOnTimeout) const;
30  bool wait_for_async_op_interrupt(OVERLAPPED &hOvl, ULONG &lengthTransferred) const;
31  bool read_pipe(unsigned char* buffer, ULONG bufferLength, PULONG lengthTransferred, LPOVERLAPPED hOvl) const;
32  bool write_pipe(const unsigned char* buffer, ULONG bufferLength, PULONG lengthTransferred, LPOVERLAPPED hOvl) const;
33  void reset_pipe(pipe_direction outPipe) const;
34  bool read_interupt_pipe(unsigned char* buffer, ULONG bufferLength, PULONG lengthTransferred, LPOVERLAPPED hOvl) const;
35  void reset_interrupt_pipe() const;
36 
37  private:
38  void init_interface(HANDLE deviceHandle);
39  void release();
40  void init_winusb_pipe();
41  void query_endpoints();
42  void set_timeout_policy(unsigned char pipeID, unsigned long timeOutInMs) const;
43 
44  unsigned long _in_out_pipe_timeout_ms = 7000;
45  WINUSB_INTERFACE_HANDLE _interface_handle;
46  UCHAR _out_pipe_id = 0;
47  UCHAR _in_pipe_id = 0;
48  UCHAR _interrupt_pipe_id = 0;
49  };
50 
51 
53  {
54  public:
55  explicit winusb_device(std::wstring lpDevicePath);
56  ~winusb_device();
57  usb_interface& get_interface() const;
58  void recreate_interface();
59 
60  private:
61  void open(std::wstring lpDevicePath);
62  void release();
63 
64  std::wstring _lp_device_path;
65  HANDLE _device_handle;
66  std::unique_ptr<usb_interface> _usb_interface;
67  };
68 
69  class usb_enumerate {
70  public:
71  static std::vector<std::wstring> query_by_interface(const std::string& guid, const std::string& vid, const std::string& pid);
72  static std::string get_camera_id(const wchar_t* device_id);
73  private:
74  static bool get_device_data(_GUID guid, int i, const HDEVINFO& device_info, std::wstring& lp_device_path);
75 
76  };
77 
79  {
80  public:
81  std::vector<uint8_t> send_receive(
82  const std::vector<uint8_t>& data,
83  int timeout_ms = 5000,
84  bool require_response = true) override;
85 
86  explicit winusb_bulk_transfer(const usb_device_info& info);
87  const wchar_t* get_path() const;
88 
89  private:
90  void write_to_pipe_and_read_response(const std::vector<uint8_t>& input,
91  std::vector<uint8_t>& output,
92  DWORD TimeOut, bool readResponse = true);
93 
94  static bool read_pipe_sync(winusb_device* usbDevice, unsigned char* buffer, int bufferLength, ULONG& lengthTransferred, DWORD TimeOut);
95 
96  std::wstring _lp_device_path = L"";
97  named_mutex _named_mutex;
98  };
99 
100  class winapi_error : public std::runtime_error
101  {
102  public:
103  explicit winapi_error(const char* message);
104 
105  static std::string last_error_string(DWORD lastError);
106  static std::string generate_message(const std::string& message);
107  };
108 
109 
110  }
111 }
Definition: backend.h:108
Definition: win-usb.h:100
Definition: backend-v4l2.h:80
Definition: algo.h:16
Definition: backend-v4l2.h:50
Definition: context.h:46
Definition: backend.h:572
pipe_direction
Definition: win-usb.h:17