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