| 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 |  | 
| Arthur Ishiguro | 930b583 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 17 | #include <log/log.h> | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 18 | #include <sys/socket.h> | 
|  | 19 | #include <utils/threads.h> | 
|  | 20 |  | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 21 | #include <android/util/ProtoOutputStream.h> | 
|  | 22 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 23 | #include <sensor/SensorEventQueue.h> | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 24 |  | 
|  | 25 | #include "vec.h" | 
|  | 26 | #include "SensorEventConnection.h" | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 27 | #include "SensorDevice.h" | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 28 |  | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 29 | #define UNUSED(x) (void)(x) | 
|  | 30 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 31 | namespace android { | 
| Anthony Stange | 07eb421 | 2020-08-28 14:50:28 -0400 | [diff] [blame] | 32 | namespace { | 
|  | 33 |  | 
|  | 34 | // Used as the default value for the target SDK until it's obtained via getTargetSdkVersion. | 
|  | 35 | constexpr int kTargetSdkUnknown = 0; | 
|  | 36 |  | 
|  | 37 | }  // namespace | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | SensorService::SensorEventConnection::SensorEventConnection( | 
|  | 40 | const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode, | 
| Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 41 | const String16& opPackageName) | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 42 | : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false), | 
| Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 43 | mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(nullptr), | 
| Brian Stack | ae4053f | 2018-12-10 14:54:18 -0800 | [diff] [blame] | 44 | mCacheSize(0), mMaxCacheSize(0), mTimeOfLastEventDrop(0), mEventsDropped(0), | 
| Anthony Stange | 07eb421 | 2020-08-28 14:50:28 -0400 | [diff] [blame] | 45 | mPackageName(packageName), mOpPackageName(opPackageName), mTargetSdk(kTargetSdkUnknown), | 
|  | 46 | mDestroyed(false) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 47 | mChannel = new BitTube(mService->mSocketBufferSize); | 
|  | 48 | #if DEBUG_CONNECTIONS | 
|  | 49 | mEventsReceived = mEventsSentFromCache = mEventsSent = 0; | 
|  | 50 | mTotalAcksNeeded = mTotalAcksReceived = 0; | 
|  | 51 | #endif | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | SensorService::SensorEventConnection::~SensorEventConnection() { | 
|  | 55 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 56 | destroy(); | 
| 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 | } | 
| Arthur Ishiguro | 930b583 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
|  | 63 | void SensorService::SensorEventConnection::destroy() { | 
| Peng Xu | 8cbefd7 | 2017-07-10 16:41:08 -0700 | [diff] [blame] | 64 | mDestroyed = true; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | void SensorService::SensorEventConnection::onFirstRef() { | 
|  | 68 | LooperCallback::onFirstRef(); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | bool SensorService::SensorEventConnection::needsWakeLock() { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 72 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 73 | return !mDead && mWakeLockRefCount > 0; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | void SensorService::SensorEventConnection::resetWakeLockRefCount() { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 77 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 78 | mWakeLockRefCount = 0; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | void SensorService::SensorEventConnection::dump(String8& result) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 82 | Mutex::Autolock _l(mConnectionLock); | 
| Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 83 | result.appendFormat("\tOperating Mode: "); | 
|  | 84 | if (!mService->isWhiteListedPackage(getPackageName())) { | 
|  | 85 | result.append("RESTRICTED\n"); | 
|  | 86 | } else if (mDataInjectionMode) { | 
|  | 87 | result.append("DATA_INJECTION\n"); | 
|  | 88 | } else { | 
|  | 89 | result.append("NORMAL\n"); | 
|  | 90 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 91 | result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | " | 
|  | 92 | "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize, | 
|  | 93 | mMaxCacheSize); | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 94 | for (auto& it : mSensorInfo) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 95 | const FlushInfo& flushInfo = it.second; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 96 | result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n", | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 97 | mService->getSensorName(it.first).string(), | 
|  | 98 | it.first, | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 99 | flushInfo.mFirstFlushPending ? "First flush pending" : | 
|  | 100 | "active", | 
|  | 101 | flushInfo.mPendingFlushEventsToSend); | 
|  | 102 | } | 
|  | 103 | #if DEBUG_CONNECTIONS | 
|  | 104 | result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |" | 
|  | 105 | " total_acks_needed %d | total_acks_recvd %d\n", | 
|  | 106 | mEventsReceived, | 
|  | 107 | mEventsSent, | 
|  | 108 | mEventsSentFromCache, | 
|  | 109 | mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize), | 
|  | 110 | mTotalAcksNeeded, | 
|  | 111 | mTotalAcksReceived); | 
|  | 112 | #endif | 
|  | 113 | } | 
|  | 114 |  | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 115 | /** | 
|  | 116 | * Dump debugging information as android.service.SensorEventConnectionProto protobuf message using | 
|  | 117 | * ProtoOutputStream. | 
|  | 118 | * | 
|  | 119 | * See proto definition and some notes about ProtoOutputStream in | 
|  | 120 | * frameworks/base/core/proto/android/service/sensor_service.proto | 
|  | 121 | */ | 
|  | 122 | void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto) const { | 
|  | 123 | using namespace service::SensorEventConnectionProto; | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 124 | Mutex::Autolock _l(mConnectionLock); | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 125 |  | 
|  | 126 | if (!mService->isWhiteListedPackage(getPackageName())) { | 
|  | 127 | proto->write(OPERATING_MODE, OP_MODE_RESTRICTED); | 
|  | 128 | } else if (mDataInjectionMode) { | 
|  | 129 | proto->write(OPERATING_MODE, OP_MODE_DATA_INJECTION); | 
|  | 130 | } else { | 
|  | 131 | proto->write(OPERATING_MODE, OP_MODE_NORMAL); | 
|  | 132 | } | 
|  | 133 | proto->write(PACKAGE_NAME, std::string(mPackageName.string())); | 
|  | 134 | proto->write(WAKE_LOCK_REF_COUNT, int32_t(mWakeLockRefCount)); | 
|  | 135 | proto->write(UID, int32_t(mUid)); | 
|  | 136 | proto->write(CACHE_SIZE, int32_t(mCacheSize)); | 
|  | 137 | proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize)); | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 138 | for (auto& it : mSensorInfo) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 139 | const FlushInfo& flushInfo = it.second; | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 140 | const uint64_t token = proto->start(FLUSH_INFOS); | 
|  | 141 | proto->write(FlushInfoProto::SENSOR_NAME, | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 142 | std::string(mService->getSensorName(it.first))); | 
|  | 143 | proto->write(FlushInfoProto::SENSOR_HANDLE, it.first); | 
| Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 144 | proto->write(FlushInfoProto::FIRST_FLUSH_PENDING, flushInfo.mFirstFlushPending); | 
|  | 145 | proto->write(FlushInfoProto::PENDING_FLUSH_EVENTS_TO_SEND, | 
|  | 146 | flushInfo.mPendingFlushEventsToSend); | 
|  | 147 | proto->end(token); | 
|  | 148 | } | 
|  | 149 | #if DEBUG_CONNECTIONS | 
|  | 150 | proto->write(EVENTS_RECEIVED, mEventsReceived); | 
|  | 151 | proto->write(EVENTS_SENT, mEventsSent); | 
|  | 152 | proto->write(EVENTS_CACHE, mEventsSentFromCache); | 
|  | 153 | proto->write(EVENTS_DROPPED, mEventsReceived - (mEventsSentFromCache + mEventsSent + | 
|  | 154 | mCacheSize)); | 
|  | 155 | proto->write(TOTAL_ACKS_NEEDED, mTotalAcksNeeded); | 
|  | 156 | proto->write(TOTAL_ACKS_RECEIVED, mTotalAcksReceived); | 
|  | 157 | #endif | 
|  | 158 | } | 
|  | 159 |  | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 160 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { | 
|  | 161 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 162 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle); | 
|  | 163 | if (si == nullptr || | 
|  | 164 | !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) || | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 165 | mSensorInfo.count(handle) > 0) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 166 | return false; | 
|  | 167 | } | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 168 | mSensorInfo[handle] = FlushInfo(); | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 169 | return true; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 173 | Mutex::Autolock _l(mConnectionLock); | 
|  | 174 | if (mSensorInfo.erase(handle) >= 0) { | 
|  | 175 | return true; | 
|  | 176 | } | 
|  | 177 | return false; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 180 | std::vector<int32_t> SensorService::SensorEventConnection::getActiveSensorHandles() const { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 181 | Mutex::Autolock _l(mConnectionLock); | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 182 | std::vector<int32_t> list; | 
|  | 183 | for (auto& it : mSensorInfo) { | 
|  | 184 | list.push_back(it.first); | 
|  | 185 | } | 
|  | 186 | return list; | 
|  | 187 | } | 
|  | 188 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 189 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 190 | Mutex::Autolock _l(mConnectionLock); | 
|  | 191 | return mSensorInfo.count(handle) > 0; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
|  | 194 | bool SensorService::SensorEventConnection::hasAnySensor() const { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 195 | Mutex::Autolock _l(mConnectionLock); | 
|  | 196 | return mSensorInfo.size() ? true : false; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
|  | 199 | bool SensorService::SensorEventConnection::hasOneShotSensors() const { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 200 | Mutex::Autolock _l(mConnectionLock); | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 201 | for (auto &it : mSensorInfo) { | 
|  | 202 | const int handle = it.first; | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 203 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle); | 
|  | 204 | if (si != nullptr && si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 205 | return true; | 
|  | 206 | } | 
|  | 207 | } | 
|  | 208 | return false; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | String8 SensorService::SensorEventConnection::getPackageName() const { | 
|  | 212 | return mPackageName; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, | 
|  | 216 | bool value) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 217 | Mutex::Autolock _l(mConnectionLock); | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 218 | if (mSensorInfo.count(handle) > 0) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 219 | FlushInfo& flushInfo = mSensorInfo[handle]; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 220 | flushInfo.mFirstFlushPending = value; | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 225 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 226 | updateLooperRegistrationLocked(looper); | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | void SensorService::SensorEventConnection::updateLooperRegistrationLocked( | 
|  | 230 | const sp<Looper>& looper) { | 
|  | 231 | bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) || | 
|  | 232 | mDataInjectionMode; | 
|  | 233 | // If all sensors are unregistered OR Looper has encountered an error, we can remove the Fd from | 
|  | 234 | // the Looper if it has been previously added. | 
|  | 235 | if (!isConnectionActive || mDead) { if (mHasLooperCallbacks) { | 
|  | 236 | ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, | 
|  | 237 | mChannel->getSendFd()); | 
|  | 238 | looper->removeFd(mChannel->getSendFd()); mHasLooperCallbacks = false; } | 
|  | 239 | return; } | 
|  | 240 |  | 
|  | 241 | int looper_flags = 0; | 
|  | 242 | if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT; | 
|  | 243 | if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT; | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 244 | for (auto& it : mSensorInfo) { | 
|  | 245 | const int handle = it.first; | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 246 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle); | 
|  | 247 | if (si != nullptr && si->getSensor().isWakeUpSensor()) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 248 | looper_flags |= ALOOPER_EVENT_INPUT; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 249 | } | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | // If flags is still set to zero, we don't need to add this fd to the Looper, if the fd has | 
|  | 253 | // already been added, remove it. This is likely to happen when ALL the events stored in the | 
|  | 254 | // cache have been sent to the corresponding app. | 
|  | 255 | if (looper_flags == 0) { | 
|  | 256 | if (mHasLooperCallbacks) { | 
|  | 257 | ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd()); | 
|  | 258 | looper->removeFd(mChannel->getSendFd()); | 
|  | 259 | mHasLooperCallbacks = false; | 
|  | 260 | } | 
|  | 261 | return; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | // Add the file descriptor to the Looper for receiving acknowledegments if the app has | 
|  | 265 | // registered for wake-up sensors OR for sending events in the cache. | 
| Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 266 | int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, nullptr); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 267 | if (ret == 1) { | 
|  | 268 | ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd()); | 
|  | 269 | mHasLooperCallbacks = true; | 
|  | 270 | } else { | 
|  | 271 | ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd()); | 
|  | 272 | } | 
|  | 273 | } | 
|  | 274 |  | 
| Stan Rokita | 29adc8c | 2020-07-06 17:38:03 -0700 | [diff] [blame] | 275 | bool SensorService::SensorEventConnection::incrementPendingFlushCountIfHasAccess(int32_t handle) { | 
|  | 276 | if (hasSensorAccess()) { | 
|  | 277 | Mutex::Autolock _l(mConnectionLock); | 
|  | 278 | if (mSensorInfo.count(handle) > 0) { | 
|  | 279 | FlushInfo& flushInfo = mSensorInfo[handle]; | 
|  | 280 | flushInfo.mPendingFlushEventsToSend++; | 
|  | 281 | } | 
|  | 282 | return true; | 
|  | 283 | } else { | 
|  | 284 | return false; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 285 | } | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | status_t SensorService::SensorEventConnection::sendEvents( | 
|  | 289 | sensors_event_t const* buffer, size_t numEvents, | 
|  | 290 | sensors_event_t* scratch, | 
| Peng Xu | ded526e | 2016-08-12 16:39:44 -0700 | [diff] [blame] | 291 | wp<const SensorEventConnection> const * mapFlushEventsToConnections) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 292 | // filter out events not for this connection | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 293 |  | 
| George Burgess IV | 1866ed4 | 2018-01-21 12:14:09 -0800 | [diff] [blame] | 294 | std::unique_ptr<sensors_event_t[]> sanitizedBuffer; | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 295 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 296 | int count = 0; | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 297 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 298 | if (scratch) { | 
|  | 299 | size_t i=0; | 
|  | 300 | while (i<numEvents) { | 
|  | 301 | int32_t sensor_handle = buffer[i].sensor; | 
|  | 302 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { | 
|  | 303 | ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", | 
|  | 304 | buffer[i].meta_data.sensor); | 
|  | 305 | // Setting sensor_handle to the correct sensor to ensure the sensor events per | 
|  | 306 | // connection are filtered correctly.  buffer[i].sensor is zero for meta_data | 
|  | 307 | // events. | 
|  | 308 | sensor_handle = buffer[i].meta_data.sensor; | 
|  | 309 | } | 
|  | 310 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 311 | // Check if this connection has registered for this sensor. If not continue to the | 
|  | 312 | // next sensor_event. | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 313 | if (mSensorInfo.count(sensor_handle) == 0) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 314 | ++i; | 
|  | 315 | continue; | 
|  | 316 | } | 
|  | 317 |  | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 318 | FlushInfo& flushInfo = mSensorInfo[sensor_handle]; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 319 | // Check if there is a pending flush_complete event for this sensor on this connection. | 
|  | 320 | if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true && | 
| Peng Xu | ded526e | 2016-08-12 16:39:44 -0700 | [diff] [blame] | 321 | mapFlushEventsToConnections[i] == this) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 322 | flushInfo.mFirstFlushPending = false; | 
|  | 323 | ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", | 
|  | 324 | buffer[i].meta_data.sensor); | 
|  | 325 | ++i; | 
|  | 326 | continue; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | // If there is a pending flush complete event for this sensor on this connection, | 
|  | 330 | // ignore the event and proceed to the next. | 
|  | 331 | if (flushInfo.mFirstFlushPending) { | 
|  | 332 | ++i; | 
|  | 333 | continue; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | do { | 
|  | 337 | // Keep copying events into the scratch buffer as long as they are regular | 
|  | 338 | // sensor_events are from the same sensor_handle OR they are flush_complete_events | 
|  | 339 | // from the same sensor_handle AND the current connection is mapped to the | 
|  | 340 | // corresponding flush_complete_event. | 
|  | 341 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { | 
| Peng Xu | ded526e | 2016-08-12 16:39:44 -0700 | [diff] [blame] | 342 | if (mapFlushEventsToConnections[i] == this) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 343 | scratch[count++] = buffer[i]; | 
|  | 344 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 345 | } else { | 
| Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 346 | // Regular sensor event, just copy it to the scratch buffer after checking | 
|  | 347 | // the AppOp. | 
|  | 348 | if (hasSensorAccess() && noteOpIfRequired(buffer[i])) { | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 349 | scratch[count++] = buffer[i]; | 
|  | 350 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 351 | } | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 352 | i++; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 353 | } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle && | 
|  | 354 | buffer[i].type != SENSOR_TYPE_META_DATA) || | 
|  | 355 | (buffer[i].type == SENSOR_TYPE_META_DATA  && | 
|  | 356 | buffer[i].meta_data.sensor == sensor_handle))); | 
|  | 357 | } | 
|  | 358 | } else { | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 359 | if (hasSensorAccess()) { | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 360 | scratch = const_cast<sensors_event_t *>(buffer); | 
|  | 361 | count = numEvents; | 
|  | 362 | } else { | 
| George Burgess IV | 1866ed4 | 2018-01-21 12:14:09 -0800 | [diff] [blame] | 363 | sanitizedBuffer.reset(new sensors_event_t[numEvents]); | 
|  | 364 | scratch = sanitizedBuffer.get(); | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 365 | for (size_t i = 0; i < numEvents; i++) { | 
|  | 366 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { | 
|  | 367 | scratch[count++] = buffer[i++]; | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 371 | } | 
|  | 372 |  | 
|  | 373 | sendPendingFlushEventsLocked(); | 
|  | 374 | // Early return if there are no events for this connection. | 
|  | 375 | if (count == 0) { | 
|  | 376 | return status_t(NO_ERROR); | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | #if DEBUG_CONNECTIONS | 
|  | 380 | mEventsReceived += count; | 
|  | 381 | #endif | 
|  | 382 | if (mCacheSize != 0) { | 
|  | 383 | // There are some events in the cache which need to be sent first. Copy this buffer to | 
|  | 384 | // the end of cache. | 
| Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 385 | appendEventsToCacheLocked(scratch, count); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 386 | return status_t(NO_ERROR); | 
|  | 387 | } | 
|  | 388 |  | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 389 | int index_wake_up_event = -1; | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 390 | if (hasSensorAccess()) { | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 391 | index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); | 
|  | 392 | if (index_wake_up_event >= 0) { | 
|  | 393 | scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 394 | ++mWakeLockRefCount; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 395 | #if DEBUG_CONNECTIONS | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 396 | ++mTotalAcksNeeded; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 397 | #endif | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 398 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
|  | 401 | // NOTE: ASensorEvent and sensors_event_t are the same type. | 
|  | 402 | ssize_t size = SensorEventQueue::write(mChannel, | 
|  | 403 | reinterpret_cast<ASensorEvent const*>(scratch), count); | 
|  | 404 | if (size < 0) { | 
|  | 405 | // Write error, copy events to local cache. | 
|  | 406 | if (index_wake_up_event >= 0) { | 
|  | 407 | // If there was a wake_up sensor_event, reset the flag. | 
|  | 408 | scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 409 | if (mWakeLockRefCount > 0) { | 
|  | 410 | --mWakeLockRefCount; | 
|  | 411 | } | 
|  | 412 | #if DEBUG_CONNECTIONS | 
|  | 413 | --mTotalAcksNeeded; | 
|  | 414 | #endif | 
|  | 415 | } | 
| Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 416 | if (mEventCache == nullptr) { | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 417 | mMaxCacheSize = computeMaxCacheSizeLocked(); | 
|  | 418 | mEventCache = new sensors_event_t[mMaxCacheSize]; | 
|  | 419 | mCacheSize = 0; | 
|  | 420 | } | 
| Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 421 | // Save the events so that they can be written later | 
|  | 422 | appendEventsToCacheLocked(scratch, count); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 423 |  | 
|  | 424 | // Add this file descriptor to the looper to get a callback when this fd is available for | 
|  | 425 | // writing. | 
|  | 426 | updateLooperRegistrationLocked(mService->getLooper()); | 
|  | 427 | return size; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | #if DEBUG_CONNECTIONS | 
|  | 431 | if (size > 0) { | 
|  | 432 | mEventsSent += count; | 
|  | 433 | } | 
|  | 434 | #endif | 
|  | 435 |  | 
|  | 436 | return size < 0 ? status_t(size) : status_t(NO_ERROR); | 
|  | 437 | } | 
|  | 438 |  | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 439 | bool SensorService::SensorEventConnection::hasSensorAccess() { | 
| Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 440 | return mService->isUidActive(mUid) | 
|  | 441 | && !mService->mSensorPrivacyPolicy->isSensorPrivacyEnabled(); | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 444 | bool SensorService::SensorEventConnection::noteOpIfRequired(const sensors_event_t& event) { | 
|  | 445 | bool success = true; | 
|  | 446 | const auto iter = mHandleToAppOp.find(event.sensor); | 
|  | 447 | if (iter != mHandleToAppOp.end()) { | 
| Anthony Stange | 07eb421 | 2020-08-28 14:50:28 -0400 | [diff] [blame] | 448 | if (mTargetSdk == kTargetSdkUnknown) { | 
|  | 449 | // getTargetSdkVersion returns -1 if it fails so this operation should only be run once | 
|  | 450 | // per connection and then cached. Perform this here as opposed to in the constructor to | 
|  | 451 | // avoid log spam for NDK/VNDK clients that don't use sensors guarded with permissions | 
|  | 452 | // and pass in invalid op package names. | 
|  | 453 | mTargetSdk = SensorService::getTargetSdkVersion(mOpPackageName); | 
|  | 454 | } | 
|  | 455 |  | 
| Brian Duddie | 457e639 | 2020-06-19 11:38:29 -0700 | [diff] [blame] | 456 | // Special handling for step count/detect backwards compatibility: if the app's target SDK | 
|  | 457 | // is pre-Q, still permit delivering events to the app even if permission isn't granted | 
|  | 458 | // (since this permission was only introduced in Q) | 
|  | 459 | if ((event.type == SENSOR_TYPE_STEP_COUNTER || event.type == SENSOR_TYPE_STEP_DETECTOR) && | 
|  | 460 | mTargetSdk > 0 && mTargetSdk <= __ANDROID_API_P__) { | 
|  | 461 | success = true; | 
|  | 462 | } else { | 
|  | 463 | int32_t appOpMode = mService->sAppOpsManager.noteOp(iter->second, mUid, | 
|  | 464 | mOpPackageName); | 
|  | 465 | success = (appOpMode == AppOpsManager::MODE_ALLOWED); | 
|  | 466 | } | 
| Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 467 | } | 
|  | 468 | return success; | 
|  | 469 | } | 
|  | 470 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 471 | void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, | 
|  | 472 | int count) { | 
|  | 473 | sensors_event_t *eventCache_new; | 
|  | 474 | const int new_cache_size = computeMaxCacheSizeLocked(); | 
|  | 475 | // Allocate new cache, copy over events from the old cache & scratch, free up memory. | 
|  | 476 | eventCache_new = new sensors_event_t[new_cache_size]; | 
|  | 477 | memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); | 
|  | 478 | memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); | 
|  | 479 |  | 
|  | 480 | ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, | 
|  | 481 | new_cache_size); | 
|  | 482 |  | 
| George Burgess IV | 1866ed4 | 2018-01-21 12:14:09 -0800 | [diff] [blame] | 483 | delete[] mEventCache; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 484 | mEventCache = eventCache_new; | 
|  | 485 | mCacheSize += count; | 
|  | 486 | mMaxCacheSize = new_cache_size; | 
|  | 487 | } | 
|  | 488 |  | 
| Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 489 | void SensorService::SensorEventConnection::appendEventsToCacheLocked(sensors_event_t const* events, | 
|  | 490 | int count) { | 
|  | 491 | if (count <= 0) { | 
|  | 492 | return; | 
|  | 493 | } else if (mCacheSize + count <= mMaxCacheSize) { | 
|  | 494 | // The events fit within the current cache: add them | 
|  | 495 | memcpy(&mEventCache[mCacheSize], events, count * sizeof(sensors_event_t)); | 
|  | 496 | mCacheSize += count; | 
|  | 497 | } else if (mCacheSize + count <= computeMaxCacheSizeLocked()) { | 
|  | 498 | // The events fit within a resized cache: resize the cache and add the events | 
|  | 499 | reAllocateCacheLocked(events, count); | 
|  | 500 | } else { | 
|  | 501 | // The events do not fit within the cache: drop the oldest events. | 
| Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 502 | int freeSpace = mMaxCacheSize - mCacheSize; | 
|  | 503 |  | 
|  | 504 | // Drop up to the currently cached number of events to make room for new events | 
|  | 505 | int cachedEventsToDrop = std::min(mCacheSize, count - freeSpace); | 
|  | 506 |  | 
|  | 507 | // New events need to be dropped if there are more new events than the size of the cache | 
|  | 508 | int newEventsToDrop = std::max(0, count - mMaxCacheSize); | 
|  | 509 |  | 
|  | 510 | // Determine the number of new events to copy into the cache | 
|  | 511 | int eventsToCopy = std::min(mMaxCacheSize, count); | 
|  | 512 |  | 
| Brian Stack | ae4053f | 2018-12-10 14:54:18 -0800 | [diff] [blame] | 513 | constexpr nsecs_t kMinimumTimeBetweenDropLogNs = 2 * 1000 * 1000 * 1000; // 2 sec | 
|  | 514 | if (events[0].timestamp - mTimeOfLastEventDrop > kMinimumTimeBetweenDropLogNs) { | 
|  | 515 | ALOGW("Dropping %d cached events (%d/%d) to save %d/%d new events. %d events previously" | 
|  | 516 | " dropped", cachedEventsToDrop, mCacheSize, mMaxCacheSize, eventsToCopy, | 
|  | 517 | count, mEventsDropped); | 
|  | 518 | mEventsDropped = 0; | 
|  | 519 | mTimeOfLastEventDrop = events[0].timestamp; | 
|  | 520 | } else { | 
|  | 521 | // Record the number dropped | 
|  | 522 | mEventsDropped += cachedEventsToDrop + newEventsToDrop; | 
|  | 523 | } | 
|  | 524 |  | 
| Brian Stack | 93432ad | 2018-11-27 18:28:48 -0800 | [diff] [blame] | 525 | // Check for any flush complete events in the events that will be dropped | 
|  | 526 | countFlushCompleteEventsLocked(mEventCache, cachedEventsToDrop); | 
|  | 527 | countFlushCompleteEventsLocked(events, newEventsToDrop); | 
|  | 528 |  | 
|  | 529 | // Only shift the events if they will not all be overwritten | 
|  | 530 | if (eventsToCopy != mMaxCacheSize) { | 
|  | 531 | memmove(mEventCache, &mEventCache[cachedEventsToDrop], | 
|  | 532 | (mCacheSize - cachedEventsToDrop) * sizeof(sensors_event_t)); | 
|  | 533 | } | 
|  | 534 | mCacheSize -= cachedEventsToDrop; | 
|  | 535 |  | 
|  | 536 | // Copy the events into the cache | 
|  | 537 | memcpy(&mEventCache[mCacheSize], &events[newEventsToDrop], | 
|  | 538 | eventsToCopy * sizeof(sensors_event_t)); | 
|  | 539 | mCacheSize += eventsToCopy; | 
|  | 540 | } | 
|  | 541 | } | 
|  | 542 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 543 | void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { | 
|  | 544 | ASensorEvent flushCompleteEvent; | 
|  | 545 | memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent)); | 
|  | 546 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; | 
|  | 547 | // Loop through all the sensors for this connection and check if there are any pending | 
|  | 548 | // flush complete events to be sent. | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 549 | for (auto& it : mSensorInfo) { | 
|  | 550 | const int handle = it.first; | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 551 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle); | 
|  | 552 | if (si == nullptr) { | 
|  | 553 | continue; | 
|  | 554 | } | 
|  | 555 |  | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 556 | FlushInfo& flushInfo = it.second; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 557 | while (flushInfo.mPendingFlushEventsToSend > 0) { | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 558 | flushCompleteEvent.meta_data.sensor = handle; | 
|  | 559 | bool wakeUpSensor = si->getSensor().isWakeUpSensor(); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 560 | if (wakeUpSensor) { | 
|  | 561 | ++mWakeLockRefCount; | 
|  | 562 | flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 563 | } | 
|  | 564 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); | 
|  | 565 | if (size < 0) { | 
|  | 566 | if (wakeUpSensor) --mWakeLockRefCount; | 
|  | 567 | return; | 
|  | 568 | } | 
|  | 569 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", | 
|  | 570 | flushCompleteEvent.meta_data.sensor); | 
|  | 571 | flushInfo.mPendingFlushEventsToSend--; | 
|  | 572 | } | 
|  | 573 | } | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | void SensorService::SensorEventConnection::writeToSocketFromCache() { | 
|  | 577 | // At a time write at most half the size of the receiver buffer in SensorEventQueue OR | 
|  | 578 | // half the size of the socket buffer allocated in BitTube whichever is smaller. | 
|  | 579 | const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2, | 
|  | 580 | int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2))); | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 581 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 582 | // Send pending flush complete events (if any) | 
|  | 583 | sendPendingFlushEventsLocked(); | 
|  | 584 | for (int numEventsSent = 0; numEventsSent < mCacheSize;) { | 
|  | 585 | const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize); | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 586 | int index_wake_up_event = -1; | 
| Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 587 | if (hasSensorAccess()) { | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 588 | index_wake_up_event = | 
|  | 589 | findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); | 
|  | 590 | if (index_wake_up_event >= 0) { | 
|  | 591 | mEventCache[index_wake_up_event + numEventsSent].flags |= | 
|  | 592 | WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 593 | ++mWakeLockRefCount; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 594 | #if DEBUG_CONNECTIONS | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 595 | ++mTotalAcksNeeded; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 596 | #endif | 
| Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 597 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
|  | 600 | ssize_t size = SensorEventQueue::write(mChannel, | 
|  | 601 | reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), | 
|  | 602 | numEventsToWrite); | 
|  | 603 | if (size < 0) { | 
|  | 604 | if (index_wake_up_event >= 0) { | 
|  | 605 | // If there was a wake_up sensor_event, reset the flag. | 
|  | 606 | mEventCache[index_wake_up_event + numEventsSent].flags  &= | 
|  | 607 | ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 608 | if (mWakeLockRefCount > 0) { | 
|  | 609 | --mWakeLockRefCount; | 
|  | 610 | } | 
|  | 611 | #if DEBUG_CONNECTIONS | 
|  | 612 | --mTotalAcksNeeded; | 
|  | 613 | #endif | 
|  | 614 | } | 
|  | 615 | memmove(mEventCache, &mEventCache[numEventsSent], | 
|  | 616 | (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); | 
|  | 617 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", | 
|  | 618 | numEventsSent, mCacheSize); | 
|  | 619 | mCacheSize -= numEventsSent; | 
|  | 620 | return; | 
|  | 621 | } | 
|  | 622 | numEventsSent += numEventsToWrite; | 
|  | 623 | #if DEBUG_CONNECTIONS | 
|  | 624 | mEventsSentFromCache += numEventsToWrite; | 
|  | 625 | #endif | 
|  | 626 | } | 
|  | 627 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); | 
|  | 628 | // All events from the cache have been sent. Reset cache size to zero. | 
|  | 629 | mCacheSize = 0; | 
|  | 630 | // There are no more events in the cache. We don't need to poll for write on the fd. | 
|  | 631 | // Update Looper registration. | 
|  | 632 | updateLooperRegistrationLocked(mService->getLooper()); | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( | 
|  | 636 | sensors_event_t const* scratch, const int numEventsDropped) { | 
|  | 637 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); | 
|  | 638 | // Count flushComplete events in the events that are about to the dropped. These will be sent | 
|  | 639 | // separately before the next batch of events. | 
|  | 640 | for (int j = 0; j < numEventsDropped; ++j) { | 
|  | 641 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 642 | if (mSensorInfo.count(scratch[j].meta_data.sensor) == 0) { | 
| Peng Xu | 63fbab8 | 2017-06-20 12:41:33 -0700 | [diff] [blame] | 643 | ALOGW("%s: sensor 0x%x is not found in connection", | 
|  | 644 | __func__, scratch[j].meta_data.sensor); | 
|  | 645 | continue; | 
|  | 646 | } | 
|  | 647 |  | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 648 | FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor]; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 649 | flushInfo.mPendingFlushEventsToSend++; | 
|  | 650 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", | 
|  | 651 | flushInfo.mPendingFlushEventsToSend); | 
|  | 652 | } | 
|  | 653 | } | 
|  | 654 | return; | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( | 
|  | 658 | sensors_event_t const* scratch, const int count) { | 
|  | 659 | for (int i = 0; i < count; ++i) { | 
|  | 660 | if (mService->isWakeUpSensorEvent(scratch[i])) { | 
|  | 661 | return i; | 
|  | 662 | } | 
|  | 663 | } | 
|  | 664 | return -1; | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const | 
|  | 668 | { | 
|  | 669 | return mChannel; | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | status_t SensorService::SensorEventConnection::enableDisable( | 
|  | 673 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, | 
|  | 674 | int reservedFlags) | 
|  | 675 | { | 
| Arthur Ishiguro | 930b583 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 676 | if (mDestroyed) { | 
|  | 677 | android_errorWriteLog(0x534e4554, "168211968"); | 
|  | 678 | return DEAD_OBJECT; | 
|  | 679 | } | 
|  | 680 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 681 | status_t err; | 
|  | 682 | if (enabled) { | 
|  | 683 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, | 
|  | 684 | reservedFlags, mOpPackageName); | 
|  | 685 |  | 
|  | 686 | } else { | 
|  | 687 | err = mService->disable(this, handle); | 
|  | 688 | } | 
|  | 689 | return err; | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | status_t SensorService::SensorEventConnection::setEventRate( | 
|  | 693 | int handle, nsecs_t samplingPeriodNs) | 
|  | 694 | { | 
| Arthur Ishiguro | 930b583 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 695 | if (mDestroyed) { | 
|  | 696 | android_errorWriteLog(0x534e4554, "168211968"); | 
|  | 697 | return DEAD_OBJECT; | 
|  | 698 | } | 
|  | 699 |  | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 700 | return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 701 | } | 
|  | 702 |  | 
|  | 703 | status_t  SensorService::SensorEventConnection::flush() { | 
| Arthur Ishiguro | 930b583 | 2020-09-22 13:05:15 -0700 | [diff] [blame] | 704 | if (mDestroyed) { | 
|  | 705 | return DEAD_OBJECT; | 
|  | 706 | } | 
|  | 707 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 708 | return  mService->flushSensor(this, mOpPackageName); | 
|  | 709 | } | 
|  | 710 |  | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 711 | int32_t SensorService::SensorEventConnection::configureChannel(int handle, int rateLevel) { | 
|  | 712 | // SensorEventConnection does not support configureChannel, parameters not used | 
|  | 713 | UNUSED(handle); | 
|  | 714 | UNUSED(rateLevel); | 
|  | 715 | return INVALID_OPERATION; | 
|  | 716 | } | 
|  | 717 |  | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 718 | int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) { | 
|  | 719 | if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { | 
|  | 720 | { | 
|  | 721 | // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount, | 
|  | 722 | // and remove the fd from Looper. Call checkWakeLockState to know if SensorService | 
|  | 723 | // can release the wake-lock. | 
|  | 724 | ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd); | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 725 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 726 | mDead = true; | 
|  | 727 | mWakeLockRefCount = 0; | 
|  | 728 | updateLooperRegistrationLocked(mService->getLooper()); | 
|  | 729 | } | 
|  | 730 | mService->checkWakeLockState(); | 
|  | 731 | if (mDataInjectionMode) { | 
|  | 732 | // If the Looper has encountered some error in data injection mode, reset SensorService | 
|  | 733 | // back to normal mode. | 
|  | 734 | mService->resetToNormalMode(); | 
|  | 735 | mDataInjectionMode = false; | 
|  | 736 | } | 
|  | 737 | return 1; | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 | if (events & ALOOPER_EVENT_INPUT) { | 
|  | 741 | unsigned char buf[sizeof(sensors_event_t)]; | 
|  | 742 | ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT); | 
|  | 743 | { | 
| Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 744 | Mutex::Autolock _l(mConnectionLock); | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 745 | if (numBytesRead == sizeof(sensors_event_t)) { | 
|  | 746 | if (!mDataInjectionMode) { | 
|  | 747 | ALOGE("Data injected in normal mode, dropping event" | 
|  | 748 | "package=%s uid=%d", mPackageName.string(), mUid); | 
|  | 749 | // Unregister call backs. | 
|  | 750 | return 0; | 
|  | 751 | } | 
|  | 752 | sensors_event_t sensor_event; | 
|  | 753 | memcpy(&sensor_event, buf, sizeof(sensors_event_t)); | 
|  | 754 | sp<SensorInterface> si = | 
|  | 755 | mService->getSensorInterfaceFromHandle(sensor_event.sensor); | 
|  | 756 | if (si == nullptr) { | 
|  | 757 | return 1; | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | SensorDevice& dev(SensorDevice::getInstance()); | 
|  | 761 | sensor_event.type = si->getSensor().getType(); | 
|  | 762 | dev.injectSensorData(&sensor_event); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 763 | #if DEBUG_CONNECTIONS | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 764 | ++mEventsReceived; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 765 | #endif | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 766 | } else if (numBytesRead == sizeof(uint32_t)) { | 
|  | 767 | uint32_t numAcks = 0; | 
|  | 768 | memcpy(&numAcks, buf, numBytesRead); | 
|  | 769 | // Sanity check to ensure  there are no read errors in recv, numAcks is always | 
|  | 770 | // within the range and not zero. If any of the above don't hold reset | 
|  | 771 | // mWakeLockRefCount to zero. | 
|  | 772 | if (numAcks > 0 && numAcks < mWakeLockRefCount) { | 
|  | 773 | mWakeLockRefCount -= numAcks; | 
|  | 774 | } else { | 
|  | 775 | mWakeLockRefCount = 0; | 
|  | 776 | } | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 777 | #if DEBUG_CONNECTIONS | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 778 | mTotalAcksReceived += numAcks; | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 779 | #endif | 
|  | 780 | } else { | 
|  | 781 | // Read error, reset wakelock refcount. | 
|  | 782 | mWakeLockRefCount = 0; | 
|  | 783 | } | 
|  | 784 | } | 
|  | 785 | // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released | 
|  | 786 | // here as checkWakeLockState() will need it. | 
|  | 787 | if (mWakeLockRefCount == 0) { | 
|  | 788 | mService->checkWakeLockState(); | 
|  | 789 | } | 
|  | 790 | // continue getting callbacks. | 
|  | 791 | return 1; | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 | if (events & ALOOPER_EVENT_OUTPUT) { | 
|  | 795 | // send sensor data that is stored in mEventCache for this connection. | 
|  | 796 | mService->sendEventsFromCache(this); | 
|  | 797 | } | 
|  | 798 | return 1; | 
|  | 799 | } | 
|  | 800 |  | 
|  | 801 | int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { | 
|  | 802 | size_t fifoWakeUpSensors = 0; | 
|  | 803 | size_t fifoNonWakeUpSensors = 0; | 
| Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 804 | for (auto& it : mSensorInfo) { | 
|  | 805 | sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(it.first); | 
| Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 806 | if (si == nullptr) { | 
|  | 807 | continue; | 
|  | 808 | } | 
|  | 809 | const Sensor& sensor = si->getSensor(); | 
| Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 810 | if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { | 
|  | 811 | // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and | 
|  | 812 | // non wake_up sensors. | 
|  | 813 | if (sensor.isWakeUpSensor()) { | 
|  | 814 | fifoWakeUpSensors += sensor.getFifoReservedEventCount(); | 
|  | 815 | } else { | 
|  | 816 | fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); | 
|  | 817 | } | 
|  | 818 | } else { | 
|  | 819 | // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. | 
|  | 820 | if (sensor.isWakeUpSensor()) { | 
|  | 821 | fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? | 
|  | 822 | fifoWakeUpSensors : sensor.getFifoMaxEventCount(); | 
|  | 823 |  | 
|  | 824 | } else { | 
|  | 825 | fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? | 
|  | 826 | fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); | 
|  | 827 |  | 
|  | 828 | } | 
|  | 829 | } | 
|  | 830 | } | 
|  | 831 | if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { | 
|  | 832 | // It is extremely unlikely that there is a write failure in non batch mode. Return a cache | 
|  | 833 | // size that is equal to that of the batch mode. | 
|  | 834 | // ALOGW("Write failure in non-batch mode"); | 
|  | 835 | return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); | 
|  | 836 | } | 
|  | 837 | return fifoWakeUpSensors + fifoNonWakeUpSensors; | 
|  | 838 | } | 
|  | 839 |  | 
|  | 840 | } // namespace android | 
|  | 841 |  |