Skip to content

Commit

Permalink
Add hid_get_device_info (libusb#432)
Browse files Browse the repository at this point in the history
- new API function: `struct hid_device_info * hid_get_device_info(hid_device *dev);` to get `hid_device_info` after the device already opened;
- reused existing implementation on Windows and macOS from enumeration routines to have the implementation;
- refactored libusb implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;
- refactored hidraw implementation to have a shared routine for `hid_enumerate` and `hid_get_device_info`;

Resolves: libusb#431
Closes: libusb#164
Closes: libusb#163
  • Loading branch information
Julusian authored Aug 13, 2022
1 parent eaa5c7c commit 5c9f147
Show file tree
Hide file tree
Showing 6 changed files with 690 additions and 427 deletions.
15 changes: 15 additions & 0 deletions hidapi/hidapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ extern "C" {
*/
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen);

/** @brief Get The struct #hid_device_info from a HID device.
@ingroup API
@param dev A device handle returned from hid_open().
@returns
This function returns a pointer to the struct #hid_device_info
for this hid_device, or NULL in the case of failure.
Call hid_error(dev) to get the failure reason.
This struct is valid until the device is closed with hid_close().
@note The returned object is owned by the @p dev, and SHOULD NOT be freed by the user.
*/
HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info(hid_device *dev);

/** @brief Get a string from a HID device, based on its string index.
@ingroup API
Expand Down
40 changes: 27 additions & 13 deletions hidtest/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@
#endif
//

void print_device(struct hid_device_info *cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf(" Release: %hx\n", cur_dev->release_number);
printf(" Interface: %d\n", cur_dev->interface_number);
printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
printf("\n");
}

void print_devices(struct hid_device_info *cur_dev) {
while (cur_dev) {
print_device(cur_dev);
cur_dev = cur_dev->next;
}
}

int main(int argc, char* argv[])
{
(void)argc;
Expand All @@ -63,7 +81,7 @@ int main(int argc, char* argv[])
hid_device *handle;
int i;

struct hid_device_info *devs, *cur_dev;
struct hid_device_info *devs;

printf("hidapi test/example tool. Compiled with hidapi version %s, runtime version %s.\n", HID_API_VERSION_STR, hid_version_str());
if (HID_API_VERSION == HID_API_MAKE_VERSION(hid_version()->major, hid_version()->minor, hid_version()->patch)) {
Expand All @@ -83,18 +101,7 @@ int main(int argc, char* argv[])
#endif

devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf(" Release: %hx\n", cur_dev->release_number);
printf(" Interface: %d\n", cur_dev->interface_number);
printf(" Usage (page): 0x%hx (0x%hx)\n", cur_dev->usage, cur_dev->usage_page);
printf("\n");
cur_dev = cur_dev->next;
}
print_devices(devs);
hid_free_enumeration(devs);

// Set up the command buffer.
Expand Down Expand Up @@ -134,6 +141,13 @@ int main(int argc, char* argv[])
printf("Serial Number String: (%d) %ls", wstr[0], wstr);
printf("\n");

struct hid_device_info* info = hid_get_device_info(handle);
if (info == NULL) {
printf("Unable to get device info\n");
} else {
print_devices(info);
}

// Read Indexed String 1
wstr[0] = 0x0000;
res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
Expand Down
Loading

0 comments on commit 5c9f147

Please sign in to comment.