Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c9faa10 authored by Ricardo Cerqueira's avatar Ricardo Cerqueira
Browse files

sensors: Add dummy light sensor

Some devices have a sysfs toggle for the lightsensor instead of
passing the LUX values for userspace processing; for those,
use BOARD_SYSFS_LIGHT_SENSOR:=<sysfs path>

This is necessary for the rest of userspace to recognize its existence
and toggle it for auto-brightness. It doesn't generate actual light
values, though; all backlight adjustments are made in-kernel.

Change-Id: I0e546b4740720bd34d1e1d85a96b295fcc697106
parent aab24347
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3186,6 +3186,7 @@ public class PowerManagerService extends IPowerManager.Stub
        boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
            mAutoBrightessEnabled = enabled;
            enableLightSensorLocked(mAutoBrightessEnabled);
            if (isScreenOn()) {
                // force recompute of backlight values
                if (mLightSensorValue >= 0) {
+3 −1
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@ LOCAL_SHARED_LIBRARIES := \
	libui \
	libgui


ifneq ($(BOARD_SYSFS_LIGHT_SENSOR),)
    LOCAL_CFLAGS += -DSYSFS_LIGHT_SENSOR=\"$(BOARD_SYSFS_LIGHT_SENSOR)\"
endif

LOCAL_MODULE:= libsensorservice

+49 −0
Original line number Diff line number Diff line
@@ -31,6 +31,10 @@
#include "SensorDevice.h"
#include "SensorService.h"

#ifdef SYSFS_LIGHT_SENSOR
#include <fcntl.h>
#endif

namespace android {
// ---------------------------------------------------------------------------
class BatteryService : public Singleton<BatteryService> {
@@ -98,6 +102,27 @@ ANDROID_SINGLETON_STATIC_INSTANCE(BatteryService)

ANDROID_SINGLETON_STATIC_INSTANCE(SensorDevice)

#ifdef SYSFS_LIGHT_SENSOR
static ssize_t addDummyLightSensor(sensor_t const **list, ssize_t count) {
    struct sensor_t dummy_light =     {
                  name            : "CyanogenMod dummy light sensor",
                  vendor          : "CyanogenMod",
                  version         : 1,
                  handle          : SENSOR_TYPE_LIGHT,
                  type            : SENSOR_TYPE_LIGHT,
                  maxRange        : 20,
                  resolution      : 0.1,
                  power           : 20,
    };
    void * new_list = malloc((count+1)*sizeof(sensor_t));
    new_list = memcpy(new_list, *list, count*sizeof(sensor_t));
    ((sensor_t *)new_list)[count] = dummy_light;
    *list = (sensor_t const *)new_list;
    count++;
    return count;
}
#endif

SensorDevice::SensorDevice()
    :  mSensorDevice(0),
       mSensorModule(0)
@@ -117,6 +142,9 @@ SensorDevice::SensorDevice()
        if (mSensorDevice) {
            sensor_t const* list;
            ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);
#ifdef SYSFS_LIGHT_SENSOR
            count = addDummyLightSensor(&list, count);
#endif
            mActivationCount.setCapacity(count);
            Info model;
            for (size_t i=0 ; i<size_t(count) ; i++) {
@@ -157,7 +185,11 @@ void SensorDevice::dump(String8& result, char* buffer, size_t SIZE)
ssize_t SensorDevice::getSensorList(sensor_t const** list) {
    if (!mSensorModule) return NO_INIT;
    ssize_t count = mSensorModule->get_sensors_list(mSensorModule, list);
#ifdef SYSFS_LIGHT_SENSOR
    return addDummyLightSensor(list, count);
#else
    return count;
#endif
}

status_t SensorDevice::initCheck() const {
@@ -179,6 +211,23 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled)
    status_t err(NO_ERROR);
    bool actuateHardware = false;

#ifdef SYSFS_LIGHT_SENSOR

    if (handle == SENSOR_TYPE_LIGHT) {
        int nwr, ret, fd;
        char value[2];

        fd = open(SYSFS_LIGHT_SENSOR, O_RDWR);
        if(fd < 0)
            return -ENODEV;

        nwr = sprintf(value, "%s\n", enabled ? "1" : "0");
        write(fd, value, nwr);
        close(fd);
        return 0;
    }
#endif

    Info& info( mActivationCount.editValueFor(handle) );