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