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