blob: 6d698390c1d0df8febcaf2983480bff455bb0178 [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>
Aravind Akella5c538052015-06-29 12:37:48 -070023#include <linux/errno.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070024
25#include <utils/Errors.h>
26#include <utils/RefBase.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070027#include <utils/Looper.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070028
29#include <gui/Sensor.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070030#include <gui/BitTube.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070031#include <gui/SensorEventQueue.h>
32#include <gui/ISensorEventConnection.h>
33
34#include <android/sensor.h>
35
Dan Stozad723bd72014-11-18 10:24:03 -080036using std::min;
37
Mathias Agopian589ce852010-07-13 22:21:56 -070038// ----------------------------------------------------------------------------
39namespace android {
40// ----------------------------------------------------------------------------
41
42SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
Aravind Akella8a969552014-09-28 17:52:41 -070043 : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
44 mNumAcksToSend(0) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -070045 mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
Mathias Agopian589ce852010-07-13 22:21:56 -070046}
47
Mathias Agopian90ed3e82013-09-09 23:36:25 -070048SensorEventQueue::~SensorEventQueue() {
49 delete [] mRecBuffer;
Mathias Agopian589ce852010-07-13 22:21:56 -070050}
51
52void SensorEventQueue::onFirstRef()
53{
54 mSensorChannel = mSensorEventConnection->getSensorChannel();
55}
56
57int SensorEventQueue::getFd() const
58{
59 return mSensorChannel->getFd();
60}
61
Mathias Agopian7b5be952012-04-02 17:02:19 -070062
63ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
64 ASensorEvent const* events, size_t numEvents) {
65 return BitTube::sendObjects(tube, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070066}
67
Mathias Agopian90ed3e82013-09-09 23:36:25 -070068ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
69 if (mAvailable == 0) {
70 ssize_t err = BitTube::recvObjects(mSensorChannel,
71 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
72 if (err < 0) {
73 return err;
74 }
Dan Stozad723bd72014-11-18 10:24:03 -080075 mAvailable = static_cast<size_t>(err);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070076 mConsumed = 0;
77 }
Dan Stozad723bd72014-11-18 10:24:03 -080078 size_t count = min(numEvents, mAvailable);
79 memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent));
Mathias Agopian90ed3e82013-09-09 23:36:25 -070080 mAvailable -= count;
81 mConsumed += count;
Dan Stozad723bd72014-11-18 10:24:03 -080082 return static_cast<ssize_t>(count);
Mathias Agopian589ce852010-07-13 22:21:56 -070083}
84
Jeff Brown59abe7e2010-09-13 23:17:30 -070085sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -070086{
Mathias Agopiana7352c92010-07-14 23:41:37 -070087 Mutex::Autolock _l(mLock);
Jeff Brown59abe7e2010-09-13 23:17:30 -070088 if (mLooper == 0) {
89 mLooper = new Looper(true);
90 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
Mathias Agopiana7352c92010-07-14 23:41:37 -070091 }
Jeff Brown59abe7e2010-09-13 23:17:30 -070092 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -070093}
94
95status_t SensorEventQueue::waitForEvent() const
96{
97 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -070098 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -070099
Mathias Agopian29267fe2012-05-07 15:20:15 -0700100 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700101 int32_t result;
102 do {
Mathias Agopian29267fe2012-05-07 15:20:15 -0700103 result = looper->pollOnce(-1, NULL, &events, NULL);
104 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000105 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700106 result = -EPIPE; // unknown error, so we make up one
107 break;
108 }
Mathias Agopian29267fe2012-05-07 15:20:15 -0700109 if (events & ALOOPER_EVENT_HANGUP) {
110 // the other-side has died
111 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
112 result = -EPIPE; // unknown error, so we make up one
113 break;
114 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700115 } while (result != fd);
116
Mathias Agopian2ffb2472010-09-16 21:41:13 -0700117 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700118}
119
120status_t SensorEventQueue::wake() const
121{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700122 sp<Looper> looper(getLooper());
123 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700124 return NO_ERROR;
125}
126
127status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Peng Xu2f3bf132016-03-16 18:05:35 -0700128 return enableSensor(sensor, SENSOR_DELAY_NORMAL);
129}
130
131status_t SensorEventQueue::enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const {
132 return mSensorEventConnection->enableDisable(sensor->getHandle(), true,
133 us2ns(samplingPeriodUs), 0, 0);
Mathias Agopian589ce852010-07-13 22:21:56 -0700134}
135
Mathias Agopiana7352c92010-07-14 23:41:37 -0700136status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Peng Xu2f3bf132016-03-16 18:05:35 -0700137 return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, 0);
Mathias Agopian589ce852010-07-13 22:21:56 -0700138}
139
Aravind Akella724d91d2013-06-27 12:04:23 -0700140status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
141 int maxBatchReportLatencyUs, int reservedFlags) const {
142 return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
143 us2ns(maxBatchReportLatencyUs), reservedFlags);
144}
145
Aravind Akella701166d2013-10-08 14:59:26 -0700146status_t SensorEventQueue::flush() const {
147 return mSensorEventConnection->flush();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700148}
149
150status_t SensorEventQueue::disableSensor(int32_t handle) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700151 return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700152}
153
154status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700155 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
156}
157
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700158status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) {
Aravind Akella5c538052015-06-29 12:37:48 -0700159 do {
160 // Blocking call.
161 ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL);
162 if (size >= 0) {
163 return NO_ERROR;
164 } else if (size < 0 && errno == EAGAIN) {
165 // If send is returning a "Try again" error, sleep for 100ms and try again. In all
166 // other cases log a failure and exit.
167 usleep(100000);
168 } else {
169 ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size);
170 return INVALID_OPERATION;
171 }
172 } while (true);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700173}
174
Aravind Akella9a844cf2014-02-11 18:58:52 -0800175void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
176 for (int i = 0; i < count; ++i) {
177 if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
Aravind Akella8a969552014-09-28 17:52:41 -0700178 ++mNumAcksToSend;
179 }
180 }
181 // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
182 if (mNumAcksToSend > 0) {
183 ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
184 MSG_DONTWAIT | MSG_NOSIGNAL);
185 if (size < 0) {
Dan Stozaf10c46e2014-11-11 10:32:31 -0800186 ALOGE("sendAck failure %zd %d", size, mNumAcksToSend);
Aravind Akella8a969552014-09-28 17:52:41 -0700187 } else {
188 mNumAcksToSend = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800189 }
190 }
191 return;
192}
193
Mathias Agopian589ce852010-07-13 22:21:56 -0700194// ----------------------------------------------------------------------------
195}; // namespace android
196