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