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