SensorEventQueue.cpp revision 56ae42613c91f6a6fb0dc3f626daa24666fd18c2
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#include <sys/socket.h>
22
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/Looper.h>
26
27#include <gui/Sensor.h>
28#include <gui/BitTube.h>
29#include <gui/SensorEventQueue.h>
30#include <gui/ISensorEventConnection.h>
31
32#include <android/sensor.h>
33
34// ----------------------------------------------------------------------------
35namespace android {
36// ----------------------------------------------------------------------------
37
38SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
39    : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0) {
40    mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
41}
42
43SensorEventQueue::~SensorEventQueue() {
44    delete [] mRecBuffer;
45}
46
47void SensorEventQueue::onFirstRef()
48{
49    mSensorChannel = mSensorEventConnection->getSensorChannel();
50}
51
52int SensorEventQueue::getFd() const
53{
54    return mSensorChannel->getFd();
55}
56
57
58ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
59        ASensorEvent const* events, size_t numEvents) {
60    return BitTube::sendObjects(tube, events, numEvents);
61}
62
63ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
64    if (mAvailable == 0) {
65        ssize_t err = BitTube::recvObjects(mSensorChannel,
66                mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
67        if (err < 0) {
68            return err;
69        }
70        mAvailable = err;
71        mConsumed = 0;
72    }
73    size_t count = numEvents < mAvailable ? numEvents : mAvailable;
74    memcpy(events, mRecBuffer + mConsumed, count*sizeof(ASensorEvent));
75    mAvailable -= count;
76    mConsumed += count;
77    return count;
78}
79
80sp<Looper> SensorEventQueue::getLooper() const
81{
82    Mutex::Autolock _l(mLock);
83    if (mLooper == 0) {
84        mLooper = new Looper(true);
85        mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
86    }
87    return mLooper;
88}
89
90status_t SensorEventQueue::waitForEvent() const
91{
92    const int fd = getFd();
93    sp<Looper> looper(getLooper());
94
95    int events;
96    int32_t result;
97    do {
98        result = looper->pollOnce(-1, NULL, &events, NULL);
99        if (result == ALOOPER_POLL_ERROR) {
100            ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
101            result = -EPIPE; // unknown error, so we make up one
102            break;
103        }
104        if (events & ALOOPER_EVENT_HANGUP) {
105            // the other-side has died
106            ALOGE("SensorEventQueue::waitForEvent error HANGUP");
107            result = -EPIPE; // unknown error, so we make up one
108            break;
109        }
110    } while (result != fd);
111
112    return  (result == fd) ? status_t(NO_ERROR) : result;
113}
114
115status_t SensorEventQueue::wake() const
116{
117    sp<Looper> looper(getLooper());
118    looper->wake();
119    return NO_ERROR;
120}
121
122status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
123    return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
124}
125
126status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
127    return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
128}
129
130status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
131                                        int maxBatchReportLatencyUs, int reservedFlags) const {
132    return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
133                                                 us2ns(maxBatchReportLatencyUs), reservedFlags);
134}
135
136status_t SensorEventQueue::flush() const {
137    return mSensorEventConnection->flush();
138}
139
140status_t SensorEventQueue::disableSensor(int32_t handle) const {
141    return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
142}
143
144status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
145    return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
146}
147
148void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
149    for (int i = 0; i < count; ++i) {
150        if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
151            // Send just a byte of data to acknowledge for the wake up sensor events
152            // received
153            char buf = '1';
154            ssize_t size = ::send(mSensorChannel->getFd(), &buf, sizeof(buf),
155                                             MSG_DONTWAIT | MSG_NOSIGNAL);
156            if (size < 0) {
157                ALOGE("sendAck failure %d", size);
158            }
159        }
160    }
161    return;
162}
163
164// ----------------------------------------------------------------------------
165}; // namespace android
166
167