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