Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [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 | */ |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 16 | #include <android-base/strings.h> |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 17 | #include <android/content/pm/IPackageManagerNative.h> |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 18 | #include <android/util/ProtoOutputStream.h> |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 19 | #include <binder/ActivityManager.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 20 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 22 | #include <binder/PermissionCache.h> |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 23 | #include <binder/PermissionController.h> |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 24 | #include <cutils/ashmem.h> |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 25 | #include <cutils/misc.h> |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Brian Duddie | 0a4cebb | 2022-08-25 19:20:18 +0000 | [diff] [blame] | 27 | #include <frameworks/base/core/proto/android/service/sensor_service.proto.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 28 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 29 | #include <hardware_legacy/power.h> |
Brian Duddie | 0eb4624 | 2018-02-15 15:02:29 -0800 | [diff] [blame] | 30 | #include <log/log.h> |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 31 | #include <openssl/digest.h> |
| 32 | #include <openssl/hmac.h> |
| 33 | #include <openssl/rand.h> |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 34 | #include <sensor/SensorEventQueue.h> |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 35 | #include <sensorprivacy/SensorPrivacyManager.h> |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 36 | #include <utils/SystemClock.h> |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 37 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 38 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 39 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 40 | #include "GravitySensor.h" |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 41 | #include "LimitedAxesImuSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 42 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 43 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 44 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 45 | #include "SensorFusion.h" |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 46 | #include "SensorInterface.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 47 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 48 | #include "SensorService.h" |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 49 | #include "SensorDirectConnection.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 50 | #include "SensorEventAckReceiver.h" |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 51 | #include "SensorEventConnection.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 52 | #include "SensorRecord.h" |
| 53 | #include "SensorRegistrationInfo.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 54 | |
| 55 | #include <inttypes.h> |
| 56 | #include <math.h> |
Peng Xu | 98d30f6 | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 57 | #include <sched.h> |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 58 | #include <stdint.h> |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 59 | #include <sys/socket.h> |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 60 | #include <sys/stat.h> |
| 61 | #include <sys/types.h> |
| 62 | #include <unistd.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 63 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 64 | #include <ctime> |
| 65 | #include <future> |
| 66 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 67 | #include <private/android_filesystem_config.h> |
| 68 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 69 | using namespace std::chrono_literals; |
| 70 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 71 | namespace android { |
| 72 | // --------------------------------------------------------------------------- |
| 73 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Notes: |
| 76 | * |
| 77 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 78 | * - run mag sensor from time to time to force calibration |
| 79 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 80 | * |
| 81 | */ |
| 82 | |
Aravind Akella | 8ef3c89 | 2015-07-10 11:24:28 -0700 | [diff] [blame] | 83 | const char* SensorService::WAKE_LOCK_NAME = "SensorService_wakelock"; |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 84 | uint8_t SensorService::sHmacGlobalKey[128] = {}; |
| 85 | bool SensorService::sHmacGlobalKeyIsValid = false; |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 86 | std::map<String16, int> SensorService::sPackageTargetVersion; |
| 87 | Mutex SensorService::sPackageTargetVersionLock; |
Anthony Stange | 07eb421 | 2020-08-28 14:50:28 -0400 | [diff] [blame] | 88 | String16 SensorService::sSensorInterfaceDescriptorPrefix = |
| 89 | String16("android.frameworks.sensorservice@"); |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 90 | AppOpsManager SensorService::sAppOpsManager; |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 91 | std::atomic_uint64_t SensorService::curProxCallbackSeq(0); |
| 92 | std::atomic_uint64_t SensorService::completedCallbackSeq(0); |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 93 | |
| 94 | #define SENSOR_SERVICE_DIR "/data/system/sensor_service" |
| 95 | #define SENSOR_SERVICE_HMAC_KEY_FILE SENSOR_SERVICE_DIR "/hmac_key" |
Peng Xu | 98d30f6 | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 96 | #define SENSOR_SERVICE_SCHED_FIFO_PRIORITY 10 |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 97 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 98 | // Permissions. |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 99 | static const String16 sAccessHighSensorSamplingRatePermission( |
| 100 | "android.permission.HIGH_SAMPLING_RATE_SENSORS"); |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 101 | static const String16 sDumpPermission("android.permission.DUMP"); |
| 102 | static const String16 sLocationHardwarePermission("android.permission.LOCATION_HARDWARE"); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 103 | static const String16 sManageSensorsPermission("android.permission.MANAGE_SENSORS"); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 104 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 105 | namespace { |
| 106 | |
| 107 | // TODO(b/259227294): Move the sensor ranges to the HAL. |
| 108 | int32_t nextRuntimeSensorHandle() { |
| 109 | static constexpr int32_t kRuntimeHandleBase = 0x5F000000; |
| 110 | static constexpr int32_t kRuntimeHandleEnd = 0x5FFFFFFF; |
| 111 | static int32_t nextHandle = kRuntimeHandleBase; |
| 112 | if (nextHandle == kRuntimeHandleEnd) { |
| 113 | return -1; |
| 114 | } |
| 115 | return nextHandle++; |
| 116 | } |
| 117 | |
| 118 | class RuntimeSensorCallbackProxy : public RuntimeSensor::StateChangeCallback { |
| 119 | public: |
| 120 | RuntimeSensorCallbackProxy(sp<SensorService::RuntimeSensorStateChangeCallback> callback) |
| 121 | : mCallback(std::move(callback)) {} |
| 122 | void onStateChanged(bool enabled, int64_t samplingPeriodNs, |
| 123 | int64_t batchReportLatencyNs) override { |
| 124 | mCallback->onStateChanged(enabled, samplingPeriodNs, batchReportLatencyNs); |
| 125 | } |
| 126 | private: |
| 127 | sp<SensorService::RuntimeSensorStateChangeCallback> mCallback; |
| 128 | }; |
| 129 | |
| 130 | } // namespace |
| 131 | |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 132 | static bool isAutomotive() { |
| 133 | sp<IServiceManager> serviceManager = defaultServiceManager(); |
| 134 | if (serviceManager.get() == nullptr) { |
| 135 | ALOGE("%s: unable to access native ServiceManager", __func__); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | sp<content::pm::IPackageManagerNative> packageManager; |
| 140 | sp<IBinder> binder = serviceManager->waitForService(String16("package_native")); |
| 141 | packageManager = interface_cast<content::pm::IPackageManagerNative>(binder); |
| 142 | if (packageManager == nullptr) { |
| 143 | ALOGE("%s: unable to access native PackageManager", __func__); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | bool isAutomotive = false; |
| 148 | binder::Status status = |
| 149 | packageManager->hasSystemFeature(String16("android.hardware.type.automotive"), 0, |
| 150 | &isAutomotive); |
| 151 | if (!status.isOk()) { |
| 152 | ALOGE("%s: hasSystemFeature failed: %s", __func__, status.exceptionMessage().c_str()); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return isAutomotive; |
| 157 | } |
| 158 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 159 | SensorService::SensorService() |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 160 | : mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED), |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 161 | mWakeLockAcquired(false), mLastReportedProxIsActive(false) { |
Jaekyun Seok | 2d7e351 | 2018-03-28 19:12:11 +0900 | [diff] [blame] | 162 | mUidPolicy = new UidPolicy(this); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 163 | mSensorPrivacyPolicy = new SensorPrivacyPolicy(this); |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 164 | mMicSensorPrivacyPolicy = new MicrophonePrivacyPolicy(this); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 167 | int SensorService::registerRuntimeSensor( |
| 168 | const sensor_t& sensor, int deviceId, sp<RuntimeSensorStateChangeCallback> callback) { |
| 169 | int handle = 0; |
| 170 | while (handle == 0 || !mSensors.isNewHandle(handle)) { |
| 171 | handle = nextRuntimeSensorHandle(); |
| 172 | if (handle < 0) { |
| 173 | // Ran out of the dedicated range for runtime sensors. |
| 174 | return handle; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | ALOGI("Registering runtime sensor handle 0x%x, type %d, name %s", |
| 179 | handle, sensor.type, sensor.name); |
| 180 | |
| 181 | sp<RuntimeSensor::StateChangeCallback> runtimeSensorCallback( |
| 182 | new RuntimeSensorCallbackProxy(std::move(callback))); |
| 183 | sensor_t runtimeSensor = sensor; |
| 184 | // force the handle to be consistent |
| 185 | runtimeSensor.handle = handle; |
| 186 | SensorInterface *si = new RuntimeSensor(runtimeSensor, std::move(runtimeSensorCallback)); |
| 187 | |
| 188 | Mutex::Autolock _l(mLock); |
| 189 | const Sensor& s = registerSensor(si, /* isDebug= */ false, /* isVirtual= */ false, deviceId); |
| 190 | |
| 191 | if (s.getHandle() != handle) { |
| 192 | // The registration was unsuccessful. |
| 193 | return s.getHandle(); |
| 194 | } |
| 195 | return handle; |
| 196 | } |
| 197 | |
| 198 | status_t SensorService::unregisterRuntimeSensor(int handle) { |
| 199 | ALOGI("Unregistering runtime sensor handle 0x%x disconnected", handle); |
| 200 | { |
| 201 | Mutex::Autolock _l(mLock); |
| 202 | if (!unregisterDynamicSensorLocked(handle)) { |
| 203 | ALOGE("Runtime sensor release error."); |
| 204 | return UNKNOWN_ERROR; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 209 | for (const sp<SensorEventConnection>& connection : connLock.getActiveConnections()) { |
| 210 | connection->removeSensor(handle); |
| 211 | } |
| 212 | return OK; |
| 213 | } |
| 214 | |
| 215 | status_t SensorService::sendRuntimeSensorEvent(const sensors_event_t& event) { |
| 216 | Mutex::Autolock _l(mLock); |
| 217 | mRuntimeSensorEventQueue.push(event); |
| 218 | return OK; |
| 219 | } |
| 220 | |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 221 | bool SensorService::initializeHmacKey() { |
| 222 | int fd = open(SENSOR_SERVICE_HMAC_KEY_FILE, O_RDONLY|O_CLOEXEC); |
| 223 | if (fd != -1) { |
| 224 | int result = read(fd, sHmacGlobalKey, sizeof(sHmacGlobalKey)); |
| 225 | close(fd); |
| 226 | if (result == sizeof(sHmacGlobalKey)) { |
| 227 | return true; |
| 228 | } |
| 229 | ALOGW("Unable to read HMAC key; generating new one."); |
| 230 | } |
| 231 | |
| 232 | if (RAND_bytes(sHmacGlobalKey, sizeof(sHmacGlobalKey)) == -1) { |
| 233 | ALOGW("Can't generate HMAC key; dynamic sensor getId() will be wrong."); |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | // We need to make sure this is only readable to us. |
| 238 | bool wroteKey = false; |
| 239 | mkdir(SENSOR_SERVICE_DIR, S_IRWXU); |
| 240 | fd = open(SENSOR_SERVICE_HMAC_KEY_FILE, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, |
| 241 | S_IRUSR|S_IWUSR); |
| 242 | if (fd != -1) { |
| 243 | int result = write(fd, sHmacGlobalKey, sizeof(sHmacGlobalKey)); |
| 244 | close(fd); |
| 245 | wroteKey = (result == sizeof(sHmacGlobalKey)); |
| 246 | } |
| 247 | if (wroteKey) { |
| 248 | ALOGI("Generated new HMAC key."); |
| 249 | } else { |
| 250 | ALOGW("Unable to write HMAC key; dynamic sensor getId() will change " |
| 251 | "after reboot."); |
| 252 | } |
| 253 | // Even if we failed to write the key we return true, because we did |
| 254 | // initialize the HMAC key. |
| 255 | return true; |
| 256 | } |
| 257 | |
Peng Xu | 98d30f6 | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 258 | // Set main thread to SCHED_FIFO to lower sensor event latency when system is under load |
| 259 | void SensorService::enableSchedFifoMode() { |
| 260 | struct sched_param param = {0}; |
| 261 | param.sched_priority = SENSOR_SERVICE_SCHED_FIFO_PRIORITY; |
| 262 | if (sched_setscheduler(getTid(), SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m) != 0) { |
| 263 | ALOGE("Couldn't set SCHED_FIFO for SensorService thread"); |
| 264 | } |
| 265 | } |
| 266 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 267 | void SensorService::onFirstRef() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 268 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 269 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 270 | |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 271 | sHmacGlobalKeyIsValid = initializeHmacKey(); |
| 272 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 273 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 274 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 275 | ssize_t count = dev.getSensorList(&list); |
| 276 | if (count > 0) { |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 277 | bool hasGyro = false, hasAccel = false, hasMag = false; |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 278 | bool hasGyroUncalibrated = false; |
| 279 | bool hasAccelUncalibrated = false; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 280 | uint32_t virtualSensorsNeeds = |
| 281 | (1<<SENSOR_TYPE_GRAVITY) | |
| 282 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 283 | (1<<SENSOR_TYPE_ROTATION_VECTOR) | |
| 284 | (1<<SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR) | |
| 285 | (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 286 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 287 | for (ssize_t i=0 ; i<count ; i++) { |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 288 | bool useThisSensor = true; |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 289 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 290 | switch (list[i].type) { |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 291 | case SENSOR_TYPE_ACCELEROMETER: |
| 292 | hasAccel = true; |
| 293 | break; |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 294 | case SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED: |
| 295 | hasAccelUncalibrated = true; |
| 296 | break; |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 297 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 298 | hasMag = true; |
| 299 | break; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 300 | case SENSOR_TYPE_GYROSCOPE: |
| 301 | hasGyro = true; |
| 302 | break; |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 303 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
| 304 | hasGyroUncalibrated = true; |
| 305 | break; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 306 | case SENSOR_TYPE_GRAVITY: |
| 307 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 308 | case SENSOR_TYPE_ROTATION_VECTOR: |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 309 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 310 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 311 | if (IGNORE_HARDWARE_FUSION) { |
| 312 | useThisSensor = false; |
| 313 | } else { |
| 314 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 315 | } |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 316 | break; |
Arthur Ishiguro | 2fd90a7 | 2021-11-12 17:24:41 +0000 | [diff] [blame] | 317 | default: |
| 318 | break; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 319 | } |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 320 | if (useThisSensor) { |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 321 | if (list[i].type == SENSOR_TYPE_PROXIMITY) { |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 322 | SensorInterface* s = new ProximitySensor(list[i], *this); |
| 323 | registerSensor(s); |
| 324 | mProxSensorHandles.push_back(s->getSensor().getHandle()); |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 325 | } else { |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 326 | registerSensor(new HardwareSensor(list[i])); |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 327 | } |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 328 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 329 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 330 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 331 | // it's safe to instantiate the SensorFusion object here |
| 332 | // (it wants to be instantiated after h/w sensors have been |
| 333 | // registered) |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 334 | SensorFusion::getInstance(); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 335 | |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 336 | if ((hasGyro || hasGyroUncalibrated) && hasAccel && hasMag) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 337 | // Add Android virtual sensors if they're not already |
| 338 | // available in the HAL |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 339 | bool needRotationVector = |
| 340 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) != 0; |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 341 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 342 | registerSensor(new RotationVectorSensor(), !needRotationVector, true); |
| 343 | registerSensor(new OrientationSensor(), !needRotationVector, true); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 344 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 345 | // virtual debugging sensors are not for user |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 346 | registerSensor( new CorrectedGyroSensor(list, count), true, true); |
| 347 | registerSensor( new GyroDriftSensor(), true, true); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 350 | if (hasAccel && (hasGyro || hasGyroUncalibrated)) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 351 | bool needGravitySensor = (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) != 0; |
| 352 | registerSensor(new GravitySensor(list, count), !needGravitySensor, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 353 | |
Pierre Fite-Georgel | 63daa66 | 2019-12-17 16:52:29 -0800 | [diff] [blame] | 354 | bool needLinearAcceleration = |
| 355 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) != 0; |
| 356 | registerSensor(new LinearAccelerationSensor(list, count), |
| 357 | !needLinearAcceleration, true); |
| 358 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 359 | bool needGameRotationVector = |
| 360 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR)) != 0; |
| 361 | registerSensor(new GameRotationVectorSensor(), !needGameRotationVector, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | if (hasAccel && hasMag) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 365 | bool needGeoMagRotationVector = |
| 366 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR)) != 0; |
| 367 | registerSensor(new GeoMagRotationVectorSensor(), !needGeoMagRotationVector, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Tyler Trephan | c7b9263 | 2021-07-21 10:39:38 -0700 | [diff] [blame] | 370 | if (isAutomotive()) { |
| 371 | if (hasAccel) { |
| 372 | registerSensor(new LimitedAxesImuSensor(list, count, SENSOR_TYPE_ACCELEROMETER), |
| 373 | /*isDebug=*/false, /*isVirtual=*/true); |
| 374 | } |
| 375 | |
| 376 | if (hasGyro) { |
| 377 | registerSensor(new LimitedAxesImuSensor(list, count, SENSOR_TYPE_GYROSCOPE), |
| 378 | /*isDebug=*/false, /*isVirtual=*/true); |
| 379 | } |
| 380 | |
| 381 | if (hasAccelUncalibrated) { |
| 382 | registerSensor(new LimitedAxesImuSensor(list, count, |
| 383 | SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED), |
| 384 | /*isDebug=*/false, /*isVirtual=*/true); |
| 385 | } |
| 386 | |
| 387 | if (hasGyroUncalibrated) { |
| 388 | registerSensor(new LimitedAxesImuSensor(list, count, |
| 389 | SENSOR_TYPE_GYROSCOPE_UNCALIBRATED), |
| 390 | /*isDebug=*/false, /*isVirtual=*/true); |
| 391 | } |
| 392 | } |
| 393 | |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 394 | // Check if the device really supports batching by looking at the FIFO event |
| 395 | // counts for each sensor. |
| 396 | bool batchingSupported = false; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 397 | mSensors.forEachSensor( |
| 398 | [&batchingSupported] (const Sensor& s) -> bool { |
| 399 | if (s.getFifoMaxEventCount() > 0) { |
| 400 | batchingSupported = true; |
| 401 | } |
| 402 | return !batchingSupported; |
| 403 | }); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 404 | |
| 405 | if (batchingSupported) { |
| 406 | // Increase socket buffer size to a max of 100 KB for batching capabilities. |
| 407 | mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; |
| 408 | } else { |
| 409 | mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; |
| 410 | } |
| 411 | |
| 412 | // Compare the socketBufferSize value against the system limits and limit |
| 413 | // it to maxSystemSocketBufferSize if necessary. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 414 | FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r"); |
| 415 | char line[128]; |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 416 | if (fp != nullptr && fgets(line, sizeof(line), fp) != nullptr) { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 417 | line[sizeof(line) - 1] = '\0'; |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 418 | size_t maxSystemSocketBufferSize; |
| 419 | sscanf(line, "%zu", &maxSystemSocketBufferSize); |
| 420 | if (mSocketBufferSize > maxSystemSocketBufferSize) { |
| 421 | mSocketBufferSize = maxSystemSocketBufferSize; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 424 | if (fp) { |
| 425 | fclose(fp); |
| 426 | } |
| 427 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 428 | mWakeLockAcquired = false; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 429 | mLooper = new Looper(false); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 430 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 431 | mSensorEventBuffer = new sensors_event_t[minBufferSize]; |
| 432 | mSensorEventScratch = new sensors_event_t[minBufferSize]; |
Peng Xu | eb05947 | 2016-08-12 16:39:44 -0700 | [diff] [blame] | 433 | mMapFlushEventsToConnections = new wp<const SensorEventConnection> [minBufferSize]; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 434 | mCurrentOperatingMode = NORMAL; |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 435 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 436 | mNextSensorRegIndex = 0; |
| 437 | for (int i = 0; i < SENSOR_REGISTRATIONS_BUF_SIZE; ++i) { |
| 438 | mLastNSensorRegistrations.push(); |
| 439 | } |
| 440 | |
| 441 | mInitCheck = NO_ERROR; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 442 | mAckReceiver = new SensorEventAckReceiver(this); |
| 443 | mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY); |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 444 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
Peng Xu | 98d30f6 | 2016-08-01 18:12:11 -0700 | [diff] [blame] | 445 | |
| 446 | // priority can only be changed after run |
| 447 | enableSchedFifoMode(); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 448 | |
| 449 | // Start watching UID changes to apply policy. |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 450 | mUidPolicy->registerSelf(); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 451 | |
| 452 | // Start watching sensor privacy changes |
| 453 | mSensorPrivacyPolicy->registerSelf(); |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 454 | |
| 455 | // Start watching mic sensor privacy changes |
| 456 | mMicSensorPrivacyPolicy->registerSelf(); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 461 | void SensorService::onUidStateChanged(uid_t uid, UidState state) { |
| 462 | SensorDevice& dev(SensorDevice::getInstance()); |
| 463 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 464 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 465 | for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 466 | if (conn->getUid() == uid) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 467 | dev.setUidStateForConnection(conn.get(), state); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 468 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 469 | } |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 470 | |
| 471 | for (const sp<SensorDirectConnection>& conn : connLock.getDirectConnections()) { |
| 472 | if (conn->getUid() == uid) { |
| 473 | // Update sensor subscriptions if needed |
| 474 | bool hasAccess = hasSensorAccessLocked(conn->getUid(), conn->getOpPackageName()); |
| 475 | conn->onSensorAccessChanged(hasAccess); |
| 476 | } |
| 477 | } |
Chris Kuiper | 587ef9b | 2021-10-12 16:36:43 -0700 | [diff] [blame] | 478 | checkAndReportProxStateChangeLocked(); |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | bool SensorService::hasSensorAccess(uid_t uid, const String16& opPackageName) { |
| 482 | Mutex::Autolock _l(mLock); |
| 483 | return hasSensorAccessLocked(uid, opPackageName); |
| 484 | } |
| 485 | |
| 486 | bool SensorService::hasSensorAccessLocked(uid_t uid, const String16& opPackageName) { |
| 487 | return !mSensorPrivacyPolicy->isSensorPrivacyEnabled() |
| 488 | && isUidActive(uid) && !isOperationRestrictedLocked(opPackageName); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 491 | const Sensor& SensorService::registerSensor(SensorInterface* s, bool isDebug, bool isVirtual, |
| 492 | int deviceId) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 493 | int handle = s->getSensor().getHandle(); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 494 | int type = s->getSensor().getType(); |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 495 | if (mSensors.add(handle, s, isDebug, isVirtual, deviceId)) { |
Brian Stack | 4baa5be | 2018-09-18 14:03:13 -0700 | [diff] [blame] | 496 | mRecentEvent.emplace(handle, new SensorServiceUtil::RecentEventLogger(type)); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 497 | return s->getSensor(); |
| 498 | } else { |
| 499 | return mSensors.getNonSensor(); |
| 500 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 503 | const Sensor& SensorService::registerDynamicSensorLocked(SensorInterface* s, bool isDebug) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 504 | return registerSensor(s, isDebug); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 507 | bool SensorService::unregisterDynamicSensorLocked(int handle) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 508 | bool ret = mSensors.remove(handle); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 509 | |
| 510 | const auto i = mRecentEvent.find(handle); |
| 511 | if (i != mRecentEvent.end()) { |
| 512 | delete i->second; |
| 513 | mRecentEvent.erase(i); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 514 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 515 | return ret; |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 518 | const Sensor& SensorService::registerVirtualSensor(SensorInterface* s, bool isDebug) { |
| 519 | return registerSensor(s, isDebug, true); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 522 | SensorService::~SensorService() { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 523 | for (auto && entry : mRecentEvent) { |
| 524 | delete entry.second; |
| 525 | } |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 526 | mUidPolicy->unregisterSelf(); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 527 | mSensorPrivacyPolicy->unregisterSelf(); |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 528 | mMicSensorPrivacyPolicy->unregisterSelf(); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | status_t SensorService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 532 | String8 result; |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 533 | if (!PermissionCache::checkCallingPermission(sDumpPermission)) { |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 534 | result.appendFormat("Permission Denial: can't dump SensorService from pid=%d, uid=%d\n", |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 535 | IPCThreadState::self()->getCallingPid(), |
| 536 | IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 537 | } else { |
Peng Xu | fba3c11 | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 538 | bool privileged = IPCThreadState::self()->getCallingUid() == 0; |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 539 | if (args.size() > 2) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 540 | return INVALID_OPERATION; |
| 541 | } |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 542 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 543 | SensorDevice& dev(SensorDevice::getInstance()); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 544 | if (args.size() == 2 && args[0] == String16("restrict")) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 545 | // If already in restricted mode. Ignore. |
| 546 | if (mCurrentOperatingMode == RESTRICTED) { |
| 547 | return status_t(NO_ERROR); |
| 548 | } |
| 549 | // If in any mode other than normal, ignore. |
| 550 | if (mCurrentOperatingMode != NORMAL) { |
| 551 | return INVALID_OPERATION; |
| 552 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 553 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 554 | mCurrentOperatingMode = RESTRICTED; |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 555 | // temporarily stop all sensor direct report and disable sensors |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 556 | disableAllSensorsLocked(&connLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 557 | mWhiteListedPackage.setTo(String8(args[1])); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 558 | return status_t(NO_ERROR); |
| 559 | } else if (args.size() == 1 && args[0] == String16("enable")) { |
| 560 | // If currently in restricted mode, reset back to NORMAL mode else ignore. |
| 561 | if (mCurrentOperatingMode == RESTRICTED) { |
| 562 | mCurrentOperatingMode = NORMAL; |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 563 | // enable sensors and recover all sensor direct report |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 564 | enableAllSensorsLocked(&connLock); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 565 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 566 | if (mCurrentOperatingMode == DATA_INJECTION) { |
| 567 | resetToNormalModeLocked(); |
| 568 | } |
| 569 | mWhiteListedPackage.clear(); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 570 | return status_t(NO_ERROR); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 571 | } else if (args.size() == 2 && args[0] == String16("data_injection")) { |
| 572 | if (mCurrentOperatingMode == NORMAL) { |
| 573 | dev.disableAllSensors(); |
| 574 | status_t err = dev.setMode(DATA_INJECTION); |
| 575 | if (err == NO_ERROR) { |
| 576 | mCurrentOperatingMode = DATA_INJECTION; |
| 577 | } else { |
| 578 | // Re-enable sensors. |
| 579 | dev.enableAllSensors(); |
| 580 | } |
| 581 | mWhiteListedPackage.setTo(String8(args[1])); |
| 582 | return NO_ERROR; |
| 583 | } else if (mCurrentOperatingMode == DATA_INJECTION) { |
| 584 | // Already in DATA_INJECTION mode. Treat this as a no_op. |
| 585 | return NO_ERROR; |
| 586 | } else { |
| 587 | // Transition to data injection mode supported only from NORMAL mode. |
| 588 | return INVALID_OPERATION; |
| 589 | } |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 590 | } else if (args.size() == 1 && args[0] == String16("--proto")) { |
| 591 | return dumpProtoLocked(fd, &connLock); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 592 | } else if (!mSensors.hasAnySensor()) { |
Aravind Akella | ee155ca | 2015-06-24 08:31:32 -0700 | [diff] [blame] | 593 | result.append("No Sensors on the device\n"); |
Ashutosh Joshi | 53e5aa9 | 2017-07-19 09:52:57 -0700 | [diff] [blame] | 594 | result.appendFormat("devInitCheck : %d\n", SensorDevice::getInstance().initCheck()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 595 | } else { |
| 596 | // Default dump the sensor list and debugging information. |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 597 | // |
Brian Stack | 51498e6 | 2018-10-03 10:52:21 -0700 | [diff] [blame] | 598 | timespec curTime; |
| 599 | clock_gettime(CLOCK_REALTIME, &curTime); |
| 600 | struct tm* timeinfo = localtime(&(curTime.tv_sec)); |
| 601 | result.appendFormat("Captured at: %02d:%02d:%02d.%03d\n", timeinfo->tm_hour, |
| 602 | timeinfo->tm_min, timeinfo->tm_sec, (int)ns2ms(curTime.tv_nsec)); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 603 | result.append("Sensor Device:\n"); |
| 604 | result.append(SensorDevice::getInstance().dump().c_str()); |
| 605 | |
| 606 | result.append("Sensor List:\n"); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 607 | result.append(mSensors.dump().c_str()); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 608 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 609 | result.append("Fusion States:\n"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 610 | SensorFusion::getInstance().dump(result); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 611 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 612 | result.append("Recent Sensor events:\n"); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 613 | for (auto&& i : mRecentEvent) { |
| 614 | sp<SensorInterface> s = mSensors.getInterface(i.first); |
Peng Xu | fba3c11 | 2016-09-08 12:36:59 -0700 | [diff] [blame] | 615 | if (!i.second->isEmpty()) { |
| 616 | if (privileged || s->getSensor().getRequiredPermission().isEmpty()) { |
| 617 | i.second->setFormat("normal"); |
| 618 | } else { |
| 619 | i.second->setFormat("mask_data"); |
| 620 | } |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 621 | // if there is events and sensor does not need special permission. |
| 622 | result.appendFormat("%s: ", s->getSensor().getName().string()); |
| 623 | result.append(i.second->dump().c_str()); |
| 624 | } |
| 625 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 626 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 627 | result.append("Active sensors:\n"); |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 628 | SensorDevice& dev = SensorDevice::getInstance(); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 629 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
| 630 | int handle = mActiveSensors.keyAt(i); |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 631 | if (dev.isSensorActive(handle)) { |
| 632 | result.appendFormat("%s (handle=0x%08x, connections=%zu)\n", |
| 633 | getSensorName(handle).string(), |
| 634 | handle, |
| 635 | mActiveSensors.valueAt(i)->getNumConnections()); |
| 636 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 639 | result.appendFormat("Socket Buffer size = %zd events\n", |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 640 | mSocketBufferSize/sizeof(sensors_event_t)); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 641 | result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : |
| 642 | "not held"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 643 | result.appendFormat("Mode :"); |
| 644 | switch(mCurrentOperatingMode) { |
| 645 | case NORMAL: |
| 646 | result.appendFormat(" NORMAL\n"); |
| 647 | break; |
| 648 | case RESTRICTED: |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 649 | result.appendFormat(" RESTRICTED : %s\n", mWhiteListedPackage.string()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 650 | break; |
| 651 | case DATA_INJECTION: |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 652 | result.appendFormat(" DATA_INJECTION : %s\n", mWhiteListedPackage.string()); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 653 | } |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 654 | result.appendFormat("Sensor Privacy: %s\n", |
| 655 | mSensorPrivacyPolicy->isSensorPrivacyEnabled() ? "enabled" : "disabled"); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 656 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 657 | const auto& activeConnections = connLock.getActiveConnections(); |
| 658 | result.appendFormat("%zd active connections\n", activeConnections.size()); |
| 659 | for (size_t i=0 ; i < activeConnections.size() ; i++) { |
| 660 | result.appendFormat("Connection Number: %zu \n", i); |
| 661 | activeConnections[i]->dump(result); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 662 | } |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 663 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 664 | const auto& directConnections = connLock.getDirectConnections(); |
| 665 | result.appendFormat("%zd direct connections\n", directConnections.size()); |
| 666 | for (size_t i = 0 ; i < directConnections.size() ; i++) { |
| 667 | result.appendFormat("Direct connection %zu:\n", i); |
| 668 | directConnections[i]->dump(result); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 671 | result.appendFormat("Previous Registrations:\n"); |
| 672 | // Log in the reverse chronological order. |
| 673 | int currentIndex = (mNextSensorRegIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 674 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 675 | const int startIndex = currentIndex; |
| 676 | do { |
| 677 | const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[currentIndex]; |
| 678 | if (SensorRegistrationInfo::isSentinel(reg_info)) { |
| 679 | // Ignore sentinel, proceed to next item. |
| 680 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 681 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 682 | continue; |
| 683 | } |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 684 | result.appendFormat("%s\n", reg_info.dump().c_str()); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 685 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 686 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 687 | } while(startIndex != currentIndex); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 688 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 689 | } |
| 690 | write(fd, result.string(), result.size()); |
| 691 | return NO_ERROR; |
| 692 | } |
| 693 | |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 694 | /** |
| 695 | * Dump debugging information as android.service.SensorServiceProto protobuf message using |
| 696 | * ProtoOutputStream. |
| 697 | * |
| 698 | * See proto definition and some notes about ProtoOutputStream in |
| 699 | * frameworks/base/core/proto/android/service/sensor_service.proto |
| 700 | */ |
| 701 | status_t SensorService::dumpProtoLocked(int fd, ConnectionSafeAutolock* connLock) const { |
| 702 | using namespace service::SensorServiceProto; |
| 703 | util::ProtoOutputStream proto; |
Mike Ma | 285aac1 | 2020-02-07 12:29:46 -0800 | [diff] [blame] | 704 | proto.write(INIT_STATUS, int(SensorDevice::getInstance().initCheck())); |
| 705 | if (!mSensors.hasAnySensor()) { |
| 706 | return proto.flush(fd) ? OK : UNKNOWN_ERROR; |
| 707 | } |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 708 | const bool privileged = IPCThreadState::self()->getCallingUid() == 0; |
| 709 | |
| 710 | timespec curTime; |
| 711 | clock_gettime(CLOCK_REALTIME, &curTime); |
| 712 | proto.write(CURRENT_TIME_MS, curTime.tv_sec * 1000 + ns2ms(curTime.tv_nsec)); |
| 713 | |
| 714 | // Write SensorDeviceProto |
| 715 | uint64_t token = proto.start(SENSOR_DEVICE); |
| 716 | SensorDevice::getInstance().dump(&proto); |
| 717 | proto.end(token); |
| 718 | |
| 719 | // Write SensorListProto |
| 720 | token = proto.start(SENSORS); |
| 721 | mSensors.dump(&proto); |
| 722 | proto.end(token); |
| 723 | |
| 724 | // Write SensorFusionProto |
| 725 | token = proto.start(FUSION_STATE); |
| 726 | SensorFusion::getInstance().dump(&proto); |
| 727 | proto.end(token); |
| 728 | |
| 729 | // Write SensorEventsProto |
| 730 | token = proto.start(SENSOR_EVENTS); |
| 731 | for (auto&& i : mRecentEvent) { |
| 732 | sp<SensorInterface> s = mSensors.getInterface(i.first); |
| 733 | if (!i.second->isEmpty()) { |
| 734 | i.second->setFormat(privileged || s->getSensor().getRequiredPermission().isEmpty() ? |
| 735 | "normal" : "mask_data"); |
| 736 | const uint64_t mToken = proto.start(service::SensorEventsProto::RECENT_EVENTS_LOGS); |
| 737 | proto.write(service::SensorEventsProto::RecentEventsLog::NAME, |
| 738 | std::string(s->getSensor().getName().string())); |
| 739 | i.second->dump(&proto); |
| 740 | proto.end(mToken); |
| 741 | } |
| 742 | } |
| 743 | proto.end(token); |
| 744 | |
| 745 | // Write ActiveSensorProto |
| 746 | SensorDevice& dev = SensorDevice::getInstance(); |
| 747 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
| 748 | int handle = mActiveSensors.keyAt(i); |
| 749 | if (dev.isSensorActive(handle)) { |
| 750 | token = proto.start(ACTIVE_SENSORS); |
| 751 | proto.write(service::ActiveSensorProto::NAME, |
| 752 | std::string(getSensorName(handle).string())); |
| 753 | proto.write(service::ActiveSensorProto::HANDLE, handle); |
| 754 | proto.write(service::ActiveSensorProto::NUM_CONNECTIONS, |
| 755 | int(mActiveSensors.valueAt(i)->getNumConnections())); |
| 756 | proto.end(token); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | proto.write(SOCKET_BUFFER_SIZE, int(mSocketBufferSize)); |
| 761 | proto.write(SOCKET_BUFFER_SIZE_IN_EVENTS, int(mSocketBufferSize / sizeof(sensors_event_t))); |
| 762 | proto.write(WAKE_LOCK_ACQUIRED, mWakeLockAcquired); |
| 763 | |
| 764 | switch(mCurrentOperatingMode) { |
| 765 | case NORMAL: |
| 766 | proto.write(OPERATING_MODE, OP_MODE_NORMAL); |
| 767 | break; |
| 768 | case RESTRICTED: |
| 769 | proto.write(OPERATING_MODE, OP_MODE_RESTRICTED); |
| 770 | proto.write(WHITELISTED_PACKAGE, std::string(mWhiteListedPackage.string())); |
| 771 | break; |
| 772 | case DATA_INJECTION: |
| 773 | proto.write(OPERATING_MODE, OP_MODE_DATA_INJECTION); |
| 774 | proto.write(WHITELISTED_PACKAGE, std::string(mWhiteListedPackage.string())); |
| 775 | break; |
| 776 | default: |
| 777 | proto.write(OPERATING_MODE, OP_MODE_UNKNOWN); |
| 778 | } |
| 779 | proto.write(SENSOR_PRIVACY, mSensorPrivacyPolicy->isSensorPrivacyEnabled()); |
| 780 | |
| 781 | // Write repeated SensorEventConnectionProto |
| 782 | const auto& activeConnections = connLock->getActiveConnections(); |
| 783 | for (size_t i = 0; i < activeConnections.size(); i++) { |
| 784 | token = proto.start(ACTIVE_CONNECTIONS); |
| 785 | activeConnections[i]->dump(&proto); |
| 786 | proto.end(token); |
| 787 | } |
| 788 | |
| 789 | // Write repeated SensorDirectConnectionProto |
| 790 | const auto& directConnections = connLock->getDirectConnections(); |
| 791 | for (size_t i = 0 ; i < directConnections.size() ; i++) { |
| 792 | token = proto.start(DIRECT_CONNECTIONS); |
| 793 | directConnections[i]->dump(&proto); |
| 794 | proto.end(token); |
| 795 | } |
| 796 | |
| 797 | // Write repeated SensorRegistrationInfoProto |
| 798 | const int startIndex = mNextSensorRegIndex; |
| 799 | int curr = startIndex; |
| 800 | do { |
| 801 | const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[curr]; |
| 802 | if (SensorRegistrationInfo::isSentinel(reg_info)) { |
| 803 | // Ignore sentinel, proceed to next item. |
| 804 | curr = (curr + 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % SENSOR_REGISTRATIONS_BUF_SIZE; |
| 805 | continue; |
| 806 | } |
| 807 | token = proto.start(PREVIOUS_REGISTRATIONS); |
| 808 | reg_info.dump(&proto); |
| 809 | proto.end(token); |
| 810 | curr = (curr + 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % SENSOR_REGISTRATIONS_BUF_SIZE; |
| 811 | } while (startIndex != curr); |
| 812 | |
| 813 | return proto.flush(fd) ? OK : UNKNOWN_ERROR; |
| 814 | } |
| 815 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 816 | void SensorService::disableAllSensors() { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 817 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 818 | disableAllSensorsLocked(&connLock); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 821 | void SensorService::disableAllSensorsLocked(ConnectionSafeAutolock* connLock) { |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 822 | SensorDevice& dev(SensorDevice::getInstance()); |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 823 | for (const sp<SensorDirectConnection>& conn : connLock->getDirectConnections()) { |
| 824 | bool hasAccess = hasSensorAccessLocked(conn->getUid(), conn->getOpPackageName()); |
| 825 | conn->onSensorAccessChanged(hasAccess); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 826 | } |
| 827 | dev.disableAllSensors(); |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 828 | checkAndReportProxStateChangeLocked(); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 829 | // Clear all pending flush connections for all active sensors. If one of the active |
| 830 | // connections has called flush() and the underlying sensor has been disabled before a |
| 831 | // flush complete event is returned, we need to remove the connection from this queue. |
| 832 | for (size_t i=0 ; i< mActiveSensors.size(); ++i) { |
| 833 | mActiveSensors.valueAt(i)->clearAllPendingFlushConnections(); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | void SensorService::enableAllSensors() { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 838 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 839 | enableAllSensorsLocked(&connLock); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 842 | void SensorService::enableAllSensorsLocked(ConnectionSafeAutolock* connLock) { |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 843 | // sensors should only be enabled if the operating state is not restricted and sensor |
| 844 | // privacy is not enabled. |
| 845 | if (mCurrentOperatingMode == RESTRICTED || mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 846 | ALOGW("Sensors cannot be enabled: mCurrentOperatingMode = %d, sensor privacy = %s", |
| 847 | mCurrentOperatingMode, |
| 848 | mSensorPrivacyPolicy->isSensorPrivacyEnabled() ? "enabled" : "disabled"); |
| 849 | return; |
| 850 | } |
| 851 | SensorDevice& dev(SensorDevice::getInstance()); |
| 852 | dev.enableAllSensors(); |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 853 | for (const sp<SensorDirectConnection>& conn : connLock->getDirectConnections()) { |
| 854 | bool hasAccess = hasSensorAccessLocked(conn->getUid(), conn->getOpPackageName()); |
| 855 | conn->onSensorAccessChanged(hasAccess); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 856 | } |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 857 | checkAndReportProxStateChangeLocked(); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 860 | void SensorService::capRates() { |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 861 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 862 | for (const sp<SensorDirectConnection>& conn : connLock.getDirectConnections()) { |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 863 | conn->onMicSensorAccessChanged(true); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) { |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 867 | conn->onMicSensorAccessChanged(true); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 871 | void SensorService::uncapRates() { |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 872 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 873 | for (const sp<SensorDirectConnection>& conn : connLock.getDirectConnections()) { |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 874 | conn->onMicSensorAccessChanged(false); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) { |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 878 | conn->onMicSensorAccessChanged(false); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 879 | } |
| 880 | } |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 881 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 882 | // NOTE: This is a remote API - make sure all args are validated |
| 883 | status_t SensorService::shellCommand(int in, int out, int err, Vector<String16>& args) { |
| 884 | if (!checkCallingPermission(sManageSensorsPermission, nullptr, nullptr)) { |
| 885 | return PERMISSION_DENIED; |
| 886 | } |
karthik bharadwaj | 1b38658 | 2020-05-19 18:32:36 -0700 | [diff] [blame] | 887 | if (args.size() == 0) { |
| 888 | return BAD_INDEX; |
| 889 | } |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 890 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 891 | return BAD_VALUE; |
| 892 | } |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 893 | if (args[0] == String16("set-uid-state")) { |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 894 | return handleSetUidState(args, err); |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 895 | } else if (args[0] == String16("reset-uid-state")) { |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 896 | return handleResetUidState(args, err); |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 897 | } else if (args[0] == String16("get-uid-state")) { |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 898 | return handleGetUidState(args, out, err); |
Brian Duddie | 4a4d046 | 2022-05-09 16:49:49 -0700 | [diff] [blame] | 899 | } else if (args[0] == String16("unrestrict-ht")) { |
| 900 | mHtRestricted = false; |
| 901 | return NO_ERROR; |
| 902 | } else if (args[0] == String16("restrict-ht")) { |
| 903 | mHtRestricted = true; |
| 904 | return NO_ERROR; |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 905 | } else if (args.size() == 1 && args[0] == String16("help")) { |
| 906 | printHelp(out); |
| 907 | return NO_ERROR; |
| 908 | } |
| 909 | printHelp(err); |
| 910 | return BAD_VALUE; |
| 911 | } |
| 912 | |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 913 | static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) { |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 914 | PermissionController pc; |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 915 | uid = pc.getPackageUid(packageName, 0); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 916 | if (uid <= 0) { |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 917 | ALOGE("Unknown package: '%s'", String8(packageName).string()); |
| 918 | dprintf(err, "Unknown package: '%s'\n", String8(packageName).string()); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 919 | return BAD_VALUE; |
| 920 | } |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 921 | |
| 922 | if (userId < 0) { |
| 923 | ALOGE("Invalid user: %d", userId); |
| 924 | dprintf(err, "Invalid user: %d\n", userId); |
| 925 | return BAD_VALUE; |
| 926 | } |
| 927 | |
| 928 | uid = multiuser_get_uid(userId, uid); |
| 929 | return NO_ERROR; |
| 930 | } |
| 931 | |
| 932 | status_t SensorService::handleSetUidState(Vector<String16>& args, int err) { |
| 933 | // Valid arg.size() is 3 or 5, args.size() is 5 with --user option. |
| 934 | if (!(args.size() == 3 || args.size() == 5)) { |
| 935 | printHelp(err); |
| 936 | return BAD_VALUE; |
| 937 | } |
| 938 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 939 | bool active = false; |
| 940 | if (args[2] == String16("active")) { |
| 941 | active = true; |
| 942 | } else if ((args[2] != String16("idle"))) { |
| 943 | ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string()); |
| 944 | return BAD_VALUE; |
| 945 | } |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 946 | |
| 947 | int userId = 0; |
| 948 | if (args.size() == 5 && args[3] == String16("--user")) { |
| 949 | userId = atoi(String8(args[4])); |
| 950 | } |
| 951 | |
| 952 | uid_t uid; |
| 953 | if (getUidForPackage(args[1], userId, uid, err) != NO_ERROR) { |
| 954 | return BAD_VALUE; |
| 955 | } |
| 956 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 957 | mUidPolicy->addOverrideUid(uid, active); |
| 958 | return NO_ERROR; |
| 959 | } |
| 960 | |
| 961 | status_t SensorService::handleResetUidState(Vector<String16>& args, int err) { |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 962 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 963 | if (!(args.size() == 2 || args.size() == 4)) { |
| 964 | printHelp(err); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 965 | return BAD_VALUE; |
| 966 | } |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 967 | |
| 968 | int userId = 0; |
| 969 | if (args.size() == 4 && args[2] == String16("--user")) { |
| 970 | userId = atoi(String8(args[3])); |
| 971 | } |
| 972 | |
| 973 | uid_t uid; |
| 974 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 975 | return BAD_VALUE; |
| 976 | } |
| 977 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 978 | mUidPolicy->removeOverrideUid(uid); |
| 979 | return NO_ERROR; |
| 980 | } |
| 981 | |
| 982 | status_t SensorService::handleGetUidState(Vector<String16>& args, int out, int err) { |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 983 | // Valid arg.size() is 2 or 4, args.size() is 4 with --user option. |
| 984 | if (!(args.size() == 2 || args.size() == 4)) { |
| 985 | printHelp(err); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 986 | return BAD_VALUE; |
| 987 | } |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 988 | |
| 989 | int userId = 0; |
| 990 | if (args.size() == 4 && args[2] == String16("--user")) { |
| 991 | userId = atoi(String8(args[3])); |
| 992 | } |
| 993 | |
| 994 | uid_t uid; |
| 995 | if (getUidForPackage(args[1], userId, uid, err) == BAD_VALUE) { |
| 996 | return BAD_VALUE; |
| 997 | } |
| 998 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 999 | if (mUidPolicy->isUidActive(uid)) { |
| 1000 | return dprintf(out, "active\n"); |
| 1001 | } else { |
| 1002 | return dprintf(out, "idle\n"); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | status_t SensorService::printHelp(int out) { |
| 1007 | return dprintf(out, "Sensor service commands:\n" |
Tanmay Patil | d33a182 | 2019-04-11 18:38:55 -0700 | [diff] [blame] | 1008 | " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n" |
| 1009 | " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n" |
| 1010 | " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n" |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 1011 | " help print this message\n"); |
| 1012 | } |
| 1013 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1014 | //TODO: move to SensorEventConnection later |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1015 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1016 | sensors_event_t const* buffer, const int count) { |
| 1017 | for (int i=0 ; i<count ; i++) { |
| 1018 | int handle = buffer[i].sensor; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1019 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 1020 | handle = buffer[i].meta_data.sensor; |
| 1021 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 1022 | if (connection->hasSensor(handle)) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1023 | sp<SensorInterface> si = getSensorInterfaceFromHandle(handle); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 1024 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 1025 | // for this particular one_shot sensor, try cleaning up the connection. |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1026 | if (si != nullptr && |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1027 | si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 1028 | si->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1029 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1030 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1031 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1032 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1033 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1036 | bool SensorService::threadLoop() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1037 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1038 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 1039 | // each virtual sensor could generate an event per "real" event, that's why we need to size |
| 1040 | // numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. in practice, this is too |
| 1041 | // aggressive, but guaranteed to be enough. |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1042 | const size_t vcount = mSensors.getVirtualSensors().size(); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 1043 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1044 | const size_t numEventMax = minBufferSize / (1 + vcount); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 1045 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1046 | SensorDevice& device(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1047 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1048 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1049 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1050 | ssize_t count = device.poll(mSensorEventBuffer, numEventMax); |
| 1051 | if (count < 0) { |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 1052 | if(count == DEAD_OBJECT && device.isReconnecting()) { |
| 1053 | device.reconnect(); |
| 1054 | continue; |
| 1055 | } else { |
| 1056 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
| 1057 | break; |
| 1058 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1059 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1060 | |
| 1061 | // Reset sensors_event_t.flags to zero for all events in the buffer. |
| 1062 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1063 | mSensorEventBuffer[i].flags = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1064 | } |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1065 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 1066 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1067 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 1068 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 1069 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 1070 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 1071 | // releasing the wakelock. |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 1072 | uint32_t wakeEvents = 0; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1073 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1074 | if (isWakeUpSensorEvent(mSensorEventBuffer[i])) { |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 1075 | wakeEvents++; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 1079 | if (wakeEvents > 0) { |
| 1080 | if (!mWakeLockAcquired) { |
| 1081 | setWakeLockAcquiredLocked(true); |
| 1082 | } |
| 1083 | device.writeWakeLockHandled(wakeEvents); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1084 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1085 | recordLastValueLocked(mSensorEventBuffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 1086 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1087 | // handle virtual sensors |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 1088 | bool bufferNeedsSorting = false; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1089 | if (count && vcount) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1090 | sensors_event_t const * const event = mSensorEventBuffer; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1091 | if (!mActiveVirtualSensors.empty()) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1092 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 1093 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 1094 | if (fusion.isEnabled()) { |
| 1095 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 1096 | fusion.process(event[i]); |
| 1097 | } |
| 1098 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 1099 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1100 | for (int handle : mActiveVirtualSensors) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 1101 | if (count + k >= minBufferSize) { |
| 1102 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1103 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 1104 | count, k, minBufferSize); |
| 1105 | break; |
| 1106 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1107 | sensors_event_t out; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1108 | sp<SensorInterface> si = mSensors.getInterface(handle); |
| 1109 | if (si == nullptr) { |
| 1110 | ALOGE("handle %d is not an valid virtual sensor", handle); |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 1114 | if (si->process(&out, event[i])) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1115 | mSensorEventBuffer[count + k] = out; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1116 | k++; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | if (k) { |
| 1121 | // record the last synthesized values |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1122 | recordLastValueLocked(&mSensorEventBuffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1123 | count += k; |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 1124 | bufferNeedsSorting = true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 1129 | // handle runtime sensors |
| 1130 | { |
| 1131 | size_t k = 0; |
| 1132 | while (!mRuntimeSensorEventQueue.empty()) { |
| 1133 | if (count + k >= minBufferSize) { |
| 1134 | ALOGE("buffer too small to hold all events: count=%zd, k=%zu, size=%zu", |
| 1135 | count, k, minBufferSize); |
| 1136 | break; |
| 1137 | } |
| 1138 | mSensorEventBuffer[count + k] = mRuntimeSensorEventQueue.front(); |
| 1139 | mRuntimeSensorEventQueue.pop(); |
| 1140 | k++; |
| 1141 | } |
| 1142 | if (k) { |
| 1143 | // record the last synthesized values |
| 1144 | recordLastValueLocked(&mSensorEventBuffer[count], k); |
| 1145 | count += k; |
| 1146 | bufferNeedsSorting = true; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | if (bufferNeedsSorting) { |
| 1151 | // sort the buffer by time-stamps |
| 1152 | sortEventBuffer(mSensorEventBuffer, count); |
| 1153 | } |
| 1154 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1155 | // handle backward compatibility for RotationVector sensor |
| 1156 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 1157 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1158 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1159 | // All the 4 components of the quaternion should be available |
| 1160 | // No heading accuracy. Set it to -1 |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1161 | mSensorEventBuffer[i].data[4] = -1; |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1166 | // Cache the list of active connections, since we use it in multiple places below but won't |
| 1167 | // modify it here |
| 1168 | const std::vector<sp<SensorEventConnection>> activeConnections = connLock.getActiveConnections(); |
| 1169 | |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1170 | for (int i = 0; i < count; ++i) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1171 | // Map flush_complete_events in the buffer to SensorEventConnections which called flush |
| 1172 | // on the hardware sensor. mapFlushEventsToConnections[i] will be the |
| 1173 | // SensorEventConnection mapped to the corresponding flush_complete_event in |
| 1174 | // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise). |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 1175 | mMapFlushEventsToConnections[i] = nullptr; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1176 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) { |
| 1177 | const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor; |
| 1178 | SensorRecord* rec = mActiveSensors.valueFor(sensor_handle); |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 1179 | if (rec != nullptr) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1180 | mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection(); |
| 1181 | rec->removeFirstPendingFlushConnection(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1182 | } |
| 1183 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1184 | |
| 1185 | // handle dynamic sensor meta events, process registration and unregistration of dynamic |
| 1186 | // sensor based on content of event. |
| 1187 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META) { |
| 1188 | if (mSensorEventBuffer[i].dynamic_sensor_meta.connected) { |
| 1189 | int handle = mSensorEventBuffer[i].dynamic_sensor_meta.handle; |
| 1190 | const sensor_t& dynamicSensor = |
| 1191 | *(mSensorEventBuffer[i].dynamic_sensor_meta.sensor); |
| 1192 | ALOGI("Dynamic sensor handle 0x%x connected, type %d, name %s", |
| 1193 | handle, dynamicSensor.type, dynamicSensor.name); |
| 1194 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1195 | if (mSensors.isNewHandle(handle)) { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1196 | const auto& uuid = mSensorEventBuffer[i].dynamic_sensor_meta.uuid; |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1197 | sensor_t s = dynamicSensor; |
| 1198 | // make sure the dynamic sensor flag is set |
| 1199 | s.flags |= DYNAMIC_SENSOR_MASK; |
| 1200 | // force the handle to be consistent |
| 1201 | s.handle = handle; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1202 | |
| 1203 | SensorInterface *si = new HardwareSensor(s, uuid); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1204 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1205 | // This will release hold on dynamic sensor meta, so it should be called |
| 1206 | // after Sensor object is created. |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1207 | device.handleDynamicSensorConnection(handle, true /*connected*/); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1208 | registerDynamicSensorLocked(si); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1209 | } else { |
| 1210 | ALOGE("Handle %d has been used, cannot use again before reboot.", handle); |
| 1211 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1212 | } else { |
| 1213 | int handle = mSensorEventBuffer[i].dynamic_sensor_meta.handle; |
| 1214 | ALOGI("Dynamic sensor handle 0x%x disconnected", handle); |
| 1215 | |
| 1216 | device.handleDynamicSensorConnection(handle, false /*connected*/); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1217 | if (!unregisterDynamicSensorLocked(handle)) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1218 | ALOGE("Dynamic sensor release error."); |
| 1219 | } |
| 1220 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1221 | for (const sp<SensorEventConnection>& connection : activeConnections) { |
| 1222 | connection->removeSensor(handle); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1228 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 1229 | // lock if none of the clients need it. |
| 1230 | bool needsWakeLock = false; |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1231 | for (const sp<SensorEventConnection>& connection : activeConnections) { |
| 1232 | connection->sendEvents(mSensorEventBuffer, count, mSensorEventScratch, |
| 1233 | mMapFlushEventsToConnections); |
| 1234 | needsWakeLock |= connection->needsWakeLock(); |
| 1235 | // If the connection has one-shot sensors, it may be cleaned up after first trigger. |
| 1236 | // Early check for one-shot sensors. |
| 1237 | if (connection->hasOneShotSensors()) { |
| 1238 | cleanupAutoDisabledSensorLocked(connection, mSensorEventBuffer, count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1239 | } |
| 1240 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1241 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1242 | if (mWakeLockAcquired && !needsWakeLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1243 | setWakeLockAcquiredLocked(false); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1244 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1245 | } while (!Thread::exitPending()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1246 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1247 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 1248 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1252 | sp<Looper> SensorService::getLooper() const { |
| 1253 | return mLooper; |
| 1254 | } |
| 1255 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1256 | void SensorService::resetAllWakeLockRefCounts() { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1257 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 1258 | for (const sp<SensorEventConnection>& connection : connLock.getActiveConnections()) { |
| 1259 | connection->resetWakeLockRefCount(); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1260 | } |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1261 | setWakeLockAcquiredLocked(false); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void SensorService::setWakeLockAcquiredLocked(bool acquire) { |
| 1265 | if (acquire) { |
| 1266 | if (!mWakeLockAcquired) { |
| 1267 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 1268 | mWakeLockAcquired = true; |
| 1269 | } |
| 1270 | mLooper->wake(); |
| 1271 | } else { |
| 1272 | if (mWakeLockAcquired) { |
| 1273 | release_wake_lock(WAKE_LOCK_NAME); |
| 1274 | mWakeLockAcquired = false; |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1279 | bool SensorService::isWakeLockAcquired() { |
| 1280 | Mutex::Autolock _l(mLock); |
| 1281 | return mWakeLockAcquired; |
| 1282 | } |
| 1283 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1284 | bool SensorService::SensorEventAckReceiver::threadLoop() { |
| 1285 | ALOGD("new thread SensorEventAckReceiver"); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1286 | sp<Looper> looper = mService->getLooper(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1287 | do { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1288 | bool wakeLockAcquired = mService->isWakeLockAcquired(); |
| 1289 | int timeout = -1; |
| 1290 | if (wakeLockAcquired) timeout = 5000; |
| 1291 | int ret = looper->pollOnce(timeout); |
| 1292 | if (ret == ALOOPER_POLL_TIMEOUT) { |
| 1293 | mService->resetAllWakeLockRefCounts(); |
| 1294 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1295 | } while(!Thread::exitPending()); |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1299 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 1300 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 1301 | for (size_t i = 0; i < count; i++) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1302 | if (buffer[i].type == SENSOR_TYPE_META_DATA || |
| 1303 | buffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META || |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1304 | buffer[i].type == SENSOR_TYPE_ADDITIONAL_INFO) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1305 | continue; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 1306 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1307 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1308 | auto logger = mRecentEvent.find(buffer[i].sensor); |
| 1309 | if (logger != mRecentEvent.end()) { |
| 1310 | logger->second->addEvent(buffer[i]); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1311 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 1312 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1315 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1316 | struct compar { |
| 1317 | static int cmp(void const* lhs, void const* rhs) { |
| 1318 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 1319 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 1320 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1321 | } |
| 1322 | }; |
| 1323 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 1324 | } |
| 1325 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 1326 | String8 SensorService::getSensorName(int handle) const { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1327 | return mSensors.getName(handle); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 1328 | } |
| 1329 | |
Arthur Ishiguro | 883748c | 2020-10-28 13:18:02 -0700 | [diff] [blame] | 1330 | String8 SensorService::getSensorStringType(int handle) const { |
| 1331 | return mSensors.getStringType(handle); |
| 1332 | } |
| 1333 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1334 | bool SensorService::isVirtualSensor(int handle) const { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1335 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1336 | return sensor != nullptr && sensor->isVirtual(); |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1339 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame] | 1340 | int handle = event.sensor; |
| 1341 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 1342 | handle = event.meta_data.sensor; |
| 1343 | } |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1344 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1345 | return sensor != nullptr && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 1348 | int32_t SensorService::getIdFromUuid(const Sensor::uuid_t &uuid) const { |
| 1349 | if ((uuid.i64[0] == 0) && (uuid.i64[1] == 0)) { |
| 1350 | // UUID is not supported for this device. |
| 1351 | return 0; |
| 1352 | } |
| 1353 | if ((uuid.i64[0] == INT64_C(~0)) && (uuid.i64[1] == INT64_C(~0))) { |
| 1354 | // This sensor can be uniquely identified in the system by |
| 1355 | // the combination of its type and name. |
| 1356 | return -1; |
| 1357 | } |
| 1358 | |
| 1359 | // We have a dynamic sensor. |
| 1360 | |
| 1361 | if (!sHmacGlobalKeyIsValid) { |
lifr | 3ab0a1b | 2021-08-06 00:14:26 +0800 | [diff] [blame] | 1362 | // Rather than risk exposing UUIDs, we slow down dynamic sensors. |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 1363 | ALOGW("HMAC key failure; dynamic sensor getId() will be wrong."); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | // We want each app author/publisher to get a different ID, so that the |
| 1368 | // same dynamic sensor cannot be tracked across apps by multiple |
| 1369 | // authors/publishers. So we use both our UUID and our User ID. |
| 1370 | // Note potential confusion: |
| 1371 | // UUID => Universally Unique Identifier. |
| 1372 | // UID => User Identifier. |
| 1373 | // We refrain from using "uid" except as needed by API to try to |
| 1374 | // keep this distinction clear. |
| 1375 | |
| 1376 | auto appUserId = IPCThreadState::self()->getCallingUid(); |
| 1377 | uint8_t uuidAndApp[sizeof(uuid) + sizeof(appUserId)]; |
| 1378 | memcpy(uuidAndApp, &uuid, sizeof(uuid)); |
| 1379 | memcpy(uuidAndApp + sizeof(uuid), &appUserId, sizeof(appUserId)); |
| 1380 | |
| 1381 | // Now we use our key on our UUID/app combo to get the hash. |
| 1382 | uint8_t hash[EVP_MAX_MD_SIZE]; |
| 1383 | unsigned int hashLen; |
| 1384 | if (HMAC(EVP_sha256(), |
| 1385 | sHmacGlobalKey, sizeof(sHmacGlobalKey), |
| 1386 | uuidAndApp, sizeof(uuidAndApp), |
| 1387 | hash, &hashLen) == nullptr) { |
lifr | 3ab0a1b | 2021-08-06 00:14:26 +0800 | [diff] [blame] | 1388 | // Rather than risk exposing UUIDs, we slow down dynamic sensors. |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 1389 | ALOGW("HMAC failure; dynamic sensor getId() will be wrong."); |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | int32_t id = 0; |
| 1394 | if (hashLen < sizeof(id)) { |
| 1395 | // We never expect this case, but out of paranoia, we handle it. |
| 1396 | // Our 'id' length is already quite small, we don't want the |
| 1397 | // effective length of it to be even smaller. |
| 1398 | // Rather than risk exposing UUIDs, we cripple dynamic sensors. |
| 1399 | ALOGW("HMAC insufficient; dynamic sensor getId() will be wrong."); |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | // This is almost certainly less than all of 'hash', but it's as secure |
| 1404 | // as we can be with our current 'id' length. |
| 1405 | memcpy(&id, hash, sizeof(id)); |
| 1406 | |
| 1407 | // Note at the beginning of the function that we return the values of |
| 1408 | // 0 and -1 to represent special cases. As a result, we can't return |
| 1409 | // those as dynamic sensor IDs. If we happened to hash to one of those |
| 1410 | // values, we change 'id' so we report as a dynamic sensor, and not as |
| 1411 | // one of those special cases. |
| 1412 | if (id == -1) { |
| 1413 | id = -2; |
| 1414 | } else if (id == 0) { |
| 1415 | id = 1; |
| 1416 | } |
| 1417 | return id; |
| 1418 | } |
| 1419 | |
| 1420 | void SensorService::makeUuidsIntoIdsForSensorList(Vector<Sensor> &sensorList) const { |
| 1421 | for (auto &sensor : sensorList) { |
| 1422 | int32_t id = getIdFromUuid(sensor.getUuid()); |
| 1423 | sensor.setId(id); |
Eric Laurent | e3f27df | 2022-01-05 19:20:32 +0100 | [diff] [blame] | 1424 | // The sensor UUID must always be anonymized here for non privileged clients. |
| 1425 | // There is no other checks after this point before returning to client process. |
| 1426 | if (!isAudioServerOrSystemServerUid(IPCThreadState::self()->getCallingUid())) { |
| 1427 | sensor.anonymizeUuid(); |
| 1428 | } |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Anh Pham | 4e29133 | 2021-02-10 14:17:56 +0100 | [diff] [blame] | 1432 | Vector<Sensor> SensorService::getSensorList(const String16& opPackageName) { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 1433 | char value[PROPERTY_VALUE_MAX]; |
| 1434 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1435 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1436 | mSensors.getUserDebugSensors() : mSensors.getUserSensors(); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1437 | Vector<Sensor> accessibleSensorList; |
Anh Pham | 4e29133 | 2021-02-10 14:17:56 +0100 | [diff] [blame] | 1438 | |
Brian Duddie | 0d4ac56 | 2022-05-23 17:47:50 -0700 | [diff] [blame] | 1439 | resetTargetSdkVersionCache(opPackageName); |
Anh Pham | 4e29133 | 2021-02-10 14:17:56 +0100 | [diff] [blame] | 1440 | bool isCapped = isRateCappedBasedOnPermission(opPackageName); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1441 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 1442 | Sensor sensor = initialSensorList[i]; |
Anh Pham | 4e29133 | 2021-02-10 14:17:56 +0100 | [diff] [blame] | 1443 | if (isCapped && isSensorInCappedSet(sensor.getType())) { |
| 1444 | sensor.capMinDelayMicros(SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS / 1000); |
| 1445 | sensor.capHighestDirectReportRateLevel(SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL); |
| 1446 | } |
Nick Vaccaro | 2c588c5 | 2016-11-23 08:44:15 -0800 | [diff] [blame] | 1447 | accessibleSensorList.add(sensor); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 1448 | } |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 1449 | makeUuidsIntoIdsForSensorList(accessibleSensorList); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1450 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 1453 | void SensorService::addSensorIfAccessible(const String16& opPackageName, const Sensor& sensor, |
| 1454 | Vector<Sensor>& accessibleSensorList) { |
| 1455 | if (canAccessSensor(sensor, "can't see", opPackageName)) { |
| 1456 | accessibleSensorList.add(sensor); |
| 1457 | } else if (sensor.getType() != SENSOR_TYPE_HEAD_TRACKER) { |
| 1458 | ALOGI("Skipped sensor %s because it requires permission %s and app op %" PRId32, |
| 1459 | sensor.getName().string(), sensor.getRequiredPermission().string(), |
| 1460 | sensor.getRequiredAppOp()); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1464 | Vector<Sensor> SensorService::getDynamicSensorList(const String16& opPackageName) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1465 | Vector<Sensor> accessibleSensorList; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1466 | mSensors.forEachSensor( |
Brian Duddie | 4a4d046 | 2022-05-09 16:49:49 -0700 | [diff] [blame] | 1467 | [this, &opPackageName, &accessibleSensorList] (const Sensor& sensor) -> bool { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1468 | if (sensor.isDynamicSensor()) { |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame^] | 1469 | addSensorIfAccessible(opPackageName, sensor, accessibleSensorList); |
| 1470 | } |
| 1471 | return true; |
| 1472 | }); |
| 1473 | makeUuidsIntoIdsForSensorList(accessibleSensorList); |
| 1474 | return accessibleSensorList; |
| 1475 | } |
| 1476 | |
| 1477 | Vector<Sensor> SensorService::getRuntimeSensorList(const String16& opPackageName, int deviceId) { |
| 1478 | Vector<Sensor> accessibleSensorList; |
| 1479 | mSensors.forEachEntry( |
| 1480 | [this, &opPackageName, deviceId, &accessibleSensorList] ( |
| 1481 | const SensorServiceUtil::SensorList::Entry& e) -> bool { |
| 1482 | if (e.deviceId == deviceId) { |
| 1483 | addSensorIfAccessible(opPackageName, e.si->getSensor(), accessibleSensorList); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1484 | } |
| 1485 | return true; |
| 1486 | }); |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 1487 | makeUuidsIntoIdsForSensorList(accessibleSensorList); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1488 | return accessibleSensorList; |
| 1489 | } |
| 1490 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1491 | sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName, |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 1492 | int requestedMode, const String16& opPackageName, const String16& attributionTag) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1493 | // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION. |
| 1494 | if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 1495 | return nullptr; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1496 | } |
Brian Duddie | 0d4ac56 | 2022-05-23 17:47:50 -0700 | [diff] [blame] | 1497 | resetTargetSdkVersionCache(opPackageName); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1498 | |
| 1499 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1500 | // To create a client in DATA_INJECTION mode to inject data, SensorService should already be |
| 1501 | // operating in DI mode. |
| 1502 | if (requestedMode == DATA_INJECTION) { |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 1503 | if (mCurrentOperatingMode != DATA_INJECTION) return nullptr; |
| 1504 | if (!isWhiteListedPackage(packageName)) return nullptr; |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 1507 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Peng Xu | 58d450a | 2017-06-08 15:08:39 -0700 | [diff] [blame] | 1508 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 1509 | |
| 1510 | String8 connPackageName = |
| 1511 | (packageName == "") ? String8::format("unknown_package_pid_%d", pid) : packageName; |
| 1512 | String16 connOpPackageName = |
| 1513 | (opPackageName == String16("")) ? String16(connPackageName) : opPackageName; |
| 1514 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid, connPackageName, |
Arthur Ishiguro | 340882c | 2021-02-18 15:17:44 -0800 | [diff] [blame] | 1515 | requestedMode == DATA_INJECTION, connOpPackageName, attributionTag)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1516 | if (requestedMode == DATA_INJECTION) { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1517 | mConnectionHolder.addEventConnectionIfNotPresent(result); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1518 | // Add the associated file descriptor to the Looper for polling whenever there is data to |
| 1519 | // be injected. |
| 1520 | result->updateLooperRegistration(mLooper); |
| 1521 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1522 | return result; |
| 1523 | } |
| 1524 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1525 | int SensorService::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1526 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1527 | return (mCurrentOperatingMode == DATA_INJECTION); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1530 | sp<ISensorEventConnection> SensorService::createSensorDirectConnection( |
| 1531 | const String16& opPackageName, uint32_t size, int32_t type, int32_t format, |
| 1532 | const native_handle *resource) { |
Brian Duddie | 0d4ac56 | 2022-05-23 17:47:50 -0700 | [diff] [blame] | 1533 | resetTargetSdkVersionCache(opPackageName); |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1534 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1535 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 1536 | // No new direct connections are allowed when sensor privacy is enabled |
| 1537 | if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
| 1538 | ALOGE("Cannot create new direct connections when sensor privacy is enabled"); |
| 1539 | return nullptr; |
| 1540 | } |
| 1541 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1542 | struct sensors_direct_mem_t mem = { |
| 1543 | .type = type, |
| 1544 | .format = format, |
| 1545 | .size = size, |
| 1546 | .handle = resource, |
| 1547 | }; |
| 1548 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 1549 | |
| 1550 | if (mem.handle == nullptr) { |
| 1551 | ALOGE("Failed to clone resource handle"); |
| 1552 | return nullptr; |
| 1553 | } |
| 1554 | |
| 1555 | // check format |
| 1556 | if (format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { |
| 1557 | ALOGE("Direct channel format %d is unsupported!", format); |
| 1558 | return nullptr; |
| 1559 | } |
| 1560 | |
| 1561 | // check for duplication |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1562 | for (const sp<SensorDirectConnection>& connection : connLock.getDirectConnections()) { |
| 1563 | if (connection->isEquivalent(&mem)) { |
Peng Xu | f88e2b9 | 2017-04-10 15:52:58 -0700 | [diff] [blame] | 1564 | ALOGE("Duplicate create channel request for the same share memory"); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1565 | return nullptr; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | // check specific to memory type |
| 1570 | switch(type) { |
| 1571 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: { // channel backed by ashmem |
Brian Duddie | 0eb4624 | 2018-02-15 15:02:29 -0800 | [diff] [blame] | 1572 | if (resource->numFds < 1) { |
| 1573 | ALOGE("Ashmem direct channel requires a memory region to be supplied"); |
| 1574 | android_errorWriteLog(0x534e4554, "70986337"); // SafetyNet |
| 1575 | return nullptr; |
| 1576 | } |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1577 | int fd = resource->data[0]; |
Anthony Stange | 3de9119 | 2019-11-21 18:35:04 -0500 | [diff] [blame] | 1578 | if (!ashmem_valid(fd)) { |
| 1579 | ALOGE("Supplied Ashmem memory region is invalid"); |
| 1580 | return nullptr; |
| 1581 | } |
| 1582 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1583 | int size2 = ashmem_get_size_region(fd); |
| 1584 | // check size consistency |
Brian Duddie | 0eb4624 | 2018-02-15 15:02:29 -0800 | [diff] [blame] | 1585 | if (size2 < static_cast<int64_t>(size)) { |
Peng Xu | f88e2b9 | 2017-04-10 15:52:58 -0700 | [diff] [blame] | 1586 | ALOGE("Ashmem direct channel size %" PRIu32 " greater than shared memory size %d", |
| 1587 | size, size2); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1588 | return nullptr; |
| 1589 | } |
| 1590 | break; |
| 1591 | } |
| 1592 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: |
Peng Xu | f88e2b9 | 2017-04-10 15:52:58 -0700 | [diff] [blame] | 1593 | // no specific checks for gralloc |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1594 | break; |
| 1595 | default: |
| 1596 | ALOGE("Unknown direct connection memory type %d", type); |
| 1597 | return nullptr; |
| 1598 | } |
| 1599 | |
| 1600 | native_handle_t *clone = native_handle_clone(resource); |
| 1601 | if (!clone) { |
| 1602 | return nullptr; |
| 1603 | } |
Brian Duddie | 0a4cebb | 2022-08-25 19:20:18 +0000 | [diff] [blame] | 1604 | native_handle_set_fdsan_tag(clone); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1605 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1606 | sp<SensorDirectConnection> conn; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1607 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1608 | int channelHandle = dev.registerDirectChannel(&mem); |
| 1609 | |
| 1610 | if (channelHandle <= 0) { |
| 1611 | ALOGE("SensorDevice::registerDirectChannel returns %d", channelHandle); |
| 1612 | } else { |
| 1613 | mem.handle = clone; |
| 1614 | conn = new SensorDirectConnection(this, uid, &mem, channelHandle, opPackageName); |
| 1615 | } |
| 1616 | |
| 1617 | if (conn == nullptr) { |
Brian Duddie | 0a4cebb | 2022-08-25 19:20:18 +0000 | [diff] [blame] | 1618 | native_handle_close_with_tag(clone); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1619 | native_handle_delete(clone); |
| 1620 | } else { |
| 1621 | // add to list of direct connections |
| 1622 | // sensor service should never hold pointer or sp of SensorDirectConnection object. |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1623 | mConnectionHolder.addDirectConnection(conn); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1624 | } |
| 1625 | return conn; |
| 1626 | } |
| 1627 | |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 1628 | int SensorService::setOperationParameter( |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 1629 | int32_t handle, int32_t type, |
| 1630 | const Vector<float> &floats, const Vector<int32_t> &ints) { |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 1631 | Mutex::Autolock _l(mLock); |
| 1632 | |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 1633 | if (!checkCallingPermission(sLocationHardwarePermission, nullptr, nullptr)) { |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 1634 | return PERMISSION_DENIED; |
| 1635 | } |
| 1636 | |
| 1637 | bool isFloat = true; |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 1638 | bool isCustom = false; |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 1639 | size_t expectSize = INT32_MAX; |
| 1640 | switch (type) { |
| 1641 | case AINFO_LOCAL_GEOMAGNETIC_FIELD: |
| 1642 | isFloat = true; |
| 1643 | expectSize = 3; |
| 1644 | break; |
| 1645 | case AINFO_LOCAL_GRAVITY: |
| 1646 | isFloat = true; |
| 1647 | expectSize = 1; |
| 1648 | break; |
| 1649 | case AINFO_DOCK_STATE: |
| 1650 | case AINFO_HIGH_PERFORMANCE_MODE: |
| 1651 | case AINFO_MAGNETIC_FIELD_CALIBRATION: |
| 1652 | isFloat = false; |
| 1653 | expectSize = 1; |
| 1654 | break; |
| 1655 | default: |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 1656 | // CUSTOM events must only contain float data; it may have variable size |
| 1657 | if (type < AINFO_CUSTOM_START || type >= AINFO_DEBUGGING_START || |
| 1658 | ints.size() || |
| 1659 | sizeof(additional_info_event_t::data_float)/sizeof(float) < floats.size() || |
| 1660 | handle < 0) { |
| 1661 | return BAD_VALUE; |
| 1662 | } |
| 1663 | isFloat = true; |
| 1664 | isCustom = true; |
| 1665 | expectSize = floats.size(); |
| 1666 | break; |
| 1667 | } |
| 1668 | |
| 1669 | if (!isCustom && handle != -1) { |
| 1670 | return BAD_VALUE; |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // three events: first one is begin tag, last one is end tag, the one in the middle |
| 1674 | // is the payload. |
| 1675 | sensors_event_t event[3]; |
| 1676 | int64_t timestamp = elapsedRealtimeNano(); |
| 1677 | for (sensors_event_t* i = event; i < event + 3; i++) { |
| 1678 | *i = (sensors_event_t) { |
| 1679 | .version = sizeof(sensors_event_t), |
Alexey Polyudov | 88711e8 | 2017-05-23 19:54:04 -0700 | [diff] [blame] | 1680 | .sensor = handle, |
Peng Xu | dd5c5cb | 2017-03-16 17:39:43 -0700 | [diff] [blame] | 1681 | .type = SENSOR_TYPE_ADDITIONAL_INFO, |
| 1682 | .timestamp = timestamp++, |
| 1683 | .additional_info = (additional_info_event_t) { |
| 1684 | .serial = 0 |
| 1685 | } |
| 1686 | }; |
| 1687 | } |
| 1688 | |
| 1689 | event[0].additional_info.type = AINFO_BEGIN; |
| 1690 | event[1].additional_info.type = type; |
| 1691 | event[2].additional_info.type = AINFO_END; |
| 1692 | |
| 1693 | if (isFloat) { |
| 1694 | if (floats.size() != expectSize) { |
| 1695 | return BAD_VALUE; |
| 1696 | } |
| 1697 | for (size_t i = 0; i < expectSize; ++i) { |
| 1698 | event[1].additional_info.data_float[i] = floats[i]; |
| 1699 | } |
| 1700 | } else { |
| 1701 | if (ints.size() != expectSize) { |
| 1702 | return BAD_VALUE; |
| 1703 | } |
| 1704 | for (size_t i = 0; i < expectSize; ++i) { |
| 1705 | event[1].additional_info.data_int32[i] = ints[i]; |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1710 | for (sensors_event_t* i = event; i < event + 3; i++) { |
| 1711 | int ret = dev.injectSensorData(i); |
| 1712 | if (ret != NO_ERROR) { |
| 1713 | return ret; |
| 1714 | } |
| 1715 | } |
| 1716 | return NO_ERROR; |
| 1717 | } |
| 1718 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1719 | status_t SensorService::resetToNormalMode() { |
| 1720 | Mutex::Autolock _l(mLock); |
| 1721 | return resetToNormalModeLocked(); |
| 1722 | } |
| 1723 | |
| 1724 | status_t SensorService::resetToNormalModeLocked() { |
| 1725 | SensorDevice& dev(SensorDevice::getInstance()); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1726 | status_t err = dev.setMode(NORMAL); |
Aniroop Mathur | bfac17e | 2016-08-31 22:59:44 +0530 | [diff] [blame] | 1727 | if (err == NO_ERROR) { |
| 1728 | mCurrentOperatingMode = NORMAL; |
| 1729 | dev.enableAllSensors(); |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 1730 | checkAndReportProxStateChangeLocked(); |
Aniroop Mathur | bfac17e | 2016-08-31 22:59:44 +0530 | [diff] [blame] | 1731 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1732 | return err; |
| 1733 | } |
| 1734 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1735 | void SensorService::cleanupConnection(SensorEventConnection* c) { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1736 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1737 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1738 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1739 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1740 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1741 | int handle = mActiveSensors.keyAt(i); |
| 1742 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1743 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1744 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1745 | if (sensor != nullptr) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1746 | sensor->activate(c, false); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1747 | } else { |
| 1748 | ALOGE("sensor interface of handle=0x%08x is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1749 | } |
Brian Duddie | 7be8557 | 2021-12-21 10:44:55 -0800 | [diff] [blame] | 1750 | if (c->removeSensor(handle)) { |
| 1751 | BatteryService::disableSensor(c->getUid(), handle); |
| 1752 | } |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1753 | } |
| 1754 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1755 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1756 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1757 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 1758 | c, i, handle); |
| 1759 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1760 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1761 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1762 | mActiveSensors.removeItemsAt(i, 1); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1763 | mActiveVirtualSensors.erase(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1764 | delete rec; |
| 1765 | size--; |
| 1766 | } else { |
| 1767 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1768 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1769 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1770 | c->updateLooperRegistration(mLooper); |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1771 | mConnectionHolder.removeEventConnection(connection); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1772 | if (c->needsWakeLock()) { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1773 | checkWakeLockStateLocked(&connLock); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1774 | } |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 1775 | |
| 1776 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1777 | dev.notifyConnectionDestroyed(c); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1780 | void SensorService::cleanupConnection(SensorDirectConnection* c) { |
| 1781 | Mutex::Autolock _l(mLock); |
| 1782 | |
| 1783 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1784 | dev.unregisterDirectChannel(c->getHalChannelHandle()); |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1785 | mConnectionHolder.removeDirectConnection(c); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 1788 | void SensorService::checkAndReportProxStateChangeLocked() { |
| 1789 | if (mProxSensorHandles.empty()) return; |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 1790 | |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 1791 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1792 | bool isActive = false; |
| 1793 | for (auto& sensor : mProxSensorHandles) { |
| 1794 | if (dev.isSensorActive(sensor)) { |
| 1795 | isActive = true; |
| 1796 | break; |
| 1797 | } |
| 1798 | } |
| 1799 | if (isActive != mLastReportedProxIsActive) { |
| 1800 | notifyProximityStateLocked(isActive, mProximityActiveListeners); |
| 1801 | mLastReportedProxIsActive = isActive; |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | void SensorService::notifyProximityStateLocked( |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 1806 | const bool isActive, |
Santos Cordon | 7ea287a | 2021-06-14 13:45:29 +0100 | [diff] [blame] | 1807 | const std::vector<sp<ProximityActiveListener>>& listeners) { |
Santos Cordon | 7ea287a | 2021-06-14 13:45:29 +0100 | [diff] [blame] | 1808 | const uint64_t mySeq = ++curProxCallbackSeq; |
| 1809 | std::thread t([isActive, mySeq, listenersCopy = listeners]() { |
| 1810 | while (completedCallbackSeq.load() != mySeq - 1) |
| 1811 | std::this_thread::sleep_for(1ms); |
| 1812 | for (auto& listener : listenersCopy) |
| 1813 | listener->onProximityActive(isActive); |
| 1814 | completedCallbackSeq++; |
| 1815 | }); |
| 1816 | t.detach(); |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | status_t SensorService::addProximityActiveListener(const sp<ProximityActiveListener>& callback) { |
| 1820 | if (callback == nullptr) { |
| 1821 | return BAD_VALUE; |
| 1822 | } |
| 1823 | |
| 1824 | Mutex::Autolock _l(mLock); |
| 1825 | |
| 1826 | // Check if the callback was already added. |
| 1827 | for (const auto& cb : mProximityActiveListeners) { |
| 1828 | if (cb == callback) { |
| 1829 | return ALREADY_EXISTS; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | mProximityActiveListeners.push_back(callback); |
| 1834 | std::vector<sp<ProximityActiveListener>> listener(1, callback); |
Chris Kuiper | df11ff2 | 2021-10-12 16:30:01 -0700 | [diff] [blame] | 1835 | notifyProximityStateLocked(mLastReportedProxIsActive, listener); |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 1836 | return OK; |
| 1837 | } |
| 1838 | |
| 1839 | status_t SensorService::removeProximityActiveListener( |
| 1840 | const sp<ProximityActiveListener>& callback) { |
| 1841 | if (callback == nullptr) { |
| 1842 | return BAD_VALUE; |
| 1843 | } |
| 1844 | |
| 1845 | Mutex::Autolock _l(mLock); |
| 1846 | |
| 1847 | for (auto iter = mProximityActiveListeners.begin(); |
| 1848 | iter != mProximityActiveListeners.end(); |
| 1849 | ++iter) { |
| 1850 | if (*iter == callback) { |
| 1851 | mProximityActiveListeners.erase(iter); |
| 1852 | return OK; |
| 1853 | } |
| 1854 | } |
| 1855 | return NAME_NOT_FOUND; |
| 1856 | } |
| 1857 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1858 | sp<SensorInterface> SensorService::getSensorInterfaceFromHandle(int handle) const { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1859 | return mSensors.getInterface(handle); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1862 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1863 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1864 | const String16& opPackageName) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1865 | if (mInitCheck != NO_ERROR) |
| 1866 | return mInitCheck; |
| 1867 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1868 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1869 | if (sensor == nullptr || |
| 1870 | !canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1871 | return BAD_VALUE; |
| 1872 | } |
| 1873 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1874 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 1875 | if (mCurrentOperatingMode != NORMAL |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1876 | && !isWhiteListedPackage(connection->getPackageName())) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1877 | return INVALID_OPERATION; |
| 1878 | } |
| 1879 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1880 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 1881 | if (rec == nullptr) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1882 | rec = new SensorRecord(connection); |
| 1883 | mActiveSensors.add(handle, rec); |
| 1884 | if (sensor->isVirtual()) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1885 | mActiveVirtualSensors.emplace(handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1886 | } |
Brian Stack | 26029ee | 2019-04-22 17:25:49 -0700 | [diff] [blame] | 1887 | |
| 1888 | // There was no SensorRecord for this sensor which means it was previously disabled. Mark |
| 1889 | // the recent event as stale to ensure that the previous event is not sent to a client. This |
| 1890 | // ensures on-change events that were generated during a previous sensor activation are not |
| 1891 | // erroneously sent to newly connected clients, especially if a second client registers for |
| 1892 | // an on-change sensor before the first client receives the updated event. Once an updated |
| 1893 | // event is received, the recent events will be marked as current, and any new clients will |
| 1894 | // immediately receive the most recent event. |
| 1895 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
| 1896 | auto logger = mRecentEvent.find(handle); |
| 1897 | if (logger != mRecentEvent.end()) { |
| 1898 | logger->second->setLastEventStale(); |
| 1899 | } |
| 1900 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1901 | } else { |
| 1902 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1903 | // this sensor is already activated, but we are adding a connection that uses it. |
| 1904 | // Immediately send down the last known value of the requested sensor if it's not a |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1905 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 1906 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1907 | // NOTE: The wake_up flag of this event may get set to |
| 1908 | // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event. |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1909 | |
| 1910 | auto logger = mRecentEvent.find(handle); |
| 1911 | if (logger != mRecentEvent.end()) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1912 | sensors_event_t event; |
Brian Stack | 0b858c1 | 2018-10-19 10:53:25 -0700 | [diff] [blame] | 1913 | // Verify that the last sensor event was generated from the current activation |
| 1914 | // of the sensor. If not, it is possible for an on-change sensor to receive a |
| 1915 | // sensor event that is stale if two clients re-activate the sensor |
| 1916 | // simultaneously. |
| 1917 | if(logger->second->populateLastEventIfCurrent(&event)) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1918 | event.sensor = handle; |
| 1919 | if (event.version == sizeof(sensors_event_t)) { |
| 1920 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 1921 | setWakeLockAcquiredLocked(true); |
| 1922 | } |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 1923 | connection->sendEvents(&event, 1, nullptr); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1924 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1925 | checkWakeLockStateLocked(&connLock); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1928 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | |
Arthur Ishiguro | 062b27b | 2020-04-13 08:04:49 -0700 | [diff] [blame] | 1934 | if (connection->addSensor(handle)) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1935 | BatteryService::enableSensor(connection->getUid(), handle); |
| 1936 | // the sensor was added (which means it wasn't already there) |
| 1937 | // so, see if this connection becomes active |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 1938 | mConnectionHolder.addEventConnectionIfNotPresent(connection); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1939 | } else { |
| 1940 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 1941 | handle, connection.get()); |
| 1942 | } |
| 1943 | |
Aniroop Mathur | d8a5ce3 | 2016-06-01 22:17:05 +0530 | [diff] [blame] | 1944 | // Check maximum delay for the sensor. |
Jim Kaye | 663720b | 2017-05-08 09:07:27 -0700 | [diff] [blame] | 1945 | nsecs_t maxDelayNs = sensor->getSensor().getMaxDelay() * 1000LL; |
Aniroop Mathur | d8a5ce3 | 2016-06-01 22:17:05 +0530 | [diff] [blame] | 1946 | if (maxDelayNs > 0 && (samplingPeriodNs > maxDelayNs)) { |
| 1947 | samplingPeriodNs = maxDelayNs; |
| 1948 | } |
| 1949 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1950 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 1951 | if (samplingPeriodNs < minDelayNs) { |
| 1952 | samplingPeriodNs = minDelayNs; |
| 1953 | } |
| 1954 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1955 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" |
| 1956 | "rate=%" PRId64 " timeout== %" PRId64"", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1957 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 1958 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1959 | status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1960 | maxBatchReportLatencyNs); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1961 | |
Peng Xu | 20483c4 | 2015-10-26 15:14:43 -0700 | [diff] [blame] | 1962 | // Call flush() before calling activate() on the sensor. Wait for a first |
| 1963 | // flush complete event before sending events on this connection. Ignore |
| 1964 | // one-shot sensors which don't support flush(). Ignore on-change sensors |
| 1965 | // to maintain the on-change logic (any on-change events except the initial |
| 1966 | // one should be trigger by a change in value). Also if this sensor isn't |
| 1967 | // already active, don't call flush(). |
| 1968 | if (err == NO_ERROR && |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1969 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_CONTINUOUS && |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1970 | rec->getNumConnections() > 1) { |
| 1971 | connection->setFirstFlushPending(handle, true); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1972 | status_t err_flush = sensor->flush(connection.get(), handle); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1973 | // Flush may return error if the underlying h/w sensor uses an older HAL. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1974 | if (err_flush == NO_ERROR) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1975 | rec->addPendingFlushConnection(connection.get()); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1976 | } else { |
| 1977 | connection->setFirstFlushPending(handle, false); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1978 | } |
| 1979 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1980 | |
| 1981 | if (err == NO_ERROR) { |
| 1982 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); |
| 1983 | err = sensor->activate(connection.get(), true); |
| 1984 | } |
| 1985 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1986 | if (err == NO_ERROR) { |
| 1987 | connection->updateLooperRegistration(mLooper); |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 1988 | |
Brian Stack | 240d1d6 | 2019-05-17 09:49:39 -0700 | [diff] [blame] | 1989 | if (sensor->getSensor().getRequiredPermission().size() > 0 && |
| 1990 | sensor->getSensor().getRequiredAppOp() >= 0) { |
Brian Stack | c225aa1 | 2019-04-19 09:30:25 -0700 | [diff] [blame] | 1991 | connection->mHandleToAppOp[handle] = sensor->getSensor().getRequiredAppOp(); |
| 1992 | } |
| 1993 | |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 1994 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex) = |
| 1995 | SensorRegistrationInfo(handle, connection->getPackageName(), |
| 1996 | samplingPeriodNs, maxBatchReportLatencyNs, true); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1997 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 2000 | if (err != NO_ERROR) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 2001 | // batch/activate has failed, reset our state. |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 2002 | cleanupWithoutDisableLocked(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2003 | } |
| 2004 | return err; |
| 2005 | } |
| 2006 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 2007 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 2008 | if (mInitCheck != NO_ERROR) |
| 2009 | return mInitCheck; |
| 2010 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 2011 | Mutex::Autolock _l(mLock); |
| 2012 | status_t err = cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 2013 | if (err == NO_ERROR) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 2014 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 2015 | err = sensor != nullptr ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 2016 | |
| 2017 | } |
| 2018 | if (err == NO_ERROR) { |
Peng Xu | 5122468 | 2017-03-10 16:57:27 -0800 | [diff] [blame] | 2019 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex) = |
| 2020 | SensorRegistrationInfo(handle, connection->getPackageName(), 0, 0, false); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 2021 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 2022 | } |
| 2023 | return err; |
| 2024 | } |
| 2025 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 2026 | status_t SensorService::cleanupWithoutDisable( |
| 2027 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2028 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 2029 | return cleanupWithoutDisableLocked(connection, handle); |
| 2030 | } |
| 2031 | |
| 2032 | status_t SensorService::cleanupWithoutDisableLocked( |
| 2033 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2034 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2035 | if (rec) { |
| 2036 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 2037 | if (connection->removeSensor(handle)) { |
| 2038 | BatteryService::disableSensor(connection->getUid(), handle); |
| 2039 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2040 | if (connection->hasAnySensor() == false) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 2041 | connection->updateLooperRegistration(mLooper); |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2042 | mConnectionHolder.removeEventConnection(connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2043 | } |
| 2044 | // see if this sensor becomes inactive |
| 2045 | if (rec->removeConnection(connection)) { |
| 2046 | mActiveSensors.removeItem(handle); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 2047 | mActiveVirtualSensors.erase(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2048 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2049 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 2050 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 2051 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 2052 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 2055 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 2056 | int handle, nsecs_t ns, const String16& opPackageName) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 2057 | if (mInitCheck != NO_ERROR) |
| 2058 | return mInitCheck; |
| 2059 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 2060 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 2061 | if (sensor == nullptr || |
| 2062 | !canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2063 | return BAD_VALUE; |
| 2064 | } |
| 2065 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 2066 | if (ns < 0) |
| 2067 | return BAD_VALUE; |
| 2068 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 2069 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 2070 | if (ns < minDelayNs) { |
| 2071 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 2074 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2077 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, |
| 2078 | const String16& opPackageName) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2079 | if (mInitCheck != NO_ERROR) return mInitCheck; |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 2080 | SensorDevice& dev(SensorDevice::getInstance()); |
| 2081 | const int halVersion = dev.getHalDeviceVersion(); |
| 2082 | status_t err(NO_ERROR); |
| 2083 | Mutex::Autolock _l(mLock); |
| 2084 | // Loop through all sensors for this connection and call flush on each of them. |
Arthur Ishiguro | ad46c78 | 2020-03-26 11:24:43 -0700 | [diff] [blame] | 2085 | for (int handle : connection->getActiveSensorHandles()) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 2086 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 2087 | if (sensor == nullptr) { |
| 2088 | continue; |
| 2089 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 2090 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 2091 | ALOGE("flush called on a one-shot sensor"); |
| 2092 | err = INVALID_OPERATION; |
| 2093 | continue; |
| 2094 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 2095 | if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) { |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 2096 | // For older devices just increment pending flush count which will send a trivial |
| 2097 | // flush complete event. |
Stan Rokita | 29adc8c | 2020-07-06 17:38:03 -0700 | [diff] [blame] | 2098 | if (!connection->incrementPendingFlushCountIfHasAccess(handle)) { |
| 2099 | ALOGE("flush called on an inaccessible sensor"); |
| 2100 | err = INVALID_OPERATION; |
| 2101 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 2102 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2103 | if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) { |
| 2104 | err = INVALID_OPERATION; |
| 2105 | continue; |
| 2106 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 2107 | status_t err_flush = sensor->flush(connection.get(), handle); |
| 2108 | if (err_flush == NO_ERROR) { |
| 2109 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Yi Kong | 8f313e3 | 2018-07-17 14:13:29 -0700 | [diff] [blame] | 2110 | if (rec != nullptr) rec->addPendingFlushConnection(connection); |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 2111 | } |
| 2112 | err = (err_flush != NO_ERROR) ? err_flush : err; |
| 2113 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2114 | } |
Arthur Ishiguro | fb64fca | 2020-04-14 11:28:11 -0700 | [diff] [blame] | 2115 | return err; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 2116 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2117 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2118 | bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, |
| 2119 | const String16& opPackageName) { |
Brian Duddie | 4a4d046 | 2022-05-09 16:49:49 -0700 | [diff] [blame] | 2120 | // Special case for Head Tracker sensor type: currently restricted to system usage only, unless |
| 2121 | // the restriction is specially lifted for testing |
| 2122 | if (sensor.getType() == SENSOR_TYPE_HEAD_TRACKER && |
| 2123 | !isAudioServerOrSystemServerUid(IPCThreadState::self()->getCallingUid())) { |
| 2124 | if (!mHtRestricted) { |
| 2125 | ALOGI("Permitting access to HT sensor type outside system (%s)", |
| 2126 | String8(opPackageName).string()); |
| 2127 | } else { |
| 2128 | ALOGW("%s %s a sensor (%s) as a non-system client", String8(opPackageName).string(), |
| 2129 | operation, sensor.getName().string()); |
| 2130 | return false; |
| 2131 | } |
| 2132 | } |
| 2133 | |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2134 | // Check if a permission is required for this sensor |
| 2135 | if (sensor.getRequiredPermission().length() <= 0) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2136 | return true; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2139 | const int32_t opCode = sensor.getRequiredAppOp(); |
Brian Duddie | 457e639 | 2020-06-19 11:38:29 -0700 | [diff] [blame] | 2140 | int targetSdkVersion = getTargetSdkVersion(opPackageName); |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2141 | |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2142 | bool canAccess = false; |
Brian Duddie | 457e639 | 2020-06-19 11:38:29 -0700 | [diff] [blame] | 2143 | if (targetSdkVersion > 0 && targetSdkVersion <= __ANDROID_API_P__ && |
| 2144 | (sensor.getType() == SENSOR_TYPE_STEP_COUNTER || |
| 2145 | sensor.getType() == SENSOR_TYPE_STEP_DETECTOR)) { |
| 2146 | // Allow access to step sensors if the application targets pre-Q, which is before the |
| 2147 | // requirement to hold the AR permission to access Step Counter and Step Detector events |
| 2148 | // was introduced. |
| 2149 | canAccess = true; |
| 2150 | } else if (hasPermissionForSensor(sensor)) { |
Brian Stack | 6a700f9 | 2019-05-08 17:02:53 -0700 | [diff] [blame] | 2151 | // Ensure that the AppOp is allowed, or that there is no necessary app op for the sensor |
Arthur Ishiguro | 883748c | 2020-10-28 13:18:02 -0700 | [diff] [blame] | 2152 | if (opCode >= 0) { |
| 2153 | const int32_t appOpMode = sAppOpsManager.checkOp(opCode, |
| 2154 | IPCThreadState::self()->getCallingUid(), opPackageName); |
| 2155 | canAccess = (appOpMode == AppOpsManager::MODE_ALLOWED); |
| 2156 | } else { |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2157 | canAccess = true; |
Brian Stack | 6a700f9 | 2019-05-08 17:02:53 -0700 | [diff] [blame] | 2158 | } |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Arthur Ishiguro | 883748c | 2020-10-28 13:18:02 -0700 | [diff] [blame] | 2161 | if (!canAccess) { |
Brian Duddie | 457e639 | 2020-06-19 11:38:29 -0700 | [diff] [blame] | 2162 | ALOGE("%s %s a sensor (%s) without holding %s", String8(opPackageName).string(), |
| 2163 | operation, sensor.getName().string(), sensor.getRequiredPermission().string()); |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | return canAccess; |
| 2167 | } |
| 2168 | |
| 2169 | bool SensorService::hasPermissionForSensor(const Sensor& sensor) { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2170 | bool hasPermission = false; |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2171 | const String8& requiredPermission = sensor.getRequiredPermission(); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2172 | |
| 2173 | // Runtime permissions can't use the cache as they may change. |
| 2174 | if (sensor.isRequiredPermissionRuntime()) { |
| 2175 | hasPermission = checkPermission(String16(requiredPermission), |
matthuang | 6579446 | 2022-03-24 16:02:57 +0800 | [diff] [blame] | 2176 | IPCThreadState::self()->getCallingPid(), |
| 2177 | IPCThreadState::self()->getCallingUid(), |
| 2178 | /*logPermissionFailure=*/ false); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2179 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2180 | hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission)); |
| 2181 | } |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2182 | return hasPermission; |
| 2183 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2184 | |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2185 | int SensorService::getTargetSdkVersion(const String16& opPackageName) { |
Anthony Stange | 07eb421 | 2020-08-28 14:50:28 -0400 | [diff] [blame] | 2186 | // Don't query the SDK version for the ISensorManager descriptor as it doesn't have one. This |
| 2187 | // descriptor tends to be used for VNDK clients, but can technically be set by anyone so don't |
| 2188 | // give it elevated privileges. |
| 2189 | if (opPackageName.startsWith(sSensorInterfaceDescriptorPrefix)) { |
| 2190 | return -1; |
| 2191 | } |
| 2192 | |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2193 | Mutex::Autolock packageLock(sPackageTargetVersionLock); |
| 2194 | int targetSdkVersion = -1; |
| 2195 | auto entry = sPackageTargetVersion.find(opPackageName); |
| 2196 | if (entry != sPackageTargetVersion.end()) { |
| 2197 | targetSdkVersion = entry->second; |
| 2198 | } else { |
| 2199 | sp<IBinder> binder = defaultServiceManager()->getService(String16("package_native")); |
| 2200 | if (binder != nullptr) { |
| 2201 | sp<content::pm::IPackageManagerNative> packageManager = |
| 2202 | interface_cast<content::pm::IPackageManagerNative>(binder); |
| 2203 | if (packageManager != nullptr) { |
| 2204 | binder::Status status = packageManager->getTargetSdkVersionForPackage( |
| 2205 | opPackageName, &targetSdkVersion); |
| 2206 | if (!status.isOk()) { |
| 2207 | targetSdkVersion = -1; |
| 2208 | } |
| 2209 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2210 | } |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2211 | sPackageTargetVersion[opPackageName] = targetSdkVersion; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 2212 | } |
Brian Stack | 793f464 | 2019-04-18 17:21:34 -0700 | [diff] [blame] | 2213 | return targetSdkVersion; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Brian Duddie | 0d4ac56 | 2022-05-23 17:47:50 -0700 | [diff] [blame] | 2216 | void SensorService::resetTargetSdkVersionCache(const String16& opPackageName) { |
| 2217 | Mutex::Autolock packageLock(sPackageTargetVersionLock); |
| 2218 | auto iter = sPackageTargetVersion.find(opPackageName); |
| 2219 | if (iter != sPackageTargetVersion.end()) { |
| 2220 | sPackageTargetVersion.erase(iter); |
| 2221 | } |
| 2222 | } |
| 2223 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 2224 | void SensorService::checkWakeLockState() { |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2225 | ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock); |
| 2226 | checkWakeLockStateLocked(&connLock); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 2227 | } |
| 2228 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2229 | void SensorService::checkWakeLockStateLocked(ConnectionSafeAutolock* connLock) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 2230 | if (!mWakeLockAcquired) { |
| 2231 | return; |
| 2232 | } |
| 2233 | bool releaseLock = true; |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2234 | for (const sp<SensorEventConnection>& connection : connLock->getActiveConnections()) { |
| 2235 | if (connection->needsWakeLock()) { |
| 2236 | releaseLock = false; |
| 2237 | break; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | if (releaseLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 2241 | setWakeLockAcquiredLocked(false); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) { |
| 2246 | Mutex::Autolock _l(mLock); |
| 2247 | connection->writeToSocketFromCache(); |
| 2248 | if (connection->needsWakeLock()) { |
| 2249 | setWakeLockAcquiredLocked(true); |
| 2250 | } |
| 2251 | } |
| 2252 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 2253 | bool SensorService::isWhiteListedPackage(const String8& packageName) { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 2254 | return (packageName.contains(mWhiteListedPackage.string())); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 2255 | } |
| 2256 | |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 2257 | bool SensorService::isOperationRestrictedLocked(const String16& opPackageName) { |
Brian Stack | 5180e46 | 2019-03-08 17:15:19 -0800 | [diff] [blame] | 2258 | if (mCurrentOperatingMode == RESTRICTED) { |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 2259 | String8 package(opPackageName); |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 2260 | return !isWhiteListedPackage(package); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 2261 | } |
Arthur Ishiguro | e3ed3d2 | 2020-04-13 10:29:44 -0700 | [diff] [blame] | 2262 | return false; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 2265 | void SensorService::UidPolicy::registerSelf() { |
| 2266 | ActivityManager am; |
| 2267 | am.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE |
| 2268 | | ActivityManager::UID_OBSERVER_IDLE |
| 2269 | | ActivityManager::UID_OBSERVER_ACTIVE, |
| 2270 | ActivityManager::PROCESS_STATE_UNKNOWN, |
| 2271 | String16("android")); |
| 2272 | } |
| 2273 | |
| 2274 | void SensorService::UidPolicy::unregisterSelf() { |
| 2275 | ActivityManager am; |
| 2276 | am.unregisterUidObserver(this); |
| 2277 | } |
| 2278 | |
| 2279 | void SensorService::UidPolicy::onUidGone(__unused uid_t uid, __unused bool disabled) { |
| 2280 | onUidIdle(uid, disabled); |
| 2281 | } |
| 2282 | |
| 2283 | void SensorService::UidPolicy::onUidActive(uid_t uid) { |
| 2284 | { |
| 2285 | Mutex::Autolock _l(mUidLock); |
| 2286 | mActiveUids.insert(uid); |
| 2287 | } |
| 2288 | sp<SensorService> service = mService.promote(); |
| 2289 | if (service != nullptr) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 2290 | service->onUidStateChanged(uid, UID_STATE_ACTIVE); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | void SensorService::UidPolicy::onUidIdle(uid_t uid, __unused bool disabled) { |
| 2295 | bool deleted = false; |
| 2296 | { |
| 2297 | Mutex::Autolock _l(mUidLock); |
| 2298 | if (mActiveUids.erase(uid) > 0) { |
| 2299 | deleted = true; |
| 2300 | } |
| 2301 | } |
| 2302 | if (deleted) { |
| 2303 | sp<SensorService> service = mService.promote(); |
| 2304 | if (service != nullptr) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 2305 | service->onUidStateChanged(uid, UID_STATE_IDLE); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | void SensorService::UidPolicy::addOverrideUid(uid_t uid, bool active) { |
| 2311 | updateOverrideUid(uid, active, true); |
| 2312 | } |
| 2313 | |
| 2314 | void SensorService::UidPolicy::removeOverrideUid(uid_t uid) { |
| 2315 | updateOverrideUid(uid, false, false); |
| 2316 | } |
| 2317 | |
| 2318 | void SensorService::UidPolicy::updateOverrideUid(uid_t uid, bool active, bool insert) { |
| 2319 | bool wasActive = false; |
| 2320 | bool isActive = false; |
| 2321 | { |
| 2322 | Mutex::Autolock _l(mUidLock); |
| 2323 | wasActive = isUidActiveLocked(uid); |
| 2324 | mOverrideUids.erase(uid); |
| 2325 | if (insert) { |
| 2326 | mOverrideUids.insert(std::pair<uid_t, bool>(uid, active)); |
| 2327 | } |
| 2328 | isActive = isUidActiveLocked(uid); |
| 2329 | } |
| 2330 | if (wasActive != isActive) { |
| 2331 | sp<SensorService> service = mService.promote(); |
| 2332 | if (service != nullptr) { |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 2333 | service->onUidStateChanged(uid, isActive ? UID_STATE_ACTIVE : UID_STATE_IDLE); |
Svet Ganov | e752a5c | 2018-01-15 17:14:20 -0800 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | bool SensorService::UidPolicy::isUidActive(uid_t uid) { |
| 2339 | // Non-app UIDs are considered always active |
| 2340 | if (uid < FIRST_APPLICATION_UID) { |
| 2341 | return true; |
| 2342 | } |
| 2343 | Mutex::Autolock _l(mUidLock); |
| 2344 | return isUidActiveLocked(uid); |
| 2345 | } |
| 2346 | |
| 2347 | bool SensorService::UidPolicy::isUidActiveLocked(uid_t uid) { |
| 2348 | // Non-app UIDs are considered always active |
| 2349 | if (uid < FIRST_APPLICATION_UID) { |
| 2350 | return true; |
| 2351 | } |
| 2352 | auto it = mOverrideUids.find(uid); |
| 2353 | if (it != mOverrideUids.end()) { |
| 2354 | return it->second; |
| 2355 | } |
| 2356 | return mActiveUids.find(uid) != mActiveUids.end(); |
| 2357 | } |
| 2358 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 2359 | bool SensorService::isUidActive(uid_t uid) { |
| 2360 | return mUidPolicy->isUidActive(uid); |
| 2361 | } |
| 2362 | |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2363 | bool SensorService::isRateCappedBasedOnPermission(const String16& opPackageName) { |
| 2364 | int targetSdk = getTargetSdkVersion(opPackageName); |
Anh Pham | a553823 | 2021-06-23 08:41:11 +0200 | [diff] [blame] | 2365 | bool hasSamplingRatePermission = checkPermission(sAccessHighSensorSamplingRatePermission, |
| 2366 | IPCThreadState::self()->getCallingPid(), |
matthuang | 6579446 | 2022-03-24 16:02:57 +0800 | [diff] [blame] | 2367 | IPCThreadState::self()->getCallingUid(), |
| 2368 | /*logPermissionFailure=*/ false); |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2369 | if (targetSdk < __ANDROID_API_S__ || |
| 2370 | (targetSdk >= __ANDROID_API_S__ && hasSamplingRatePermission)) { |
| 2371 | return false; |
| 2372 | } |
| 2373 | return true; |
| 2374 | } |
| 2375 | |
Anh Pham | 3dae77f | 2021-06-11 16:24:37 +0200 | [diff] [blame] | 2376 | /** |
| 2377 | * Checks if a sensor should be capped according to HIGH_SAMPLING_RATE_SENSORS |
| 2378 | * permission. |
| 2379 | * |
| 2380 | * This needs to be kept in sync with the list defined on the Java side |
| 2381 | * in frameworks/base/core/java/android/hardware/SystemSensorManager.java |
| 2382 | */ |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2383 | bool SensorService::isSensorInCappedSet(int sensorType) { |
| 2384 | return (sensorType == SENSOR_TYPE_ACCELEROMETER |
| 2385 | || sensorType == SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED |
| 2386 | || sensorType == SENSOR_TYPE_GYROSCOPE |
| 2387 | || sensorType == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED |
| 2388 | || sensorType == SENSOR_TYPE_MAGNETIC_FIELD |
| 2389 | || sensorType == SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED); |
| 2390 | } |
| 2391 | |
| 2392 | status_t SensorService::adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* requestedPeriodNs, |
| 2393 | const String16& opPackageName) { |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2394 | if (*requestedPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) { |
| 2395 | return OK; |
| 2396 | } |
Arthur Ishiguro | 8a62852 | 2021-09-22 14:10:16 -0700 | [diff] [blame] | 2397 | bool shouldCapBasedOnPermission = isRateCappedBasedOnPermission(opPackageName); |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2398 | if (shouldCapBasedOnPermission) { |
| 2399 | *requestedPeriodNs = SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS; |
| 2400 | if (isPackageDebuggable(opPackageName)) { |
| 2401 | return PERMISSION_DENIED; |
| 2402 | } |
| 2403 | return OK; |
| 2404 | } |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2405 | if (mMicSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2406 | *requestedPeriodNs = SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS; |
| 2407 | return OK; |
| 2408 | } |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2409 | return OK; |
| 2410 | } |
| 2411 | |
| 2412 | status_t SensorService::adjustRateLevelBasedOnMicAndPermission(int* requestedRateLevel, |
| 2413 | const String16& opPackageName) { |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2414 | if (*requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) { |
| 2415 | return OK; |
| 2416 | } |
Arthur Ishiguro | 8a62852 | 2021-09-22 14:10:16 -0700 | [diff] [blame] | 2417 | bool shouldCapBasedOnPermission = isRateCappedBasedOnPermission(opPackageName); |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2418 | if (shouldCapBasedOnPermission) { |
| 2419 | *requestedRateLevel = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL; |
| 2420 | if (isPackageDebuggable(opPackageName)) { |
| 2421 | return PERMISSION_DENIED; |
| 2422 | } |
| 2423 | return OK; |
| 2424 | } |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2425 | if (mMicSensorPrivacyPolicy->isSensorPrivacyEnabled()) { |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2426 | *requestedRateLevel = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL; |
| 2427 | return OK; |
| 2428 | } |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2429 | return OK; |
| 2430 | } |
| 2431 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2432 | void SensorService::SensorPrivacyPolicy::registerSelf() { |
Anh Pham | b04658b | 2021-03-22 18:17:17 +0100 | [diff] [blame] | 2433 | AutoCallerClear acc; |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2434 | SensorPrivacyManager spm; |
| 2435 | mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled(); |
| 2436 | spm.addSensorPrivacyListener(this); |
| 2437 | } |
| 2438 | |
| 2439 | void SensorService::SensorPrivacyPolicy::unregisterSelf() { |
Anh Pham | b04658b | 2021-03-22 18:17:17 +0100 | [diff] [blame] | 2440 | AutoCallerClear acc; |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2441 | SensorPrivacyManager spm; |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2442 | spm.removeSensorPrivacyListener(this); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | bool SensorService::SensorPrivacyPolicy::isSensorPrivacyEnabled() { |
| 2446 | return mSensorPrivacyEnabled; |
| 2447 | } |
| 2448 | |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2449 | binder::Status SensorService::SensorPrivacyPolicy::onSensorPrivacyChanged(int toggleType __unused, |
| 2450 | int sensor __unused, bool enabled) { |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2451 | mSensorPrivacyEnabled = enabled; |
| 2452 | sp<SensorService> service = mService.promote(); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2453 | |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2454 | if (service != nullptr) { |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2455 | if (enabled) { |
| 2456 | service->disableAllSensors(); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2457 | } else { |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2458 | service->enableAllSensors(); |
Michael Groover | 5e1f60b | 2018-12-04 22:34:29 -0800 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | return binder::Status::ok(); |
| 2462 | } |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2463 | |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2464 | void SensorService::MicrophonePrivacyPolicy::registerSelf() { |
Anh Pham | b04658b | 2021-03-22 18:17:17 +0100 | [diff] [blame] | 2465 | AutoCallerClear acc; |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2466 | SensorPrivacyManager spm; |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2467 | mSensorPrivacyEnabled = |
| 2468 | spm.isToggleSensorPrivacyEnabled( |
| 2469 | SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, |
| 2470 | SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE) |
| 2471 | || spm.isToggleSensorPrivacyEnabled( |
| 2472 | SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, |
| 2473 | SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE); |
| 2474 | spm.addToggleSensorPrivacyListener(this); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2477 | void SensorService::MicrophonePrivacyPolicy::unregisterSelf() { |
| 2478 | AutoCallerClear acc; |
| 2479 | SensorPrivacyManager spm; |
| 2480 | spm.removeToggleSensorPrivacyListener(this); |
| 2481 | } |
| 2482 | |
| 2483 | binder::Status SensorService::MicrophonePrivacyPolicy::onSensorPrivacyChanged(int toggleType __unused, |
| 2484 | int sensor, bool enabled) { |
| 2485 | if (sensor != SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE) { |
| 2486 | return binder::Status::ok(); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2487 | } |
Evan Severson | 4c19785 | 2022-01-27 10:44:27 -0800 | [diff] [blame] | 2488 | mSensorPrivacyEnabled = enabled; |
| 2489 | sp<SensorService> service = mService.promote(); |
| 2490 | |
| 2491 | if (service != nullptr) { |
| 2492 | if (enabled) { |
| 2493 | service->capRates(); |
| 2494 | } else { |
| 2495 | service->uncapRates(); |
| 2496 | } |
| 2497 | } |
| 2498 | return binder::Status::ok(); |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 2499 | } |
| 2500 | |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2501 | SensorService::ConnectionSafeAutolock::ConnectionSafeAutolock( |
| 2502 | SensorService::SensorConnectionHolder& holder, Mutex& mutex) |
| 2503 | : mConnectionHolder(holder), mAutolock(mutex) {} |
| 2504 | |
| 2505 | template<typename ConnectionType> |
| 2506 | const std::vector<sp<ConnectionType>>& SensorService::ConnectionSafeAutolock::getConnectionsHelper( |
| 2507 | const SortedVector<wp<ConnectionType>>& connectionList, |
| 2508 | std::vector<std::vector<sp<ConnectionType>>>* referenceHolder) { |
| 2509 | referenceHolder->emplace_back(); |
| 2510 | std::vector<sp<ConnectionType>>& connections = referenceHolder->back(); |
| 2511 | for (const wp<ConnectionType>& weakConnection : connectionList){ |
| 2512 | sp<ConnectionType> connection = weakConnection.promote(); |
| 2513 | if (connection != nullptr) { |
| 2514 | connections.push_back(std::move(connection)); |
| 2515 | } |
| 2516 | } |
| 2517 | return connections; |
| 2518 | } |
| 2519 | |
| 2520 | const std::vector<sp<SensorService::SensorEventConnection>>& |
| 2521 | SensorService::ConnectionSafeAutolock::getActiveConnections() { |
| 2522 | return getConnectionsHelper(mConnectionHolder.mActiveConnections, |
| 2523 | &mReferencedActiveConnections); |
| 2524 | } |
| 2525 | |
| 2526 | const std::vector<sp<SensorService::SensorDirectConnection>>& |
| 2527 | SensorService::ConnectionSafeAutolock::getDirectConnections() { |
| 2528 | return getConnectionsHelper(mConnectionHolder.mDirectConnections, |
| 2529 | &mReferencedDirectConnections); |
| 2530 | } |
| 2531 | |
| 2532 | void SensorService::SensorConnectionHolder::addEventConnectionIfNotPresent( |
| 2533 | const sp<SensorService::SensorEventConnection>& connection) { |
| 2534 | if (mActiveConnections.indexOf(connection) < 0) { |
| 2535 | mActiveConnections.add(connection); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | void SensorService::SensorConnectionHolder::removeEventConnection( |
| 2540 | const wp<SensorService::SensorEventConnection>& connection) { |
| 2541 | mActiveConnections.remove(connection); |
| 2542 | } |
| 2543 | |
| 2544 | void SensorService::SensorConnectionHolder::addDirectConnection( |
| 2545 | const sp<SensorService::SensorDirectConnection>& connection) { |
| 2546 | mDirectConnections.add(connection); |
| 2547 | } |
| 2548 | |
| 2549 | void SensorService::SensorConnectionHolder::removeDirectConnection( |
| 2550 | const wp<SensorService::SensorDirectConnection>& connection) { |
| 2551 | mDirectConnections.remove(connection); |
| 2552 | } |
| 2553 | |
| 2554 | SensorService::ConnectionSafeAutolock SensorService::SensorConnectionHolder::lock(Mutex& mutex) { |
| 2555 | return ConnectionSafeAutolock(*this, mutex); |
| 2556 | } |
| 2557 | |
Anh Pham | af91a91 | 2021-02-10 14:10:53 +0100 | [diff] [blame] | 2558 | bool SensorService::isPackageDebuggable(const String16& opPackageName) { |
| 2559 | bool debugMode = false; |
| 2560 | sp<IBinder> binder = defaultServiceManager()->getService(String16("package_native")); |
| 2561 | if (binder != nullptr) { |
| 2562 | sp<content::pm::IPackageManagerNative> packageManager = |
| 2563 | interface_cast<content::pm::IPackageManagerNative>(binder); |
| 2564 | if (packageManager != nullptr) { |
| 2565 | binder::Status status = packageManager->isPackageDebuggable( |
| 2566 | opPackageName, &debugMode); |
| 2567 | } |
| 2568 | } |
| 2569 | return debugMode; |
| 2570 | } |
Brian Duddie | 967ce17 | 2019-06-10 11:08:27 -0700 | [diff] [blame] | 2571 | } // namespace android |