diff options
author | Andrew Duggan <aduggan@synaptics.com> | 2016-02-23 11:00:00 -0800 |
---|---|---|
committer | Andrew Duggan <aduggan@synaptics.com> | 2016-02-23 11:00:00 -0800 |
commit | 8438f388c6004df0f385a61eab42e69ec7f43ac3 (patch) | |
tree | 0863edb88829f5152f1865edafbf3590215ea004 /rmihidtool | |
parent | 93de3d6514bcec9c5d8ceb4454c429e4ef9da545 (diff) | |
download | rmi4utils-8438f388c6004df0f385a61eab42e69ec7f43ac3.tar.gz |
rmihidtool: Search for a device if non is provided
For simple cases where there is only one Synaptics device this allows
the tool to figure out which device to open without forcing the user
to figure out which device to open ahead of time. If there are more
then one device the user can explicitly specify the device with the -d
option. Which has been repurposed. Printing device into now uses -n.
Diffstat (limited to 'rmihidtool')
-rw-r--r-- | rmihidtool/main.cpp | 54 |
1 files changed, 44 insertions, 10 deletions
diff --git a/rmihidtool/main.cpp b/rmihidtool/main.cpp index 540b5a7..5bd8d45 100644 --- a/rmihidtool/main.cpp +++ b/rmihidtool/main.cpp @@ -19,6 +19,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <dirent.h> #include <errno.h> #include <string.h> #include <unistd.h> @@ -34,7 +35,7 @@ #include "hiddevice.h" -#define RMI4UPDATE_GETOPTS "hp:ir:w:foambdec" +#define RMI4UPDATE_GETOPTS "hp:ir:w:foambd:ecn" enum rmihidtool_cmd { RMIHIDTOOL_CMD_INTERACTIVE, @@ -57,6 +58,7 @@ void print_help(const char *prog_name) { fprintf(stdout, "Usage: %s [OPTIONS] DEVICEFILE\n", prog_name); fprintf(stdout, "\t-h, --help\t\t\t\tPrint this message\n"); + fprintf(stdout, "\t-d, --device\t\t\thidraw device file associated with the device.\n"); fprintf(stdout, "\t-p, --protocol [protocol]\t\tSet which transport prototocl to use.\n"); fprintf(stdout, "\t-i, --interactive\t\t\tRun in interactive mode.\n"); fprintf(stdout, "\t-r, --read [address] [length]\t\tRead registers starting at the address.\n"); @@ -67,7 +69,7 @@ void print_help(const char *prog_name) fprintf(stdout, "\t-a, --attention\t\t\t\tPrint attention reports until control + c\n"); fprintf(stdout, "\t-m, --print-functions\t\t\tPrint RMI4 functions for the device.\n"); fprintf(stdout, "\t-b, --rebind-driver\t\t\tRebind the driver to force an update of device properties.\n"); - fprintf(stdout, "\t-d, --device-info\t\t\tPrint protocol specific information about the device.\n"); + fprintf(stdout, "\t-n, --device-info\t\t\tPrint protocol specific information about the device.\n"); fprintf(stdout, "\t-e, --reset-device\t\t\tReset the device.\n"); } @@ -203,12 +205,14 @@ int main(int argc, char ** argv) struct sigaction sig_cleanup_action; int opt; int index; + char *deviceName = NULL; RMIDevice *device; const char *protocol = "HID"; unsigned char report[256]; char token[256]; static struct option long_options[] = { {"help", 0, NULL, 'h'}, + {"device", 1, NULL, 'd'}, {"protocol", 1, NULL, 'p'}, {"interactive", 0, NULL, 'i'}, {"read", 1, NULL, 'r'}, @@ -219,7 +223,7 @@ int main(int argc, char ** argv) {"attention", 0, NULL, 'a'}, {"print-functions", 0, NULL, 'm'}, {"rebind-driver", 0, NULL, 'b'}, - {"device-info", 0, NULL, 'd'}, + {"device-info", 0, NULL, 'n'}, {"reset-device", 0, NULL, 'e'}, {0, 0, 0, 0}, }; @@ -230,6 +234,10 @@ int main(int argc, char ** argv) char * start; char * end; int i = 0; + struct dirent * devDirEntry; + DIR * devDir; + char deviceFile[PATH_MAX]; + bool found = false; memset(&sig_cleanup_action, 0, sizeof(struct sigaction)); sig_cleanup_action.sa_handler = cleanup; @@ -244,6 +252,9 @@ int main(int argc, char ** argv) case 'p': protocol = optarg; break; + case 'd': + deviceName = optarg; + break; case 'i': cmd = RMIHIDTOOL_CMD_INTERACTIVE; break; @@ -275,7 +286,7 @@ int main(int argc, char ** argv) case 'b': cmd = RMIHIDTOOL_CMD_REBIND_DRIVER; break; - case 'd': + case 'n': cmd = RMIHIDTOOL_CMD_PRINT_DEVICE_INFO; break; case 'e': @@ -296,16 +307,39 @@ int main(int argc, char ** argv) return -1; } - if (optind >= argc) { + if (optind != argc) { print_help(argv[0]); return -1; } - rc = device->Open(argv[optind++]); - if (rc) { - fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno, - strerror(errno)); - return 1; + if (deviceName) { + rc = device->Open(deviceName); + if (rc) { + fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno, + strerror(errno)); + return 1; + } + } else { + devDir = opendir("/dev"); + if (!devDir) + return -1; + + while ((devDirEntry = readdir(devDir)) != NULL) { + if (strstr(devDirEntry->d_name, "hidraw")) { + snprintf(deviceFile, PATH_MAX, "/dev/%s", devDirEntry->d_name); + rc = device->Open(deviceFile); + if (rc != 0) { + continue; + } else { + found = true; + break; + } + } + } + closedir(devDir); + + if (!found) + return -1; } g_device = device; |