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

Commit a1b7db95 authored by Mathias Agopian's avatar Mathias Agopian
Browse files

Fix a few issues with sensors reference-counting

parent e04a63b3
Loading
Loading
Loading
Loading
+20 −2
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@
#include <hardware/sensors.h>
#include <hardware/sensors.h>


#include "SensorDevice.h"
#include "SensorDevice.h"
#include "SensorService.h"


namespace android {
namespace android {
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
@@ -166,17 +167,32 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled)
    bool actuateHardware = false;
    bool actuateHardware = false;


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


    LOGD_IF(DEBUG_CONNECTIONS,
            "SensorDevice::activate: ident=%p, handle=0x%08x, enabled=%d, count=%d",
            ident, handle, enabled, info.rates.size());

    if (enabled) {
    if (enabled) {
        Mutex::Autolock _l(mLock);
        Mutex::Autolock _l(mLock);
        LOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
                info.rates.indexOfKey(ident));

        if (info.rates.indexOfKey(ident) < 0) {
        if (info.rates.indexOfKey(ident) < 0) {
            info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
            info.rates.add(ident, DEFAULT_EVENTS_PERIOD);
            if (info.rates.size() == 1) {
                actuateHardware = true;
                actuateHardware = true;
            }
        } else {
        } else {
            // sensor was already activated for this ident
            // sensor was already activated for this ident
        }
        }
    } else {
    } else {
        Mutex::Autolock _l(mLock);
        Mutex::Autolock _l(mLock);
        if (info.rates.removeItem(ident) >= 0) {
        LOGD_IF(DEBUG_CONNECTIONS, "... index=%ld",
                info.rates.indexOfKey(ident));

        ssize_t idx = info.rates.removeItem(ident);
        if (idx >= 0) {
            if (info.rates.size() == 0) {
            if (info.rates.size() == 0) {
                actuateHardware = true;
                actuateHardware = true;
            }
            }
@@ -186,6 +202,8 @@ status_t SensorDevice::activate(void* ident, int handle, int enabled)
    }
    }


    if (actuateHardware) {
    if (actuateHardware) {
        LOGD_IF(DEBUG_CONNECTIONS, "\t>>> actuating h/w");

        err = mSensorDevice->activate(mSensorDevice, handle, enabled);
        err = mSensorDevice->activate(mSensorDevice, handle, enabled);
        if (enabled) {
        if (enabled) {
            LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err));
            LOGE_IF(err, "Error activating sensor %d (%s)", handle, strerror(-err));
+10 −0
Original line number Original line Diff line number Diff line
@@ -297,16 +297,25 @@ void SensorService::cleanupConnection(SensorEventConnection* c)
    Mutex::Autolock _l(mLock);
    Mutex::Autolock _l(mLock);
    const wp<SensorEventConnection> connection(c);
    const wp<SensorEventConnection> connection(c);
    size_t size = mActiveSensors.size();
    size_t size = mActiveSensors.size();
    LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
    for (size_t i=0 ; i<size ; ) {
    for (size_t i=0 ; i<size ; ) {
        int handle = mActiveSensors.keyAt(i);
        int handle = mActiveSensors.keyAt(i);
        if (c->hasSensor(handle)) {
        if (c->hasSensor(handle)) {
            LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
            SensorInterface* sensor = mSensorMap.valueFor( handle );
            SensorInterface* sensor = mSensorMap.valueFor( handle );
            LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
            if (sensor) {
            if (sensor) {
                sensor->activate(c, false);
                sensor->activate(c, false);
            }
            }
        }
        }
        SensorRecord* rec = mActiveSensors.valueAt(i);
        SensorRecord* rec = mActiveSensors.valueAt(i);
        LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
        LOGD_IF(DEBUG_CONNECTIONS,
                "removing connection %p for sensor[%d].handle=0x%08x",
                c, i, handle);

        if (rec && rec->removeConnection(connection)) {
        if (rec && rec->removeConnection(connection)) {
            LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
            mActiveSensors.removeItemsAt(i, 1);
            mActiveSensors.removeItemsAt(i, 1);
            mActiveVirtualSensors.removeItem(handle);
            mActiveVirtualSensors.removeItem(handle);
            delete rec;
            delete rec;
@@ -446,6 +455,7 @@ SensorService::SensorEventConnection::SensorEventConnection(


SensorService::SensorEventConnection::~SensorEventConnection()
SensorService::SensorEventConnection::~SensorEventConnection()
{
{
    LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
    mService->cleanupConnection(this);
    mService->cleanupConnection(this);
}
}


+2 −0
Original line number Original line Diff line number Diff line
@@ -38,6 +38,8 @@


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------


#define DEBUG_CONNECTIONS   false

struct sensors_poll_device_t;
struct sensors_poll_device_t;
struct sensors_module_t;
struct sensors_module_t;