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. |
Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 318 | appendEventsToCacheLocked(scratch, count); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 319 | return status_t(NO_ERROR); |
| 320 | } |
| 321 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 322 | int index_wake_up_event = -1; |
| 323 | if (mHasSensorAccess) { |
| 324 | index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); |
| 325 | if (index_wake_up_event >= 0) { |
| 326 | scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 327 | ++mWakeLockRefCount; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 328 | #if DEBUG_CONNECTIONS |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 329 | ++mTotalAcksNeeded; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 330 | #endif |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 331 | } |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // NOTE: ASensorEvent and sensors_event_t are the same type. |
| 335 | ssize_t size = SensorEventQueue::write(mChannel, |
| 336 | reinterpret_cast<ASensorEvent const*>(scratch), count); |
| 337 | if (size < 0) { |
| 338 | // Write error, copy events to local cache. |
| 339 | if (index_wake_up_event >= 0) { |
| 340 | // If there was a wake_up sensor_event, reset the flag. |
| 341 | scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 342 | if (mWakeLockRefCount > 0) { |
| 343 | --mWakeLockRefCount; |
| 344 | } |
| 345 | #if DEBUG_CONNECTIONS |
| 346 | --mTotalAcksNeeded; |
| 347 | #endif |
| 348 | } |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 349 | if (mEventCache == nullptr) { |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 350 | mMaxCacheSize = computeMaxCacheSizeLocked(); |
| 351 | mEventCache = new sensors_event_t[mMaxCacheSize]; |
| 352 | mCacheSize = 0; |
| 353 | } |
Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 354 | // Save the events so that they can be written later |
| 355 | appendEventsToCacheLocked(scratch, count); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 356 | |
| 357 | // Add this file descriptor to the looper to get a callback when this fd is available for |
| 358 | // writing. |
| 359 | updateLooperRegistrationLocked(mService->getLooper()); |
| 360 | return size; |
| 361 | } |
| 362 | |
| 363 | #if DEBUG_CONNECTIONS |
| 364 | if (size > 0) { |
| 365 | mEventsSent += count; |
| 366 | } |
| 367 | #endif |
| 368 | |
| 369 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 370 | } |
| 371 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 372 | void SensorService::SensorEventConnection::setSensorAccess(const bool hasAccess) { |
| 373 | Mutex::Autolock _l(mConnectionLock); |
| 374 | mHasSensorAccess = hasAccess; |
| 375 | } |
| 376 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 377 | void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, |
| 378 | int count) { |
| 379 | sensors_event_t *eventCache_new; |
| 380 | const int new_cache_size = computeMaxCacheSizeLocked(); |
| 381 | // Allocate new cache, copy over events from the old cache & scratch, free up memory. |
| 382 | eventCache_new = new sensors_event_t[new_cache_size]; |
| 383 | memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); |
| 384 | memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 385 | |
| 386 | ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, |
| 387 | new_cache_size); |
| 388 | |
George Burgess IV | 1866ed4 | 2018-01-21 12:14:09 -0800 | [diff] [blame] | 389 | delete[] mEventCache; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 390 | mEventCache = eventCache_new; |
| 391 | mCacheSize += count; |
| 392 | mMaxCacheSize = new_cache_size; |
| 393 | } |
| 394 | |
Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 395 | void SensorService::SensorEventConnection::appendEventsToCacheLocked(sensors_event_t const* events, |
| 396 | int count) { |
| 397 | if (count <= 0) { |
| 398 | return; |
| 399 | } else if (mCacheSize + count <= mMaxCacheSize) { |
| 400 | // The events fit within the current cache: add them |
| 401 | memcpy(&mEventCache[mCacheSize], events, count * sizeof(sensors_event_t)); |
| 402 | mCacheSize += count; |
| 403 | } else if (mCacheSize + count <= computeMaxCacheSizeLocked()) { |
| 404 | // The events fit within a resized cache: resize the cache and add the events |
| 405 | reAllocateCacheLocked(events, count); |
| 406 | } else { |
| 407 | // The events do not fit within the cache: drop the oldest events. |
| 408 | ALOGW("Dropping events from cache (%d / %d) to save %d newer events", mCacheSize, |
| 409 | mMaxCacheSize, count); |
| 410 | |
| 411 | int freeSpace = mMaxCacheSize - mCacheSize; |
| 412 | |
| 413 | // Drop up to the currently cached number of events to make room for new events |
| 414 | int cachedEventsToDrop = std::min(mCacheSize, count - freeSpace); |
| 415 | |
| 416 | // New events need to be dropped if there are more new events than the size of the cache |
| 417 | int newEventsToDrop = std::max(0, count - mMaxCacheSize); |
| 418 | |
| 419 | // Determine the number of new events to copy into the cache |
| 420 | int eventsToCopy = std::min(mMaxCacheSize, count); |
| 421 | |
| 422 | // Check for any flush complete events in the events that will be dropped |
| 423 | countFlushCompleteEventsLocked(mEventCache, cachedEventsToDrop); |
| 424 | countFlushCompleteEventsLocked(events, newEventsToDrop); |
| 425 | |
| 426 | // Only shift the events if they will not all be overwritten |
| 427 | if (eventsToCopy != mMaxCacheSize) { |
| 428 | memmove(mEventCache, &mEventCache[cachedEventsToDrop], |
| 429 | (mCacheSize - cachedEventsToDrop) * sizeof(sensors_event_t)); |
| 430 | } |
| 431 | mCacheSize -= cachedEventsToDrop; |
| 432 | |
| 433 | // Copy the events into the cache |
| 434 | memcpy(&mEventCache[mCacheSize], &events[newEventsToDrop], |
| 435 | eventsToCopy * sizeof(sensors_event_t)); |
| 436 | mCacheSize += eventsToCopy; |
| 437 | } |
| 438 | } |
| 439 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 440 | void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { |
| 441 | ASensorEvent flushCompleteEvent; |
| 442 | memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent)); |
| 443 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; |
| 444 | // Loop through all the sensors for this connection and check if there are any pending |
| 445 | // flush complete events to be sent. |
| 446 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 447 | const int handle = mSensorInfo.keyAt(i); |
| 448 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle); |
| 449 | if (si == nullptr) { |
| 450 | continue; |
| 451 | } |
| 452 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 453 | FlushInfo& flushInfo = mSensorInfo.editValueAt(i); |
| 454 | while (flushInfo.mPendingFlushEventsToSend > 0) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 455 | flushCompleteEvent.meta_data.sensor = handle; |
| 456 | bool wakeUpSensor = si->getSensor().isWakeUpSensor(); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 457 | if (wakeUpSensor) { |
| 458 | ++mWakeLockRefCount; |
| 459 | flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 460 | } |
| 461 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); |
| 462 | if (size < 0) { |
| 463 | if (wakeUpSensor) --mWakeLockRefCount; |
| 464 | return; |
| 465 | } |
| 466 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", |
| 467 | flushCompleteEvent.meta_data.sensor); |
| 468 | flushInfo.mPendingFlushEventsToSend--; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void SensorService::SensorEventConnection::writeToSocketFromCache() { |
| 474 | // At a time write at most half the size of the receiver buffer in SensorEventQueue OR |
| 475 | // half the size of the socket buffer allocated in BitTube whichever is smaller. |
| 476 | const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2, |
| 477 | int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2))); |
| 478 | Mutex::Autolock _l(mConnectionLock); |
| 479 | // Send pending flush complete events (if any) |
| 480 | sendPendingFlushEventsLocked(); |
| 481 | for (int numEventsSent = 0; numEventsSent < mCacheSize;) { |
| 482 | const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 483 | int index_wake_up_event = -1; |
| 484 | if (mHasSensorAccess) { |
| 485 | index_wake_up_event = |
| 486 | findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); |
| 487 | if (index_wake_up_event >= 0) { |
| 488 | mEventCache[index_wake_up_event + numEventsSent].flags |= |
| 489 | WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 490 | ++mWakeLockRefCount; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 491 | #if DEBUG_CONNECTIONS |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 492 | ++mTotalAcksNeeded; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 493 | #endif |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 494 | } |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | ssize_t size = SensorEventQueue::write(mChannel, |
| 498 | reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), |
| 499 | numEventsToWrite); |
| 500 | if (size < 0) { |
| 501 | if (index_wake_up_event >= 0) { |
| 502 | // If there was a wake_up sensor_event, reset the flag. |
| 503 | mEventCache[index_wake_up_event + numEventsSent].flags &= |
| 504 | ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 505 | if (mWakeLockRefCount > 0) { |
| 506 | --mWakeLockRefCount; |
| 507 | } |
| 508 | #if DEBUG_CONNECTIONS |
| 509 | --mTotalAcksNeeded; |
| 510 | #endif |
| 511 | } |
| 512 | memmove(mEventCache, &mEventCache[numEventsSent], |
| 513 | (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); |
| 514 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", |
| 515 | numEventsSent, mCacheSize); |
| 516 | mCacheSize -= numEventsSent; |
| 517 | return; |
| 518 | } |
| 519 | numEventsSent += numEventsToWrite; |
| 520 | #if DEBUG_CONNECTIONS |
| 521 | mEventsSentFromCache += numEventsToWrite; |
| 522 | #endif |
| 523 | } |
| 524 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); |
| 525 | // All events from the cache have been sent. Reset cache size to zero. |
| 526 | mCacheSize = 0; |
| 527 | // There are no more events in the cache. We don't need to poll for write on the fd. |
| 528 | // Update Looper registration. |
| 529 | updateLooperRegistrationLocked(mService->getLooper()); |
| 530 | } |
| 531 | |
| 532 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( |
| 533 | sensors_event_t const* scratch, const int numEventsDropped) { |
| 534 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); |
| 535 | // Count flushComplete events in the events that are about to the dropped. These will be sent |
| 536 | // separately before the next batch of events. |
| 537 | for (int j = 0; j < numEventsDropped; ++j) { |
| 538 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { |
Peng Xu | 63fbab8 | 2017-06-20 12:41:33 -0700 | [diff] [blame] | 539 | ssize_t index = mSensorInfo.indexOfKey(scratch[j].meta_data.sensor); |
| 540 | if (index < 0) { |
| 541 | ALOGW("%s: sensor 0x%x is not found in connection", |
| 542 | __func__, scratch[j].meta_data.sensor); |
| 543 | continue; |
| 544 | } |
| 545 | |
| 546 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 547 | flushInfo.mPendingFlushEventsToSend++; |
| 548 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", |
| 549 | flushInfo.mPendingFlushEventsToSend); |
| 550 | } |
| 551 | } |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( |
| 556 | sensors_event_t const* scratch, const int count) { |
| 557 | for (int i = 0; i < count; ++i) { |
| 558 | if (mService->isWakeUpSensorEvent(scratch[i])) { |
| 559 | return i; |
| 560 | } |
| 561 | } |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
| 566 | { |
| 567 | return mChannel; |
| 568 | } |
| 569 | |
| 570 | status_t SensorService::SensorEventConnection::enableDisable( |
| 571 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, |
| 572 | int reservedFlags) |
| 573 | { |
| 574 | status_t err; |
| 575 | if (enabled) { |
| 576 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, |
| 577 | reservedFlags, mOpPackageName); |
| 578 | |
| 579 | } else { |
| 580 | err = mService->disable(this, handle); |
| 581 | } |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | status_t SensorService::SensorEventConnection::setEventRate( |
| 586 | int handle, nsecs_t samplingPeriodNs) |
| 587 | { |
| 588 | return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName); |
| 589 | } |
| 590 | |
| 591 | status_t SensorService::SensorEventConnection::flush() { |
| 592 | return mService->flushSensor(this, mOpPackageName); |
| 593 | } |
| 594 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 595 | int32_t SensorService::SensorEventConnection::configureChannel(int handle, int rateLevel) { |
| 596 | // SensorEventConnection does not support configureChannel, parameters not used |
| 597 | UNUSED(handle); |
| 598 | UNUSED(rateLevel); |
| 599 | return INVALID_OPERATION; |
| 600 | } |
| 601 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 602 | int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) { |
| 603 | if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { |
| 604 | { |
| 605 | // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount, |
| 606 | // and remove the fd from Looper. Call checkWakeLockState to know if SensorService |
| 607 | // can release the wake-lock. |
| 608 | ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd); |
| 609 | Mutex::Autolock _l(mConnectionLock); |
| 610 | mDead = true; |
| 611 | mWakeLockRefCount = 0; |
| 612 | updateLooperRegistrationLocked(mService->getLooper()); |
| 613 | } |
| 614 | mService->checkWakeLockState(); |
| 615 | if (mDataInjectionMode) { |
| 616 | // If the Looper has encountered some error in data injection mode, reset SensorService |
| 617 | // back to normal mode. |
| 618 | mService->resetToNormalMode(); |
| 619 | mDataInjectionMode = false; |
| 620 | } |
| 621 | return 1; |
| 622 | } |
| 623 | |
| 624 | if (events & ALOOPER_EVENT_INPUT) { |
| 625 | unsigned char buf[sizeof(sensors_event_t)]; |
| 626 | ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT); |
| 627 | { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 628 | Mutex::Autolock _l(mConnectionLock); |
| 629 | if (numBytesRead == sizeof(sensors_event_t)) { |
| 630 | if (!mDataInjectionMode) { |
| 631 | ALOGE("Data injected in normal mode, dropping event" |
| 632 | "package=%s uid=%d", mPackageName.string(), mUid); |
| 633 | // Unregister call backs. |
| 634 | return 0; |
| 635 | } |
| 636 | sensors_event_t sensor_event; |
| 637 | memcpy(&sensor_event, buf, sizeof(sensors_event_t)); |
| 638 | sp<SensorInterface> si = |
| 639 | mService->getSensorInterfaceFromHandle(sensor_event.sensor); |
| 640 | if (si == nullptr) { |
| 641 | return 1; |
| 642 | } |
| 643 | |
| 644 | SensorDevice& dev(SensorDevice::getInstance()); |
| 645 | sensor_event.type = si->getSensor().getType(); |
| 646 | dev.injectSensorData(&sensor_event); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 647 | #if DEBUG_CONNECTIONS |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 648 | ++mEventsReceived; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 649 | #endif |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 650 | } else if (numBytesRead == sizeof(uint32_t)) { |
| 651 | uint32_t numAcks = 0; |
| 652 | memcpy(&numAcks, buf, numBytesRead); |
| 653 | // Sanity check to ensure there are no read errors in recv, numAcks is always |
| 654 | // within the range and not zero. If any of the above don't hold reset |
| 655 | // mWakeLockRefCount to zero. |
| 656 | if (numAcks > 0 && numAcks < mWakeLockRefCount) { |
| 657 | mWakeLockRefCount -= numAcks; |
| 658 | } else { |
| 659 | mWakeLockRefCount = 0; |
| 660 | } |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 661 | #if DEBUG_CONNECTIONS |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 662 | mTotalAcksReceived += numAcks; |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 663 | #endif |
| 664 | } else { |
| 665 | // Read error, reset wakelock refcount. |
| 666 | mWakeLockRefCount = 0; |
| 667 | } |
| 668 | } |
| 669 | // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released |
| 670 | // here as checkWakeLockState() will need it. |
| 671 | if (mWakeLockRefCount == 0) { |
| 672 | mService->checkWakeLockState(); |
| 673 | } |
| 674 | // continue getting callbacks. |
| 675 | return 1; |
| 676 | } |
| 677 | |
| 678 | if (events & ALOOPER_EVENT_OUTPUT) { |
| 679 | // send sensor data that is stored in mEventCache for this connection. |
| 680 | mService->sendEventsFromCache(this); |
| 681 | } |
| 682 | return 1; |
| 683 | } |
| 684 | |
| 685 | int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { |
| 686 | size_t fifoWakeUpSensors = 0; |
| 687 | size_t fifoNonWakeUpSensors = 0; |
| 688 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 689 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(mSensorInfo.keyAt(i)); |
| 690 | if (si == nullptr) { |
| 691 | continue; |
| 692 | } |
| 693 | const Sensor& sensor = si->getSensor(); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 694 | if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { |
| 695 | // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and |
| 696 | // non wake_up sensors. |
| 697 | if (sensor.isWakeUpSensor()) { |
| 698 | fifoWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 699 | } else { |
| 700 | fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 701 | } |
| 702 | } else { |
| 703 | // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. |
| 704 | if (sensor.isWakeUpSensor()) { |
| 705 | fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 706 | fifoWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 707 | |
| 708 | } else { |
| 709 | fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 710 | fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 711 | |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { |
| 716 | // It is extremely unlikely that there is a write failure in non batch mode. Return a cache |
| 717 | // size that is equal to that of the batch mode. |
| 718 | // ALOGW("Write failure in non-batch mode"); |
| 719 | return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); |
| 720 | } |
| 721 | return fifoWakeUpSensors + fifoNonWakeUpSensors; |
| 722 | } |
| 723 | |
| 724 | } // namespace android |
| 725 | |