blob: 76ae47034c7be4292819929b948753065878d3b7 [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
Dan Stozad723bd72014-11-18 10:24:03 -080019#include <algorithm>
Mathias Agopian589ce852010-07-13 22:21:56 -070020#include <stdint.h>
21#include <sys/types.h>
Aravind Akella56ae4262014-07-10 16:01:10 -070022#include <sys/socket.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070023
24#include <utils/Errors.h>
25#include <utils/RefBase.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070026#include <utils/Looper.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070027
28#include <gui/Sensor.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070029#include <gui/BitTube.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070030#include <gui/SensorEventQueue.h>
31#include <gui/ISensorEventConnection.h>
32
33#include <android/sensor.h>
34
Dan Stozad723bd72014-11-18 10:24:03 -080035using std::min;
36
Mathias Agopian589ce852010-07-13 22:21:56 -070037// ----------------------------------------------------------------------------
38namespace android {
39// ----------------------------------------------------------------------------
40
41SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
Aravind Akella8a969552014-09-28 17:52:41 -070042 : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
43 mNumAcksToSend(0) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -070044 mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
Mathias Agopian589ce852010-07-13 22:21:56 -070045}
46
Mathias Agopian90ed3e82013-09-09 23:36:25 -070047SensorEventQueue::~SensorEventQueue() {
48 delete [] mRecBuffer;
Mathias Agopian589ce852010-07-13 22:21:56 -070049}
50
51void SensorEventQueue::onFirstRef()
52{
53 mSensorChannel = mSensorEventConnection->getSensorChannel();
54}
55
56int SensorEventQueue::getFd() const
57{
58 return mSensorChannel->getFd();
59}
60
Mathias Agopian7b5be952012-04-02 17:02:19 -070061
62ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
63 ASensorEvent const* events, size_t numEvents) {
64 return BitTube::sendObjects(tube, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070065}
66
Mathias Agopian90ed3e82013-09-09 23:36:25 -070067ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
68 if (mAvailable == 0) {
69 ssize_t err = BitTube::recvObjects(mSensorChannel,
70 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
71 if (err < 0) {
72 return err;
73 }
Dan Stozad723bd72014-11-18 10:24:03 -080074 mAvailable = static_cast<size_t>(err);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070075 mConsumed = 0;
76 }
Dan Stozad723bd72014-11-18 10:24:03 -080077 size_t count = min(numEvents, mAvailable);
78 memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent));
Mathias Agopian90ed3e82013-09-09 23:36:25 -070079 mAvailable -= count;
80 mConsumed += count;
Dan Stozad723bd72014-11-18 10:24:03 -080081 return static_cast<ssize_t>(count);
Mathias Agopian589ce852010-07-13 22:21:56 -070082}
83
Jeff Brown59abe7e2010-09-13 23:17:30 -070084sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -070085{
Mathias Agopiana7352c92010-07-14 23:41:37 -070086 Mutex::Autolock _l(mLock);
Jeff Brown59abe7e2010-09-13 23:17:30 -070087 if (mLooper == 0) {
88 mLooper = new Looper(true);
89 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
Mathias Agopiana7352c92010-07-14 23:41:37 -070090 }
Jeff Brown59abe7e2010-09-13 23:17:30 -070091 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -070092}
93
94status_t SensorEventQueue::waitForEvent() const
95{
96 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -070097 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -070098
Mathias Agopian29267fe2012-05-07 15:20:15 -070099 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700100 int32_t result;
101 do {
Mathias Agopian29267fe2012-05-07 15:20:15 -0700102 result = looper->pollOnce(-1, NULL, &events, NULL);
103 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000104 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700105 result = -EPIPE; // unknown error, so we make up one
106 break;
107 }
Mathias Agopian29267fe2012-05-07 15:20:15 -0700108 if (events & ALOOPER_EVENT_HANGUP) {
109 // the other-side has died
110 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
111 result = -EPIPE; // unknown error, so we make up one
112 break;
113 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700114 } while (result != fd);
115
Mathias Agopian2ffb2472010-09-16 21:41:13 -0700116 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700117}
118
119status_t SensorEventQueue::wake() const
120{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700121 sp<Looper> looper(getLooper());
122 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700123 return NO_ERROR;
124}
125
126status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700127 return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
Mathias Agopian589ce852010-07-13 22:21:56 -0700128}
129
Mathias Agopiana7352c92010-07-14 23:41:37 -0700130status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700131 return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
Mathias Agopian589ce852010-07-13 22:21:56 -0700132}
133
Aravind Akella724d91d2013-06-27 12:04:23 -0700134status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
135 int maxBatchReportLatencyUs, int reservedFlags) const {
136 return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
137 us2ns(maxBatchReportLatencyUs), reservedFlags);
138}
139
Aravind Akella701166d2013-10-08 14:59:26 -0700140status_t SensorEventQueue::flush() const {
141 return mSensorEventConnection->flush();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700142}
143
144status_t SensorEventQueue::disableSensor(int32_t handle) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700145 return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700146}
147
148status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700149 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
150}
151
Aravind Akella9a844cf2014-02-11 18:58:52 -0800152void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
153 for (int i = 0; i < count; ++i) {
154 if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
Aravind Akella8a969552014-09-28 17:52:41 -0700155 ++mNumAcksToSend;
156 }
157 }
158 // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
159 if (mNumAcksToSend > 0) {
160 ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
161 MSG_DONTWAIT | MSG_NOSIGNAL);
162 if (size < 0) {
Dan Stozaf10c46e2014-11-11 10:32:31 -0800163 ALOGE("sendAck failure %zd %d", size, mNumAcksToSend);
Aravind Akella8a969552014-09-28 17:52:41 -0700164 } else {
165 mNumAcksToSend = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800166 }
167 }
168 return;
169}
170
Mathias Agopian589ce852010-07-13 22:21:56 -0700171// ----------------------------------------------------------------------------
172}; // namespace android
173