Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hid_get_path to API resolves #163 #164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions hidapi/hidapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions hidtest/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ struct hid_device_ {
#ifdef DETACH_KERNEL_DRIVER
int is_driver_detached;
#endif
char *path;
};

static libusb_context *usb_context = NULL;
Expand All @@ -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;
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
{
Expand Down
19 changes: 18 additions & 1 deletion linux/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions mac/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
{
Expand Down
13 changes: 13 additions & 0 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ struct hid_device_ {
BOOL read_pending;
char *read_buf;
OVERLAPPED ol;
char *path;
};

static hid_device *new_hid_device()
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
{
Expand Down