2010-07-14 05:21:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
#define LOG_TAG "Sensors"
|
|
|
|
|
2010-07-14 05:21:56 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
2014-07-10 23:01:10 +00:00
|
|
|
#include <sys/socket.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/RefBase.h>
|
2010-09-14 06:17:30 +00:00
|
|
|
#include <utils/Looper.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
|
|
|
|
#include <gui/Sensor.h>
|
2011-10-21 01:42:02 +00:00
|
|
|
#include <gui/BitTube.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
#include <gui/SensorEventQueue.h>
|
|
|
|
#include <gui/ISensorEventConnection.h>
|
|
|
|
|
|
|
|
#include <android/sensor.h>
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
|
2014-09-29 00:52:41 +00:00
|
|
|
: mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
|
|
|
|
mNumAcksToSend(0) {
|
2013-09-10 06:36:25 +00:00
|
|
|
mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 06:36:25 +00:00
|
|
|
SensorEventQueue::~SensorEventQueue() {
|
|
|
|
delete [] mRecBuffer;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SensorEventQueue::onFirstRef()
|
|
|
|
{
|
|
|
|
mSensorChannel = mSensorEventConnection->getSensorChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SensorEventQueue::getFd() const
|
|
|
|
{
|
|
|
|
return mSensorChannel->getFd();
|
|
|
|
}
|
|
|
|
|
2012-04-03 00:02:19 +00:00
|
|
|
|
|
|
|
ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
|
|
|
|
ASensorEvent const* events, size_t numEvents) {
|
|
|
|
return BitTube::sendObjects(tube, events, numEvents);
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 06:36:25 +00:00
|
|
|
ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
|
|
|
|
if (mAvailable == 0) {
|
|
|
|
ssize_t err = BitTube::recvObjects(mSensorChannel,
|
|
|
|
mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
|
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
mAvailable = err;
|
|
|
|
mConsumed = 0;
|
|
|
|
}
|
|
|
|
size_t count = numEvents < mAvailable ? numEvents : mAvailable;
|
|
|
|
memcpy(events, mRecBuffer + mConsumed, count*sizeof(ASensorEvent));
|
|
|
|
mAvailable -= count;
|
|
|
|
mConsumed += count;
|
|
|
|
return count;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2010-09-14 06:17:30 +00:00
|
|
|
sp<Looper> SensorEventQueue::getLooper() const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2010-07-15 06:41:37 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2010-09-14 06:17:30 +00:00
|
|
|
if (mLooper == 0) {
|
|
|
|
mLooper = new Looper(true);
|
|
|
|
mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
2010-09-14 06:17:30 +00:00
|
|
|
return mLooper;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
status_t SensorEventQueue::waitForEvent() const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2010-07-15 06:41:37 +00:00
|
|
|
const int fd = getFd();
|
2010-09-14 06:17:30 +00:00
|
|
|
sp<Looper> looper(getLooper());
|
2010-09-17 00:04:16 +00:00
|
|
|
|
2012-05-07 22:20:15 +00:00
|
|
|
int events;
|
2010-09-17 00:04:16 +00:00
|
|
|
int32_t result;
|
|
|
|
do {
|
2012-05-07 22:20:15 +00:00
|
|
|
result = looper->pollOnce(-1, NULL, &events, NULL);
|
|
|
|
if (result == ALOOPER_POLL_ERROR) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
|
2010-09-17 00:04:16 +00:00
|
|
|
result = -EPIPE; // unknown error, so we make up one
|
|
|
|
break;
|
|
|
|
}
|
2012-05-07 22:20:15 +00:00
|
|
|
if (events & ALOOPER_EVENT_HANGUP) {
|
|
|
|
// the other-side has died
|
|
|
|
ALOGE("SensorEventQueue::waitForEvent error HANGUP");
|
|
|
|
result = -EPIPE; // unknown error, so we make up one
|
|
|
|
break;
|
|
|
|
}
|
2010-09-17 00:04:16 +00:00
|
|
|
} while (result != fd);
|
|
|
|
|
2010-09-17 04:41:13 +00:00
|
|
|
return (result == fd) ? status_t(NO_ERROR) : result;
|
2010-07-14 05:21:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
status_t SensorEventQueue::wake() const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2010-09-14 06:17:30 +00:00
|
|
|
sp<Looper> looper(getLooper());
|
|
|
|
looper->wake();
|
2010-07-15 06:41:37 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
|
2013-06-27 19:04:23 +00:00
|
|
|
return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
|
2013-06-27 19:04:23 +00:00
|
|
|
return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 19:04:23 +00:00
|
|
|
status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
|
|
|
|
int maxBatchReportLatencyUs, int reservedFlags) const {
|
|
|
|
return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
|
|
|
|
us2ns(maxBatchReportLatencyUs), reservedFlags);
|
|
|
|
}
|
|
|
|
|
2013-10-08 21:59:26 +00:00
|
|
|
status_t SensorEventQueue::flush() const {
|
|
|
|
return mSensorEventConnection->flush();
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorEventQueue::disableSensor(int32_t handle) const {
|
2013-06-27 19:04:23 +00:00
|
|
|
return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
|
2010-07-14 05:21:56 +00:00
|
|
|
return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
|
2014-09-29 00:52:41 +00:00
|
|
|
++mNumAcksToSend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Send mNumAcksToSend to acknowledge for the wake up sensor events received.
|
|
|
|
if (mNumAcksToSend > 0) {
|
|
|
|
ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
|
|
|
|
MSG_DONTWAIT | MSG_NOSIGNAL);
|
|
|
|
if (size < 0) {
|
|
|
|
ALOGE("sendAck failure %d %d", size, mNumAcksToSend);
|
|
|
|
} else {
|
|
|
|
mNumAcksToSend = 0;
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-14 05:21:56 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
|