1589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian/*
2589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Copyright (C) 2010 The Android Open Source Project
3589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
4589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * you may not use this file except in compliance with the License.
6589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * You may obtain a copy of the License at
7589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
8589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *
10589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Unless required by applicable law or agreed to in writing, software
11589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * See the License for the specific language governing permissions and
14589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * limitations under the License.
15589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian */
16a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
17a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian#define LOG_TAG "Sensors"
18a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
19589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <stdint.h>
20589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <sys/types.h>
21589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
22589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Errors.h>
23589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/RefBase.h>
2459abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown#include <utils/Looper.h>
25589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
26589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/Sensor.h>
275cae0d0699a169e468fff3e21165f35db12f2cdeMathias Agopian#include <gui/BitTube.h>
28589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/SensorEventQueue.h>
29589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/ISensorEventConnection.h>
30589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
31589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <android/sensor.h>
32589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
33589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
34589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiannamespace android {
35589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
36589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
37589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianSensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection)
38589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    : mSensorEventConnection(connection)
39589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
40589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
41589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
42589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianSensorEventQueue::~SensorEventQueue()
43589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
44589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
45589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
46589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianvoid SensorEventQueue::onFirstRef()
47589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
48589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    mSensorChannel = mSensorEventConnection->getSensorChannel();
49589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
50589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
51589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianint SensorEventQueue::getFd() const
52589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
53589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mSensorChannel->getFd();
54589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
55589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
567b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian
577b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopianssize_t SensorEventQueue::write(const sp<BitTube>& tube,
587b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian        ASensorEvent const* events, size_t numEvents) {
597b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian    return BitTube::sendObjects(tube, events, numEvents);
60589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
61589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
62589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopianssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
63589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
647b5be95cb3903087742f1079fe89cddd8abe3696Mathias Agopian    return BitTube::recvObjects(mSensorChannel, events, numEvents);
65589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
66589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
6759abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brownsp<Looper> SensorEventQueue::getLooper() const
68589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
69a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    Mutex::Autolock _l(mLock);
7059abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown    if (mLooper == 0) {
7159abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown        mLooper = new Looper(true);
7259abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown        mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL);
73a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    }
7459abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown    return mLooper;
75589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
76589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
77a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianstatus_t SensorEventQueue::waitForEvent() const
78589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
79a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    const int fd = getFd();
8059abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown    sp<Looper> looper(getLooper());
81aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian
8229267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian    int events;
83aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian    int32_t result;
84aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian    do {
8529267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian        result = looper->pollOnce(-1, NULL, &events, NULL);
8629267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian        if (result == ALOOPER_POLL_ERROR) {
87e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
88aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian            result = -EPIPE; // unknown error, so we make up one
89aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian            break;
90aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian        }
9129267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian        if (events & ALOOPER_EVENT_HANGUP) {
9229267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian            // the other-side has died
9329267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian            ALOGE("SensorEventQueue::waitForEvent error HANGUP");
9429267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian            result = -EPIPE; // unknown error, so we make up one
9529267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian            break;
9629267fe8495a74893dfce1bd9eceb6448df7abeaMathias Agopian        }
97aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian    } while (result != fd);
98aeda9afdba5c28d4d152e3ec3bce74be42949065Mathias Agopian
992ffb24799e579c47a73c992ab567f05c4bf0c962Mathias Agopian    return  (result == fd) ? status_t(NO_ERROR) : result;
100589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
101589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
102a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianstatus_t SensorEventQueue::wake() const
103589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
10459abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown    sp<Looper> looper(getLooper());
10559abe7e0909bf4b7bf7b9601e1e40a05f6d4fd8aJeff Brown    looper->wake();
106a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    return NO_ERROR;
107a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian}
108a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
109a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianstatus_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
110a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    return mSensorEventConnection->enableDisable(sensor->getHandle(), true);
111a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian}
112a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
113a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianstatus_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
114a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
115a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian}
116a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
117a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopianstatus_t SensorEventQueue::enableSensor(int32_t handle, int32_t us) const {
118e3c8234765050023b8a588607f819e8cc41a6363Mathias Agopian    status_t err = mSensorEventConnection->enableDisable(handle, true);
119e3c8234765050023b8a588607f819e8cc41a6363Mathias Agopian    if (err == NO_ERROR) {
120a48bcf62b6a26f24a0bdd2b44bb39fadce499e92Mathias Agopian        mSensorEventConnection->setEventRate(handle, us2ns(us));
121e3c8234765050023b8a588607f819e8cc41a6363Mathias Agopian    }
122e3c8234765050023b8a588607f819e8cc41a6363Mathias Agopian    return err;
123a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian}
124a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
125a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianstatus_t SensorEventQueue::disableSensor(int32_t handle) const {
126a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    return mSensorEventConnection->enableDisable(handle, false);
127a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian}
128a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian
129a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianstatus_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
130589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian    return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
131589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
132589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
133589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
134589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}; // namespace android
135589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
136