summaryrefslogtreecommitdiffstats
path: root/libsensors
diff options
context:
space:
mode:
authorDheeraj CVR <cvr.dheeraj@gmail.com>2015-01-21 19:04:31 +0530
committerDheeraj CVR <cvr.dheeraj@gmail.com>2015-01-22 18:12:53 +0530
commite2e299a94a967a2acd2ff8afe966c290e0a59549 (patch)
treeb18f4c77198faebdc2836fee69836772865472b4 /libsensors
parentaf243d9b52f0c6bb70002b374f86a1d2321948ef (diff)
downloaddevice_samsung_t0lte-e2e299a94a967a2acd2ff8afe966c290e0a59549.tar.gz
device_samsung_t0lte-e2e299a94a967a2acd2ff8afe966c290e0a59549.tar.bz2
device_samsung_t0lte-e2e299a94a967a2acd2ff8afe966c290e0a59549.zip
libsensors: fix sensors enable and disable
* "//Accel sensor is first on and last off" This is totally wrong. The since we are using a single sysfs path to enable and disable all of the sensors, the kernel driver uses bitwise operations on the input value to know which sensors should be enabled. The current logic wrongly assumes that the sensors are enabled and disabled in a specific order and if accelerometer was disabled, all of the other sensors were being disabled too. This caused inconsistent sensor states between the sensor HAL and the kernel driver and most of the time, the sensors failed to work properly and drained battery. Simplify the code to use bitwise operations instead of arithematic operations to keep things clean. Additionally, since two or more sensors can be enabled or disabled at the same time, we use a lock to prevent race conditions. Change-Id: Ifc3fab1456d4061f58d52f2ecda822c6157aae6e
Diffstat (limited to 'libsensors')
-rw-r--r--libsensors/SensorBase.cpp40
1 files changed, 16 insertions, 24 deletions
diff --git a/libsensors/SensorBase.cpp b/libsensors/SensorBase.cpp
index 9c27184..1a25c96 100644
--- a/libsensors/SensorBase.cpp
+++ b/libsensors/SensorBase.cpp
@@ -21,6 +21,7 @@
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
+#include <pthread.h>
#include <cutils/log.h>
@@ -30,6 +31,8 @@
/*****************************************************************************/
+static pthread_mutex_t sspEnableLock = PTHREAD_MUTEX_INITIALIZER;
+
SensorBase::SensorBase(
const char* dev_name,
const char* data_name)
@@ -141,34 +144,23 @@ int SensorBase::flush(int handle)
int SensorBase::sspEnable(const char* sensorname, int sensorvalue, int en)
{
FILE* sspfile;
- int oldvalue = 0;
- int reset = 0;
- int newvalue;
- int fd;
+ int sspValue = 0;
+
+ pthread_mutex_lock(&sspEnableLock);
sspfile = fopen(SSP_DEVICE_ENABLE, "r+");
- fscanf(sspfile, "%d", &oldvalue);
+ fscanf(sspfile, "%d", &sspValue);
fclose(sspfile);
-//Accel sensor is first on and last off, if we are disabling it
-// assume the screen is off, disable all sensors and zero everything out
-// to keep enable file in sync.
- if(sensorvalue == SSP_ACCEL && !en) {
- //ALOGD("SensorBase: Resetting sensors");
- for(int i; i < 6; i++){
- newvalue = oldvalue - ssp_sensors[i];
- //ALOGD("SensorBase: newvalue: %i ",newvalue);
- sspWrite(newvalue);
- }
- sspWrite('\0');
- return 0;
- } else if(en) {
- newvalue = oldvalue + sensorvalue;
- } else {
- newvalue = oldvalue - sensorvalue;
- }
- //ALOGI("%s: name: %s sensor: %i old value: %i new value: %i ", __func__, sensorname, sensorvalue, oldvalue, newvalue);
- sspWrite(newvalue);
+ if (en)
+ sspValue |= sensorvalue;
+ else
+ sspValue &= ~sensorvalue;
+
+ sspWrite(sspValue);
+
+ pthread_mutex_unlock(&sspEnableLock);
+
return 0;
}