Skip to content

Commit 6f07e76

Browse files
authored
Added --repeat option for --dev arguments (#376)
1 parent c0c9b50 commit 6f07e76

File tree

1 file changed

+67
-48
lines changed

1 file changed

+67
-48
lines changed

src/dev.c

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ static void print_help()
134134
"\t--timeout in millisecounds for --receive\n");
135135
printf(" --receive-feature REPORTID\n"
136136
"\tTry to receive a report for REPORTID.\n");
137+
printf(" --repeat SECS\n"
138+
"\tRepeat command every SECS.\n");
137139
printf("\n");
138140

139141
printf(" --dev-help\n"
@@ -163,13 +165,15 @@ int dev_main(int argc, char* argv[])
163165
int send = 0;
164166
int send_feature = 0;
165167

166-
int sleep = -1;
168+
int sleep_time = -1;
167169

168170
int receive = 0;
169171
int receivereport = 0;
170172

171173
int timeout = -1;
172174

175+
int repeat_seconds = 0;
176+
173177
int print_deviceinfo = 0;
174178

175179
#define BUFFERLENGTH 1024
@@ -191,6 +195,7 @@ int dev_main(int argc, char* argv[])
191195
{ "receive-feature", required_argument, NULL, 'g' },
192196
{ "timeout", required_argument, NULL, 't' },
193197
{ "dev-help", no_argument, NULL, 'h' },
198+
{ "repeat", required_argument, NULL, 0 },
194199
{ 0, 0, 0, 0 }
195200
};
196201

@@ -274,9 +279,9 @@ int dev_main(int argc, char* argv[])
274279
break;
275280
}
276281
case 'm': { // --sleep
277-
sleep = strtol(optarg, NULL, 10);
282+
sleep_time = strtol(optarg, NULL, 10);
278283

279-
if (sleep < 0) {
284+
if (sleep_time < 0) {
280285
fprintf(stderr, "--sleep must be positive\n");
281286
return 1;
282287
}
@@ -310,6 +315,17 @@ int dev_main(int argc, char* argv[])
310315
}
311316
break;
312317
}
318+
case 0: {
319+
if (strcmp(opts[option_index].name, "repeat") == 0) { // --repeat SECS
320+
repeat_seconds = strtol(optarg, NULL, 10);
321+
322+
if (repeat_seconds < 1) {
323+
fprintf(stderr, "--repeat SECS cannot be smaller than 1\n");
324+
return 1;
325+
}
326+
}
327+
break;
328+
}
313329
case 'l': { // --list [vendorid:productid]
314330
print_deviceinfo = 1;
315331
break;
@@ -361,63 +377,66 @@ int dev_main(int argc, char* argv[])
361377
return 1;
362378
}
363379

364-
if (send) {
365-
int ret = hid_write(device_handle, (const unsigned char*)sendbuffer, send);
380+
do {
381+
if (send) {
382+
int ret = hid_write(device_handle, (const unsigned char*)sendbuffer, send);
366383

367-
if (ret < 0) {
368-
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
369-
terminate_hid(&device_handle, &hid_path);
370-
return 1;
384+
if (ret < 0) {
385+
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
386+
terminate_hid(&device_handle, &hid_path);
387+
return 1;
388+
}
371389
}
372-
}
373390

374-
if (send_feature) {
375-
int ret = hid_send_feature_report(device_handle, (const unsigned char*)sendreportbuffer, send_feature);
391+
if (send_feature) {
392+
int ret = hid_send_feature_report(device_handle, (const unsigned char*)sendreportbuffer, send_feature);
393+
394+
if (ret < 0) {
395+
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
396+
terminate_hid(&device_handle, &hid_path);
397+
return 1;
398+
}
399+
}
376400

377-
if (ret < 0) {
378-
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
379-
terminate_hid(&device_handle, &hid_path);
380-
return 1;
401+
if (sleep_time >= 0) { // also allow 0 as a minimum sleep
402+
usleep(sleep_time * 1000);
381403
}
382-
}
383404

384-
if (sleep >= 0) { // also allow 0 as a minimum sleep
385-
usleep(sleep * 1000);
386-
}
405+
if (receive) {
406+
int read = hid_read_timeout(device_handle, receivebuffer, BUFFERLENGTH, timeout);
387407

388-
if (receive) {
389-
int read = hid_read_timeout(device_handle, receivebuffer, BUFFERLENGTH, timeout);
390-
391-
if (read < 0) {
392-
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
393-
terminate_hid(&device_handle, &hid_path);
394-
return 1;
395-
} else if (read == 0) {
396-
fprintf(stderr, "No data to read\n");
397-
} else {
398-
for (int i = 0; i < read; i++) {
399-
printf("%#04x ", receivebuffer[i]);
408+
if (read < 0) {
409+
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
410+
terminate_hid(&device_handle, &hid_path);
411+
return 1;
412+
} else if (read == 0) {
413+
fprintf(stderr, "No data to read\n");
414+
} else {
415+
for (int i = 0; i < read; i++) {
416+
printf("%#04x ", receivebuffer[i]);
417+
}
418+
printf("\n");
400419
}
401-
printf("\n");
402420
}
403-
}
404421

405-
if (receivereport) {
406-
int read = hid_get_feature_report(device_handle, receivereportbuffer, BUFFERLENGTH);
407-
408-
if (read < 0) {
409-
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
410-
terminate_hid(&device_handle, &hid_path);
411-
return 1;
412-
} else if (read == 0) {
413-
fprintf(stderr, "No data to read\n");
414-
} else {
415-
for (int i = 0; i < read; i++) {
416-
printf("%#04x ", receivereportbuffer[i]);
422+
if (receivereport) {
423+
int read = hid_get_feature_report(device_handle, receivereportbuffer, BUFFERLENGTH);
424+
425+
if (read < 0) {
426+
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
427+
terminate_hid(&device_handle, &hid_path);
428+
return 1;
429+
} else if (read == 0) {
430+
fprintf(stderr, "No data to read\n");
431+
} else {
432+
for (int i = 0; i < read; i++) {
433+
printf("%#04x ", receivereportbuffer[i]);
434+
}
435+
printf("\n");
417436
}
418-
printf("\n");
419437
}
420-
}
438+
sleep(repeat_seconds);
439+
} while (repeat_seconds);
421440

422441
terminate_hid(&device_handle, &hid_path);
423442

0 commit comments

Comments
 (0)