blob: 6f68fb5f7bad48a647e1680cdc48c6df9daee9ee [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 Agopian801ea092017-03-06 15:05:04 -080019#include <sensor/SensorEventQueue.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070020
Mathias Agopian801ea092017-03-06 15:05:04 -080021#include <algorithm>
22#include <sys/socket.h>
23
Mathias Agopian589ce852010-07-13 22:21:56 -070024#include <utils/RefBase.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070025#include <utils/Looper.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070026
Mathias Agopian801ea092017-03-06 15:05:04 -080027#include <sensor/Sensor.h>
28#include <sensor/BitTube.h>
29#include <sensor/ISensorEventConnection.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070030
31#include <android/sensor.h>
32
Dan Stozad723bd72014-11-18 10:24:03 -080033using std::min;
34
Mathias Agopian589ce852010-07-13 22:21:56 -070035// ----------------------------------------------------------------------------
36namespace android {
37// ----------------------------------------------------------------------------
38
39SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
Aravind Akella8a969552014-09-28 17:52:41 -070040 : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
41 mNumAcksToSend(0) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -070042 mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
Mathias Agopian589ce852010-07-13 22:21:56 -070043}
44
Mathias Agopian90ed3e82013-09-09 23:36:25 -070045SensorEventQueue::~SensorEventQueue() {
46 delete [] mRecBuffer;
Mathias Agopian589ce852010-07-13 22:21:56 -070047}
48
49void SensorEventQueue::onFirstRef()
50{
51 mSensorChannel = mSensorEventConnection->getSensorChannel();
52}
53
54int SensorEventQueue::getFd() const
55{
56 return mSensorChannel->getFd();
57}
58
Mathias Agopian7b5be952012-04-02 17:02:19 -070059
60ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
61 ASensorEvent const* events, size_t numEvents) {
62 return BitTube::sendObjects(tube, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070063}
64
Mathias Agopian90ed3e82013-09-09 23:36:25 -070065ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
66 if (mAvailable == 0) {
67 ssize_t err = BitTube::recvObjects(mSensorChannel,
68 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
69 if (err < 0) {
70 return err;
71 }
Dan Stozad723bd72014-11-18 10:24:03 -080072 mAvailable = static_cast<size_t>(err);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070073 mConsumed = 0;
74 }
Dan Stozad723bd72014-11-18 10:24:03 -080075 size_t count = min(numEvents, mAvailable);
76 memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent));
Mathias Agopian90ed3e82013-09-09 23:36:25 -070077 mAvailable -= count;
78 mConsumed += count;
Dan Stozad723bd72014-11-18 10:24:03 -080079 return static_cast<ssize_t>(count);
Mathias Agopian589ce852010-07-13 22:21:56 -070080}
81
Jeff Brown59abe7e2010-09-13 23:17:30 -070082sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -070083{
Mathias Agopiana7352c92010-07-14 23:41:37 -070084 Mutex::Autolock _l(mLock);
Jeff Brown59abe7e2010-09-13 23:17:30 -070085 if (mLooper == 0) {
86 mLooper = new Looper(true);
87 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
Mathias Agopiana7352c92010-07-14 23:41:37 -070088 }
Jeff Brown59abe7e2010-09-13 23:17:30 -070089 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -070090}
91
92status_t SensorEventQueue::waitForEvent() const
93{
94 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -070095 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -070096
Mathias Agopian29267fe2012-05-07 15:20:15 -070097 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -070098 int32_t result;
99 do {
Mathias Agopian29267fe2012-05-07 15:20:15 -0700100 result = looper->pollOnce(-1, NULL, &events, NULL);
101 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000102 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700103 result = -EPIPE; // unknown error, so we make up one
104 break;
105 }
Mathias Agopian29267fe2012-05-07 15:20:15 -0700106 if (events & ALOOPER_EVENT_HANGUP) {
107 // the other-side has died
108 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
109 result = -EPIPE; // unknown error, so we make up one
110 break;
111 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700112 } while (result != fd);
113
Mathias Agopian2ffb2472010-09-16 21:41:13 -0700114 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700115}
116
117status_t SensorEventQueue::wake() const
118{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700119 sp<Looper> looper(getLooper());
120 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700121 return NO_ERROR;
122}
123
124status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Peng Xu2f3bf132016-03-16 18:05:35 -0700125 return enableSensor(sensor, SENSOR_DELAY_NORMAL);
126}
127
128status_t SensorEventQueue::enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const {
129 return mSensorEventConnection->enableDisable(sensor->getHandle(), true,
130 us2ns(samplingPeriodUs), 0, 0);
Mathias Agopian589ce852010-07-13 22:21:56 -0700131}
132
Mathias Agopiana7352c92010-07-14 23:41:37 -0700133status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Peng Xu2f3bf132016-03-16 18:05:35 -0700134 return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, 0);
Mathias Agopian589ce852010-07-13 22:21:56 -0700135}
136
Aravind Akella724d91d2013-06-27 12:04:23 -0700137status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
Peng Xuda8385c2017-02-28 20:19:47 -0800138 int64_t maxBatchReportLatencyUs, int reservedFlags) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700139 return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
140 us2ns(maxBatchReportLatencyUs), reservedFlags);
141}
142
Aravind Akella701166d2013-10-08 14:59:26 -0700143status_t SensorEventQueue::flush() const {
144 return mSensorEventConnection->flush();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700145}
146
147status_t SensorEventQueue::disableSensor(int32_t handle) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700148 return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700149}
150
151status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700152 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
153}
154
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700155status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) {
Aravind Akella5c538052015-06-29 12:37:48 -0700156 do {
157 // Blocking call.
158 ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL);
159 if (size >= 0) {
160 return NO_ERROR;
161 } else if (size < 0 && errno == EAGAIN) {
162 // If send is returning a "Try again" error, sleep for 100ms and try again. In all
163 // other cases log a failure and exit.
164 usleep(100000);
165 } else {
166 ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size);
167 return INVALID_OPERATION;
168 }
169 } while (true);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700170}
171
Aravind Akella9a844cf2014-02-11 18:58:52 -0800172void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
173 for (int i = 0; i < count; ++i) {
174 if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
Aravind Akella8a969552014-09-28 17:52:41 -0700175 ++mNumAcksToSend;
176 }
177 }
178 // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
179 if (mNumAcksToSend > 0) {
180 ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
181 MSG_DONTWAIT | MSG_NOSIGNAL);
182 if (size < 0) {
Dan Stozaf10c46e2014-11-11 10:32:31 -0800183 ALOGE("sendAck failure %zd %d", size, mNumAcksToSend);
Aravind Akella8a969552014-09-28 17:52:41 -0700184 } else {
185 mNumAcksToSend = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800186 }
187 }
188 return;
189}
190
Mathias Agopian589ce852010-07-13 22:21:56 -0700191// ----------------------------------------------------------------------------
192}; // namespace android
193