Skip to content

Commit c15392a

Browse files
authored
winapi: add hid_winapi_set_write_timeout (#700)
Add API function to control hid_write timeout on WinAPI backend. Resolves: #686
1 parent 5c4acf8 commit c15392a

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

hidtest/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ int main(int argc, char* argv[])
198198
return 1;
199199
}
200200

201+
#if defined(_WIN32) && HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)
202+
hid_winapi_set_write_timeout(handle, 5000);
203+
#endif
204+
201205
// Read the Manufacturer String
202206
wstr[0] = 0x0000;
203207
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);

windows/hid.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ struct hid_device_ {
194194
OVERLAPPED ol;
195195
OVERLAPPED write_ol;
196196
struct hid_device_info* device_info;
197+
DWORD write_timeout_ms;
197198
};
198199

199200
static hid_device *new_hid_device()
@@ -219,6 +220,7 @@ static hid_device *new_hid_device()
219220
memset(&dev->write_ol, 0, sizeof(dev->write_ol));
220221
dev->write_ol.hEvent = CreateEvent(NULL, FALSE, FALSE /*initial state f=nonsignaled*/, NULL);
221222
dev->device_info = NULL;
223+
dev->write_timeout_ms = 1000;
222224

223225
return dev;
224226
}
@@ -1052,6 +1054,11 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
10521054
return dev;
10531055
}
10541056

1057+
void HID_API_EXPORT_CALL hid_winapi_set_write_timeout(hid_device *dev, unsigned long timeout)
1058+
{
1059+
dev->write_timeout_ms = timeout;
1060+
}
1061+
10551062
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length)
10561063
{
10571064
DWORD bytes_written = 0;
@@ -1103,7 +1110,7 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *
11031110
if (overlapped) {
11041111
/* Wait for the transaction to complete. This makes
11051112
hid_write() synchronous. */
1106-
res = WaitForSingleObject(dev->write_ol.hEvent, 1000);
1113+
res = WaitForSingleObject(dev->write_ol.hEvent, dev->write_timeout_ms);
11071114
if (res != WAIT_OBJECT_0) {
11081115
/* There was a Timeout. */
11091116
register_winapi_error(dev, L"hid_write/WaitForSingleObject");

windows/hidapi_winapi.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ extern "C" {
6767
*/
6868
int HID_API_EXPORT_CALL hid_winapi_descriptor_reconstruct_pp_data(void *hidp_preparsed_data, unsigned char *buf, size_t buf_size);
6969

70+
/**
71+
* @brief Sets the timeout for hid_write operation.
72+
*
73+
* Since version 0.15.0, @ref HID_API_VERSION >= HID_API_MAKE_VERSION(0, 15, 0)
74+
*
75+
* The default timeout is 1sec for winapi backend.
76+
*
77+
* In case if 1sec is not enough, on in case of multi-platform development,
78+
* the recommended value is 5sec, e.g. to match (unconfigurable) 5sec timeout
79+
* set for hidraw (linux kernel) implementation.
80+
*
81+
* When the timeout is set to 0, hid_write function becomes non-blocking and would exit immediately.
82+
* When the timeout is set to INFINITE ((DWORD)-1), the function will not exit,
83+
* until the write operation is performed or an error occured.
84+
* See dwMilliseconds parameter documentation of WaitForSingleObject function.
85+
*
86+
* @param timeout New timeout value in milliseconds.
87+
*/
88+
void HID_API_EXPORT_CALL hid_winapi_set_write_timeout(hid_device *dev, unsigned long timeout);
89+
7090
#ifdef __cplusplus
7191
}
7292
#endif

0 commit comments

Comments
 (0)