SensorEventConnection.cpp revision eb4d628b69831d533f14c09fd63400f75e69ba76
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <sys/socket.h> 18#include <utils/threads.h> 19 20#include <gui/SensorEventQueue.h> 21 22#include "vec.h" 23#include "SensorEventConnection.h" 24 25namespace android { 26 27SensorService::SensorEventConnection::SensorEventConnection( 28 const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode, 29 const String16& opPackageName) 30 : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false), 31 mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL), 32 mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) { 33 mChannel = new BitTube(mService->mSocketBufferSize); 34#if DEBUG_CONNECTIONS 35 mEventsReceived = mEventsSentFromCache = mEventsSent = 0; 36 mTotalAcksNeeded = mTotalAcksReceived = 0; 37#endif 38} 39 40SensorService::SensorEventConnection::~SensorEventConnection() { 41 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); 42 mService->cleanupConnection(this); 43 if (mEventCache != NULL) { 44 delete mEventCache; 45 } 46} 47 48void SensorService::SensorEventConnection::onFirstRef() { 49 LooperCallback::onFirstRef(); 50} 51 52bool SensorService::SensorEventConnection::needsWakeLock() { 53 Mutex::Autolock _l(mConnectionLock); 54 return !mDead && mWakeLockRefCount > 0; 55} 56 57void SensorService::SensorEventConnection::resetWakeLockRefCount() { 58 Mutex::Autolock _l(mConnectionLock); 59 mWakeLockRefCount = 0; 60} 61 62void SensorService::SensorEventConnection::dump(String8& result) { 63 Mutex::Autolock _l(mConnectionLock); 64 result.appendFormat("\tOperating Mode: %s\n",mDataInjectionMode ? "DATA_INJECTION" : "NORMAL"); 65 result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | " 66 "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize, 67 mMaxCacheSize); 68 for (size_t i = 0; i < mSensorInfo.size(); ++i) { 69 const FlushInfo& flushInfo = mSensorInfo.valueAt(i); 70 result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n", 71 mService->getSensorName(mSensorInfo.keyAt(i)).string(), 72 mSensorInfo.keyAt(i), 73 flushInfo.mFirstFlushPending ? "First flush pending" : 74 "active", 75 flushInfo.mPendingFlushEventsToSend); 76 } 77#if DEBUG_CONNECTIONS 78 result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |" 79 " total_acks_needed %d | total_acks_recvd %d\n", 80 mEventsReceived, 81 mEventsSent, 82 mEventsSentFromCache, 83 mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize), 84 mTotalAcksNeeded, 85 mTotalAcksReceived); 86#endif 87} 88 89bool SensorService::SensorEventConnection::addSensor(int32_t handle) { 90 Mutex::Autolock _l(mConnectionLock); 91 if (!canAccessSensor(mService->getSensorFromHandle(handle), 92 "Tried adding", mOpPackageName)) { 93 return false; 94 } 95 if (mSensorInfo.indexOfKey(handle) < 0) { 96 mSensorInfo.add(handle, FlushInfo()); 97 return true; 98 } 99 return false; 100} 101 102bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { 103 Mutex::Autolock _l(mConnectionLock); 104 if (mSensorInfo.removeItem(handle) >= 0) { 105 return true; 106 } 107 return false; 108} 109 110bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { 111 Mutex::Autolock _l(mConnectionLock); 112 return mSensorInfo.indexOfKey(handle) >= 0; 113} 114 115bool SensorService::SensorEventConnection::hasAnySensor() const { 116 Mutex::Autolock _l(mConnectionLock); 117 return mSensorInfo.size() ? true : false; 118} 119 120bool SensorService::SensorEventConnection::hasOneShotSensors() const { 121 Mutex::Autolock _l(mConnectionLock); 122 for (size_t i = 0; i < mSensorInfo.size(); ++i) { 123 const int handle = mSensorInfo.keyAt(i); 124 if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) { 125 return true; 126 } 127 } 128 return false; 129} 130 131String8 SensorService::SensorEventConnection::getPackageName() const { 132 return mPackageName; 133} 134 135void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, 136 bool value) { 137 Mutex::Autolock _l(mConnectionLock); 138 ssize_t index = mSensorInfo.indexOfKey(handle); 139 if (index >= 0) { 140 FlushInfo& flushInfo = mSensorInfo.editValueAt(index); 141 flushInfo.mFirstFlushPending = value; 142 } 143} 144 145void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) { 146 Mutex::Autolock _l(mConnectionLock); 147 updateLooperRegistrationLocked(looper); 148} 149 150void SensorService::SensorEventConnection::updateLooperRegistrationLocked( 151 const sp<Looper>& looper) { 152 bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) || 153 mDataInjectionMode; 154 // If all sensors are unregistered OR Looper has encountered an error, we can remove the Fd from 155 // the Looper if it has been previously added. 156 if (!isConnectionActive || mDead) { if (mHasLooperCallbacks) { 157 ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, 158 mChannel->getSendFd()); 159 looper->removeFd(mChannel->getSendFd()); mHasLooperCallbacks = false; } 160 return; } 161 162 int looper_flags = 0; 163 if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT; 164 if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT; 165 for (size_t i = 0; i < mSensorInfo.size(); ++i) { 166 const int handle = mSensorInfo.keyAt(i); 167 if (mService->getSensorFromHandle(handle).isWakeUpSensor()) { 168 looper_flags |= ALOOPER_EVENT_INPUT; 169 break; 170 } 171 } 172 173 // If flags is still set to zero, we don't need to add this fd to the Looper, if the fd has 174 // already been added, remove it. This is likely to happen when ALL the events stored in the 175 // cache have been sent to the corresponding app. 176 if (looper_flags == 0) { 177 if (mHasLooperCallbacks) { 178 ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd()); 179 looper->removeFd(mChannel->getSendFd()); 180 mHasLooperCallbacks = false; 181 } 182 return; 183 } 184 185 // Add the file descriptor to the Looper for receiving acknowledegments if the app has 186 // registered for wake-up sensors OR for sending events in the cache. 187 int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL); 188 if (ret == 1) { 189 ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd()); 190 mHasLooperCallbacks = true; 191 } else { 192 ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd()); 193 } 194} 195 196void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) { 197 Mutex::Autolock _l(mConnectionLock); 198 ssize_t index = mSensorInfo.indexOfKey(handle); 199 if (index >= 0) { 200 FlushInfo& flushInfo = mSensorInfo.editValueAt(index); 201 flushInfo.mPendingFlushEventsToSend++; 202 } 203} 204 205status_t SensorService::SensorEventConnection::sendEvents( 206 sensors_event_t const* buffer, size_t numEvents, 207 sensors_event_t* scratch, 208 SensorEventConnection const * const * mapFlushEventsToConnections) { 209 // filter out events not for this connection 210 int count = 0; 211 Mutex::Autolock _l(mConnectionLock); 212 if (scratch) { 213 size_t i=0; 214 while (i<numEvents) { 215 int32_t sensor_handle = buffer[i].sensor; 216 if (buffer[i].type == SENSOR_TYPE_META_DATA) { 217 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", 218 buffer[i].meta_data.sensor); 219 // Setting sensor_handle to the correct sensor to ensure the sensor events per 220 // connection are filtered correctly. buffer[i].sensor is zero for meta_data 221 // events. 222 sensor_handle = buffer[i].meta_data.sensor; 223 } 224 225 ssize_t index = mSensorInfo.indexOfKey(sensor_handle); 226 // Check if this connection has registered for this sensor. If not continue to the 227 // next sensor_event. 228 if (index < 0) { 229 ++i; 230 continue; 231 } 232 233 FlushInfo& flushInfo = mSensorInfo.editValueAt(index); 234 // Check if there is a pending flush_complete event for this sensor on this connection. 235 if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true && 236 this == mapFlushEventsToConnections[i]) { 237 flushInfo.mFirstFlushPending = false; 238 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", 239 buffer[i].meta_data.sensor); 240 ++i; 241 continue; 242 } 243 244 // If there is a pending flush complete event for this sensor on this connection, 245 // ignore the event and proceed to the next. 246 if (flushInfo.mFirstFlushPending) { 247 ++i; 248 continue; 249 } 250 251 do { 252 // Keep copying events into the scratch buffer as long as they are regular 253 // sensor_events are from the same sensor_handle OR they are flush_complete_events 254 // from the same sensor_handle AND the current connection is mapped to the 255 // corresponding flush_complete_event. 256 if (buffer[i].type == SENSOR_TYPE_META_DATA) { 257 if (this == mapFlushEventsToConnections[i]) { 258 scratch[count++] = buffer[i]; 259 } 260 ++i; 261 } else { 262 // Regular sensor event, just copy it to the scratch buffer. 263 scratch[count++] = buffer[i++]; 264 } 265 } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle && 266 buffer[i].type != SENSOR_TYPE_META_DATA) || 267 (buffer[i].type == SENSOR_TYPE_META_DATA && 268 buffer[i].meta_data.sensor == sensor_handle))); 269 } 270 } else { 271 scratch = const_cast<sensors_event_t *>(buffer); 272 count = numEvents; 273 } 274 275 sendPendingFlushEventsLocked(); 276 // Early return if there are no events for this connection. 277 if (count == 0) { 278 return status_t(NO_ERROR); 279 } 280 281#if DEBUG_CONNECTIONS 282 mEventsReceived += count; 283#endif 284 if (mCacheSize != 0) { 285 // There are some events in the cache which need to be sent first. Copy this buffer to 286 // the end of cache. 287 if (mCacheSize + count <= mMaxCacheSize) { 288 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); 289 mCacheSize += count; 290 } else { 291 // Check if any new sensors have registered on this connection which may have increased 292 // the max cache size that is desired. 293 if (mCacheSize + count < computeMaxCacheSizeLocked()) { 294 reAllocateCacheLocked(scratch, count); 295 return status_t(NO_ERROR); 296 } 297 // Some events need to be dropped. 298 int remaningCacheSize = mMaxCacheSize - mCacheSize; 299 if (remaningCacheSize != 0) { 300 memcpy(&mEventCache[mCacheSize], scratch, 301 remaningCacheSize * sizeof(sensors_event_t)); 302 } 303 int numEventsDropped = count - remaningCacheSize; 304 countFlushCompleteEventsLocked(mEventCache, numEventsDropped); 305 // Drop the first "numEventsDropped" in the cache. 306 memmove(mEventCache, &mEventCache[numEventsDropped], 307 (mCacheSize - numEventsDropped) * sizeof(sensors_event_t)); 308 309 // Copy the remainingEvents in scratch buffer to the end of cache. 310 memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize, 311 numEventsDropped * sizeof(sensors_event_t)); 312 } 313 return status_t(NO_ERROR); 314 } 315 316 int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); 317 if (index_wake_up_event >= 0) { 318 scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; 319 ++mWakeLockRefCount; 320#if DEBUG_CONNECTIONS 321 ++mTotalAcksNeeded; 322#endif 323 } 324 325 // NOTE: ASensorEvent and sensors_event_t are the same type. 326 ssize_t size = SensorEventQueue::write(mChannel, 327 reinterpret_cast<ASensorEvent const*>(scratch), count); 328 if (size < 0) { 329 // Write error, copy events to local cache. 330 if (index_wake_up_event >= 0) { 331 // If there was a wake_up sensor_event, reset the flag. 332 scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; 333 if (mWakeLockRefCount > 0) { 334 --mWakeLockRefCount; 335 } 336#if DEBUG_CONNECTIONS 337 --mTotalAcksNeeded; 338#endif 339 } 340 if (mEventCache == NULL) { 341 mMaxCacheSize = computeMaxCacheSizeLocked(); 342 mEventCache = new sensors_event_t[mMaxCacheSize]; 343 mCacheSize = 0; 344 } 345 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); 346 mCacheSize += count; 347 348 // Add this file descriptor to the looper to get a callback when this fd is available for 349 // writing. 350 updateLooperRegistrationLocked(mService->getLooper()); 351 return size; 352 } 353 354#if DEBUG_CONNECTIONS 355 if (size > 0) { 356 mEventsSent += count; 357 } 358#endif 359 360 return size < 0 ? status_t(size) : status_t(NO_ERROR); 361} 362 363void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, 364 int count) { 365 sensors_event_t *eventCache_new; 366 const int new_cache_size = computeMaxCacheSizeLocked(); 367 // Allocate new cache, copy over events from the old cache & scratch, free up memory. 368 eventCache_new = new sensors_event_t[new_cache_size]; 369 memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); 370 memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); 371 372 ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, 373 new_cache_size); 374 375 delete mEventCache; 376 mEventCache = eventCache_new; 377 mCacheSize += count; 378 mMaxCacheSize = new_cache_size; 379} 380 381void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { 382 ASensorEvent flushCompleteEvent; 383 memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent)); 384 flushCompleteEvent.type = SENSOR_TYPE_META_DATA; 385 // Loop through all the sensors for this connection and check if there are any pending 386 // flush complete events to be sent. 387 for (size_t i = 0; i < mSensorInfo.size(); ++i) { 388 FlushInfo& flushInfo = mSensorInfo.editValueAt(i); 389 while (flushInfo.mPendingFlushEventsToSend > 0) { 390 const int sensor_handle = mSensorInfo.keyAt(i); 391 flushCompleteEvent.meta_data.sensor = sensor_handle; 392 bool wakeUpSensor = mService->getSensorFromHandle(sensor_handle).isWakeUpSensor(); 393 if (wakeUpSensor) { 394 ++mWakeLockRefCount; 395 flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; 396 } 397 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); 398 if (size < 0) { 399 if (wakeUpSensor) --mWakeLockRefCount; 400 return; 401 } 402 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", 403 flushCompleteEvent.meta_data.sensor); 404 flushInfo.mPendingFlushEventsToSend--; 405 } 406 } 407} 408 409void SensorService::SensorEventConnection::writeToSocketFromCache() { 410 // At a time write at most half the size of the receiver buffer in SensorEventQueue OR 411 // half the size of the socket buffer allocated in BitTube whichever is smaller. 412 const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2, 413 int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2))); 414 Mutex::Autolock _l(mConnectionLock); 415 // Send pending flush complete events (if any) 416 sendPendingFlushEventsLocked(); 417 for (int numEventsSent = 0; numEventsSent < mCacheSize;) { 418 const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize); 419 int index_wake_up_event = 420 findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); 421 if (index_wake_up_event >= 0) { 422 mEventCache[index_wake_up_event + numEventsSent].flags |= 423 WAKE_UP_SENSOR_EVENT_NEEDS_ACK; 424 ++mWakeLockRefCount; 425#if DEBUG_CONNECTIONS 426 ++mTotalAcksNeeded; 427#endif 428 } 429 430 ssize_t size = SensorEventQueue::write(mChannel, 431 reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), 432 numEventsToWrite); 433 if (size < 0) { 434 if (index_wake_up_event >= 0) { 435 // If there was a wake_up sensor_event, reset the flag. 436 mEventCache[index_wake_up_event + numEventsSent].flags &= 437 ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; 438 if (mWakeLockRefCount > 0) { 439 --mWakeLockRefCount; 440 } 441#if DEBUG_CONNECTIONS 442 --mTotalAcksNeeded; 443#endif 444 } 445 memmove(mEventCache, &mEventCache[numEventsSent], 446 (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); 447 ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", 448 numEventsSent, mCacheSize); 449 mCacheSize -= numEventsSent; 450 return; 451 } 452 numEventsSent += numEventsToWrite; 453#if DEBUG_CONNECTIONS 454 mEventsSentFromCache += numEventsToWrite; 455#endif 456 } 457 ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); 458 // All events from the cache have been sent. Reset cache size to zero. 459 mCacheSize = 0; 460 // There are no more events in the cache. We don't need to poll for write on the fd. 461 // Update Looper registration. 462 updateLooperRegistrationLocked(mService->getLooper()); 463} 464 465void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( 466 sensors_event_t const* scratch, const int numEventsDropped) { 467 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); 468 // Count flushComplete events in the events that are about to the dropped. These will be sent 469 // separately before the next batch of events. 470 for (int j = 0; j < numEventsDropped; ++j) { 471 if (scratch[j].type == SENSOR_TYPE_META_DATA) { 472 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor); 473 flushInfo.mPendingFlushEventsToSend++; 474 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", 475 flushInfo.mPendingFlushEventsToSend); 476 } 477 } 478 return; 479} 480 481int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( 482 sensors_event_t const* scratch, const int count) { 483 for (int i = 0; i < count; ++i) { 484 if (mService->isWakeUpSensorEvent(scratch[i])) { 485 return i; 486 } 487 } 488 return -1; 489} 490 491sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const 492{ 493 return mChannel; 494} 495 496status_t SensorService::SensorEventConnection::enableDisable( 497 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, 498 int reservedFlags) 499{ 500 status_t err; 501 if (enabled) { 502 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, 503 reservedFlags, mOpPackageName); 504 505 } else { 506 err = mService->disable(this, handle); 507 } 508 return err; 509} 510 511status_t SensorService::SensorEventConnection::setEventRate( 512 int handle, nsecs_t samplingPeriodNs) 513{ 514 return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName); 515} 516 517status_t SensorService::SensorEventConnection::flush() { 518 return mService->flushSensor(this, mOpPackageName); 519} 520 521int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) { 522 if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { 523 { 524 // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount, 525 // and remove the fd from Looper. Call checkWakeLockState to know if SensorService 526 // can release the wake-lock. 527 ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd); 528 Mutex::Autolock _l(mConnectionLock); 529 mDead = true; 530 mWakeLockRefCount = 0; 531 updateLooperRegistrationLocked(mService->getLooper()); 532 } 533 mService->checkWakeLockState(); 534 if (mDataInjectionMode) { 535 // If the Looper has encountered some error in data injection mode, reset SensorService 536 // back to normal mode. 537 mService->resetToNormalMode(); 538 mDataInjectionMode = false; 539 } 540 return 1; 541 } 542 543 if (events & ALOOPER_EVENT_INPUT) { 544 unsigned char buf[sizeof(sensors_event_t)]; 545 ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT); 546 { 547 Mutex::Autolock _l(mConnectionLock); 548 if (numBytesRead == sizeof(sensors_event_t)) { 549 if (!mDataInjectionMode) { 550 ALOGE("Data injected in normal mode, dropping event" 551 "package=%s uid=%d", mPackageName.string(), mUid); 552 // Unregister call backs. 553 return 0; 554 } 555 SensorDevice& dev(SensorDevice::getInstance()); 556 sensors_event_t sensor_event; 557 memset(&sensor_event, 0, sizeof(sensor_event)); 558 memcpy(&sensor_event, buf, sizeof(sensors_event_t)); 559 Sensor sensor = mService->getSensorFromHandle(sensor_event.sensor); 560 sensor_event.type = sensor.getType(); 561 dev.injectSensorData(&sensor_event); 562#if DEBUG_CONNECTIONS 563 ++mEventsReceived; 564#endif 565 } else if (numBytesRead == sizeof(uint32_t)) { 566 uint32_t numAcks = 0; 567 memcpy(&numAcks, buf, numBytesRead); 568 // Sanity check to ensure there are no read errors in recv, numAcks is always 569 // within the range and not zero. If any of the above don't hold reset 570 // mWakeLockRefCount to zero. 571 if (numAcks > 0 && numAcks < mWakeLockRefCount) { 572 mWakeLockRefCount -= numAcks; 573 } else { 574 mWakeLockRefCount = 0; 575 } 576#if DEBUG_CONNECTIONS 577 mTotalAcksReceived += numAcks; 578#endif 579 } else { 580 // Read error, reset wakelock refcount. 581 mWakeLockRefCount = 0; 582 } 583 } 584 // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released 585 // here as checkWakeLockState() will need it. 586 if (mWakeLockRefCount == 0) { 587 mService->checkWakeLockState(); 588 } 589 // continue getting callbacks. 590 return 1; 591 } 592 593 if (events & ALOOPER_EVENT_OUTPUT) { 594 // send sensor data that is stored in mEventCache for this connection. 595 mService->sendEventsFromCache(this); 596 } 597 return 1; 598} 599 600int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { 601 size_t fifoWakeUpSensors = 0; 602 size_t fifoNonWakeUpSensors = 0; 603 for (size_t i = 0; i < mSensorInfo.size(); ++i) { 604 const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i)); 605 if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { 606 // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and 607 // non wake_up sensors. 608 if (sensor.isWakeUpSensor()) { 609 fifoWakeUpSensors += sensor.getFifoReservedEventCount(); 610 } else { 611 fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); 612 } 613 } else { 614 // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. 615 if (sensor.isWakeUpSensor()) { 616 fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? 617 fifoWakeUpSensors : sensor.getFifoMaxEventCount(); 618 619 } else { 620 fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? 621 fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); 622 623 } 624 } 625 } 626 if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { 627 // It is extremely unlikely that there is a write failure in non batch mode. Return a cache 628 // size that is equal to that of the batch mode. 629 // ALOGW("Write failure in non-batch mode"); 630 return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); 631 } 632 return fifoWakeUpSensors + fifoNonWakeUpSensors; 633} 634 635} // namespace android 636 637