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