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