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