blob: 7eb6da5a6e9ba924ec249a5a6e02e2f8b60d20a4 [file] [log] [blame]
Mathias Agopianb957b9d2010-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 Agopian1bf79782010-07-14 23:41:37 -070016
17#define LOG_TAG "Sensors"
18
Mathias Agopianb957b9d2010-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>
Mathias Agopian1bf79782010-07-14 23:41:37 -070024#include <utils/PollLoop.h>
Mathias Agopianb957b9d2010-07-13 22:21:56 -070025
26#include <gui/Sensor.h>
27#include <gui/SensorChannel.h>
28#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
56ssize_t SensorEventQueue::write(ASensorEvent const* events, size_t numEvents)
57{
58 ssize_t size = mSensorChannel->write(events, numEvents * sizeof(events[0]));
59 if (size >= 0) {
60 if (size % sizeof(events[0])) {
61 // partial write!!! should never happen.
62 return -EINVAL;
63 }
64 // returns number of events written
65 size /= sizeof(events[0]);
66 }
67 return size;
68}
69
70ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents)
71{
72 ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0]));
73 if (size >= 0) {
74 if (size % sizeof(events[0])) {
Mathias Agopian1bf79782010-07-14 23:41:37 -070075 // partial read!!! should never happen.
Mathias Agopianb957b9d2010-07-13 22:21:56 -070076 return -EINVAL;
77 }
78 // returns number of events read
79 size /= sizeof(events[0]);
80 }
81 return size;
82}
83
Mathias Agopian1bf79782010-07-14 23:41:37 -070084sp<PollLoop> SensorEventQueue::getPollLoop() const
Mathias Agopianb957b9d2010-07-13 22:21:56 -070085{
Mathias Agopian1bf79782010-07-14 23:41:37 -070086 Mutex::Autolock _l(mLock);
87 if (mPollLoop == 0) {
88 mPollLoop = new PollLoop(true);
Dianne Hackborn42c03e52010-09-07 15:28:30 -070089 mPollLoop->setCallback(getFd(), getFd(), POLLIN, NULL, NULL);
Mathias Agopian1bf79782010-07-14 23:41:37 -070090 }
91 return mPollLoop;
92}
93
94status_t SensorEventQueue::waitForEvent() const
95{
96 const int fd = getFd();
97 sp<PollLoop> pollLoop(getPollLoop());
98 int32_t result = pollLoop->pollOnce(-1, NULL, NULL);
99 return (result == fd) ? NO_ERROR : -1;
100}
101
102status_t SensorEventQueue::wake() const
103{
104 sp<PollLoop> pollLoop(getPollLoop());
105 pollLoop->wake();
106 return NO_ERROR;
107}
108
109status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700110 return mSensorEventConnection->enableDisable(sensor->getHandle(), true);
111}
112
Mathias Agopian1bf79782010-07-14 23:41:37 -0700113status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700114 return mSensorEventConnection->enableDisable(sensor->getHandle(), false);
115}
116
Mathias Agopian050b5622010-07-29 16:51:38 -0700117status_t SensorEventQueue::enableSensor(int32_t handle, int32_t us) const {
Mathias Agopian23e8de22010-07-21 15:59:50 -0700118 status_t err = mSensorEventConnection->enableDisable(handle, true);
119 if (err == NO_ERROR) {
Mathias Agopian050b5622010-07-29 16:51:38 -0700120 mSensorEventConnection->setEventRate(handle, us2ns(us));
Mathias Agopian23e8de22010-07-21 15:59:50 -0700121 }
122 return err;
Mathias Agopian1bf79782010-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 Agopianb957b9d2010-07-13 22:21:56 -0700130 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
131}
132
133// ----------------------------------------------------------------------------
134}; // namespace android
135