Skip to content

Commit

Permalink
Added --repeat option for --dev arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola02nb committed Oct 20, 2024
1 parent 3a6e0c3 commit 26f3dd0
Showing 1 changed file with 68 additions and 48 deletions.
116 changes: 68 additions & 48 deletions src/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ static void print_help()
"\t--timeout in millisecounds for --receive\n");
printf(" --receive-feature REPORTID\n"
"\tTry to receive a report for REPORTID.\n");
printf(" --repeat SECS\n"
"\tRepeat command every SECS.\n");
printf("\n");


printf(" --dev-help\n"
"\tThis menu\n");
Expand Down Expand Up @@ -163,13 +166,15 @@ int dev_main(int argc, char* argv[])
int send = 0;
int send_feature = 0;

int sleep = -1;
int sleep_time = -1;

int receive = 0;
int receivereport = 0;

int timeout = -1;

int repeat_seconds = 0;

int print_deviceinfo = 0;

#define BUFFERLENGTH 1024
Expand All @@ -191,6 +196,7 @@ int dev_main(int argc, char* argv[])
{ "receive-feature", required_argument, NULL, 'g' },
{ "timeout", required_argument, NULL, 't' },
{ "dev-help", no_argument, NULL, 'h' },
{ "repeat", required_argument, NULL, 0 },
{ 0, 0, 0, 0 }
};

Expand Down Expand Up @@ -274,9 +280,9 @@ int dev_main(int argc, char* argv[])
break;
}
case 'm': { // --sleep
sleep = strtol(optarg, NULL, 10);
sleep_time = strtol(optarg, NULL, 10);

if (sleep < 0) {
if (sleep_time < 0) {
fprintf(stderr, "--sleep must be positive\n");
return 1;
}
Expand Down Expand Up @@ -310,6 +316,17 @@ int dev_main(int argc, char* argv[])
}
break;
}
case 0: {
if (strcmp(opts[option_index].name, "repeat") == 0) { // --repeat SECS
repeat_seconds = strtol(optarg, NULL, 10);

if (repeat_seconds < 1) {
fprintf(stderr, "--repeat SECS cannot be smaller than 1\n");
return 1;
}
}
break;
}
case 'l': { // --list [vendorid:productid]
print_deviceinfo = 1;
break;
Expand Down Expand Up @@ -361,63 +378,66 @@ int dev_main(int argc, char* argv[])
return 1;
}

if (send) {
int ret = hid_write(device_handle, (const unsigned char*)sendbuffer, send);
do{
if (send) {
int ret = hid_write(device_handle, (const unsigned char*)sendbuffer, send);

if (ret < 0) {
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
if (ret < 0) {
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
}
}
}

if (send_feature) {
int ret = hid_send_feature_report(device_handle, (const unsigned char*)sendreportbuffer, send_feature);
if (send_feature) {
int ret = hid_send_feature_report(device_handle, (const unsigned char*)sendreportbuffer, send_feature);

if (ret < 0) {
fprintf(stderr, "Failed to send data. Error: %d: %ls\n", ret, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
}
}

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

if (sleep >= 0) { // also allow 0 as a minimum sleep
usleep(sleep * 1000);
}
if (receive) {
int read = hid_read_timeout(device_handle, receivebuffer, BUFFERLENGTH, timeout);

if (receive) {
int read = hid_read_timeout(device_handle, receivebuffer, BUFFERLENGTH, timeout);

if (read < 0) {
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
} else if (read == 0) {
fprintf(stderr, "No data to read\n");
} else {
for (int i = 0; i < read; i++) {
printf("%#04x ", receivebuffer[i]);
if (read < 0) {
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
} else if (read == 0) {
fprintf(stderr, "No data to read\n");
} else {
for (int i = 0; i < read; i++) {
printf("%#04x ", receivebuffer[i]);
}
printf("\n");
}
printf("\n");
}
}

if (receivereport) {
int read = hid_get_feature_report(device_handle, receivereportbuffer, BUFFERLENGTH);

if (read < 0) {
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
} else if (read == 0) {
fprintf(stderr, "No data to read\n");
} else {
for (int i = 0; i < read; i++) {
printf("%#04x ", receivereportbuffer[i]);
if (receivereport) {
int read = hid_get_feature_report(device_handle, receivereportbuffer, BUFFERLENGTH);

if (read < 0) {
fprintf(stderr, "Failed to read. Error: %d: %ls\n", read, hid_error(device_handle));
terminate_hid(&device_handle, &hid_path);
return 1;
} else if (read == 0) {
fprintf(stderr, "No data to read\n");
} else {
for (int i = 0; i < read; i++) {
printf("%#04x ", receivereportbuffer[i]);
}
printf("\n");
}
printf("\n");
}
}
sleep(repeat_seconds);
}while(repeat_seconds);

terminate_hid(&device_handle, &hid_path);

Expand Down

0 comments on commit 26f3dd0

Please sign in to comment.