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