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