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