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