diff --git a/hidapi/hidapi.h b/hidapi/hidapi.h index 4102e6c17..03c292bf8 100644 --- a/hidapi/hidapi.h +++ b/hidapi/hidapi.h @@ -417,6 +417,16 @@ extern "C" { */ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen); + /** @brief Get the Path String from a HID device. + + @ingroup API + @param dev A device handle returned from hid_open(). + + @returns + This function returns a pointer to path string or NULL on error. + */ + HID_API_EXPORT_CALL const char *hid_get_path(hid_device *dev); + /** @brief Get a string describing the last error which occurred. Whether a function sets the last error is noted in its diff --git a/hidtest/test.c b/hidtest/test.c index 857300a58..50b08d51c 100644 --- a/hidtest/test.c +++ b/hidtest/test.c @@ -76,6 +76,9 @@ int main(int argc, char* argv[]) return 1; } + // Read the Path to the Device + printf("Path: \"%s\"\n", hid_get_path(handle)); + // Read the Manufacturer String wstr[0] = 0x0000; res = hid_get_manufacturer_string(handle, wstr, MAX_STR); diff --git a/libusb/hid.c b/libusb/hid.c index 8a1ae645d..3f6e5c674 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -178,6 +178,7 @@ struct hid_device_ { #ifdef DETACH_KERNEL_DRIVER int is_driver_detached; #endif + char *path; }; static libusb_context *usb_context = NULL; @@ -194,6 +195,8 @@ static hid_device *new_hid_device(void) pthread_cond_init(&dev->condition, NULL); pthread_barrier_init(&dev->barrier, NULL, 2); + dev->path = NULL; + return dev; } @@ -204,6 +207,9 @@ static void free_hid_device(hid_device *dev) pthread_cond_destroy(&dev->condition); pthread_mutex_destroy(&dev->mutex); + /* Free the path str */ + free(dev->path); + /* Free the device itself */ free(dev); } @@ -910,6 +916,9 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) break; } good_open = 1; + /* Save path */ + dev->path = strdup(path); + #ifdef DETACH_KERNEL_DRIVER /* Detach the kernel driver, but only if the device is managed by the kernel */ @@ -1339,6 +1348,13 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index return -1; } +HID_API_EXPORT_CALL const char * hid_get_path(hid_device *dev) +{ + if (dev) { + return dev->path; + } + return NULL; +} HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) { diff --git a/linux/hid.c b/linux/hid.c index 07ab3e17a..598ca64da 100644 --- a/linux/hid.c +++ b/linux/hid.c @@ -67,6 +67,7 @@ struct hid_device_ { int blocking; int uses_numbered_reports; wchar_t *last_error_str; + char *path; }; /* Global error message that is not specific to a device, e.g. for @@ -80,10 +81,16 @@ static hid_device *new_hid_device(void) dev->blocking = 1; dev->uses_numbered_reports = 0; dev->last_error_str = NULL; + dev->path = NULL; return dev; } +static void free_hid_device(hid_device *dev) +{ + free(dev->path); + free(dev); +} /* The caller must free the returned string with free(). */ static wchar_t *utf8_to_wchar_t(const char *utf8) @@ -656,6 +663,9 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) /* Set device error to none */ register_device_error(dev, NULL); + /* Save path */ + dev->path = strdup(path); + /* Get the report descriptor */ int res, desc_size = 0; struct hidraw_report_descriptor rpt_desc; @@ -808,7 +818,7 @@ void HID_API_EXPORT hid_close(hid_device *dev) /* Free the device error message */ register_device_error(dev, NULL); - free(dev); + free_hid_device(dev); } @@ -832,6 +842,13 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index return -1; } +HID_API_EXPORT_CALL const char * hid_get_path(hid_device *dev) +{ + if (dev) { + return dev->path; + } + return NULL; +} /* Passing in NULL means asking for the last global error message. */ HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) diff --git a/mac/hid.c b/mac/hid.c index 2235342fe..51b46853e 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -121,6 +121,7 @@ struct hid_device_ { pthread_barrier_t barrier; /* Ensures correct startup sequence */ pthread_barrier_t shutdown_barrier; /* Ensures correct shutdown sequence */ int shutdown_thread; + char *path; }; static hid_device *new_hid_device(void) @@ -143,6 +144,8 @@ static hid_device *new_hid_device(void) pthread_barrier_init(&dev->barrier, NULL, 2); pthread_barrier_init(&dev->shutdown_barrier, NULL, 2); + dev->path = NULL; + return dev; } @@ -175,6 +178,9 @@ static void free_hid_device(hid_device *dev) pthread_cond_destroy(&dev->condition); pthread_mutex_destroy(&dev->mutex); + /* Free the path str */ + free(dev->path); + /* Free the structure itself. */ free(dev); } @@ -821,6 +827,9 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) if (ret == kIOReturnSuccess) { char str[32]; + /* Save path */ + dev->path = strdup(path); + /* Create the buffers for receiving data */ dev->max_input_report_len = (CFIndex) get_max_report_length(dev->device_handle); dev->input_report_buf = (uint8_t*) calloc(dev->max_input_report_len, sizeof(uint8_t)); @@ -1156,6 +1165,13 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index return 0; } +HID_API_EXPORT_CALL const char * hid_get_path(hid_device *dev) +{ + if (dev) { + return dev->path; + } + return NULL; +} HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) { diff --git a/windows/hid.c b/windows/hid.c index 799b35550..13afc6764 100755 --- a/windows/hid.c +++ b/windows/hid.c @@ -144,6 +144,7 @@ struct hid_device_ { BOOL read_pending; char *read_buf; OVERLAPPED ol; + char *path; }; static hid_device *new_hid_device() @@ -159,6 +160,7 @@ static hid_device *new_hid_device() dev->read_buf = NULL; memset(&dev->ol, 0, sizeof(dev->ol)); dev->ol.hEvent = CreateEvent(NULL, FALSE, FALSE /*initial state f=nonsignaled*/, NULL); + dev->path = NULL; return dev; } @@ -169,6 +171,7 @@ static void free_hid_device(hid_device *dev) CloseHandle(dev->device_handle); LocalFree(dev->last_error_str); free(dev->read_buf); + free(dev->path); free(dev); } @@ -594,6 +597,9 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) } } + /* Save path */ + dev->path = strdup(path); + /* Set the Input Report buffer size to 64 reports. */ res = HidD_SetNumInputBuffers(dev->device_handle, 64); if (!res) { @@ -934,6 +940,13 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int return 0; } +HID_API_EXPORT_CALL const char *hid_get_path(hid_device *dev) +{ + if (dev) { + return dev->path; + } + return NULL; +} HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev) {