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