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