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