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> |
| 38 | |
| 39 | #include <hardware/sensors.h> |
| 40 | |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 41 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 42 | #include "GravitySensor.h" |
| 43 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 44 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 45 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 46 | #include "SensorFusion.h" |
| 47 | #include "SensorService.h" |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 48 | |
| 49 | namespace android { |
| 50 | // --------------------------------------------------------------------------- |
| 51 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 52 | /* |
| 53 | * Notes: |
| 54 | * |
| 55 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 56 | * - run mag sensor from time to time to force calibration |
| 57 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 58 | * |
| 59 | */ |
| 60 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 61 | SensorService::SensorService() |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 62 | : mInitCheck(NO_INIT) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 63 | { |
| 64 | } |
| 65 | |
| 66 | void SensorService::onFirstRef() |
| 67 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 68 | LOGD("nuSensorService starting..."); |
| 69 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 70 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 71 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 72 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 73 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 74 | ssize_t count = dev.getSensorList(&list); |
| 75 | if (count > 0) { |
| 76 | ssize_t orientationIndex = -1; |
| 77 | bool hasGyro = false; |
| 78 | uint32_t virtualSensorsNeeds = |
| 79 | (1<<SENSOR_TYPE_GRAVITY) | |
| 80 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
| 81 | (1<<SENSOR_TYPE_ROTATION_VECTOR); |
| 82 | |
| 83 | mLastEventSeen.setCapacity(count); |
| 84 | for (ssize_t i=0 ; i<count ; i++) { |
| 85 | registerSensor( new HardwareSensor(list[i]) ); |
| 86 | switch (list[i].type) { |
| 87 | case SENSOR_TYPE_ORIENTATION: |
| 88 | orientationIndex = i; |
| 89 | break; |
| 90 | case SENSOR_TYPE_GYROSCOPE: |
| 91 | hasGyro = true; |
| 92 | break; |
| 93 | case SENSOR_TYPE_GRAVITY: |
| 94 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 95 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 96 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 97 | break; |
| 98 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 99 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 100 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 101 | // it's safe to instantiate the SensorFusion object here |
| 102 | // (it wants to be instantiated after h/w sensors have been |
| 103 | // registered) |
| 104 | const SensorFusion& fusion(SensorFusion::getInstance()); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 105 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 106 | if (hasGyro) { |
| 107 | // Always instantiate Android's virtual sensors. Since they are |
| 108 | // instantiated behind sensors from the HAL, they won't |
| 109 | // interfere with applications, unless they looks specifically |
| 110 | // for them (by name). |
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 | registerVirtualSensor( new RotationVectorSensor() ); |
| 113 | registerVirtualSensor( new GravitySensor(list, count) ); |
| 114 | registerVirtualSensor( new LinearAccelerationSensor(list, count) ); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 115 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 116 | // these are optional |
| 117 | registerVirtualSensor( new OrientationSensor() ); |
| 118 | registerVirtualSensor( new CorrectedGyroSensor(list, count) ); |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 119 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 120 | // virtual debugging sensors... |
| 121 | char value[PROPERTY_VALUE_MAX]; |
| 122 | property_get("debug.sensors", value, "0"); |
| 123 | if (atoi(value)) { |
| 124 | registerVirtualSensor( new GyroDriftSensor() ); |
| 125 | } |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 126 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 127 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 128 | // build the sensor list returned to users |
| 129 | mUserSensorList = mSensorList; |
| 130 | if (hasGyro && |
| 131 | (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) { |
| 132 | // if we have the fancy sensor fusion, and it's not provided by the |
| 133 | // HAL, use our own (fused) orientation sensor by removing the |
| 134 | // HAL supplied one form the user list. |
| 135 | if (orientationIndex >= 0) { |
| 136 | mUserSensorList.removeItemsAt(orientationIndex); |
| 137 | } |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 138 | } |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 139 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 140 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
| 141 | mInitCheck = NO_ERROR; |
| 142 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 143 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 146 | void SensorService::registerSensor(SensorInterface* s) |
| 147 | { |
| 148 | sensors_event_t event; |
| 149 | memset(&event, 0, sizeof(event)); |
| 150 | |
| 151 | const Sensor sensor(s->getSensor()); |
| 152 | // add to the sensor list (returned to clients) |
| 153 | mSensorList.add(sensor); |
| 154 | // add to our handle->SensorInterface mapping |
| 155 | mSensorMap.add(sensor.getHandle(), s); |
| 156 | // create an entry in the mLastEventSeen array |
| 157 | mLastEventSeen.add(sensor.getHandle(), event); |
| 158 | } |
| 159 | |
| 160 | void SensorService::registerVirtualSensor(SensorInterface* s) |
| 161 | { |
| 162 | registerSensor(s); |
| 163 | mVirtualSensorList.add( s ); |
| 164 | } |
| 165 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 166 | SensorService::~SensorService() |
| 167 | { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 168 | for (size_t i=0 ; i<mSensorMap.size() ; i++) |
| 169 | delete mSensorMap.valueAt(i); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 172 | static const String16 sDump("android.permission.DUMP"); |
| 173 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 174 | status_t SensorService::dump(int fd, const Vector<String16>& args) |
| 175 | { |
| 176 | const size_t SIZE = 1024; |
| 177 | char buffer[SIZE]; |
| 178 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 179 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 180 | snprintf(buffer, SIZE, "Permission Denial: " |
| 181 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 182 | IPCThreadState::self()->getCallingPid(), |
| 183 | IPCThreadState::self()->getCallingUid()); |
| 184 | result.append(buffer); |
| 185 | } else { |
| 186 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 187 | snprintf(buffer, SIZE, "Sensor List:\n"); |
| 188 | result.append(buffer); |
| 189 | for (size_t i=0 ; i<mSensorList.size() ; i++) { |
| 190 | const Sensor& s(mSensorList[i]); |
| 191 | const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 192 | snprintf(buffer, SIZE, |
| 193 | "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | " |
| 194 | "last=<%5.1f,%5.1f,%5.1f>\n", |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 195 | s.getName().string(), |
| 196 | s.getVendor().string(), |
| 197 | s.getHandle(), |
Mathias Agopian | 24d7235 | 2010-11-05 19:12:58 -0700 | [diff] [blame] | 198 | s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f, |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 199 | e.data[0], e.data[1], e.data[2]); |
| 200 | result.append(buffer); |
| 201 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 202 | SensorFusion::getInstance().dump(result, buffer, SIZE); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 203 | SensorDevice::getInstance().dump(result, buffer, SIZE); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 204 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 205 | snprintf(buffer, SIZE, "%d active connections\n", |
| 206 | mActiveConnections.size()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 207 | result.append(buffer); |
| 208 | snprintf(buffer, SIZE, "Active sensors:\n"); |
| 209 | result.append(buffer); |
| 210 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 211 | int handle = mActiveSensors.keyAt(i); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 212 | snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n", |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 213 | getSensorName(handle).string(), |
| 214 | handle, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 215 | mActiveSensors.valueAt(i)->getNumConnections()); |
| 216 | result.append(buffer); |
| 217 | } |
| 218 | } |
| 219 | write(fd, result.string(), result.size()); |
| 220 | return NO_ERROR; |
| 221 | } |
| 222 | |
| 223 | bool SensorService::threadLoop() |
| 224 | { |
| 225 | LOGD("nuSensorService thread starting..."); |
| 226 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 227 | const size_t numEventMax = 16 * (1 + mVirtualSensorList.size()); |
| 228 | sensors_event_t buffer[numEventMax]; |
| 229 | sensors_event_t scratch[numEventMax]; |
| 230 | SensorDevice& device(SensorDevice::getInstance()); |
| 231 | const size_t vcount = mVirtualSensorList.size(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 232 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 233 | ssize_t count; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 234 | do { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 235 | count = device.poll(buffer, numEventMax); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 236 | if (count<0) { |
| 237 | LOGE("sensor poll failed (%s)", strerror(-count)); |
| 238 | break; |
| 239 | } |
| 240 | |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 241 | recordLastValue(buffer, count); |
| 242 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 243 | // handle virtual sensors |
| 244 | if (count && vcount) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 245 | sensors_event_t const * const event = buffer; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 246 | const DefaultKeyedVector<int, SensorInterface*> virtualSensors( |
| 247 | getActiveVirtualSensors()); |
| 248 | const size_t activeVirtualSensorCount = virtualSensors.size(); |
| 249 | if (activeVirtualSensorCount) { |
| 250 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 251 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 252 | if (fusion.isEnabled()) { |
| 253 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 254 | fusion.process(event[i]); |
| 255 | } |
| 256 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 257 | for (size_t i=0 ; i<size_t(count) ; i++) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 258 | for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { |
| 259 | sensors_event_t out; |
| 260 | if (virtualSensors.valueAt(j)->process(&out, event[i])) { |
| 261 | buffer[count + k] = out; |
| 262 | k++; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | if (k) { |
| 267 | // record the last synthesized values |
| 268 | recordLastValue(&buffer[count], k); |
| 269 | count += k; |
| 270 | // sort the buffer by time-stamps |
| 271 | sortEventBuffer(buffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 276 | // send our events to clients... |
| 277 | const SortedVector< wp<SensorEventConnection> > activeConnections( |
| 278 | getActiveConnections()); |
| 279 | size_t numConnections = activeConnections.size(); |
| 280 | for (size_t i=0 ; i<numConnections ; i++) { |
| 281 | sp<SensorEventConnection> connection( |
| 282 | activeConnections[i].promote()); |
| 283 | if (connection != 0) { |
| 284 | connection->sendEvents(buffer, count, scratch); |
| 285 | } |
| 286 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 287 | } while (count >= 0 || Thread::exitPending()); |
| 288 | |
| 289 | LOGW("Exiting SensorService::threadLoop!"); |
| 290 | return false; |
| 291 | } |
| 292 | |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 293 | void SensorService::recordLastValue( |
| 294 | sensors_event_t const * buffer, size_t count) |
| 295 | { |
| 296 | Mutex::Autolock _l(mLock); |
| 297 | |
| 298 | // record the last event for each sensor |
| 299 | int32_t prev = buffer[0].sensor; |
| 300 | for (size_t i=1 ; i<count ; i++) { |
| 301 | // record the last event of each sensor type in this buffer |
| 302 | int32_t curr = buffer[i].sensor; |
| 303 | if (curr != prev) { |
| 304 | mLastEventSeen.editValueFor(prev) = buffer[i-1]; |
| 305 | prev = curr; |
| 306 | } |
| 307 | } |
| 308 | mLastEventSeen.editValueFor(prev) = buffer[count-1]; |
| 309 | } |
| 310 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 311 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) |
| 312 | { |
| 313 | struct compar { |
| 314 | static int cmp(void const* lhs, void const* rhs) { |
| 315 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 316 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
| 317 | return r->timestamp - l->timestamp; |
| 318 | } |
| 319 | }; |
| 320 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 321 | } |
| 322 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 323 | SortedVector< wp<SensorService::SensorEventConnection> > |
| 324 | SensorService::getActiveConnections() const |
| 325 | { |
| 326 | Mutex::Autolock _l(mLock); |
| 327 | return mActiveConnections; |
| 328 | } |
| 329 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 330 | DefaultKeyedVector<int, SensorInterface*> |
| 331 | SensorService::getActiveVirtualSensors() const |
| 332 | { |
| 333 | Mutex::Autolock _l(mLock); |
| 334 | return mActiveVirtualSensors; |
| 335 | } |
| 336 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 337 | String8 SensorService::getSensorName(int handle) const { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 338 | size_t count = mUserSensorList.size(); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 339 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 340 | const Sensor& sensor(mUserSensorList[i]); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 341 | if (sensor.getHandle() == handle) { |
| 342 | return sensor.getName(); |
| 343 | } |
| 344 | } |
| 345 | String8 result("unknown"); |
| 346 | return result; |
| 347 | } |
| 348 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 349 | Vector<Sensor> SensorService::getSensorList() |
| 350 | { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 351 | return mUserSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | sp<ISensorEventConnection> SensorService::createSensorEventConnection() |
| 355 | { |
| 356 | sp<SensorEventConnection> result(new SensorEventConnection(this)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 357 | return result; |
| 358 | } |
| 359 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 360 | void SensorService::cleanupConnection(SensorEventConnection* c) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 361 | { |
| 362 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 363 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 364 | size_t size = mActiveSensors.size(); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 365 | LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 366 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 367 | int handle = mActiveSensors.keyAt(i); |
| 368 | if (c->hasSensor(handle)) { |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 369 | LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 370 | SensorInterface* sensor = mSensorMap.valueFor( handle ); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 371 | LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 372 | if (sensor) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 373 | sensor->activate(c, false); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 374 | } |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 375 | } |
| 376 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 377 | LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle); |
| 378 | LOGD_IF(DEBUG_CONNECTIONS, |
| 379 | "removing connection %p for sensor[%d].handle=0x%08x", |
| 380 | c, i, handle); |
| 381 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 382 | if (rec && rec->removeConnection(connection)) { |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 383 | LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 384 | mActiveSensors.removeItemsAt(i, 1); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 385 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 386 | delete rec; |
| 387 | size--; |
| 388 | } else { |
| 389 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 390 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 391 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 392 | mActiveConnections.remove(connection); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
| 396 | int handle) |
| 397 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 398 | if (mInitCheck != NO_ERROR) |
| 399 | return mInitCheck; |
| 400 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 401 | Mutex::Autolock _l(mLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 402 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 403 | status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 404 | if (err == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 405 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 406 | if (rec == 0) { |
| 407 | rec = new SensorRecord(connection); |
| 408 | mActiveSensors.add(handle, rec); |
| 409 | if (sensor->isVirtual()) { |
| 410 | mActiveVirtualSensors.add(handle, sensor); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 411 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 412 | } else { |
| 413 | if (rec->addConnection(connection)) { |
| 414 | // this sensor is already activated, but we are adding a |
| 415 | // connection that uses it. Immediately send down the last |
Mathias Agopian | 3f2f891 | 2011-03-10 15:23:28 -0800 | [diff] [blame] | 416 | // known value of the requested sensor if it's not a |
| 417 | // "continuous" sensor. |
| 418 | if (sensor->getSensor().getMinDelay() == 0) { |
| 419 | sensors_event_t scratch; |
| 420 | sensors_event_t& event(mLastEventSeen.editValueFor(handle)); |
| 421 | if (event.version == sizeof(sensors_event_t)) { |
| 422 | connection->sendEvents(&event, 1); |
| 423 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | if (err == NO_ERROR) { |
| 428 | // connection now active |
| 429 | if (connection->addSensor(handle)) { |
| 430 | // the sensor was added (which means it wasn't already there) |
| 431 | // so, see if this connection becomes active |
| 432 | if (mActiveConnections.indexOf(connection) < 0) { |
| 433 | mActiveConnections.add(connection); |
| 434 | } |
| 435 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | return err; |
| 439 | } |
| 440 | |
| 441 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, |
| 442 | int handle) |
| 443 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 444 | if (mInitCheck != NO_ERROR) |
| 445 | return mInitCheck; |
| 446 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 447 | status_t err = NO_ERROR; |
| 448 | Mutex::Autolock _l(mLock); |
| 449 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 450 | if (rec) { |
| 451 | // see if this connection becomes inactive |
| 452 | connection->removeSensor(handle); |
| 453 | if (connection->hasAnySensor() == false) { |
| 454 | mActiveConnections.remove(connection); |
| 455 | } |
| 456 | // see if this sensor becomes inactive |
| 457 | if (rec->removeConnection(connection)) { |
| 458 | mActiveSensors.removeItem(handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 459 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 460 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 461 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 462 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 463 | err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 464 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 465 | return err; |
| 466 | } |
| 467 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 468 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 469 | int handle, nsecs_t ns) |
| 470 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 471 | if (mInitCheck != NO_ERROR) |
| 472 | return mInitCheck; |
| 473 | |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 474 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 475 | if (!sensor) |
| 476 | return BAD_VALUE; |
| 477 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 478 | if (ns < 0) |
| 479 | return BAD_VALUE; |
| 480 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame^] | 481 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 482 | if (ns < minDelayNs) { |
| 483 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 486 | if (ns < MINIMUM_EVENTS_PERIOD) |
| 487 | ns = MINIMUM_EVENTS_PERIOD; |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 488 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 489 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // --------------------------------------------------------------------------- |
| 493 | |
| 494 | SensorService::SensorRecord::SensorRecord( |
| 495 | const sp<SensorEventConnection>& connection) |
| 496 | { |
| 497 | mConnections.add(connection); |
| 498 | } |
| 499 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 500 | bool SensorService::SensorRecord::addConnection( |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 501 | const sp<SensorEventConnection>& connection) |
| 502 | { |
| 503 | if (mConnections.indexOf(connection) < 0) { |
| 504 | mConnections.add(connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 505 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 506 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 507 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | bool SensorService::SensorRecord::removeConnection( |
| 511 | const wp<SensorEventConnection>& connection) |
| 512 | { |
| 513 | ssize_t index = mConnections.indexOf(connection); |
| 514 | if (index >= 0) { |
| 515 | mConnections.removeItemsAt(index, 1); |
| 516 | } |
| 517 | return mConnections.size() ? false : true; |
| 518 | } |
| 519 | |
| 520 | // --------------------------------------------------------------------------- |
| 521 | |
| 522 | SensorService::SensorEventConnection::SensorEventConnection( |
| 523 | const sp<SensorService>& service) |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 524 | : mService(service), mChannel(new BitTube()) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 525 | { |
| 526 | } |
| 527 | |
| 528 | SensorService::SensorEventConnection::~SensorEventConnection() |
| 529 | { |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 530 | LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 531 | mService->cleanupConnection(this); |
| 532 | } |
| 533 | |
| 534 | void SensorService::SensorEventConnection::onFirstRef() |
| 535 | { |
| 536 | } |
| 537 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 538 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 539 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 540 | if (mSensorInfo.indexOf(handle) <= 0) { |
| 541 | mSensorInfo.add(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 542 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 543 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 544 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 547 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 548 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 549 | if (mSensorInfo.remove(handle) >= 0) { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 550 | return true; |
| 551 | } |
| 552 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 556 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 557 | return mSensorInfo.indexOf(handle) >= 0; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | bool SensorService::SensorEventConnection::hasAnySensor() const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 561 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 562 | return mSensorInfo.size() ? true : false; |
| 563 | } |
| 564 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 565 | status_t SensorService::SensorEventConnection::sendEvents( |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 566 | sensors_event_t const* buffer, size_t numEvents, |
| 567 | sensors_event_t* scratch) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 568 | { |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 569 | // filter out events not for this connection |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 570 | size_t count = 0; |
| 571 | if (scratch) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 572 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 573 | size_t i=0; |
| 574 | while (i<numEvents) { |
| 575 | const int32_t curr = buffer[i].sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 576 | if (mSensorInfo.indexOf(curr) >= 0) { |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 577 | do { |
| 578 | scratch[count++] = buffer[i++]; |
| 579 | } while ((i<numEvents) && (buffer[i].sensor == curr)); |
| 580 | } else { |
| 581 | i++; |
| 582 | } |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 583 | } |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 584 | } else { |
| 585 | scratch = const_cast<sensors_event_t *>(buffer); |
| 586 | count = numEvents; |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 587 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 588 | |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 589 | if (count == 0) |
| 590 | return 0; |
| 591 | |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 592 | ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 593 | if (size == -EAGAIN) { |
| 594 | // the destination doesn't accept events anymore, it's probably |
| 595 | // full. For now, we just drop the events on the floor. |
Mathias Agopian | 16c3e4a | 2011-07-20 18:51:15 -0700 | [diff] [blame] | 596 | //LOGW("dropping %d events on the floor", count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 597 | return size; |
| 598 | } |
| 599 | |
Mathias Agopian | 16c3e4a | 2011-07-20 18:51:15 -0700 | [diff] [blame] | 600 | //LOGE_IF(size<0, "dropping %d events on the floor (%s)", |
| 601 | // count, strerror(-size)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 602 | |
Jeff Brown | 1e0b1e8 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 603 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 606 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 607 | { |
| 608 | return mChannel; |
| 609 | } |
| 610 | |
| 611 | status_t SensorService::SensorEventConnection::enableDisable( |
| 612 | int handle, bool enabled) |
| 613 | { |
| 614 | status_t err; |
| 615 | if (enabled) { |
| 616 | err = mService->enable(this, handle); |
| 617 | } else { |
| 618 | err = mService->disable(this, handle); |
| 619 | } |
| 620 | return err; |
| 621 | } |
| 622 | |
| 623 | status_t SensorService::SensorEventConnection::setEventRate( |
| 624 | int handle, nsecs_t ns) |
| 625 | { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 626 | return mService->setEventRate(this, handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // --------------------------------------------------------------------------- |
| 630 | }; // namespace android |
| 631 | |