aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Chiu <dchiu@synaptics.com>2021-12-09 16:58:17 +0800
committerVincent Huang <vincenthsw@gmail.com>2021-12-10 11:17:43 +0800
commitc0a94c67dc4d64cca7193ab0584d8496bc572d8f (patch)
tree26e633da84fa82bad7fd04825c8468fcff3d09af
parent894fb799331cada9b23320ffb7325795e55ad066 (diff)
downloadrmi4utils-c0a94c67dc4d64cca7193ab0584d8496bc572d8f.tar.gz
Add timeout 10 ms for select() and resend scheme in Read() to avoid indefinitely loop because of wrong report ID in return.
-rwxr-xr-xrmi4update/main.cpp3
-rwxr-xr-xrmidevice/hiddevice.cpp20
-rw-r--r--rmidevice/rmidevice.cpp3
3 files changed, 21 insertions, 5 deletions
diff --git a/rmi4update/main.cpp b/rmi4update/main.cpp
index 8dcd6ec..f10c532 100755
--- a/rmi4update/main.cpp
+++ b/rmi4update/main.cpp
@@ -27,6 +27,7 @@
#include <time.h>
#include <string>
#include <sstream>
+#include <time.h>
#include "hiddevice.h"
#include "rmi4update.h"
@@ -149,7 +150,7 @@ int main(int argc, char **argv)
if (printFirmwareProps) {
std::string props;
-
+
if (!deviceName) {
fprintf(stderr, "Specifiy which device to query\n");
return 1;
diff --git a/rmidevice/hiddevice.cpp b/rmidevice/hiddevice.cpp
index 709559d..a41e680 100755
--- a/rmidevice/hiddevice.cpp
+++ b/rmidevice/hiddevice.cpp
@@ -291,7 +291,12 @@ int HIDDevice::Read(unsigned short addr, unsigned char *buf, unsigned short len)
size_t bytesToRequest;
int reportId;
int rc;
+ struct timeval tv;
+ int resendCount = 0;
+ tv.tv_sec = 10 / 1000;
+ tv.tv_usec = (10 % 1000) * 1000;
+
if (!m_deviceOpen)
return -1;
@@ -301,6 +306,11 @@ int HIDDevice::Read(unsigned short addr, unsigned char *buf, unsigned short len)
bytesPerRequest = len;
for (totalBytesRead = 0; totalBytesRead < len; totalBytesRead += bytesReadPerRequest) {
+Resend:
+ if (resendCount == 3) {
+ fprintf(stderr, "resend count exceed, return as failure\n");
+ return -1;
+ }
count = 0;
if ((len - totalBytesRead) < bytesPerRequest)
bytesToRequest = len % bytesPerRequest;
@@ -334,7 +344,8 @@ int HIDDevice::Read(unsigned short addr, unsigned char *buf, unsigned short len)
bytesReadPerRequest = 0;
while (bytesReadPerRequest < bytesToRequest) {
- rc = GetReport(&reportId);
+ // Add timeout 10 ms for select() called in GetReport().
+ rc = GetReport(&reportId, &tv);
if (rc > 0 && reportId == RMI_READ_DATA_REPORT_ID) {
if (static_cast<ssize_t>(m_inputReportSize) <
std::max(HID_RMI4_READ_INPUT_COUNT,
@@ -350,6 +361,11 @@ int HIDDevice::Read(unsigned short addr, unsigned char *buf, unsigned short len)
bytesInDataReport);
bytesReadPerRequest += bytesInDataReport;
m_dataBytesRead = 0;
+ resendCount = 0;
+ } else {
+ fprintf(stderr, "Some error with GetReport : rc(%d), reportID(0x%x)\n", rc, reportId);
+ resendCount += 1;
+ goto Resend;
}
}
addr += bytesPerRequest;
@@ -391,7 +407,7 @@ int HIDDevice::SetMode(int mode)
{
int rc;
char buf[2];
-
+
if (!m_deviceOpen)
return -1;
diff --git a/rmidevice/rmidevice.cpp b/rmidevice/rmidevice.cpp
index 15335f9..029e614 100644
--- a/rmidevice/rmidevice.cpp
+++ b/rmidevice/rmidevice.cpp
@@ -62,7 +62,6 @@ int RMIDevice::SetRMIPage(unsigned char page)
if (m_page == page)
return 0;
-
m_page = page;
rc = Write(RMI_DEVICE_PAGE_SELECT_REGISTER, &page, 1);
if (rc < 0 || rc < 1) {
@@ -327,7 +326,7 @@ int RMIDevice::ScanPDT(int endFunc, int endPage)
}
m_numInterruptRegs = (interruptCount + 7) / 8;
-
+
return 0;
}