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