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