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 | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 25 | #include <gui/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 | } |
| 483 | if (reg_info.mActivated) { |
Peng Xu | 363b3fd | 2016-07-07 15:40:08 -0700 | [diff] [blame] | 484 | result.appendFormat("%02d:%02d:%02d activated handle=0x%08x " |
| 485 | "samplingRate=%dus maxReportLatency=%dus package=%s\n", |
| 486 | reg_info.mHour, reg_info.mMin, reg_info.mSec, reg_info.mSensorHandle, |
| 487 | reg_info.mSamplingRateUs, reg_info.mMaxReportLatencyUs, |
| 488 | reg_info.mPackageName.string()); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 489 | } else { |
Peng Xu | 363b3fd | 2016-07-07 15:40:08 -0700 | [diff] [blame] | 490 | 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] | 491 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
Peng Xu | 363b3fd | 2016-07-07 15:40:08 -0700 | [diff] [blame] | 492 | reg_info.mSensorHandle, reg_info.mPackageName.string()); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 493 | } |
| 494 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 495 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 496 | } while(startIndex != currentIndex); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 497 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 498 | } |
| 499 | write(fd, result.string(), result.size()); |
| 500 | return NO_ERROR; |
| 501 | } |
| 502 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 503 | //TODO: move to SensorEventConnection later |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 504 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 505 | sensors_event_t const* buffer, const int count) { |
| 506 | for (int i=0 ; i<count ; i++) { |
| 507 | int handle = buffer[i].sensor; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 508 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 509 | handle = buffer[i].meta_data.sensor; |
| 510 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 511 | if (connection->hasSensor(handle)) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 512 | sp<SensorInterface> si = getSensorInterfaceFromHandle(handle); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 513 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 514 | // for this particular one_shot sensor, try cleaning up the connection. |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 515 | if (si != nullptr && |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 516 | si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 517 | si->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 518 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 519 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 520 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 521 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 522 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 525 | bool SensorService::threadLoop() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 526 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 527 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 528 | // each virtual sensor could generate an event per "real" event, that's why we need to size |
| 529 | // numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. in practice, this is too |
| 530 | // aggressive, but guaranteed to be enough. |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 531 | const size_t vcount = mSensors.getVirtualSensors().size(); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 532 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 533 | const size_t numEventMax = minBufferSize / (1 + vcount); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 534 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 535 | SensorDevice& device(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 536 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 537 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 538 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 539 | ssize_t count = device.poll(mSensorEventBuffer, numEventMax); |
| 540 | if (count < 0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 541 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 542 | break; |
| 543 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 544 | |
| 545 | // Reset sensors_event_t.flags to zero for all events in the buffer. |
| 546 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 547 | mSensorEventBuffer[i].flags = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 548 | } |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 549 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 550 | // Make a copy of the connection vector as some connections may be removed during the course |
| 551 | // of this loop (especially when one-shot sensor events are present in the sensor_event |
| 552 | // buffer). Promote all connections to StrongPointers before the lock is acquired. If the |
| 553 | // destructor of the sp gets called when the lock is acquired, it may result in a deadlock |
| 554 | // as ~SensorEventConnection() needs to acquire mLock again for cleanup. So copy all the |
| 555 | // strongPointers to a vector before the lock is acquired. |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 556 | SortedVector< sp<SensorEventConnection> > activeConnections; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 557 | populateActiveConnections(&activeConnections); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 558 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 559 | Mutex::Autolock _l(mLock); |
| 560 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 561 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 562 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 563 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 564 | // releasing the wakelock. |
| 565 | bool bufferHasWakeUpEvent = false; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 566 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 567 | if (isWakeUpSensorEvent(mSensorEventBuffer[i])) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 568 | bufferHasWakeUpEvent = true; |
| 569 | break; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 573 | if (bufferHasWakeUpEvent && !mWakeLockAcquired) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 574 | setWakeLockAcquiredLocked(true); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 575 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 576 | recordLastValueLocked(mSensorEventBuffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 577 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 578 | // handle virtual sensors |
| 579 | if (count && vcount) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 580 | sensors_event_t const * const event = mSensorEventBuffer; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 581 | if (!mActiveVirtualSensors.empty()) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 582 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 583 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 584 | if (fusion.isEnabled()) { |
| 585 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 586 | fusion.process(event[i]); |
| 587 | } |
| 588 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 589 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 590 | for (int handle : mActiveVirtualSensors) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 591 | if (count + k >= minBufferSize) { |
| 592 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 593 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 594 | count, k, minBufferSize); |
| 595 | break; |
| 596 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 597 | sensors_event_t out; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 598 | sp<SensorInterface> si = mSensors.getInterface(handle); |
| 599 | if (si == nullptr) { |
| 600 | ALOGE("handle %d is not an valid virtual sensor", handle); |
| 601 | continue; |
| 602 | } |
| 603 | |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 604 | if (si->process(&out, event[i])) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 605 | mSensorEventBuffer[count + k] = out; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 606 | k++; |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | if (k) { |
| 611 | // record the last synthesized values |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 612 | recordLastValueLocked(&mSensorEventBuffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 613 | count += k; |
| 614 | // sort the buffer by time-stamps |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 615 | sortEventBuffer(mSensorEventBuffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 620 | // handle backward compatibility for RotationVector sensor |
| 621 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 622 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 623 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 624 | // All the 4 components of the quaternion should be available |
| 625 | // No heading accuracy. Set it to -1 |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 626 | mSensorEventBuffer[i].data[4] = -1; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 631 | for (int i = 0; i < count; ++i) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 632 | // Map flush_complete_events in the buffer to SensorEventConnections which called flush |
| 633 | // on the hardware sensor. mapFlushEventsToConnections[i] will be the |
| 634 | // SensorEventConnection mapped to the corresponding flush_complete_event in |
| 635 | // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise). |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 636 | mMapFlushEventsToConnections[i] = NULL; |
| 637 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) { |
| 638 | const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor; |
| 639 | SensorRecord* rec = mActiveSensors.valueFor(sensor_handle); |
| 640 | if (rec != NULL) { |
| 641 | mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection(); |
| 642 | rec->removeFirstPendingFlushConnection(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 645 | |
| 646 | // handle dynamic sensor meta events, process registration and unregistration of dynamic |
| 647 | // sensor based on content of event. |
| 648 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META) { |
| 649 | if (mSensorEventBuffer[i].dynamic_sensor_meta.connected) { |
| 650 | int handle = mSensorEventBuffer[i].dynamic_sensor_meta.handle; |
| 651 | const sensor_t& dynamicSensor = |
| 652 | *(mSensorEventBuffer[i].dynamic_sensor_meta.sensor); |
| 653 | ALOGI("Dynamic sensor handle 0x%x connected, type %d, name %s", |
| 654 | handle, dynamicSensor.type, dynamicSensor.name); |
| 655 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 656 | if (mSensors.isNewHandle(handle)) { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 657 | const auto& uuid = mSensorEventBuffer[i].dynamic_sensor_meta.uuid; |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 658 | sensor_t s = dynamicSensor; |
| 659 | // make sure the dynamic sensor flag is set |
| 660 | s.flags |= DYNAMIC_SENSOR_MASK; |
| 661 | // force the handle to be consistent |
| 662 | s.handle = handle; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 663 | |
| 664 | SensorInterface *si = new HardwareSensor(s, uuid); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 665 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 666 | // This will release hold on dynamic sensor meta, so it should be called |
| 667 | // after Sensor object is created. |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 668 | device.handleDynamicSensorConnection(handle, true /*connected*/); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 669 | registerDynamicSensorLocked(si); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 670 | } else { |
| 671 | ALOGE("Handle %d has been used, cannot use again before reboot.", handle); |
| 672 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 673 | } else { |
| 674 | int handle = mSensorEventBuffer[i].dynamic_sensor_meta.handle; |
| 675 | ALOGI("Dynamic sensor handle 0x%x disconnected", handle); |
| 676 | |
| 677 | device.handleDynamicSensorConnection(handle, false /*connected*/); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 678 | if (!unregisterDynamicSensorLocked(handle)) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 679 | ALOGE("Dynamic sensor release error."); |
| 680 | } |
| 681 | |
| 682 | size_t numConnections = activeConnections.size(); |
| 683 | for (size_t i=0 ; i < numConnections; ++i) { |
| 684 | if (activeConnections[i] != NULL) { |
| 685 | activeConnections[i]->removeSensor(handle); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 692 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 693 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 694 | // lock if none of the clients need it. |
| 695 | bool needsWakeLock = false; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 696 | size_t numConnections = activeConnections.size(); |
| 697 | for (size_t i=0 ; i < numConnections; ++i) { |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 698 | if (activeConnections[i] != 0) { |
| 699 | activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch, |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 700 | mMapFlushEventsToConnections); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 701 | needsWakeLock |= activeConnections[i]->needsWakeLock(); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 702 | // If the connection has one-shot sensors, it may be cleaned up after first trigger. |
| 703 | // Early check for one-shot sensors. |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 704 | if (activeConnections[i]->hasOneShotSensors()) { |
| 705 | cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer, |
| 706 | count); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 707 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 708 | } |
| 709 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 710 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 711 | if (mWakeLockAcquired && !needsWakeLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 712 | setWakeLockAcquiredLocked(false); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 713 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 714 | } while (!Thread::exitPending()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 715 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 716 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 717 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 718 | return false; |
| 719 | } |
| 720 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 721 | sp<Looper> SensorService::getLooper() const { |
| 722 | return mLooper; |
| 723 | } |
| 724 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 725 | void SensorService::resetAllWakeLockRefCounts() { |
| 726 | SortedVector< sp<SensorEventConnection> > activeConnections; |
| 727 | populateActiveConnections(&activeConnections); |
| 728 | { |
| 729 | Mutex::Autolock _l(mLock); |
| 730 | for (size_t i=0 ; i < activeConnections.size(); ++i) { |
| 731 | if (activeConnections[i] != 0) { |
| 732 | activeConnections[i]->resetWakeLockRefCount(); |
| 733 | } |
| 734 | } |
| 735 | setWakeLockAcquiredLocked(false); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | void SensorService::setWakeLockAcquiredLocked(bool acquire) { |
| 740 | if (acquire) { |
| 741 | if (!mWakeLockAcquired) { |
| 742 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 743 | mWakeLockAcquired = true; |
| 744 | } |
| 745 | mLooper->wake(); |
| 746 | } else { |
| 747 | if (mWakeLockAcquired) { |
| 748 | release_wake_lock(WAKE_LOCK_NAME); |
| 749 | mWakeLockAcquired = false; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 754 | bool SensorService::isWakeLockAcquired() { |
| 755 | Mutex::Autolock _l(mLock); |
| 756 | return mWakeLockAcquired; |
| 757 | } |
| 758 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 759 | bool SensorService::SensorEventAckReceiver::threadLoop() { |
| 760 | ALOGD("new thread SensorEventAckReceiver"); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 761 | sp<Looper> looper = mService->getLooper(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 762 | do { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 763 | bool wakeLockAcquired = mService->isWakeLockAcquired(); |
| 764 | int timeout = -1; |
| 765 | if (wakeLockAcquired) timeout = 5000; |
| 766 | int ret = looper->pollOnce(timeout); |
| 767 | if (ret == ALOOPER_POLL_TIMEOUT) { |
| 768 | mService->resetAllWakeLockRefCounts(); |
| 769 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 770 | } while(!Thread::exitPending()); |
| 771 | return false; |
| 772 | } |
| 773 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 774 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 775 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 776 | for (size_t i = 0; i < count; i++) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 777 | if (buffer[i].type == SENSOR_TYPE_META_DATA || |
| 778 | buffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META || |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 779 | buffer[i].type == SENSOR_TYPE_ADDITIONAL_INFO) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 780 | continue; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 781 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 782 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 783 | auto logger = mRecentEvent.find(buffer[i].sensor); |
| 784 | if (logger != mRecentEvent.end()) { |
| 785 | logger->second->addEvent(buffer[i]); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 786 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 787 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 790 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 791 | struct compar { |
| 792 | static int cmp(void const* lhs, void const* rhs) { |
| 793 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 794 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 795 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 796 | } |
| 797 | }; |
| 798 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 799 | } |
| 800 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 801 | String8 SensorService::getSensorName(int handle) const { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 802 | return mSensors.getName(handle); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 805 | bool SensorService::isVirtualSensor(int handle) const { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 806 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 807 | return sensor != nullptr && sensor->isVirtual(); |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 810 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame] | 811 | int handle = event.sensor; |
| 812 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 813 | handle = event.meta_data.sensor; |
| 814 | } |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 815 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 816 | return sensor != nullptr && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 819 | int32_t SensorService::getIdFromUuid(const Sensor::uuid_t &uuid) const { |
| 820 | if ((uuid.i64[0] == 0) && (uuid.i64[1] == 0)) { |
| 821 | // UUID is not supported for this device. |
| 822 | return 0; |
| 823 | } |
| 824 | if ((uuid.i64[0] == INT64_C(~0)) && (uuid.i64[1] == INT64_C(~0))) { |
| 825 | // This sensor can be uniquely identified in the system by |
| 826 | // the combination of its type and name. |
| 827 | return -1; |
| 828 | } |
| 829 | |
| 830 | // We have a dynamic sensor. |
| 831 | |
| 832 | if (!sHmacGlobalKeyIsValid) { |
| 833 | // Rather than risk exposing UUIDs, we cripple dynamic sensors. |
| 834 | ALOGW("HMAC key failure; dynamic sensor getId() will be wrong."); |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | // We want each app author/publisher to get a different ID, so that the |
| 839 | // same dynamic sensor cannot be tracked across apps by multiple |
| 840 | // authors/publishers. So we use both our UUID and our User ID. |
| 841 | // Note potential confusion: |
| 842 | // UUID => Universally Unique Identifier. |
| 843 | // UID => User Identifier. |
| 844 | // We refrain from using "uid" except as needed by API to try to |
| 845 | // keep this distinction clear. |
| 846 | |
| 847 | auto appUserId = IPCThreadState::self()->getCallingUid(); |
| 848 | uint8_t uuidAndApp[sizeof(uuid) + sizeof(appUserId)]; |
| 849 | memcpy(uuidAndApp, &uuid, sizeof(uuid)); |
| 850 | memcpy(uuidAndApp + sizeof(uuid), &appUserId, sizeof(appUserId)); |
| 851 | |
| 852 | // Now we use our key on our UUID/app combo to get the hash. |
| 853 | uint8_t hash[EVP_MAX_MD_SIZE]; |
| 854 | unsigned int hashLen; |
| 855 | if (HMAC(EVP_sha256(), |
| 856 | sHmacGlobalKey, sizeof(sHmacGlobalKey), |
| 857 | uuidAndApp, sizeof(uuidAndApp), |
| 858 | hash, &hashLen) == nullptr) { |
| 859 | // Rather than risk exposing UUIDs, we cripple dynamic sensors. |
| 860 | ALOGW("HMAC failure; dynamic sensor getId() will be wrong."); |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | int32_t id = 0; |
| 865 | if (hashLen < sizeof(id)) { |
| 866 | // We never expect this case, but out of paranoia, we handle it. |
| 867 | // Our 'id' length is already quite small, we don't want the |
| 868 | // effective length of it to be even smaller. |
| 869 | // Rather than risk exposing UUIDs, we cripple dynamic sensors. |
| 870 | ALOGW("HMAC insufficient; dynamic sensor getId() will be wrong."); |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | // This is almost certainly less than all of 'hash', but it's as secure |
| 875 | // as we can be with our current 'id' length. |
| 876 | memcpy(&id, hash, sizeof(id)); |
| 877 | |
| 878 | // Note at the beginning of the function that we return the values of |
| 879 | // 0 and -1 to represent special cases. As a result, we can't return |
| 880 | // those as dynamic sensor IDs. If we happened to hash to one of those |
| 881 | // values, we change 'id' so we report as a dynamic sensor, and not as |
| 882 | // one of those special cases. |
| 883 | if (id == -1) { |
| 884 | id = -2; |
| 885 | } else if (id == 0) { |
| 886 | id = 1; |
| 887 | } |
| 888 | return id; |
| 889 | } |
| 890 | |
| 891 | void SensorService::makeUuidsIntoIdsForSensorList(Vector<Sensor> &sensorList) const { |
| 892 | for (auto &sensor : sensorList) { |
| 893 | int32_t id = getIdFromUuid(sensor.getUuid()); |
| 894 | sensor.setId(id); |
| 895 | } |
| 896 | } |
| 897 | |
Nick Vaccaro | 2c588c5 | 2016-11-23 08:44:15 -0800 | [diff] [blame] | 898 | Vector<Sensor> SensorService::getSensorList(const String16& /* opPackageName */) { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 899 | char value[PROPERTY_VALUE_MAX]; |
| 900 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 901 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 902 | mSensors.getUserDebugSensors() : mSensors.getUserSensors(); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 903 | Vector<Sensor> accessibleSensorList; |
| 904 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 905 | Sensor sensor = initialSensorList[i]; |
Nick Vaccaro | 2c588c5 | 2016-11-23 08:44:15 -0800 | [diff] [blame] | 906 | accessibleSensorList.add(sensor); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 907 | } |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 908 | makeUuidsIntoIdsForSensorList(accessibleSensorList); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 909 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 912 | Vector<Sensor> SensorService::getDynamicSensorList(const String16& opPackageName) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 913 | Vector<Sensor> accessibleSensorList; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 914 | mSensors.forEachSensor( |
| 915 | [&opPackageName, &accessibleSensorList] (const Sensor& sensor) -> bool { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 916 | if (sensor.isDynamicSensor()) { |
| 917 | if (canAccessSensor(sensor, "getDynamicSensorList", opPackageName)) { |
| 918 | accessibleSensorList.add(sensor); |
| 919 | } else { |
| 920 | ALOGI("Skipped sensor %s because it requires permission %s and app op %" PRId32, |
| 921 | sensor.getName().string(), |
| 922 | sensor.getRequiredPermission().string(), |
| 923 | sensor.getRequiredAppOp()); |
| 924 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 925 | } |
| 926 | return true; |
| 927 | }); |
Greg Kaiser | 53ca2e0 | 2016-06-21 16:11:14 -0700 | [diff] [blame] | 928 | makeUuidsIntoIdsForSensorList(accessibleSensorList); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 929 | return accessibleSensorList; |
| 930 | } |
| 931 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 932 | sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 933 | int requestedMode, const String16& opPackageName) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 934 | // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION. |
| 935 | if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { |
| 936 | return NULL; |
| 937 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 938 | |
| 939 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 940 | // To create a client in DATA_INJECTION mode to inject data, SensorService should already be |
| 941 | // operating in DI mode. |
| 942 | if (requestedMode == DATA_INJECTION) { |
| 943 | if (mCurrentOperatingMode != DATA_INJECTION) return NULL; |
| 944 | if (!isWhiteListedPackage(packageName)) return NULL; |
| 945 | } |
| 946 | |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 947 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 948 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 949 | requestedMode == DATA_INJECTION, opPackageName)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 950 | if (requestedMode == DATA_INJECTION) { |
| 951 | if (mActiveConnections.indexOf(result) < 0) { |
| 952 | mActiveConnections.add(result); |
| 953 | } |
| 954 | // Add the associated file descriptor to the Looper for polling whenever there is data to |
| 955 | // be injected. |
| 956 | result->updateLooperRegistration(mLooper); |
| 957 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 958 | return result; |
| 959 | } |
| 960 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 961 | int SensorService::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 962 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 963 | return (mCurrentOperatingMode == DATA_INJECTION); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame^] | 966 | sp<ISensorEventConnection> SensorService::createSensorDirectConnection( |
| 967 | const String16& opPackageName, uint32_t size, int32_t type, int32_t format, |
| 968 | const native_handle *resource) { |
| 969 | Mutex::Autolock _l(mLock); |
| 970 | |
| 971 | struct sensors_direct_mem_t mem = { |
| 972 | .type = type, |
| 973 | .format = format, |
| 974 | .size = size, |
| 975 | .handle = resource, |
| 976 | }; |
| 977 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 978 | |
| 979 | if (mem.handle == nullptr) { |
| 980 | ALOGE("Failed to clone resource handle"); |
| 981 | return nullptr; |
| 982 | } |
| 983 | |
| 984 | // check format |
| 985 | if (format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { |
| 986 | ALOGE("Direct channel format %d is unsupported!", format); |
| 987 | return nullptr; |
| 988 | } |
| 989 | |
| 990 | // check for duplication |
| 991 | for (auto &i : mDirectConnections) { |
| 992 | sp<SensorDirectConnection> connection(i.promote()); |
| 993 | if (connection != nullptr && connection->isEquivalent(&mem)) { |
| 994 | return nullptr; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | // check specific to memory type |
| 999 | switch(type) { |
| 1000 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: { // channel backed by ashmem |
| 1001 | int fd = resource->data[0]; |
| 1002 | int size2 = ashmem_get_size_region(fd); |
| 1003 | // check size consistency |
| 1004 | if (size2 != static_cast<int>(size)) { |
| 1005 | ALOGE("Ashmem direct channel size mismatch, %" PRIu32 " vs %d", size, size2); |
| 1006 | return nullptr; |
| 1007 | } |
| 1008 | break; |
| 1009 | } |
| 1010 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: |
| 1011 | LOG_FATAL("%s: Finish implementation of ION and GRALLOC or remove", __FUNCTION__); |
| 1012 | break; |
| 1013 | default: |
| 1014 | ALOGE("Unknown direct connection memory type %d", type); |
| 1015 | return nullptr; |
| 1016 | } |
| 1017 | |
| 1018 | native_handle_t *clone = native_handle_clone(resource); |
| 1019 | if (!clone) { |
| 1020 | return nullptr; |
| 1021 | } |
| 1022 | |
| 1023 | SensorDirectConnection* conn = nullptr; |
| 1024 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1025 | int channelHandle = dev.registerDirectChannel(&mem); |
| 1026 | |
| 1027 | if (channelHandle <= 0) { |
| 1028 | ALOGE("SensorDevice::registerDirectChannel returns %d", channelHandle); |
| 1029 | } else { |
| 1030 | mem.handle = clone; |
| 1031 | conn = new SensorDirectConnection(this, uid, &mem, channelHandle, opPackageName); |
| 1032 | } |
| 1033 | |
| 1034 | if (conn == nullptr) { |
| 1035 | native_handle_close(clone); |
| 1036 | native_handle_delete(clone); |
| 1037 | } else { |
| 1038 | // add to list of direct connections |
| 1039 | // sensor service should never hold pointer or sp of SensorDirectConnection object. |
| 1040 | mDirectConnections.add(wp<SensorDirectConnection>(conn)); |
| 1041 | } |
| 1042 | return conn; |
| 1043 | } |
| 1044 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1045 | status_t SensorService::resetToNormalMode() { |
| 1046 | Mutex::Autolock _l(mLock); |
| 1047 | return resetToNormalModeLocked(); |
| 1048 | } |
| 1049 | |
| 1050 | status_t SensorService::resetToNormalModeLocked() { |
| 1051 | SensorDevice& dev(SensorDevice::getInstance()); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1052 | status_t err = dev.setMode(NORMAL); |
Aniroop Mathur | bfac17e | 2016-08-31 22:59:44 +0530 | [diff] [blame] | 1053 | if (err == NO_ERROR) { |
| 1054 | mCurrentOperatingMode = NORMAL; |
| 1055 | dev.enableAllSensors(); |
| 1056 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1057 | return err; |
| 1058 | } |
| 1059 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1060 | void SensorService::cleanupConnection(SensorEventConnection* c) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1061 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1062 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1063 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1064 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1065 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1066 | int handle = mActiveSensors.keyAt(i); |
| 1067 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1068 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1069 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1070 | if (sensor != nullptr) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1071 | sensor->activate(c, false); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1072 | } else { |
| 1073 | ALOGE("sensor interface of handle=0x%08x is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1074 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1075 | c->removeSensor(handle); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1076 | } |
| 1077 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1078 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1079 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 1080 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 1081 | c, i, handle); |
| 1082 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 1083 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1084 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1085 | mActiveSensors.removeItemsAt(i, 1); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1086 | mActiveVirtualSensors.erase(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1087 | delete rec; |
| 1088 | size--; |
| 1089 | } else { |
| 1090 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1091 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1092 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1093 | c->updateLooperRegistration(mLooper); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1094 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 1095 | BatteryService::cleanup(c->getUid()); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1096 | if (c->needsWakeLock()) { |
| 1097 | checkWakeLockStateLocked(); |
| 1098 | } |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 1099 | |
| 1100 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1101 | dev.notifyConnectionDestroyed(c); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame^] | 1104 | void SensorService::cleanupConnection(SensorDirectConnection* c) { |
| 1105 | Mutex::Autolock _l(mLock); |
| 1106 | |
| 1107 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1108 | dev.unregisterDirectChannel(c->getHalChannelHandle()); |
| 1109 | mDirectConnections.remove(c); |
| 1110 | } |
| 1111 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1112 | sp<SensorInterface> SensorService::getSensorInterfaceFromHandle(int handle) const { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 1113 | return mSensors.getInterface(handle); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1116 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1117 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1118 | const String16& opPackageName) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1119 | if (mInitCheck != NO_ERROR) |
| 1120 | return mInitCheck; |
| 1121 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1122 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1123 | if (sensor == nullptr || |
| 1124 | !canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1125 | return BAD_VALUE; |
| 1126 | } |
| 1127 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1128 | Mutex::Autolock _l(mLock); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame^] | 1129 | if (mCurrentOperatingMode != NORMAL |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1130 | && !isWhiteListedPackage(connection->getPackageName())) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1131 | return INVALID_OPERATION; |
| 1132 | } |
| 1133 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1134 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 1135 | if (rec == 0) { |
| 1136 | rec = new SensorRecord(connection); |
| 1137 | mActiveSensors.add(handle, rec); |
| 1138 | if (sensor->isVirtual()) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1139 | mActiveVirtualSensors.emplace(handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } else { |
| 1142 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1143 | // this sensor is already activated, but we are adding a connection that uses it. |
| 1144 | // 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] | 1145 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 1146 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1147 | // NOTE: The wake_up flag of this event may get set to |
| 1148 | // 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] | 1149 | |
| 1150 | auto logger = mRecentEvent.find(handle); |
| 1151 | if (logger != mRecentEvent.end()) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1152 | sensors_event_t event; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1153 | // It is unlikely that this buffer is empty as the sensor is already active. |
| 1154 | // One possible corner case may be two applications activating an on-change |
| 1155 | // sensor at the same time. |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 1156 | if(logger->second->populateLastEvent(&event)) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1157 | event.sensor = handle; |
| 1158 | if (event.version == sizeof(sensors_event_t)) { |
| 1159 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 1160 | setWakeLockAcquiredLocked(true); |
| 1161 | } |
| 1162 | connection->sendEvents(&event, 1, NULL); |
| 1163 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
| 1164 | checkWakeLockStateLocked(); |
| 1165 | } |
| 1166 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1167 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | if (connection->addSensor(handle)) { |
| 1174 | BatteryService::enableSensor(connection->getUid(), handle); |
| 1175 | // the sensor was added (which means it wasn't already there) |
| 1176 | // so, see if this connection becomes active |
| 1177 | if (mActiveConnections.indexOf(connection) < 0) { |
| 1178 | mActiveConnections.add(connection); |
| 1179 | } |
| 1180 | } else { |
| 1181 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 1182 | handle, connection.get()); |
| 1183 | } |
| 1184 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1185 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 1186 | if (samplingPeriodNs < minDelayNs) { |
| 1187 | samplingPeriodNs = minDelayNs; |
| 1188 | } |
| 1189 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1190 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" |
| 1191 | "rate=%" PRId64 " timeout== %" PRId64"", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1192 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 1193 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1194 | status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1195 | maxBatchReportLatencyNs); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1196 | |
Peng Xu | 20483c4 | 2015-10-26 15:14:43 -0700 | [diff] [blame] | 1197 | // Call flush() before calling activate() on the sensor. Wait for a first |
| 1198 | // flush complete event before sending events on this connection. Ignore |
| 1199 | // one-shot sensors which don't support flush(). Ignore on-change sensors |
| 1200 | // to maintain the on-change logic (any on-change events except the initial |
| 1201 | // one should be trigger by a change in value). Also if this sensor isn't |
| 1202 | // already active, don't call flush(). |
| 1203 | if (err == NO_ERROR && |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 1204 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_CONTINUOUS && |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1205 | rec->getNumConnections() > 1) { |
| 1206 | connection->setFirstFlushPending(handle, true); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1207 | status_t err_flush = sensor->flush(connection.get(), handle); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1208 | // 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] | 1209 | if (err_flush == NO_ERROR) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1210 | rec->addPendingFlushConnection(connection.get()); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1211 | } else { |
| 1212 | connection->setFirstFlushPending(handle, false); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1215 | |
| 1216 | if (err == NO_ERROR) { |
| 1217 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); |
| 1218 | err = sensor->activate(connection.get(), true); |
| 1219 | } |
| 1220 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1221 | if (err == NO_ERROR) { |
| 1222 | connection->updateLooperRegistration(mLooper); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1223 | SensorRegistrationInfo ®_info = |
| 1224 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); |
| 1225 | reg_info.mSensorHandle = handle; |
| 1226 | reg_info.mSamplingRateUs = samplingPeriodNs/1000; |
| 1227 | reg_info.mMaxReportLatencyUs = maxBatchReportLatencyNs/1000; |
| 1228 | reg_info.mActivated = true; |
| 1229 | reg_info.mPackageName = connection->getPackageName(); |
| 1230 | time_t rawtime = time(NULL); |
| 1231 | struct tm * timeinfo = localtime(&rawtime); |
| 1232 | reg_info.mHour = timeinfo->tm_hour; |
| 1233 | reg_info.mMin = timeinfo->tm_min; |
| 1234 | reg_info.mSec = timeinfo->tm_sec; |
| 1235 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1238 | if (err != NO_ERROR) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1239 | // batch/activate has failed, reset our state. |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1240 | cleanupWithoutDisableLocked(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1241 | } |
| 1242 | return err; |
| 1243 | } |
| 1244 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1245 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1246 | if (mInitCheck != NO_ERROR) |
| 1247 | return mInitCheck; |
| 1248 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1249 | Mutex::Autolock _l(mLock); |
| 1250 | status_t err = cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1251 | if (err == NO_ERROR) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1252 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1253 | err = sensor != nullptr ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1254 | |
| 1255 | } |
| 1256 | if (err == NO_ERROR) { |
| 1257 | SensorRegistrationInfo ®_info = |
| 1258 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); |
| 1259 | reg_info.mActivated = false; |
| 1260 | reg_info.mPackageName= connection->getPackageName(); |
| 1261 | reg_info.mSensorHandle = handle; |
| 1262 | time_t rawtime = time(NULL); |
| 1263 | struct tm * timeinfo = localtime(&rawtime); |
| 1264 | reg_info.mHour = timeinfo->tm_hour; |
| 1265 | reg_info.mMin = timeinfo->tm_min; |
| 1266 | reg_info.mSec = timeinfo->tm_sec; |
| 1267 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1268 | } |
| 1269 | return err; |
| 1270 | } |
| 1271 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1272 | status_t SensorService::cleanupWithoutDisable( |
| 1273 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1274 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1275 | return cleanupWithoutDisableLocked(connection, handle); |
| 1276 | } |
| 1277 | |
| 1278 | status_t SensorService::cleanupWithoutDisableLocked( |
| 1279 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1280 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1281 | if (rec) { |
| 1282 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 1283 | if (connection->removeSensor(handle)) { |
| 1284 | BatteryService::disableSensor(connection->getUid(), handle); |
| 1285 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1286 | if (connection->hasAnySensor() == false) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1287 | connection->updateLooperRegistration(mLooper); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1288 | mActiveConnections.remove(connection); |
| 1289 | } |
| 1290 | // see if this sensor becomes inactive |
| 1291 | if (rec->removeConnection(connection)) { |
| 1292 | mActiveSensors.removeItem(handle); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1293 | mActiveVirtualSensors.erase(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1294 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1295 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1296 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1297 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1298 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1301 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1302 | int handle, nsecs_t ns, const String16& opPackageName) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1303 | if (mInitCheck != NO_ERROR) |
| 1304 | return mInitCheck; |
| 1305 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1306 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1307 | if (sensor == nullptr || |
| 1308 | !canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1309 | return BAD_VALUE; |
| 1310 | } |
| 1311 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1312 | if (ns < 0) |
| 1313 | return BAD_VALUE; |
| 1314 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 1315 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 1316 | if (ns < minDelayNs) { |
| 1317 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1320 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1323 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, |
| 1324 | const String16& opPackageName) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1325 | if (mInitCheck != NO_ERROR) return mInitCheck; |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1326 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1327 | const int halVersion = dev.getHalDeviceVersion(); |
| 1328 | status_t err(NO_ERROR); |
| 1329 | Mutex::Autolock _l(mLock); |
| 1330 | // Loop through all sensors for this connection and call flush on each of them. |
| 1331 | for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) { |
| 1332 | const int handle = connection->mSensorInfo.keyAt(i); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1333 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1334 | if (sensor == nullptr) { |
| 1335 | continue; |
| 1336 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1337 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 1338 | ALOGE("flush called on a one-shot sensor"); |
| 1339 | err = INVALID_OPERATION; |
| 1340 | continue; |
| 1341 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1342 | if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) { |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1343 | // For older devices just increment pending flush count which will send a trivial |
| 1344 | // flush complete event. |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1345 | connection->incrementPendingFlushCount(handle); |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1346 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1347 | if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) { |
| 1348 | err = INVALID_OPERATION; |
| 1349 | continue; |
| 1350 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1351 | status_t err_flush = sensor->flush(connection.get(), handle); |
| 1352 | if (err_flush == NO_ERROR) { |
| 1353 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 1354 | if (rec != NULL) rec->addPendingFlushConnection(connection); |
| 1355 | } |
| 1356 | err = (err_flush != NO_ERROR) ? err_flush : err; |
| 1357 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1358 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1359 | return err; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1360 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1361 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1362 | bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, |
| 1363 | const String16& opPackageName) { |
| 1364 | const String8& requiredPermission = sensor.getRequiredPermission(); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1365 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1366 | if (requiredPermission.length() <= 0) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1367 | return true; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | bool hasPermission = false; |
| 1371 | |
| 1372 | // Runtime permissions can't use the cache as they may change. |
| 1373 | if (sensor.isRequiredPermissionRuntime()) { |
| 1374 | hasPermission = checkPermission(String16(requiredPermission), |
| 1375 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1376 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1377 | hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission)); |
| 1378 | } |
| 1379 | |
| 1380 | if (!hasPermission) { |
| 1381 | ALOGE("%s a sensor (%s) without holding its required permission: %s", |
| 1382 | operation, sensor.getName().string(), sensor.getRequiredPermission().string()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1385 | |
| 1386 | const int32_t opCode = sensor.getRequiredAppOp(); |
| 1387 | if (opCode >= 0) { |
| 1388 | AppOpsManager appOps; |
| 1389 | if (appOps.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName) |
| 1390 | != AppOpsManager::MODE_ALLOWED) { |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 1391 | ALOGE("%s a sensor (%s) without enabled required app op: %d", |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1392 | operation, sensor.getName().string(), opCode); |
| 1393 | return false; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | return true; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1400 | void SensorService::checkWakeLockState() { |
| 1401 | Mutex::Autolock _l(mLock); |
| 1402 | checkWakeLockStateLocked(); |
| 1403 | } |
| 1404 | |
| 1405 | void SensorService::checkWakeLockStateLocked() { |
| 1406 | if (!mWakeLockAcquired) { |
| 1407 | return; |
| 1408 | } |
| 1409 | bool releaseLock = true; |
| 1410 | for (size_t i=0 ; i<mActiveConnections.size() ; i++) { |
| 1411 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 1412 | if (connection != 0) { |
| 1413 | if (connection->needsWakeLock()) { |
| 1414 | releaseLock = false; |
| 1415 | break; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | if (releaseLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1420 | setWakeLockAcquiredLocked(false); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) { |
| 1425 | Mutex::Autolock _l(mLock); |
| 1426 | connection->writeToSocketFromCache(); |
| 1427 | if (connection->needsWakeLock()) { |
| 1428 | setWakeLockAcquiredLocked(true); |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | void SensorService::populateActiveConnections( |
| 1433 | SortedVector< sp<SensorEventConnection> >* activeConnections) { |
| 1434 | Mutex::Autolock _l(mLock); |
| 1435 | for (size_t i=0 ; i < mActiveConnections.size(); ++i) { |
| 1436 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 1437 | if (connection != 0) { |
| 1438 | activeConnections->add(connection); |
| 1439 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1440 | } |
| 1441 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1442 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1443 | bool SensorService::isWhiteListedPackage(const String8& packageName) { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1444 | return (packageName.contains(mWhiteListedPackage.string())); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame^] | 1447 | bool SensorService::isOperationRestricted(const String16& opPackageName) { |
| 1448 | Mutex::Autolock _l(mLock); |
| 1449 | if (mCurrentOperatingMode != RESTRICTED) { |
| 1450 | String8 package(opPackageName); |
| 1451 | return !isWhiteListedPackage(package); |
| 1452 | } |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1456 | }; // namespace android |
| 1457 | |