blob: 5be4ccde1fd278450994b4906b29d194c9d624ef [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 Ishiguro8a7ec572020-03-18 11:02:35 -070017#include <cinttypes>
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 {
32
33SensorService::SensorEventConnection::SensorEventConnection(
34 const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode,
Svet Ganove752a5c2018-01-15 17:14:20 -080035 const String16& opPackageName, bool hasSensorAccess)
Peng Xueb4d6282015-12-10 18:02:41 -080036 : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
Yi Kong8f313e32018-07-17 14:13:29 -070037 mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(nullptr),
Brian Stackae4053f2018-12-10 14:54:18 -080038 mCacheSize(0), mMaxCacheSize(0), mTimeOfLastEventDrop(0), mEventsDropped(0),
39 mPackageName(packageName), mOpPackageName(opPackageName), mDestroyed(false),
40 mHasSensorAccess(hasSensorAccess) {
Peng Xueb4d6282015-12-10 18:02:41 -080041 mChannel = new BitTube(mService->mSocketBufferSize);
42#if DEBUG_CONNECTIONS
43 mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
44 mTotalAcksNeeded = mTotalAcksReceived = 0;
45#endif
46}
47
48SensorService::SensorEventConnection::~SensorEventConnection() {
49 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Peng Xu8cbefd72017-07-10 16:41:08 -070050 destroy();
51}
52
53void SensorService::SensorEventConnection::destroy() {
54 Mutex::Autolock _l(mDestroyLock);
55
56 // destroy once only
57 if (mDestroyed) {
58 return;
59 }
60
Peng Xueb4d6282015-12-10 18:02:41 -080061 mService->cleanupConnection(this);
Yi Kong8f313e32018-07-17 14:13:29 -070062 if (mEventCache != nullptr) {
George Burgess IV1866ed42018-01-21 12:14:09 -080063 delete[] mEventCache;
Peng Xueb4d6282015-12-10 18:02:41 -080064 }
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 Ishiguro410d5672020-04-06 08:50:20 -070073 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -080074 return !mDead && mWakeLockRefCount > 0;
75}
76
77void SensorService::SensorEventConnection::resetWakeLockRefCount() {
Arthur Ishiguro410d5672020-04-06 08:50:20 -070078 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -080079 mWakeLockRefCount = 0;
80}
81
82void SensorService::SensorEventConnection::dump(String8& result) {
Brian Stackbce04d72019-03-21 10:54:10 -070083 result.appendFormat("\tOperating Mode: ");
84 if (!mService->isWhiteListedPackage(getPackageName())) {
85 result.append("RESTRICTED\n");
86 } else if (mDataInjectionMode) {
87 result.append("DATA_INJECTION\n");
88 } else {
89 result.append("NORMAL\n");
90 }
Peng Xueb4d6282015-12-10 18:02:41 -080091 result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
92 "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
93 mMaxCacheSize);
Arthur Ishiguro410d5672020-04-06 08:50:20 -070094
95 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -070096 for (auto& it : mSensorInfo) {
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -070097 const FlushInfo& flushInfo = it.second.flushInfo;
Peng Xueb4d6282015-12-10 18:02:41 -080098 result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
Arthur Ishiguroad46c782020-03-26 11:24:43 -070099 mService->getSensorName(it.first).string(),
100 it.first,
Peng Xueb4d6282015-12-10 18:02:41 -0800101 flushInfo.mFirstFlushPending ? "First flush pending" :
102 "active",
103 flushInfo.mPendingFlushEventsToSend);
104 }
105#if DEBUG_CONNECTIONS
106 result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
107 " total_acks_needed %d | total_acks_recvd %d\n",
108 mEventsReceived,
109 mEventsSent,
110 mEventsSentFromCache,
111 mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
112 mTotalAcksNeeded,
113 mTotalAcksReceived);
114#endif
115}
116
Mike Ma24743862020-01-29 00:36:55 -0800117/**
118 * Dump debugging information as android.service.SensorEventConnectionProto protobuf message using
119 * ProtoOutputStream.
120 *
121 * See proto definition and some notes about ProtoOutputStream in
122 * frameworks/base/core/proto/android/service/sensor_service.proto
123 */
124void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto) const {
125 using namespace service::SensorEventConnectionProto;
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700126 std::lock_guard<std::mutex> _l(mConnectionLock);
Mike Ma24743862020-01-29 00:36:55 -0800127
128 if (!mService->isWhiteListedPackage(getPackageName())) {
129 proto->write(OPERATING_MODE, OP_MODE_RESTRICTED);
130 } else if (mDataInjectionMode) {
131 proto->write(OPERATING_MODE, OP_MODE_DATA_INJECTION);
132 } else {
133 proto->write(OPERATING_MODE, OP_MODE_NORMAL);
134 }
135 proto->write(PACKAGE_NAME, std::string(mPackageName.string()));
136 proto->write(WAKE_LOCK_REF_COUNT, int32_t(mWakeLockRefCount));
137 proto->write(UID, int32_t(mUid));
138 proto->write(CACHE_SIZE, int32_t(mCacheSize));
139 proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700140 for (auto& it : mSensorInfo) {
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700141 const FlushInfo& flushInfo = it.second.flushInfo;
Mike Ma24743862020-01-29 00:36:55 -0800142 const uint64_t token = proto->start(FLUSH_INFOS);
143 proto->write(FlushInfoProto::SENSOR_NAME,
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700144 std::string(mService->getSensorName(it.first)));
145 proto->write(FlushInfoProto::SENSOR_HANDLE, it.first);
Mike Ma24743862020-01-29 00:36:55 -0800146 proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending);
147 proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND,
148 flushInfo.mPendingFlushEventsToSend);
149 proto->end(token);
150 }
151#if DEBUG_CONNECTIONS
152 proto->write(EVENTS_RECEIVED, mEventsReceived);
153 proto->write(EVENTS_SENT, mEventsSent);
154 proto->write(EVENTS_CACHE, mEventsSentFromCache);
155 proto->write(EVENTS_DROPPED, mEventsReceived - (mEventsSentFromCache + mEventsSent +
156 mCacheSize));
157 proto->write(TOTAL_ACKS_NEEDED, mTotalAcksNeeded);
158 proto->write(TOTAL_ACKS_RECEIVED, mTotalAcksReceived);
159#endif
160}
161
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700162bool SensorService::SensorEventConnection::addSensor(
163 int32_t handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags) {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700164 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xu755c4512016-04-07 23:15:14 -0700165 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
166 if (si == nullptr ||
167 !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700168 mSensorInfo.count(handle) > 0) {
Peng Xueb4d6282015-12-10 18:02:41 -0800169 return false;
170 }
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700171
172 SensorRequest request = {
173 .samplingPeriodNs = samplingPeriodNs,
174 .maxBatchReportLatencyNs = maxBatchReportLatencyNs,
175 .reservedFlags = reservedFlags
176 };
177
178 mSensorInfo[handle] = request;
Peng Xu755c4512016-04-07 23:15:14 -0700179 return true;
Peng Xueb4d6282015-12-10 18:02:41 -0800180}
181
182bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700183 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700184 return mSensorInfo.erase(handle) > 0;
Peng Xueb4d6282015-12-10 18:02:41 -0800185}
186
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700187std::vector<int32_t> SensorService::SensorEventConnection::getActiveSensorHandles() const {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700188 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700189 std::vector<int32_t> list;
190 for (auto& it : mSensorInfo) {
191 list.push_back(it.first);
192 }
193 return list;
194}
195
Peng Xueb4d6282015-12-10 18:02:41 -0800196bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700197 std::lock_guard<std::recursive_mutex> _backlock(mBackupLock);
198 std::lock_guard<std::mutex> _lock(mConnectionLock);
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700199 return mSensorInfo.count(handle) + mSensorInfoBackup.count(handle) > 0;
Peng Xueb4d6282015-12-10 18:02:41 -0800200}
201
202bool SensorService::SensorEventConnection::hasAnySensor() const {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700203 std::lock_guard<std::recursive_mutex> _backlock(mBackupLock);
204 std::lock_guard<std::mutex> _lock(mConnectionLock);
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700205 return mSensorInfo.size() + mSensorInfoBackup.size() ? true : false;
Peng Xueb4d6282015-12-10 18:02:41 -0800206}
207
208bool SensorService::SensorEventConnection::hasOneShotSensors() const {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700209 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700210 for (auto &it : mSensorInfo) {
211 const int handle = it.first;
Peng Xu755c4512016-04-07 23:15:14 -0700212 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
213 if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
Peng Xueb4d6282015-12-10 18:02:41 -0800214 return true;
215 }
216 }
217 return false;
218}
219
220String8 SensorService::SensorEventConnection::getPackageName() const {
221 return mPackageName;
222}
223
224void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
225 bool value) {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700226 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700227 if (mSensorInfo.count(handle) > 0) {
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700228 FlushInfo& flushInfo = mSensorInfo[handle].flushInfo;
Peng Xueb4d6282015-12-10 18:02:41 -0800229 flushInfo.mFirstFlushPending = value;
230 }
231}
232
233void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700234 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800235 updateLooperRegistrationLocked(looper);
236}
237
238void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
239 const sp<Looper>& looper) {
240 bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) ||
241 mDataInjectionMode;
242 // If all sensors are unregistered OR Looper has encountered an error, we can remove the Fd from
243 // the Looper if it has been previously added.
244 if (!isConnectionActive || mDead) { if (mHasLooperCallbacks) {
245 ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this,
246 mChannel->getSendFd());
247 looper->removeFd(mChannel->getSendFd()); mHasLooperCallbacks = false; }
248 return; }
249
250 int looper_flags = 0;
251 if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
252 if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700253 for (auto& it : mSensorInfo) {
254 const int handle = it.first;
Peng Xu755c4512016-04-07 23:15:14 -0700255 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
256 if (si != nullptr && si->getSensor().isWakeUpSensor()) {
Peng Xueb4d6282015-12-10 18:02:41 -0800257 looper_flags |= ALOOPER_EVENT_INPUT;
Peng Xueb4d6282015-12-10 18:02:41 -0800258 }
259 }
260
261 // If flags is still set to zero, we don't need to add this fd to the Looper, if the fd has
262 // already been added, remove it. This is likely to happen when ALL the events stored in the
263 // cache have been sent to the corresponding app.
264 if (looper_flags == 0) {
265 if (mHasLooperCallbacks) {
266 ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd());
267 looper->removeFd(mChannel->getSendFd());
268 mHasLooperCallbacks = false;
269 }
270 return;
271 }
272
273 // Add the file descriptor to the Looper for receiving acknowledegments if the app has
274 // registered for wake-up sensors OR for sending events in the cache.
Yi Kong8f313e32018-07-17 14:13:29 -0700275 int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, nullptr);
Peng Xueb4d6282015-12-10 18:02:41 -0800276 if (ret == 1) {
277 ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd());
278 mHasLooperCallbacks = true;
279 } else {
280 ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd());
281 }
282}
283
284void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700285 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700286 if (mSensorInfo.count(handle) > 0) {
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700287 FlushInfo& flushInfo = mSensorInfo[handle].flushInfo;
Peng Xueb4d6282015-12-10 18:02:41 -0800288 flushInfo.mPendingFlushEventsToSend++;
289 }
290}
291
292status_t SensorService::SensorEventConnection::sendEvents(
293 sensors_event_t const* buffer, size_t numEvents,
294 sensors_event_t* scratch,
Peng Xuded526e2016-08-12 16:39:44 -0700295 wp<const SensorEventConnection> const * mapFlushEventsToConnections) {
Peng Xueb4d6282015-12-10 18:02:41 -0800296 // filter out events not for this connection
Svet Ganove752a5c2018-01-15 17:14:20 -0800297
George Burgess IV1866ed42018-01-21 12:14:09 -0800298 std::unique_ptr<sensors_event_t[]> sanitizedBuffer;
Svet Ganove752a5c2018-01-15 17:14:20 -0800299
Peng Xueb4d6282015-12-10 18:02:41 -0800300 int count = 0;
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700301 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800302 if (scratch) {
303 size_t i=0;
304 while (i<numEvents) {
305 int32_t sensor_handle = buffer[i].sensor;
306 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
307 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
308 buffer[i].meta_data.sensor);
309 // Setting sensor_handle to the correct sensor to ensure the sensor events per
310 // connection are filtered correctly. buffer[i].sensor is zero for meta_data
311 // events.
312 sensor_handle = buffer[i].meta_data.sensor;
313 }
314
Peng Xueb4d6282015-12-10 18:02:41 -0800315 // Check if this connection has registered for this sensor. If not continue to the
316 // next sensor_event.
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700317 if (mSensorInfo.count(sensor_handle) == 0) {
Peng Xueb4d6282015-12-10 18:02:41 -0800318 ++i;
319 continue;
320 }
321
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700322 FlushInfo& flushInfo = mSensorInfo[sensor_handle].flushInfo;
Peng Xueb4d6282015-12-10 18:02:41 -0800323 // Check if there is a pending flush_complete event for this sensor on this connection.
324 if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
Peng Xuded526e2016-08-12 16:39:44 -0700325 mapFlushEventsToConnections[i] == this) {
Peng Xueb4d6282015-12-10 18:02:41 -0800326 flushInfo.mFirstFlushPending = false;
327 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
328 buffer[i].meta_data.sensor);
329 ++i;
330 continue;
331 }
332
333 // If there is a pending flush complete event for this sensor on this connection,
334 // ignore the event and proceed to the next.
335 if (flushInfo.mFirstFlushPending) {
336 ++i;
337 continue;
338 }
339
340 do {
341 // Keep copying events into the scratch buffer as long as they are regular
342 // sensor_events are from the same sensor_handle OR they are flush_complete_events
343 // from the same sensor_handle AND the current connection is mapped to the
344 // corresponding flush_complete_event.
345 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
Peng Xuded526e2016-08-12 16:39:44 -0700346 if (mapFlushEventsToConnections[i] == this) {
Peng Xueb4d6282015-12-10 18:02:41 -0800347 scratch[count++] = buffer[i];
348 }
Peng Xueb4d6282015-12-10 18:02:41 -0800349 } else {
Brian Stackc225aa12019-04-19 09:30:25 -0700350 // Regular sensor event, just copy it to the scratch buffer after checking
351 // the AppOp.
352 if (hasSensorAccess() && noteOpIfRequired(buffer[i])) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800353 scratch[count++] = buffer[i];
354 }
Peng Xueb4d6282015-12-10 18:02:41 -0800355 }
Svet Ganove752a5c2018-01-15 17:14:20 -0800356 i++;
Peng Xueb4d6282015-12-10 18:02:41 -0800357 } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle &&
358 buffer[i].type != SENSOR_TYPE_META_DATA) ||
359 (buffer[i].type == SENSOR_TYPE_META_DATA &&
360 buffer[i].meta_data.sensor == sensor_handle)));
361 }
362 } else {
Michael Groover5e1f60b2018-12-04 22:34:29 -0800363 if (hasSensorAccess()) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800364 scratch = const_cast<sensors_event_t *>(buffer);
365 count = numEvents;
366 } else {
George Burgess IV1866ed42018-01-21 12:14:09 -0800367 sanitizedBuffer.reset(new sensors_event_t[numEvents]);
368 scratch = sanitizedBuffer.get();
Svet Ganove752a5c2018-01-15 17:14:20 -0800369 for (size_t i = 0; i < numEvents; i++) {
370 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
371 scratch[count++] = buffer[i++];
372 }
373 }
374 }
Peng Xueb4d6282015-12-10 18:02:41 -0800375 }
376
377 sendPendingFlushEventsLocked();
378 // Early return if there are no events for this connection.
379 if (count == 0) {
380 return status_t(NO_ERROR);
381 }
382
383#if DEBUG_CONNECTIONS
384 mEventsReceived += count;
385#endif
386 if (mCacheSize != 0) {
387 // There are some events in the cache which need to be sent first. Copy this buffer to
388 // the end of cache.
Brian Stack93432ad2018-11-27 18:28:48 -0800389 appendEventsToCacheLocked(scratch, count);
Peng Xueb4d6282015-12-10 18:02:41 -0800390 return status_t(NO_ERROR);
391 }
392
Svet Ganove752a5c2018-01-15 17:14:20 -0800393 int index_wake_up_event = -1;
Michael Groover5e1f60b2018-12-04 22:34:29 -0800394 if (hasSensorAccess()) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800395 index_wake_up_event = findWakeUpSensorEventLocked(scratch, count);
396 if (index_wake_up_event >= 0) {
397 scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
398 ++mWakeLockRefCount;
Peng Xueb4d6282015-12-10 18:02:41 -0800399#if DEBUG_CONNECTIONS
Svet Ganove752a5c2018-01-15 17:14:20 -0800400 ++mTotalAcksNeeded;
Peng Xueb4d6282015-12-10 18:02:41 -0800401#endif
Svet Ganove752a5c2018-01-15 17:14:20 -0800402 }
Peng Xueb4d6282015-12-10 18:02:41 -0800403 }
404
405 // NOTE: ASensorEvent and sensors_event_t are the same type.
406 ssize_t size = SensorEventQueue::write(mChannel,
407 reinterpret_cast<ASensorEvent const*>(scratch), count);
408 if (size < 0) {
409 // Write error, copy events to local cache.
410 if (index_wake_up_event >= 0) {
411 // If there was a wake_up sensor_event, reset the flag.
412 scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
413 if (mWakeLockRefCount > 0) {
414 --mWakeLockRefCount;
415 }
416#if DEBUG_CONNECTIONS
417 --mTotalAcksNeeded;
418#endif
419 }
Yi Kong8f313e32018-07-17 14:13:29 -0700420 if (mEventCache == nullptr) {
Peng Xueb4d6282015-12-10 18:02:41 -0800421 mMaxCacheSize = computeMaxCacheSizeLocked();
422 mEventCache = new sensors_event_t[mMaxCacheSize];
423 mCacheSize = 0;
424 }
Brian Stack93432ad2018-11-27 18:28:48 -0800425 // Save the events so that they can be written later
426 appendEventsToCacheLocked(scratch, count);
Peng Xueb4d6282015-12-10 18:02:41 -0800427
428 // Add this file descriptor to the looper to get a callback when this fd is available for
429 // writing.
430 updateLooperRegistrationLocked(mService->getLooper());
431 return size;
432 }
433
434#if DEBUG_CONNECTIONS
435 if (size > 0) {
436 mEventsSent += count;
437 }
438#endif
439
440 return size < 0 ? status_t(size) : status_t(NO_ERROR);
441}
442
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700443void SensorService::SensorEventConnection::updateSensorSubscriptions() {
444 if (!hasSensorAccess()) {
445 stopAll();
446 } else {
447 recoverAll();
448 }
449}
450
Svet Ganove752a5c2018-01-15 17:14:20 -0800451void SensorService::SensorEventConnection::setSensorAccess(const bool hasAccess) {
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700452 if (mHasSensorAccess != hasAccess) {
453 mHasSensorAccess = hasAccess;
454 updateSensorSubscriptions();
455 }
456}
457
458void SensorService::SensorEventConnection::stopAll() {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700459 bool backupPerformed = false;
460 std::lock_guard<std::recursive_mutex> _backlock(mBackupLock);
461 {
462 std::lock_guard<std::mutex> _lock(mConnectionLock);
463 if (!mSensorInfo.empty()) {
464 mSensorInfoBackup = mSensorInfo;
465 mSensorInfo.clear();
466 backupPerformed = true;
467 }
468 }
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700469
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700470 if (backupPerformed) {
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700471 for (auto& it : mSensorInfoBackup) {
472 int32_t handle = it.first;
473
474 status_t err = mService->disable(this, handle);
475
476 if (err != NO_ERROR) {
477 ALOGE("Error disabling sensor %d", handle);
478 }
479 }
480 }
481}
482
483void SensorService::SensorEventConnection::recoverAll() {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700484 std::lock_guard<std::recursive_mutex> _l(mBackupLock);
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700485 for (auto& it : mSensorInfoBackup) {
486 int32_t handle = it.first;
487 SensorRequest &request = it.second;
488
489 status_t err = mService->enable(
490 this, handle, request.samplingPeriodNs, request.maxBatchReportLatencyNs,
491 request.reservedFlags, mOpPackageName);
492
493 if (err != NO_ERROR) {
494 ALOGE("Error recovering sensor %d", handle);
495 }
496 }
497
498 mSensorInfoBackup.clear();
Svet Ganove752a5c2018-01-15 17:14:20 -0800499}
500
Michael Groover5e1f60b2018-12-04 22:34:29 -0800501bool SensorService::SensorEventConnection::hasSensorAccess() {
502 return mHasSensorAccess && !mService->mSensorPrivacyPolicy->isSensorPrivacyEnabled();
503}
504
Brian Stackc225aa12019-04-19 09:30:25 -0700505bool SensorService::SensorEventConnection::noteOpIfRequired(const sensors_event_t& event) {
506 bool success = true;
507 const auto iter = mHandleToAppOp.find(event.sensor);
508 if (iter != mHandleToAppOp.end()) {
509 int32_t appOpMode = mService->sAppOpsManager.noteOp((*iter).second, mUid, mOpPackageName);
510 success = (appOpMode == AppOpsManager::MODE_ALLOWED);
511 }
512 return success;
513}
514
Peng Xueb4d6282015-12-10 18:02:41 -0800515void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch,
516 int count) {
517 sensors_event_t *eventCache_new;
518 const int new_cache_size = computeMaxCacheSizeLocked();
519 // Allocate new cache, copy over events from the old cache & scratch, free up memory.
520 eventCache_new = new sensors_event_t[new_cache_size];
521 memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t));
522 memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t));
523
524 ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize,
525 new_cache_size);
526
George Burgess IV1866ed42018-01-21 12:14:09 -0800527 delete[] mEventCache;
Peng Xueb4d6282015-12-10 18:02:41 -0800528 mEventCache = eventCache_new;
529 mCacheSize += count;
530 mMaxCacheSize = new_cache_size;
531}
532
Brian Stack93432ad2018-11-27 18:28:48 -0800533void SensorService::SensorEventConnection::appendEventsToCacheLocked(sensors_event_t const* events,
534 int count) {
535 if (count <= 0) {
536 return;
537 } else if (mCacheSize + count <= mMaxCacheSize) {
538 // The events fit within the current cache: add them
539 memcpy(&mEventCache[mCacheSize], events, count * sizeof(sensors_event_t));
540 mCacheSize += count;
541 } else if (mCacheSize + count <= computeMaxCacheSizeLocked()) {
542 // The events fit within a resized cache: resize the cache and add the events
543 reAllocateCacheLocked(events, count);
544 } else {
545 // The events do not fit within the cache: drop the oldest events.
Brian Stack93432ad2018-11-27 18:28:48 -0800546 int freeSpace = mMaxCacheSize - mCacheSize;
547
548 // Drop up to the currently cached number of events to make room for new events
549 int cachedEventsToDrop = std::min(mCacheSize, count - freeSpace);
550
551 // New events need to be dropped if there are more new events than the size of the cache
552 int newEventsToDrop = std::max(0, count - mMaxCacheSize);
553
554 // Determine the number of new events to copy into the cache
555 int eventsToCopy = std::min(mMaxCacheSize, count);
556
Brian Stackae4053f2018-12-10 14:54:18 -0800557 constexpr nsecs_t kMinimumTimeBetweenDropLogNs = 2 * 1000 * 1000 * 1000; // 2 sec
558 if (events[0].timestamp - mTimeOfLastEventDrop > kMinimumTimeBetweenDropLogNs) {
559 ALOGW("Dropping %d cached events (%d/%d) to save %d/%d new events. %d events previously"
560 " dropped", cachedEventsToDrop, mCacheSize, mMaxCacheSize, eventsToCopy,
561 count, mEventsDropped);
562 mEventsDropped = 0;
563 mTimeOfLastEventDrop = events[0].timestamp;
564 } else {
565 // Record the number dropped
566 mEventsDropped += cachedEventsToDrop + newEventsToDrop;
567 }
568
Brian Stack93432ad2018-11-27 18:28:48 -0800569 // Check for any flush complete events in the events that will be dropped
570 countFlushCompleteEventsLocked(mEventCache, cachedEventsToDrop);
571 countFlushCompleteEventsLocked(events, newEventsToDrop);
572
573 // Only shift the events if they will not all be overwritten
574 if (eventsToCopy != mMaxCacheSize) {
575 memmove(mEventCache, &mEventCache[cachedEventsToDrop],
576 (mCacheSize - cachedEventsToDrop) * sizeof(sensors_event_t));
577 }
578 mCacheSize -= cachedEventsToDrop;
579
580 // Copy the events into the cache
581 memcpy(&mEventCache[mCacheSize], &events[newEventsToDrop],
582 eventsToCopy * sizeof(sensors_event_t));
583 mCacheSize += eventsToCopy;
584 }
585}
586
Peng Xueb4d6282015-12-10 18:02:41 -0800587void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
588 ASensorEvent flushCompleteEvent;
589 memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
590 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
591 // Loop through all the sensors for this connection and check if there are any pending
592 // flush complete events to be sent.
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700593 for (auto& it : mSensorInfo) {
594 const int handle = it.first;
Peng Xu755c4512016-04-07 23:15:14 -0700595 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
596 if (si == nullptr) {
597 continue;
598 }
599
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700600 FlushInfo& flushInfo = it.second.flushInfo;
Peng Xueb4d6282015-12-10 18:02:41 -0800601 while (flushInfo.mPendingFlushEventsToSend > 0) {
Peng Xu755c4512016-04-07 23:15:14 -0700602 flushCompleteEvent.meta_data.sensor = handle;
603 bool wakeUpSensor = si->getSensor().isWakeUpSensor();
Peng Xueb4d6282015-12-10 18:02:41 -0800604 if (wakeUpSensor) {
605 ++mWakeLockRefCount;
606 flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
607 }
608 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
609 if (size < 0) {
610 if (wakeUpSensor) --mWakeLockRefCount;
611 return;
612 }
613 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
614 flushCompleteEvent.meta_data.sensor);
615 flushInfo.mPendingFlushEventsToSend--;
616 }
617 }
618}
619
620void SensorService::SensorEventConnection::writeToSocketFromCache() {
621 // At a time write at most half the size of the receiver buffer in SensorEventQueue OR
622 // half the size of the socket buffer allocated in BitTube whichever is smaller.
623 const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
624 int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700625 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800626 // Send pending flush complete events (if any)
627 sendPendingFlushEventsLocked();
628 for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
629 const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
Svet Ganove752a5c2018-01-15 17:14:20 -0800630 int index_wake_up_event = -1;
Michael Groover5e1f60b2018-12-04 22:34:29 -0800631 if (hasSensorAccess()) {
Svet Ganove752a5c2018-01-15 17:14:20 -0800632 index_wake_up_event =
633 findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
634 if (index_wake_up_event >= 0) {
635 mEventCache[index_wake_up_event + numEventsSent].flags |=
636 WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
637 ++mWakeLockRefCount;
Peng Xueb4d6282015-12-10 18:02:41 -0800638#if DEBUG_CONNECTIONS
Svet Ganove752a5c2018-01-15 17:14:20 -0800639 ++mTotalAcksNeeded;
Peng Xueb4d6282015-12-10 18:02:41 -0800640#endif
Svet Ganove752a5c2018-01-15 17:14:20 -0800641 }
Peng Xueb4d6282015-12-10 18:02:41 -0800642 }
643
644 ssize_t size = SensorEventQueue::write(mChannel,
645 reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
646 numEventsToWrite);
647 if (size < 0) {
648 if (index_wake_up_event >= 0) {
649 // If there was a wake_up sensor_event, reset the flag.
650 mEventCache[index_wake_up_event + numEventsSent].flags &=
651 ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
652 if (mWakeLockRefCount > 0) {
653 --mWakeLockRefCount;
654 }
655#if DEBUG_CONNECTIONS
656 --mTotalAcksNeeded;
657#endif
658 }
659 memmove(mEventCache, &mEventCache[numEventsSent],
660 (mCacheSize - numEventsSent) * sizeof(sensors_event_t));
661 ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ",
662 numEventsSent, mCacheSize);
663 mCacheSize -= numEventsSent;
664 return;
665 }
666 numEventsSent += numEventsToWrite;
667#if DEBUG_CONNECTIONS
668 mEventsSentFromCache += numEventsToWrite;
669#endif
670 }
671 ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize);
672 // All events from the cache have been sent. Reset cache size to zero.
673 mCacheSize = 0;
674 // There are no more events in the cache. We don't need to poll for write on the fd.
675 // Update Looper registration.
676 updateLooperRegistrationLocked(mService->getLooper());
677}
678
679void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
680 sensors_event_t const* scratch, const int numEventsDropped) {
681 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
682 // Count flushComplete events in the events that are about to the dropped. These will be sent
683 // separately before the next batch of events.
684 for (int j = 0; j < numEventsDropped; ++j) {
685 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700686 if (mSensorInfo.count(scratch[j].meta_data.sensor) == 0) {
Peng Xu63fbab82017-06-20 12:41:33 -0700687 ALOGW("%s: sensor 0x%x is not found in connection",
688 __func__, scratch[j].meta_data.sensor);
689 continue;
690 }
691
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700692 FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor].flushInfo;
Peng Xueb4d6282015-12-10 18:02:41 -0800693 flushInfo.mPendingFlushEventsToSend++;
694 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
695 flushInfo.mPendingFlushEventsToSend);
696 }
697 }
698 return;
699}
700
701int SensorService::SensorEventConnection::findWakeUpSensorEventLocked(
702 sensors_event_t const* scratch, const int count) {
703 for (int i = 0; i < count; ++i) {
704 if (mService->isWakeUpSensorEvent(scratch[i])) {
705 return i;
706 }
707 }
708 return -1;
709}
710
711sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
712{
713 return mChannel;
714}
715
716status_t SensorService::SensorEventConnection::enableDisable(
717 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
718 int reservedFlags)
719{
720 status_t err;
721 if (enabled) {
722 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
723 reservedFlags, mOpPackageName);
724
725 } else {
726 err = mService->disable(this, handle);
727 }
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700728
Peng Xueb4d6282015-12-10 18:02:41 -0800729 return err;
730}
731
732status_t SensorService::SensorEventConnection::setEventRate(
733 int handle, nsecs_t samplingPeriodNs)
734{
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700735 status_t err = mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
736
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700737 std::lock_guard<std::mutex> _l(mConnectionLock);
Arthur Ishiguro8a7ec572020-03-18 11:02:35 -0700738 if (err == NO_ERROR && mSensorInfo.count(handle) > 0) {
739 mSensorInfo[handle].samplingPeriodNs = samplingPeriodNs;
740 }
741
742 return err;
Peng Xueb4d6282015-12-10 18:02:41 -0800743}
744
745status_t SensorService::SensorEventConnection::flush() {
746 return mService->flushSensor(this, mOpPackageName);
747}
748
Peng Xue36e3472016-11-03 11:57:10 -0700749int32_t SensorService::SensorEventConnection::configureChannel(int handle, int rateLevel) {
750 // SensorEventConnection does not support configureChannel, parameters not used
751 UNUSED(handle);
752 UNUSED(rateLevel);
753 return INVALID_OPERATION;
754}
755
Peng Xueb4d6282015-12-10 18:02:41 -0800756int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) {
757 if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) {
758 {
759 // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount,
760 // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
761 // can release the wake-lock.
762 ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700763 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xueb4d6282015-12-10 18:02:41 -0800764 mDead = true;
765 mWakeLockRefCount = 0;
766 updateLooperRegistrationLocked(mService->getLooper());
767 }
768 mService->checkWakeLockState();
769 if (mDataInjectionMode) {
770 // If the Looper has encountered some error in data injection mode, reset SensorService
771 // back to normal mode.
772 mService->resetToNormalMode();
773 mDataInjectionMode = false;
774 }
775 return 1;
776 }
777
778 if (events & ALOOPER_EVENT_INPUT) {
779 unsigned char buf[sizeof(sensors_event_t)];
780 ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
781 {
Arthur Ishiguro410d5672020-04-06 08:50:20 -0700782 std::lock_guard<std::mutex> _l(mConnectionLock);
Peng Xu755c4512016-04-07 23:15:14 -0700783 if (numBytesRead == sizeof(sensors_event_t)) {
784 if (!mDataInjectionMode) {
785 ALOGE("Data injected in normal mode, dropping event"
786 "package=%s uid=%d", mPackageName.string(), mUid);
787 // Unregister call backs.
788 return 0;
789 }
790 sensors_event_t sensor_event;
791 memcpy(&sensor_event, buf, sizeof(sensors_event_t));
792 sp<SensorInterface> si =
793 mService->getSensorInterfaceFromHandle(sensor_event.sensor);
794 if (si == nullptr) {
795 return 1;
796 }
797
798 SensorDevice& dev(SensorDevice::getInstance());
799 sensor_event.type = si->getSensor().getType();
800 dev.injectSensorData(&sensor_event);
Peng Xueb4d6282015-12-10 18:02:41 -0800801#if DEBUG_CONNECTIONS
Peng Xu755c4512016-04-07 23:15:14 -0700802 ++mEventsReceived;
Peng Xueb4d6282015-12-10 18:02:41 -0800803#endif
Peng Xu755c4512016-04-07 23:15:14 -0700804 } else if (numBytesRead == sizeof(uint32_t)) {
805 uint32_t numAcks = 0;
806 memcpy(&numAcks, buf, numBytesRead);
807 // Sanity check to ensure there are no read errors in recv, numAcks is always
808 // within the range and not zero. If any of the above don't hold reset
809 // mWakeLockRefCount to zero.
810 if (numAcks > 0 && numAcks < mWakeLockRefCount) {
811 mWakeLockRefCount -= numAcks;
812 } else {
813 mWakeLockRefCount = 0;
814 }
Peng Xueb4d6282015-12-10 18:02:41 -0800815#if DEBUG_CONNECTIONS
Peng Xu755c4512016-04-07 23:15:14 -0700816 mTotalAcksReceived += numAcks;
Peng Xueb4d6282015-12-10 18:02:41 -0800817#endif
818 } else {
819 // Read error, reset wakelock refcount.
820 mWakeLockRefCount = 0;
821 }
822 }
823 // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released
824 // here as checkWakeLockState() will need it.
825 if (mWakeLockRefCount == 0) {
826 mService->checkWakeLockState();
827 }
828 // continue getting callbacks.
829 return 1;
830 }
831
832 if (events & ALOOPER_EVENT_OUTPUT) {
833 // send sensor data that is stored in mEventCache for this connection.
834 mService->sendEventsFromCache(this);
835 }
836 return 1;
837}
838
839int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
840 size_t fifoWakeUpSensors = 0;
841 size_t fifoNonWakeUpSensors = 0;
Arthur Ishiguroad46c782020-03-26 11:24:43 -0700842 for (auto& it : mSensorInfo) {
843 sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(it.first);
Peng Xu755c4512016-04-07 23:15:14 -0700844 if (si == nullptr) {
845 continue;
846 }
847 const Sensor& sensor = si->getSensor();
Peng Xueb4d6282015-12-10 18:02:41 -0800848 if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) {
849 // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and
850 // non wake_up sensors.
851 if (sensor.isWakeUpSensor()) {
852 fifoWakeUpSensors += sensor.getFifoReservedEventCount();
853 } else {
854 fifoNonWakeUpSensors += sensor.getFifoReservedEventCount();
855 }
856 } else {
857 // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors.
858 if (sensor.isWakeUpSensor()) {
859 fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ?
860 fifoWakeUpSensors : sensor.getFifoMaxEventCount();
861
862 } else {
863 fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ?
864 fifoNonWakeUpSensors : sensor.getFifoMaxEventCount();
865
866 }
867 }
868 }
869 if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) {
870 // It is extremely unlikely that there is a write failure in non batch mode. Return a cache
871 // size that is equal to that of the batch mode.
872 // ALOGW("Write failure in non-batch mode");
873 return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t);
874 }
875 return fifoWakeUpSensors + fifoNonWakeUpSensors;
876}
877
878} // namespace android
879