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