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> |
| 21 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
| 23 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 24 | #include <utils/SortedVector.h> |
| 25 | #include <utils/KeyedVector.h> |
| 26 | #include <utils/threads.h> |
| 27 | #include <utils/Atomic.h> |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/RefBase.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 30 | #include <utils/Singleton.h> |
Mathias Agopian | c4a930d | 2010-07-22 22:19:43 -0700 | [diff] [blame] | 31 | #include <utils/String16.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 32 | |
| 33 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 34 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 35 | #include <binder/PermissionCache.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 36 | |
| 37 | #include <gui/ISensorServer.h> |
| 38 | #include <gui/ISensorEventConnection.h> |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 39 | #include <gui/SensorEventQueue.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 40 | |
| 41 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 42 | #include <hardware_legacy/power.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 43 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 44 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 45 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 46 | #include "GravitySensor.h" |
| 47 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 48 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 49 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 50 | #include "SensorFusion.h" |
| 51 | #include "SensorService.h" |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | // --------------------------------------------------------------------------- |
| 55 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Notes: |
| 58 | * |
| 59 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 60 | * - run mag sensor from time to time to force calibration |
| 61 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 62 | * |
| 63 | */ |
| 64 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 65 | const char* SensorService::WAKE_LOCK_NAME = "SensorService"; |
| 66 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 67 | SensorService::SensorService() |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 68 | : mInitCheck(NO_INIT) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 69 | { |
| 70 | } |
| 71 | |
| 72 | void SensorService::onFirstRef() |
| 73 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 74 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 75 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 76 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 77 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 78 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 79 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 80 | ssize_t count = dev.getSensorList(&list); |
| 81 | if (count > 0) { |
| 82 | ssize_t orientationIndex = -1; |
| 83 | bool hasGyro = false; |
| 84 | uint32_t virtualSensorsNeeds = |
| 85 | (1<<SENSOR_TYPE_GRAVITY) | |
| 86 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
| 87 | (1<<SENSOR_TYPE_ROTATION_VECTOR); |
| 88 | |
| 89 | mLastEventSeen.setCapacity(count); |
| 90 | for (ssize_t i=0 ; i<count ; i++) { |
| 91 | registerSensor( new HardwareSensor(list[i]) ); |
| 92 | switch (list[i].type) { |
| 93 | case SENSOR_TYPE_ORIENTATION: |
| 94 | orientationIndex = i; |
| 95 | break; |
| 96 | case SENSOR_TYPE_GYROSCOPE: |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 97 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 98 | hasGyro = true; |
| 99 | break; |
| 100 | case SENSOR_TYPE_GRAVITY: |
| 101 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 102 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 103 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 104 | break; |
| 105 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 106 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 107 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 108 | // it's safe to instantiate the SensorFusion object here |
| 109 | // (it wants to be instantiated after h/w sensors have been |
| 110 | // registered) |
| 111 | const SensorFusion& fusion(SensorFusion::getInstance()); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 112 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 113 | // build the sensor list returned to users |
| 114 | mUserSensorList = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 115 | |
| 116 | if (hasGyro) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 117 | Sensor aSensor; |
| 118 | |
| 119 | // Add Android virtual sensors if they're not already |
| 120 | // available in the HAL |
| 121 | |
| 122 | aSensor = registerVirtualSensor( new RotationVectorSensor() ); |
| 123 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 124 | mUserSensorList.add(aSensor); |
| 125 | } |
| 126 | |
| 127 | aSensor = registerVirtualSensor( new GravitySensor(list, count) ); |
| 128 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) { |
| 129 | mUserSensorList.add(aSensor); |
| 130 | } |
| 131 | |
| 132 | aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) ); |
| 133 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) { |
| 134 | mUserSensorList.add(aSensor); |
| 135 | } |
| 136 | |
| 137 | aSensor = registerVirtualSensor( new OrientationSensor() ); |
| 138 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 139 | // if we are doing our own rotation-vector, also add |
| 140 | // the orientation sensor and remove the HAL provided one. |
| 141 | mUserSensorList.replaceAt(aSensor, orientationIndex); |
| 142 | } |
| 143 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 144 | // virtual debugging sensors are not added to mUserSensorList |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 145 | registerVirtualSensor( new CorrectedGyroSensor(list, count) ); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 146 | registerVirtualSensor( new GyroDriftSensor() ); |
| 147 | } |
| 148 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 149 | // debugging sensor list |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 150 | mUserSensorListDebug = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 151 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 152 | mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; |
| 153 | FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r"); |
| 154 | char line[128]; |
| 155 | if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) { |
| 156 | line[sizeof(line) - 1] = '\0'; |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 157 | sscanf(line, "%zu", &mSocketBufferSize); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 158 | if (mSocketBufferSize > MAX_SOCKET_BUFFER_SIZE_BATCHED) { |
| 159 | mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; |
| 160 | } |
| 161 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 162 | if (fp) { |
| 163 | fclose(fp); |
| 164 | } |
| 165 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 166 | mWakeLockAcquired = false; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 167 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
| 168 | mInitCheck = NO_ERROR; |
| 169 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 170 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 173 | Sensor SensorService::registerSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 174 | { |
| 175 | sensors_event_t event; |
| 176 | memset(&event, 0, sizeof(event)); |
| 177 | |
| 178 | const Sensor sensor(s->getSensor()); |
| 179 | // add to the sensor list (returned to clients) |
| 180 | mSensorList.add(sensor); |
| 181 | // add to our handle->SensorInterface mapping |
| 182 | mSensorMap.add(sensor.getHandle(), s); |
| 183 | // create an entry in the mLastEventSeen array |
| 184 | mLastEventSeen.add(sensor.getHandle(), event); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 185 | |
| 186 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 189 | Sensor SensorService::registerVirtualSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 190 | { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 191 | Sensor sensor = registerSensor(s); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 192 | mVirtualSensorList.add( s ); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 193 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 196 | SensorService::~SensorService() |
| 197 | { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 198 | for (size_t i=0 ; i<mSensorMap.size() ; i++) |
| 199 | delete mSensorMap.valueAt(i); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 202 | static const String16 sDump("android.permission.DUMP"); |
| 203 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 204 | status_t SensorService::dump(int fd, const Vector<String16>& /*args*/) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 205 | { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 206 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 207 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 208 | result.appendFormat("Permission Denial: " |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 209 | "can't dump SensorService from pid=%d, uid=%d\n", |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 210 | IPCThreadState::self()->getCallingPid(), |
| 211 | IPCThreadState::self()->getCallingUid()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 212 | } else { |
| 213 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 214 | result.append("Sensor List:\n"); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 215 | for (size_t i=0 ; i<mSensorList.size() ; i++) { |
| 216 | const Sensor& s(mSensorList[i]); |
| 217 | const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 218 | result.appendFormat( |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 219 | "%-48s| %-32s| %-48s| 0x%08x | \"%s\"\n\t", |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 220 | s.getName().string(), |
| 221 | s.getVendor().string(), |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 222 | s.getStringType().string(), |
| 223 | s.getHandle(), |
| 224 | s.getRequiredPermission().string()); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 225 | |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 226 | const int reportingMode = s.getReportingMode(); |
| 227 | if (reportingMode == AREPORTING_MODE_CONTINUOUS) { |
| 228 | result.append("continuous |"); |
| 229 | } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) { |
| 230 | result.append("on-change | "); |
| 231 | } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) { |
| 232 | result.append("one-shot | "); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 233 | } else { |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 234 | result.append("special-trigger | "); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 235 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 236 | |
| 237 | if (s.getMinDelay() > 0) { |
| 238 | result.appendFormat("maxRate=%7.2fHz | ", 1e6f / s.getMinDelay()); |
| 239 | } else { |
| 240 | result.appendFormat("minDelay=%5dus |", s.getMinDelay()); |
| 241 | } |
| 242 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 243 | if (s.getFifoMaxEventCount() > 0) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 244 | result.appendFormat("FifoMax=%d events | ", |
| 245 | s.getFifoMaxEventCount()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 246 | } else { |
| 247 | result.append("no batching support | "); |
| 248 | } |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 249 | |
| 250 | switch (s.getType()) { |
| 251 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 252 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 253 | result.appendFormat( |
| 254 | "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n", |
| 255 | e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]); |
| 256 | break; |
| 257 | case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED: |
| 258 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
| 259 | result.appendFormat( |
| 260 | "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n", |
| 261 | e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]); |
| 262 | break; |
| 263 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 264 | result.appendFormat( |
| 265 | "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n", |
| 266 | e.data[0], e.data[1], e.data[2], e.data[3]); |
| 267 | break; |
| 268 | case SENSOR_TYPE_SIGNIFICANT_MOTION: |
| 269 | case SENSOR_TYPE_STEP_DETECTOR: |
| 270 | result.appendFormat( "last=<%f>\n", e.data[0]); |
| 271 | break; |
| 272 | case SENSOR_TYPE_STEP_COUNTER: |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 273 | result.appendFormat( "last=<%" PRIu64 ">\n", e.u64.step_counter); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 274 | break; |
| 275 | default: |
| 276 | // default to 3 values |
| 277 | result.appendFormat( |
| 278 | "last=<%5.1f,%5.1f,%5.1f>\n", |
| 279 | e.data[0], e.data[1], e.data[2]); |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | SensorFusion::getInstance().dump(result); |
| 284 | SensorDevice::getInstance().dump(result); |
| 285 | |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 286 | result.append("Active sensors:\n"); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 287 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 288 | int handle = mActiveSensors.keyAt(i); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 289 | result.appendFormat("%s (handle=0x%08x, connections=%zu)\n", |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 290 | getSensorName(handle).string(), |
| 291 | handle, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 292 | mActiveSensors.valueAt(i)->getNumConnections()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 293 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 294 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 295 | result.appendFormat("%zu Max Socket Buffer size\n", mSocketBufferSize); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 296 | result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : "not held"); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 297 | result.appendFormat("%zd active connections\n", mActiveConnections.size()); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 298 | |
| 299 | for (size_t i=0 ; i < mActiveConnections.size() ; i++) { |
| 300 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 301 | if (connection != 0) { |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 302 | result.appendFormat("Connection Number: %zu \n", i); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 303 | connection->dump(result); |
| 304 | } |
| 305 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 306 | } |
| 307 | write(fd, result.string(), result.size()); |
| 308 | return NO_ERROR; |
| 309 | } |
| 310 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 311 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 312 | sensors_event_t const* buffer, const int count) { |
| 313 | for (int i=0 ; i<count ; i++) { |
| 314 | int handle = buffer[i].sensor; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 315 | if (connection->hasSensor(handle)) { |
| 316 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 317 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 318 | // for this particular one_shot sensor, try cleaning up the connection. |
| 319 | if (sensor != NULL && |
| 320 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 321 | sensor->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 322 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 328 | bool SensorService::threadLoop() |
| 329 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 330 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 331 | |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 332 | // each virtual sensor could generate an event per "real" event, that's why we need |
| 333 | // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. |
| 334 | // in practice, this is too aggressive, but guaranteed to be enough. |
| 335 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 336 | const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size()); |
| 337 | |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 338 | sensors_event_t buffer[minBufferSize]; |
| 339 | sensors_event_t scratch[minBufferSize]; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 340 | SensorDevice& device(SensorDevice::getInstance()); |
| 341 | const size_t vcount = mVirtualSensorList.size(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 342 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 343 | ssize_t count; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 344 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 345 | do { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 346 | count = device.poll(buffer, numEventMax); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 347 | if (count<0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 348 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 349 | break; |
| 350 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 351 | Mutex::Autolock _l(mLock); |
| 352 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 353 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 354 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 355 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 356 | // releasing the wakelock. |
| 357 | bool bufferHasWakeUpEvent = false; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 358 | for (int i = 0; i < count; i++) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 359 | if (isWakeUpSensorEvent(buffer[i])) { |
| 360 | bufferHasWakeUpEvent = true; |
| 361 | break; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 365 | if (bufferHasWakeUpEvent && !mWakeLockAcquired) { |
| 366 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 367 | mWakeLockAcquired = true; |
| 368 | ALOGD_IF(DEBUG_CONNECTIONS, "acquired wakelock %s", WAKE_LOCK_NAME); |
| 369 | } |
| 370 | recordLastValueLocked(buffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 371 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 372 | // handle virtual sensors |
| 373 | if (count && vcount) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 374 | sensors_event_t const * const event = buffer; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 375 | const size_t activeVirtualSensorCount = mActiveVirtualSensors.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 376 | if (activeVirtualSensorCount) { |
| 377 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 378 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 379 | if (fusion.isEnabled()) { |
| 380 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 381 | fusion.process(event[i]); |
| 382 | } |
| 383 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 384 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 385 | for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 386 | if (count + k >= minBufferSize) { |
| 387 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 388 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 389 | count, k, minBufferSize); |
| 390 | break; |
| 391 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 392 | sensors_event_t out; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 393 | SensorInterface* si = mActiveVirtualSensors.valueAt(j); |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 394 | if (si->process(&out, event[i])) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 395 | buffer[count + k] = out; |
| 396 | k++; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | if (k) { |
| 401 | // record the last synthesized values |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 402 | recordLastValueLocked(&buffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 403 | count += k; |
| 404 | // sort the buffer by time-stamps |
| 405 | sortEventBuffer(buffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 410 | // handle backward compatibility for RotationVector sensor |
| 411 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 412 | for (int i = 0; i < count; i++) { |
Mathias Agopian | 7438fd1 | 2013-07-08 12:50:39 -0700 | [diff] [blame] | 413 | if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 414 | // All the 4 components of the quaternion should be available |
| 415 | // No heading accuracy. Set it to -1 |
| 416 | buffer[i].data[4] = -1; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 421 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 422 | // lock if none of the clients need it. |
| 423 | bool needsWakeLock = false; |
| 424 | for (size_t i=0 ; i < mActiveConnections.size(); i++) { |
| 425 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 426 | if (connection != 0) { |
| 427 | connection->sendEvents(buffer, count, scratch); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 428 | needsWakeLock |= connection->needsWakeLock(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 429 | // Some sensors need to be auto disabled after the trigger |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 430 | cleanupAutoDisabledSensorLocked(connection, buffer, count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 431 | } |
| 432 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 433 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 434 | if (mWakeLockAcquired && !needsWakeLock) { |
| 435 | release_wake_lock(WAKE_LOCK_NAME); |
| 436 | mWakeLockAcquired = false; |
| 437 | ALOGD_IF(DEBUG_CONNECTIONS, "released wakelock %s", WAKE_LOCK_NAME); |
| 438 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 439 | } while (count >= 0 || Thread::exitPending()); |
| 440 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 441 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 442 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 443 | return false; |
| 444 | } |
| 445 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 446 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 447 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 448 | const sensors_event_t* last = NULL; |
| 449 | for (size_t i = 0; i < count; i++) { |
| 450 | const sensors_event_t* event = &buffer[i]; |
| 451 | if (event->type != SENSOR_TYPE_META_DATA) { |
| 452 | if (last && event->sensor != last->sensor) { |
| 453 | mLastEventSeen.editValueFor(last->sensor) = *last; |
| 454 | } |
| 455 | last = event; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 458 | if (last) { |
| 459 | mLastEventSeen.editValueFor(last->sensor) = *last; |
| 460 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 463 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) |
| 464 | { |
| 465 | struct compar { |
| 466 | static int cmp(void const* lhs, void const* rhs) { |
| 467 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 468 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 469 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 470 | } |
| 471 | }; |
| 472 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 473 | } |
| 474 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 475 | String8 SensorService::getSensorName(int handle) const { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 476 | size_t count = mUserSensorList.size(); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 477 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 478 | const Sensor& sensor(mUserSensorList[i]); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 479 | if (sensor.getHandle() == handle) { |
| 480 | return sensor.getName(); |
| 481 | } |
| 482 | } |
| 483 | String8 result("unknown"); |
| 484 | return result; |
| 485 | } |
| 486 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 487 | bool SensorService::isVirtualSensor(int handle) const { |
| 488 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 489 | return sensor->isVirtual(); |
| 490 | } |
| 491 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 492 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame^] | 493 | int handle = event.sensor; |
| 494 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 495 | handle = event.meta_data.sensor; |
| 496 | } |
| 497 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 498 | return sensor != NULL && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 501 | Vector<Sensor> SensorService::getSensorList() |
| 502 | { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 503 | char value[PROPERTY_VALUE_MAX]; |
| 504 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 505 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
| 506 | mUserSensorListDebug : mUserSensorList; |
| 507 | Vector<Sensor> accessibleSensorList; |
| 508 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 509 | Sensor sensor = initialSensorList[i]; |
| 510 | if (canAccessSensor(sensor)) { |
| 511 | accessibleSensorList.add(sensor); |
| 512 | } else { |
| 513 | String8 infoMessage; |
| 514 | infoMessage.appendFormat( |
| 515 | "Skipped sensor %s because it requires permission %s", |
| 516 | sensor.getName().string(), |
| 517 | sensor.getRequiredPermission().string()); |
| 518 | ALOGI(infoMessage.string()); |
| 519 | } |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 520 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 521 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | sp<ISensorEventConnection> SensorService::createSensorEventConnection() |
| 525 | { |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 526 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 527 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 528 | return result; |
| 529 | } |
| 530 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 531 | void SensorService::cleanupConnection(SensorEventConnection* c) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 532 | { |
| 533 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 534 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 535 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 536 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 537 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 538 | int handle = mActiveSensors.keyAt(i); |
| 539 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 540 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 541 | SensorInterface* sensor = mSensorMap.valueFor( handle ); |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 542 | ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 543 | if (sensor) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 544 | sensor->activate(c, false); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 545 | } |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 546 | } |
| 547 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 548 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 549 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 550 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 551 | c, i, handle); |
| 552 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 553 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 554 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 555 | mActiveSensors.removeItemsAt(i, 1); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 556 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 557 | delete rec; |
| 558 | size--; |
| 559 | } else { |
| 560 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 561 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 562 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 563 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 564 | BatteryService::cleanup(c->getUid()); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 565 | if (c->needsWakeLock()) { |
| 566 | checkWakeLockStateLocked(); |
| 567 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 570 | Sensor SensorService::getSensorFromHandle(int handle) const { |
| 571 | return mSensorMap.valueFor(handle)->getSensor(); |
| 572 | } |
| 573 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 574 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 575 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 576 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 577 | if (mInitCheck != NO_ERROR) |
| 578 | return mInitCheck; |
| 579 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 580 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 581 | if (sensor == NULL) { |
| 582 | return BAD_VALUE; |
| 583 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 584 | |
| 585 | if (!verifyCanAccessSensor(sensor->getSensor(), "Tried enabling")) { |
| 586 | return BAD_VALUE; |
| 587 | } |
| 588 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 589 | Mutex::Autolock _l(mLock); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 590 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 591 | if (rec == 0) { |
| 592 | rec = new SensorRecord(connection); |
| 593 | mActiveSensors.add(handle, rec); |
| 594 | if (sensor->isVirtual()) { |
| 595 | mActiveVirtualSensors.add(handle, sensor); |
| 596 | } |
| 597 | } else { |
| 598 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 599 | // this sensor is already activated, but we are adding a connection that uses it. |
| 600 | // 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] | 601 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 602 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 603 | // NOTE: The wake_up flag of this event may get set to |
| 604 | // 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] | 605 | sensors_event_t& event(mLastEventSeen.editValueFor(handle)); |
| 606 | if (event.version == sizeof(sensors_event_t)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 607 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 608 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 609 | mWakeLockAcquired = true; |
| 610 | ALOGD_IF(DEBUG_CONNECTIONS, "acquired wakelock for on_change sensor %s", |
| 611 | WAKE_LOCK_NAME); |
| 612 | } |
| 613 | connection->sendEvents(&event, 1, NULL); |
| 614 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
| 615 | checkWakeLockStateLocked(); |
| 616 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (connection->addSensor(handle)) { |
| 623 | BatteryService::enableSensor(connection->getUid(), handle); |
| 624 | // the sensor was added (which means it wasn't already there) |
| 625 | // so, see if this connection becomes active |
| 626 | if (mActiveConnections.indexOf(connection) < 0) { |
| 627 | mActiveConnections.add(connection); |
| 628 | } |
| 629 | } else { |
| 630 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 631 | handle, connection.get()); |
| 632 | } |
| 633 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 634 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 635 | if (samplingPeriodNs < minDelayNs) { |
| 636 | samplingPeriodNs = minDelayNs; |
| 637 | } |
| 638 | |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 639 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d rate=%" PRId64 " timeout== %" PRId64, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 640 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 641 | |
| 642 | status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs, |
| 643 | maxBatchReportLatencyNs); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 644 | if (err == NO_ERROR) { |
| 645 | connection->setFirstFlushPending(handle, true); |
| 646 | status_t err_flush = sensor->flush(connection.get(), handle); |
| 647 | // Flush may return error if the sensor is not activated or the underlying h/w sensor does |
| 648 | // not support flush. |
| 649 | if (err_flush != NO_ERROR) { |
| 650 | connection->setFirstFlushPending(handle, false); |
| 651 | } |
| 652 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 653 | |
| 654 | if (err == NO_ERROR) { |
| 655 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); |
| 656 | err = sensor->activate(connection.get(), true); |
| 657 | } |
| 658 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 659 | if (err != NO_ERROR) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 660 | // batch/activate has failed, reset our state. |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 661 | cleanupWithoutDisableLocked(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 662 | } |
| 663 | return err; |
| 664 | } |
| 665 | |
| 666 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, |
| 667 | int handle) |
| 668 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 669 | if (mInitCheck != NO_ERROR) |
| 670 | return mInitCheck; |
| 671 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 672 | Mutex::Autolock _l(mLock); |
| 673 | status_t err = cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 674 | if (err == NO_ERROR) { |
| 675 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 676 | err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
| 677 | } |
| 678 | return err; |
| 679 | } |
| 680 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 681 | status_t SensorService::cleanupWithoutDisable( |
| 682 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 683 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 684 | return cleanupWithoutDisableLocked(connection, handle); |
| 685 | } |
| 686 | |
| 687 | status_t SensorService::cleanupWithoutDisableLocked( |
| 688 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 689 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 690 | if (rec) { |
| 691 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 692 | if (connection->removeSensor(handle)) { |
| 693 | BatteryService::disableSensor(connection->getUid(), handle); |
| 694 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 695 | if (connection->hasAnySensor() == false) { |
| 696 | mActiveConnections.remove(connection); |
| 697 | } |
| 698 | // see if this sensor becomes inactive |
| 699 | if (rec->removeConnection(connection)) { |
| 700 | mActiveSensors.removeItem(handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 701 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 702 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 703 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 704 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 705 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 706 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 709 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 710 | int handle, nsecs_t ns) |
| 711 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 712 | if (mInitCheck != NO_ERROR) |
| 713 | return mInitCheck; |
| 714 | |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 715 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 716 | if (!sensor) |
| 717 | return BAD_VALUE; |
| 718 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 719 | if (!verifyCanAccessSensor(sensor->getSensor(), "Tried configuring")) { |
| 720 | return BAD_VALUE; |
| 721 | } |
| 722 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 723 | if (ns < 0) |
| 724 | return BAD_VALUE; |
| 725 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 726 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 727 | if (ns < minDelayNs) { |
| 728 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 731 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 734 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, |
| 735 | int handle) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 736 | if (mInitCheck != NO_ERROR) return mInitCheck; |
| 737 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 738 | if (sensor == NULL) { |
| 739 | return BAD_VALUE; |
| 740 | } |
| 741 | |
| 742 | if (!verifyCanAccessSensor(sensor->getSensor(), "Tried flushing")) { |
| 743 | return BAD_VALUE; |
| 744 | } |
| 745 | |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 746 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 747 | ALOGE("flush called on a one-shot sensor"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 748 | return INVALID_OPERATION; |
| 749 | } |
| 750 | return sensor->flush(connection.get(), handle); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 751 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 752 | |
| 753 | |
| 754 | bool SensorService::canAccessSensor(const Sensor& sensor) { |
| 755 | String16 permissionString(sensor.getRequiredPermission()); |
| 756 | return permissionString.size() == 0 || |
| 757 | PermissionCache::checkCallingPermission(permissionString); |
| 758 | } |
| 759 | |
| 760 | bool SensorService::verifyCanAccessSensor(const Sensor& sensor, const char* operation) { |
| 761 | if (canAccessSensor(sensor)) { |
| 762 | return true; |
| 763 | } else { |
| 764 | String8 errorMessage; |
| 765 | errorMessage.appendFormat( |
| 766 | "%s a sensor (%s) without holding its required permission: %s", |
| 767 | operation, |
| 768 | sensor.getName().string(), |
| 769 | sensor.getRequiredPermission().string()); |
| 770 | return false; |
| 771 | } |
| 772 | } |
| 773 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 774 | void SensorService::checkWakeLockState() { |
| 775 | Mutex::Autolock _l(mLock); |
| 776 | checkWakeLockStateLocked(); |
| 777 | } |
| 778 | |
| 779 | void SensorService::checkWakeLockStateLocked() { |
| 780 | if (!mWakeLockAcquired) { |
| 781 | return; |
| 782 | } |
| 783 | bool releaseLock = true; |
| 784 | for (size_t i=0 ; i<mActiveConnections.size() ; i++) { |
| 785 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 786 | if (connection != 0) { |
| 787 | if (connection->needsWakeLock()) { |
| 788 | releaseLock = false; |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | if (releaseLock) { |
| 794 | ALOGD_IF(DEBUG_CONNECTIONS, "releasing wakelock %s", WAKE_LOCK_NAME); |
| 795 | release_wake_lock(WAKE_LOCK_NAME); |
| 796 | mWakeLockAcquired = false; |
| 797 | } |
| 798 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 799 | // --------------------------------------------------------------------------- |
| 800 | |
| 801 | SensorService::SensorRecord::SensorRecord( |
| 802 | const sp<SensorEventConnection>& connection) |
| 803 | { |
| 804 | mConnections.add(connection); |
| 805 | } |
| 806 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 807 | bool SensorService::SensorRecord::addConnection( |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 808 | const sp<SensorEventConnection>& connection) |
| 809 | { |
| 810 | if (mConnections.indexOf(connection) < 0) { |
| 811 | mConnections.add(connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 812 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 813 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 814 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | bool SensorService::SensorRecord::removeConnection( |
| 818 | const wp<SensorEventConnection>& connection) |
| 819 | { |
| 820 | ssize_t index = mConnections.indexOf(connection); |
| 821 | if (index >= 0) { |
| 822 | mConnections.removeItemsAt(index, 1); |
| 823 | } |
| 824 | return mConnections.size() ? false : true; |
| 825 | } |
| 826 | |
| 827 | // --------------------------------------------------------------------------- |
| 828 | |
| 829 | SensorService::SensorEventConnection::SensorEventConnection( |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 830 | const sp<SensorService>& service, uid_t uid) |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 831 | : mService(service), mUid(uid), mWakeLockRefCount(0) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 832 | { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 833 | const SensorDevice& device(SensorDevice::getInstance()); |
| 834 | if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) { |
| 835 | // Increase socket buffer size to 1MB for batching capabilities. |
| 836 | mChannel = new BitTube(service->mSocketBufferSize); |
| 837 | } else { |
| 838 | mChannel = new BitTube(SOCKET_BUFFER_SIZE_NON_BATCHED); |
| 839 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | SensorService::SensorEventConnection::~SensorEventConnection() |
| 843 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 844 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 845 | mService->cleanupConnection(this); |
| 846 | } |
| 847 | |
| 848 | void SensorService::SensorEventConnection::onFirstRef() |
| 849 | { |
| 850 | } |
| 851 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 852 | bool SensorService::SensorEventConnection::needsWakeLock() { |
| 853 | Mutex::Autolock _l(mConnectionLock); |
| 854 | return mWakeLockRefCount > 0; |
| 855 | } |
| 856 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 857 | void SensorService::SensorEventConnection::dump(String8& result) { |
| 858 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 859 | result.appendFormat("%d WakeLockRefCount\n", mWakeLockRefCount); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 860 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 861 | const FlushInfo& flushInfo = mSensorInfo.valueAt(i); |
Patrick Tjin | eefced1 | 2014-02-05 15:06:03 -0800 | [diff] [blame] | 862 | result.appendFormat("\t %s | status: %s | pending flush events %d | uid %d\n", |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 863 | mService->getSensorName(mSensorInfo.keyAt(i)).string(), |
| 864 | flushInfo.mFirstFlushPending ? "First flush pending" : |
| 865 | "active", |
Patrick Tjin | eefced1 | 2014-02-05 15:06:03 -0800 | [diff] [blame] | 866 | flushInfo.mPendingFlushEventsToSend, |
| 867 | mUid); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 871 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 872 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 873 | if (!verifyCanAccessSensor(mService->getSensorFromHandle(handle), "Tried adding")) { |
| 874 | return false; |
| 875 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 876 | if (mSensorInfo.indexOfKey(handle) < 0) { |
| 877 | mSensorInfo.add(handle, FlushInfo()); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 878 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 879 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 880 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 883 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 884 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 885 | if (mSensorInfo.removeItem(handle) >= 0) { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 886 | return true; |
| 887 | } |
| 888 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 892 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 893 | return mSensorInfo.indexOfKey(handle) >= 0; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | bool SensorService::SensorEventConnection::hasAnySensor() const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 897 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 898 | return mSensorInfo.size() ? true : false; |
| 899 | } |
| 900 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 901 | void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, |
| 902 | bool value) { |
| 903 | Mutex::Autolock _l(mConnectionLock); |
| 904 | ssize_t index = mSensorInfo.indexOfKey(handle); |
| 905 | if (index >= 0) { |
| 906 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
| 907 | flushInfo.mFirstFlushPending = value; |
| 908 | } |
| 909 | } |
| 910 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 911 | status_t SensorService::SensorEventConnection::sendEvents( |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 912 | sensors_event_t const* buffer, size_t numEvents, |
| 913 | sensors_event_t* scratch) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 914 | { |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 915 | // filter out events not for this connection |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 916 | size_t count = 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 917 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 918 | if (scratch) { |
| 919 | size_t i=0; |
| 920 | while (i<numEvents) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 921 | int32_t curr = buffer[i].sensor; |
| 922 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 923 | ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", |
| 924 | buffer[i].meta_data.sensor); |
| 925 | // Setting curr to the correct sensor to ensure the sensor events per connection are |
| 926 | // filtered correctly. buffer[i].sensor is zero for meta_data events. |
| 927 | curr = buffer[i].meta_data.sensor; |
| 928 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 929 | ssize_t index = mSensorInfo.indexOfKey(curr); |
| 930 | if (index >= 0 && mSensorInfo[index].mFirstFlushPending == true && |
| 931 | buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 932 | // This is the first flush before activate is called. Events can now be sent for |
| 933 | // this sensor on this connection. |
| 934 | ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", |
| 935 | buffer[i].meta_data.sensor); |
| 936 | mSensorInfo.editValueAt(index).mFirstFlushPending = false; |
| 937 | } |
| 938 | if (index >= 0 && mSensorInfo[index].mFirstFlushPending == false) { |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 939 | do { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 940 | scratch[count++] = buffer[i++]; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 941 | } while ((i<numEvents) && ((buffer[i].sensor == curr) || |
| 942 | (buffer[i].type == SENSOR_TYPE_META_DATA && |
| 943 | buffer[i].meta_data.sensor == curr))); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 944 | } else { |
| 945 | i++; |
| 946 | } |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 947 | } |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 948 | } else { |
| 949 | scratch = const_cast<sensors_event_t *>(buffer); |
| 950 | count = numEvents; |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 951 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 952 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 953 | // Send pending flush events (if any) before sending events from the cache. |
| 954 | { |
| 955 | ASensorEvent flushCompleteEvent; |
| 956 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; |
| 957 | flushCompleteEvent.sensor = 0; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 958 | // Loop through all the sensors for this connection and check if there are any pending |
| 959 | // flush complete events to be sent. |
| 960 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 961 | FlushInfo& flushInfo = mSensorInfo.editValueAt(i); |
| 962 | while (flushInfo.mPendingFlushEventsToSend > 0) { |
| 963 | flushCompleteEvent.meta_data.sensor = mSensorInfo.keyAt(i); |
| 964 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); |
| 965 | if (size < 0) { |
| 966 | // ALOGW("dropping %d events on the floor", count); |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 967 | countFlushCompleteEventsLocked(scratch, count); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 968 | return size; |
| 969 | } |
| 970 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", |
| 971 | flushCompleteEvent.meta_data.sensor); |
| 972 | flushInfo.mPendingFlushEventsToSend--; |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 977 | // Early return if there are no events for this connection. |
| 978 | if (count == 0) { |
| 979 | return status_t(NO_ERROR); |
| 980 | } |
| 981 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 982 | int numWakeUpSensorEvents = countWakeUpSensorEventsLocked(scratch, count); |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 983 | // NOTE: ASensorEvent and sensors_event_t are the same type |
| 984 | ssize_t size = SensorEventQueue::write(mChannel, |
| 985 | reinterpret_cast<ASensorEvent const*>(scratch), count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 986 | if (size == -EAGAIN) { |
| 987 | // the destination doesn't accept events anymore, it's probably |
| 988 | // full. For now, we just drop the events on the floor. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 989 | // ALOGW("dropping %d events on the floor", count); |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 990 | countFlushCompleteEventsLocked(scratch, count); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 991 | mWakeLockRefCount -= numWakeUpSensorEvents; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 992 | return size; |
| 993 | } |
Jeff Brown | 1e0b1e8 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 994 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 997 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 998 | sensors_event_t* scratch, const int numEventsDropped) { |
| 999 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1000 | // Count flushComplete events in the events that are about to the dropped. These will be sent |
| 1001 | // separately before the next batch of events. |
| 1002 | for (int j = 0; j < numEventsDropped; ++j) { |
| 1003 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { |
| 1004 | FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor); |
| 1005 | flushInfo.mPendingFlushEventsToSend++; |
| 1006 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", |
| 1007 | flushInfo.mPendingFlushEventsToSend); |
| 1008 | } |
| 1009 | } |
| 1010 | return; |
| 1011 | } |
| 1012 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1013 | int SensorService::SensorEventConnection::countWakeUpSensorEventsLocked( |
| 1014 | sensors_event_t* scratch, const int count) { |
| 1015 | for (int i = 0; i < count; ++i) { |
| 1016 | if (mService->isWakeUpSensorEvent(scratch[i])) { |
| 1017 | scratch[i].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1018 | ++mWakeLockRefCount; |
| 1019 | return 1; |
| 1020 | } |
| 1021 | } |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 1025 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1026 | { |
| 1027 | return mChannel; |
| 1028 | } |
| 1029 | |
| 1030 | status_t SensorService::SensorEventConnection::enableDisable( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1031 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, |
| 1032 | int reservedFlags) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1033 | { |
| 1034 | status_t err; |
| 1035 | if (enabled) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1036 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, |
| 1037 | reservedFlags); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1038 | } else { |
| 1039 | err = mService->disable(this, handle); |
| 1040 | } |
| 1041 | return err; |
| 1042 | } |
| 1043 | |
| 1044 | status_t SensorService::SensorEventConnection::setEventRate( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1045 | int handle, nsecs_t samplingPeriodNs) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1046 | { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1047 | return mService->setEventRate(this, handle, samplingPeriodNs); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1050 | status_t SensorService::SensorEventConnection::flush() { |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1051 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1052 | const int halVersion = dev.getHalDeviceVersion(); |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1053 | Mutex::Autolock _l(mConnectionLock); |
| 1054 | status_t err(NO_ERROR); |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1055 | // Loop through all sensors for this connection and call flush on each of them. |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1056 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1057 | const int handle = mSensorInfo.keyAt(i); |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1058 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 || mService->isVirtualSensor(handle)) { |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1059 | // For older devices just increment pending flush count which will send a trivial |
| 1060 | // flush complete event. |
| 1061 | FlushInfo& flushInfo = mSensorInfo.editValueFor(handle); |
| 1062 | flushInfo.mPendingFlushEventsToSend++; |
| 1063 | } else { |
| 1064 | status_t err_flush = mService->flushSensor(this, handle); |
| 1065 | if (err_flush != NO_ERROR) { |
| 1066 | ALOGE("Flush error handle=%d %s", handle, strerror(-err_flush)); |
| 1067 | } |
| 1068 | err = (err_flush != NO_ERROR) ? err_flush : err; |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1069 | } |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1070 | } |
| 1071 | return err; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1072 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1073 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1074 | void SensorService::SensorEventConnection::decreaseWakeLockRefCount() { |
| 1075 | { |
| 1076 | Mutex::Autolock _l(mConnectionLock); |
| 1077 | --mWakeLockRefCount; |
| 1078 | } |
| 1079 | // Release the lock before calling checkWakeLockState which also needs the same connectionLock. |
| 1080 | if (mWakeLockRefCount == 0) { |
| 1081 | mService->checkWakeLockState(); |
| 1082 | } |
| 1083 | } |
| 1084 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1085 | // --------------------------------------------------------------------------- |
| 1086 | }; // namespace android |
| 1087 | |