blob: 90e33a9134e7dbfab15d0ce11c5e846d675359a6 [file] [log] [blame]
Peng Xueb4d6282015-12-10 18:02:41 -08001/*
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
Arthur Ishiguro3e9afc12020-09-22 13:05:15 -070017#include <log/log.h>
Peng Xueb4d6282015-12-10 18:02:41 -080018#include <sys/socket.h>
19#include <utils/threads.h>
20
Mike Ma24743862020-01-29 00:36:55 -080021#include <android/util/ProtoOutputStream.h>
22#include <frameworks/base/core/proto/android/service/sensor_service.proto.h>
Mathias Agopian801ea092017-03-06 15:05:04 -080023#include <sensor/SensorEventQueue.h>
Peng Xueb4d6282015-12-10 18:02:41 -080024
25#include "vec.h"
26#include "SensorEventConnection.h"
Peng Xu755c4512016-04-07 23:15:14 -070027#include "SensorDevice.h"
Peng Xueb4d6282015-12-10 18:02:41 -080028
Peng Xue36e3472016-11-03 11:57:10 -070029#define UNUSED(x) (void)(x)
30
Peng Xueb4d6282015-12-10 18:02:41 -080031namespace android {
Anthony Stange07eb4212020-08-28 14:50:28 -040032namespace {
33
34// Used as the default value for the target SDK until it's obtained via getTargetSdkVersion.
35constexpr int kTargetSdkUnknown = 0;
36
37} // namespace
Peng Xueb4d6282015-12-10 18:02:41 -080038
39SensorService::SensorEventConnection::SensorEventConnection(
40 const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode,
Arthur Ishiguro539c27c2020-04-13 09:47:59 -070041 const String16& opPackageName)
Peng Xueb4d6282015-12-10 18:02:41 -080042 : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
Yi Kong8f313e32018-07-17 14:13:29 -070043 mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(nullptr),
Brian Stackae4053f2018-12-10 14:54:18 -080044 mCacheSize(0), mMaxCacheSize(0), mTimeOfLastEventDrop(0), mEventsDropped(0),
Anthony Stange07eb4212020-08-28 14:50:28 -040045 mPackageName(packageName), mOpPackageName(opPackageName), mTargetSdk(kTargetSdkUnknown),
46 mDestroyed(false) {
Anh Phamaf91a912021-02-10 14:10:53 +010047 mIsRateCappedBasedOnPermission = mService->isRateCappedBasedOnPermission(mOpPackageName);
Peng Xueb4d6282015-12-10 18:02:41 -080048 mChannel = new BitTube(mService->mSocketBufferSize);
49#if DEBUG_CONNECTIONS
50 mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
51 mTotalAcksNeeded = mTotalAcksReceived = 0;
52#endif
53}
54
55SensorService::SensorEventConnection::~SensorEventConnection() {
56 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Peng Xu8cbefd72017-07-10 16:41:08 -070057 destroy();
Peng Xueb4d6282015-12-10 18:02:41 -080058 mService->cleanupConnection(this);
Yi Kong8f313e32018-07-17 14:13:29 -070059 if (mEventCache != nullptr) {
George Burgess IV1866ed42018-01-21 12:14:09 -080060 delete[] mEventCache;
Peng Xueb4d6282015-12-10 18:02:41 -080061 }
Arthur Ishiguro3e9afc12020-09-22 13:05:15 -070062}
63
64void SensorService::SensorEventConnection::destroy() {
Peng Xu8cbefd72017-07-10 16:41:08 -070065 mDestroyed = true;
Peng Xueb4d6282015-12-10 18:02:41 -080066}
67
68void SensorService::SensorEventConnection::onFirstRef() {
69 LooperCallback::onFirstRef();
70}
71
72bool SensorService::SensorEventConnection::needsWakeLock() {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -070073 Mutex::Autolock _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -080074 return !mDead && mWakeLockRefCount > 0;
75}
76
77void SensorService::SensorEventConnection::resetWakeLockRefCount() {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -070078 Mutex::Autolock _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -080079 mWakeLockRefCount = 0;
80}
81
82void SensorService::SensorEventConnection::dump(String8& result) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -070083 Mutex::Autolock _l(mConnectionLock);
Brian Stackbce04d72019-03-21 10:54:10 -070084 result.appendFormat("\tOperating Mode: ");
85 if (!mService->isWhiteListedPackage(getPackageName())) {
86 result.append("RESTRICTED\n");
87 } else if (mDataInjectionMode) {
88 result.append("DATA_INJECTION\n");
89 } else {
90 result.append("NORMAL\n");
91 }
Peng Xueb4d6282015-12-10 18:02:41 -080092 result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
93 "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
94 mMaxCacheSize);
Arthur Ishiguroad46c782020-03-26 11:24:43 -070095 for (auto& it : mSensorInfo) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -070096 const FlushInfo& flushInfo = it.second;
Peng Xueb4d6282015-12-10 18:02:41 -080097 result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
Arthur Ishiguroad46c782020-03-26 11:24:43 -070098 mService->getSensorName(it.first).string(),
99 it.first,
Peng Xueb4d6282015-12-10 18:02:41 -0800100 flushInfo.mFirstFlushPending ? "First flush pending" :
101 "active",
102 flushInfo.mPendingFlushEventsToSend);
103 }
104#if DEBUG_CONNECTIONS
105 result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
106 " total_acks_needed %d | total_acks_recvd %d\n",
107 mEventsReceived,
108 mEventsSent,
109 mEventsSentFromCache,
110 mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
111 mTotalAcksNeeded,
112 mTotalAcksReceived);
113#endif
114}
115
Mike Ma24743862020-01-29 00:36:55 -0800116/**
117 * Dump debugging information as android.service.SensorEventConnectionProto protobuf message using
118 * ProtoOutputStream.
119 *
120 * See proto definition and some notes about ProtoOutputStream in
121 * frameworks/base/core/proto/android/service/sensor_service.proto
122 */
123void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto) const {
124 using namespace service::SensorEventConnectionProto;
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700125 Mutex::Autolock _l(mConnectionLock);
Mike Ma24743862020-01-29 00:36:55 -0800126
127 if (!mService->isWhiteListedPackage(getPackageName())) {
128 proto->write(OPERATING_MODE, OP_MODE_RESTRICTED);
129 } else if (mDataInjectionMode) {
130 proto->write(OPERATING_MODE, OP_MODE_DATA_INJECTION);
131 } else {
132 proto->write(OPERATING_MODE, OP_MODE_NORMAL);
133 }
134 proto->write(PACKAGE_NAME, std::string(mPackageName.string()));
135 proto->write(WAKE_LOCK_REF_COUNT, int32_t(mWakeLockRefCount));
136 proto->write(UID, int32_t(mUid));
137 proto->write(CACHE_SIZE, int32_t(mCacheSize));
138 proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700139 for (auto& it : mSensorInfo) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700140 const FlushInfo& flushInfo = it.second;
Mike Ma24743862020-01-29 00:36:55 -0800141 const uint64_t token = proto->start(FLUSH_INFOS);
142 proto->write(FlushInfoProto::SENSOR_NAME,
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700143 std::string(mService->getSensorName(it.first)));
144 proto->write(FlushInfoProto::SENSOR_HANDLE, it.first);
Mike Ma24743862020-01-29 00:36:55 -0800145 proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending);
146 proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND,
147 flushInfo.mPendingFlushEventsToSend);
148 proto->end(token);
149 }
150#if DEBUG_CONNECTIONS
151 proto->write(EVENTS_RECEIVED, mEventsReceived);
152 proto->write(EVENTS_SENT, mEventsSent);
153 proto->write(EVENTS_CACHE, mEventsSentFromCache);
154 proto->write(EVENTS_DROPPED, mEventsReceived - (mEventsSentFromCache + mEventsSent +
155 mCacheSize));
156 proto->write(TOTAL_ACKS_NEEDED, mTotalAcksNeeded);
157 proto->write(TOTAL_ACKS_RECEIVED, mTotalAcksReceived);
158#endif
159}
160
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700161bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
162 Mutex::Autolock _l(mConnectionLock);
Peng Xu755c4512016-04-07 23:15:14 -0700163 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
164 if (si == nullptr ||
Arthur Ishiguro883748c2020-10-28 13:18:02 -0700165 !canAccessSensor(si->getSensor(), "Add to SensorEventConnection: ", mOpPackageName) ||
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700166 mSensorInfo.count(handle) > 0) {
Peng Xueb4d6282015-12-10 18:02:41 -0800167 return false;
168 }
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700169 mSensorInfo[handle] = FlushInfo();
Peng Xu755c4512016-04-07 23:15:14 -0700170 return true;
Peng Xueb4d6282015-12-10 18:02:41 -0800171}
172
173bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700174 Mutex::Autolock _l(mConnectionLock);
175 if (mSensorInfo.erase(handle) >= 0) {
176 return true;
177 }
178 return false;
Peng Xueb4d6282015-12-10 18:02:41 -0800179}
180
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700181std::vector<int32_t> SensorService::SensorEventConnection::getActiveSensorHandles() const {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700182 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700183 std::vector<int32_t> list;
184 for (auto& it : mSensorInfo) {
185 list.push_back(it.first);
186 }
187 return list;
188}
189
Peng Xueb4d6282015-12-10 18:02:41 -0800190bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700191 Mutex::Autolock _l(mConnectionLock);
192 return mSensorInfo.count(handle) > 0;
Peng Xueb4d6282015-12-10 18:02:41 -0800193}
194
195bool SensorService::SensorEventConnection::hasAnySensor() const {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700196 Mutex::Autolock _l(mConnectionLock);
197 return mSensorInfo.size() ? true : false;
Peng Xueb4d6282015-12-10 18:02:41 -0800198}
199
200bool SensorService::SensorEventConnection::hasOneShotSensors() const {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700201 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700202 for (auto &it : mSensorInfo) {
203 const int handle = it.first;
Peng Xu755c4512016-04-07 23:15:14 -0700204 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
205 if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
Peng Xueb4d6282015-12-10 18:02:41 -0800206 return true;
207 }
208 }
209 return false;
210}
211
212String8 SensorService::SensorEventConnection::getPackageName() const {
213 return mPackageName;
214}
215
216void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
217 bool value) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700218 Mutex::Autolock _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700219 if (mSensorInfo.count(handle) > 0) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700220 FlushInfo& flushInfo = mSensorInfo[handle];
Peng Xueb4d6282015-12-10 18:02:41 -0800221 flushInfo.mFirstFlushPending = value;
222 }
223}
224
225void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700226 Mutex::Autolock _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800227 updateLooperRegistrationLocked(looper);
228}
229
230void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
231 const sp<Looper>& looper) {
232 bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) ||
233 mDataInjectionMode;
234 // If all sensors are unregistered OR Looper has encountered an error, we can remove the Fd from
235 // the Looper if it has been previously added.
236 if (!isConnectionActive || mDead) { if (mHasLooperCallbacks) {
237 ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this,
238 mChannel->getSendFd());
239 looper->removeFd(mChannel->getSendFd()); mHasLooperCallbacks = false; }
240 return; }
241
242 int looper_flags = 0;
243 if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
244 if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700245 for (auto& it : mSensorInfo) {
246 const int handle = it.first;
Peng Xu755c4512016-04-07 23:15:14 -0700247 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
248 if (si != nullptr && si->getSensor().isWakeUpSensor()) {
Peng Xueb4d6282015-12-10 18:02:41 -0800249 looper_flags |= ALOOPER_EVENT_INPUT;
Peng Xueb4d6282015-12-10 18:02:41 -0800250 }
251 }
252
253 // If flags is still set to zero, we don't need to add this fd to the Looper, if the fd has
254 // already been added, remove it. This is likely to happen when ALL the events stored in the
255 // cache have been sent to the corresponding app.
256 if (looper_flags == 0) {
257 if (mHasLooperCallbacks) {
258 ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd());
259 looper->removeFd(mChannel->getSendFd());
260 mHasLooperCallbacks = false;
261 }
262 return;
263 }
264
265 // Add the file descriptor to the Looper for receiving acknowledegments if the app has
266 // registered for wake-up sensors OR for sending events in the cache.
Yi Kong8f313e32018-07-17 14:13:29 -0700267 int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, nullptr);
Peng Xueb4d6282015-12-10 18:02:41 -0800268 if (ret == 1) {
269 ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd());
270 mHasLooperCallbacks = true;
271 } else {
272 ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd());
273 }
274}
275
Stan Rokita29adc8c2020-07-06 17:38:03 -0700276bool SensorService::SensorEventConnection::incrementPendingFlushCountIfHasAccess(int32_t handle) {
277 if (hasSensorAccess()) {
278 Mutex::Autolock _l(mConnectionLock);
279 if (mSensorInfo.count(handle) > 0) {
280 FlushInfo& flushInfo = mSensorInfo[handle];
281 flushInfo.mPendingFlushEventsToSend++;
282 }
283 return true;
284 } else {
285 return false;
Peng Xueb4d6282015-12-10 18:02:41 -0800286 }
287}
288
289status_t SensorService::SensorEventConnection::sendEvents(
290 sensors_event_t const* buffer, size_t numEvents,
291 sensors_event_t* scratch,
Peng Xuded526e2016-08-12 16:39:44 -0700292 wp<const SensorEventConnection> const * mapFlushEventsToConnections) {
Peng Xueb4d6282015-12-10 18:02:41 -0800293 // filter out events not for this connection
Svet Ganove752a5c2018-01-15 17:14:20 -0800294
George Burgess IV1866ed42018-01-21 12:14:09 -0800295 std::unique_ptr<sensors_event_t[]> sanitizedBuffer;
Svet Ganove752a5c2018-01-15 17:14:20 -0800296
Peng Xueb4d6282015-12-10 18:02:41 -0800297 int count = 0;
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700298 Mutex::Autolock _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800299 if (scratch) {
300 size_t i=0;
301 while (i<numEvents) {
302 int32_t sensor_handle = buffer[i].sensor;
303 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
304 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
305 buffer[i].meta_data.sensor);
306 // Setting sensor_handle to the correct sensor to ensure the sensor events per
307 // connection are filtered correctly. buffer[i].sensor is zero for meta_data
308 // events.
309 sensor_handle = buffer[i].meta_data.sensor;
310 }
311
Peng Xueb4d6282015-12-10 18:02:41 -0800312 // Check if this connection has registered for this sensor. If not continue to the
313 // next sensor_event.
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700314 if (mSensorInfo.count(sensor_handle) == 0) {
Peng Xueb4d6282015-12-10 18:02:41 -0800315 ++i;
316 continue;
317 }
318
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700319 FlushInfo& flushInfo = mSensorInfo[sensor_handle];
Peng Xueb4d6282015-12-10 18:02:41 -0800320 // Check if there is a pending flush_complete event for this sensor on this connection.
321 if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
Peng Xuded526e2016-08-12 16:39:44 -0700322 mapFlushEventsToConnections[i] == this) {
Peng Xueb4d6282015-12-10 18:02:41 -0800323 flushInfo.mFirstFlushPending = false;
324 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
325 buffer[i].meta_data.sensor);
326 ++i;
327 continue;
328 }
329
330 // If there is a pending flush complete event for this sensor on this connection,
331 // ignore the event and proceed to the next.
332 if (flushInfo.mFirstFlushPending) {
333 ++i;
334 continue;
335 }
336
337 do {
338 // Keep copying events into the scratch buffer as long as they are regular
339 // sensor_events are from the same sensor_handle OR they are flush_complete_events
340 // from the same sensor_handle AND the current connection is mapped to the
341 // corresponding flush_complete_event.
342 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
Peng Xuded526e2016-08-12 16:39:44 -0700343 if (mapFlushEventsToConnections[i] == this) {
Peng Xueb4d6282015-12-10 18:02:41 -0800344 scratch[count++] = buffer[i];
345 }
Peng Xueb4d6282015-12-10 18:02:41 -0800346 } else {
Brian Stackc225aa12019-04-19 09:30:25 -0700347 // Regular sensor event, just copy it to the scratch buffer after checking
348 // the AppOp.
349 if (hasSensorAccess() && noteOpIfRequired(buffer[i])) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800350 scratch[count++] = buffer[i];
351 }
Peng Xueb4d6282015-12-10 18:02:41 -0800352 }
Svet Ganove752a5c2018-01-15 17:14:20 -0800353 i++;
Peng Xueb4d6282015-12-10 18:02:41 -0800354 } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle &&
355 buffer[i].type != SENSOR_TYPE_META_DATA) ||
356 (buffer[i].type == SENSOR_TYPE_META_DATA &&
357 buffer[i].meta_data.sensor == sensor_handle)));
358 }
359 } else {
Michael Groover5e1f60b2018-12-04 22:34:29 -0800360 if (hasSensorAccess()) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800361 scratch = const_cast<sensors_event_t *>(buffer);
362 count = numEvents;
363 } else {
George Burgess IV1866ed42018-01-21 12:14:09 -0800364 sanitizedBuffer.reset(new sensors_event_t[numEvents]);
365 scratch = sanitizedBuffer.get();
Svet Ganove752a5c2018-01-15 17:14:20 -0800366 for (size_t i = 0; i < numEvents; i++) {
367 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
368 scratch[count++] = buffer[i++];
369 }
370 }
371 }
Peng Xueb4d6282015-12-10 18:02:41 -0800372 }
373
374 sendPendingFlushEventsLocked();
375 // Early return if there are no events for this connection.
376 if (count == 0) {
377 return status_t(NO_ERROR);
378 }
379
380#if DEBUG_CONNECTIONS
381 mEventsReceived += count;
382#endif
383 if (mCacheSize != 0) {
384 // There are some events in the cache which need to be sent first. Copy this buffer to
385 // the end of cache.
Brian Stack93432ad2018-11-27 18:28:48 -0800386 appendEventsToCacheLocked(scratch, count);
Peng Xueb4d6282015-12-10 18:02:41 -0800387 return status_t(NO_ERROR);
388 }
389
Svet Ganove752a5c2018-01-15 17:14:20 -0800390 int index_wake_up_event = -1;
Michael Groover5e1f60b2018-12-04 22:34:29 -0800391 if (hasSensorAccess()) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800392 index_wake_up_event = findWakeUpSensorEventLocked(scratch, count);
393 if (index_wake_up_event >= 0) {
394 scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
395 ++mWakeLockRefCount;
Peng Xueb4d6282015-12-10 18:02:41 -0800396#if DEBUG_CONNECTIONS
Svet Ganove752a5c2018-01-15 17:14:20 -0800397 ++mTotalAcksNeeded;
Peng Xueb4d6282015-12-10 18:02:41 -0800398#endif
Svet Ganove752a5c2018-01-15 17:14:20 -0800399 }
Peng Xueb4d6282015-12-10 18:02:41 -0800400 }
401
402 // NOTE: ASensorEvent and sensors_event_t are the same type.
403 ssize_t size = SensorEventQueue::write(mChannel,
404 reinterpret_cast<ASensorEvent const*>(scratch), count);
405 if (size < 0) {
406 // Write error, copy events to local cache.
407 if (index_wake_up_event >= 0) {
408 // If there was a wake_up sensor_event, reset the flag.
409 scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
410 if (mWakeLockRefCount > 0) {
411 --mWakeLockRefCount;
412 }
413#if DEBUG_CONNECTIONS
414 --mTotalAcksNeeded;
415#endif
416 }
Yi Kong8f313e32018-07-17 14:13:29 -0700417 if (mEventCache == nullptr) {
Peng Xueb4d6282015-12-10 18:02:41 -0800418 mMaxCacheSize = computeMaxCacheSizeLocked();
419 mEventCache = new sensors_event_t[mMaxCacheSize];
420 mCacheSize = 0;
421 }
Brian Stack93432ad2018-11-27 18:28:48 -0800422 // Save the events so that they can be written later
423 appendEventsToCacheLocked(scratch, count);
Peng Xueb4d6282015-12-10 18:02:41 -0800424
425 // Add this file descriptor to the looper to get a callback when this fd is available for
426 // writing.
427 updateLooperRegistrationLocked(mService->getLooper());
428 return size;
429 }
430
431#if DEBUG_CONNECTIONS
432 if (size > 0) {
433 mEventsSent += count;
434 }
435#endif
436
437 return size < 0 ? status_t(size) : status_t(NO_ERROR);
438}
439
Michael Groover5e1f60b2018-12-04 22:34:29 -0800440bool SensorService::SensorEventConnection::hasSensorAccess() {
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700441 return mService->isUidActive(mUid)
442 && !mService->mSensorPrivacyPolicy->isSensorPrivacyEnabled();
Michael Groover5e1f60b2018-12-04 22:34:29 -0800443}
444
Brian Stackc225aa12019-04-19 09:30:25 -0700445bool SensorService::SensorEventConnection::noteOpIfRequired(const sensors_event_t& event) {
446 bool success = true;
447 const auto iter = mHandleToAppOp.find(event.sensor);
448 if (iter != mHandleToAppOp.end()) {
Anthony Stange07eb4212020-08-28 14:50:28 -0400449 if (mTargetSdk == kTargetSdkUnknown) {
450 // getTargetSdkVersion returns -1 if it fails so this operation should only be run once
451 // per connection and then cached. Perform this here as opposed to in the constructor to
452 // avoid log spam for NDK/VNDK clients that don't use sensors guarded with permissions
453 // and pass in invalid op package names.
454 mTargetSdk = SensorService::getTargetSdkVersion(mOpPackageName);
455 }
456
Brian Duddie457e6392020-06-19 11:38:29 -0700457 // Special handling for step count/detect backwards compatibility: if the app's target SDK
458 // is pre-Q, still permit delivering events to the app even if permission isn't granted
459 // (since this permission was only introduced in Q)
460 if ((event.type == SENSOR_TYPE_STEP_COUNTER || event.type == SENSOR_TYPE_STEP_DETECTOR) &&
461 mTargetSdk > 0 && mTargetSdk <= __ANDROID_API_P__) {
462 success = true;
463 } else {
Arthur Ishiguro883748c2020-10-28 13:18:02 -0700464 int32_t sensorHandle = event.sensor;
465 String16 noteMsg("Sensor event (");
466 noteMsg.append(String16(mService->getSensorStringType(sensorHandle)));
467 noteMsg.append(String16(")"));
Brian Duddie457e6392020-06-19 11:38:29 -0700468 int32_t appOpMode = mService->sAppOpsManager.noteOp(iter->second, mUid,
Arthur Ishiguro883748c2020-10-28 13:18:02 -0700469 mOpPackageName, {}, noteMsg);
Brian Duddie457e6392020-06-19 11:38:29 -0700470 success = (appOpMode == AppOpsManager::MODE_ALLOWED);
471 }
Brian Stackc225aa12019-04-19 09:30:25 -0700472 }
473 return success;
474}
475
Peng Xueb4d6282015-12-10 18:02:41 -0800476void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch,
477 int count) {
478 sensors_event_t *eventCache_new;
479 const int new_cache_size = computeMaxCacheSizeLocked();
480 // Allocate new cache, copy over events from the old cache & scratch, free up memory.
481 eventCache_new = new sensors_event_t[new_cache_size];
482 memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t));
483 memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t));
484
485 ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize,
486 new_cache_size);
487
George Burgess IV1866ed42018-01-21 12:14:09 -0800488 delete[] mEventCache;
Peng Xueb4d6282015-12-10 18:02:41 -0800489 mEventCache = eventCache_new;
490 mCacheSize += count;
491 mMaxCacheSize = new_cache_size;
492}
493
Brian Stack93432ad2018-11-27 18:28:48 -0800494void SensorService::SensorEventConnection::appendEventsToCacheLocked(sensors_event_t const* events,
495 int count) {
496 if (count <= 0) {
497 return;
498 } else if (mCacheSize + count <= mMaxCacheSize) {
499 // The events fit within the current cache: add them
500 memcpy(&mEventCache[mCacheSize], events, count * sizeof(sensors_event_t));
501 mCacheSize += count;
502 } else if (mCacheSize + count <= computeMaxCacheSizeLocked()) {
503 // The events fit within a resized cache: resize the cache and add the events
504 reAllocateCacheLocked(events, count);
505 } else {
506 // The events do not fit within the cache: drop the oldest events.
Brian Stack93432ad2018-11-27 18:28:48 -0800507 int freeSpace = mMaxCacheSize - mCacheSize;
508
509 // Drop up to the currently cached number of events to make room for new events
510 int cachedEventsToDrop = std::min(mCacheSize, count - freeSpace);
511
512 // New events need to be dropped if there are more new events than the size of the cache
513 int newEventsToDrop = std::max(0, count - mMaxCacheSize);
514
515 // Determine the number of new events to copy into the cache
516 int eventsToCopy = std::min(mMaxCacheSize, count);
517
Brian Stackae4053f2018-12-10 14:54:18 -0800518 constexpr nsecs_t kMinimumTimeBetweenDropLogNs = 2 * 1000 * 1000 * 1000; // 2 sec
519 if (events[0].timestamp - mTimeOfLastEventDrop > kMinimumTimeBetweenDropLogNs) {
520 ALOGW("Dropping %d cached events (%d/%d) to save %d/%d new events. %d events previously"
521 " dropped", cachedEventsToDrop, mCacheSize, mMaxCacheSize, eventsToCopy,
522 count, mEventsDropped);
523 mEventsDropped = 0;
524 mTimeOfLastEventDrop = events[0].timestamp;
525 } else {
526 // Record the number dropped
527 mEventsDropped += cachedEventsToDrop + newEventsToDrop;
528 }
529
Brian Stack93432ad2018-11-27 18:28:48 -0800530 // Check for any flush complete events in the events that will be dropped
531 countFlushCompleteEventsLocked(mEventCache, cachedEventsToDrop);
532 countFlushCompleteEventsLocked(events, newEventsToDrop);
533
534 // Only shift the events if they will not all be overwritten
535 if (eventsToCopy != mMaxCacheSize) {
536 memmove(mEventCache, &mEventCache[cachedEventsToDrop],
537 (mCacheSize - cachedEventsToDrop) * sizeof(sensors_event_t));
538 }
539 mCacheSize -= cachedEventsToDrop;
540
541 // Copy the events into the cache
542 memcpy(&mEventCache[mCacheSize], &events[newEventsToDrop],
543 eventsToCopy * sizeof(sensors_event_t));
544 mCacheSize += eventsToCopy;
545 }
546}
547
Peng Xueb4d6282015-12-10 18:02:41 -0800548void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
549 ASensorEvent flushCompleteEvent;
550 memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
551 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
552 // Loop through all the sensors for this connection and check if there are any pending
553 // flush complete events to be sent.
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700554 for (auto& it : mSensorInfo) {
555 const int handle = it.first;
Peng Xu755c4512016-04-07 23:15:14 -0700556 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
557 if (si == nullptr) {
558 continue;
559 }
560
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700561 FlushInfo& flushInfo = it.second;
Peng Xueb4d6282015-12-10 18:02:41 -0800562 while (flushInfo.mPendingFlushEventsToSend > 0) {
Peng Xu755c4512016-04-07 23:15:14 -0700563 flushCompleteEvent.meta_data.sensor = handle;
564 bool wakeUpSensor = si->getSensor().isWakeUpSensor();
Peng Xueb4d6282015-12-10 18:02:41 -0800565 if (wakeUpSensor) {
566 ++mWakeLockRefCount;
567 flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
568 }
569 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
570 if (size < 0) {
571 if (wakeUpSensor) --mWakeLockRefCount;
572 return;
573 }
574 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
575 flushCompleteEvent.meta_data.sensor);
576 flushInfo.mPendingFlushEventsToSend--;
577 }
578 }
579}
580
581void SensorService::SensorEventConnection::writeToSocketFromCache() {
582 // At a time write at most half the size of the receiver buffer in SensorEventQueue OR
583 // half the size of the socket buffer allocated in BitTube whichever is smaller.
584 const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
585 int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700586 Mutex::Autolock _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800587 // Send pending flush complete events (if any)
588 sendPendingFlushEventsLocked();
589 for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
590 const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
Svet Ganove752a5c2018-01-15 17:14:20 -0800591 int index_wake_up_event = -1;
Michael Groover5e1f60b2018-12-04 22:34:29 -0800592 if (hasSensorAccess()) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800593 index_wake_up_event =
594 findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
595 if (index_wake_up_event >= 0) {
596 mEventCache[index_wake_up_event + numEventsSent].flags |=
597 WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
598 ++mWakeLockRefCount;
Peng Xueb4d6282015-12-10 18:02:41 -0800599#if DEBUG_CONNECTIONS
Svet Ganove752a5c2018-01-15 17:14:20 -0800600 ++mTotalAcksNeeded;
Peng Xueb4d6282015-12-10 18:02:41 -0800601#endif
Svet Ganove752a5c2018-01-15 17:14:20 -0800602 }
Peng Xueb4d6282015-12-10 18:02:41 -0800603 }
604
605 ssize_t size = SensorEventQueue::write(mChannel,
606 reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
607 numEventsToWrite);
608 if (size < 0) {
609 if (index_wake_up_event >= 0) {
610 // If there was a wake_up sensor_event, reset the flag.
611 mEventCache[index_wake_up_event + numEventsSent].flags &=
612 ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
613 if (mWakeLockRefCount > 0) {
614 --mWakeLockRefCount;
615 }
616#if DEBUG_CONNECTIONS
617 --mTotalAcksNeeded;
618#endif
619 }
620 memmove(mEventCache, &mEventCache[numEventsSent],
621 (mCacheSize - numEventsSent) * sizeof(sensors_event_t));
622 ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ",
623 numEventsSent, mCacheSize);
624 mCacheSize -= numEventsSent;
625 return;
626 }
627 numEventsSent += numEventsToWrite;
628#if DEBUG_CONNECTIONS
629 mEventsSentFromCache += numEventsToWrite;
630#endif
631 }
632 ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize);
633 // All events from the cache have been sent. Reset cache size to zero.
634 mCacheSize = 0;
635 // There are no more events in the cache. We don't need to poll for write on the fd.
636 // Update Looper registration.
637 updateLooperRegistrationLocked(mService->getLooper());
638}
639
640void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
641 sensors_event_t const* scratch, const int numEventsDropped) {
642 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
643 // Count flushComplete events in the events that are about to the dropped. These will be sent
644 // separately before the next batch of events.
645 for (int j = 0; j < numEventsDropped; ++j) {
646 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700647 if (mSensorInfo.count(scratch[j].meta_data.sensor) == 0) {
Peng Xu63fbab82017-06-20 12:41:33 -0700648 ALOGW("%s: sensor 0x%x is not found in connection",
649 __func__, scratch[j].meta_data.sensor);
650 continue;
651 }
652
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700653 FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor];
Peng Xueb4d6282015-12-10 18:02:41 -0800654 flushInfo.mPendingFlushEventsToSend++;
655 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
656 flushInfo.mPendingFlushEventsToSend);
657 }
658 }
659 return;
660}
661
662int SensorService::SensorEventConnection::findWakeUpSensorEventLocked(
663 sensors_event_t const* scratch, const int count) {
664 for (int i = 0; i < count; ++i) {
665 if (mService->isWakeUpSensorEvent(scratch[i])) {
666 return i;
667 }
668 }
669 return -1;
670}
671
672sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
673{
674 return mChannel;
675}
676
677status_t SensorService::SensorEventConnection::enableDisable(
678 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
679 int reservedFlags)
680{
Arthur Ishiguro3e9afc12020-09-22 13:05:15 -0700681 if (mDestroyed) {
682 android_errorWriteLog(0x534e4554, "168211968");
683 return DEAD_OBJECT;
684 }
685
Peng Xueb4d6282015-12-10 18:02:41 -0800686 status_t err;
687 if (enabled) {
Anh Phamaf91a912021-02-10 14:10:53 +0100688 bool isSensorCapped = false;
689 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
690 if (si != nullptr) {
691 const Sensor& s = si->getSensor();
692 if (mService->isSensorInCappedSet(s.getType())) {
693 isSensorCapped = true;
694 }
695 }
696 if (isSensorCapped) {
697 err = mService->adjustSamplingPeriodBasedOnMicAndPermission(&samplingPeriodNs,
698 String16(mOpPackageName));
699 if (err != OK) {
700 return err;
701 }
702 }
Peng Xueb4d6282015-12-10 18:02:41 -0800703 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
704 reservedFlags, mOpPackageName);
705
706 } else {
707 err = mService->disable(this, handle);
708 }
709 return err;
710}
711
Anh Phamaf91a912021-02-10 14:10:53 +0100712status_t SensorService::SensorEventConnection::setEventRate(int handle, nsecs_t samplingPeriodNs) {
Arthur Ishiguro3e9afc12020-09-22 13:05:15 -0700713 if (mDestroyed) {
714 android_errorWriteLog(0x534e4554, "168211968");
715 return DEAD_OBJECT;
716 }
717
Anh Phamaf91a912021-02-10 14:10:53 +0100718 bool isSensorCapped = false;
719 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
720 if (si != nullptr) {
721 const Sensor& s = si->getSensor();
722 if (mService->isSensorInCappedSet(s.getType())) {
723 isSensorCapped = true;
724 }
725 }
726 if (isSensorCapped) {
727 status_t err = mService->adjustSamplingPeriodBasedOnMicAndPermission(&samplingPeriodNs,
728 String16(mOpPackageName));
729 if (err != OK) {
730 return err;
731 }
732 }
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700733 return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
Peng Xueb4d6282015-12-10 18:02:41 -0800734}
735
736status_t SensorService::SensorEventConnection::flush() {
Arthur Ishiguro3e9afc12020-09-22 13:05:15 -0700737 if (mDestroyed) {
738 return DEAD_OBJECT;
739 }
740
Peng Xueb4d6282015-12-10 18:02:41 -0800741 return mService->flushSensor(this, mOpPackageName);
742}
743
Peng Xue36e3472016-11-03 11:57:10 -0700744int32_t SensorService::SensorEventConnection::configureChannel(int handle, int rateLevel) {
745 // SensorEventConnection does not support configureChannel, parameters not used
746 UNUSED(handle);
747 UNUSED(rateLevel);
748 return INVALID_OPERATION;
749}
750
Peng Xueb4d6282015-12-10 18:02:41 -0800751int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) {
752 if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) {
753 {
754 // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount,
755 // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
756 // can release the wake-lock.
757 ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700758 Mutex::Autolock _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800759 mDead = true;
760 mWakeLockRefCount = 0;
761 updateLooperRegistrationLocked(mService->getLooper());
762 }
763 mService->checkWakeLockState();
764 if (mDataInjectionMode) {
765 // If the Looper has encountered some error in data injection mode, reset SensorService
766 // back to normal mode.
767 mService->resetToNormalMode();
768 mDataInjectionMode = false;
769 }
770 return 1;
771 }
772
773 if (events & ALOOPER_EVENT_INPUT) {
774 unsigned char buf[sizeof(sensors_event_t)];
775 ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
776 {
Arthur Ishiguro062b27b2020-04-13 08:04:49 -0700777 Mutex::Autolock _l(mConnectionLock);
Peng Xu755c4512016-04-07 23:15:14 -0700778 if (numBytesRead == sizeof(sensors_event_t)) {
779 if (!mDataInjectionMode) {
780 ALOGE("Data injected in normal mode, dropping event"
781 "package=%s uid=%d", mPackageName.string(), mUid);
782 // Unregister call backs.
783 return 0;
784 }
785 sensors_event_t sensor_event;
786 memcpy(&sensor_event, buf, sizeof(sensors_event_t));
787 sp<SensorInterface> si =
788 mService->getSensorInterfaceFromHandle(sensor_event.sensor);
789 if (si == nullptr) {
790 return 1;
791 }
792
793 SensorDevice& dev(SensorDevice::getInstance());
794 sensor_event.type = si->getSensor().getType();
795 dev.injectSensorData(&sensor_event);
Peng Xueb4d6282015-12-10 18:02:41 -0800796#if DEBUG_CONNECTIONS
Peng Xu755c4512016-04-07 23:15:14 -0700797 ++mEventsReceived;
Peng Xueb4d6282015-12-10 18:02:41 -0800798#endif
Peng Xu755c4512016-04-07 23:15:14 -0700799 } else if (numBytesRead == sizeof(uint32_t)) {
800 uint32_t numAcks = 0;
801 memcpy(&numAcks, buf, numBytesRead);
802 // Sanity check to ensure there are no read errors in recv, numAcks is always
803 // within the range and not zero. If any of the above don't hold reset
804 // mWakeLockRefCount to zero.
805 if (numAcks > 0 && numAcks < mWakeLockRefCount) {
806 mWakeLockRefCount -= numAcks;
807 } else {
808 mWakeLockRefCount = 0;
809 }
Peng Xueb4d6282015-12-10 18:02:41 -0800810#if DEBUG_CONNECTIONS
Peng Xu755c4512016-04-07 23:15:14 -0700811 mTotalAcksReceived += numAcks;
Peng Xueb4d6282015-12-10 18:02:41 -0800812#endif
813 } else {
814 // Read error, reset wakelock refcount.
815 mWakeLockRefCount = 0;
816 }
817 }
818 // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released
819 // here as checkWakeLockState() will need it.
820 if (mWakeLockRefCount == 0) {
821 mService->checkWakeLockState();
822 }
823 // continue getting callbacks.
824 return 1;
825 }
826
827 if (events & ALOOPER_EVENT_OUTPUT) {
828 // send sensor data that is stored in mEventCache for this connection.
829 mService->sendEventsFromCache(this);
830 }
831 return 1;
832}
833
834int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
835 size_t fifoWakeUpSensors = 0;
836 size_t fifoNonWakeUpSensors = 0;
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700837 for (auto& it : mSensorInfo) {
838 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(it.first);
Peng Xu755c4512016-04-07 23:15:14 -0700839 if (si == nullptr) {
840 continue;
841 }
842 const Sensor& sensor = si->getSensor();
Peng Xueb4d6282015-12-10 18:02:41 -0800843 if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) {
844 // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and
845 // non wake_up sensors.
846 if (sensor.isWakeUpSensor()) {
847 fifoWakeUpSensors += sensor.getFifoReservedEventCount();
848 } else {
849 fifoNonWakeUpSensors += sensor.getFifoReservedEventCount();
850 }
851 } else {
852 // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors.
853 if (sensor.isWakeUpSensor()) {
854 fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ?
855 fifoWakeUpSensors : sensor.getFifoMaxEventCount();
856
857 } else {
858 fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ?
859 fifoNonWakeUpSensors : sensor.getFifoMaxEventCount();
860
861 }
862 }
863 }
864 if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) {
865 // It is extremely unlikely that there is a write failure in non batch mode. Return a cache
866 // size that is equal to that of the batch mode.
867 // ALOGW("Write failure in non-batch mode");
868 return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t);
869 }
870 return fifoWakeUpSensors + fifoNonWakeUpSensors;
871}
872
873} // namespace android
874