vrpn  07.33
Virtual Reality Peripheral Network
vrpn_HumanInterface.h
Go to the documentation of this file.
1 // vrpn_HumanInterface.h: Generic USB HID driver I/O interface.
2 // This implementation uses the cross-platform HIDAPI library
3 // from http://www.signal11.us/oss/hidapi/ to handle the cross-
4 // platform HID device interface. The older version had a different
5 // implementation for each platform; it has been discontinued as
6 // of version 7.29.
7 
8 /* For looking at the types of inforamtion that a device can send, we can use
9  the following (from a post at microchip.com/forums)
10 
11  mcuee@Ubuntu804:~$ lsusb
12  Bus 002 Device 010: ID 04f2:0760 Chicony Electronics Co., Ltd
13  Bus 002 Device 007: ID ffff:0005
14  Bus 002 Device 005: ID 046d:c054 Logitech, Inc.
15  Bus 002 Device 004: ID 14c0:0008
16  Bus 002 Device 003: ID 1947:0033
17  Bus 002 Device 002: ID 058f:9360 Alcor Micro Corp. 8-in-1 Media Card Reader
18  Bus 002 Device 001: ID 0000:0000
19  Bus 001 Device 001: ID 0000:0000
20  mcuee@Ubuntu804:~$ sudo libhid-detach-device 04f2:0760
21  Trying to detach HID with IDs 04f2:0760... done.
22  mcuee@Ubuntu804:~$ sudo lsusb -vvv | more
23 
24  Bus 002 Device 010: ID 04f2:0760 Chicony Electronics Co., Ltd
25  Device Descriptor:
26  bLength 18
27  bDescriptorType 1
28 */
29 
30 #ifndef VRPN_HUMANINTERFACE_H
31 #define VRPN_HUMANINTERFACE_H
32 
33 #include <stddef.h> // for size_t
34 #include <wchar.h> // for wcscmp
35 
36 #include "vrpn_Configure.h" // for VRPN_API, VRPN_USE_HID, etc
37 #include "vrpn_Types.h" // for vrpn_uint16, vrpn_uint8
38 
40  vrpn_uint16 vendor; // USB Vendor ID
41  vrpn_uint16 product; // USB Product ID
42  wchar_t *serial_number; // USB device serial number
44  wchar_t *product_string;
46 };
47 
48 // General interface for device enumeration:
49 // vrpn_HidInterface will connect to the first device your class accepts.
50 // The reset() method will be called before starting a new enumueration, in
51 // case you want to connect to the second or third device found that matches
52 // some criterion. There are some example HID acceptors at the end of
53 // this file.
55 public:
56  virtual ~vrpn_HidAcceptor() {}
57  virtual bool accept(const vrpn_HIDDEVINFO &device) = 0;
58  virtual void reset() {}
59 };
60 
61 #if defined(VRPN_USE_HID)
62 
63 // Forward declarations for hid_device
64 struct hid_device_;
65 typedef struct hid_device_ hid_device;
66 
67 // Main VRPN API for HID devices
69 public:
71  virtual ~vrpn_HidInterface();
72 
74  virtual bool connected() const;
75 
79  virtual void update();
80 
83  virtual bool reconnect();
84 
86  vrpn_uint16 vendor() const;
87 
89  vrpn_uint16 product() const;
90 
92  int interface_number() const;
93 
94 protected:
105  virtual void on_data_received(size_t bytes, vrpn_uint8 *buffer) = 0;
106 
108  void send_data(size_t bytes, const vrpn_uint8 *buffer);
109 
112  void send_feature_report(size_t bytes, const vrpn_uint8 *buffer);
113 
117  int get_feature_report(size_t bytes, vrpn_uint8 *buffer);
118 
128 
129  bool _working;
130  vrpn_uint16 _vendor;
131  vrpn_uint16 _product;
133 
134 private:
135  hid_device *_device;
136 };
137 
138 #endif // VRPN_USE_HID
139 
140 // Some sample acceptors
141 
145 public:
146  bool accept(const vrpn_HIDDEVINFO &) { return true; }
147 };
148 
151 public:
152  vrpn_HidProductAcceptor(vrpn_uint16 vendorId, vrpn_uint16 productId)
153  : product(productId)
154  , vendor(vendorId)
155  {
156  }
157  bool accept(const vrpn_HIDDEVINFO &device)
158  {
159  return (device.vendor == vendor) && (device.product == product);
160  }
161 
162 private:
163  vrpn_uint16 product, vendor;
164 };
165 
168 public:
169  vrpn_HidSerialNumberAcceptor(const wchar_t *serial)
170  : devNum(serial)
171  {
172  }
173  bool accept(const vrpn_HIDDEVINFO &device)
174  {
175  return !wcscmp(devNum, device.serial_number);
176  }
177 
178 private:
179  const wchar_t *devNum;
180 };
181 
185 public:
187  : _iface(iface)
188  {
189  }
190  bool accept(const vrpn_HIDDEVINFO &device)
191  {
192  return _iface == device.interface_number;
193  }
194 
195 private:
196  const int _iface;
197 };
198 
206 public:
208  : target(index)
209  , found(0)
210  , delegate(acceptor)
211  {
212  }
213 
214  virtual ~vrpn_HidNthMatchAcceptor() { delete delegate; }
215 
216  bool accept(const vrpn_HIDDEVINFO &device)
217  {
218  return delegate->accept(device) && (found++ == target);
219  }
220  void reset()
221  {
222  found = 0;
223  delegate->reset();
224  }
225 
226 private:
227  size_t target, found;
228  vrpn_HidAcceptor *delegate;
229 };
230 
234 public:
236  : first(p)
237  , second(q)
238  {
239  }
240  bool accept(const vrpn_HIDDEVINFO &device)
241  {
242  bool p = first->accept(device);
243  bool q = second->accept(device);
244  return p && q;
245  }
246  void reset()
247  {
248  first->reset();
249  second->reset();
250  }
251 
252 private:
253  vrpn_HidAcceptor *first, *second;
254 };
255 
259 public:
261  : first(p)
262  , second(q)
263  {
264  }
265  bool accept(const vrpn_HIDDEVINFO &device)
266  {
267  bool p = first->accept(device);
268  bool q = second->accept(device);
269  return p || q;
270  }
271  void reset()
272  {
273  first->reset();
274  second->reset();
275  }
276 
277 private:
278  vrpn_HidAcceptor *first, *second;
279 };
280 
282 
283 #endif // VRPN_HUMANINTERFACE_H
vrpn_HidSerialNumberAcceptor(const wchar_t *serial)
vrpn_HidNthMatchAcceptor(size_t index, vrpn_HidAcceptor *acceptor)
bool accept(const vrpn_HIDDEVINFO &device)
wchar_t * manufacturer_string
bool accept(const vrpn_HIDDEVINFO &device)
Always accepts the first device passed. Pointless by itself except for testing.
vrpn_HidBooleanAndAcceptor(vrpn_HidAcceptor *p, vrpn_HidAcceptor *q)
Accepts any device with a particular serial number.
bool accept(const vrpn_HIDDEVINFO &)
bool accept(const vrpn_HIDDEVINFO &device)
Accepts any device with the given vendor and product IDs.
bool accept(const vrpn_HIDDEVINFO &device)
Accepts any device with a particular interface number. Best in conjunction with vrpn_HidBooleanAndAcc...
#define VRPN_API
Accepts devices meeting at least one of two criteria. NOT SHORT-CIRCUIT. Another demonstration of acc...
Accepts the Nth device matching a given acceptor.
virtual void reset()
vrpn_HidProductAcceptor(vrpn_uint16 vendorId, vrpn_uint16 productId)
vrpn_HidBooleanOrAcceptor(vrpn_HidAcceptor *p, vrpn_HidAcceptor *q)
bool accept(const vrpn_HIDDEVINFO &device)
vrpn_HidAcceptor * _acceptor
This is the HidAcceptor we use when reconnecting.
bool accept(const vrpn_HIDDEVINFO &device)
struct hid_device_ hid_device
Accepts only devices meeting two criteria. NOT SHORT-CIRCUIT. Another demonstration of acceptor compo...