blob: 1305e9f63a6c0f1c04fdc4a91dfa3f3cc80a0f5a [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>
Aravind Akella56ae4262014-07-10 16:01:10 -070021#include <sys/socket.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070022
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
Jeff Brown59abe7e2010-09-13 23:17:30 -070025#include <utils/Looper.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070026
27#include <gui/Sensor.h>
Mathias Agopian5cae0d02011-10-20 18:42:02 -070028#include <gui/BitTube.h>
Mathias Agopian589ce852010-07-13 22:21:56 -070029#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)
Aravind Akella8a969552014-09-28 17:52:41 -070039 : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0),
40 mNumAcksToSend(0) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -070041 mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
Mathias Agopian589ce852010-07-13 22:21:56 -070042}
43
Mathias Agopian90ed3e82013-09-09 23:36:25 -070044SensorEventQueue::~SensorEventQueue() {
45 delete [] mRecBuffer;
Mathias Agopian589ce852010-07-13 22:21:56 -070046}
47
48void SensorEventQueue::onFirstRef()
49{
50 mSensorChannel = mSensorEventConnection->getSensorChannel();
51}
52
53int SensorEventQueue::getFd() const
54{
55 return mSensorChannel->getFd();
56}
57
Mathias Agopian7b5be952012-04-02 17:02:19 -070058
59ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
60 ASensorEvent const* events, size_t numEvents) {
61 return BitTube::sendObjects(tube, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070062}
63
Mathias Agopian90ed3e82013-09-09 23:36:25 -070064ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
65 if (mAvailable == 0) {
66 ssize_t err = BitTube::recvObjects(mSensorChannel,
67 mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
68 if (err < 0) {
69 return err;
70 }
71 mAvailable = err;
72 mConsumed = 0;
73 }
74 size_t count = numEvents < mAvailable ? numEvents : mAvailable;
75 memcpy(events, mRecBuffer + mConsumed, count*sizeof(ASensorEvent));
76 mAvailable -= count;
77 mConsumed += count;
78 return count;
Mathias Agopian589ce852010-07-13 22:21:56 -070079}
80
Jeff Brown59abe7e2010-09-13 23:17:30 -070081sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -070082{
Mathias Agopiana7352c92010-07-14 23:41:37 -070083 Mutex::Autolock _l(mLock);
Jeff Brown59abe7e2010-09-13 23:17:30 -070084 if (mLooper == 0) {
85 mLooper = new Looper(true);
86 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
Mathias Agopiana7352c92010-07-14 23:41:37 -070087 }
Jeff Brown59abe7e2010-09-13 23:17:30 -070088 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -070089}
90
91status_t SensorEventQueue::waitForEvent() const
92{
93 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -070094 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -070095
Mathias Agopian29267fe2012-05-07 15:20:15 -070096 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -070097 int32_t result;
98 do {
Mathias Agopian29267fe2012-05-07 15:20:15 -070099 result = looper->pollOnce(-1, NULL, &events, NULL);
100 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000101 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700102 result = -EPIPE; // unknown error, so we make up one
103 break;
104 }
Mathias Agopian29267fe2012-05-07 15:20:15 -0700105 if (events & ALOOPER_EVENT_HANGUP) {
106 // the other-side has died
107 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
108 result = -EPIPE; // unknown error, so we make up one
109 break;
110 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700111 } while (result != fd);
112
Mathias Agopian2ffb2472010-09-16 21:41:13 -0700113 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700114}
115
116status_t SensorEventQueue::wake() const
117{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700118 sp<Looper> looper(getLooper());
119 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700120 return NO_ERROR;
121}
122
123status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700124 return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
Mathias Agopian589ce852010-07-13 22:21:56 -0700125}
126
Mathias Agopiana7352c92010-07-14 23:41:37 -0700127status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700128 return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false);
Mathias Agopian589ce852010-07-13 22:21:56 -0700129}
130
Aravind Akella724d91d2013-06-27 12:04:23 -0700131status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
132 int maxBatchReportLatencyUs, int reservedFlags) const {
133 return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
134 us2ns(maxBatchReportLatencyUs), reservedFlags);
135}
136
Aravind Akella701166d2013-10-08 14:59:26 -0700137status_t SensorEventQueue::flush() const {
138 return mSensorEventConnection->flush();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700139}
140
141status_t SensorEventQueue::disableSensor(int32_t handle) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700142 return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700143}
144
145status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700146 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
147}
148
Aravind Akella9a844cf2014-02-11 18:58:52 -0800149void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
150 for (int i = 0; i < count; ++i) {
151 if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
Aravind Akella8a969552014-09-28 17:52:41 -0700152 ++mNumAcksToSend;
153 }
154 }
155 // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
156 if (mNumAcksToSend > 0) {
157 ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
158 MSG_DONTWAIT | MSG_NOSIGNAL);
159 if (size < 0) {
160 ALOGE("sendAck failure %d %d", size, mNumAcksToSend);
161 } else {
162 mNumAcksToSend = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800163 }
164 }
165 return;
166}
167
Mathias Agopian589ce852010-07-13 22:21:56 -0700168// ----------------------------------------------------------------------------
169}; // namespace android
170