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