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