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