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