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 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 18 | #include <math.h> |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 22 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
| 24 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 25 | #include <utils/SortedVector.h> |
| 26 | #include <utils/KeyedVector.h> |
| 27 | #include <utils/threads.h> |
| 28 | #include <utils/Atomic.h> |
| 29 | #include <utils/Errors.h> |
| 30 | #include <utils/RefBase.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 31 | #include <utils/Singleton.h> |
Mathias Agopian | c4a930d | 2010-07-22 22:19:43 -0700 | [diff] [blame] | 32 | #include <utils/String16.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 33 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 34 | #include <binder/AppOpsManager.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 35 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 36 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 37 | #include <binder/PermissionCache.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 38 | |
| 39 | #include <gui/ISensorServer.h> |
| 40 | #include <gui/ISensorEventConnection.h> |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 41 | #include <gui/SensorEventQueue.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 42 | |
| 43 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 44 | #include <hardware_legacy/power.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 45 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 46 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 47 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 48 | #include "GravitySensor.h" |
| 49 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 50 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 51 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 52 | #include "SensorFusion.h" |
| 53 | #include "SensorService.h" |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 54 | |
| 55 | namespace android { |
| 56 | // --------------------------------------------------------------------------- |
| 57 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Notes: |
| 60 | * |
| 61 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 62 | * - run mag sensor from time to time to force calibration |
| 63 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 64 | * |
| 65 | */ |
| 66 | |
Aravind Akella | 8ef3c89 | 2015-07-10 11:24:28 -0700 | [diff] [blame] | 67 | const char* SensorService::WAKE_LOCK_NAME = "SensorService_wakelock"; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 68 | // Permissions. |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 69 | static const String16 sDump("android.permission.DUMP"); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 70 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 71 | SensorService::SensorService() |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 72 | : mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED), |
| 73 | mWakeLockAcquired(false) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 74 | { |
| 75 | } |
| 76 | |
| 77 | void SensorService::onFirstRef() |
| 78 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 79 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 80 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 81 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 82 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 83 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 84 | ssize_t count = dev.getSensorList(&list); |
| 85 | if (count > 0) { |
| 86 | ssize_t orientationIndex = -1; |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 87 | bool hasGyro = false, hasAccel = false, hasMag = false; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 88 | uint32_t virtualSensorsNeeds = |
| 89 | (1<<SENSOR_TYPE_GRAVITY) | |
| 90 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 91 | (1<<SENSOR_TYPE_ROTATION_VECTOR) | |
| 92 | (1<<SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR) | |
| 93 | (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 94 | |
| 95 | mLastEventSeen.setCapacity(count); |
| 96 | for (ssize_t i=0 ; i<count ; i++) { |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 97 | bool useThisSensor=true; |
| 98 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 99 | switch (list[i].type) { |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 100 | case SENSOR_TYPE_ACCELEROMETER: |
| 101 | hasAccel = true; |
| 102 | break; |
| 103 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 104 | hasMag = true; |
| 105 | break; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 106 | case SENSOR_TYPE_ORIENTATION: |
| 107 | orientationIndex = i; |
| 108 | break; |
| 109 | case SENSOR_TYPE_GYROSCOPE: |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 110 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 111 | hasGyro = true; |
| 112 | break; |
| 113 | case SENSOR_TYPE_GRAVITY: |
| 114 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 115 | case SENSOR_TYPE_ROTATION_VECTOR: |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 116 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 117 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 118 | if (IGNORE_HARDWARE_FUSION) { |
| 119 | useThisSensor = false; |
| 120 | } else { |
| 121 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 122 | } |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 123 | break; |
| 124 | } |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 125 | if (useThisSensor) { |
| 126 | registerSensor( new HardwareSensor(list[i]) ); |
| 127 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 128 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 129 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 130 | // it's safe to instantiate the SensorFusion object here |
| 131 | // (it wants to be instantiated after h/w sensors have been |
| 132 | // registered) |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 133 | SensorFusion::getInstance(); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 134 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 135 | // build the sensor list returned to users |
| 136 | mUserSensorList = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 137 | |
Aravind Akella | f504789 | 2015-07-20 17:29:33 -0700 | [diff] [blame] | 138 | if (hasGyro && hasAccel && hasMag) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 139 | // Add Android virtual sensors if they're not already |
| 140 | // available in the HAL |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 141 | Sensor aSensor; |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 142 | |
| 143 | aSensor = registerVirtualSensor( new RotationVectorSensor() ); |
| 144 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 145 | mUserSensorList.add(aSensor); |
| 146 | } |
| 147 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 148 | aSensor = registerVirtualSensor( new OrientationSensor() ); |
| 149 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 150 | // if we are doing our own rotation-vector, also add |
| 151 | // the orientation sensor and remove the HAL provided one. |
| 152 | mUserSensorList.replaceAt(aSensor, orientationIndex); |
| 153 | } |
| 154 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 155 | aSensor = registerVirtualSensor( |
| 156 | new LinearAccelerationSensor(list, count) ); |
| 157 | if (virtualSensorsNeeds & |
| 158 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) { |
| 159 | mUserSensorList.add(aSensor); |
| 160 | } |
| 161 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 162 | // virtual debugging sensors are not added to mUserSensorList |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 163 | registerVirtualSensor( new CorrectedGyroSensor(list, count) ); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 164 | registerVirtualSensor( new GyroDriftSensor() ); |
| 165 | } |
| 166 | |
Peng Xu | f66684a | 2015-07-23 11:41:53 -0700 | [diff] [blame^] | 167 | if (hasAccel && hasGyro) { |
| 168 | Sensor aSensor; |
| 169 | |
| 170 | aSensor = registerVirtualSensor( |
| 171 | new GravitySensor(list, count) ); |
| 172 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) { |
| 173 | mUserSensorList.add(aSensor); |
| 174 | } |
| 175 | |
| 176 | aSensor = registerVirtualSensor( |
| 177 | new GameRotationVectorSensor() ); |
| 178 | if (virtualSensorsNeeds & |
| 179 | (1<<SENSOR_TYPE_GAME_ROTATION_VECTOR)) { |
| 180 | mUserSensorList.add(aSensor); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (hasAccel && hasMag) { |
| 185 | Sensor aSensor; |
| 186 | |
| 187 | aSensor = registerVirtualSensor( |
| 188 | new GeoMagRotationVectorSensor() ); |
| 189 | if (virtualSensorsNeeds & |
| 190 | (1<<SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR)) { |
| 191 | mUserSensorList.add(aSensor); |
| 192 | } |
| 193 | } |
| 194 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 195 | // debugging sensor list |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 196 | mUserSensorListDebug = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 197 | |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 198 | // Check if the device really supports batching by looking at the FIFO event |
| 199 | // counts for each sensor. |
| 200 | bool batchingSupported = false; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 201 | for (size_t i = 0; i < mSensorList.size(); ++i) { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 202 | if (mSensorList[i].getFifoMaxEventCount() > 0) { |
| 203 | batchingSupported = true; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (batchingSupported) { |
| 209 | // Increase socket buffer size to a max of 100 KB for batching capabilities. |
| 210 | mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; |
| 211 | } else { |
| 212 | mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; |
| 213 | } |
| 214 | |
| 215 | // Compare the socketBufferSize value against the system limits and limit |
| 216 | // it to maxSystemSocketBufferSize if necessary. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 217 | FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r"); |
| 218 | char line[128]; |
| 219 | if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) { |
| 220 | line[sizeof(line) - 1] = '\0'; |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 221 | size_t maxSystemSocketBufferSize; |
| 222 | sscanf(line, "%zu", &maxSystemSocketBufferSize); |
| 223 | if (mSocketBufferSize > maxSystemSocketBufferSize) { |
| 224 | mSocketBufferSize = maxSystemSocketBufferSize; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 227 | if (fp) { |
| 228 | fclose(fp); |
| 229 | } |
| 230 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 231 | mWakeLockAcquired = false; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 232 | mLooper = new Looper(false); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 233 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 234 | mSensorEventBuffer = new sensors_event_t[minBufferSize]; |
| 235 | mSensorEventScratch = new sensors_event_t[minBufferSize]; |
| 236 | mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize]; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 237 | mCurrentOperatingMode = NORMAL; |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 238 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 239 | mNextSensorRegIndex = 0; |
| 240 | for (int i = 0; i < SENSOR_REGISTRATIONS_BUF_SIZE; ++i) { |
| 241 | mLastNSensorRegistrations.push(); |
| 242 | } |
| 243 | |
| 244 | mInitCheck = NO_ERROR; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 245 | mAckReceiver = new SensorEventAckReceiver(this); |
| 246 | mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY); |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 247 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 248 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 249 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 252 | Sensor SensorService::registerSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 253 | { |
| 254 | sensors_event_t event; |
| 255 | memset(&event, 0, sizeof(event)); |
| 256 | |
| 257 | const Sensor sensor(s->getSensor()); |
| 258 | // add to the sensor list (returned to clients) |
| 259 | mSensorList.add(sensor); |
| 260 | // add to our handle->SensorInterface mapping |
| 261 | mSensorMap.add(sensor.getHandle(), s); |
| 262 | // create an entry in the mLastEventSeen array |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 263 | mLastEventSeen.add(sensor.getHandle(), NULL); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 264 | |
| 265 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 268 | Sensor SensorService::registerVirtualSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 269 | { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 270 | Sensor sensor = registerSensor(s); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 271 | mVirtualSensorList.add( s ); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 272 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 275 | SensorService::~SensorService() |
| 276 | { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 277 | for (size_t i=0 ; i<mSensorMap.size() ; i++) |
| 278 | delete mSensorMap.valueAt(i); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 281 | status_t SensorService::dump(int fd, const Vector<String16>& args) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 282 | { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 283 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 284 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 285 | result.appendFormat("Permission Denial: " |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 286 | "can't dump SensorService from pid=%d, uid=%d\n", |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 287 | IPCThreadState::self()->getCallingPid(), |
| 288 | IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 289 | } else { |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 290 | if (args.size() > 2) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 291 | return INVALID_OPERATION; |
| 292 | } |
| 293 | Mutex::Autolock _l(mLock); |
| 294 | SensorDevice& dev(SensorDevice::getInstance()); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 295 | if (args.size() == 2 && args[0] == String16("restrict")) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 296 | // If already in restricted mode. Ignore. |
| 297 | if (mCurrentOperatingMode == RESTRICTED) { |
| 298 | return status_t(NO_ERROR); |
| 299 | } |
| 300 | // If in any mode other than normal, ignore. |
| 301 | if (mCurrentOperatingMode != NORMAL) { |
| 302 | return INVALID_OPERATION; |
| 303 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 304 | mCurrentOperatingMode = RESTRICTED; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 305 | dev.disableAllSensors(); |
| 306 | // Clear all pending flush connections for all active sensors. If one of the active |
| 307 | // connections has called flush() and the underlying sensor has been disabled before a |
| 308 | // flush complete event is returned, we need to remove the connection from this queue. |
| 309 | for (size_t i=0 ; i< mActiveSensors.size(); ++i) { |
| 310 | mActiveSensors.valueAt(i)->clearAllPendingFlushConnections(); |
| 311 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 312 | mWhiteListedPackage.setTo(String8(args[1])); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 313 | return status_t(NO_ERROR); |
| 314 | } else if (args.size() == 1 && args[0] == String16("enable")) { |
| 315 | // If currently in restricted mode, reset back to NORMAL mode else ignore. |
| 316 | if (mCurrentOperatingMode == RESTRICTED) { |
| 317 | mCurrentOperatingMode = NORMAL; |
| 318 | dev.enableAllSensors(); |
| 319 | } |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 320 | if (mCurrentOperatingMode == DATA_INJECTION) { |
| 321 | resetToNormalModeLocked(); |
| 322 | } |
| 323 | mWhiteListedPackage.clear(); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 324 | return status_t(NO_ERROR); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 325 | } else if (args.size() == 2 && args[0] == String16("data_injection")) { |
| 326 | if (mCurrentOperatingMode == NORMAL) { |
| 327 | dev.disableAllSensors(); |
| 328 | status_t err = dev.setMode(DATA_INJECTION); |
| 329 | if (err == NO_ERROR) { |
| 330 | mCurrentOperatingMode = DATA_INJECTION; |
| 331 | } else { |
| 332 | // Re-enable sensors. |
| 333 | dev.enableAllSensors(); |
| 334 | } |
| 335 | mWhiteListedPackage.setTo(String8(args[1])); |
| 336 | return NO_ERROR; |
| 337 | } else if (mCurrentOperatingMode == DATA_INJECTION) { |
| 338 | // Already in DATA_INJECTION mode. Treat this as a no_op. |
| 339 | return NO_ERROR; |
| 340 | } else { |
| 341 | // Transition to data injection mode supported only from NORMAL mode. |
| 342 | return INVALID_OPERATION; |
| 343 | } |
Aravind Akella | ee155ca | 2015-06-24 08:31:32 -0700 | [diff] [blame] | 344 | } else if (mSensorList.size() == 0) { |
| 345 | result.append("No Sensors on the device\n"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 346 | } else { |
| 347 | // Default dump the sensor list and debugging information. |
| 348 | result.append("Sensor List:\n"); |
| 349 | for (size_t i=0 ; i<mSensorList.size() ; i++) { |
| 350 | const Sensor& s(mSensorList[i]); |
| 351 | result.appendFormat( |
| 352 | "%-15s| %-10s| version=%d |%-20s| 0x%08x | \"%s\" | type=%d |", |
| 353 | s.getName().string(), |
| 354 | s.getVendor().string(), |
| 355 | s.getVersion(), |
| 356 | s.getStringType().string(), |
| 357 | s.getHandle(), |
| 358 | s.getRequiredPermission().string(), |
| 359 | s.getType()); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 360 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 361 | const int reportingMode = s.getReportingMode(); |
| 362 | if (reportingMode == AREPORTING_MODE_CONTINUOUS) { |
| 363 | result.append(" continuous | "); |
| 364 | } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) { |
| 365 | result.append(" on-change | "); |
| 366 | } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) { |
| 367 | result.append(" one-shot | "); |
| 368 | } else { |
| 369 | result.append(" special-trigger | "); |
| 370 | } |
| 371 | |
| 372 | if (s.getMaxDelay() > 0) { |
| 373 | result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay()); |
| 374 | } else { |
| 375 | result.appendFormat("maxDelay=%dus |", s.getMaxDelay()); |
| 376 | } |
| 377 | |
| 378 | if (s.getMinDelay() > 0) { |
| 379 | result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay()); |
| 380 | } else { |
| 381 | result.appendFormat("minDelay=%dus |", s.getMinDelay()); |
| 382 | } |
| 383 | |
| 384 | if (s.getFifoMaxEventCount() > 0) { |
| 385 | result.appendFormat("FifoMax=%d events | ", |
| 386 | s.getFifoMaxEventCount()); |
| 387 | } else { |
| 388 | result.append("no batching | "); |
| 389 | } |
| 390 | |
| 391 | if (s.isWakeUpSensor()) { |
| 392 | result.appendFormat("wakeUp | "); |
| 393 | } else { |
| 394 | result.appendFormat("non-wakeUp | "); |
| 395 | } |
| 396 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 397 | int bufIndex = mLastEventSeen.indexOfKey(s.getHandle()); |
| 398 | if (bufIndex >= 0) { |
| 399 | const CircularBuffer* buf = mLastEventSeen.valueAt(bufIndex); |
| 400 | if (buf != NULL && s.getRequiredPermission().isEmpty()) { |
| 401 | buf->printBuffer(result); |
| 402 | } else { |
| 403 | result.append("last=<> \n"); |
| 404 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 405 | } |
| 406 | result.append("\n"); |
| 407 | } |
| 408 | SensorFusion::getInstance().dump(result); |
| 409 | SensorDevice::getInstance().dump(result); |
| 410 | |
| 411 | result.append("Active sensors:\n"); |
| 412 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
| 413 | int handle = mActiveSensors.keyAt(i); |
| 414 | result.appendFormat("%s (handle=0x%08x, connections=%zu)\n", |
| 415 | getSensorName(handle).string(), |
| 416 | handle, |
| 417 | mActiveSensors.valueAt(i)->getNumConnections()); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 420 | result.appendFormat("Socket Buffer size = %zd events\n", |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 421 | mSocketBufferSize/sizeof(sensors_event_t)); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 422 | result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : |
| 423 | "not held"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 424 | result.appendFormat("Mode :"); |
| 425 | switch(mCurrentOperatingMode) { |
| 426 | case NORMAL: |
| 427 | result.appendFormat(" NORMAL\n"); |
| 428 | break; |
| 429 | case RESTRICTED: |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 430 | result.appendFormat(" RESTRICTED : %s\n", mWhiteListedPackage.string()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 431 | break; |
| 432 | case DATA_INJECTION: |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 433 | result.appendFormat(" DATA_INJECTION : %s\n", mWhiteListedPackage.string()); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 434 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 435 | result.appendFormat("%zd active connections\n", mActiveConnections.size()); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 436 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 437 | for (size_t i=0 ; i < mActiveConnections.size() ; i++) { |
| 438 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 439 | if (connection != 0) { |
| 440 | result.appendFormat("Connection Number: %zu \n", i); |
| 441 | connection->dump(result); |
| 442 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 443 | } |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 444 | |
| 445 | result.appendFormat("Previous Registrations:\n"); |
| 446 | // Log in the reverse chronological order. |
| 447 | int currentIndex = (mNextSensorRegIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 448 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 449 | const int startIndex = currentIndex; |
| 450 | do { |
| 451 | const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[currentIndex]; |
| 452 | if (SensorRegistrationInfo::isSentinel(reg_info)) { |
| 453 | // Ignore sentinel, proceed to next item. |
| 454 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 455 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 456 | continue; |
| 457 | } |
| 458 | if (reg_info.mActivated) { |
| 459 | result.appendFormat("%02d:%02d:%02d activated package=%s handle=0x%08x " |
| 460 | "samplingRate=%dus maxReportLatency=%dus\n", |
| 461 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
| 462 | reg_info.mPackageName.string(), reg_info.mSensorHandle, |
| 463 | reg_info.mSamplingRateUs, reg_info.mMaxReportLatencyUs); |
| 464 | } else { |
| 465 | result.appendFormat("%02d:%02d:%02d de-activated package=%s handle=0x%08x\n", |
| 466 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
| 467 | reg_info.mPackageName.string(), reg_info.mSensorHandle); |
| 468 | } |
| 469 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 470 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 471 | } while(startIndex != currentIndex); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 472 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 473 | } |
| 474 | write(fd, result.string(), result.size()); |
| 475 | return NO_ERROR; |
| 476 | } |
| 477 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 478 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 479 | sensors_event_t const* buffer, const int count) { |
| 480 | for (int i=0 ; i<count ; i++) { |
| 481 | int handle = buffer[i].sensor; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 482 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 483 | handle = buffer[i].meta_data.sensor; |
| 484 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 485 | if (connection->hasSensor(handle)) { |
| 486 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 487 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 488 | // for this particular one_shot sensor, try cleaning up the connection. |
| 489 | if (sensor != NULL && |
| 490 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 491 | sensor->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 492 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 493 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 494 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 495 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 496 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 499 | bool SensorService::threadLoop() |
| 500 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 501 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 502 | |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 503 | // each virtual sensor could generate an event per "real" event, that's why we need |
| 504 | // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. |
| 505 | // in practice, this is too aggressive, but guaranteed to be enough. |
| 506 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 507 | const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size()); |
| 508 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 509 | SensorDevice& device(SensorDevice::getInstance()); |
| 510 | const size_t vcount = mVirtualSensorList.size(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 511 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 512 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 513 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 514 | ssize_t count = device.poll(mSensorEventBuffer, numEventMax); |
| 515 | if (count < 0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 516 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 517 | break; |
| 518 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 519 | |
| 520 | // Reset sensors_event_t.flags to zero for all events in the buffer. |
| 521 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 522 | mSensorEventBuffer[i].flags = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 523 | } |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 524 | |
| 525 | // Make a copy of the connection vector as some connections may be removed during the |
| 526 | // course of this loop (especially when one-shot sensor events are present in the |
| 527 | // sensor_event buffer). Promote all connections to StrongPointers before the lock is |
| 528 | // acquired. If the destructor of the sp gets called when the lock is acquired, it may |
| 529 | // result in a deadlock as ~SensorEventConnection() needs to acquire mLock again for |
| 530 | // cleanup. So copy all the strongPointers to a vector before the lock is acquired. |
| 531 | SortedVector< sp<SensorEventConnection> > activeConnections; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 532 | populateActiveConnections(&activeConnections); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 533 | Mutex::Autolock _l(mLock); |
| 534 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 535 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 536 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 537 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 538 | // releasing the wakelock. |
| 539 | bool bufferHasWakeUpEvent = false; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 540 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 541 | if (isWakeUpSensorEvent(mSensorEventBuffer[i])) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 542 | bufferHasWakeUpEvent = true; |
| 543 | break; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 547 | if (bufferHasWakeUpEvent && !mWakeLockAcquired) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 548 | setWakeLockAcquiredLocked(true); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 549 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 550 | recordLastValueLocked(mSensorEventBuffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 551 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 552 | // handle virtual sensors |
| 553 | if (count && vcount) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 554 | sensors_event_t const * const event = mSensorEventBuffer; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 555 | const size_t activeVirtualSensorCount = mActiveVirtualSensors.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 556 | if (activeVirtualSensorCount) { |
| 557 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 558 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 559 | if (fusion.isEnabled()) { |
| 560 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 561 | fusion.process(event[i]); |
| 562 | } |
| 563 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 564 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 565 | for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 566 | if (count + k >= minBufferSize) { |
| 567 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 568 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 569 | count, k, minBufferSize); |
| 570 | break; |
| 571 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 572 | sensors_event_t out; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 573 | SensorInterface* si = mActiveVirtualSensors.valueAt(j); |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 574 | if (si->process(&out, event[i])) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 575 | mSensorEventBuffer[count + k] = out; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 576 | k++; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | if (k) { |
| 581 | // record the last synthesized values |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 582 | recordLastValueLocked(&mSensorEventBuffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 583 | count += k; |
| 584 | // sort the buffer by time-stamps |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 585 | sortEventBuffer(mSensorEventBuffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 590 | // handle backward compatibility for RotationVector sensor |
| 591 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 592 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 593 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 594 | // All the 4 components of the quaternion should be available |
| 595 | // No heading accuracy. Set it to -1 |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 596 | mSensorEventBuffer[i].data[4] = -1; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // Map flush_complete_events in the buffer to SensorEventConnections which called |
| 602 | // flush on the hardware sensor. mapFlushEventsToConnections[i] will be the |
| 603 | // SensorEventConnection mapped to the corresponding flush_complete_event in |
| 604 | // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise). |
| 605 | for (int i = 0; i < count; ++i) { |
| 606 | mMapFlushEventsToConnections[i] = NULL; |
| 607 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) { |
| 608 | const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor; |
| 609 | SensorRecord* rec = mActiveSensors.valueFor(sensor_handle); |
| 610 | if (rec != NULL) { |
| 611 | mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection(); |
| 612 | rec->removeFirstPendingFlushConnection(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 617 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 618 | // lock if none of the clients need it. |
| 619 | bool needsWakeLock = false; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 620 | size_t numConnections = activeConnections.size(); |
| 621 | for (size_t i=0 ; i < numConnections; ++i) { |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 622 | if (activeConnections[i] != 0) { |
| 623 | activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch, |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 624 | mMapFlushEventsToConnections); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 625 | needsWakeLock |= activeConnections[i]->needsWakeLock(); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 626 | // If the connection has one-shot sensors, it may be cleaned up after first trigger. |
| 627 | // Early check for one-shot sensors. |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 628 | if (activeConnections[i]->hasOneShotSensors()) { |
| 629 | cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer, |
| 630 | count); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 631 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 634 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 635 | if (mWakeLockAcquired && !needsWakeLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 636 | setWakeLockAcquiredLocked(false); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 637 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 638 | } while (!Thread::exitPending()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 639 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 640 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 641 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 645 | sp<Looper> SensorService::getLooper() const { |
| 646 | return mLooper; |
| 647 | } |
| 648 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 649 | void SensorService::resetAllWakeLockRefCounts() { |
| 650 | SortedVector< sp<SensorEventConnection> > activeConnections; |
| 651 | populateActiveConnections(&activeConnections); |
| 652 | { |
| 653 | Mutex::Autolock _l(mLock); |
| 654 | for (size_t i=0 ; i < activeConnections.size(); ++i) { |
| 655 | if (activeConnections[i] != 0) { |
| 656 | activeConnections[i]->resetWakeLockRefCount(); |
| 657 | } |
| 658 | } |
| 659 | setWakeLockAcquiredLocked(false); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | void SensorService::setWakeLockAcquiredLocked(bool acquire) { |
| 664 | if (acquire) { |
| 665 | if (!mWakeLockAcquired) { |
| 666 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 667 | mWakeLockAcquired = true; |
| 668 | } |
| 669 | mLooper->wake(); |
| 670 | } else { |
| 671 | if (mWakeLockAcquired) { |
| 672 | release_wake_lock(WAKE_LOCK_NAME); |
| 673 | mWakeLockAcquired = false; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 678 | bool SensorService::isWakeLockAcquired() { |
| 679 | Mutex::Autolock _l(mLock); |
| 680 | return mWakeLockAcquired; |
| 681 | } |
| 682 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 683 | bool SensorService::SensorEventAckReceiver::threadLoop() { |
| 684 | ALOGD("new thread SensorEventAckReceiver"); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 685 | sp<Looper> looper = mService->getLooper(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 686 | do { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 687 | bool wakeLockAcquired = mService->isWakeLockAcquired(); |
| 688 | int timeout = -1; |
| 689 | if (wakeLockAcquired) timeout = 5000; |
| 690 | int ret = looper->pollOnce(timeout); |
| 691 | if (ret == ALOOPER_POLL_TIMEOUT) { |
| 692 | mService->resetAllWakeLockRefCounts(); |
| 693 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 694 | } while(!Thread::exitPending()); |
| 695 | return false; |
| 696 | } |
| 697 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 698 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 699 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 700 | for (size_t i = 0; i < count; i++) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 701 | if (buffer[i].type != SENSOR_TYPE_META_DATA) { |
| 702 | CircularBuffer* &circular_buf = mLastEventSeen.editValueFor(buffer[i].sensor); |
| 703 | if (circular_buf == NULL) { |
| 704 | circular_buf = new CircularBuffer(buffer[i].type); |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 705 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 706 | circular_buf->addEvent(buffer[i]); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 711 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) |
| 712 | { |
| 713 | struct compar { |
| 714 | static int cmp(void const* lhs, void const* rhs) { |
| 715 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 716 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 717 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 718 | } |
| 719 | }; |
| 720 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 721 | } |
| 722 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 723 | String8 SensorService::getSensorName(int handle) const { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 724 | size_t count = mUserSensorList.size(); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 725 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 726 | const Sensor& sensor(mUserSensorList[i]); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 727 | if (sensor.getHandle() == handle) { |
| 728 | return sensor.getName(); |
| 729 | } |
| 730 | } |
| 731 | String8 result("unknown"); |
| 732 | return result; |
| 733 | } |
| 734 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 735 | bool SensorService::isVirtualSensor(int handle) const { |
| 736 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 737 | return sensor->isVirtual(); |
| 738 | } |
| 739 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 740 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame] | 741 | int handle = event.sensor; |
| 742 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 743 | handle = event.meta_data.sensor; |
| 744 | } |
| 745 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 746 | return sensor != NULL && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 749 | SensorService::SensorRecord * SensorService::getSensorRecord(int handle) { |
| 750 | return mActiveSensors.valueFor(handle); |
| 751 | } |
| 752 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 753 | Vector<Sensor> SensorService::getSensorList(const String16& opPackageName) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 754 | { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 755 | char value[PROPERTY_VALUE_MAX]; |
| 756 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 757 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
| 758 | mUserSensorListDebug : mUserSensorList; |
| 759 | Vector<Sensor> accessibleSensorList; |
| 760 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 761 | Sensor sensor = initialSensorList[i]; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 762 | if (canAccessSensor(sensor, "getSensorList", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 763 | accessibleSensorList.add(sensor); |
| 764 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 765 | 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] | 766 | sensor.getName().string(), |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 767 | sensor.getRequiredPermission().string(), |
| 768 | sensor.getRequiredAppOp()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 769 | } |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 770 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 771 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 774 | sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 775 | int requestedMode, const String16& opPackageName) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 776 | // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION. |
| 777 | if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { |
| 778 | return NULL; |
| 779 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 780 | |
| 781 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 782 | // To create a client in DATA_INJECTION mode to inject data, SensorService should already be |
| 783 | // operating in DI mode. |
| 784 | if (requestedMode == DATA_INJECTION) { |
| 785 | if (mCurrentOperatingMode != DATA_INJECTION) return NULL; |
| 786 | if (!isWhiteListedPackage(packageName)) return NULL; |
| 787 | } |
| 788 | |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 789 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 790 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 791 | requestedMode == DATA_INJECTION, opPackageName)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 792 | if (requestedMode == DATA_INJECTION) { |
| 793 | if (mActiveConnections.indexOf(result) < 0) { |
| 794 | mActiveConnections.add(result); |
| 795 | } |
| 796 | // Add the associated file descriptor to the Looper for polling whenever there is data to |
| 797 | // be injected. |
| 798 | result->updateLooperRegistration(mLooper); |
| 799 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 800 | return result; |
| 801 | } |
| 802 | |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 803 | int SensorService::isDataInjectionEnabled() { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 804 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 805 | return (mCurrentOperatingMode == DATA_INJECTION); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | status_t SensorService::resetToNormalMode() { |
| 809 | Mutex::Autolock _l(mLock); |
| 810 | return resetToNormalModeLocked(); |
| 811 | } |
| 812 | |
| 813 | status_t SensorService::resetToNormalModeLocked() { |
| 814 | SensorDevice& dev(SensorDevice::getInstance()); |
| 815 | dev.enableAllSensors(); |
| 816 | status_t err = dev.setMode(NORMAL); |
| 817 | mCurrentOperatingMode = NORMAL; |
| 818 | return err; |
| 819 | } |
| 820 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 821 | void SensorService::cleanupConnection(SensorEventConnection* c) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 822 | { |
| 823 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 824 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 825 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 826 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 827 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 828 | int handle = mActiveSensors.keyAt(i); |
| 829 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 830 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 831 | SensorInterface* sensor = mSensorMap.valueFor( handle ); |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 832 | ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 833 | if (sensor) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 834 | sensor->activate(c, false); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 835 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 836 | c->removeSensor(handle); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 837 | } |
| 838 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 839 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 840 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 841 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 842 | c, i, handle); |
| 843 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 844 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 845 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 846 | mActiveSensors.removeItemsAt(i, 1); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 847 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 848 | delete rec; |
| 849 | size--; |
| 850 | } else { |
| 851 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 852 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 853 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 854 | c->updateLooperRegistration(mLooper); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 855 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 856 | BatteryService::cleanup(c->getUid()); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 857 | if (c->needsWakeLock()) { |
| 858 | checkWakeLockStateLocked(); |
| 859 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 862 | Sensor SensorService::getSensorFromHandle(int handle) const { |
| 863 | return mSensorMap.valueFor(handle)->getSensor(); |
| 864 | } |
| 865 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 866 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 867 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, |
| 868 | const String16& opPackageName) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 869 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 870 | if (mInitCheck != NO_ERROR) |
| 871 | return mInitCheck; |
| 872 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 873 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 874 | if (sensor == NULL) { |
| 875 | return BAD_VALUE; |
| 876 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 877 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 878 | if (!canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 879 | return BAD_VALUE; |
| 880 | } |
| 881 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 882 | Mutex::Autolock _l(mLock); |
Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 883 | if ((mCurrentOperatingMode == RESTRICTED || mCurrentOperatingMode == DATA_INJECTION) |
| 884 | && !isWhiteListedPackage(connection->getPackageName())) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 885 | return INVALID_OPERATION; |
| 886 | } |
| 887 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 888 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 889 | if (rec == 0) { |
| 890 | rec = new SensorRecord(connection); |
| 891 | mActiveSensors.add(handle, rec); |
| 892 | if (sensor->isVirtual()) { |
| 893 | mActiveVirtualSensors.add(handle, sensor); |
| 894 | } |
| 895 | } else { |
| 896 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 897 | // this sensor is already activated, but we are adding a connection that uses it. |
| 898 | // 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] | 899 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 900 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 901 | // NOTE: The wake_up flag of this event may get set to |
| 902 | // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event. |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 903 | CircularBuffer *circular_buf = mLastEventSeen.valueFor(handle); |
| 904 | if (circular_buf) { |
| 905 | sensors_event_t event; |
| 906 | memset(&event, 0, sizeof(event)); |
| 907 | // It is unlikely that this buffer is empty as the sensor is already active. |
| 908 | // One possible corner case may be two applications activating an on-change |
| 909 | // sensor at the same time. |
| 910 | if(circular_buf->populateLastEvent(&event)) { |
| 911 | event.sensor = handle; |
| 912 | if (event.version == sizeof(sensors_event_t)) { |
| 913 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 914 | setWakeLockAcquiredLocked(true); |
| 915 | } |
| 916 | connection->sendEvents(&event, 1, NULL); |
| 917 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
| 918 | checkWakeLockStateLocked(); |
| 919 | } |
| 920 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 921 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (connection->addSensor(handle)) { |
| 928 | BatteryService::enableSensor(connection->getUid(), handle); |
| 929 | // the sensor was added (which means it wasn't already there) |
| 930 | // so, see if this connection becomes active |
| 931 | if (mActiveConnections.indexOf(connection) < 0) { |
| 932 | mActiveConnections.add(connection); |
| 933 | } |
| 934 | } else { |
| 935 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 936 | handle, connection.get()); |
| 937 | } |
| 938 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 939 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 940 | if (samplingPeriodNs < minDelayNs) { |
| 941 | samplingPeriodNs = minDelayNs; |
| 942 | } |
| 943 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 944 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" |
| 945 | "rate=%" PRId64 " timeout== %" PRId64"", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 946 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 947 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 948 | status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 949 | maxBatchReportLatencyNs); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 950 | |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 951 | // Call flush() before calling activate() on the sensor. Wait for a first flush complete |
| 952 | // event before sending events on this connection. Ignore one-shot sensors which don't |
| 953 | // support flush(). Also if this sensor isn't already active, don't call flush(). |
| 954 | if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT && |
| 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 | // --------------------------------------------------------------------------- |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1223 | SensorService::SensorRecord::SensorRecord( |
| 1224 | const sp<SensorEventConnection>& connection) |
| 1225 | { |
| 1226 | mConnections.add(connection); |
| 1227 | } |
| 1228 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1229 | bool SensorService::SensorRecord::addConnection( |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1230 | const sp<SensorEventConnection>& connection) |
| 1231 | { |
| 1232 | if (mConnections.indexOf(connection) < 0) { |
| 1233 | mConnections.add(connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1234 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1235 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1236 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | bool SensorService::SensorRecord::removeConnection( |
| 1240 | const wp<SensorEventConnection>& connection) |
| 1241 | { |
| 1242 | ssize_t index = mConnections.indexOf(connection); |
| 1243 | if (index >= 0) { |
| 1244 | mConnections.removeItemsAt(index, 1); |
| 1245 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1246 | // Remove this connections from the queue of flush() calls made on this sensor. |
| 1247 | for (Vector< wp<SensorEventConnection> >::iterator it = |
| 1248 | mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1249 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1250 | if (it->unsafe_get() == connection.unsafe_get()) { |
| 1251 | it = mPendingFlushConnections.erase(it); |
| 1252 | } else { |
| 1253 | ++it; |
| 1254 | } |
| 1255 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1256 | return mConnections.size() ? false : true; |
| 1257 | } |
| 1258 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1259 | void SensorService::SensorRecord::addPendingFlushConnection( |
| 1260 | const sp<SensorEventConnection>& connection) { |
| 1261 | mPendingFlushConnections.add(connection); |
| 1262 | } |
| 1263 | |
| 1264 | void SensorService::SensorRecord::removeFirstPendingFlushConnection() { |
| 1265 | if (mPendingFlushConnections.size() > 0) { |
| 1266 | mPendingFlushConnections.removeAt(0); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | SensorService::SensorEventConnection * |
| 1271 | SensorService::SensorRecord::getFirstPendingFlushConnection() { |
| 1272 | if (mPendingFlushConnections.size() > 0) { |
| 1273 | return mPendingFlushConnections[0].unsafe_get(); |
| 1274 | } |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1278 | void SensorService::SensorRecord::clearAllPendingFlushConnections() { |
| 1279 | mPendingFlushConnections.clear(); |
| 1280 | } |
| 1281 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1282 | |
| 1283 | // --------------------------------------------------------------------------- |
| 1284 | SensorService::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) { |
| 1285 | mTimestamp = -1; |
| 1286 | const int numData = SensorService::getNumEventsForSensorType(sensorType); |
| 1287 | if (sensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1288 | mStepCounter = 0; |
| 1289 | } else { |
| 1290 | mData = new float[numData]; |
| 1291 | for (int i = 0; i < numData; ++i) { |
| 1292 | mData[i] = -1.0; |
| 1293 | } |
| 1294 | } |
| 1295 | mHour = mMin = mSec = INT32_MIN; |
| 1296 | } |
| 1297 | |
| 1298 | bool SensorService::TrimmedSensorEvent::isSentinel(const TrimmedSensorEvent& event) { |
| 1299 | return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN); |
| 1300 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1301 | // -------------------------------------------------------------------------- |
| 1302 | SensorService::CircularBuffer::CircularBuffer(int sensor_event_type) { |
| 1303 | mNextInd = 0; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1304 | mBufSize = CIRCULAR_BUF_SIZE; |
| 1305 | if (sensor_event_type == SENSOR_TYPE_STEP_COUNTER || |
| 1306 | sensor_event_type == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 1307 | sensor_event_type == SENSOR_TYPE_ACCELEROMETER) { |
| 1308 | mBufSize = CIRCULAR_BUF_SIZE * 5; |
| 1309 | } |
| 1310 | mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize]; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1311 | mSensorType = sensor_event_type; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1312 | for (int i = 0; i < mBufSize; ++i) { |
| 1313 | mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | void SensorService::CircularBuffer::addEvent(const sensors_event_t& sensor_event) { |
| 1318 | TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd]; |
| 1319 | curr_event->mTimestamp = sensor_event.timestamp; |
| 1320 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1321 | curr_event->mStepCounter = sensor_event.u64.step_counter; |
| 1322 | } else { |
| 1323 | memcpy(curr_event->mData, sensor_event.data, |
| 1324 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); |
| 1325 | } |
| 1326 | time_t rawtime = time(NULL); |
| 1327 | struct tm * timeinfo = localtime(&rawtime); |
| 1328 | curr_event->mHour = timeinfo->tm_hour; |
| 1329 | curr_event->mMin = timeinfo->tm_min; |
| 1330 | curr_event->mSec = timeinfo->tm_sec; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1331 | mNextInd = (mNextInd + 1) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | void SensorService::CircularBuffer::printBuffer(String8& result) const { |
| 1335 | const int numData = SensorService::getNumEventsForSensorType(mSensorType); |
| 1336 | int i = mNextInd, eventNum = 1; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1337 | result.appendFormat("last %d events = < ", mBufSize); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1338 | do { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1339 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1340 | // Sentinel, ignore. |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1341 | i = (i + 1) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1342 | continue; |
| 1343 | } |
| 1344 | result.appendFormat("%d) ", eventNum++); |
| 1345 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 1346 | result.appendFormat("%" PRIu64 ",", mTrimmedSensorEventArr[i]->mStepCounter); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1347 | } else { |
| 1348 | for (int j = 0; j < numData; ++j) { |
| 1349 | result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]); |
| 1350 | } |
| 1351 | } |
Andreas Gampe | d4036b6 | 2015-07-28 13:49:04 -0700 | [diff] [blame] | 1352 | result.appendFormat("%" PRId64 " %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp, |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1353 | mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin, |
| 1354 | mTrimmedSensorEventArr[i]->mSec); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1355 | i = (i + 1) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1356 | } while (i != mNextInd); |
| 1357 | result.appendFormat(">\n"); |
| 1358 | } |
| 1359 | |
| 1360 | bool SensorService::CircularBuffer::populateLastEvent(sensors_event_t *event) { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1361 | int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1362 | // Check if the buffer is empty. |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1363 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1364 | return false; |
| 1365 | } |
| 1366 | event->version = sizeof(sensors_event_t); |
| 1367 | event->type = mSensorType; |
| 1368 | event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp; |
| 1369 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1370 | event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter; |
| 1371 | } else { |
| 1372 | memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData, |
| 1373 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); |
| 1374 | } |
| 1375 | return true; |
| 1376 | } |
| 1377 | |
| 1378 | SensorService::CircularBuffer::~CircularBuffer() { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1379 | for (int i = 0; i < mBufSize; ++i) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1380 | delete mTrimmedSensorEventArr[i]; |
| 1381 | } |
| 1382 | delete [] mTrimmedSensorEventArr; |
| 1383 | } |
| 1384 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1385 | // --------------------------------------------------------------------------- |
| 1386 | |
| 1387 | SensorService::SensorEventConnection::SensorEventConnection( |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1388 | const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode, |
| 1389 | const String16& opPackageName) |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1390 | : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false), |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1391 | mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL), |
| 1392 | mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1393 | mChannel = new BitTube(mService->mSocketBufferSize); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1394 | #if DEBUG_CONNECTIONS |
| 1395 | mEventsReceived = mEventsSentFromCache = mEventsSent = 0; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1396 | mTotalAcksNeeded = mTotalAcksReceived = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1397 | #endif |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1400 | SensorService::SensorEventConnection::~SensorEventConnection() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1401 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 1402 | mService->cleanupConnection(this); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1403 | if (mEventCache != NULL) { |
| 1404 | delete mEventCache; |
| 1405 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1408 | void SensorService::SensorEventConnection::onFirstRef() { |
| 1409 | LooperCallback::onFirstRef(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1412 | bool SensorService::SensorEventConnection::needsWakeLock() { |
| 1413 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1414 | return !mDead && mWakeLockRefCount > 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1417 | void SensorService::SensorEventConnection::resetWakeLockRefCount() { |
| 1418 | Mutex::Autolock _l(mConnectionLock); |
| 1419 | mWakeLockRefCount = 0; |
| 1420 | } |
| 1421 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1422 | void SensorService::SensorEventConnection::dump(String8& result) { |
| 1423 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 8ef3c89 | 2015-07-10 11:24:28 -0700 | [diff] [blame] | 1424 | result.appendFormat("\tOperating Mode: %s\n",mDataInjectionMode ? "DATA_INJECTION" : "NORMAL"); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1425 | result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | " |
| 1426 | "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize, |
| 1427 | mMaxCacheSize); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1428 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1429 | const FlushInfo& flushInfo = mSensorInfo.valueAt(i); |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1430 | result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n", |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1431 | mService->getSensorName(mSensorInfo.keyAt(i)).string(), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1432 | mSensorInfo.keyAt(i), |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1433 | flushInfo.mFirstFlushPending ? "First flush pending" : |
| 1434 | "active", |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1435 | flushInfo.mPendingFlushEventsToSend); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1436 | } |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1437 | #if DEBUG_CONNECTIONS |
| 1438 | result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |" |
| 1439 | " total_acks_needed %d | total_acks_recvd %d\n", |
| 1440 | mEventsReceived, |
| 1441 | mEventsSent, |
| 1442 | mEventsSentFromCache, |
| 1443 | mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize), |
| 1444 | mTotalAcksNeeded, |
| 1445 | mTotalAcksReceived); |
| 1446 | #endif |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1449 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1450 | Mutex::Autolock _l(mConnectionLock); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1451 | if (!canAccessSensor(mService->getSensorFromHandle(handle), |
| 1452 | "Tried adding", mOpPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1453 | return false; |
| 1454 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1455 | if (mSensorInfo.indexOfKey(handle) < 0) { |
| 1456 | mSensorInfo.add(handle, FlushInfo()); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1457 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1458 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1459 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1462 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1463 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1464 | if (mSensorInfo.removeItem(handle) >= 0) { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1465 | return true; |
| 1466 | } |
| 1467 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1471 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1472 | return mSensorInfo.indexOfKey(handle) >= 0; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | bool SensorService::SensorEventConnection::hasAnySensor() const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1476 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1477 | return mSensorInfo.size() ? true : false; |
| 1478 | } |
| 1479 | |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1480 | bool SensorService::SensorEventConnection::hasOneShotSensors() const { |
| 1481 | Mutex::Autolock _l(mConnectionLock); |
| 1482 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1483 | const int handle = mSensorInfo.keyAt(i); |
| 1484 | if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 1485 | return true; |
| 1486 | } |
| 1487 | } |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1491 | String8 SensorService::SensorEventConnection::getPackageName() const { |
| 1492 | return mPackageName; |
| 1493 | } |
| 1494 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1495 | void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, |
| 1496 | bool value) { |
| 1497 | Mutex::Autolock _l(mConnectionLock); |
| 1498 | ssize_t index = mSensorInfo.indexOfKey(handle); |
| 1499 | if (index >= 0) { |
| 1500 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
| 1501 | flushInfo.mFirstFlushPending = value; |
| 1502 | } |
| 1503 | } |
| 1504 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1505 | void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) { |
| 1506 | Mutex::Autolock _l(mConnectionLock); |
| 1507 | updateLooperRegistrationLocked(looper); |
| 1508 | } |
| 1509 | |
| 1510 | void SensorService::SensorEventConnection::updateLooperRegistrationLocked( |
| 1511 | const sp<Looper>& looper) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1512 | bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) || |
| 1513 | mDataInjectionMode; |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1514 | // If all sensors are unregistered OR Looper has encountered an error, we |
| 1515 | // can remove the Fd from the Looper if it has been previously added. |
| 1516 | if (!isConnectionActive || mDead) { |
| 1517 | if (mHasLooperCallbacks) { |
| 1518 | ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd()); |
| 1519 | looper->removeFd(mChannel->getSendFd()); |
| 1520 | mHasLooperCallbacks = false; |
| 1521 | } |
| 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | int looper_flags = 0; |
| 1526 | if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1527 | if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT; |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1528 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1529 | const int handle = mSensorInfo.keyAt(i); |
| 1530 | if (mService->getSensorFromHandle(handle).isWakeUpSensor()) { |
| 1531 | looper_flags |= ALOOPER_EVENT_INPUT; |
| 1532 | break; |
| 1533 | } |
| 1534 | } |
| 1535 | // If flags is still set to zero, we don't need to add this fd to the Looper, if |
| 1536 | // the fd has already been added, remove it. This is likely to happen when ALL the |
| 1537 | // events stored in the cache have been sent to the corresponding app. |
| 1538 | if (looper_flags == 0) { |
| 1539 | if (mHasLooperCallbacks) { |
| 1540 | ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd()); |
| 1541 | looper->removeFd(mChannel->getSendFd()); |
| 1542 | mHasLooperCallbacks = false; |
| 1543 | } |
| 1544 | return; |
| 1545 | } |
| 1546 | // Add the file descriptor to the Looper for receiving acknowledegments if the app has |
| 1547 | // registered for wake-up sensors OR for sending events in the cache. |
| 1548 | int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL); |
| 1549 | if (ret == 1) { |
| 1550 | ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd()); |
| 1551 | mHasLooperCallbacks = true; |
| 1552 | } else { |
| 1553 | ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd()); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) { |
| 1558 | Mutex::Autolock _l(mConnectionLock); |
| 1559 | ssize_t index = mSensorInfo.indexOfKey(handle); |
| 1560 | if (index >= 0) { |
| 1561 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
| 1562 | flushInfo.mPendingFlushEventsToSend++; |
| 1563 | } |
| 1564 | } |
| 1565 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1566 | status_t SensorService::SensorEventConnection::sendEvents( |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1567 | sensors_event_t const* buffer, size_t numEvents, |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1568 | sensors_event_t* scratch, |
| 1569 | SensorEventConnection const * const * mapFlushEventsToConnections) { |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1570 | // filter out events not for this connection |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1571 | int count = 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1572 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1573 | if (scratch) { |
| 1574 | size_t i=0; |
| 1575 | while (i<numEvents) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1576 | int32_t sensor_handle = buffer[i].sensor; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1577 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 1578 | ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1579 | buffer[i].meta_data.sensor); |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1580 | // Setting sensor_handle to the correct sensor to ensure the sensor events per |
| 1581 | // connection are filtered correctly. buffer[i].sensor is zero for meta_data |
| 1582 | // events. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1583 | sensor_handle = buffer[i].meta_data.sensor; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1584 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1585 | ssize_t index = mSensorInfo.indexOfKey(sensor_handle); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1586 | // Check if this connection has registered for this sensor. If not continue to the |
| 1587 | // next sensor_event. |
| 1588 | if (index < 0) { |
| 1589 | ++i; |
| 1590 | continue; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1591 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1592 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1593 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1594 | // Check if there is a pending flush_complete event for this sensor on this connection. |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1595 | if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true && |
| 1596 | this == mapFlushEventsToConnections[i]) { |
| 1597 | flushInfo.mFirstFlushPending = false; |
| 1598 | ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", |
| 1599 | buffer[i].meta_data.sensor); |
| 1600 | ++i; |
| 1601 | continue; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | // If there is a pending flush complete event for this sensor on this connection, |
| 1605 | // ignore the event and proceed to the next. |
| 1606 | if (flushInfo.mFirstFlushPending) { |
| 1607 | ++i; |
| 1608 | continue; |
| 1609 | } |
| 1610 | |
| 1611 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1612 | // Keep copying events into the scratch buffer as long as they are regular |
| 1613 | // sensor_events are from the same sensor_handle OR they are flush_complete_events |
| 1614 | // from the same sensor_handle AND the current connection is mapped to the |
| 1615 | // corresponding flush_complete_event. |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1616 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1617 | if (this == mapFlushEventsToConnections[i]) { |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1618 | scratch[count++] = buffer[i]; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1619 | } |
| 1620 | ++i; |
| 1621 | } else { |
| 1622 | // Regular sensor event, just copy it to the scratch buffer. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1623 | scratch[count++] = buffer[i++]; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1624 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1625 | } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle && |
| 1626 | buffer[i].type != SENSOR_TYPE_META_DATA) || |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1627 | (buffer[i].type == SENSOR_TYPE_META_DATA && |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1628 | buffer[i].meta_data.sensor == sensor_handle))); |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1629 | } |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1630 | } else { |
| 1631 | scratch = const_cast<sensors_event_t *>(buffer); |
| 1632 | count = numEvents; |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1633 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1634 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1635 | sendPendingFlushEventsLocked(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1636 | // Early return if there are no events for this connection. |
| 1637 | if (count == 0) { |
| 1638 | return status_t(NO_ERROR); |
| 1639 | } |
| 1640 | |
| 1641 | #if DEBUG_CONNECTIONS |
| 1642 | mEventsReceived += count; |
| 1643 | #endif |
| 1644 | if (mCacheSize != 0) { |
| 1645 | // There are some events in the cache which need to be sent first. Copy this buffer to |
| 1646 | // the end of cache. |
| 1647 | if (mCacheSize + count <= mMaxCacheSize) { |
| 1648 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1649 | mCacheSize += count; |
| 1650 | } else { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1651 | // Check if any new sensors have registered on this connection which may have increased |
| 1652 | // the max cache size that is desired. |
| 1653 | if (mCacheSize + count < computeMaxCacheSizeLocked()) { |
| 1654 | reAllocateCacheLocked(scratch, count); |
| 1655 | return status_t(NO_ERROR); |
| 1656 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1657 | // Some events need to be dropped. |
| 1658 | int remaningCacheSize = mMaxCacheSize - mCacheSize; |
| 1659 | if (remaningCacheSize != 0) { |
| 1660 | memcpy(&mEventCache[mCacheSize], scratch, |
| 1661 | remaningCacheSize * sizeof(sensors_event_t)); |
| 1662 | } |
| 1663 | int numEventsDropped = count - remaningCacheSize; |
| 1664 | countFlushCompleteEventsLocked(mEventCache, numEventsDropped); |
| 1665 | // Drop the first "numEventsDropped" in the cache. |
| 1666 | memmove(mEventCache, &mEventCache[numEventsDropped], |
| 1667 | (mCacheSize - numEventsDropped) * sizeof(sensors_event_t)); |
| 1668 | |
| 1669 | // Copy the remainingEvents in scratch buffer to the end of cache. |
| 1670 | memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize, |
| 1671 | numEventsDropped * sizeof(sensors_event_t)); |
| 1672 | } |
| 1673 | return status_t(NO_ERROR); |
| 1674 | } |
| 1675 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1676 | int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); |
| 1677 | if (index_wake_up_event >= 0) { |
| 1678 | scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1679 | ++mWakeLockRefCount; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1680 | #if DEBUG_CONNECTIONS |
| 1681 | ++mTotalAcksNeeded; |
| 1682 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1683 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1684 | |
| 1685 | // NOTE: ASensorEvent and sensors_event_t are the same type. |
| 1686 | ssize_t size = SensorEventQueue::write(mChannel, |
| 1687 | reinterpret_cast<ASensorEvent const*>(scratch), count); |
| 1688 | if (size < 0) { |
| 1689 | // Write error, copy events to local cache. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1690 | if (index_wake_up_event >= 0) { |
| 1691 | // If there was a wake_up sensor_event, reset the flag. |
| 1692 | scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1693 | if (mWakeLockRefCount > 0) { |
| 1694 | --mWakeLockRefCount; |
| 1695 | } |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1696 | #if DEBUG_CONNECTIONS |
| 1697 | --mTotalAcksNeeded; |
| 1698 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1699 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1700 | if (mEventCache == NULL) { |
| 1701 | mMaxCacheSize = computeMaxCacheSizeLocked(); |
| 1702 | mEventCache = new sensors_event_t[mMaxCacheSize]; |
| 1703 | mCacheSize = 0; |
| 1704 | } |
| 1705 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1706 | mCacheSize += count; |
| 1707 | |
| 1708 | // Add this file descriptor to the looper to get a callback when this fd is available for |
| 1709 | // writing. |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1710 | updateLooperRegistrationLocked(mService->getLooper()); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1711 | return size; |
| 1712 | } |
| 1713 | |
| 1714 | #if DEBUG_CONNECTIONS |
| 1715 | if (size > 0) { |
| 1716 | mEventsSent += count; |
| 1717 | } |
| 1718 | #endif |
| 1719 | |
| 1720 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 1721 | } |
| 1722 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1723 | void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, |
| 1724 | int count) { |
| 1725 | sensors_event_t *eventCache_new; |
| 1726 | const int new_cache_size = computeMaxCacheSizeLocked(); |
| 1727 | // Allocate new cache, copy over events from the old cache & scratch, free up memory. |
| 1728 | eventCache_new = new sensors_event_t[new_cache_size]; |
| 1729 | memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); |
| 1730 | memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1731 | |
| 1732 | ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, |
| 1733 | new_cache_size); |
| 1734 | |
| 1735 | delete mEventCache; |
| 1736 | mEventCache = eventCache_new; |
| 1737 | mCacheSize += count; |
| 1738 | mMaxCacheSize = new_cache_size; |
| 1739 | } |
| 1740 | |
| 1741 | void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { |
| 1742 | ASensorEvent flushCompleteEvent; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1743 | memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent)); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1744 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1745 | // Loop through all the sensors for this connection and check if there are any pending |
| 1746 | // flush complete events to be sent. |
| 1747 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1748 | FlushInfo& flushInfo = mSensorInfo.editValueAt(i); |
| 1749 | while (flushInfo.mPendingFlushEventsToSend > 0) { |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1750 | const int sensor_handle = mSensorInfo.keyAt(i); |
| 1751 | flushCompleteEvent.meta_data.sensor = sensor_handle; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1752 | bool wakeUpSensor = mService->getSensorFromHandle(sensor_handle).isWakeUpSensor(); |
| 1753 | if (wakeUpSensor) { |
| 1754 | ++mWakeLockRefCount; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1755 | flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1756 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1757 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); |
| 1758 | if (size < 0) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1759 | if (wakeUpSensor) --mWakeLockRefCount; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1760 | return; |
| 1761 | } |
| 1762 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", |
| 1763 | flushCompleteEvent.meta_data.sensor); |
| 1764 | flushInfo.mPendingFlushEventsToSend--; |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1769 | void SensorService::SensorEventConnection::writeToSocketFromCache() { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1770 | // At a time write at most half the size of the receiver buffer in SensorEventQueue OR |
| 1771 | // half the size of the socket buffer allocated in BitTube whichever is smaller. |
| 1772 | const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2, |
| 1773 | int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2))); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1774 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1775 | // Send pending flush complete events (if any) |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1776 | sendPendingFlushEventsLocked(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1777 | for (int numEventsSent = 0; numEventsSent < mCacheSize;) { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1778 | const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1779 | int index_wake_up_event = |
| 1780 | findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); |
| 1781 | if (index_wake_up_event >= 0) { |
| 1782 | mEventCache[index_wake_up_event + numEventsSent].flags |= |
| 1783 | WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1784 | ++mWakeLockRefCount; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1785 | #if DEBUG_CONNECTIONS |
| 1786 | ++mTotalAcksNeeded; |
| 1787 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1788 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1789 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1790 | ssize_t size = SensorEventQueue::write(mChannel, |
| 1791 | reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1792 | numEventsToWrite); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1793 | if (size < 0) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1794 | if (index_wake_up_event >= 0) { |
| 1795 | // If there was a wake_up sensor_event, reset the flag. |
| 1796 | mEventCache[index_wake_up_event + numEventsSent].flags &= |
| 1797 | ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1798 | if (mWakeLockRefCount > 0) { |
| 1799 | --mWakeLockRefCount; |
| 1800 | } |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1801 | #if DEBUG_CONNECTIONS |
| 1802 | --mTotalAcksNeeded; |
| 1803 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1804 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1805 | memmove(mEventCache, &mEventCache[numEventsSent], |
| 1806 | (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); |
| 1807 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1808 | numEventsSent, mCacheSize); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1809 | mCacheSize -= numEventsSent; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1810 | return; |
| 1811 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1812 | numEventsSent += numEventsToWrite; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1813 | #if DEBUG_CONNECTIONS |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1814 | mEventsSentFromCache += numEventsToWrite; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1815 | #endif |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1816 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1817 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); |
| 1818 | // All events from the cache have been sent. Reset cache size to zero. |
| 1819 | mCacheSize = 0; |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1820 | // There are no more events in the cache. We don't need to poll for write on the fd. |
| 1821 | // Update Looper registration. |
| 1822 | updateLooperRegistrationLocked(mService->getLooper()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1825 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1826 | sensors_event_t const* scratch, const int numEventsDropped) { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1827 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1828 | // Count flushComplete events in the events that are about to the dropped. These will be sent |
| 1829 | // separately before the next batch of events. |
| 1830 | for (int j = 0; j < numEventsDropped; ++j) { |
| 1831 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { |
| 1832 | FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor); |
| 1833 | flushInfo.mPendingFlushEventsToSend++; |
| 1834 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", |
| 1835 | flushInfo.mPendingFlushEventsToSend); |
| 1836 | } |
| 1837 | } |
| 1838 | return; |
| 1839 | } |
| 1840 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1841 | int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( |
| 1842 | sensors_event_t const* scratch, const int count) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1843 | for (int i = 0; i < count; ++i) { |
| 1844 | if (mService->isWakeUpSensorEvent(scratch[i])) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1845 | return i; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1846 | } |
| 1847 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1848 | return -1; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1849 | } |
| 1850 | |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 1851 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1852 | { |
| 1853 | return mChannel; |
| 1854 | } |
| 1855 | |
| 1856 | status_t SensorService::SensorEventConnection::enableDisable( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1857 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, |
| 1858 | int reservedFlags) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1859 | { |
| 1860 | status_t err; |
| 1861 | if (enabled) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1862 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1863 | reservedFlags, mOpPackageName); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1864 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1865 | } else { |
| 1866 | err = mService->disable(this, handle); |
| 1867 | } |
| 1868 | return err; |
| 1869 | } |
| 1870 | |
| 1871 | status_t SensorService::SensorEventConnection::setEventRate( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1872 | int handle, nsecs_t samplingPeriodNs) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1873 | { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1874 | return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1877 | status_t SensorService::SensorEventConnection::flush() { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1878 | return mService->flushSensor(this, mOpPackageName); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1879 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1880 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1881 | int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) { |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1882 | if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1883 | { |
| 1884 | // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount, |
| 1885 | // and remove the fd from Looper. Call checkWakeLockState to know if SensorService |
| 1886 | // can release the wake-lock. |
| 1887 | ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd); |
| 1888 | Mutex::Autolock _l(mConnectionLock); |
| 1889 | mDead = true; |
| 1890 | mWakeLockRefCount = 0; |
| 1891 | updateLooperRegistrationLocked(mService->getLooper()); |
| 1892 | } |
| 1893 | mService->checkWakeLockState(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1894 | if (mDataInjectionMode) { |
| 1895 | // If the Looper has encountered some error in data injection mode, reset SensorService |
| 1896 | // back to normal mode. |
| 1897 | mService->resetToNormalMode(); |
| 1898 | mDataInjectionMode = false; |
| 1899 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1900 | return 1; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | if (events & ALOOPER_EVENT_INPUT) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1904 | unsigned char buf[sizeof(sensors_event_t)]; |
| 1905 | ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1906 | { |
| 1907 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1908 | if (numBytesRead == sizeof(sensors_event_t)) { |
| 1909 | if (!mDataInjectionMode) { |
| 1910 | ALOGE("Data injected in normal mode, dropping event" |
| 1911 | "package=%s uid=%d", mPackageName.string(), mUid); |
| 1912 | // Unregister call backs. |
| 1913 | return 0; |
| 1914 | } |
| 1915 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1916 | sensors_event_t sensor_event; |
| 1917 | memset(&sensor_event, 0, sizeof(sensor_event)); |
| 1918 | memcpy(&sensor_event, buf, sizeof(sensors_event_t)); |
| 1919 | Sensor sensor = mService->getSensorFromHandle(sensor_event.sensor); |
| 1920 | sensor_event.type = sensor.getType(); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1921 | dev.injectSensorData(&sensor_event); |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1922 | #if DEBUG_CONNECTIONS |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1923 | ++mEventsReceived; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1924 | #endif |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1925 | } else if (numBytesRead == sizeof(uint32_t)) { |
| 1926 | uint32_t numAcks = 0; |
Aravind Akella | 08f04bf | 2015-05-08 15:59:23 -0700 | [diff] [blame] | 1927 | memcpy(&numAcks, buf, numBytesRead); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1928 | // Sanity check to ensure there are no read errors in recv, numAcks is always |
| 1929 | // within the range and not zero. If any of the above don't hold reset |
| 1930 | // mWakeLockRefCount to zero. |
| 1931 | if (numAcks > 0 && numAcks < mWakeLockRefCount) { |
| 1932 | mWakeLockRefCount -= numAcks; |
| 1933 | } else { |
| 1934 | mWakeLockRefCount = 0; |
| 1935 | } |
| 1936 | #if DEBUG_CONNECTIONS |
| 1937 | mTotalAcksReceived += numAcks; |
| 1938 | #endif |
| 1939 | } else { |
| 1940 | // Read error, reset wakelock refcount. |
| 1941 | mWakeLockRefCount = 0; |
| 1942 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1943 | } |
| 1944 | // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released |
| 1945 | // here as checkWakeLockState() will need it. |
| 1946 | if (mWakeLockRefCount == 0) { |
| 1947 | mService->checkWakeLockState(); |
| 1948 | } |
| 1949 | // continue getting callbacks. |
| 1950 | return 1; |
| 1951 | } |
| 1952 | |
| 1953 | if (events & ALOOPER_EVENT_OUTPUT) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1954 | // send sensor data that is stored in mEventCache for this connection. |
| 1955 | mService->sendEventsFromCache(this); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1956 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1957 | return 1; |
| 1958 | } |
| 1959 | |
| 1960 | int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1961 | size_t fifoWakeUpSensors = 0; |
| 1962 | size_t fifoNonWakeUpSensors = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1963 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1964 | const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i)); |
| 1965 | if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { |
| 1966 | // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and |
| 1967 | // non wake_up sensors. |
| 1968 | if (sensor.isWakeUpSensor()) { |
| 1969 | fifoWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 1970 | } else { |
| 1971 | fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 1972 | } |
| 1973 | } else { |
| 1974 | // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. |
| 1975 | if (sensor.isWakeUpSensor()) { |
| 1976 | fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 1977 | fifoWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 1978 | |
| 1979 | } else { |
| 1980 | fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 1981 | fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 1982 | |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { |
| 1987 | // It is extremely unlikely that there is a write failure in non batch mode. Return a cache |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1988 | // size that is equal to that of the batch mode. |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1989 | // ALOGW("Write failure in non-batch mode"); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1990 | return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1991 | } |
| 1992 | return fifoWakeUpSensors + fifoNonWakeUpSensors; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1993 | } |
| 1994 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1995 | // --------------------------------------------------------------------------- |
| 1996 | }; // namespace android |
| 1997 | |