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