Skip to content
Open
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
14 changes: 11 additions & 3 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ struct hid_device_ {

hidapi_error_ctx error;
wchar_t *last_read_error_str;

unsigned int write_timeout_ms;
};

static struct hid_api_version api_version = {
Expand All @@ -165,6 +167,7 @@ static hid_device *new_hid_device(void)
return NULL;

dev->blocking = 1;
dev->write_timeout_ms = 1000;

hidapi_thread_state_init(&dev->thread_state);

Expand Down Expand Up @@ -1577,6 +1580,11 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_libusb_wrap_sys_device(intptr_t sys
return NULL;
}

void HID_API_EXPORT hid_libusb_set_write_timeout(hid_device *dev, unsigned int timeout)
{
dev->write_timeout_ms = timeout;
}


int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
{
Expand Down Expand Up @@ -1610,7 +1618,7 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
dev->output_endpoint,
(unsigned char*)data,
(int)length,
&actual_length, 1000);
&actual_length, dev->write_timeout_ms);

if (res < 0) {
register_libusb_error(&dev->error, res, "hid_write");
Expand Down Expand Up @@ -1795,7 +1803,7 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
(3/*HID feature*/ << 8) | report_number,
dev->interface,
(unsigned char *)data, length,
1000/*timeout millis*/);
dev->write_timeout_ms);

if (res < 0) {
register_libusb_error(&dev->error, res, "hid_send_feature_report");
Expand Down Expand Up @@ -1877,7 +1885,7 @@ int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *
(2/*HID output*/ << 8) | report_number,
dev->interface,
(unsigned char *)data, length,
1000/*timeout millis*/);
dev->write_timeout_ms);

if (res < 0) {
register_libusb_error(&dev->error, res, "hid_send_output_report");
Expand Down
43 changes: 31 additions & 12 deletions libusb/hidapi_libusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,41 @@ extern "C" {
*/
HID_API_EXPORT hid_device * HID_API_CALL hid_libusb_wrap_sys_device(intptr_t sys_dev, int interface_num);

/** @brief Similar to @ref hid_error but gives a libusb error code.
/** @brief Similar to @ref hid_error but gives a libusb error code.

Since version 0.15.0, @ref HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)
Since version 0.15.0, @ref HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)

If the error occurred is not immediately caused by a libusb function call,
the returned value is 1. @ref hid_error would still contain a valid and meaningful error message.
If the error occurred is not immediately caused by a libusb function call,
the returned value is 1. @ref hid_error would still contain a valid and meaningful error message.

@ingroup API
@param dev A device handle returned from hid_open(),
or NULL to get the last non-device-specific error
(e.g. for errors in hid_open() or hid_enumerate()).
@ingroup API
@param dev A device handle returned from hid_open(),
or NULL to get the last non-device-specific error
(e.g. for errors in hid_open() or hid_enumerate()).

@returns
enum libusb_error value representing last error code.
*/
HID_API_EXPORT int HID_API_CALL hid_libusb_error(hid_device *dev);

/**
* @brief Sets the timeout for hid_write operation.
*
* Since version 0.16.0, @ref HID_API_VERSION >= HID_API_MAKE_VERSION(0, 16, 0)
*
* The default timeout is 1sec for libusb backend.
*
* In case if 1sec is not enough, on in case of multi-platform development,
* the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout
* set for hidraw (linux kernel) implementation.
*
* When the timeout is set to 0, the function will not exit,
* until the write operation is performed or an error occurred.
*
* @param timeout New timeout value in milliseconds.
*/
HID_API_EXPORT void HID_API_CALL hid_libusb_set_write_timeout(hid_device *dev, unsigned int timeout);

@returns
enum libusb_error value representing last error code.
*/
HID_API_EXPORT int HID_API_CALL hid_libusb_error(hid_device *dev);

#ifdef __cplusplus
}
Expand Down
Loading