Skip to content

Commit c3ae3d0

Browse files
authored
Run hidtest for no more than 10 seconds when no hid_read data (signal11#440)
Avoid infinite reading from the device if the device doesn't send any interrupt reports. Otherwise hidtest has to be stopped by Ctrl+C interruption, which doesn't give a chance to close the device properly, as a result this has negative side-effects when using LIBUSB backed.
1 parent 597160d commit c3ae3d0

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

hidtest/test.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*******************************************************
2-
Windows HID simplification
2+
HIDAPI - Multi-Platform library for
3+
communication with HID devices.
34
45
Alan Ott
56
Signal 11 Software
67
7-
8/22/2009
8+
libusb/hidapi Team
89
9-
Copyright 2009
10+
Copyright 2022.
1011
1112
This contents of this file may be used by anyone
1213
for any reason without any conditions and may be
@@ -179,8 +180,7 @@ int main(int argc, char* argv[])
179180
buf[0] = 0x2;
180181
res = hid_get_feature_report(handle, buf, sizeof(buf));
181182
if (res < 0) {
182-
printf("Unable to get a feature report.\n");
183-
printf("%ls", hid_error(handle));
183+
printf("Unable to get a feature report: %ls\n", hid_error(handle));
184184
}
185185
else {
186186
// Print out the returned buffer.
@@ -197,40 +197,53 @@ int main(int argc, char* argv[])
197197
buf[1] = 0x80;
198198
res = hid_write(handle, buf, 17);
199199
if (res < 0) {
200-
printf("Unable to write()\n");
201-
printf("Error: %ls\n", hid_error(handle));
200+
printf("Unable to write(): %ls\n", hid_error(handle));
202201
}
203202

204203

205204
// Request state (cmd 0x81). The first byte is the report number (0x1).
206205
buf[0] = 0x1;
207206
buf[1] = 0x81;
208207
hid_write(handle, buf, 17);
209-
if (res < 0)
210-
printf("Unable to write() (2)\n");
208+
if (res < 0) {
209+
printf("Unable to write()/2: %ls\n", hid_error(handle));
210+
}
211211

212212
// Read requested state. hid_read() has been set to be
213213
// non-blocking by the call to hid_set_nonblocking() above.
214214
// This loop demonstrates the non-blocking nature of hid_read().
215215
res = 0;
216+
i = 0;
216217
while (res == 0) {
217218
res = hid_read(handle, buf, sizeof(buf));
218-
if (res == 0)
219+
if (res == 0) {
219220
printf("waiting...\n");
220-
if (res < 0)
221-
printf("Unable to read()\n");
222-
#ifdef _WIN32
221+
}
222+
if (res < 0) {
223+
printf("Unable to read(): %ls\n", hid_error(handle));
224+
break;
225+
}
226+
227+
i++;
228+
if (i >= 10) { /* 10 tries by 500 ms - 5 seconds of waiting*/
229+
printf("read() timeout\n");
230+
break;
231+
}
232+
233+
#ifdef _WIN32
223234
Sleep(500);
224-
#else
235+
#else
225236
usleep(500*1000);
226-
#endif
237+
#endif
227238
}
228239

229-
printf("Data read:\n ");
230-
// Print out the returned buffer.
231-
for (i = 0; i < res; i++)
232-
printf("%02x ", (unsigned int) buf[i]);
233-
printf("\n");
240+
if (res > 0) {
241+
printf("Data read:\n ");
242+
// Print out the returned buffer.
243+
for (i = 0; i < res; i++)
244+
printf("%02x ", (unsigned int) buf[i]);
245+
printf("\n");
246+
}
234247

235248
hid_close(handle);
236249

0 commit comments

Comments
 (0)