diff --git a/include/gui/ISensorServer.h b/include/gui/ISensorServer.h index 3e0507633..9c8afc5a0 100644 --- a/include/gui/ISensorServer.h +++ b/include/gui/ISensorServer.h @@ -36,7 +36,7 @@ class ISensorServer : public IInterface public: DECLARE_META_INTERFACE(SensorServer); - virtual Vector getSensorList()= 0; + virtual Vector getSensorList() = 0; virtual sp createSensorEventConnection() = 0; }; diff --git a/include/gui/Sensor.h b/include/gui/Sensor.h index 86a16f14f..e696d6386 100644 --- a/include/gui/Sensor.h +++ b/include/gui/Sensor.h @@ -51,7 +51,8 @@ public: TYPE_PROXIMITY = ASENSOR_TYPE_PROXIMITY }; - Sensor(); + Sensor(); + Sensor(struct sensor_t const* hwSensor); virtual ~Sensor(); const String8& getName() const; diff --git a/include/gui/SensorEventQueue.h b/include/gui/SensorEventQueue.h index d8d8128e7..bb03c12a7 100644 --- a/include/gui/SensorEventQueue.h +++ b/include/gui/SensorEventQueue.h @@ -42,6 +42,7 @@ namespace android { class ISensorEventConnection; class Sensor; +class PollLoop; // ---------------------------------------------------------------------------- @@ -56,13 +57,21 @@ public: ssize_t write(ASensorEvent const* 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 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; private: + sp getPollLoop() const; sp mSensorEventConnection; sp mSensorChannel; + mutable Mutex mLock; + mutable sp mPollLoop; }; // ---------------------------------------------------------------------------- diff --git a/include/gui/SensorManager.h b/include/gui/SensorManager.h index 0d65334ce..e1b1a7be4 100644 --- a/include/gui/SensorManager.h +++ b/include/gui/SensorManager.h @@ -47,13 +47,13 @@ public: SensorManager(); ~SensorManager(); - ssize_t getSensorList(Sensor**) const; - Sensor* getDefaultSensor(int type); + ssize_t getSensorList(Sensor const* const** list) const; + Sensor const* getDefaultSensor(int type); sp createEventQueue(); private: sp mSensorServer; - Sensor* mSensorList; + Sensor const** mSensorList; Vector mSensors; }; diff --git a/libs/gui/ISensorEventConnection.cpp b/libs/gui/ISensorEventConnection.cpp index 3e9d456fd..a5083fed0 100644 --- a/libs/gui/ISensorEventConnection.cpp +++ b/libs/gui/ISensorEventConnection.cpp @@ -47,6 +47,7 @@ public: virtual sp getSensorChannel() const { Parcel data, reply; + data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); remote()->transact(GET_SENSOR_CHANNEL, data, &reply); return new SensorChannel(reply); } @@ -54,6 +55,7 @@ public: virtual status_t enableDisable(int handle, bool enabled) { Parcel data, reply; + data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); data.writeInt32(handle); data.writeInt32(enabled); remote()->transact(ENABLE_DISABLE, data, &reply); @@ -63,6 +65,7 @@ public: virtual status_t setEventRate(int handle, nsecs_t ns) { Parcel data, reply; + data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor()); data.writeInt32(handle); data.writeInt64(ns); remote()->transact(SET_EVENT_RATE, data, &reply); diff --git a/libs/gui/ISensorServer.cpp b/libs/gui/ISensorServer.cpp index c6177bc71..71110925d 100644 --- a/libs/gui/ISensorServer.cpp +++ b/libs/gui/ISensorServer.cpp @@ -48,6 +48,7 @@ public: virtual Vector getSensorList() { Parcel data, reply; + data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); remote()->transact(GET_SENSOR_LIST, data, &reply); Sensor s; Vector v; @@ -63,6 +64,7 @@ public: virtual sp createSensorEventConnection() { Parcel data, reply; + data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply); return interface_cast(reply.readStrongBinder()); } diff --git a/libs/gui/Sensor.cpp b/libs/gui/Sensor.cpp index 1fdd28582..48e1cb7a9 100644 --- a/libs/gui/Sensor.cpp +++ b/libs/gui/Sensor.cpp @@ -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() { } diff --git a/libs/gui/SensorEventQueue.cpp b/libs/gui/SensorEventQueue.cpp index f922ac4b0..cc98656cf 100644 --- a/libs/gui/SensorEventQueue.cpp +++ b/libs/gui/SensorEventQueue.cpp @@ -13,11 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#define LOG_TAG "Sensors" + #include #include #include #include +#include #include #include @@ -68,7 +72,7 @@ ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0])); if (size >= 0) { if (size % sizeof(events[0])) { - // partial write!!! should never happen. + // partial read!!! should never happen. return -EINVAL; } // returns number of events read @@ -77,18 +81,48 @@ ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) return size; } -status_t SensorEventQueue::enableSensor(Sensor const* sensor) const +sp 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(getPollLoop()); + int32_t result = pollLoop->pollOnce(-1, NULL, NULL); + return (result == fd) ? NO_ERROR : -1; +} + +status_t SensorEventQueue::wake() const +{ + sp pollLoop(getPollLoop()); + pollLoop->wake(); + return NO_ERROR; +} + +status_t SensorEventQueue::enableSensor(Sensor const* sensor) const { 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); } -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); } diff --git a/libs/gui/SensorManager.cpp b/libs/gui/SensorManager.cpp index cd892851f..d719efb5f 100644 --- a/libs/gui/SensorManager.cpp +++ b/libs/gui/SensorManager.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#define LOG_TAG "Sensors" + #include #include @@ -21,6 +23,8 @@ #include #include +#include + #include #include #include @@ -36,25 +40,40 @@ ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager) SensorManager::SensorManager() : mSensorList(0) { + const String16 name("sensorservice"); + while (getService(name, &mSensorServer) != NO_ERROR) { + usleep(250000); + } + mSensors = mSensorServer->getSensorList(); - // TODO: needs implementation + size_t count = mSensors.size(); + mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*)); + for (size_t i=0 ; igetType() == type) + return mSensorList[i]; + } + return NULL; } sp SensorManager::createEventQueue()