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