SensorEventQueue.cpp revision 724d91d778e71c8186399f4955de14b54812b3ed
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Sensors"
18
19#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Errors.h>
23#include <utils/RefBase.h>
24#include <utils/Looper.h>
25
26#include <gui/Sensor.h>
27#include <gui/BitTube.h>
28#include <gui/SensorEventQueue.h>
29#include <gui/ISensorEventConnection.h>
30
31#include <android/sensor.h>
32
33// ----------------------------------------------------------------------------
34namespace android {
35// ----------------------------------------------------------------------------
36
37SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
38    : mSensorEventConnection(connection)
39{
40}
41
42SensorEventQueue::~SensorEventQueue()
43{
44}
45
46void SensorEventQueue::onFirstRef()
47{
48    mSensorChannel = mSensorEventConnection->getSensorChannel();
49}
50
51int SensorEventQueue::getFd() const
52{
53    return mSensorChannel->getFd();
54}
55
56
57ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
58        ASensorEvent const* events, size_t numEvents) {
59    return BitTube::sendObjects(tube, events, numEvents);
60}
61
62ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
63{
64    return BitTube::recvObjects(mSensorChannel, events, numEvents);
65}
66
67sp<Looper> SensorEventQueue::getLooper() const
68{
69    Mutex::Autolock _l(mLock);
70    if (mLooper == 0) {
71        mLooper = new Looper(true);
72        mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
73    }
74    return mLooper;
75}
76
77status_t SensorEventQueue::waitForEvent() const
78{
79    const int fd = getFd();
80    sp<Looper> looper(getLooper());
81
82    int events;
83    int32_t result;
84    do {
85        result = looper->pollOnce(-1, NULL, &events, NULL);
86        if (result == ALOOPER_POLL_ERROR) {
87            ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
88            result = -EPIPE; // unknown error, so we make up one
89            break;
90        }
91        if (events & ALOOPER_EVENT_HANGUP) {
92            // the other-side has died
93            ALOGE("SensorEventQueue::waitForEvent error HANGUP");
94            result = -EPIPE; // unknown error, so we make up one
95            break;
96        }
97    } while (result != fd);
98
99    return  (result == fd) ? status_t(NO_ERROR) : result;
100}
101
102status_t SensorEventQueue::wake() const
103{
104    sp<Looper> looper(getLooper());
105    looper->wake();
106    return NO_ERROR;
107}
108
109status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
110    return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
111}
112
113status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
114    return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
115}
116
117status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
118                                        int maxBatchReportLatencyUs, int reservedFlags) const {
119    return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
120                                                 us2ns(maxBatchReportLatencyUs), reservedFlags);
121}
122
123status_t SensorEventQueue::flushSensor(int32_t handle) const {
124    return mSensorEventConnection->flushSensor(handle);
125}
126
127status_t SensorEventQueue::disableSensor(int32_t handle) const {
128    return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
129}
130
131status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
132    return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
133}
134
135// ----------------------------------------------------------------------------
136}; // namespace android
137
138