diff options
author | Saadi Maalem <saadi.maalem@intel.com> | 2015-08-07 13:18:54 +0800 |
---|---|---|
committer | Zhengyin Qian <qianzy@google.com> | 2015-08-13 19:37:48 -0700 |
commit | 7a627f4f16f24947625bd0ef59e78e32222fdd00 (patch) | |
tree | caf0f5c734ffc1ae82921d7dab2935a377d5b59a | |
parent | 7e89936cef8aea0e53e690dee9d58f9b14dd9f16 (diff) | |
download | sensors-7a627f4f16f24947625bd0ef59e78e32222fdd00.tar.gz |
Sensor: change activate tilt sequences
JIRA: MARVIN-486
We should enable the device first, then enable the interrupt
or state machine; otherwise, the state machine may not work.
Change-Id: I0bf3952231bf7f0d645dd1c1f51dfae6ae5be23d
Signed-off-by: Fei Li <feix.f.li@intel.com>
Reviewed-on: https://android.intel.com/398222
Reviewed-by: jenkins_ndg <jenkins_ndg@intel.com>
Reviewed-by: Maalem, Saadi <saadi.maalem@intel.com>
-rw-r--r-- | tilt/TiltSensor.cpp | 48 | ||||
-rw-r--r-- | tilt/TiltSensor.h | 1 |
2 files changed, 13 insertions, 36 deletions
diff --git a/tilt/TiltSensor.cpp b/tilt/TiltSensor.cpp index 895d5c4..f4967b6 100644 --- a/tilt/TiltSensor.cpp +++ b/tilt/TiltSensor.cpp @@ -51,14 +51,11 @@ int TiltSensor::setDelay(int32_t /* handle */, int64_t ns) int fd; int n, len, ms, ret = 0; char buf[6]; - char *sysfs_path = (char *)malloc(strlen(INPUT_SYSFS_BASE)+strlen(POLL_PERIOD_MS)+2); - - if(sysfs_path == NULL) - return -ENOMEM; + char sysfs_path[SYSFS_MAX_PATH_LEN]; ms = ns / 1000000; - sprintf(sysfs_path, "%s/%s", INPUT_SYSFS_BASE, POLL_PERIOD_MS); + snprintf(sysfs_path, SYSFS_MAX_PATH_LEN, "%s/%s", INPUT_SYSFS_BASE, POLL_PERIOD_MS); fd = open(sysfs_path, O_RDWR); if (fd > 0) { len = 6; @@ -73,70 +70,54 @@ int TiltSensor::setDelay(int32_t /* handle */, int64_t ns) } error_free: - free(sysfs_path); return ret; } int TiltSensor::enable(int32_t /* handle */, int en) { int ret = 0; int flags = en ? 1 : 0; + char sysfs_path[SYSFS_MAX_PATH_LEN]; if (flags != mEnabled) { int fd; char buf[2]; - char *sysfs_path = (char *)malloc(strlen(INPUT_SYSFS_BASE)+strlen(ENABLE_STATE_PROG)+2); - if (sysfs_path == NULL) - return -ENOMEM; - - sprintf(sysfs_path, "%s/%s", INPUT_SYSFS_BASE, ENABLE_STATE_PROG); + snprintf(sysfs_path, SYSFS_MAX_PATH_LEN, "%s/%s", INPUT_SYSFS_BASE, ENABLE_DEVICE); fd = open(sysfs_path, O_RDWR); if (fd > 0) { buf[1] = 0; if (flags) - buf[0] = '2'; /* 3: enable SM1 and SM2; 2: enable SM1; 1: enable SM2; 0: disbale */ + buf[0] = '1'; else - buf[1] = '0'; + buf[0] = '0'; write(fd, buf, sizeof(buf)); close(fd); - free(sysfs_path); } else { - free(sysfs_path); ret = -1; goto out; } - sysfs_path = (char *)malloc(strlen(INPUT_SYSFS_BASE)+strlen(ENABLE_INTERRUPT_OUTPUT)+2); - if (sysfs_path == NULL) - return -ENOMEM; - - sprintf(sysfs_path, "%s/%s", INPUT_SYSFS_BASE, ENABLE_INTERRUPT_OUTPUT); + snprintf(sysfs_path, SYSFS_MAX_PATH_LEN, "%s/%s", INPUT_SYSFS_BASE, ENABLE_INTERRUPT_OUTPUT); fd = open(sysfs_path, O_RDWR); if (fd > 0) { buf[1] = 0; if (flags) buf[0] = '2'; /* 3: enable int1 and int2; 2: enable int1; 1: enable int2; 0: disbale */ else - buf[1] = '0'; + buf[0] = '0'; write(fd, buf, sizeof(buf)); close(fd); - free(sysfs_path); } else { - free(sysfs_path); ret = -1; goto out; } - sysfs_path = (char *)malloc(strlen(INPUT_SYSFS_BASE)+strlen(ENABLE_DEVICE)+2); - if (sysfs_path == NULL) - return -ENOMEM; - - sprintf(sysfs_path, "%s/%s", INPUT_SYSFS_BASE, ENABLE_DEVICE); + snprintf(sysfs_path, SYSFS_MAX_PATH_LEN, "%s/%s", INPUT_SYSFS_BASE, ENABLE_STATE_PROG); fd = open(sysfs_path, O_RDWR); if(fd > 0) { buf[1] = 0; if (flags) - buf[0] = '1'; + buf[0] = '2'; /* 3: enable SM1 and SM2; 2: enable SM1; 1: enable SM2; 0: disbale */ else buf[0] = '0'; write(fd, buf, sizeof(buf)); @@ -145,7 +126,6 @@ int TiltSensor::enable(int32_t /* handle */, int en) } else { ret = -1; } - free(sysfs_path); } out: return ret; @@ -162,14 +142,11 @@ int TiltSensor::batch(int /* handle */, int /* flags */, int64_t period_ns, int6 int fd; int n, len, ms, ret = 0; char buf[6]; - char *sysfs_path = (char *)malloc(strlen(INPUT_SYSFS_BASE)+strlen(POLL_PERIOD_MS)+2); - - if(sysfs_path == NULL) - return -ENOMEM; + char sysfs_path[SYSFS_MAX_PATH_LEN]; ms = period_ns / 1000000; - sprintf(sysfs_path, "%s/%s", INPUT_SYSFS_BASE, POLL_PERIOD_MS); + snprintf(sysfs_path, SYSFS_MAX_PATH_LEN, "%s/%s", INPUT_SYSFS_BASE, POLL_PERIOD_MS); fd = open(sysfs_path, O_RDWR); if (fd > 0) { len = 6; @@ -184,7 +161,6 @@ int TiltSensor::batch(int /* handle */, int /* flags */, int64_t period_ns, int6 } error_free: - free(sysfs_path); return ret; } #endif diff --git a/tilt/TiltSensor.h b/tilt/TiltSensor.h index 693d049..2d0b4e2 100644 --- a/tilt/TiltSensor.h +++ b/tilt/TiltSensor.h @@ -26,6 +26,7 @@ #include "SensorBase.h" #include "InputEventReader.h" +#define SYSFS_MAX_PATH_LEN 64 #define INPUT_SYSFS_BASE "/sys/class/i2c-adapter/i2c-4/4-001e" #define POLL_PERIOD_MS "poll_period_ms" #define RANGE "range" |