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