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

Commit 1a2b83a1 authored by Mathias Agopian's avatar Mathias Agopian
Browse files

SensorManager reconnects to sensor service when the later dies

if system process ever restarted, processes using a SensorManager
would loose the ability to use it, resulting to a crash.
we now listen for sensor service death and reconnected if necessary.

Bug: 5445240
Change-Id: Ia47f8b26cdcecb729fa22bf11d55e10fcaef8cfc
parent 1d011ec8
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@
#include <stdint.h>
#include <sys/types.h>

#include <binder/IBinder.h>

#include <utils/Errors.h>
#include <utils/RefBase.h>
#include <utils/Singleton.h>
@@ -41,7 +43,9 @@ class SensorEventQueue;

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

class SensorManager : public ASensorManager, public Singleton<SensorManager>
class SensorManager :
    public ASensorManager,
    public Singleton<SensorManager>
{
public:
    SensorManager();
@@ -52,9 +56,17 @@ public:
    sp<SensorEventQueue> createEventQueue();

private:
    sp<ISensorServer> mSensorServer;
    Sensor const** mSensorList;
    Vector<Sensor> mSensors;
    // DeathRecipient interface
    void sensorManagerDied();

    status_t assertStateLocked() const;

private:
    mutable Mutex mLock;
    mutable sp<ISensorServer> mSensorServer;
    mutable Sensor const** mSensorList;
    mutable Vector<Sensor> mSensors;
    mutable sp<IBinder::DeathRecipient> mDeathObserver;
};

// ----------------------------------------------------------------------------
+79 −30
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <utils/RefBase.h>
#include <utils/Singleton.h>

#include <binder/IBinder.h>
#include <binder/IServiceManager.h>

#include <gui/ISensorServer.h>
@@ -40,10 +41,52 @@ ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
SensorManager::SensorManager()
    : mSensorList(0)
{
    // okay we're not locked here, but it's not needed during construction
    assertStateLocked();
}

SensorManager::~SensorManager()
{
    free(mSensorList);
}

void SensorManager::sensorManagerDied()
{
    Mutex::Autolock _l(mLock);
    mSensorServer.clear();
    free(mSensorList);
    mSensorList = NULL;
    mSensors.clear();
}

status_t SensorManager::assertStateLocked() const {
    if (mSensorServer == NULL) {
        // try for one second
        const String16 name("sensorservice");
    while (getService(name, &mSensorServer) != NO_ERROR) {
        for (int i=0 ; i<4 ; i++) {
            status_t err = getService(name, &mSensorServer);
            if (err == NAME_NOT_FOUND) {
                usleep(250000);
                continue;
            }
            if (err != NO_ERROR) {
                return err;
            }
            break;
        }

        class DeathObserver : public IBinder::DeathRecipient {
            SensorManager& mSensorManger;
            virtual void binderDied(const wp<IBinder>& who) {
                LOGW("sensorservice died [%p]", who.unsafe_get());
                mSensorManger.sensorManagerDied();
            }
        public:
            DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { }
        };

        mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
        mSensorServer->asBinder()->linkToDeath(mDeathObserver);

        mSensors = mSensorServer->getSensorList();
        size_t count = mSensors.size();
@@ -53,19 +96,26 @@ SensorManager::SensorManager()
        }
    }

SensorManager::~SensorManager()
{
    free(mSensorList);
    return NO_ERROR;
}



ssize_t SensorManager::getSensorList(Sensor const* const** list) const
{
    Mutex::Autolock _l(mLock);
    status_t err = assertStateLocked();
    if (err < 0) {
        return ssize_t(err);
    }
    *list = mSensorList;
    return mSensors.size();
}

Sensor const* SensorManager::getDefaultSensor(int type)
{
    Mutex::Autolock _l(mLock);
    if (assertStateLocked() == NO_ERROR) {
        // For now we just return the first sensor of that type we find.
        // in the future it will make sense to let the SensorService make
        // that decision.
@@ -73,6 +123,7 @@ Sensor const* SensorManager::getDefaultSensor(int type)
            if (mSensorList[i]->getType() == type)
                return mSensorList[i];
        }
    }
    return NULL;
}

@@ -80,20 +131,18 @@ sp<SensorEventQueue> SensorManager::createEventQueue()
{
    sp<SensorEventQueue> queue;

    if (mSensorServer == NULL) {
        LOGE("createEventQueue: mSensorSever is NULL");
        return queue;
    }

    Mutex::Autolock _l(mLock);
    while (assertStateLocked() == NO_ERROR) {
        sp<ISensorEventConnection> connection =
                mSensorServer->createSensorEventConnection();
        if (connection == NULL) {
        LOGE("createEventQueue: connection is NULL");
        return queue;
            // SensorService just died.
            LOGE("createEventQueue: connection is NULL. SensorService died.");
            continue;
        }

        queue = new SensorEventQueue(connection);

        break;
    }
    return queue;
}