diff options
author | Andrew Duggan <aduggan@synaptics.com> | 2016-05-16 13:03:24 -0700 |
---|---|---|
committer | Andrew Duggan <aduggan@synaptics.com> | 2016-05-16 15:10:07 -0700 |
commit | 21d9d3069254d0840c3c12f5f04fb802213e3c58 (patch) | |
tree | b9290847bff15114fa2689af7f7be5ad4b7af545 | |
parent | 5aa97c91b96439cdaff175561403fbc9ff504272 (diff) | |
download | rmi4utils-21d9d3069254d0840c3c12f5f04fb802213e3c58.tar.gz |
Consolidate device discovery into the rmidevice library
-rw-r--r-- | f54test/main.cpp | 52 | ||||
-rw-r--r-- | rmidevice/hiddevice.cpp | 31 | ||||
-rw-r--r-- | rmidevice/hiddevice.h | 2 | ||||
-rw-r--r-- | rmidevice/rmidevice.h | 2 | ||||
-rw-r--r-- | rmihidtool/main.cpp | 24 |
5 files changed, 46 insertions, 65 deletions
diff --git a/f54test/main.cpp b/f54test/main.cpp index d0cf8f7..d7b44ec 100644 --- a/f54test/main.cpp +++ b/f54test/main.cpp @@ -48,10 +48,9 @@ void printHelp(const char *prog_name) fprintf(stdout, "\t-n, --no_reset\tDo not reset after the report.\n"); } -int RunF54Test(const char * deviceFile, f54_report_types reportType, bool continuousMode, bool noReset) +int RunF54Test(RMIDevice & rmidevice, f54_report_types reportType, bool continuousMode, bool noReset) { int rc; - HIDDevice rmidevice; Display * display; if (continuousMode) @@ -65,10 +64,6 @@ int RunF54Test(const char * deviceFile, f54_report_types reportType, bool contin display->Clear(); - rc = rmidevice.Open(deviceFile); - if (rc) - return rc; - F54Test f54Test(rmidevice, *display); rc = f54Test.Prepare(reportType); @@ -85,8 +80,6 @@ int RunF54Test(const char * deviceFile, f54_report_types reportType, bool contin if (!noReset) rmidevice.Reset(); - rmidevice.Close(); - delete display; return rc; @@ -111,11 +104,10 @@ int main(int argc, char **argv) {"no_reset", 0, NULL, 'n'}, {0, 0, 0, 0}, }; - struct dirent * devDirEntry; - DIR * devDir; f54_report_types reportType = F54_16BIT_IMAGE; bool continuousMode = false; bool noReset = false; + HIDDevice device; while ((opt = getopt_long(argc, argv, F54TEST_GETOPTS, long_options, &index)) != -1) { switch (opt) { @@ -148,38 +140,16 @@ int main(int argc, char **argv) } if (deviceName) { - rc = RunF54Test(deviceName, reportType, continuousMode, noReset); - if (rc) - return rc; - - return rc; - } else { - char rawDevice[PATH_MAX]; - char deviceFile[PATH_MAX]; - bool found = false; - - devDir = opendir("/dev"); - if (!devDir) - return -1; - - while ((devDirEntry = readdir(devDir)) != NULL) { - if (strstr(devDirEntry->d_name, "hidraw")) { - strncpy(rawDevice, devDirEntry->d_name, PATH_MAX); - snprintf(deviceFile, PATH_MAX, "/dev/%s", devDirEntry->d_name); - rc = RunF54Test(deviceFile, reportType, continuousMode, noReset); - if (rc != 0) { - continue; - } else { - found = true; - break; - } - } + rc = device.Open(deviceName); + if (rc) { + fprintf(stderr, "%s: failed to initialize rmi device (%d): %s\n", argv[0], errno, + strerror(errno)); + return 1; } - closedir(devDir); - - if (!found) - return rc; + } else { + if (!device.FindDevice()) + return 1; } - return 0; + return RunF54Test(device, reportType, continuousMode, noReset); } diff --git a/rmidevice/hiddevice.cpp b/rmidevice/hiddevice.cpp index 7fc03c5..5bb804d 100644 --- a/rmidevice/hiddevice.cpp +++ b/rmidevice/hiddevice.cpp @@ -762,4 +762,33 @@ bool HIDDevice::WaitForHidRawDevice(int notifyFd, std::string & deviceName, } } } -}
\ No newline at end of file +} + +bool HIDDevice::FindDevice() +{ + DIR * devDir; + struct dirent * devDirEntry; + char deviceFile[PATH_MAX]; + bool found; + int rc; + + 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 = Open(deviceFile); + if (rc != 0) { + continue; + } else { + found = true; + break; + } + } + } + closedir(devDir); + + return found; +} diff --git a/rmidevice/hiddevice.h b/rmidevice/hiddevice.h index af1a385..6673c10 100644 --- a/rmidevice/hiddevice.h +++ b/rmidevice/hiddevice.h @@ -48,6 +48,8 @@ public: virtual void PrintDeviceInfo(); + virtual bool FindDevice(); + private: int m_fd; diff --git a/rmidevice/rmidevice.h b/rmidevice/rmidevice.h index c2b004c..af938ec 100644 --- a/rmidevice/rmidevice.h +++ b/rmidevice/rmidevice.h @@ -70,6 +70,8 @@ public: unsigned int GetNumInterruptRegs() { return m_numInterruptRegs; } + virtual bool FindDevice() = 0; + protected: std::vector<RMIFunction> m_functionList; unsigned char m_manufacturerID; diff --git a/rmihidtool/main.cpp b/rmihidtool/main.cpp index cd887f0..ea05fba 100644 --- a/rmihidtool/main.cpp +++ b/rmihidtool/main.cpp @@ -236,10 +236,6 @@ 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; @@ -322,25 +318,7 @@ int main(int argc, char ** argv) 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) + if (!device->FindDevice()) return -1; } |