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 | */ |
| 16 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 17 | #include <cutils/properties.h> |
| 18 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 19 | #include <binder/AppOpsManager.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 20 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 22 | #include <binder/PermissionCache.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 23 | |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 24 | #include <gui/SensorEventQueue.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 25 | |
| 26 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 27 | #include <hardware_legacy/power.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 28 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 29 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 30 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 31 | #include "GravitySensor.h" |
| 32 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 33 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 34 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 35 | #include "SensorFusion.h" |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 36 | #include "SensorInterface.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 37 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 38 | #include "SensorService.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 39 | #include "SensorEventAckReceiver.h" |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 40 | #include "SensorEventConnection.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 41 | #include "SensorRecord.h" |
| 42 | #include "SensorRegistrationInfo.h" |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 43 | |
| 44 | #include <inttypes.h> |
| 45 | #include <math.h> |
| 46 | #include <stdint.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <sys/socket.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 49 | |
| 50 | namespace android { |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Notes: |
| 55 | * |
| 56 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 57 | * - run mag sensor from time to time to force calibration |
| 58 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 59 | * |
| 60 | */ |
| 61 | |
Aravind Akella | 8ef3c89 | 2015-07-10 11:24:28 -0700 | [diff] [blame] | 62 | const char* SensorService::WAKE_LOCK_NAME = "SensorService_wakelock"; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 63 | // Permissions. |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 64 | static const String16 sDump("android.permission.DUMP"); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 65 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 66 | SensorService::SensorService() |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 67 | : mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED), |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 68 | mWakeLockAcquired(false) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 71 | void SensorService::onFirstRef() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 72 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 73 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 74 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 75 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 76 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 77 | ssize_t count = dev.getSensorList(&list); |
| 78 | if (count > 0) { |
| 79 | ssize_t orientationIndex = -1; |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 80 | bool hasGyro = false, hasAccel = false, hasMag = false; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 81 | uint32_t virtualSensorsNeeds = |
| 82 | (1<<SENSOR_TYPE_GRAVITY) | |
| 83 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 84 | (1<<SENSOR_TYPE_ROTATION_VECTOR) | |
| 85 | (1<<SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR) | |
| 86 | (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 87 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 88 | for (ssize_t i=0 ; i<count ; i++) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 89 | bool useThisSensor=true; |
| 90 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 91 | switch (list[i].type) { |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 92 | case SENSOR_TYPE_ACCELEROMETER: |
| 93 | hasAccel = true; |
| 94 | break; |
| 95 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 96 | hasMag = true; |
| 97 | break; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 98 | case SENSOR_TYPE_ORIENTATION: |
| 99 | orientationIndex = i; |
| 100 | break; |
| 101 | case SENSOR_TYPE_GYROSCOPE: |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 102 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 103 | hasGyro = true; |
| 104 | break; |
| 105 | case SENSOR_TYPE_GRAVITY: |
| 106 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 107 | case SENSOR_TYPE_ROTATION_VECTOR: |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 108 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 109 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 110 | if (IGNORE_HARDWARE_FUSION) { |
| 111 | useThisSensor = false; |
| 112 | } else { |
| 113 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 114 | } |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 115 | break; |
| 116 | } |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 117 | if (useThisSensor) { |
| 118 | registerSensor( new HardwareSensor(list[i]) ); |
| 119 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 120 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 121 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 122 | // it's safe to instantiate the SensorFusion object here |
| 123 | // (it wants to be instantiated after h/w sensors have been |
| 124 | // registered) |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 125 | SensorFusion::getInstance(); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 126 | |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 127 | if (hasGyro && hasAccel && hasMag) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 128 | // Add Android virtual sensors if they're not already |
| 129 | // available in the HAL |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 130 | bool needRotationVector = |
| 131 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) != 0; |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 132 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 133 | registerSensor(new RotationVectorSensor(), !needRotationVector, true); |
| 134 | registerSensor(new OrientationSensor(), !needRotationVector, true); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 135 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 136 | bool needLinearAcceleration = |
| 137 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) != 0; |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 138 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 139 | registerSensor(new LinearAccelerationSensor(list, count), |
| 140 | !needLinearAcceleration, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 141 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 142 | // virtual debugging sensors are not for user |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 143 | registerSensor( new CorrectedGyroSensor(list, count), true, true); |
| 144 | registerSensor( new GyroDriftSensor(), true, true); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 147 | if (hasAccel && hasGyro) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 148 | bool needGravitySensor = (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) != 0; |
| 149 | registerSensor(new GravitySensor(list, count), !needGravitySensor, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 150 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 151 | bool needGameRotationVector = |
| 152 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR)) != 0; |
| 153 | registerSensor(new GameRotationVectorSensor(), !needGameRotationVector, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (hasAccel && hasMag) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 157 | bool needGeoMagRotationVector = |
| 158 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR)) != 0; |
| 159 | registerSensor(new GeoMagRotationVectorSensor(), !needGeoMagRotationVector, true); |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 162 | // Check if the device really supports batching by looking at the FIFO event |
| 163 | // counts for each sensor. |
| 164 | bool batchingSupported = false; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 165 | mSensors.forEachSensor( |
| 166 | [&batchingSupported] (const Sensor& s) -> bool { |
| 167 | if (s.getFifoMaxEventCount() > 0) { |
| 168 | batchingSupported = true; |
| 169 | } |
| 170 | return !batchingSupported; |
| 171 | }); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 172 | |
| 173 | if (batchingSupported) { |
| 174 | // Increase socket buffer size to a max of 100 KB for batching capabilities. |
| 175 | mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; |
| 176 | } else { |
| 177 | mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; |
| 178 | } |
| 179 | |
| 180 | // Compare the socketBufferSize value against the system limits and limit |
| 181 | // it to maxSystemSocketBufferSize if necessary. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 182 | FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r"); |
| 183 | char line[128]; |
| 184 | if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) { |
| 185 | line[sizeof(line) - 1] = '\0'; |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 186 | size_t maxSystemSocketBufferSize; |
| 187 | sscanf(line, "%zu", &maxSystemSocketBufferSize); |
| 188 | if (mSocketBufferSize > maxSystemSocketBufferSize) { |
| 189 | mSocketBufferSize = maxSystemSocketBufferSize; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 192 | if (fp) { |
| 193 | fclose(fp); |
| 194 | } |
| 195 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 196 | mWakeLockAcquired = false; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 197 | mLooper = new Looper(false); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 198 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 199 | mSensorEventBuffer = new sensors_event_t[minBufferSize]; |
| 200 | mSensorEventScratch = new sensors_event_t[minBufferSize]; |
| 201 | mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize]; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 202 | mCurrentOperatingMode = NORMAL; |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 203 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 204 | mNextSensorRegIndex = 0; |
| 205 | for (int i = 0; i < SENSOR_REGISTRATIONS_BUF_SIZE; ++i) { |
| 206 | mLastNSensorRegistrations.push(); |
| 207 | } |
| 208 | |
| 209 | mInitCheck = NO_ERROR; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 210 | mAckReceiver = new SensorEventAckReceiver(this); |
| 211 | mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY); |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 212 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 213 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 214 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 217 | const Sensor& SensorService::registerSensor(SensorInterface* s, bool isDebug, bool isVirtual) { |
| 218 | int handle = s->getSensor().getHandle(); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 219 | int type = s->getSensor().getType(); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 220 | if (mSensors.add(handle, s, isDebug, isVirtual)){ |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 221 | mRecentEvent.emplace(handle, new RecentEventLogger(type)); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 222 | return s->getSensor(); |
| 223 | } else { |
| 224 | return mSensors.getNonSensor(); |
| 225 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 228 | const Sensor& SensorService::registerDynamicSensorLocked(SensorInterface* s, bool isDebug) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 229 | return registerSensor(s, isDebug); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 232 | bool SensorService::unregisterDynamicSensorLocked(int handle) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 233 | bool ret = mSensors.remove(handle); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 234 | |
| 235 | const auto i = mRecentEvent.find(handle); |
| 236 | if (i != mRecentEvent.end()) { |
| 237 | delete i->second; |
| 238 | mRecentEvent.erase(i); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 239 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 240 | return ret; |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 243 | const Sensor& SensorService::registerVirtualSensor(SensorInterface* s, bool isDebug) { |
| 244 | return registerSensor(s, isDebug, true); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 247 | SensorService::~SensorService() { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 248 | for (auto && entry : mRecentEvent) { |
| 249 | delete entry.second; |
| 250 | } |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | status_t SensorService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 254 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 255 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 256 | 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] | 257 | IPCThreadState::self()->getCallingPid(), |
| 258 | IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 259 | } else { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 260 | if (args.size() > 2) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 261 | return INVALID_OPERATION; |
| 262 | } |
| 263 | Mutex::Autolock _l(mLock); |
| 264 | SensorDevice& dev(SensorDevice::getInstance()); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 265 | if (args.size() == 2 && args[0] == String16("restrict")) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 266 | // If already in restricted mode. Ignore. |
| 267 | if (mCurrentOperatingMode == RESTRICTED) { |
| 268 | return status_t(NO_ERROR); |
| 269 | } |
| 270 | // If in any mode other than normal, ignore. |
| 271 | if (mCurrentOperatingMode != NORMAL) { |
| 272 | return INVALID_OPERATION; |
| 273 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 274 | mCurrentOperatingMode = RESTRICTED; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 275 | dev.disableAllSensors(); |
| 276 | // Clear all pending flush connections for all active sensors. If one of the active |
| 277 | // connections has called flush() and the underlying sensor has been disabled before a |
| 278 | // flush complete event is returned, we need to remove the connection from this queue. |
| 279 | for (size_t i=0 ; i< mActiveSensors.size(); ++i) { |
| 280 | mActiveSensors.valueAt(i)->clearAllPendingFlushConnections(); |
| 281 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 282 | mWhiteListedPackage.setTo(String8(args[1])); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 283 | return status_t(NO_ERROR); |
| 284 | } else if (args.size() == 1 && args[0] == String16("enable")) { |
| 285 | // If currently in restricted mode, reset back to NORMAL mode else ignore. |
| 286 | if (mCurrentOperatingMode == RESTRICTED) { |
| 287 | mCurrentOperatingMode = NORMAL; |
| 288 | dev.enableAllSensors(); |
| 289 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 290 | if (mCurrentOperatingMode == DATA_INJECTION) { |
| 291 | resetToNormalModeLocked(); |
| 292 | } |
| 293 | mWhiteListedPackage.clear(); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 294 | return status_t(NO_ERROR); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 295 | } else if (args.size() == 2 && args[0] == String16("data_injection")) { |
| 296 | if (mCurrentOperatingMode == NORMAL) { |
| 297 | dev.disableAllSensors(); |
| 298 | status_t err = dev.setMode(DATA_INJECTION); |
| 299 | if (err == NO_ERROR) { |
| 300 | mCurrentOperatingMode = DATA_INJECTION; |
| 301 | } else { |
| 302 | // Re-enable sensors. |
| 303 | dev.enableAllSensors(); |
| 304 | } |
| 305 | mWhiteListedPackage.setTo(String8(args[1])); |
| 306 | return NO_ERROR; |
| 307 | } else if (mCurrentOperatingMode == DATA_INJECTION) { |
| 308 | // Already in DATA_INJECTION mode. Treat this as a no_op. |
| 309 | return NO_ERROR; |
| 310 | } else { |
| 311 | // Transition to data injection mode supported only from NORMAL mode. |
| 312 | return INVALID_OPERATION; |
| 313 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 314 | } else if (!mSensors.hasAnySensor()) { |
Aravind Akella | ee155ca | 2015-06-24 08:31:32 -0700 | [diff] [blame] | 315 | result.append("No Sensors on the device\n"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 316 | } else { |
| 317 | // Default dump the sensor list and debugging information. |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 318 | // |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 319 | result.append("Sensor Device:\n"); |
| 320 | result.append(SensorDevice::getInstance().dump().c_str()); |
| 321 | |
| 322 | result.append("Sensor List:\n"); |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 323 | result.append(mSensors.dump().c_str()); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 324 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 325 | result.append("Fusion States:\n"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 326 | SensorFusion::getInstance().dump(result); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 327 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 328 | result.append("Recent Sensor events:\n"); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 329 | for (auto&& i : mRecentEvent) { |
| 330 | sp<SensorInterface> s = mSensors.getInterface(i.first); |
| 331 | if (!i.second->isEmpty() && |
| 332 | s->getSensor().getRequiredPermission().isEmpty()) { |
| 333 | // if there is events and sensor does not need special permission. |
| 334 | result.appendFormat("%s: ", s->getSensor().getName().string()); |
| 335 | result.append(i.second->dump().c_str()); |
| 336 | } |
| 337 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 338 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 339 | result.append("Active sensors:\n"); |
| 340 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
| 341 | int handle = mActiveSensors.keyAt(i); |
| 342 | result.appendFormat("%s (handle=0x%08x, connections=%zu)\n", |
| 343 | getSensorName(handle).string(), |
| 344 | handle, |
| 345 | mActiveSensors.valueAt(i)->getNumConnections()); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 348 | result.appendFormat("Socket Buffer size = %zd events\n", |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 349 | mSocketBufferSize/sizeof(sensors_event_t)); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 350 | result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : |
| 351 | "not held"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 352 | result.appendFormat("Mode :"); |
| 353 | switch(mCurrentOperatingMode) { |
| 354 | case NORMAL: |
| 355 | result.appendFormat(" NORMAL\n"); |
| 356 | break; |
| 357 | case RESTRICTED: |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 358 | result.appendFormat(" RESTRICTED : %s\n", mWhiteListedPackage.string()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 359 | break; |
| 360 | case DATA_INJECTION: |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 361 | result.appendFormat(" DATA_INJECTION : %s\n", mWhiteListedPackage.string()); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 362 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 363 | result.appendFormat("%zd active connections\n", mActiveConnections.size()); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 364 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 365 | for (size_t i=0 ; i < mActiveConnections.size() ; i++) { |
| 366 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 367 | if (connection != 0) { |
| 368 | result.appendFormat("Connection Number: %zu \n", i); |
| 369 | connection->dump(result); |
| 370 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 371 | } |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 372 | |
| 373 | result.appendFormat("Previous Registrations:\n"); |
| 374 | // Log in the reverse chronological order. |
| 375 | int currentIndex = (mNextSensorRegIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 376 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 377 | const int startIndex = currentIndex; |
| 378 | do { |
| 379 | const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[currentIndex]; |
| 380 | if (SensorRegistrationInfo::isSentinel(reg_info)) { |
| 381 | // Ignore sentinel, proceed to next item. |
| 382 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 383 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 384 | continue; |
| 385 | } |
| 386 | if (reg_info.mActivated) { |
| 387 | result.appendFormat("%02d:%02d:%02d activated package=%s handle=0x%08x " |
| 388 | "samplingRate=%dus maxReportLatency=%dus\n", |
| 389 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
| 390 | reg_info.mPackageName.string(), reg_info.mSensorHandle, |
| 391 | reg_info.mSamplingRateUs, reg_info.mMaxReportLatencyUs); |
| 392 | } else { |
| 393 | result.appendFormat("%02d:%02d:%02d de-activated package=%s handle=0x%08x\n", |
| 394 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
| 395 | reg_info.mPackageName.string(), reg_info.mSensorHandle); |
| 396 | } |
| 397 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 398 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 399 | } while(startIndex != currentIndex); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 400 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 401 | } |
| 402 | write(fd, result.string(), result.size()); |
| 403 | return NO_ERROR; |
| 404 | } |
| 405 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 406 | //TODO: move to SensorEventConnection later |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 407 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 408 | sensors_event_t const* buffer, const int count) { |
| 409 | for (int i=0 ; i<count ; i++) { |
| 410 | int handle = buffer[i].sensor; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 411 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 412 | handle = buffer[i].meta_data.sensor; |
| 413 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 414 | if (connection->hasSensor(handle)) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 415 | sp<SensorInterface> si = getSensorInterfaceFromHandle(handle); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 416 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 417 | // for this particular one_shot sensor, try cleaning up the connection. |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 418 | if (si != nullptr && |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 419 | si->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 420 | si->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 421 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 422 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 423 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 424 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 425 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 428 | bool SensorService::threadLoop() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 429 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 430 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 431 | // each virtual sensor could generate an event per "real" event, that's why we need to size |
| 432 | // numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. in practice, this is too |
| 433 | // aggressive, but guaranteed to be enough. |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 434 | const size_t vcount = mSensors.getVirtualSensors().size(); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 435 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 436 | const size_t numEventMax = minBufferSize / (1 + vcount); |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 437 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 438 | SensorDevice& device(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 439 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 440 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 441 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 442 | ssize_t count = device.poll(mSensorEventBuffer, numEventMax); |
| 443 | if (count < 0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 444 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 445 | break; |
| 446 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 447 | |
| 448 | // Reset sensors_event_t.flags to zero for all events in the buffer. |
| 449 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 450 | mSensorEventBuffer[i].flags = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 451 | } |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 452 | |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 453 | // Make a copy of the connection vector as some connections may be removed during the course |
| 454 | // of this loop (especially when one-shot sensor events are present in the sensor_event |
| 455 | // buffer). Promote all connections to StrongPointers before the lock is acquired. If the |
| 456 | // destructor of the sp gets called when the lock is acquired, it may result in a deadlock |
| 457 | // as ~SensorEventConnection() needs to acquire mLock again for cleanup. So copy all the |
| 458 | // strongPointers to a vector before the lock is acquired. |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 459 | SortedVector< sp<SensorEventConnection> > activeConnections; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 460 | populateActiveConnections(&activeConnections); |
Peng Xu | eb4d628 | 2015-12-10 18:02:41 -0800 | [diff] [blame] | 461 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 462 | Mutex::Autolock _l(mLock); |
| 463 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 464 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 465 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 466 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 467 | // releasing the wakelock. |
| 468 | bool bufferHasWakeUpEvent = false; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 469 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 470 | if (isWakeUpSensorEvent(mSensorEventBuffer[i])) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 471 | bufferHasWakeUpEvent = true; |
| 472 | break; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 476 | if (bufferHasWakeUpEvent && !mWakeLockAcquired) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 477 | setWakeLockAcquiredLocked(true); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 478 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 479 | recordLastValueLocked(mSensorEventBuffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 480 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 481 | // handle virtual sensors |
| 482 | if (count && vcount) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 483 | sensors_event_t const * const event = mSensorEventBuffer; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 484 | if (!mActiveVirtualSensors.empty()) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 485 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 486 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 487 | if (fusion.isEnabled()) { |
| 488 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 489 | fusion.process(event[i]); |
| 490 | } |
| 491 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 492 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 493 | for (int handle : mActiveVirtualSensors) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 494 | if (count + k >= minBufferSize) { |
| 495 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 496 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 497 | count, k, minBufferSize); |
| 498 | break; |
| 499 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 500 | sensors_event_t out; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 501 | sp<SensorInterface> si = mSensors.getInterface(handle); |
| 502 | if (si == nullptr) { |
| 503 | ALOGE("handle %d is not an valid virtual sensor", handle); |
| 504 | continue; |
| 505 | } |
| 506 | |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 507 | if (si->process(&out, event[i])) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 508 | mSensorEventBuffer[count + k] = out; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 509 | k++; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | if (k) { |
| 514 | // record the last synthesized values |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 515 | recordLastValueLocked(&mSensorEventBuffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 516 | count += k; |
| 517 | // sort the buffer by time-stamps |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 518 | sortEventBuffer(mSensorEventBuffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 523 | // handle backward compatibility for RotationVector sensor |
| 524 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 525 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 526 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 527 | // All the 4 components of the quaternion should be available |
| 528 | // No heading accuracy. Set it to -1 |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 529 | mSensorEventBuffer[i].data[4] = -1; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 534 | for (int i = 0; i < count; ++i) { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 535 | // Map flush_complete_events in the buffer to SensorEventConnections which called flush |
| 536 | // on the hardware sensor. mapFlushEventsToConnections[i] will be the |
| 537 | // SensorEventConnection mapped to the corresponding flush_complete_event in |
| 538 | // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise). |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 539 | mMapFlushEventsToConnections[i] = NULL; |
| 540 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) { |
| 541 | const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor; |
| 542 | SensorRecord* rec = mActiveSensors.valueFor(sensor_handle); |
| 543 | if (rec != NULL) { |
| 544 | mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection(); |
| 545 | rec->removeFirstPendingFlushConnection(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 548 | |
| 549 | // handle dynamic sensor meta events, process registration and unregistration of dynamic |
| 550 | // sensor based on content of event. |
| 551 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META) { |
| 552 | if (mSensorEventBuffer[i].dynamic_sensor_meta.connected) { |
| 553 | int handle = mSensorEventBuffer[i].dynamic_sensor_meta.handle; |
| 554 | const sensor_t& dynamicSensor = |
| 555 | *(mSensorEventBuffer[i].dynamic_sensor_meta.sensor); |
| 556 | ALOGI("Dynamic sensor handle 0x%x connected, type %d, name %s", |
| 557 | handle, dynamicSensor.type, dynamicSensor.name); |
| 558 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 559 | if (mSensors.isNewHandle(handle)) { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 560 | const auto& uuid = mSensorEventBuffer[i].dynamic_sensor_meta.uuid; |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 561 | sensor_t s = dynamicSensor; |
| 562 | // make sure the dynamic sensor flag is set |
| 563 | s.flags |= DYNAMIC_SENSOR_MASK; |
| 564 | // force the handle to be consistent |
| 565 | s.handle = handle; |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 566 | |
| 567 | SensorInterface *si = new HardwareSensor(s, uuid); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 568 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 569 | // This will release hold on dynamic sensor meta, so it should be called |
| 570 | // after Sensor object is created. |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 571 | device.handleDynamicSensorConnection(handle, true /*connected*/); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 572 | registerDynamicSensorLocked(si); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 573 | } else { |
| 574 | ALOGE("Handle %d has been used, cannot use again before reboot.", handle); |
| 575 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 576 | } else { |
| 577 | int handle = mSensorEventBuffer[i].dynamic_sensor_meta.handle; |
| 578 | ALOGI("Dynamic sensor handle 0x%x disconnected", handle); |
| 579 | |
| 580 | device.handleDynamicSensorConnection(handle, false /*connected*/); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 581 | if (!unregisterDynamicSensorLocked(handle)) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 582 | ALOGE("Dynamic sensor release error."); |
| 583 | } |
| 584 | |
| 585 | size_t numConnections = activeConnections.size(); |
| 586 | for (size_t i=0 ; i < numConnections; ++i) { |
| 587 | if (activeConnections[i] != NULL) { |
| 588 | activeConnections[i]->removeSensor(handle); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 595 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 596 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 597 | // lock if none of the clients need it. |
| 598 | bool needsWakeLock = false; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 599 | size_t numConnections = activeConnections.size(); |
| 600 | for (size_t i=0 ; i < numConnections; ++i) { |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 601 | if (activeConnections[i] != 0) { |
| 602 | activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch, |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 603 | mMapFlushEventsToConnections); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 604 | needsWakeLock |= activeConnections[i]->needsWakeLock(); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 605 | // If the connection has one-shot sensors, it may be cleaned up after first trigger. |
| 606 | // Early check for one-shot sensors. |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 607 | if (activeConnections[i]->hasOneShotSensors()) { |
| 608 | cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer, |
| 609 | count); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 610 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 613 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 614 | if (mWakeLockAcquired && !needsWakeLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 615 | setWakeLockAcquiredLocked(false); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 616 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 617 | } while (!Thread::exitPending()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 618 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 619 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 620 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 624 | sp<Looper> SensorService::getLooper() const { |
| 625 | return mLooper; |
| 626 | } |
| 627 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 628 | void SensorService::resetAllWakeLockRefCounts() { |
| 629 | SortedVector< sp<SensorEventConnection> > activeConnections; |
| 630 | populateActiveConnections(&activeConnections); |
| 631 | { |
| 632 | Mutex::Autolock _l(mLock); |
| 633 | for (size_t i=0 ; i < activeConnections.size(); ++i) { |
| 634 | if (activeConnections[i] != 0) { |
| 635 | activeConnections[i]->resetWakeLockRefCount(); |
| 636 | } |
| 637 | } |
| 638 | setWakeLockAcquiredLocked(false); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | void SensorService::setWakeLockAcquiredLocked(bool acquire) { |
| 643 | if (acquire) { |
| 644 | if (!mWakeLockAcquired) { |
| 645 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 646 | mWakeLockAcquired = true; |
| 647 | } |
| 648 | mLooper->wake(); |
| 649 | } else { |
| 650 | if (mWakeLockAcquired) { |
| 651 | release_wake_lock(WAKE_LOCK_NAME); |
| 652 | mWakeLockAcquired = false; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 657 | bool SensorService::isWakeLockAcquired() { |
| 658 | Mutex::Autolock _l(mLock); |
| 659 | return mWakeLockAcquired; |
| 660 | } |
| 661 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 662 | bool SensorService::SensorEventAckReceiver::threadLoop() { |
| 663 | ALOGD("new thread SensorEventAckReceiver"); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 664 | sp<Looper> looper = mService->getLooper(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 665 | do { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 666 | bool wakeLockAcquired = mService->isWakeLockAcquired(); |
| 667 | int timeout = -1; |
| 668 | if (wakeLockAcquired) timeout = 5000; |
| 669 | int ret = looper->pollOnce(timeout); |
| 670 | if (ret == ALOOPER_POLL_TIMEOUT) { |
| 671 | mService->resetAllWakeLockRefCounts(); |
| 672 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 673 | } while(!Thread::exitPending()); |
| 674 | return false; |
| 675 | } |
| 676 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 677 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 678 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 679 | for (size_t i = 0; i < count; i++) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 680 | if (buffer[i].type == SENSOR_TYPE_META_DATA || |
| 681 | buffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META || |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 682 | buffer[i].type == SENSOR_TYPE_ADDITIONAL_INFO) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 683 | continue; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 684 | } |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 685 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 686 | auto logger = mRecentEvent.find(buffer[i].sensor); |
| 687 | if (logger != mRecentEvent.end()) { |
| 688 | logger->second->addEvent(buffer[i]); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 689 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 690 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 693 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 694 | struct compar { |
| 695 | static int cmp(void const* lhs, void const* rhs) { |
| 696 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 697 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 698 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 699 | } |
| 700 | }; |
| 701 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 702 | } |
| 703 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 704 | String8 SensorService::getSensorName(int handle) const { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 705 | return mSensors.getName(handle); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 708 | bool SensorService::isVirtualSensor(int handle) const { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 709 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 710 | return sensor != nullptr && sensor->isVirtual(); |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 713 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame] | 714 | int handle = event.sensor; |
| 715 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 716 | handle = event.meta_data.sensor; |
| 717 | } |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 718 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 719 | return sensor != nullptr && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 722 | Vector<Sensor> SensorService::getSensorList(const String16& opPackageName) { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 723 | char value[PROPERTY_VALUE_MAX]; |
| 724 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 725 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 726 | mSensors.getUserDebugSensors() : mSensors.getUserSensors(); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 727 | Vector<Sensor> accessibleSensorList; |
| 728 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 729 | Sensor sensor = initialSensorList[i]; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 730 | if (canAccessSensor(sensor, "getSensorList", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 731 | accessibleSensorList.add(sensor); |
| 732 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 733 | ALOGI("Skipped sensor %s because it requires permission %s and app op %d", |
Bernhard Rosenkränzer | 5f61993 | 2014-11-17 21:06:20 +0100 | [diff] [blame] | 734 | sensor.getName().string(), |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 735 | sensor.getRequiredPermission().string(), |
| 736 | sensor.getRequiredAppOp()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 737 | } |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 738 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 739 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 742 | Vector<Sensor> SensorService::getDynamicSensorList(const String16& opPackageName) { |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 743 | Vector<Sensor> accessibleSensorList; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 744 | mSensors.forEachSensor( |
| 745 | [&opPackageName, &accessibleSensorList] (const Sensor& sensor) -> bool { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 746 | if (sensor.isDynamicSensor()) { |
| 747 | if (canAccessSensor(sensor, "getDynamicSensorList", opPackageName)) { |
| 748 | accessibleSensorList.add(sensor); |
| 749 | } else { |
| 750 | ALOGI("Skipped sensor %s because it requires permission %s and app op %" PRId32, |
| 751 | sensor.getName().string(), |
| 752 | sensor.getRequiredPermission().string(), |
| 753 | sensor.getRequiredAppOp()); |
| 754 | } |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 755 | } |
| 756 | return true; |
| 757 | }); |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 758 | return accessibleSensorList; |
| 759 | } |
| 760 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 761 | sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 762 | int requestedMode, const String16& opPackageName) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 763 | // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION. |
| 764 | if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { |
| 765 | return NULL; |
| 766 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 767 | |
| 768 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 769 | // To create a client in DATA_INJECTION mode to inject data, SensorService should already be |
| 770 | // operating in DI mode. |
| 771 | if (requestedMode == DATA_INJECTION) { |
| 772 | if (mCurrentOperatingMode != DATA_INJECTION) return NULL; |
| 773 | if (!isWhiteListedPackage(packageName)) return NULL; |
| 774 | } |
| 775 | |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 776 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 777 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 778 | requestedMode == DATA_INJECTION, opPackageName)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 779 | if (requestedMode == DATA_INJECTION) { |
| 780 | if (mActiveConnections.indexOf(result) < 0) { |
| 781 | mActiveConnections.add(result); |
| 782 | } |
| 783 | // Add the associated file descriptor to the Looper for polling whenever there is data to |
| 784 | // be injected. |
| 785 | result->updateLooperRegistration(mLooper); |
| 786 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 787 | return result; |
| 788 | } |
| 789 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 790 | int SensorService::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 791 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 792 | return (mCurrentOperatingMode == DATA_INJECTION); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | status_t SensorService::resetToNormalMode() { |
| 796 | Mutex::Autolock _l(mLock); |
| 797 | return resetToNormalModeLocked(); |
| 798 | } |
| 799 | |
| 800 | status_t SensorService::resetToNormalModeLocked() { |
| 801 | SensorDevice& dev(SensorDevice::getInstance()); |
| 802 | dev.enableAllSensors(); |
| 803 | status_t err = dev.setMode(NORMAL); |
| 804 | mCurrentOperatingMode = NORMAL; |
| 805 | return err; |
| 806 | } |
| 807 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 808 | void SensorService::cleanupConnection(SensorEventConnection* c) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 809 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 810 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 811 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 812 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 813 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 814 | int handle = mActiveSensors.keyAt(i); |
| 815 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 816 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 817 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 818 | if (sensor != nullptr) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 819 | sensor->activate(c, false); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 820 | } else { |
| 821 | ALOGE("sensor interface of handle=0x%08x is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 822 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 823 | c->removeSensor(handle); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 824 | } |
| 825 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 826 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 827 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 828 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 829 | c, i, handle); |
| 830 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 831 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 832 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 833 | mActiveSensors.removeItemsAt(i, 1); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 834 | mActiveVirtualSensors.erase(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 835 | delete rec; |
| 836 | size--; |
| 837 | } else { |
| 838 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 839 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 840 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 841 | c->updateLooperRegistration(mLooper); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 842 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 843 | BatteryService::cleanup(c->getUid()); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 844 | if (c->needsWakeLock()) { |
| 845 | checkWakeLockStateLocked(); |
| 846 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 849 | sp<SensorInterface> SensorService::getSensorInterfaceFromHandle(int handle) const { |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 850 | return mSensors.getInterface(handle); |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 853 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 854 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 855 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 856 | const String16& opPackageName) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 857 | if (mInitCheck != NO_ERROR) |
| 858 | return mInitCheck; |
| 859 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 860 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 861 | if (sensor == nullptr || |
| 862 | !canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 863 | return BAD_VALUE; |
| 864 | } |
| 865 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 866 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 867 | if ((mCurrentOperatingMode == RESTRICTED || mCurrentOperatingMode == DATA_INJECTION) |
| 868 | && !isWhiteListedPackage(connection->getPackageName())) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 869 | return INVALID_OPERATION; |
| 870 | } |
| 871 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 872 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 873 | if (rec == 0) { |
| 874 | rec = new SensorRecord(connection); |
| 875 | mActiveSensors.add(handle, rec); |
| 876 | if (sensor->isVirtual()) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 877 | mActiveVirtualSensors.emplace(handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 878 | } |
| 879 | } else { |
| 880 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 881 | // this sensor is already activated, but we are adding a connection that uses it. |
| 882 | // 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] | 883 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 884 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 885 | // NOTE: The wake_up flag of this event may get set to |
| 886 | // 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^] | 887 | |
| 888 | auto logger = mRecentEvent.find(handle); |
| 889 | if (logger != mRecentEvent.end()) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 890 | sensors_event_t event; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 891 | // It is unlikely that this buffer is empty as the sensor is already active. |
| 892 | // One possible corner case may be two applications activating an on-change |
| 893 | // sensor at the same time. |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame^] | 894 | if(logger->second->populateLastEvent(&event)) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 895 | event.sensor = handle; |
| 896 | if (event.version == sizeof(sensors_event_t)) { |
| 897 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 898 | setWakeLockAcquiredLocked(true); |
| 899 | } |
| 900 | connection->sendEvents(&event, 1, NULL); |
| 901 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
| 902 | checkWakeLockStateLocked(); |
| 903 | } |
| 904 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 905 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | if (connection->addSensor(handle)) { |
| 912 | BatteryService::enableSensor(connection->getUid(), handle); |
| 913 | // the sensor was added (which means it wasn't already there) |
| 914 | // so, see if this connection becomes active |
| 915 | if (mActiveConnections.indexOf(connection) < 0) { |
| 916 | mActiveConnections.add(connection); |
| 917 | } |
| 918 | } else { |
| 919 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 920 | handle, connection.get()); |
| 921 | } |
| 922 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 923 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 924 | if (samplingPeriodNs < minDelayNs) { |
| 925 | samplingPeriodNs = minDelayNs; |
| 926 | } |
| 927 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 928 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" |
| 929 | "rate=%" PRId64 " timeout== %" PRId64"", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 930 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 931 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 932 | status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 933 | maxBatchReportLatencyNs); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 934 | |
Peng Xu | 20483c4 | 2015-10-26 15:14:43 -0700 | [diff] [blame] | 935 | // Call flush() before calling activate() on the sensor. Wait for a first |
| 936 | // flush complete event before sending events on this connection. Ignore |
| 937 | // one-shot sensors which don't support flush(). Ignore on-change sensors |
| 938 | // to maintain the on-change logic (any on-change events except the initial |
| 939 | // one should be trigger by a change in value). Also if this sensor isn't |
| 940 | // already active, don't call flush(). |
| 941 | if (err == NO_ERROR && |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 942 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_CONTINUOUS && |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 943 | rec->getNumConnections() > 1) { |
| 944 | connection->setFirstFlushPending(handle, true); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 945 | status_t err_flush = sensor->flush(connection.get(), handle); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 946 | // 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] | 947 | if (err_flush == NO_ERROR) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 948 | rec->addPendingFlushConnection(connection.get()); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 949 | } else { |
| 950 | connection->setFirstFlushPending(handle, false); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 953 | |
| 954 | if (err == NO_ERROR) { |
| 955 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); |
| 956 | err = sensor->activate(connection.get(), true); |
| 957 | } |
| 958 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 959 | if (err == NO_ERROR) { |
| 960 | connection->updateLooperRegistration(mLooper); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 961 | SensorRegistrationInfo ®_info = |
| 962 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); |
| 963 | reg_info.mSensorHandle = handle; |
| 964 | reg_info.mSamplingRateUs = samplingPeriodNs/1000; |
| 965 | reg_info.mMaxReportLatencyUs = maxBatchReportLatencyNs/1000; |
| 966 | reg_info.mActivated = true; |
| 967 | reg_info.mPackageName = connection->getPackageName(); |
| 968 | time_t rawtime = time(NULL); |
| 969 | struct tm * timeinfo = localtime(&rawtime); |
| 970 | reg_info.mHour = timeinfo->tm_hour; |
| 971 | reg_info.mMin = timeinfo->tm_min; |
| 972 | reg_info.mSec = timeinfo->tm_sec; |
| 973 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 976 | if (err != NO_ERROR) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 977 | // batch/activate has failed, reset our state. |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 978 | cleanupWithoutDisableLocked(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 979 | } |
| 980 | return err; |
| 981 | } |
| 982 | |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 983 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 984 | if (mInitCheck != NO_ERROR) |
| 985 | return mInitCheck; |
| 986 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 987 | Mutex::Autolock _l(mLock); |
| 988 | status_t err = cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 989 | if (err == NO_ERROR) { |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 990 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 991 | err = sensor != nullptr ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 992 | |
| 993 | } |
| 994 | if (err == NO_ERROR) { |
| 995 | SensorRegistrationInfo ®_info = |
| 996 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); |
| 997 | reg_info.mActivated = false; |
| 998 | reg_info.mPackageName= connection->getPackageName(); |
| 999 | reg_info.mSensorHandle = handle; |
| 1000 | time_t rawtime = time(NULL); |
| 1001 | struct tm * timeinfo = localtime(&rawtime); |
| 1002 | reg_info.mHour = timeinfo->tm_hour; |
| 1003 | reg_info.mMin = timeinfo->tm_min; |
| 1004 | reg_info.mSec = timeinfo->tm_sec; |
| 1005 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1006 | } |
| 1007 | return err; |
| 1008 | } |
| 1009 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1010 | status_t SensorService::cleanupWithoutDisable( |
| 1011 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1012 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1013 | return cleanupWithoutDisableLocked(connection, handle); |
| 1014 | } |
| 1015 | |
| 1016 | status_t SensorService::cleanupWithoutDisableLocked( |
| 1017 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1018 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1019 | if (rec) { |
| 1020 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 1021 | if (connection->removeSensor(handle)) { |
| 1022 | BatteryService::disableSensor(connection->getUid(), handle); |
| 1023 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1024 | if (connection->hasAnySensor() == false) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1025 | connection->updateLooperRegistration(mLooper); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1026 | mActiveConnections.remove(connection); |
| 1027 | } |
| 1028 | // see if this sensor becomes inactive |
| 1029 | if (rec->removeConnection(connection)) { |
| 1030 | mActiveSensors.removeItem(handle); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1031 | mActiveVirtualSensors.erase(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1032 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1033 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1034 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1035 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1036 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1039 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Peng Xu | 47e9601 | 2016-03-28 17:55:56 -0700 | [diff] [blame] | 1040 | int handle, nsecs_t ns, const String16& opPackageName) { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1041 | if (mInitCheck != NO_ERROR) |
| 1042 | return mInitCheck; |
| 1043 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1044 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1045 | if (sensor == nullptr || |
| 1046 | !canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1047 | return BAD_VALUE; |
| 1048 | } |
| 1049 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1050 | if (ns < 0) |
| 1051 | return BAD_VALUE; |
| 1052 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 1053 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 1054 | if (ns < minDelayNs) { |
| 1055 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1058 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1061 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, |
| 1062 | const String16& opPackageName) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1063 | if (mInitCheck != NO_ERROR) return mInitCheck; |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1064 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1065 | const int halVersion = dev.getHalDeviceVersion(); |
| 1066 | status_t err(NO_ERROR); |
| 1067 | Mutex::Autolock _l(mLock); |
| 1068 | // Loop through all sensors for this connection and call flush on each of them. |
| 1069 | for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) { |
| 1070 | const int handle = connection->mSensorInfo.keyAt(i); |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 1071 | sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle); |
| 1072 | if (sensor == nullptr) { |
| 1073 | continue; |
| 1074 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1075 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 1076 | ALOGE("flush called on a one-shot sensor"); |
| 1077 | err = INVALID_OPERATION; |
| 1078 | continue; |
| 1079 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1080 | if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) { |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1081 | // For older devices just increment pending flush count which will send a trivial |
| 1082 | // flush complete event. |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1083 | connection->incrementPendingFlushCount(handle); |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1084 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1085 | if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) { |
| 1086 | err = INVALID_OPERATION; |
| 1087 | continue; |
| 1088 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1089 | status_t err_flush = sensor->flush(connection.get(), handle); |
| 1090 | if (err_flush == NO_ERROR) { |
| 1091 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 1092 | if (rec != NULL) rec->addPendingFlushConnection(connection); |
| 1093 | } |
| 1094 | err = (err_flush != NO_ERROR) ? err_flush : err; |
| 1095 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1096 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1097 | return err; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1098 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1099 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1100 | bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, |
| 1101 | const String16& opPackageName) { |
| 1102 | const String8& requiredPermission = sensor.getRequiredPermission(); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1103 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1104 | if (requiredPermission.length() <= 0) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1105 | return true; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | bool hasPermission = false; |
| 1109 | |
| 1110 | // Runtime permissions can't use the cache as they may change. |
| 1111 | if (sensor.isRequiredPermissionRuntime()) { |
| 1112 | hasPermission = checkPermission(String16(requiredPermission), |
| 1113 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1114 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1115 | hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission)); |
| 1116 | } |
| 1117 | |
| 1118 | if (!hasPermission) { |
| 1119 | ALOGE("%s a sensor (%s) without holding its required permission: %s", |
| 1120 | operation, sensor.getName().string(), sensor.getRequiredPermission().string()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1121 | return false; |
| 1122 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1123 | |
| 1124 | const int32_t opCode = sensor.getRequiredAppOp(); |
| 1125 | if (opCode >= 0) { |
| 1126 | AppOpsManager appOps; |
| 1127 | if (appOps.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName) |
| 1128 | != AppOpsManager::MODE_ALLOWED) { |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 1129 | ALOGE("%s a sensor (%s) without enabled required app op: %d", |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1130 | operation, sensor.getName().string(), opCode); |
| 1131 | return false; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | return true; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1138 | void SensorService::checkWakeLockState() { |
| 1139 | Mutex::Autolock _l(mLock); |
| 1140 | checkWakeLockStateLocked(); |
| 1141 | } |
| 1142 | |
| 1143 | void SensorService::checkWakeLockStateLocked() { |
| 1144 | if (!mWakeLockAcquired) { |
| 1145 | return; |
| 1146 | } |
| 1147 | bool releaseLock = true; |
| 1148 | for (size_t i=0 ; i<mActiveConnections.size() ; i++) { |
| 1149 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 1150 | if (connection != 0) { |
| 1151 | if (connection->needsWakeLock()) { |
| 1152 | releaseLock = false; |
| 1153 | break; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | if (releaseLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1158 | setWakeLockAcquiredLocked(false); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) { |
| 1163 | Mutex::Autolock _l(mLock); |
| 1164 | connection->writeToSocketFromCache(); |
| 1165 | if (connection->needsWakeLock()) { |
| 1166 | setWakeLockAcquiredLocked(true); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | void SensorService::populateActiveConnections( |
| 1171 | SortedVector< sp<SensorEventConnection> >* activeConnections) { |
| 1172 | Mutex::Autolock _l(mLock); |
| 1173 | for (size_t i=0 ; i < mActiveConnections.size(); ++i) { |
| 1174 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 1175 | if (connection != 0) { |
| 1176 | activeConnections->add(connection); |
| 1177 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1178 | } |
| 1179 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1180 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1181 | bool SensorService::isWhiteListedPackage(const String8& packageName) { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1182 | return (packageName.contains(mWhiteListedPackage.string())); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1185 | }; // namespace android |
| 1186 | |