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