Skip to content

Commit

Permalink
Parse the interface number out of the path on Windows where possible.
Browse files Browse the repository at this point in the history
Although the Windows API doesn't provide direct access to interface number,
on child devices representing the interfaces of a composite device, it is
parsable from the device path. An excerpt of a path for interface 1 of a
Razer Hydra device is as follows:
\?\hid#vid_1532&pid_0300&mi_01

See table 4 and related text here:
  http://msdn.microsoft.com/en-us/windows/hardware/gg487473

This patch, if a path is available, and further, if &mi_ is found, parses
what it can for a hex value into the interface_number field. If there is
any problem, the invalid value (-1) is used instead.

Patch from Ryan Pavlik <abiryan@ryand.net> with modifications by Alan Ott.
  • Loading branch information
rpavlik authored and signal11 committed Aug 30, 2011
1 parent abd82e6 commit 36c309f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion hidapi/hidapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ extern "C" {
(Windows/Mac only).*/
unsigned short usage;
/** The USB interface which this logical device
represents (Linux/libusb implementation only). */
represents. Valid on the Linux/libusb implementation
in all cases, and valid on the Windows implementation
only if the device contains more than one interface. */
int interface_number;

/** Pointer to the next device */
Expand Down
18 changes: 17 additions & 1 deletion windows/hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,24 @@ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned shor
/* Release Number */
cur_dev->release_number = attrib.VersionNumber;

/* Interface Number (Unsupported on Windows)*/
/* Interface Number. It can sometimes be parsed out of the path
on Windows if a device has multiple interfaces. See
http://msdn.microsoft.com/en-us/windows/hardware/gg487473 or
search for "Hardware IDs for HID Devices" at MSDN. If it's not
in the path, it's set to -1. */
cur_dev->interface_number = -1;
if (cur_dev->path) {
char *interface_component = strstr(cur_dev->path, "&mi_");
if (interface_component) {
char *hex_str = interface_component + 4;
char *endptr = NULL;
cur_dev->interface_number = strtol(hex_str, &endptr, 16);
if (endptr == hex_str) {
/* The parsing failed. Set interface_number to -1. */
cur_dev->interface_number = -1;
}
}
}
}

cont_close:
Expand Down

0 comments on commit 36c309f

Please sign in to comment.