Merge "SensorManager reconnects to sensor service when the later dies" into ics-mr0

This commit is contained in:
Mathias Agopian
2011-10-17 15:50:36 -07:00
committed by Android (Google) Code Review
2 changed files with 95 additions and 34 deletions

View File

@@ -20,6 +20,8 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <binder/IBinder.h>
#include <utils/Errors.h> #include <utils/Errors.h>
#include <utils/RefBase.h> #include <utils/RefBase.h>
#include <utils/Singleton.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: public:
SensorManager(); SensorManager();
@@ -52,9 +56,17 @@ public:
sp<SensorEventQueue> createEventQueue(); sp<SensorEventQueue> createEventQueue();
private: private:
sp<ISensorServer> mSensorServer; // DeathRecipient interface
Sensor const** mSensorList; void sensorManagerDied();
Vector<Sensor> mSensors;
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;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -23,6 +23,7 @@
#include <utils/RefBase.h> #include <utils/RefBase.h>
#include <utils/Singleton.h> #include <utils/Singleton.h>
#include <binder/IBinder.h>
#include <binder/IServiceManager.h> #include <binder/IServiceManager.h>
#include <gui/ISensorServer.h> #include <gui/ISensorServer.h>
@@ -40,11 +41,53 @@ ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
SensorManager::SensorManager() SensorManager::SensorManager()
: mSensorList(0) : mSensorList(0)
{ {
const String16 name("sensorservice"); // okay we're not locked here, but it's not needed during construction
while (getService(name, &mSensorServer) != NO_ERROR) { assertStateLocked();
usleep(250000);
} }
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");
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(); mSensors = mSensorServer->getSensorList();
size_t count = mSensors.size(); size_t count = mSensors.size();
mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*)); mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
@@ -53,19 +96,26 @@ SensorManager::SensorManager()
} }
} }
SensorManager::~SensorManager() return NO_ERROR;
{
free(mSensorList);
} }
ssize_t SensorManager::getSensorList(Sensor const* const** list) const 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; *list = mSensorList;
return mSensors.size(); return mSensors.size();
} }
Sensor const* SensorManager::getDefaultSensor(int type) 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. // 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 // in the future it will make sense to let the SensorService make
// that decision. // that decision.
@@ -73,6 +123,7 @@ Sensor const* SensorManager::getDefaultSensor(int type)
if (mSensorList[i]->getType() == type) if (mSensorList[i]->getType() == type)
return mSensorList[i]; return mSensorList[i];
} }
}
return NULL; return NULL;
} }
@@ -80,20 +131,18 @@ sp<SensorEventQueue> SensorManager::createEventQueue()
{ {
sp<SensorEventQueue> queue; sp<SensorEventQueue> queue;
if (mSensorServer == NULL) { Mutex::Autolock _l(mLock);
LOGE("createEventQueue: mSensorSever is NULL"); while (assertStateLocked() == NO_ERROR) {
return queue;
}
sp<ISensorEventConnection> connection = sp<ISensorEventConnection> connection =
mSensorServer->createSensorEventConnection(); mSensorServer->createSensorEventConnection();
if (connection == NULL) { if (connection == NULL) {
LOGE("createEventQueue: connection is NULL"); // SensorService just died.
return queue; LOGE("createEventQueue: connection is NULL. SensorService died.");
continue;
} }
queue = new SensorEventQueue(connection); queue = new SensorEventQueue(connection);
break;
}
return queue; return queue;
} }