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