| 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 | 33a004b | 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 | 33a004b | 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 | 33a004b | 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 | 33a004b | 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) | 
|  | 133 | const SensorFusion& fusion(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 | 33a004b | 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 | 33a004b | 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 | 33a004b | 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 |  | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 420 | result.appendFormat("Socket Buffer size = %d events\n", | 
|  | 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]; | 
| Peng Xu | b7beb52 | 2016-01-21 23:15:04 -0800 | [diff] [blame] | 762 | accessibleSensorList.add(sensor); | 
| Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 763 | } | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 764 | return accessibleSensorList; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 765 | } | 
|  | 766 |  | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 767 | sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 768 | int requestedMode, const String16& opPackageName) { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 769 | // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION. | 
|  | 770 | if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { | 
|  | 771 | return NULL; | 
|  | 772 | } | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 773 |  | 
|  | 774 | Mutex::Autolock _l(mLock); | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 775 | // To create a client in DATA_INJECTION mode to inject data, SensorService should already be | 
|  | 776 | // operating in DI mode. | 
|  | 777 | if (requestedMode == DATA_INJECTION) { | 
|  | 778 | if (mCurrentOperatingMode != DATA_INJECTION) return NULL; | 
|  | 779 | if (!isWhiteListedPackage(packageName)) return NULL; | 
|  | 780 | } | 
|  | 781 |  | 
| Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 782 | uid_t uid = IPCThreadState::self()->getCallingUid(); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 783 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 784 | requestedMode == DATA_INJECTION, opPackageName)); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 785 | if (requestedMode == DATA_INJECTION) { | 
|  | 786 | if (mActiveConnections.indexOf(result) < 0) { | 
|  | 787 | mActiveConnections.add(result); | 
|  | 788 | } | 
|  | 789 | // Add the associated file descriptor to the Looper for polling whenever there is data to | 
|  | 790 | // be injected. | 
|  | 791 | result->updateLooperRegistration(mLooper); | 
|  | 792 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 793 | return result; | 
|  | 794 | } | 
|  | 795 |  | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 796 | int SensorService::isDataInjectionEnabled() { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 797 | Mutex::Autolock _l(mLock); | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 798 | return (mCurrentOperatingMode == DATA_INJECTION); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 799 | } | 
|  | 800 |  | 
|  | 801 | status_t SensorService::resetToNormalMode() { | 
|  | 802 | Mutex::Autolock _l(mLock); | 
|  | 803 | return resetToNormalModeLocked(); | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | status_t SensorService::resetToNormalModeLocked() { | 
|  | 807 | SensorDevice& dev(SensorDevice::getInstance()); | 
|  | 808 | dev.enableAllSensors(); | 
|  | 809 | status_t err = dev.setMode(NORMAL); | 
|  | 810 | mCurrentOperatingMode = NORMAL; | 
|  | 811 | return err; | 
|  | 812 | } | 
|  | 813 |  | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 814 | void SensorService::cleanupConnection(SensorEventConnection* c) | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 815 | { | 
|  | 816 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 817 | const wp<SensorEventConnection> connection(c); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 818 | size_t size = mActiveSensors.size(); | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 819 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 820 | for (size_t i=0 ; i<size ; ) { | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 821 | int handle = mActiveSensors.keyAt(i); | 
|  | 822 | if (c->hasSensor(handle)) { | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 823 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 824 | SensorInterface* sensor = mSensorMap.valueFor( handle ); | 
| Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 825 | ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 826 | if (sensor) { | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 827 | sensor->activate(c, false); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 828 | } | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 829 | c->removeSensor(handle); | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 830 | } | 
|  | 831 | SensorRecord* rec = mActiveSensors.valueAt(i); | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 832 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 833 | ALOGD_IF(DEBUG_CONNECTIONS, | 
| Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 834 | "removing connection %p for sensor[%zu].handle=0x%08x", | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 835 | c, i, handle); | 
|  | 836 |  | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 837 | if (rec && rec->removeConnection(connection)) { | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 838 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 839 | mActiveSensors.removeItemsAt(i, 1); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 840 | mActiveVirtualSensors.removeItem(handle); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 841 | delete rec; | 
|  | 842 | size--; | 
|  | 843 | } else { | 
|  | 844 | i++; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 845 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 846 | } | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 847 | c->updateLooperRegistration(mLooper); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 848 | mActiveConnections.remove(connection); | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 849 | BatteryService::cleanup(c->getUid()); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 850 | if (c->needsWakeLock()) { | 
|  | 851 | checkWakeLockStateLocked(); | 
|  | 852 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 853 | } | 
|  | 854 |  | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 855 | Sensor SensorService::getSensorFromHandle(int handle) const { | 
|  | 856 | return mSensorMap.valueFor(handle)->getSensor(); | 
|  | 857 | } | 
|  | 858 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 859 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 860 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, | 
|  | 861 | const String16& opPackageName) | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 862 | { | 
| Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 863 | if (mInitCheck != NO_ERROR) | 
|  | 864 | return mInitCheck; | 
|  | 865 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 866 | SensorInterface* sensor = mSensorMap.valueFor(handle); | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 867 | if (sensor == NULL) { | 
|  | 868 | return BAD_VALUE; | 
|  | 869 | } | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 870 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 871 | if (!canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) { | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 872 | return BAD_VALUE; | 
|  | 873 | } | 
|  | 874 |  | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 875 | Mutex::Autolock _l(mLock); | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 876 | if ((mCurrentOperatingMode == RESTRICTED || mCurrentOperatingMode == DATA_INJECTION) | 
|  | 877 | && !isWhiteListedPackage(connection->getPackageName())) { | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 878 | return INVALID_OPERATION; | 
|  | 879 | } | 
|  | 880 |  | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 881 | SensorRecord* rec = mActiveSensors.valueFor(handle); | 
|  | 882 | if (rec == 0) { | 
|  | 883 | rec = new SensorRecord(connection); | 
|  | 884 | mActiveSensors.add(handle, rec); | 
|  | 885 | if (sensor->isVirtual()) { | 
|  | 886 | mActiveVirtualSensors.add(handle, sensor); | 
|  | 887 | } | 
|  | 888 | } else { | 
|  | 889 | if (rec->addConnection(connection)) { | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 890 | // this sensor is already activated, but we are adding a connection that uses it. | 
|  | 891 | // 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] | 892 | // "continuous" sensor. | 
| Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 893 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 894 | // NOTE: The wake_up flag of this event may get set to | 
|  | 895 | // 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] | 896 | CircularBuffer *circular_buf = mLastEventSeen.valueFor(handle); | 
|  | 897 | if (circular_buf) { | 
|  | 898 | sensors_event_t event; | 
|  | 899 | memset(&event, 0, sizeof(event)); | 
|  | 900 | // It is unlikely that this buffer is empty as the sensor is already active. | 
|  | 901 | // One possible corner case may be two applications activating an on-change | 
|  | 902 | // sensor at the same time. | 
|  | 903 | if(circular_buf->populateLastEvent(&event)) { | 
|  | 904 | event.sensor = handle; | 
|  | 905 | if (event.version == sizeof(sensors_event_t)) { | 
|  | 906 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { | 
|  | 907 | setWakeLockAcquiredLocked(true); | 
|  | 908 | } | 
|  | 909 | connection->sendEvents(&event, 1, NULL); | 
|  | 910 | if (!connection->needsWakeLock() && mWakeLockAcquired) { | 
|  | 911 | checkWakeLockStateLocked(); | 
|  | 912 | } | 
|  | 913 | } | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 914 | } | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 915 | } | 
|  | 916 | } | 
|  | 917 | } | 
|  | 918 | } | 
|  | 919 |  | 
|  | 920 | if (connection->addSensor(handle)) { | 
|  | 921 | BatteryService::enableSensor(connection->getUid(), handle); | 
|  | 922 | // the sensor was added (which means it wasn't already there) | 
|  | 923 | // so, see if this connection becomes active | 
|  | 924 | if (mActiveConnections.indexOf(connection) < 0) { | 
|  | 925 | mActiveConnections.add(connection); | 
|  | 926 | } | 
|  | 927 | } else { | 
|  | 928 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", | 
|  | 929 | handle, connection.get()); | 
|  | 930 | } | 
|  | 931 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 932 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); | 
|  | 933 | if (samplingPeriodNs < minDelayNs) { | 
|  | 934 | samplingPeriodNs = minDelayNs; | 
|  | 935 | } | 
|  | 936 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 937 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" | 
|  | 938 | "rate=%" PRId64 " timeout== %" PRId64"", | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 939 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); | 
|  | 940 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 941 | status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 942 | maxBatchReportLatencyNs); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 943 |  | 
| Peng Xu | 20483c4 | 2015-10-26 15:14:43 -0700 | [diff] [blame] | 944 | // Call flush() before calling activate() on the sensor. Wait for a first | 
|  | 945 | // flush complete event before sending events on this connection. Ignore | 
|  | 946 | // one-shot sensors which don't support flush(). Ignore on-change sensors | 
|  | 947 | // to maintain the on-change logic (any on-change events except the initial | 
|  | 948 | // one should be trigger by a change in value). Also if this sensor isn't | 
|  | 949 | // already active, don't call flush(). | 
|  | 950 | if (err == NO_ERROR && | 
|  | 951 | sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT && | 
|  | 952 | sensor->getSensor().getReportingMode() != AREPORTING_MODE_ON_CHANGE && | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 953 | rec->getNumConnections() > 1) { | 
|  | 954 | connection->setFirstFlushPending(handle, true); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 955 | status_t err_flush = sensor->flush(connection.get(), handle); | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 956 | // 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] | 957 | if (err_flush == NO_ERROR) { | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 958 | rec->addPendingFlushConnection(connection.get()); | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 959 | } else { | 
|  | 960 | connection->setFirstFlushPending(handle, false); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 961 | } | 
|  | 962 | } | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 963 |  | 
|  | 964 | if (err == NO_ERROR) { | 
|  | 965 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); | 
|  | 966 | err = sensor->activate(connection.get(), true); | 
|  | 967 | } | 
|  | 968 |  | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 969 | if (err == NO_ERROR) { | 
|  | 970 | connection->updateLooperRegistration(mLooper); | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 971 | SensorRegistrationInfo ®_info = | 
|  | 972 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); | 
|  | 973 | reg_info.mSensorHandle = handle; | 
|  | 974 | reg_info.mSamplingRateUs = samplingPeriodNs/1000; | 
|  | 975 | reg_info.mMaxReportLatencyUs = maxBatchReportLatencyNs/1000; | 
|  | 976 | reg_info.mActivated = true; | 
|  | 977 | reg_info.mPackageName = connection->getPackageName(); | 
|  | 978 | time_t rawtime = time(NULL); | 
|  | 979 | struct tm * timeinfo = localtime(&rawtime); | 
|  | 980 | reg_info.mHour = timeinfo->tm_hour; | 
|  | 981 | reg_info.mMin = timeinfo->tm_min; | 
|  | 982 | reg_info.mSec = timeinfo->tm_sec; | 
|  | 983 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 984 | } | 
|  | 985 |  | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 986 | if (err != NO_ERROR) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 987 | // batch/activate has failed, reset our state. | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 988 | cleanupWithoutDisableLocked(connection, handle); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 989 | } | 
|  | 990 | return err; | 
|  | 991 | } | 
|  | 992 |  | 
|  | 993 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, | 
|  | 994 | int handle) | 
|  | 995 | { | 
| Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 996 | if (mInitCheck != NO_ERROR) | 
|  | 997 | return mInitCheck; | 
|  | 998 |  | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 999 | Mutex::Autolock _l(mLock); | 
|  | 1000 | status_t err = cleanupWithoutDisableLocked(connection, handle); | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1001 | if (err == NO_ERROR) { | 
|  | 1002 | SensorInterface* sensor = mSensorMap.valueFor(handle); | 
|  | 1003 | err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1004 |  | 
|  | 1005 | } | 
|  | 1006 | if (err == NO_ERROR) { | 
|  | 1007 | SensorRegistrationInfo ®_info = | 
|  | 1008 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); | 
|  | 1009 | reg_info.mActivated = false; | 
|  | 1010 | reg_info.mPackageName= connection->getPackageName(); | 
|  | 1011 | reg_info.mSensorHandle = handle; | 
|  | 1012 | time_t rawtime = time(NULL); | 
|  | 1013 | struct tm * timeinfo = localtime(&rawtime); | 
|  | 1014 | reg_info.mHour = timeinfo->tm_hour; | 
|  | 1015 | reg_info.mMin = timeinfo->tm_min; | 
|  | 1016 | reg_info.mSec = timeinfo->tm_sec; | 
|  | 1017 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1018 | } | 
|  | 1019 | return err; | 
|  | 1020 | } | 
|  | 1021 |  | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1022 | status_t SensorService::cleanupWithoutDisable( | 
|  | 1023 | const sp<SensorEventConnection>& connection, int handle) { | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1024 | Mutex::Autolock _l(mLock); | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 1025 | return cleanupWithoutDisableLocked(connection, handle); | 
|  | 1026 | } | 
|  | 1027 |  | 
|  | 1028 | status_t SensorService::cleanupWithoutDisableLocked( | 
|  | 1029 | const sp<SensorEventConnection>& connection, int handle) { | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1030 | SensorRecord* rec = mActiveSensors.valueFor(handle); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1031 | if (rec) { | 
|  | 1032 | // see if this connection becomes inactive | 
| Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 1033 | if (connection->removeSensor(handle)) { | 
|  | 1034 | BatteryService::disableSensor(connection->getUid(), handle); | 
|  | 1035 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1036 | if (connection->hasAnySensor() == false) { | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1037 | connection->updateLooperRegistration(mLooper); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1038 | mActiveConnections.remove(connection); | 
|  | 1039 | } | 
|  | 1040 | // see if this sensor becomes inactive | 
|  | 1041 | if (rec->removeConnection(connection)) { | 
|  | 1042 | mActiveSensors.removeItem(handle); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1043 | mActiveVirtualSensors.removeItem(handle); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1044 | delete rec; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1045 | } | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1046 | return NO_ERROR; | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1047 | } | 
| Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1048 | return BAD_VALUE; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1049 | } | 
|  | 1050 |  | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1051 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1052 | int handle, nsecs_t ns, const String16& opPackageName) | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1053 | { | 
| Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1054 | if (mInitCheck != NO_ERROR) | 
|  | 1055 | return mInitCheck; | 
|  | 1056 |  | 
| Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 1057 | SensorInterface* sensor = mSensorMap.valueFor(handle); | 
|  | 1058 | if (!sensor) | 
|  | 1059 | return BAD_VALUE; | 
|  | 1060 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1061 | if (!canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) { | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1062 | return BAD_VALUE; | 
|  | 1063 | } | 
|  | 1064 |  | 
| Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1065 | if (ns < 0) | 
|  | 1066 | return BAD_VALUE; | 
|  | 1067 |  | 
| Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 1068 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); | 
|  | 1069 | if (ns < minDelayNs) { | 
|  | 1070 | ns = minDelayNs; | 
| Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 1071 | } | 
|  | 1072 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1073 | return sensor->setDelay(connection.get(), handle, ns); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1074 | } | 
|  | 1075 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1076 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, | 
|  | 1077 | const String16& opPackageName) { | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1078 | if (mInitCheck != NO_ERROR) return mInitCheck; | 
| Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1079 | SensorDevice& dev(SensorDevice::getInstance()); | 
|  | 1080 | const int halVersion = dev.getHalDeviceVersion(); | 
|  | 1081 | status_t err(NO_ERROR); | 
|  | 1082 | Mutex::Autolock _l(mLock); | 
|  | 1083 | // Loop through all sensors for this connection and call flush on each of them. | 
|  | 1084 | for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) { | 
|  | 1085 | const int handle = connection->mSensorInfo.keyAt(i); | 
|  | 1086 | SensorInterface* sensor = mSensorMap.valueFor(handle); | 
|  | 1087 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { | 
|  | 1088 | ALOGE("flush called on a one-shot sensor"); | 
|  | 1089 | err = INVALID_OPERATION; | 
|  | 1090 | continue; | 
|  | 1091 | } | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1092 | if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) { | 
| Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1093 | // For older devices just increment pending flush count which will send a trivial | 
|  | 1094 | // flush complete event. | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1095 | connection->incrementPendingFlushCount(handle); | 
| Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1096 | } else { | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1097 | if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) { | 
|  | 1098 | err = INVALID_OPERATION; | 
|  | 1099 | continue; | 
|  | 1100 | } | 
| Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1101 | status_t err_flush = sensor->flush(connection.get(), handle); | 
|  | 1102 | if (err_flush == NO_ERROR) { | 
|  | 1103 | SensorRecord* rec = mActiveSensors.valueFor(handle); | 
|  | 1104 | if (rec != NULL) rec->addPendingFlushConnection(connection); | 
|  | 1105 | } | 
|  | 1106 | err = (err_flush != NO_ERROR) ? err_flush : err; | 
|  | 1107 | } | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1108 | } | 
| Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1109 | return err; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1110 | } | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1111 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1112 | bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, | 
|  | 1113 | const String16& opPackageName) { | 
|  | 1114 | const String8& requiredPermission = sensor.getRequiredPermission(); | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1115 |  | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1116 | if (requiredPermission.length() <= 0) { | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1117 | return true; | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1118 | } | 
|  | 1119 |  | 
|  | 1120 | bool hasPermission = false; | 
|  | 1121 |  | 
|  | 1122 | // Runtime permissions can't use the cache as they may change. | 
|  | 1123 | if (sensor.isRequiredPermissionRuntime()) { | 
|  | 1124 | hasPermission = checkPermission(String16(requiredPermission), | 
|  | 1125 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1126 | } else { | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1127 | hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission)); | 
|  | 1128 | } | 
|  | 1129 |  | 
|  | 1130 | if (!hasPermission) { | 
|  | 1131 | ALOGE("%s a sensor (%s) without holding its required permission: %s", | 
|  | 1132 | operation, sensor.getName().string(), sensor.getRequiredPermission().string()); | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1133 | return false; | 
|  | 1134 | } | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1135 |  | 
|  | 1136 | const int32_t opCode = sensor.getRequiredAppOp(); | 
|  | 1137 | if (opCode >= 0) { | 
|  | 1138 | AppOpsManager appOps; | 
|  | 1139 | if (appOps.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName) | 
|  | 1140 | != AppOpsManager::MODE_ALLOWED) { | 
|  | 1141 | ALOGE("%s a sensor (%s) without enabled required app op: %D", | 
|  | 1142 | operation, sensor.getName().string(), opCode); | 
|  | 1143 | return false; | 
|  | 1144 | } | 
|  | 1145 | } | 
|  | 1146 |  | 
|  | 1147 | return true; | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1148 | } | 
|  | 1149 |  | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1150 | void SensorService::checkWakeLockState() { | 
|  | 1151 | Mutex::Autolock _l(mLock); | 
|  | 1152 | checkWakeLockStateLocked(); | 
|  | 1153 | } | 
|  | 1154 |  | 
|  | 1155 | void SensorService::checkWakeLockStateLocked() { | 
|  | 1156 | if (!mWakeLockAcquired) { | 
|  | 1157 | return; | 
|  | 1158 | } | 
|  | 1159 | bool releaseLock = true; | 
|  | 1160 | for (size_t i=0 ; i<mActiveConnections.size() ; i++) { | 
|  | 1161 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); | 
|  | 1162 | if (connection != 0) { | 
|  | 1163 | if (connection->needsWakeLock()) { | 
|  | 1164 | releaseLock = false; | 
|  | 1165 | break; | 
|  | 1166 | } | 
|  | 1167 | } | 
|  | 1168 | } | 
|  | 1169 | if (releaseLock) { | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1170 | setWakeLockAcquiredLocked(false); | 
|  | 1171 | } | 
|  | 1172 | } | 
|  | 1173 |  | 
|  | 1174 | void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) { | 
|  | 1175 | Mutex::Autolock _l(mLock); | 
|  | 1176 | connection->writeToSocketFromCache(); | 
|  | 1177 | if (connection->needsWakeLock()) { | 
|  | 1178 | setWakeLockAcquiredLocked(true); | 
|  | 1179 | } | 
|  | 1180 | } | 
|  | 1181 |  | 
|  | 1182 | void SensorService::populateActiveConnections( | 
|  | 1183 | SortedVector< sp<SensorEventConnection> >* activeConnections) { | 
|  | 1184 | Mutex::Autolock _l(mLock); | 
|  | 1185 | for (size_t i=0 ; i < mActiveConnections.size(); ++i) { | 
|  | 1186 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); | 
|  | 1187 | if (connection != 0) { | 
|  | 1188 | activeConnections->add(connection); | 
|  | 1189 | } | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1190 | } | 
|  | 1191 | } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1192 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1193 | bool SensorService::isWhiteListedPackage(const String8& packageName) { | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 1194 | return (packageName.contains(mWhiteListedPackage.string())); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1195 | } | 
|  | 1196 |  | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1197 | int SensorService::getNumEventsForSensorType(int sensor_event_type) { | 
| Nick Vaccaro | 95f1390 | 2016-03-01 15:49:08 -0800 | [diff] [blame] | 1198 | if (sensor_event_type >= SENSOR_TYPE_DEVICE_PRIVATE_BASE) { | 
|  | 1199 | return 16; | 
|  | 1200 | } | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1201 | switch (sensor_event_type) { | 
|  | 1202 | case SENSOR_TYPE_ROTATION_VECTOR: | 
|  | 1203 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: | 
|  | 1204 | return 5; | 
|  | 1205 |  | 
|  | 1206 | case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED: | 
|  | 1207 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: | 
|  | 1208 | return 6; | 
|  | 1209 |  | 
|  | 1210 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: | 
|  | 1211 | return 4; | 
|  | 1212 |  | 
|  | 1213 | case SENSOR_TYPE_SIGNIFICANT_MOTION: | 
|  | 1214 | case SENSOR_TYPE_STEP_DETECTOR: | 
|  | 1215 | case SENSOR_TYPE_STEP_COUNTER: | 
|  | 1216 | return 1; | 
|  | 1217 |  | 
|  | 1218 | default: | 
|  | 1219 | return 3; | 
|  | 1220 | } | 
|  | 1221 | } | 
|  | 1222 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1223 | // --------------------------------------------------------------------------- | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1224 | SensorService::SensorRecord::SensorRecord( | 
|  | 1225 | const sp<SensorEventConnection>& connection) | 
|  | 1226 | { | 
|  | 1227 | mConnections.add(connection); | 
|  | 1228 | } | 
|  | 1229 |  | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1230 | bool SensorService::SensorRecord::addConnection( | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1231 | const sp<SensorEventConnection>& connection) | 
|  | 1232 | { | 
|  | 1233 | if (mConnections.indexOf(connection) < 0) { | 
|  | 1234 | mConnections.add(connection); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1235 | return true; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1236 | } | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1237 | return false; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1238 | } | 
|  | 1239 |  | 
|  | 1240 | bool SensorService::SensorRecord::removeConnection( | 
|  | 1241 | const wp<SensorEventConnection>& connection) | 
|  | 1242 | { | 
|  | 1243 | ssize_t index = mConnections.indexOf(connection); | 
|  | 1244 | if (index >= 0) { | 
|  | 1245 | mConnections.removeItemsAt(index, 1); | 
|  | 1246 | } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1247 | // Remove this connections from the queue of flush() calls made on this sensor. | 
|  | 1248 | for (Vector< wp<SensorEventConnection> >::iterator it = | 
|  | 1249 | mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) { | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1250 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1251 | if (it->unsafe_get() == connection.unsafe_get()) { | 
|  | 1252 | it = mPendingFlushConnections.erase(it); | 
|  | 1253 | } else { | 
|  | 1254 | ++it; | 
|  | 1255 | } | 
|  | 1256 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1257 | return mConnections.size() ? false : true; | 
|  | 1258 | } | 
|  | 1259 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1260 | void SensorService::SensorRecord::addPendingFlushConnection( | 
|  | 1261 | const sp<SensorEventConnection>& connection) { | 
|  | 1262 | mPendingFlushConnections.add(connection); | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | void SensorService::SensorRecord::removeFirstPendingFlushConnection() { | 
|  | 1266 | if (mPendingFlushConnections.size() > 0) { | 
|  | 1267 | mPendingFlushConnections.removeAt(0); | 
|  | 1268 | } | 
|  | 1269 | } | 
|  | 1270 |  | 
|  | 1271 | SensorService::SensorEventConnection * | 
|  | 1272 | SensorService::SensorRecord::getFirstPendingFlushConnection() { | 
|  | 1273 | if (mPendingFlushConnections.size() > 0) { | 
|  | 1274 | return mPendingFlushConnections[0].unsafe_get(); | 
|  | 1275 | } | 
|  | 1276 | return NULL; | 
|  | 1277 | } | 
|  | 1278 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1279 | void SensorService::SensorRecord::clearAllPendingFlushConnections() { | 
|  | 1280 | mPendingFlushConnections.clear(); | 
|  | 1281 | } | 
|  | 1282 |  | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1283 |  | 
|  | 1284 | // --------------------------------------------------------------------------- | 
|  | 1285 | SensorService::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) { | 
|  | 1286 | mTimestamp = -1; | 
|  | 1287 | const int numData = SensorService::getNumEventsForSensorType(sensorType); | 
|  | 1288 | if (sensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 1289 | mStepCounter = 0; | 
|  | 1290 | } else { | 
|  | 1291 | mData = new float[numData]; | 
|  | 1292 | for (int i = 0; i < numData; ++i) { | 
|  | 1293 | mData[i] = -1.0; | 
|  | 1294 | } | 
|  | 1295 | } | 
|  | 1296 | mHour = mMin = mSec = INT32_MIN; | 
|  | 1297 | } | 
|  | 1298 |  | 
|  | 1299 | bool SensorService::TrimmedSensorEvent::isSentinel(const TrimmedSensorEvent& event) { | 
|  | 1300 | return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN); | 
|  | 1301 | } | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1302 | // -------------------------------------------------------------------------- | 
|  | 1303 | SensorService::CircularBuffer::CircularBuffer(int sensor_event_type) { | 
|  | 1304 | mNextInd = 0; | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1305 | mBufSize = CIRCULAR_BUF_SIZE; | 
|  | 1306 | if (sensor_event_type == SENSOR_TYPE_STEP_COUNTER || | 
|  | 1307 | sensor_event_type == SENSOR_TYPE_SIGNIFICANT_MOTION || | 
|  | 1308 | sensor_event_type == SENSOR_TYPE_ACCELEROMETER) { | 
|  | 1309 | mBufSize = CIRCULAR_BUF_SIZE * 5; | 
|  | 1310 | } | 
|  | 1311 | mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize]; | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1312 | mSensorType = sensor_event_type; | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1313 | for (int i = 0; i < mBufSize; ++i) { | 
|  | 1314 | mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType); | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1315 | } | 
|  | 1316 | } | 
|  | 1317 |  | 
|  | 1318 | void SensorService::CircularBuffer::addEvent(const sensors_event_t& sensor_event) { | 
|  | 1319 | TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd]; | 
|  | 1320 | curr_event->mTimestamp = sensor_event.timestamp; | 
|  | 1321 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 1322 | curr_event->mStepCounter = sensor_event.u64.step_counter; | 
|  | 1323 | } else { | 
|  | 1324 | memcpy(curr_event->mData, sensor_event.data, | 
|  | 1325 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); | 
|  | 1326 | } | 
|  | 1327 | time_t rawtime = time(NULL); | 
|  | 1328 | struct tm * timeinfo = localtime(&rawtime); | 
|  | 1329 | curr_event->mHour = timeinfo->tm_hour; | 
|  | 1330 | curr_event->mMin = timeinfo->tm_min; | 
|  | 1331 | curr_event->mSec = timeinfo->tm_sec; | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1332 | mNextInd = (mNextInd + 1) % mBufSize; | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1333 | } | 
|  | 1334 |  | 
|  | 1335 | void SensorService::CircularBuffer::printBuffer(String8& result) const { | 
|  | 1336 | const int numData = SensorService::getNumEventsForSensorType(mSensorType); | 
|  | 1337 | int i = mNextInd, eventNum = 1; | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1338 | result.appendFormat("last %d events = < ", mBufSize); | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1339 | do { | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1340 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) { | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1341 | // Sentinel, ignore. | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1342 | i = (i + 1) % mBufSize; | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1343 | continue; | 
|  | 1344 | } | 
|  | 1345 | result.appendFormat("%d) ", eventNum++); | 
|  | 1346 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 1347 | result.appendFormat("%llu,", mTrimmedSensorEventArr[i]->mStepCounter); | 
|  | 1348 | } else { | 
|  | 1349 | for (int j = 0; j < numData; ++j) { | 
|  | 1350 | result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]); | 
|  | 1351 | } | 
|  | 1352 | } | 
|  | 1353 | result.appendFormat("%lld %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp, | 
|  | 1354 | mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin, | 
|  | 1355 | mTrimmedSensorEventArr[i]->mSec); | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1356 | i = (i + 1) % mBufSize; | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1357 | } while (i != mNextInd); | 
|  | 1358 | result.appendFormat(">\n"); | 
|  | 1359 | } | 
|  | 1360 |  | 
|  | 1361 | bool SensorService::CircularBuffer::populateLastEvent(sensors_event_t *event) { | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1362 | int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize; | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1363 | // Check if the buffer is empty. | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1364 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) { | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1365 | return false; | 
|  | 1366 | } | 
|  | 1367 | event->version = sizeof(sensors_event_t); | 
|  | 1368 | event->type = mSensorType; | 
|  | 1369 | event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp; | 
|  | 1370 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { | 
|  | 1371 | event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter; | 
|  | 1372 | } else { | 
|  | 1373 | memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData, | 
|  | 1374 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); | 
|  | 1375 | } | 
|  | 1376 | return true; | 
|  | 1377 | } | 
|  | 1378 |  | 
|  | 1379 | SensorService::CircularBuffer::~CircularBuffer() { | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1380 | for (int i = 0; i < mBufSize; ++i) { | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1381 | delete mTrimmedSensorEventArr[i]; | 
|  | 1382 | } | 
|  | 1383 | delete [] mTrimmedSensorEventArr; | 
|  | 1384 | } | 
|  | 1385 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1386 | // --------------------------------------------------------------------------- | 
|  | 1387 |  | 
|  | 1388 | SensorService::SensorEventConnection::SensorEventConnection( | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1389 | const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode, | 
|  | 1390 | const String16& opPackageName) | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1391 | : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false), | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1392 | mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL), | 
|  | 1393 | mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) { | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1394 | mChannel = new BitTube(mService->mSocketBufferSize); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1395 | #if DEBUG_CONNECTIONS | 
|  | 1396 | mEventsReceived = mEventsSentFromCache = mEventsSent = 0; | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1397 | mTotalAcksNeeded = mTotalAcksReceived = 0; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1398 | #endif | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1399 | } | 
|  | 1400 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1401 | SensorService::SensorEventConnection::~SensorEventConnection() { | 
| Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1402 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); | 
| Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 1403 | mService->cleanupConnection(this); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1404 | if (mEventCache != NULL) { | 
|  | 1405 | delete mEventCache; | 
|  | 1406 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1407 | } | 
|  | 1408 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1409 | void SensorService::SensorEventConnection::onFirstRef() { | 
|  | 1410 | LooperCallback::onFirstRef(); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1411 | } | 
|  | 1412 |  | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1413 | bool SensorService::SensorEventConnection::needsWakeLock() { | 
|  | 1414 | Mutex::Autolock _l(mConnectionLock); | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1415 | return !mDead && mWakeLockRefCount > 0; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1416 | } | 
|  | 1417 |  | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1418 | void SensorService::SensorEventConnection::resetWakeLockRefCount() { | 
|  | 1419 | Mutex::Autolock _l(mConnectionLock); | 
|  | 1420 | mWakeLockRefCount = 0; | 
|  | 1421 | } | 
|  | 1422 |  | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1423 | void SensorService::SensorEventConnection::dump(String8& result) { | 
|  | 1424 | Mutex::Autolock _l(mConnectionLock); | 
| Aravind Akella | 8ef3c89 | 2015-07-10 11:24:28 -0700 | [diff] [blame] | 1425 | result.appendFormat("\tOperating Mode: %s\n",mDataInjectionMode ? "DATA_INJECTION" : "NORMAL"); | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1426 | result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | " | 
|  | 1427 | "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize, | 
|  | 1428 | mMaxCacheSize); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1429 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { | 
|  | 1430 | const FlushInfo& flushInfo = mSensorInfo.valueAt(i); | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1431 | 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] | 1432 | mService->getSensorName(mSensorInfo.keyAt(i)).string(), | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1433 | mSensorInfo.keyAt(i), | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1434 | flushInfo.mFirstFlushPending ? "First flush pending" : | 
|  | 1435 | "active", | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1436 | flushInfo.mPendingFlushEventsToSend); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1437 | } | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1438 | #if DEBUG_CONNECTIONS | 
|  | 1439 | result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |" | 
|  | 1440 | " total_acks_needed %d | total_acks_recvd %d\n", | 
|  | 1441 | mEventsReceived, | 
|  | 1442 | mEventsSent, | 
|  | 1443 | mEventsSentFromCache, | 
|  | 1444 | mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize), | 
|  | 1445 | mTotalAcksNeeded, | 
|  | 1446 | mTotalAcksReceived); | 
|  | 1447 | #endif | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1448 | } | 
|  | 1449 |  | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1450 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { | 
| Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1451 | Mutex::Autolock _l(mConnectionLock); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1452 | if (!canAccessSensor(mService->getSensorFromHandle(handle), | 
|  | 1453 | "Tried adding", mOpPackageName)) { | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1454 | return false; | 
|  | 1455 | } | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1456 | if (mSensorInfo.indexOfKey(handle) < 0) { | 
|  | 1457 | mSensorInfo.add(handle, FlushInfo()); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1458 | return true; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1459 | } | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1460 | return false; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1461 | } | 
|  | 1462 |  | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1463 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { | 
| Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1464 | Mutex::Autolock _l(mConnectionLock); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1465 | if (mSensorInfo.removeItem(handle) >= 0) { | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1466 | return true; | 
|  | 1467 | } | 
|  | 1468 | return false; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1469 | } | 
|  | 1470 |  | 
|  | 1471 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { | 
| Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1472 | Mutex::Autolock _l(mConnectionLock); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1473 | return mSensorInfo.indexOfKey(handle) >= 0; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1474 | } | 
|  | 1475 |  | 
|  | 1476 | bool SensorService::SensorEventConnection::hasAnySensor() const { | 
| Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1477 | Mutex::Autolock _l(mConnectionLock); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1478 | return mSensorInfo.size() ? true : false; | 
|  | 1479 | } | 
|  | 1480 |  | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1481 | bool SensorService::SensorEventConnection::hasOneShotSensors() const { | 
|  | 1482 | Mutex::Autolock _l(mConnectionLock); | 
|  | 1483 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { | 
|  | 1484 | const int handle = mSensorInfo.keyAt(i); | 
|  | 1485 | if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) { | 
|  | 1486 | return true; | 
|  | 1487 | } | 
|  | 1488 | } | 
|  | 1489 | return false; | 
|  | 1490 | } | 
|  | 1491 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1492 | String8 SensorService::SensorEventConnection::getPackageName() const { | 
|  | 1493 | return mPackageName; | 
|  | 1494 | } | 
|  | 1495 |  | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1496 | void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, | 
|  | 1497 | bool value) { | 
|  | 1498 | Mutex::Autolock _l(mConnectionLock); | 
|  | 1499 | ssize_t index = mSensorInfo.indexOfKey(handle); | 
|  | 1500 | if (index >= 0) { | 
|  | 1501 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); | 
|  | 1502 | flushInfo.mFirstFlushPending = value; | 
|  | 1503 | } | 
|  | 1504 | } | 
|  | 1505 |  | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1506 | void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) { | 
|  | 1507 | Mutex::Autolock _l(mConnectionLock); | 
|  | 1508 | updateLooperRegistrationLocked(looper); | 
|  | 1509 | } | 
|  | 1510 |  | 
|  | 1511 | void SensorService::SensorEventConnection::updateLooperRegistrationLocked( | 
|  | 1512 | const sp<Looper>& looper) { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1513 | bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) || | 
|  | 1514 | mDataInjectionMode; | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1515 | // If all sensors are unregistered OR Looper has encountered an error, we | 
|  | 1516 | // can remove the Fd from the Looper if it has been previously added. | 
|  | 1517 | if (!isConnectionActive || mDead) { | 
|  | 1518 | if (mHasLooperCallbacks) { | 
|  | 1519 | ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd()); | 
|  | 1520 | looper->removeFd(mChannel->getSendFd()); | 
|  | 1521 | mHasLooperCallbacks = false; | 
|  | 1522 | } | 
|  | 1523 | return; | 
|  | 1524 | } | 
|  | 1525 |  | 
|  | 1526 | int looper_flags = 0; | 
|  | 1527 | if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT; | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1528 | if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT; | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1529 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { | 
|  | 1530 | const int handle = mSensorInfo.keyAt(i); | 
|  | 1531 | if (mService->getSensorFromHandle(handle).isWakeUpSensor()) { | 
|  | 1532 | looper_flags |= ALOOPER_EVENT_INPUT; | 
|  | 1533 | break; | 
|  | 1534 | } | 
|  | 1535 | } | 
|  | 1536 | // If flags is still set to zero, we don't need to add this fd to the Looper, if | 
|  | 1537 | // the fd has already been added, remove it. This is likely to happen when ALL the | 
|  | 1538 | // events stored in the cache have been sent to the corresponding app. | 
|  | 1539 | if (looper_flags == 0) { | 
|  | 1540 | if (mHasLooperCallbacks) { | 
|  | 1541 | ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd()); | 
|  | 1542 | looper->removeFd(mChannel->getSendFd()); | 
|  | 1543 | mHasLooperCallbacks = false; | 
|  | 1544 | } | 
|  | 1545 | return; | 
|  | 1546 | } | 
|  | 1547 | // Add the file descriptor to the Looper for receiving acknowledegments if the app has | 
|  | 1548 | // registered for wake-up sensors OR for sending events in the cache. | 
|  | 1549 | int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL); | 
|  | 1550 | if (ret == 1) { | 
|  | 1551 | ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd()); | 
|  | 1552 | mHasLooperCallbacks = true; | 
|  | 1553 | } else { | 
|  | 1554 | ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd()); | 
|  | 1555 | } | 
|  | 1556 | } | 
|  | 1557 |  | 
|  | 1558 | void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) { | 
|  | 1559 | Mutex::Autolock _l(mConnectionLock); | 
|  | 1560 | ssize_t index = mSensorInfo.indexOfKey(handle); | 
|  | 1561 | if (index >= 0) { | 
|  | 1562 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); | 
|  | 1563 | flushInfo.mPendingFlushEventsToSend++; | 
|  | 1564 | } | 
|  | 1565 | } | 
|  | 1566 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1567 | status_t SensorService::SensorEventConnection::sendEvents( | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1568 | sensors_event_t const* buffer, size_t numEvents, | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1569 | sensors_event_t* scratch, | 
|  | 1570 | SensorEventConnection const * const * mapFlushEventsToConnections) { | 
| Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1571 | // filter out events not for this connection | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1572 | int count = 0; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1573 | Mutex::Autolock _l(mConnectionLock); | 
| Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1574 | if (scratch) { | 
|  | 1575 | size_t i=0; | 
|  | 1576 | while (i<numEvents) { | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1577 | int32_t sensor_handle = buffer[i].sensor; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1578 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { | 
|  | 1579 | ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", | 
| Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1580 | buffer[i].meta_data.sensor); | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1581 | // Setting sensor_handle to the correct sensor to ensure the sensor events per | 
|  | 1582 | // connection are filtered correctly.  buffer[i].sensor is zero for meta_data | 
|  | 1583 | // events. | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1584 | sensor_handle = buffer[i].meta_data.sensor; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1585 | } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1586 | ssize_t index = mSensorInfo.indexOfKey(sensor_handle); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1587 | // Check if this connection has registered for this sensor. If not continue to the | 
|  | 1588 | // next sensor_event. | 
|  | 1589 | if (index < 0) { | 
|  | 1590 | ++i; | 
|  | 1591 | continue; | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1592 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1593 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1594 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1595 | // 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] | 1596 | if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true && | 
|  | 1597 | this == mapFlushEventsToConnections[i]) { | 
|  | 1598 | flushInfo.mFirstFlushPending = false; | 
|  | 1599 | ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", | 
|  | 1600 | buffer[i].meta_data.sensor); | 
|  | 1601 | ++i; | 
|  | 1602 | continue; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1603 | } | 
|  | 1604 |  | 
|  | 1605 | // If there is a pending flush complete event for this sensor on this connection, | 
|  | 1606 | // ignore the event and proceed to the next. | 
|  | 1607 | if (flushInfo.mFirstFlushPending) { | 
|  | 1608 | ++i; | 
|  | 1609 | continue; | 
|  | 1610 | } | 
|  | 1611 |  | 
|  | 1612 | do { | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1613 | // Keep copying events into the scratch buffer as long as they are regular | 
|  | 1614 | // sensor_events are from the same sensor_handle OR they are flush_complete_events | 
|  | 1615 | // from the same sensor_handle AND the current connection is mapped to the | 
|  | 1616 | // corresponding flush_complete_event. | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1617 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1618 | if (this == mapFlushEventsToConnections[i]) { | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1619 | scratch[count++] = buffer[i]; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1620 | } | 
|  | 1621 | ++i; | 
|  | 1622 | } else { | 
|  | 1623 | // Regular sensor event, just copy it to the scratch buffer. | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1624 | scratch[count++] = buffer[i++]; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1625 | } | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1626 | } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle && | 
|  | 1627 | buffer[i].type != SENSOR_TYPE_META_DATA) || | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1628 | (buffer[i].type == SENSOR_TYPE_META_DATA  && | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1629 | buffer[i].meta_data.sensor == sensor_handle))); | 
| Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1630 | } | 
| Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1631 | } else { | 
|  | 1632 | scratch = const_cast<sensors_event_t *>(buffer); | 
|  | 1633 | count = numEvents; | 
| Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1634 | } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1635 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1636 | sendPendingFlushEventsLocked(); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1637 | // Early return if there are no events for this connection. | 
|  | 1638 | if (count == 0) { | 
|  | 1639 | return status_t(NO_ERROR); | 
|  | 1640 | } | 
|  | 1641 |  | 
|  | 1642 | #if DEBUG_CONNECTIONS | 
|  | 1643 | mEventsReceived += count; | 
|  | 1644 | #endif | 
|  | 1645 | if (mCacheSize != 0) { | 
|  | 1646 | // There are some events in the cache which need to be sent first. Copy this buffer to | 
|  | 1647 | // the end of cache. | 
|  | 1648 | if (mCacheSize + count <= mMaxCacheSize) { | 
|  | 1649 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); | 
|  | 1650 | mCacheSize += count; | 
|  | 1651 | } else { | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1652 | // Check if any new sensors have registered on this connection which may have increased | 
|  | 1653 | // the max cache size that is desired. | 
|  | 1654 | if (mCacheSize + count < computeMaxCacheSizeLocked()) { | 
|  | 1655 | reAllocateCacheLocked(scratch, count); | 
|  | 1656 | return status_t(NO_ERROR); | 
|  | 1657 | } | 
| Lianchao Song | d89d29d | 2016-04-12 21:58:32 +0800 | [diff] [blame] | 1658 | if (count >= mMaxCacheSize) { | 
|  | 1659 | //count is larger than MaxCacheSize, | 
|  | 1660 | //so copy the end MaxCacheSize data in scratch buffer to cache | 
|  | 1661 | countFlushCompleteEventsLocked(mEventCache, mCacheSize); | 
|  | 1662 | countFlushCompleteEventsLocked(scratch, count - mMaxCacheSize); | 
|  | 1663 | memcpy(&mEventCache[0], &scratch[count-mMaxCacheSize], | 
|  | 1664 | mMaxCacheSize * sizeof(sensors_event_t)); | 
|  | 1665 | mCacheSize = mMaxCacheSize; | 
|  | 1666 | } else { | 
|  | 1667 | //count is smaller than MaxCacheSize, so numEventsDropped data in cache | 
|  | 1668 | //should be dropped, then copy the scratch to the end of cache | 
|  | 1669 | int numEventsDropped = count + mCacheSize - mMaxCacheSize; | 
|  | 1670 | countFlushCompleteEventsLocked(mEventCache, numEventsDropped); | 
|  | 1671 | memmove(mEventCache, &mEventCache[numEventsDropped], | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1672 | (mCacheSize - numEventsDropped) * sizeof(sensors_event_t)); | 
|  | 1673 |  | 
| Lianchao Song | d89d29d | 2016-04-12 21:58:32 +0800 | [diff] [blame] | 1674 | memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch, | 
|  | 1675 | count * sizeof(sensors_event_t)); | 
|  | 1676 | mCacheSize = mMaxCacheSize; | 
|  | 1677 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1678 | } | 
|  | 1679 | return status_t(NO_ERROR); | 
|  | 1680 | } | 
|  | 1681 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1682 | int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); | 
|  | 1683 | if (index_wake_up_event >= 0) { | 
|  | 1684 | scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 1685 | ++mWakeLockRefCount; | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1686 | #if DEBUG_CONNECTIONS | 
|  | 1687 | ++mTotalAcksNeeded; | 
|  | 1688 | #endif | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1689 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1690 |  | 
|  | 1691 | // NOTE: ASensorEvent and sensors_event_t are the same type. | 
|  | 1692 | ssize_t size = SensorEventQueue::write(mChannel, | 
|  | 1693 | reinterpret_cast<ASensorEvent const*>(scratch), count); | 
|  | 1694 | if (size < 0) { | 
|  | 1695 | // Write error, copy events to local cache. | 
| Lianchao Song | d89d29d | 2016-04-12 21:58:32 +0800 | [diff] [blame] | 1696 | ALOGE("write failed, size=%d  err=%d:%s ", size, errno, strerror(errno)); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1697 | if (index_wake_up_event >= 0) { | 
|  | 1698 | // If there was a wake_up sensor_event, reset the flag. | 
|  | 1699 | scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1700 | if (mWakeLockRefCount > 0) { | 
|  | 1701 | --mWakeLockRefCount; | 
|  | 1702 | } | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1703 | #if DEBUG_CONNECTIONS | 
|  | 1704 | --mTotalAcksNeeded; | 
|  | 1705 | #endif | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1706 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1707 | if (mEventCache == NULL) { | 
|  | 1708 | mMaxCacheSize = computeMaxCacheSizeLocked(); | 
|  | 1709 | mEventCache = new sensors_event_t[mMaxCacheSize]; | 
|  | 1710 | mCacheSize = 0; | 
|  | 1711 | } | 
| Lianchao Song | d89d29d | 2016-04-12 21:58:32 +0800 | [diff] [blame] | 1712 | //here mCacheSize must be 0 because if (mCacheSize != 0) has been executed above | 
|  | 1713 | if (count <= mMaxCacheSize) { | 
|  | 1714 | //count is no more than free size of cache, | 
|  | 1715 | //copy the scratch to the end of cache | 
|  | 1716 | memcpy(&mEventCache[0], scratch, count * sizeof(sensors_event_t)); | 
|  | 1717 | mCacheSize = count; | 
|  | 1718 | } else { | 
|  | 1719 | //count is larger than free size of cache, | 
|  | 1720 | //so just copy the free size of scratch to the end of cache | 
|  | 1721 | ALOGI("count > MaxCache count=%d mMaxCacheSize=%d", count, mMaxCacheSize); | 
|  | 1722 | countFlushCompleteEventsLocked(scratch, count - mMaxCacheSize); | 
|  | 1723 | memcpy(&mEventCache[0], &scratch[count - mMaxCacheSize], | 
|  | 1724 | mMaxCacheSize * sizeof(sensors_event_t)); | 
|  | 1725 | mCacheSize = mMaxCacheSize; | 
|  | 1726 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1727 |  | 
|  | 1728 | // Add this file descriptor to the looper to get a callback when this fd is available for | 
|  | 1729 | // writing. | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1730 | updateLooperRegistrationLocked(mService->getLooper()); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1731 | return size; | 
|  | 1732 | } | 
|  | 1733 |  | 
|  | 1734 | #if DEBUG_CONNECTIONS | 
|  | 1735 | if (size > 0) { | 
|  | 1736 | mEventsSent += count; | 
|  | 1737 | } | 
|  | 1738 | #endif | 
|  | 1739 |  | 
|  | 1740 | return size < 0 ? status_t(size) : status_t(NO_ERROR); | 
|  | 1741 | } | 
|  | 1742 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1743 | void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, | 
|  | 1744 | int count) { | 
|  | 1745 | sensors_event_t *eventCache_new; | 
|  | 1746 | const int new_cache_size = computeMaxCacheSizeLocked(); | 
|  | 1747 | // Allocate new cache, copy over events from the old cache & scratch, free up memory. | 
|  | 1748 | eventCache_new = new sensors_event_t[new_cache_size]; | 
|  | 1749 | memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); | 
|  | 1750 | memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); | 
|  | 1751 |  | 
|  | 1752 | ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, | 
|  | 1753 | new_cache_size); | 
|  | 1754 |  | 
|  | 1755 | delete mEventCache; | 
|  | 1756 | mEventCache = eventCache_new; | 
|  | 1757 | mCacheSize += count; | 
|  | 1758 | mMaxCacheSize = new_cache_size; | 
|  | 1759 | } | 
|  | 1760 |  | 
|  | 1761 | void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { | 
|  | 1762 | ASensorEvent flushCompleteEvent; | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1763 | memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent)); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1764 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1765 | // Loop through all the sensors for this connection and check if there are any pending | 
|  | 1766 | // flush complete events to be sent. | 
|  | 1767 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { | 
|  | 1768 | FlushInfo& flushInfo = mSensorInfo.editValueAt(i); | 
|  | 1769 | while (flushInfo.mPendingFlushEventsToSend > 0) { | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1770 | const int sensor_handle = mSensorInfo.keyAt(i); | 
|  | 1771 | flushCompleteEvent.meta_data.sensor = sensor_handle; | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1772 | bool wakeUpSensor = mService->getSensorFromHandle(sensor_handle).isWakeUpSensor(); | 
|  | 1773 | if (wakeUpSensor) { | 
|  | 1774 | ++mWakeLockRefCount; | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1775 | flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 1776 | } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1777 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); | 
|  | 1778 | if (size < 0) { | 
| Lianchao Song | d89d29d | 2016-04-12 21:58:32 +0800 | [diff] [blame] | 1779 | ALOGE("write failed, size=%d  err=%d:%s ", size, errno, strerror(errno)); | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1780 | if (wakeUpSensor) --mWakeLockRefCount; | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1781 | return; | 
|  | 1782 | } | 
|  | 1783 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", | 
|  | 1784 | flushCompleteEvent.meta_data.sensor); | 
|  | 1785 | flushInfo.mPendingFlushEventsToSend--; | 
|  | 1786 | } | 
|  | 1787 | } | 
|  | 1788 | } | 
|  | 1789 |  | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1790 | void SensorService::SensorEventConnection::writeToSocketFromCache() { | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1791 | // At a time write at most half the size of the receiver buffer in SensorEventQueue OR | 
|  | 1792 | // half the size of the socket buffer allocated in BitTube whichever is smaller. | 
|  | 1793 | const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2, | 
|  | 1794 | int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2))); | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1795 | Mutex::Autolock _l(mConnectionLock); | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1796 | // Send pending flush complete events (if any) | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1797 | sendPendingFlushEventsLocked(); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1798 | for (int numEventsSent = 0; numEventsSent < mCacheSize;) { | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1799 | const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1800 | int index_wake_up_event = | 
|  | 1801 | findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); | 
|  | 1802 | if (index_wake_up_event >= 0) { | 
|  | 1803 | mEventCache[index_wake_up_event + numEventsSent].flags |= | 
|  | 1804 | WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
|  | 1805 | ++mWakeLockRefCount; | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1806 | #if DEBUG_CONNECTIONS | 
|  | 1807 | ++mTotalAcksNeeded; | 
|  | 1808 | #endif | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1809 | } | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1810 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1811 | ssize_t size = SensorEventQueue::write(mChannel, | 
|  | 1812 | reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1813 | numEventsToWrite); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1814 | if (size < 0) { | 
| Lianchao Song | d89d29d | 2016-04-12 21:58:32 +0800 | [diff] [blame] | 1815 | ALOGE("write failed, size=%d  err=%d:%s ", size, errno, strerror(errno)); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1816 | if (index_wake_up_event >= 0) { | 
|  | 1817 | // If there was a wake_up sensor_event, reset the flag. | 
|  | 1818 | mEventCache[index_wake_up_event + numEventsSent].flags  &= | 
|  | 1819 | ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1820 | if (mWakeLockRefCount > 0) { | 
|  | 1821 | --mWakeLockRefCount; | 
|  | 1822 | } | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1823 | #if DEBUG_CONNECTIONS | 
|  | 1824 | --mTotalAcksNeeded; | 
|  | 1825 | #endif | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1826 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1827 | memmove(mEventCache, &mEventCache[numEventsSent], | 
|  | 1828 | (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); | 
|  | 1829 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1830 | numEventsSent, mCacheSize); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1831 | mCacheSize -= numEventsSent; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1832 | return; | 
|  | 1833 | } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1834 | numEventsSent += numEventsToWrite; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1835 | #if DEBUG_CONNECTIONS | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1836 | mEventsSentFromCache += numEventsToWrite; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1837 | #endif | 
| Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1838 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1839 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); | 
|  | 1840 | // All events from the cache have been sent. Reset cache size to zero. | 
|  | 1841 | mCacheSize = 0; | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1842 | // There are no more events in the cache. We don't need to poll for write on the fd. | 
|  | 1843 | // Update Looper registration. | 
|  | 1844 | updateLooperRegistrationLocked(mService->getLooper()); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1845 | } | 
|  | 1846 |  | 
| Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1847 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1848 | sensors_event_t const* scratch, const int numEventsDropped) { | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1849 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1850 | // Count flushComplete events in the events that are about to the dropped. These will be sent | 
|  | 1851 | // separately before the next batch of events. | 
|  | 1852 | for (int j = 0; j < numEventsDropped; ++j) { | 
|  | 1853 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { | 
|  | 1854 | FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor); | 
|  | 1855 | flushInfo.mPendingFlushEventsToSend++; | 
|  | 1856 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", | 
|  | 1857 | flushInfo.mPendingFlushEventsToSend); | 
|  | 1858 | } | 
|  | 1859 | } | 
|  | 1860 | return; | 
|  | 1861 | } | 
|  | 1862 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1863 | int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( | 
|  | 1864 | sensors_event_t const* scratch, const int count) { | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1865 | for (int i = 0; i < count; ++i) { | 
|  | 1866 | if (mService->isWakeUpSensorEvent(scratch[i])) { | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1867 | return i; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1868 | } | 
|  | 1869 | } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1870 | return -1; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1871 | } | 
|  | 1872 |  | 
| Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 1873 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1874 | { | 
|  | 1875 | return mChannel; | 
|  | 1876 | } | 
|  | 1877 |  | 
|  | 1878 | status_t SensorService::SensorEventConnection::enableDisable( | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1879 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, | 
|  | 1880 | int reservedFlags) | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1881 | { | 
|  | 1882 | status_t err; | 
|  | 1883 | if (enabled) { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1884 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1885 | reservedFlags, mOpPackageName); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1886 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1887 | } else { | 
|  | 1888 | err = mService->disable(this, handle); | 
|  | 1889 | } | 
|  | 1890 | return err; | 
|  | 1891 | } | 
|  | 1892 |  | 
|  | 1893 | status_t SensorService::SensorEventConnection::setEventRate( | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1894 | int handle, nsecs_t samplingPeriodNs) | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1895 | { | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1896 | return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1897 | } | 
|  | 1898 |  | 
| Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1899 | status_t  SensorService::SensorEventConnection::flush() { | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1900 | return  mService->flushSensor(this, mOpPackageName); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1901 | } | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1902 |  | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1903 | int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) { | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1904 | if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1905 | { | 
|  | 1906 | // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount, | 
|  | 1907 | // and remove the fd from Looper. Call checkWakeLockState to know if SensorService | 
|  | 1908 | // can release the wake-lock. | 
|  | 1909 | ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd); | 
|  | 1910 | Mutex::Autolock _l(mConnectionLock); | 
|  | 1911 | mDead = true; | 
|  | 1912 | mWakeLockRefCount = 0; | 
|  | 1913 | updateLooperRegistrationLocked(mService->getLooper()); | 
|  | 1914 | } | 
|  | 1915 | mService->checkWakeLockState(); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1916 | if (mDataInjectionMode) { | 
|  | 1917 | // If the Looper has encountered some error in data injection mode, reset SensorService | 
|  | 1918 | // back to normal mode. | 
|  | 1919 | mService->resetToNormalMode(); | 
|  | 1920 | mDataInjectionMode = false; | 
|  | 1921 | } | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1922 | return 1; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1923 | } | 
|  | 1924 |  | 
|  | 1925 | if (events & ALOOPER_EVENT_INPUT) { | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1926 | unsigned char buf[sizeof(sensors_event_t)]; | 
|  | 1927 | ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1928 | { | 
|  | 1929 | Mutex::Autolock _l(mConnectionLock); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1930 | if (numBytesRead == sizeof(sensors_event_t)) { | 
|  | 1931 | if (!mDataInjectionMode) { | 
|  | 1932 | ALOGE("Data injected in normal mode, dropping event" | 
|  | 1933 | "package=%s uid=%d", mPackageName.string(), mUid); | 
|  | 1934 | // Unregister call backs. | 
|  | 1935 | return 0; | 
|  | 1936 | } | 
|  | 1937 | SensorDevice& dev(SensorDevice::getInstance()); | 
|  | 1938 | sensors_event_t sensor_event; | 
|  | 1939 | memset(&sensor_event, 0, sizeof(sensor_event)); | 
|  | 1940 | memcpy(&sensor_event, buf, sizeof(sensors_event_t)); | 
|  | 1941 | Sensor sensor = mService->getSensorFromHandle(sensor_event.sensor); | 
|  | 1942 | sensor_event.type = sensor.getType(); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1943 | dev.injectSensorData(&sensor_event); | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1944 | #if DEBUG_CONNECTIONS | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1945 | ++mEventsReceived; | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1946 | #endif | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1947 | } else if (numBytesRead == sizeof(uint32_t)) { | 
|  | 1948 | uint32_t numAcks = 0; | 
| Aravind Akella | 08f04bf | 2015-05-08 15:59:23 -0700 | [diff] [blame] | 1949 | memcpy(&numAcks, buf, numBytesRead); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1950 | // Sanity check to ensure  there are no read errors in recv, numAcks is always | 
|  | 1951 | // within the range and not zero. If any of the above don't hold reset | 
|  | 1952 | // mWakeLockRefCount to zero. | 
|  | 1953 | if (numAcks > 0 && numAcks < mWakeLockRefCount) { | 
|  | 1954 | mWakeLockRefCount -= numAcks; | 
|  | 1955 | } else { | 
|  | 1956 | mWakeLockRefCount = 0; | 
|  | 1957 | } | 
|  | 1958 | #if DEBUG_CONNECTIONS | 
|  | 1959 | mTotalAcksReceived += numAcks; | 
|  | 1960 | #endif | 
|  | 1961 | } else { | 
|  | 1962 | // Read error, reset wakelock refcount. | 
|  | 1963 | mWakeLockRefCount = 0; | 
|  | 1964 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1965 | } | 
|  | 1966 | // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released | 
|  | 1967 | // here as checkWakeLockState() will need it. | 
|  | 1968 | if (mWakeLockRefCount == 0) { | 
|  | 1969 | mService->checkWakeLockState(); | 
|  | 1970 | } | 
|  | 1971 | // continue getting callbacks. | 
|  | 1972 | return 1; | 
|  | 1973 | } | 
|  | 1974 |  | 
|  | 1975 | if (events & ALOOPER_EVENT_OUTPUT) { | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1976 | // send sensor data that is stored in mEventCache for this connection. | 
|  | 1977 | mService->sendEventsFromCache(this); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1978 | } | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1979 | return 1; | 
|  | 1980 | } | 
|  | 1981 |  | 
|  | 1982 | int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1983 | size_t fifoWakeUpSensors = 0; | 
|  | 1984 | size_t fifoNonWakeUpSensors = 0; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1985 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { | 
|  | 1986 | const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i)); | 
|  | 1987 | if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { | 
|  | 1988 | // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and | 
|  | 1989 | // non wake_up sensors. | 
|  | 1990 | if (sensor.isWakeUpSensor()) { | 
|  | 1991 | fifoWakeUpSensors += sensor.getFifoReservedEventCount(); | 
|  | 1992 | } else { | 
|  | 1993 | fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); | 
|  | 1994 | } | 
|  | 1995 | } else { | 
|  | 1996 | // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. | 
|  | 1997 | if (sensor.isWakeUpSensor()) { | 
|  | 1998 | fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? | 
|  | 1999 | fifoWakeUpSensors : sensor.getFifoMaxEventCount(); | 
|  | 2000 |  | 
|  | 2001 | } else { | 
|  | 2002 | fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? | 
|  | 2003 | fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); | 
|  | 2004 |  | 
|  | 2005 | } | 
|  | 2006 | } | 
|  | 2007 | } | 
|  | 2008 | if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { | 
|  | 2009 | // 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] | 2010 | // size that is equal to that of the batch mode. | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 2011 | // ALOGW("Write failure in non-batch mode"); | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 2012 | return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 2013 | } | 
|  | 2014 | return fifoWakeUpSensors + fifoNonWakeUpSensors; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 2015 | } | 
|  | 2016 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 2017 | // --------------------------------------------------------------------------- | 
|  | 2018 | }; // namespace android | 
|  | 2019 |  |