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