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