blob: bec9255fd7aafe05a11ab6d3fc69544a81e44c4d [file] [log] [blame]
Mathias Agopian589ce852010-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 Agopiana7352c92010-07-14 23:41:37 -070016
17#define LOG_TAG "Sensors"
Rocky Fang71df3972024-06-04 17:49:33 +000018#define ATRACE_TAG ATRACE_TAG_SYSTEM_SERVER
Mathias Agopian589ce852010-07-13 22:21:56 -070019
20#include <android/sensor.h>
Rocky Fang71df3972024-06-04 17:49:33 +000021#include <com_android_hardware_libsensor_flags.h>
22#include <cutils/trace.h>
Brian Stacke318d6b2019-01-16 15:43:37 -080023#include <hardware/sensors-base.h>
Rocky Fang71df3972024-06-04 17:49:33 +000024#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 Agopian589ce852010-07-13 22:21:56 -070036
Dan Stozad723bd72014-11-18 10:24:03 -080037using std::min;
Rocky Fang71df3972024-06-04 17:49:33 +000038namespace libsensor_flags = com::android::hardware::libsensor::flags;
Dan Stozad723bd72014-11-18 10:24:03 -080039
Mathias Agopian589ce852010-07-13 22:21:56 -070040// ----------------------------------------------------------------------------
41namespace android {
42// ----------------------------------------------------------------------------
43
Rocky Fang71df3972024-06-04 17:49:33 +000044SensorEventQueue::SensorEventQueue(const sp<ISensorEventConnection>& connection,
Rocky Fangc4c5c892024-08-07 23:28:55 +000045 SensorManager& sensorManager, String8 packageName)
Rocky Fang71df3972024-06-04 17:49:33 +000046 : mSensorEventConnection(connection),
47 mRecBuffer(nullptr),
48 mSensorManager(sensorManager),
Rocky Fangc4c5c892024-08-07 23:28:55 +000049 mPackageName(packageName),
Rocky Fang71df3972024-06-04 17:49:33 +000050 mAvailable(0),
51 mConsumed(0),
52 mNumAcksToSend(0) {
Mathias Agopian90ed3e82013-09-09 23:36:25 -070053 mRecBuffer = new ASensorEvent[MAX_RECEIVE_BUFFER_EVENT_COUNT];
Mathias Agopian589ce852010-07-13 22:21:56 -070054}
55
Mathias Agopian90ed3e82013-09-09 23:36:25 -070056SensorEventQueue::~SensorEventQueue() {
57 delete [] mRecBuffer;
Mathias Agopian589ce852010-07-13 22:21:56 -070058}
59
60void SensorEventQueue::onFirstRef()
61{
62 mSensorChannel = mSensorEventConnection->getSensorChannel();
63}
64
65int SensorEventQueue::getFd() const
66{
67 return mSensorChannel->getFd();
68}
69
Mathias Agopian7b5be952012-04-02 17:02:19 -070070
71ssize_t SensorEventQueue::write(const sp<BitTube>& tube,
72 ASensorEvent const* events, size_t numEvents) {
73 return BitTube::sendObjects(tube, events, numEvents);
Mathias Agopian589ce852010-07-13 22:21:56 -070074}
75
Mathias Agopian90ed3e82013-09-09 23:36:25 -070076ssize_t SensorEventQueue::read(ASensorEvent* events, size_t numEvents) {
77 if (mAvailable == 0) {
Rocky Fang71df3972024-06-04 17:49:33 +000078 ssize_t err =
79 BitTube::recvObjects(mSensorChannel, mRecBuffer, MAX_RECEIVE_BUFFER_EVENT_COUNT);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070080 if (err < 0) {
81 return err;
82 }
Dan Stozad723bd72014-11-18 10:24:03 -080083 mAvailable = static_cast<size_t>(err);
Mathias Agopian90ed3e82013-09-09 23:36:25 -070084 mConsumed = 0;
85 }
Dan Stozad723bd72014-11-18 10:24:03 -080086 size_t count = min(numEvents, mAvailable);
87 memcpy(events, mRecBuffer + mConsumed, count * sizeof(ASensorEvent));
Rocky Fang71df3972024-06-04 17:49:33 +000088
89 if (CC_UNLIKELY(ATRACE_ENABLED()) &&
90 libsensor_flags::sensor_event_queue_report_sensor_usage_in_tracing()) {
91 for (size_t i = 0; i < count; i++) {
92 std::optional<std::string_view> sensorName =
93 mSensorManager.getSensorNameByHandle(events->sensor);
94 if (sensorName.has_value()) {
95 char buffer[UINT8_MAX];
Rocky Fangc4c5c892024-08-07 23:28:55 +000096 IPCThreadState* thread = IPCThreadState::self();
97 pid_t pid = (thread != nullptr) ? thread->getCallingPid() : -1;
98 std::snprintf(buffer, sizeof(buffer),
99 "Sensor event from %s to %s PID: %d (%zu/%zu)",
100 sensorName.value().data(), mPackageName.c_str(), pid, i, count);
Rocky Fang71df3972024-06-04 17:49:33 +0000101 ATRACE_INSTANT_FOR_TRACK(LOG_TAG, buffer);
102 }
103 }
104 }
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700105 mAvailable -= count;
106 mConsumed += count;
Dan Stozad723bd72014-11-18 10:24:03 -0800107 return static_cast<ssize_t>(count);
Mathias Agopian589ce852010-07-13 22:21:56 -0700108}
109
Jeff Brown59abe7e2010-09-13 23:17:30 -0700110sp<Looper> SensorEventQueue::getLooper() const
Mathias Agopian589ce852010-07-13 22:21:56 -0700111{
Mathias Agopiana7352c92010-07-14 23:41:37 -0700112 Mutex::Autolock _l(mLock);
Yi Kongd5e079f2018-07-17 16:08:27 -0700113 if (mLooper == nullptr) {
Jeff Brown59abe7e2010-09-13 23:17:30 -0700114 mLooper = new Looper(true);
Yi Kongd5e079f2018-07-17 16:08:27 -0700115 mLooper->addFd(getFd(), getFd(), ALOOPER_EVENT_INPUT, nullptr, nullptr);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700116 }
Jeff Brown59abe7e2010-09-13 23:17:30 -0700117 return mLooper;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700118}
119
120status_t SensorEventQueue::waitForEvent() const
121{
122 const int fd = getFd();
Jeff Brown59abe7e2010-09-13 23:17:30 -0700123 sp<Looper> looper(getLooper());
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700124
Mathias Agopian29267fe2012-05-07 15:20:15 -0700125 int events;
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700126 int32_t result;
127 do {
Yi Kongd5e079f2018-07-17 16:08:27 -0700128 result = looper->pollOnce(-1, nullptr, &events, nullptr);
Mathias Agopian29267fe2012-05-07 15:20:15 -0700129 if (result == ALOOPER_POLL_ERROR) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000130 ALOGE("SensorEventQueue::waitForEvent error (errno=%d)", errno);
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700131 result = -EPIPE; // unknown error, so we make up one
132 break;
133 }
Mathias Agopian29267fe2012-05-07 15:20:15 -0700134 if (events & ALOOPER_EVENT_HANGUP) {
135 // the other-side has died
136 ALOGE("SensorEventQueue::waitForEvent error HANGUP");
137 result = -EPIPE; // unknown error, so we make up one
138 break;
139 }
Mathias Agopianaeda9af2010-09-16 17:04:16 -0700140 } while (result != fd);
141
Mathias Agopian2ffb2472010-09-16 21:41:13 -0700142 return (result == fd) ? status_t(NO_ERROR) : result;
Mathias Agopiana7352c92010-07-14 23:41:37 -0700143}
144
145status_t SensorEventQueue::wake() const
146{
Jeff Brown59abe7e2010-09-13 23:17:30 -0700147 sp<Looper> looper(getLooper());
148 looper->wake();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700149 return NO_ERROR;
150}
151
152status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
Peng Xu2f3bf132016-03-16 18:05:35 -0700153 return enableSensor(sensor, SENSOR_DELAY_NORMAL);
154}
155
156status_t SensorEventQueue::enableSensor(Sensor const* sensor, int32_t samplingPeriodUs) const {
157 return mSensorEventConnection->enableDisable(sensor->getHandle(), true,
158 us2ns(samplingPeriodUs), 0, 0);
Mathias Agopian589ce852010-07-13 22:21:56 -0700159}
160
Mathias Agopiana7352c92010-07-14 23:41:37 -0700161status_t SensorEventQueue::disableSensor(Sensor const* sensor) const {
Peng Xu2f3bf132016-03-16 18:05:35 -0700162 return mSensorEventConnection->enableDisable(sensor->getHandle(), false, 0, 0, 0);
Mathias Agopian589ce852010-07-13 22:21:56 -0700163}
164
Aravind Akella724d91d2013-06-27 12:04:23 -0700165status_t SensorEventQueue::enableSensor(int32_t handle, int32_t samplingPeriodUs,
Peng Xuda8385c2017-02-28 20:19:47 -0800166 int64_t maxBatchReportLatencyUs, int reservedFlags) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700167 return mSensorEventConnection->enableDisable(handle, true, us2ns(samplingPeriodUs),
168 us2ns(maxBatchReportLatencyUs), reservedFlags);
169}
170
Aravind Akella701166d2013-10-08 14:59:26 -0700171status_t SensorEventQueue::flush() const {
172 return mSensorEventConnection->flush();
Mathias Agopiana7352c92010-07-14 23:41:37 -0700173}
174
175status_t SensorEventQueue::disableSensor(int32_t handle) const {
Aravind Akella724d91d2013-06-27 12:04:23 -0700176 return mSensorEventConnection->enableDisable(handle, false, 0, 0, false);
Mathias Agopiana7352c92010-07-14 23:41:37 -0700177}
178
179status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const {
Mathias Agopian589ce852010-07-13 22:21:56 -0700180 return mSensorEventConnection->setEventRate(sensor->getHandle(), ns);
181}
182
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700183status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) {
Aravind Akella5c538052015-06-29 12:37:48 -0700184 do {
185 // Blocking call.
186 ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL);
187 if (size >= 0) {
188 return NO_ERROR;
189 } else if (size < 0 && errno == EAGAIN) {
190 // If send is returning a "Try again" error, sleep for 100ms and try again. In all
191 // other cases log a failure and exit.
192 usleep(100000);
193 } else {
194 ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size);
195 return INVALID_OPERATION;
196 }
197 } while (true);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700198}
199
Aravind Akella9a844cf2014-02-11 18:58:52 -0800200void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {
201 for (int i = 0; i < count; ++i) {
202 if (events[i].flags & WAKE_UP_SENSOR_EVENT_NEEDS_ACK) {
Aravind Akella8a969552014-09-28 17:52:41 -0700203 ++mNumAcksToSend;
204 }
205 }
206 // Send mNumAcksToSend to acknowledge for the wake up sensor events received.
207 if (mNumAcksToSend > 0) {
208 ssize_t size = ::send(mSensorChannel->getFd(), &mNumAcksToSend, sizeof(mNumAcksToSend),
209 MSG_DONTWAIT | MSG_NOSIGNAL);
210 if (size < 0) {
Dan Stozaf10c46e2014-11-11 10:32:31 -0800211 ALOGE("sendAck failure %zd %d", size, mNumAcksToSend);
Aravind Akella8a969552014-09-28 17:52:41 -0700212 } else {
213 mNumAcksToSend = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800214 }
215 }
216 return;
217}
218
Brian Stacke318d6b2019-01-16 15:43:37 -0800219ssize_t SensorEventQueue::filterEvents(ASensorEvent* events, size_t count) const {
220 // Check if this Sensor Event Queue is registered to receive each type of event. If it is not,
221 // then do not copy the event into the final buffer. Minimize the number of copy operations by
222 // finding consecutive sequences of events that the Sensor Event Queue should receive and only
223 // copying the events once an unregistered event type is reached.
224 bool intervalStartLocSet = false;
225 size_t intervalStartLoc = 0;
226 size_t eventsInInterval = 0;
227 ssize_t eventsCopied = 0;
228
229 for (size_t i = 0; i < count; i++) {
230 bool includeEvent =
231 (events[i].type != SENSOR_TYPE_ADDITIONAL_INFO || requestAdditionalInfo);
232
233 if (includeEvent) {
234 // Do not copy events yet since there may be more consecutive events that should be
235 // copied together. Track the start location and number of events in the current
236 // sequence.
237 if (!intervalStartLocSet) {
238 intervalStartLoc = i;
239 intervalStartLocSet = true;
240 eventsInInterval = 0;
241 }
242 eventsInInterval++;
243 }
244
245 // Shift the events from the already processed interval once an event that should not be
246 // included is reached or if this is the final event to be processed.
247 if (!includeEvent || (i + 1 == count)) {
248 // Only shift the events if the interval did not start with the first event. If the
249 // interval started with the first event, the events are already in their correct
250 // location.
251 if (intervalStartLoc > 0) {
252 memmove(&events[eventsCopied], &events[intervalStartLoc],
253 eventsInInterval * sizeof(ASensorEvent));
254 }
255 eventsCopied += eventsInInterval;
256
257 // Reset the interval information
258 eventsInInterval = 0;
259 intervalStartLocSet = false;
260 }
261 }
262 return eventsCopied;
263}
264
Mathias Agopian589ce852010-07-13 22:21:56 -0700265// ----------------------------------------------------------------------------
266}; // namespace android
267