5#if defined(VRPN_USE_HID)
7#ifdef VRPN_USE_LOCAL_HIDAPI
8#include "./submodules/hidapi/hidapi/hidapi.h"
27 : m_acceptor(acceptor)
33 , m_product_sought(0 )
37 print_error(
"vrpn_HidInterface",
"NULL acceptor",
false);
51 vrpn_uint16 vendor, vrpn_uint16 product,
53 : m_acceptor(acceptor)
58 , m_vendor_sought(vendor)
59 , m_product_sought(product)
63 print_error(
"vrpn_HidInterface",
"NULL acceptor",
false);
78 vrpn_uint16 vendor, vrpn_uint16 product)
79 : m_acceptor(acceptor)
84 , m_vendor_sought(vendor)
85 , m_product_sought(product)
89 print_error(
"vrpn_HidInterface",
"NULL acceptor",
false);
92 if (!device_path || !(device_path[0])) {
100 m_device = hid_open_path(device_path);
101 if (m_device == NULL) {
102 fprintf(stderr,
"vrpn_HidInterface::vrpn_HidInterface(): Could not "
106 fprintf(stderr,
" (Did you remember to run as root or otherwise set "
109 print_hidapi_error(
"vrpn_HidInterface");
125 class EnumerationFreer {
127 typedef struct hid_device_info *EnumerationType;
128 explicit EnumerationFreer(EnumerationType &devs)
134 hid_free_enumeration(devs_);
139 EnumerationType &devs_;
150 struct hid_device_info *devs =
152 struct hid_device_info *loop = devs;
158 EnumerationFreer freeEnumeration(devs);
161 const wchar_t *serial;
163 while ((loop != NULL) && !found) {
165 device_info.
vendor = loop->vendor_id;
166 device_info.
product = loop->product_id;
171 device_info.
path = loop->path;
180 serial = loop->serial_number;
183#ifdef VRPN_HID_DEBUGGING
184 fprintf(stderr,
"vrpn_HidInterface::reconnect(): Found %ls %ls "
185 "(%04hx:%04hx) at path %s - will attempt to "
187 loop->manufacturer_string, loop->product_string,
m_vendor,
200 m_device = hid_open_path(path);
201 if (m_device == NULL) {
203 "vrpn_HidInterface::reconnect(): Could not open device %s\n",
206 fprintf(stderr,
" (Did you remember to run as root or otherwise set "
209 print_hidapi_error(
"reconnect");
213 return finish_setup();
216bool vrpn_HidInterface::finish_setup()
223 if (hid_set_nonblocking(m_device, 1) != 0) {
224 print_error(
"finish_setup",
"Could not set device to nonblocking");
228#ifdef VRPN_HID_DEBUGGING
230 "vrpn_HidInterface::reconnect(): Device successfully opened.\n");
248 vrpn_uint8 inbuf[512];
254 ret = hid_read(m_device, inbuf,
sizeof(inbuf));
256 print_error(
"update",
"Read error");
265 static_cast<vrpn_uint8 *
>(
static_cast<void *
>(inbuf));
278 print_error(
"send_data",
"Interface not currently working",
false);
282 if ((ret = hid_write(m_device,
const_cast<vrpn_uint8 *
>(buffer), bytes)) !=
283 static_cast<int>(bytes)) {
284 print_error(
"send_data",
"hid_write failed");
289 const vrpn_uint8 *buffer)
292 print_error(
"get_feature_report",
"Interface not currently working",
297 int ret = hid_send_feature_report(m_device, buffer, bytes);
299 print_error(
"send_feature_report",
"failed to send feature report");
310 print_error(
"get_feature_report",
"Interface not currently working",
315 int ret = hid_get_feature_report(m_device, buffer, bytes);
317 print_error(
"get_feature_report",
"failed to get feature report");
326void vrpn_HidInterface::print_error(
const char *function,
const char *msg,
327 bool askHIDAPI)
const
329 fprintf(stderr,
"vrpn_HidInterface::%s(): %s\n", function, msg);
330 if (!askHIDAPI || !m_device) {
333 print_hidapi_error(function);
336void vrpn_HidInterface::print_hidapi_error(
const char *function)
const
338 const wchar_t *errmsg = hid_error(m_device);
342 fprintf(stderr,
"vrpn_HidInterface::%s(): error message: %ls\n", function,
virtual bool accept(const vrpn_HIDDEVINFO &device)=0
vrpn_uint16 product() const
Returns USB product ID of connected device May not contain valid if an already-open device was provid...
int get_feature_report(size_t bytes, vrpn_uint8 *buffer)
Call this to get a feature report from the device - first byte must be Report ID (or 0x0 for devices ...
virtual bool reconnect()
Tries to reconnect to an acceptable device. Call this if you suspect a hotplug event has occurred.
vrpn_uint16 m_product_sought
What product we want.
vrpn_HidInterface(vrpn_HidAcceptor *acceptor, vrpn_uint16 vendor=0, vrpn_uint16 product=0, hid_device *device=NULL)
Constructor If we already have a HID device from some other source, it can be passed and we'll take o...
void send_feature_report(size_t bytes, const vrpn_uint8 *buffer)
Call this to send a feature report to the device - first byte must be Report ID (or 0x0 for devices w...
vrpn_HidAcceptor * m_acceptor
This is the HidAcceptor we use when reconnecting.
virtual ~vrpn_HidInterface()
virtual void update()
Polls the device buffers and causes on_data_received callbacks if appropriate You NEED to call this f...
virtual bool connected() const
Returns true iff the last device I/O succeeded.
virtual void on_data_received(size_t bytes, vrpn_uint8 *buffer)=0
Derived class reimplements this callback.
vrpn_uint16 m_vendor_sought
What vendor we want.
vrpn_uint16 vendor() const
Returns USB vendor ID of connected device May not contain valid if an already-open device was provide...
void send_data(size_t bytes, const vrpn_uint8 *buffer)
Call this to send data to the device.
int interface_number() const
Returns the USB interface number of connected device May not contain valid information on all platfor...
wchar_t * manufacturer_string
struct hid_device_ hid_device