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