win-helpers.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 #ifndef NOMINMAX
6 #define NOMINMAX
7 #endif
8 
9 #include "usb/usb-types.h"
10 #include <string>
11 #include <vector>
12 
13 #include <windows.h>
14 #include <cfgmgr32.h>
15 
16 #define WAIT_FOR_MUTEX_TIME_OUT (5000)
17 
18 namespace librealsense
19 {
20  namespace platform
21  {
22  bool check(const char * call, HRESULT hr, bool to_throw = true);
23 #define CHECK_HR(x) check(#x, x);
24 #define LOG_HR(x) check(#x, x, false);
25 
26  std::string win_to_utf(const WCHAR * s);
27 
28  bool is_win10_redstone2();
29 
30  std::vector<std::string> tokenize(std::string string, char separator);
31 
32  std::wstring instance_id_from_device_path( LPCWSTR path );
33  bool parse_usb_path_multiple_interface(uint16_t & vid, uint16_t & pid, uint16_t & mi, std::string & unique_id, const std::string & path, std::string & device_guid);
34  bool parse_usb_path_single_interface(uint16_t & vid, uint16_t & pid, std::string & serial, const std::string & path);
35  bool get_usb_descriptors(uint16_t device_vid, uint16_t device_pid, const std::string& device_uid, std::string& location, usb_spec& spec, std::string& serial);
36  bool get_usb_device_descriptors( DEVINST devinst, uint16_t device_vid, uint16_t device_pid, const std::string& device_uid, std::string& location, usb_spec& spec, std::string& serial, std::string& composite_uid );
37 
38  /*
39  Configuration Management (CM) tree node.
40  See https://docs.microsoft.com/en-gb/windows/win32/api/cfgmgr32/
41 
42  A basic wrapper around DEVINST and the functionality we need around it, for ease of use.
43  */
44  class cm_node
45  {
46  DEVINST _devinst;
47 
48  public:
49  cm_node() : _devinst( 0 ) {}
50  cm_node( DEVINST devinst ) : _devinst( devinst ) {}
51 
52  static cm_node from_instance_id( std::wstring const& instance_id );
53  static cm_node from_device_path( LPCWSTR device_path )
54  {
55  std::wstring instance_id = instance_id_from_device_path( device_path );
56  return from_instance_id( instance_id );
57  }
58  static cm_node root();
59 
60  DEVINST get() const { return _devinst; }
61  bool valid() const { return get() != 0; }
62 
63  operator DEVINST() const { return _devinst; }
64 
65  operator DEVINST* () { return &_devinst; }
66  operator DEVINST const* () const { return &_devinst; }
67 
68  std::string get_id() const;
69  std::string get_uid() const;
70 
71  cm_node get_parent() const;
72  cm_node get_sibling() const;
73  cm_node get_child() const;
74 
75  std::string get_property( DEVPROPKEY const& property ) const;
76 
77  /*
78  Iterate through all nodes (children, grandchildren, etc.) under this one.
79  A parent is visited before any children.
80  An action (function) is run for each, which can return false to stop iteration:
81 
82  cm_node::root().foreach_node(
83  [&]( cm_node device, size_t depth ) -> bool
84  {
85  ...
86  } );
87 
88  Returns whether iteration was stopped. False if the whole tree was finished.
89  */
90  template< class F >
91  bool foreach_node( F fn, size_t depth = 1 )
92  {
93  auto node = get_child();
94  while( node.valid() )
95  {
96  if( ! fn( node, depth ))
97  return true;
98  if( node.foreach_node( fn, depth + 1 ))
99  return true;
100  node = node.get_sibling();
101  }
102  return false;
103  }
104  };
105 
107  {
108  public:
109  virtual ~event_base();
110  virtual bool set();
111  virtual bool wait(DWORD timeout) const;
112 
113  static event_base* wait(const std::vector<event_base*>& events, bool waitAll, int timeout);
114  static event_base* wait_any(const std::vector<event_base*>& events, int timeout);
115  static event_base* wait_all(const std::vector<event_base*>& events, int timeout);
116 
117  HANDLE get_handle() const { return _handle; }
118 
119  protected:
120  explicit event_base(HANDLE handle);
121 
122  HANDLE _handle;
123 
124  private:
125  event_base() = delete;
126 
127  // Disallow copy:
128  event_base(const event_base&) = delete;
129  event_base& operator=(const event_base&) = delete;
130  };
131 
133  {
134  public:
136  };
137 
139  {
140  public:
142 
143  bool reset() const;
144  };
145 
147  {
151  };
152 
153  class named_mutex
154  {
155  public:
156  named_mutex(const char* id, unsigned timeout);
157  ~named_mutex();
158  bool try_lock() const;
159  void lock() const { acquire(); }
160  void unlock() const { release(); }
161 
162  private:
163  create_and_open_status create_named_mutex(const char* camID);
164  create_and_open_status open_named_mutex(const char* camID);
165  void update_id(const char* id);
166  void acquire() const;
167  void release() const;
168  void close();
169 
170  unsigned _timeout;
172  };
173 
174  class winapi_error : public std::runtime_error
175  {
176  public:
177  explicit winapi_error(const char* message);
178 
179  static std::string last_error_string(DWORD lastError);
180  static std::string generate_message(const std::string& message);
181  };
182  }
183 }
GLenum GLuint GLenum GLsizei const GLchar * message
GLdouble s
GLboolean reset
GLuint64 GLenum void * handle
Definition: glext.h:7785
GLint location
GLsizei const GLchar *const * path
Definition: glext.h:4276
std::string win_to_utf(const WCHAR *s)
Definition: win-helpers.cpp:94
GLint GLint GLsizei GLsizei GLsizei depth
unsigned short uint16_t
Definition: stdint.h:79
GLsizei const GLchar *const * string
static cm_node from_instance_id(std::wstring const &instance_id)
bool foreach_node(F fn, size_t depth=1)
Definition: win-helpers.h:91
static cm_node from_device_path(LPCWSTR device_path)
Definition: win-helpers.h:53
std::string get_uid() const
bool get_usb_device_descriptors(DEVINST devinst, uint16_t device_vid, uint16_t device_pid, const std::string &device_uid, std::string &location, usb_spec &spec, std::string &serial, std::string &parent_uid)
std::vector< std::string > tokenize(std::string string, char separator)
std::string get_id() const
bool parse_usb_path_single_interface(uint16_t &vid, uint16_t &pid, std::string &serial, const std::string &path)
bool parse_usb_path_multiple_interface(uint16_t &vid, uint16_t &pid, uint16_t &mi, std::string &unique_id, const std::string &path, std::string &device_guid)
std::wstring instance_id_from_device_path(LPCWSTR path)
std::string get_property(DEVPROPKEY const &property) const
GLbitfield GLuint64 timeout
bool check(const char *call, HRESULT hr, bool to_throw)
Definition: win-helpers.cpp:79
bool get_usb_descriptors(uint16_t device_vid, uint16_t device_pid, const std::string &device_uid, std::string &location, usb_spec &spec, std::string &serial)


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