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