new SensorService

remove old sensor service and implement SensorManager
on top of the new (native) SensorManger API.

Change-Id: Iddb77d498755da3e11646473a44d651f12f40281
This commit is contained in:
Mathias Agopian 2010-07-14 23:41:37 -07:00
parent ac07cd613f
commit a7352c9f4a
9 changed files with 97 additions and 17 deletions

View File

@ -36,7 +36,7 @@ class ISensorServer : public IInterface
public: public:
DECLARE_META_INTERFACE(SensorServer); DECLARE_META_INTERFACE(SensorServer);
virtual Vector<Sensor> getSensorList()= 0; virtual Vector<Sensor> getSensorList() = 0;
virtual sp<ISensorEventConnection> createSensorEventConnection() = 0; virtual sp<ISensorEventConnection> createSensorEventConnection() = 0;
}; };

View File

@ -51,7 +51,8 @@ public:
TYPE_PROXIMITY = ASENSOR_TYPE_PROXIMITY TYPE_PROXIMITY = ASENSOR_TYPE_PROXIMITY
}; };
Sensor(); Sensor();
Sensor(struct sensor_t const* hwSensor);
virtual ~Sensor(); virtual ~Sensor();
const String8& getName() const; const String8& getName() const;

View File

@ -42,6 +42,7 @@ namespace android {
class ISensorEventConnection; class ISensorEventConnection;
class Sensor; class Sensor;
class PollLoop;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -56,13 +57,21 @@ public:
ssize_t write(ASensorEvent const* events, size_t numEvents); ssize_t write(ASensorEvent const* events, size_t numEvents);
ssize_t read(ASensorEvent* events, size_t numEvents); ssize_t read(ASensorEvent* events, size_t numEvents);
status_t waitForEvent() const;
status_t wake() const;
status_t enableSensor(Sensor const* sensor) const; status_t enableSensor(Sensor const* sensor) const;
status_t disableSensor(Sensor const* sensor) const; status_t disableSensor(Sensor const* sensor) const;
status_t enableSensor(int32_t handle) const;
status_t disableSensor(int32_t handle) const;
status_t setEventRate(Sensor const* sensor, nsecs_t ns) const; status_t setEventRate(Sensor const* sensor, nsecs_t ns) const;
private: private:
sp<PollLoop> getPollLoop() const;
sp<ISensorEventConnection> mSensorEventConnection; sp<ISensorEventConnection> mSensorEventConnection;
sp<SensorChannel> mSensorChannel; sp<SensorChannel> mSensorChannel;
mutable Mutex mLock;
mutable sp<PollLoop> mPollLoop;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -47,13 +47,13 @@ public:
SensorManager(); SensorManager();
~SensorManager(); ~SensorManager();
ssize_t getSensorList(Sensor**) const; ssize_t getSensorList(Sensor const* const** list) const;
Sensor* getDefaultSensor(int type); Sensor const* getDefaultSensor(int type);
sp<SensorEventQueue> createEventQueue(); sp<SensorEventQueue> createEventQueue();
private: private:
sp<ISensorServer> mSensorServer; sp<ISensorServer> mSensorServer;
Sensor* mSensorList; Sensor const** mSensorList;
Vector<Sensor> mSensors; Vector<Sensor> mSensors;
}; };

View File

@ -47,6 +47,7 @@ public:
virtual sp<SensorChannel> getSensorChannel() const virtual sp<SensorChannel> getSensorChannel() const
{ {
Parcel data, reply; Parcel data, reply;
data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
remote()->transact(GET_SENSOR_CHANNEL, data, &reply); remote()->transact(GET_SENSOR_CHANNEL, data, &reply);
return new SensorChannel(reply); return new SensorChannel(reply);
} }
@ -54,6 +55,7 @@ public:
virtual status_t enableDisable(int handle, bool enabled) virtual status_t enableDisable(int handle, bool enabled)
{ {
Parcel data, reply; Parcel data, reply;
data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
data.writeInt32(handle); data.writeInt32(handle);
data.writeInt32(enabled); data.writeInt32(enabled);
remote()->transact(ENABLE_DISABLE, data, &reply); remote()->transact(ENABLE_DISABLE, data, &reply);
@ -63,6 +65,7 @@ public:
virtual status_t setEventRate(int handle, nsecs_t ns) virtual status_t setEventRate(int handle, nsecs_t ns)
{ {
Parcel data, reply; Parcel data, reply;
data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
data.writeInt32(handle); data.writeInt32(handle);
data.writeInt64(ns); data.writeInt64(ns);
remote()->transact(SET_EVENT_RATE, data, &reply); remote()->transact(SET_EVENT_RATE, data, &reply);

View File

@ -48,6 +48,7 @@ public:
virtual Vector<Sensor> getSensorList() virtual Vector<Sensor> getSensorList()
{ {
Parcel data, reply; Parcel data, reply;
data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
remote()->transact(GET_SENSOR_LIST, data, &reply); remote()->transact(GET_SENSOR_LIST, data, &reply);
Sensor s; Sensor s;
Vector<Sensor> v; Vector<Sensor> v;
@ -63,6 +64,7 @@ public:
virtual sp<ISensorEventConnection> createSensorEventConnection() virtual sp<ISensorEventConnection> createSensorEventConnection()
{ {
Parcel data, reply; Parcel data, reply;
data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply); remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
} }

View File

@ -36,6 +36,18 @@ Sensor::Sensor()
{ {
} }
Sensor::Sensor(struct sensor_t const* hwSensor)
{
mName = hwSensor->name;
mVendor = hwSensor->vendor;
mHandle = hwSensor->handle;
mType = hwSensor->type;
mMinValue = 0; // FIXME: minValue
mMaxValue = hwSensor->maxRange; // FIXME: maxValue
mResolution = hwSensor->resolution;
mPower = hwSensor->power;
}
Sensor::~Sensor() Sensor::~Sensor()
{ {
} }

View File

@ -13,11 +13,15 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "Sensors"
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <utils/Errors.h> #include <utils/Errors.h>
#include <utils/RefBase.h> #include <utils/RefBase.h>
#include <utils/PollLoop.h>
#include <gui/Sensor.h> #include <gui/Sensor.h>
#include <gui/SensorChannel.h> #include <gui/SensorChannel.h>
@ -68,7 +72,7 @@ ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0])); ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0]));
if (size >= 0) { if (size >= 0) {
if (size % sizeof(events[0])) { if (size % sizeof(events[0])) {
// partial write!!! should never happen. // partial read!!! should never happen.
return -EINVAL; return -EINVAL;
} }
// returns number of events read // returns number of events read
@ -77,18 +81,48 @@ ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
return size; return size;
} }
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const sp<PollLoop> SensorEventQueue::getPollLoop() const
{ {
Mutex::Autolock _l(mLock);
if (mPollLoop == 0) {
mPollLoop = new PollLoop(true);
mPollLoop->setCallback(getFd(), POLLIN, NULL, NULL);
}
return mPollLoop;
}
status_t SensorEventQueue::waitForEvent() const
{
const int fd = getFd();
sp<PollLoop> pollLoop(getPollLoop());
int32_t result = pollLoop->pollOnce(-1, NULL, NULL);
return (result == fd) ? NO_ERROR : -1;
}
status_t SensorEventQueue::wake() const
{
sp<PollLoop> pollLoop(getPollLoop());
pollLoop->wake();
return NO_ERROR;
}
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
return mSensorEventConnection->enableDisable(sensor->getHandle(), true); return mSensorEventConnection->enableDisable(sensor->getHandle(), true);
} }
status_t SensorEventQueue::disableSensor(Sensor const* sensor) const status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
{
return mSensorEventConnection->enableDisable(sensor->getHandle(), false); return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
} }
status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const status_t SensorEventQueue::enableSensor(int32_t handle) const {
{ return mSensorEventConnection->enableDisable(handle, true);
}
status_t SensorEventQueue::disableSensor(int32_t handle) const {
return mSensorEventConnection->enableDisable(handle, false);
}
status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
return mSensorEventConnection->setEventRate(sensor->getHandle(), ns); return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
} }

View File

@ -14,6 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
#define LOG_TAG "Sensors"
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
@ -21,6 +23,8 @@
#include <utils/RefBase.h> #include <utils/RefBase.h>
#include <utils/Singleton.h> #include <utils/Singleton.h>
#include <binder/IServiceManager.h>
#include <gui/ISensorServer.h> #include <gui/ISensorServer.h>
#include <gui/ISensorEventConnection.h> #include <gui/ISensorEventConnection.h>
#include <gui/Sensor.h> #include <gui/Sensor.h>
@ -36,25 +40,40 @@ ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
SensorManager::SensorManager() SensorManager::SensorManager()
: mSensorList(0) : mSensorList(0)
{ {
const String16 name("sensorservice");
while (getService(name, &mSensorServer) != NO_ERROR) {
usleep(250000);
}
mSensors = mSensorServer->getSensorList(); mSensors = mSensorServer->getSensorList();
// TODO: needs implementation size_t count = mSensors.size();
mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
for (size_t i=0 ; i<count ; i++) {
mSensorList[i] = mSensors.array() + i;
}
} }
SensorManager::~SensorManager() SensorManager::~SensorManager()
{ {
// TODO: needs implementation free(mSensorList);
} }
ssize_t SensorManager::getSensorList(Sensor** list) const ssize_t SensorManager::getSensorList(Sensor const* const** list) const
{ {
*list = mSensorList; *list = mSensorList;
return mSensors.size(); return mSensors.size();
} }
Sensor* SensorManager::getDefaultSensor(int type) Sensor const* SensorManager::getDefaultSensor(int type)
{ {
// TODO: needs implementation // For now we just return the first sensor of that type we find.
return mSensorList; // in the future it will make sense to let the SensorService make
// that decision.
for (size_t i=0 ; i<mSensors.size() ; i++) {
if (mSensorList[i]->getType() == type)
return mSensorList[i];
}
return NULL;
} }
sp<SensorEventQueue> SensorManager::createEventQueue() sp<SensorEventQueue> SensorManager::createEventQueue()