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