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