blob: 8a1bf41826824f025185fc6d75506dfa33985cf6 [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)
38 : mSensorEventConnection(connection)
39{
40}
41
42SensorEventQueue::~SensorEventQueue()
43{
44}
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
62ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
63{
Mathias Agopian7b5be952012-04-02 17:02:19 -070064 return BitTube::recvObjects(mSensorChannel, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070065}
66
Jeff Brown59abe7e2010-09-13 23:17:30 -070067sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -070068{
Mathias Agopiana7352c92010-07-14 23:41:37 -070069 Mutex::Autolock _l(mLock);
Jeff Brown59abe7e2010-09-13 23:17:30 -070070 if (mLooper == 0) {
71 mLooper = new Looper(true);
72 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
Mathias Agopiana7352c92010-07-14 23:41:37 -070073 }
Jeff Brown59abe7e2010-09-13 23:17:30 -070074 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -070075}
76
77status_t SensorEventQueue::waitForEvent() const
78{
79 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -070080 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -070081
Mathias Agopian29267fe2012-05-07 15:20:15 -070082 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -070083 int32_t result;
84 do {
Mathias Agopian29267fe2012-05-07 15:20:15 -070085 result = looper->pollOnce(-1, NULL, &events, NULL);
86 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +000087 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -070088 result = -EPIPE; // unknown error, so we make up one
89 break;
90 }
Mathias Agopian29267fe2012-05-07 15:20:15 -070091 if (events & ALOOPER_EVENT_HANGUP) {
92 // the other-side has died
93 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
94 result = -EPIPE; // unknown error, so we make up one
95 break;
96 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -070097 } while (result != fd);
98
Mathias Agopian2ffb2472010-09-16 21:41:13 -070099 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700100}
101
102status_t SensorEventQueue::wake() const
103{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700104 sp<Looper> looper(getLooper());
105 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700106 return NO_ERROR;
107}
108
109status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700110 return mSensorEventConnection->enableDisable(sensor->getHandle(), true);
111}
112
Mathias Agopiana7352c92010-07-14 23:41:37 -0700113status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700114 return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
115}
116
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700117status_t SensorEventQueue::enableSensor(int32_t handle, int32_t us) const {
Mathias Agopiane3c82342010-07-21 15:59:50 -0700118 status_t err = mSensorEventConnection->enableDisable(handle, true);
119 if (err == NO_ERROR) {
Mathias Agopiana48bcf62010-07-29 16:51:38 -0700120 mSensorEventConnection->setEventRate(handle, us2ns(us));
Mathias Agopiane3c82342010-07-21 15:59:50 -0700121 }
122 return err;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700123}
124
125status_t SensorEventQueue::disableSensor(int32_t handle) const {
126 return mSensorEventConnection->enableDisable(handle, false);
127}
128
129status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700130 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
131}
132
133// ----------------------------------------------------------------------------
134}; // namespace android
135