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