Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | #include <stdint.h> |
| 17 | #include <sys/types.h> |
| 18 | |
| 19 | #include <utils/Errors.h> |
| 20 | #include <utils/RefBase.h> |
| 21 | |
| 22 | #include <gui/Sensor.h> |
| 23 | #include <gui/SensorChannel.h> |
| 24 | #include <gui/SensorEventQueue.h> |
| 25 | #include <gui/ISensorEventConnection.h> |
| 26 | |
| 27 | #include <android/sensor.h> |
| 28 | |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | namespace android { |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection) |
| 34 | : mSensorEventConnection(connection) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | SensorEventQueue::~SensorEventQueue() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void SensorEventQueue::onFirstRef() |
| 43 | { |
| 44 | mSensorChannel = mSensorEventConnection->getSensorChannel(); |
| 45 | } |
| 46 | |
| 47 | int SensorEventQueue::getFd() const |
| 48 | { |
| 49 | return mSensorChannel->getFd(); |
| 50 | } |
| 51 | |
| 52 | ssize_t SensorEventQueue::write(ASensorEvent const* events, size_t numEvents) |
| 53 | { |
| 54 | ssize_t size = mSensorChannel->write(events, numEvents * sizeof(events[0])); |
| 55 | if (size >= 0) { |
| 56 | if (size % sizeof(events[0])) { |
| 57 | // partial write!!! should never happen. |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | // returns number of events written |
| 61 | size /= sizeof(events[0]); |
| 62 | } |
| 63 | return size; |
| 64 | } |
| 65 | |
| 66 | ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) |
| 67 | { |
| 68 | ssize_t size = mSensorChannel->read(events, numEvents*sizeof(events[0])); |
| 69 | if (size >= 0) { |
| 70 | if (size % sizeof(events[0])) { |
| 71 | // partial write!!! should never happen. |
| 72 | return -EINVAL; |
| 73 | } |
| 74 | // returns number of events read |
| 75 | size /= sizeof(events[0]); |
| 76 | } |
| 77 | return size; |
| 78 | } |
| 79 | |
| 80 | status_t SensorEventQueue::enableSensor(Sensor const* sensor) const |
| 81 | { |
| 82 | return mSensorEventConnection->enableDisable(sensor->getHandle(), true); |
| 83 | } |
| 84 | |
| 85 | status_t SensorEventQueue::disableSensor(Sensor const* sensor) const |
| 86 | { |
| 87 | return mSensorEventConnection->enableDisable(sensor->getHandle(), false); |
| 88 | } |
| 89 | |
| 90 | status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const |
| 91 | { |
| 92 | return mSensorEventConnection->setEventRate(sensor->getHandle(), ns); |
| 93 | } |
| 94 | |
| 95 | // ---------------------------------------------------------------------------- |
| 96 | }; // namespace android |
| 97 | |