blob: c2eaf4ec76fab181a4a7fa43ccd373c0f0909e78 [file] [log] [blame]
Mathias Agopian589ce852010-07-13 22:21:56 -07001/*
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 */
Mathias Agopiana7352c92010-07-14 23:41:37 -070016
17#define LOG_TAG "Sensors"
18
Mathias Agopian589ce852010-07-13 22:21:56 -070019#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Errors.h>
23#include <utils/RefBase.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070024#include <utils/Looper.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070025
26#include <gui/Sensor.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070027#include <gui/BitTube.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070028#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)
Mathias Agopian90ed3e82013-09-09 23:36:25 -070038 : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0) {
39 mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
Mathias Agopian589ce852010-07-13 22:21:56 -070040}
41
Mathias Agopian90ed3e82013-09-09 23:36:25 -070042SensorEventQueue::~SensorEventQueue() {
43 delete [] mRecBuffer;
Mathias Agopian589ce852010-07-13 22:21:56 -070044}
45
46void SensorEventQueue::onFirstRef()
47{
48 mSensorChannel = mSensorEventConnection->getSensorChannel();
49}
50
51int SensorEventQueue::getFd() const
52{
53 return mSensorChannel->getFd();
54}
55
Mathias Agopian7b5be952012-04-02 17:02:19 -070056
57ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
58 ASensorEvent const* events, size_t numEvents) {
59 return BitTube::sendObjects(tube, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070060}
61
Mathias Agopian90ed3e82013-09-09 23:36:25 -070062ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
63 if (mAvailable == 0) {
64 ssize_t err = BitTube::recvObjects(mSensorChannel,
65 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
66 if (err < 0) {
67 return err;
68 }
69 mAvailable = err;
70 mConsumed = 0;
71 }
72 size_t count = numEvents < mAvailable ? numEvents : mAvailable;
73 memcpy(events, mRecBuffer + mConsumed, count*sizeof(ASensorEvent));
74 mAvailable -= count;
75 mConsumed += count;
76 return count;
Mathias Agopian589ce852010-07-13 22:21:56 -070077}
78
Jeff Brown59abe7e2010-09-13 23:17:30 -070079sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -070080{
Mathias Agopiana7352c92010-07-14 23:41:37 -070081 Mutex::Autolock _l(mLock);
Jeff Brown59abe7e2010-09-13 23:17:30 -070082 if (mLooper == 0) {
83 mLooper = new Looper(true);
84 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
Mathias Agopiana7352c92010-07-14 23:41:37 -070085 }
Jeff Brown59abe7e2010-09-13 23:17:30 -070086 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -070087}
88
89status_t SensorEventQueue::waitForEvent() const
90{
91 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -070092 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -070093
Mathias Agopian29267fe2012-05-07 15:20:15 -070094 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -070095 int32_t result;
96 do {
Mathias Agopian29267fe2012-05-07 15:20:15 -070097 result = looper->pollOnce(-1, NULL, &events, NULL);
98 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000099 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700100 result = -EPIPE; // unknown error, so we make up one
101 break;
102 }
Mathias Agopian29267fe2012-05-07 15:20:15 -0700103 if (events & ALOOPER_EVENT_HANGUP) {
104 // the other-side has died
105 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
106 result = -EPIPE; // unknown error, so we make up one
107 break;
108 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700109 } while (result != fd);
110
Mathias Agopian2ffb2472010-09-16 21:41:13 -0700111 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700112}
113
114status_t SensorEventQueue::wake() const
115{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700116 sp<Looper> looper(getLooper());
117 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700118 return NO_ERROR;
119}
120
121status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700122 return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
Mathias Agopian589ce852010-07-13 22:21:56 -0700123}
124
Mathias Agopiana7352c92010-07-14 23:41:37 -0700125status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700126 return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
Mathias Agopian589ce852010-07-13 22:21:56 -0700127}
128
Aravind Akella724d91d2013-06-27 12:04:23 -0700129status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
130 int maxBatchReportLatencyUs, int reservedFlags) const {
131 return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
132 us2ns(maxBatchReportLatencyUs), reservedFlags);
133}
134
Aravind Akella701166d2013-10-08 14:59:26 -0700135status_t SensorEventQueue::flush() const {
136 return mSensorEventConnection->flush();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700137}
138
139status_t SensorEventQueue::disableSensor(int32_t handle) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700140 return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700141}
142
143status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700144 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
145}
146
Aravind Akella9a844cf2014-02-11 18:58:52 -0800147void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
148 for (int i = 0; i < count; ++i) {
149 if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
150 mSensorEventConnection->decreaseWakeLockRefCount();
151 }
152 }
153 return;
154}
155
Mathias Agopian589ce852010-07-13 22:21:56 -0700156// ----------------------------------------------------------------------------
157}; // namespace android
158