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