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