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_HidNthMatchAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &device)
Definition: vrpn_HumanInterface.h:216
vrpn_HidInterfaceNumberAcceptor
Accepts any device with a particular interface number. Best in conjunction with vrpn_HidBooleanAndAcc...
Definition: vrpn_HumanInterface.h:184
vrpn_HidSerialNumberAcceptor::vrpn_HidSerialNumberAcceptor
vrpn_HidSerialNumberAcceptor(const wchar_t *serial)
Definition: vrpn_HumanInterface.h:169
vrpn_HIDDEVINFO::product_string
wchar_t * product_string
Definition: vrpn_HumanInterface.h:44
vrpn_HidBooleanOrAcceptor
Accepts devices meeting at least one of two criteria. NOT SHORT-CIRCUIT. Another demonstration of acc...
Definition: vrpn_HumanInterface.h:258
vrpn_HIDDEVINFO
Definition: vrpn_HumanInterface.h:39
vrpn_HidInterface::_interface
int _interface
Definition: vrpn_HumanInterface.h:132
vrpn_HIDDEVINFO::product
vrpn_uint16 product
Definition: vrpn_HumanInterface.h:41
vrpn_HidBooleanOrAcceptor::reset
void reset()
Definition: vrpn_HumanInterface.h:271
vrpn_Types.h
vrpn_HidNthMatchAcceptor::reset
void reset()
Definition: vrpn_HumanInterface.h:220
vrpn_HidInterface
Definition: vrpn_HumanInterface.h:68
vrpn_HidInterface::_vendor
vrpn_uint16 _vendor
Definition: vrpn_HumanInterface.h:130
vrpn_HidProductAcceptor::vrpn_HidProductAcceptor
vrpn_HidProductAcceptor(vrpn_uint16 vendorId, vrpn_uint16 productId)
Definition: vrpn_HumanInterface.h:152
vrpn_HidNthMatchAcceptor
Accepts the Nth device matching a given acceptor.
Definition: vrpn_HumanInterface.h:205
vrpn_HidInterface::_product
vrpn_uint16 _product
Definition: vrpn_HumanInterface.h:131
vrpn_HidAcceptor
Definition: vrpn_HumanInterface.h:54
vrpn_HidInterfaceNumberAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &device)
Definition: vrpn_HumanInterface.h:190
vrpn_HidAlwaysAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &)
Definition: vrpn_HumanInterface.h:146
vrpn_HIDDEVINFO::vendor
vrpn_uint16 vendor
Definition: vrpn_HumanInterface.h:40
vrpn_HidBooleanAndAcceptor::vrpn_HidBooleanAndAcceptor
vrpn_HidBooleanAndAcceptor(vrpn_HidAcceptor *p, vrpn_HidAcceptor *q)
Definition: vrpn_HumanInterface.h:235
hid_device
struct hid_device_ hid_device
Definition: vrpn_HumanInterface.h:65
vrpn_HidInterfaceNumberAcceptor::vrpn_HidInterfaceNumberAcceptor
vrpn_HidInterfaceNumberAcceptor(int iface)
Definition: vrpn_HumanInterface.h:186
vrpn_HidProductAcceptor
Accepts any device with the given vendor and product IDs.
Definition: vrpn_HumanInterface.h:150
vrpn_HidAcceptor::~vrpn_HidAcceptor
virtual ~vrpn_HidAcceptor()
Definition: vrpn_HumanInterface.h:56
vrpn_HidBooleanAndAcceptor::reset
void reset()
Definition: vrpn_HumanInterface.h:246
vrpn_HidSerialNumberAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &device)
Definition: vrpn_HumanInterface.h:173
vrpn_HIDDEVINFO::interface_number
int interface_number
Definition: vrpn_HumanInterface.h:45
vrpn_HidInterface::_working
bool _working
Definition: vrpn_HumanInterface.h:129
vrpn_HidAcceptor::reset
virtual void reset()
Definition: vrpn_HumanInterface.h:58
vrpn_HidSerialNumberAcceptor
Accepts any device with a particular serial number.
Definition: vrpn_HumanInterface.h:167
vrpn_HidNthMatchAcceptor::vrpn_HidNthMatchAcceptor
vrpn_HidNthMatchAcceptor(size_t index, vrpn_HidAcceptor *acceptor)
Definition: vrpn_HumanInterface.h:207
vrpn_HidNthMatchAcceptor::~vrpn_HidNthMatchAcceptor
virtual ~vrpn_HidNthMatchAcceptor()
Definition: vrpn_HumanInterface.h:214
vrpn_HidProductAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &device)
Definition: vrpn_HumanInterface.h:157
vrpn_HidBooleanOrAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &device)
Definition: vrpn_HumanInterface.h:265
vrpn_HidBooleanAndAcceptor::accept
bool accept(const vrpn_HIDDEVINFO &device)
Definition: vrpn_HumanInterface.h:240
vrpn_HIDDEVINFO::serial_number
wchar_t * serial_number
Definition: vrpn_HumanInterface.h:42
vrpn_HidBooleanAndAcceptor
Accepts only devices meeting two criteria. NOT SHORT-CIRCUIT. Another demonstration of acceptor compo...
Definition: vrpn_HumanInterface.h:233
vrpn_Configure.h
vrpn_HidInterface::_acceptor
vrpn_HidAcceptor * _acceptor
This is the HidAcceptor we use when reconnecting.
Definition: vrpn_HumanInterface.h:127
VRPN_API
#define VRPN_API
Definition: vrpn_Configure.h:646
vrpn_HidAlwaysAcceptor
Always accepts the first device passed. Pointless by itself except for testing.
Definition: vrpn_HumanInterface.h:144
vrpn_HidBooleanOrAcceptor::vrpn_HidBooleanOrAcceptor
vrpn_HidBooleanOrAcceptor(vrpn_HidAcceptor *p, vrpn_HidAcceptor *q)
Definition: vrpn_HumanInterface.h:260
vrpn_HIDDEVINFO::manufacturer_string
wchar_t * manufacturer_string
Definition: vrpn_HumanInterface.h:43