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