release-request-ee600ee1-fadc-4a92-9feb-e22548c84a75-for-git_oc-mr1-release-4318546 snap-temp-L23800000099760172

Change-Id: Ifc892de27603935b6b42c5a850fe5d9c43d42841
diff --git a/sensorhal/hubconnection.cpp b/sensorhal/hubconnection.cpp
index 0025b40..251dcb9 100644
--- a/sensorhal/hubconnection.cpp
+++ b/sensorhal/hubconnection.cpp
@@ -74,6 +74,8 @@
 
 #define OS_LOG_EVENT            0x474F4C41  // ascii: ALOG
 
+#define MAX_RETRY_CNT           5
+
 #ifdef LID_STATE_REPORTING_ENABLED
 const char LID_STATE_PROPERTY[] = "sensors.contexthub.lid_state";
 const char LID_STATE_UNKNOWN[]  = "unknown";
@@ -528,6 +530,25 @@
     }
 }
 
+ssize_t HubConnection::sendCmd(const void *buf, size_t count)
+{
+    ssize_t ret;
+    int retryCnt = 0;
+
+    do {
+        ret = TEMP_FAILURE_RETRY(::write(mFd, buf, count));
+    } while (ret == 0 && retryCnt++ < MAX_RETRY_CNT);
+
+    if (retryCnt > 0)
+        ALOGW("sendCmd: retry: count=%zu, ret=%zd, retryCnt=%d",
+              count, ret, retryCnt);
+    else if (ret < 0 || static_cast<size_t>(ret) != count)
+        ALOGW("sendCmd: failed: count=%zu, ret=%zd, errno=%d",
+              count, ret, errno);
+
+    return ret;
+}
+
 void HubConnection::setLeftyMode(bool enable) {
     struct MsgCmd *cmd;
     size_t ret;
@@ -544,7 +565,7 @@
         cmd->msg.dataLen = sizeof(bool);
         memcpy((bool *)(cmd+1), &enable, sizeof(bool));
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, cmd, sizeof(*cmd) + sizeof(bool)));
+        ret = sendCmd(cmd, sizeof(*cmd) + sizeof(bool));
         if (ret == sizeof(*cmd) + sizeof(bool))
             ALOGV("setLeftyMode: lefty (gaze) = %s\n",
                   (enable ? "true" : "false"));
@@ -554,7 +575,7 @@
 
         cmd->msg.appId = APP_ID_MAKE(APP_ID_VENDOR_GOOGLE, APP_ID_APP_UNGAZE_DETECT);
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, cmd, sizeof(*cmd) + sizeof(bool)));
+        ret = sendCmd(cmd, sizeof(*cmd) + sizeof(bool));
         if (ret == sizeof(*cmd) + sizeof(bool))
             ALOGV("setLeftyMode: lefty (ungaze) = %s\n",
                   (enable ? "true" : "false"));
@@ -564,7 +585,7 @@
 
         cmd->msg.appId = APP_ID_MAKE(APP_ID_VENDOR_GOOGLE, APP_ID_APP_WRIST_TILT_DETECT);
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, cmd, sizeof(*cmd) + sizeof(bool)));
+        ret = sendCmd(cmd, sizeof(*cmd) + sizeof(bool));
         if (ret == sizeof(*cmd) + sizeof(bool))
             ALOGV("setLeftyMode: lefty (tilt) = %s\n",
                   (enable ? "true" : "false"));
@@ -1049,7 +1070,7 @@
                   cmd.sensorType, i, mSensorState[i].enable, frequency_q10_to_period_ns(mSensorState[i].rate),
                   mSensorState[i].latency);
 
-            int ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+            int ret = sendCmd(&cmd, sizeof(cmd));
             if (ret != sizeof(cmd)) {
                 ALOGW("failed to send config command to restore sensor %d\n", cmd.sensorType);
             }
@@ -1058,7 +1079,7 @@
 
             for (auto iter = mFlushesPending[i].cbegin(); iter != mFlushesPending[i].cend(); ++iter) {
                 for (int j = 0; j < iter->count; j++) {
-                    int ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+                    int ret = sendCmd(&cmd, sizeof(cmd));
                     if (ret != sizeof(cmd)) {
                         ALOGW("failed to send flush command to sensor %d\n", cmd.sensorType);
                     }
@@ -1690,7 +1711,7 @@
 
         initConfigCmd(&cmd, handle);
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+        ret = sendCmd(&cmd, sizeof(cmd));
         if (ret == sizeof(cmd)) {
             updateSampleRate(handle, enable ? CONFIG_CMD_ENABLE : CONFIG_CMD_DISABLE);
             ALOGV("queueActivate: sensor=%d, handle=%d, enable=%d",
@@ -1720,7 +1741,7 @@
 
         initConfigCmd(&cmd, handle);
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+        ret = sendCmd(&cmd, sizeof(cmd));
         if (ret == sizeof(cmd))
             ALOGV("queueSetDelay: sensor=%d, handle=%d, period=%" PRId64,
                     cmd.sensorType, handle, sampling_period_ns);
@@ -1752,7 +1773,7 @@
 
         initConfigCmd(&cmd, handle);
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+        ret = sendCmd(&cmd, sizeof(cmd));
         if (ret == sizeof(cmd)) {
             updateSampleRate(handle, CONFIG_CMD_ENABLE); // batch uses CONFIG_CMD_ENABLE command
             ALOGV("queueBatch: sensor=%d, handle=%d, period=%" PRId64 ", latency=%" PRId64,
@@ -1798,7 +1819,7 @@
         initConfigCmd(&cmd, handle);
         cmd.cmd = CONFIG_CMD_FLUSH;
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+        ret = sendCmd(&cmd, sizeof(cmd));
         if (ret == sizeof(cmd)) {
             ALOGV("queueFlush: sensor=%d, handle=%d",
                     cmd.sensorType, handle);
@@ -1821,7 +1842,7 @@
         memcpy(cmd->data, data, length);
         cmd->cmd = CONFIG_CMD_CFG_DATA;
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, cmd, sizeof(*cmd) + length));
+        ret = sendCmd(cmd, sizeof(*cmd) + length);
         if (ret == sizeof(*cmd) + length)
             ALOGV("queueData: sensor=%d, length=%zu",
                     cmd->sensorType, length);
@@ -1907,7 +1928,7 @@
         cmd->msg.dataLen = sizeof(float);
         memcpy((float *)(cmd+1), &mUsbMagBias, sizeof(float));
 
-        ret = TEMP_FAILURE_RETRY(::write(mFd, cmd, sizeof(*cmd) + sizeof(float)));
+        ret = sendCmd(cmd, sizeof(*cmd) + sizeof(float));
         if (ret == sizeof(*cmd) + sizeof(float))
             ALOGV("queueUsbMagBias: bias=%f\n", mUsbMagBias);
         else
@@ -2139,7 +2160,7 @@
         struct ConfigCmd cmd;
         initConfigCmd(&cmd, sensor_handle);
 
-        int result = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+        int result = sendCmd(&cmd, sizeof(cmd));
         ret = ret && (result == sizeof(cmd));
     }
     return ret ? NO_ERROR : BAD_VALUE;
@@ -2180,7 +2201,7 @@
     struct ConfigCmd cmd;
     initConfigCmd(&cmd, sensor_handle);
 
-    int ret = TEMP_FAILURE_RETRY(::write(mFd, &cmd, sizeof(cmd)));
+    int ret = sendCmd(&cmd, sizeof(cmd));
 
     if (rate_level == SENSOR_DIRECT_RATE_STOP) {
         ret = NO_ERROR;
diff --git a/sensorhal/hubconnection.h b/sensorhal/hubconnection.h
index 7be18ae..4dbd64e 100644
--- a/sensorhal/hubconnection.h
+++ b/sensorhal/hubconnection.h
@@ -284,6 +284,7 @@
             && mSensorState[handle].sensorType;
     }
 
+    ssize_t sendCmd(const void *buf, size_t count);
     void initConfigCmd(struct ConfigCmd *cmd, int handle);
 
     void queueFlushInternal(int handle, bool internal);