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-07-15 06:41:37 +00:00
|
|
|
#include <utils/PollLoop.h>
|
2010-07-14 05:21:56 +00:00
|
|
|
|
|
|
|
#include <gui/Sensor.h>
|
|
|
|
#include <gui/SensorChannel.h>
|
|
|
|
#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();
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t SensorEventQueue::write(ASensorEvent const* events, size_t numEvents)
|
|
|
|
{
|
|
|
|
ssize_t size = mSensorChannel->write(events, numEvents * sizeof(events[0]));
|
|
|
|
if (size >= 0) {
|
|
|
|
if (size % sizeof(events[0])) {
|
|
|
|
// partial write!!! should never happen.
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
// returns number of events written
|
|
|
|
size /= sizeof(events[0]);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
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])) {
|
2010-07-15 06:41:37 +00:00
|
|
|
// partial read!!! should never happen.
|
2010-07-14 05:21:56 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
// returns number of events read
|
|
|
|
size /= sizeof(events[0]);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
sp<PollLoop> SensorEventQueue::getPollLoop() const
|
2010-07-14 05:21:56 +00:00
|
|
|
{
|
2010-07-15 06:41:37 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
if (mPollLoop == 0) {
|
|
|
|
mPollLoop = new PollLoop(true);
|
|
|
|
mPollLoop->setCallback(getFd(), POLLIN, NULL, NULL);
|
|
|
|
}
|
|
|
|
return mPollLoop;
|
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();
|
|
|
|
sp<PollLoop> pollLoop(getPollLoop());
|
|
|
|
int32_t result = pollLoop->pollOnce(-1, NULL, NULL);
|
|
|
|
return (result == fd) ? NO_ERROR : -1;
|
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-07-15 06:41:37 +00:00
|
|
|
sp<PollLoop> 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 {
|
|
|
|
return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
|
|
|
|
}
|
|
|
|
|
2010-07-29 23:51:38 +00:00
|
|
|
status_t SensorEventQueue::enableSensor(int32_t handle, int32_t us) const {
|
2010-07-21 22:59:50 +00:00
|
|
|
status_t err = mSensorEventConnection->enableDisable(handle, true);
|
|
|
|
if (err == NO_ERROR) {
|
2010-07-29 23:51:38 +00:00
|
|
|
mSensorEventConnection->setEventRate(handle, us2ns(us));
|
2010-07-21 22:59:50 +00:00
|
|
|
}
|
|
|
|
return err;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorEventQueue::disableSensor(int32_t handle) const {
|
|
|
|
return mSensorEventConnection->enableDisable(handle, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|