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 | */ |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "Sensors" |
| 18 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame^] | 19 | #include <algorithm> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 22 | #include <sys/socket.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 23 | |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/RefBase.h> |
Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 26 | #include <utils/Looper.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 27 | |
| 28 | #include <gui/Sensor.h> |
Mathias Agopian | 5cae0d0 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 29 | #include <gui/BitTube.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 30 | #include <gui/SensorEventQueue.h> |
| 31 | #include <gui/ISensorEventConnection.h> |
| 32 | |
| 33 | #include <android/sensor.h> |
| 34 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame^] | 35 | using std::min; |
| 36 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 37 | // ---------------------------------------------------------------------------- |
| 38 | namespace android { |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
| 41 | SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection) |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 42 | : mSensorEventConnection(connection), mRecBuffer(NULL), mAvailable(0), mConsumed(0), |
| 43 | mNumAcksToSend(0) { |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 44 | mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT]; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 47 | SensorEventQueue::~SensorEventQueue() { |
| 48 | delete [] mRecBuffer; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void SensorEventQueue::onFirstRef() |
| 52 | { |
| 53 | mSensorChannel = mSensorEventConnection->getSensorChannel(); |
| 54 | } |
| 55 | |
| 56 | int SensorEventQueue::getFd() const |
| 57 | { |
| 58 | return mSensorChannel->getFd(); |
| 59 | } |
| 60 | |
Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 61 | |
| 62 | ssize_t SensorEventQueue::write(const sp<BitTube>& tube, |
| 63 | ASensorEvent const* events, size_t numEvents) { |
| 64 | return BitTube::sendObjects(tube, events, numEvents); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 67 | ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) { |
| 68 | if (mAvailable == 0) { |
| 69 | ssize_t err = BitTube::recvObjects(mSensorChannel, |
| 70 | mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT); |
| 71 | if (err < 0) { |
| 72 | return err; |
| 73 | } |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame^] | 74 | mAvailable = static_cast<size_t>(err); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 75 | mConsumed = 0; |
| 76 | } |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame^] | 77 | size_t count = min(numEvents, mAvailable); |
| 78 | memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent)); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 79 | mAvailable -= count; |
| 80 | mConsumed += count; |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame^] | 81 | return static_cast<ssize_t>(count); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 84 | sp<Looper> SensorEventQueue::getLooper() const |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 85 | { |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 86 | Mutex::Autolock _l(mLock); |
Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 87 | if (mLooper == 0) { |
| 88 | mLooper = new Looper(true); |
| 89 | mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, NULL, NULL); |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 90 | } |
Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 91 | return mLooper; |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | status_t SensorEventQueue::waitForEvent() const |
| 95 | { |
| 96 | const int fd = getFd(); |
Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 97 | sp<Looper> looper(getLooper()); |
Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 98 | |
Mathias Agopian | 29267fe | 2012-05-07 15:20:15 -0700 | [diff] [blame] | 99 | int events; |
Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 100 | int32_t result; |
| 101 | do { |
Mathias Agopian | 29267fe | 2012-05-07 15:20:15 -0700 | [diff] [blame] | 102 | result = looper->pollOnce(-1, NULL, &events, NULL); |
| 103 | if (result == ALOOPER_POLL_ERROR) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 104 | ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno); |
Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 105 | result = -EPIPE; // unknown error, so we make up one |
| 106 | break; |
| 107 | } |
Mathias Agopian | 29267fe | 2012-05-07 15:20:15 -0700 | [diff] [blame] | 108 | if (events & ALOOPER_EVENT_HANGUP) { |
| 109 | // the other-side has died |
| 110 | ALOGE("SensorEventQueue::waitForEvent error HANGUP"); |
| 111 | result = -EPIPE; // unknown error, so we make up one |
| 112 | break; |
| 113 | } |
Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 114 | } while (result != fd); |
| 115 | |
Mathias Agopian | 2ffb247 | 2010-09-16 21:41:13 -0700 | [diff] [blame] | 116 | return (result == fd) ? status_t(NO_ERROR) : result; |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | status_t SensorEventQueue::wake() const |
| 120 | { |
Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 121 | sp<Looper> looper(getLooper()); |
| 122 | looper->wake(); |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 123 | return NO_ERROR; |
| 124 | } |
| 125 | |
| 126 | status_t SensorEventQueue::enableSensor(Sensor const* sensor) const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 127 | return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 130 | status_t SensorEventQueue::disableSensor(Sensor const* sensor) const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 131 | return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, false); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 134 | status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs, |
| 135 | int maxBatchReportLatencyUs, int reservedFlags) const { |
| 136 | return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs), |
| 137 | us2ns(maxBatchReportLatencyUs), reservedFlags); |
| 138 | } |
| 139 | |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 140 | status_t SensorEventQueue::flush() const { |
| 141 | return mSensorEventConnection->flush(); |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | status_t SensorEventQueue::disableSensor(int32_t handle) const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 145 | return mSensorEventConnection->enableDisable(handle, false, 0, 0, false); |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const { |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 149 | return mSensorEventConnection->setEventRate(sensor->getHandle(), ns); |
| 150 | } |
| 151 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 152 | void SensorEventQueue::sendAck(const ASensorEvent* events, int count) { |
| 153 | for (int i = 0; i < count; ++i) { |
| 154 | if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 155 | ++mNumAcksToSend; |
| 156 | } |
| 157 | } |
| 158 | // Send mNumAcksToSend to acknowledge for the wake up sensor events received. |
| 159 | if (mNumAcksToSend > 0) { |
| 160 | ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend), |
| 161 | MSG_DONTWAIT | MSG_NOSIGNAL); |
| 162 | if (size < 0) { |
Dan Stoza | f10c46e | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 163 | ALOGE("sendAck failure %zd %d", size, mNumAcksToSend); |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 164 | } else { |
| 165 | mNumAcksToSend = 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | return; |
| 169 | } |
| 170 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 171 | // ---------------------------------------------------------------------------- |
| 172 | }; // namespace android |
| 173 | |