flounder: sensor_hub: Add retry mechanism for IIO trigger
We encounter libart crash in EStress test.
When libart crashes, system_server will restart,
and SensorHAL constructor will be called, at this moment,
IIO trigger device node is busy.
In order to make SensorHAL more robust, add logs when error and
try to enable IIO current_trigger while we needs IIO channel.
Change-Id: Ic8a4e285cfe11535663e6a8dfcc8daf694aa01c1
diff --git a/sensor_hub/libsensors/CwMcuSensor.cpp b/sensor_hub/libsensors/CwMcuSensor.cpp
index 662f443..143d173 100644
--- a/sensor_hub/libsensors/CwMcuSensor.cpp
+++ b/sensor_hub/libsensors/CwMcuSensor.cpp
@@ -65,6 +65,7 @@
int CwMcuSensor::sysfs_set_input_attr(const char *attr, char *value, size_t len) {
char fname[PATH_MAX];
int fd;
+ int rc;
snprintf(fname, sizeof(fname), "%s/%s", mDevPath, attr);
fname[sizeof(fname) - 1] = '\0';
@@ -75,7 +76,9 @@
return -EACCES;
}
- if (write(fd, value, (size_t)len) < 0) {
+ rc = write(fd, value, (size_t)len);
+ if (rc < 0) {
+ ALOGE("%s: write failed: fd = %d, rc = %d, strerr = %s\n", __func__, fd, rc, strerror(errno));
close(fd);
return -EIO;
}
@@ -170,7 +173,8 @@
, mInputReader(IIO_MAX_BUFF_SIZE)
, mFlushSensorEnabled(-1)
, l_timestamp(0)
- , g_timestamp(0) {
+ , g_timestamp(0)
+ , init_trigger_done(false) {
int rc;
@@ -287,15 +291,19 @@
snprintf(mTriggerName, sizeof(mTriggerName), "%s-dev%d",
device_name, dev_num);
+ ALOGD("CwMcuSensor::CwMcuSensor: mTriggerName = %s\n", mTriggerName);
if (sysfs_set_input_attr_by_int("buffer/length", IIO_MAX_BUFF_SIZE) < 0)
ALOGE("CwMcuSensor::CwMcuSensor: set IIO buffer length failed: %s\n", strerror(errno));
- if (sysfs_set_input_attr("trigger/current_trigger",
- mTriggerName, strlen(mTriggerName)) < 0) {
- ALOGE("CwMcuSensor::CwMcuSensor: set current trigger failed: %s\n", strerror(errno));
+ rc = sysfs_set_input_attr("trigger/current_trigger",
+ mTriggerName, strlen(mTriggerName));
+ if (rc < 0) {
+ ALOGE("CwMcuSensor::CwMcuSensor: set current trigger failed: rc = %d, strerr() = %s\n",
+ rc, strerror(errno));
+ } else {
+ init_trigger_done = true;
}
- ALOGD("CwMcuSensor::CwMcuSensor: mTriggerName = %s\n", mTriggerName);
if (sysfs_set_input_attr_by_int("buffer/enable", 1) < 0) {
ALOGE("CwMcuSensor::CwMcuSensor: set IIO buffer enable failed: %s\n", strerror(errno));
@@ -621,6 +629,17 @@
ALOGI("CwMcuSensor::batch: set IIO buffer length = %d\n", IIO_MAX_BUFF_SIZE);
}
+ if (!init_trigger_done) {
+ err = sysfs_set_input_attr("trigger/current_trigger",
+ mTriggerName, strlen(mTriggerName));
+ if (err < 0) {
+ ALOGE("CwMcuSensor::batch: set current trigger failed: err = %d, strerr() = %s\n",
+ err, strerror(errno));
+ } else {
+ init_trigger_done = true;
+ }
+ }
+
if (sysfs_set_input_attr_by_int("buffer/enable", 1) < 0) {
ALOGE("CwMcuSensor::batch: set IIO buffer enable failed: %s\n", strerror(errno));
} else {
diff --git a/sensor_hub/libsensors/CwMcuSensor.h b/sensor_hub/libsensors/CwMcuSensor.h
index f1e33d5..3e5bf19 100644
--- a/sensor_hub/libsensors/CwMcuSensor.h
+++ b/sensor_hub/libsensors/CwMcuSensor.h
@@ -84,6 +84,8 @@
int64_t l_timestamp;
int64_t g_timestamp;
+ bool init_trigger_done;
+
int sysfs_set_input_attr(const char *attr, char *value, size_t len);
int sysfs_set_input_attr_by_int(const char *attr, int value);
public: