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>
|
|
|
|
|
|
|
|
#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)
|
|
|
|
: mSensorEventConnection(connection)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SensorEventQueue::~SensorEventQueue()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
|
|
|
|
{
|
2012-04-03 00:02:19 +00:00
|
|
|
return BitTube::recvObjects(mSensorChannel, events, numEvents);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorEventQueue::flushSensor(int32_t handle) const {
|
|
|
|
return mSensorEventConnection->flushSensor(handle);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
|