| 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 |  | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 19 | #include <sensor/SensorEventQueue.h> | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 20 |  | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 21 | #include <algorithm> | 
|  | 22 | #include <sys/socket.h> | 
|  | 23 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> | 
| Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 25 | #include <utils/Looper.h> | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 26 |  | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 27 | #include <sensor/Sensor.h> | 
|  | 28 | #include <sensor/BitTube.h> | 
|  | 29 | #include <sensor/ISensorEventConnection.h> | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | #include <android/sensor.h> | 
| Brian Stack | e318d6b | 2019-01-16 15:43:37 -0800 | [diff] [blame] | 32 | #include <hardware/sensors-base.h> | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 33 |  | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 34 | using std::min; | 
|  | 35 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 36 | // ---------------------------------------------------------------------------- | 
|  | 37 | namespace android { | 
|  | 38 | // ---------------------------------------------------------------------------- | 
|  | 39 |  | 
|  | 40 | SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection) | 
| Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 41 | : mSensorEventConnection(connection), mRecBuffer(nullptr), mAvailable(0), mConsumed(0), | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 42 | mNumAcksToSend(0) { | 
| Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 43 | mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT]; | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
| Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 46 | SensorEventQueue::~SensorEventQueue() { | 
|  | 47 | delete [] mRecBuffer; | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | void SensorEventQueue::onFirstRef() | 
|  | 51 | { | 
|  | 52 | mSensorChannel = mSensorEventConnection->getSensorChannel(); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | int SensorEventQueue::getFd() const | 
|  | 56 | { | 
|  | 57 | return mSensorChannel->getFd(); | 
|  | 58 | } | 
|  | 59 |  | 
| Mathias Agopian | 7b5be95 | 2012-04-02 17:02:19 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | ssize_t SensorEventQueue::write(const sp<BitTube>& tube, | 
|  | 62 | ASensorEvent const* events, size_t numEvents) { | 
|  | 63 | return BitTube::sendObjects(tube, events, numEvents); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 66 | ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) { | 
|  | 67 | if (mAvailable == 0) { | 
|  | 68 | ssize_t err = BitTube::recvObjects(mSensorChannel, | 
|  | 69 | mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT); | 
|  | 70 | if (err < 0) { | 
|  | 71 | return err; | 
|  | 72 | } | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 73 | mAvailable = static_cast<size_t>(err); | 
| Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 74 | mConsumed = 0; | 
|  | 75 | } | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 76 | size_t count = min(numEvents, mAvailable); | 
|  | 77 | memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent)); | 
| Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 78 | mAvailable -= count; | 
|  | 79 | mConsumed += count; | 
| Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 80 | return static_cast<ssize_t>(count); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 83 | sp<Looper> SensorEventQueue::getLooper() const | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 84 | { | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 85 | Mutex::Autolock _l(mLock); | 
| Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 86 | if (mLooper == nullptr) { | 
| Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 87 | mLooper = new Looper(true); | 
| Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 88 | mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, nullptr, nullptr); | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 89 | } | 
| Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 90 | return mLooper; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | status_t SensorEventQueue::waitForEvent() const | 
|  | 94 | { | 
|  | 95 | const int fd = getFd(); | 
| Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 96 | sp<Looper> looper(getLooper()); | 
| Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 97 |  | 
| Mathias Agopian | 29267fe | 2012-05-07 15:20:15 -0700 | [diff] [blame] | 98 | int events; | 
| Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 99 | int32_t result; | 
|  | 100 | do { | 
| Yi Kong | d5e079f | 2018-07-17 16:08:27 -0700 | [diff] [blame] | 101 | result = looper->pollOnce(-1, nullptr, &events, nullptr); | 
| Mathias Agopian | 29267fe | 2012-05-07 15:20:15 -0700 | [diff] [blame] | 102 | if (result == ALOOPER_POLL_ERROR) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 103 | ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno); | 
| Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 104 | result = -EPIPE; // unknown error, so we make up one | 
|  | 105 | break; | 
|  | 106 | } | 
| Mathias Agopian | 29267fe | 2012-05-07 15:20:15 -0700 | [diff] [blame] | 107 | if (events & ALOOPER_EVENT_HANGUP) { | 
|  | 108 | // the other-side has died | 
|  | 109 | ALOGE("SensorEventQueue::waitForEvent error HANGUP"); | 
|  | 110 | result = -EPIPE; // unknown error, so we make up one | 
|  | 111 | break; | 
|  | 112 | } | 
| Mathias Agopian | aeda9af | 2010-09-16 17:04:16 -0700 | [diff] [blame] | 113 | } while (result != fd); | 
|  | 114 |  | 
| Mathias Agopian | 2ffb247 | 2010-09-16 21:41:13 -0700 | [diff] [blame] | 115 | return  (result == fd) ? status_t(NO_ERROR) : result; | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | status_t SensorEventQueue::wake() const | 
|  | 119 | { | 
| Jeff Brown | 59abe7e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 120 | sp<Looper> looper(getLooper()); | 
|  | 121 | looper->wake(); | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 122 | return NO_ERROR; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | status_t SensorEventQueue::enableSensor(Sensor const* sensor) const { | 
| Peng Xu | 2f3bf13 | 2016-03-16 18:05:35 -0700 | [diff] [blame] | 126 | return enableSensor(sensor, SENSOR_DELAY_NORMAL); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | status_t SensorEventQueue::enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const { | 
|  | 130 | return mSensorEventConnection->enableDisable(sensor->getHandle(), true, | 
|  | 131 | us2ns(samplingPeriodUs), 0, 0); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 134 | status_t SensorEventQueue::disableSensor(Sensor const* sensor) const { | 
| Peng Xu | 2f3bf13 | 2016-03-16 18:05:35 -0700 | [diff] [blame] | 135 | return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, 0); | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 138 | status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs, | 
| Peng Xu | da8385c | 2017-02-28 20:19:47 -0800 | [diff] [blame] | 139 | int64_t maxBatchReportLatencyUs, int reservedFlags) const { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 140 | return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs), | 
|  | 141 | us2ns(maxBatchReportLatencyUs), reservedFlags); | 
|  | 142 | } | 
|  | 143 |  | 
| Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 144 | status_t SensorEventQueue::flush() const { | 
|  | 145 | return mSensorEventConnection->flush(); | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
|  | 148 | status_t SensorEventQueue::disableSensor(int32_t handle) const { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 149 | return mSensorEventConnection->enableDisable(handle, false, 0, 0, false); | 
| Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const { | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 153 | return mSensorEventConnection->setEventRate(sensor->getHandle(), ns); | 
|  | 154 | } | 
|  | 155 |  | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 156 | status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) { | 
| Aravind Akella | 5c53805 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 157 | do { | 
|  | 158 | // Blocking call. | 
|  | 159 | ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL); | 
|  | 160 | if (size >= 0) { | 
|  | 161 | return NO_ERROR; | 
|  | 162 | } else if (size < 0 && errno == EAGAIN) { | 
|  | 163 | // If send is returning a "Try again" error, sleep for 100ms and try again. In all | 
|  | 164 | // other cases log a failure and exit. | 
|  | 165 | usleep(100000); | 
|  | 166 | } else { | 
|  | 167 | ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size); | 
|  | 168 | return INVALID_OPERATION; | 
|  | 169 | } | 
|  | 170 | } while (true); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 173 | void SensorEventQueue::sendAck(const ASensorEvent* events, int count) { | 
|  | 174 | for (int i = 0; i < count; ++i) { | 
|  | 175 | if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) { | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 176 | ++mNumAcksToSend; | 
|  | 177 | } | 
|  | 178 | } | 
|  | 179 | // Send mNumAcksToSend to acknowledge for the wake up sensor events received. | 
|  | 180 | if (mNumAcksToSend > 0) { | 
|  | 181 | ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend), | 
|  | 182 | MSG_DONTWAIT | MSG_NOSIGNAL); | 
|  | 183 | if (size < 0) { | 
| Dan Stoza | f10c46e | 2014-11-11 10:32:31 -0800 | [diff] [blame] | 184 | ALOGE("sendAck failure %zd %d", size, mNumAcksToSend); | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 185 | } else { | 
|  | 186 | mNumAcksToSend = 0; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 187 | } | 
|  | 188 | } | 
|  | 189 | return; | 
|  | 190 | } | 
|  | 191 |  | 
| Brian Stack | e318d6b | 2019-01-16 15:43:37 -0800 | [diff] [blame] | 192 | ssize_t SensorEventQueue::filterEvents(ASensorEvent* events, size_t count) const { | 
|  | 193 | // Check if this Sensor Event Queue is registered to receive each type of event. If it is not, | 
|  | 194 | // then do not copy the event into the final buffer. Minimize the number of copy operations by | 
|  | 195 | // finding consecutive sequences of events that the Sensor Event Queue should receive and only | 
|  | 196 | // copying the events once an unregistered event type is reached. | 
|  | 197 | bool intervalStartLocSet = false; | 
|  | 198 | size_t intervalStartLoc = 0; | 
|  | 199 | size_t eventsInInterval = 0; | 
|  | 200 | ssize_t eventsCopied = 0; | 
|  | 201 |  | 
|  | 202 | for (size_t i = 0; i < count; i++) { | 
|  | 203 | bool includeEvent = | 
|  | 204 | (events[i].type != SENSOR_TYPE_ADDITIONAL_INFO || requestAdditionalInfo); | 
|  | 205 |  | 
|  | 206 | if (includeEvent) { | 
|  | 207 | // Do not copy events yet since there may be more consecutive events that should be | 
|  | 208 | // copied together. Track the start location and number of events in the current | 
|  | 209 | // sequence. | 
|  | 210 | if (!intervalStartLocSet) { | 
|  | 211 | intervalStartLoc = i; | 
|  | 212 | intervalStartLocSet = true; | 
|  | 213 | eventsInInterval = 0; | 
|  | 214 | } | 
|  | 215 | eventsInInterval++; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | // Shift the events from the already processed interval once an event that should not be | 
|  | 219 | // included is reached or if this is the final event to be processed. | 
|  | 220 | if (!includeEvent || (i + 1 == count)) { | 
|  | 221 | // Only shift the events if the interval did not start with the first event. If the | 
|  | 222 | // interval started with the first event, the events are already in their correct | 
|  | 223 | // location. | 
|  | 224 | if (intervalStartLoc > 0) { | 
|  | 225 | memmove(&events[eventsCopied], &events[intervalStartLoc], | 
|  | 226 | eventsInInterval * sizeof(ASensorEvent)); | 
|  | 227 | } | 
|  | 228 | eventsCopied += eventsInInterval; | 
|  | 229 |  | 
|  | 230 | // Reset the interval information | 
|  | 231 | eventsInInterval = 0; | 
|  | 232 | intervalStartLocSet = false; | 
|  | 233 | } | 
|  | 234 | } | 
|  | 235 | return eventsCopied; | 
|  | 236 | } | 
|  | 237 |  | 
| Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 238 | // ---------------------------------------------------------------------------- | 
|  | 239 | }; // namespace android | 
|  | 240 |  |