blob: 9e5e84b382ab4eaf2e14add27e8c252790f51737 [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
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 Agopianf001c922010-11-11 17:58:51 -080018#include <math.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070019#include <sys/types.h>
20
Mathias Agopian33015422011-05-27 18:18:13 -070021#include <cutils/properties.h>
22
Mathias Agopianfc328812010-07-14 23:41:37 -070023#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 Agopian451beee2010-07-19 15:03:55 -070029#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070030#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070031
32#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070033#include <binder/IServiceManager.h>
Mathias Agopian1cb13462011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070035
36#include <gui/ISensorServer.h>
37#include <gui/ISensorEventConnection.h>
Mathias Agopian907103b2012-04-02 18:38:02 -070038#include <gui/SensorEventQueue.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070039
40#include <hardware/sensors.h>
41
Mathias Agopian984826c2011-05-17 22:54:42 -070042#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080043#include "GravitySensor.h"
44#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070045#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080046#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070047#include "SensorFusion.h"
48#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070049
50namespace android {
51// ---------------------------------------------------------------------------
52
Mathias Agopian33015422011-05-27 18:18:13 -070053/*
54 * Notes:
55 *
56 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070057 * - run mag sensor from time to time to force calibration
58 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
59 *
60 */
61
Mathias Agopianfc328812010-07-14 23:41:37 -070062SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -070063 : mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070064{
65}
66
67void SensorService::onFirstRef()
68{
Steve Blocka5512372011-12-20 16:23:08 +000069 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070070
Mathias Agopianf001c922010-11-11 17:58:51 -080071 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070072
Mathias Agopianf001c922010-11-11 17:58:51 -080073 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080074 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070075 ssize_t count = dev.getSensorList(&list);
76 if (count > 0) {
77 ssize_t orientationIndex = -1;
78 bool hasGyro = false;
79 uint32_t virtualSensorsNeeds =
80 (1<<SENSOR_TYPE_GRAVITY) |
81 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
82 (1<<SENSOR_TYPE_ROTATION_VECTOR);
83
84 mLastEventSeen.setCapacity(count);
85 for (ssize_t i=0 ; i<count ; i++) {
86 registerSensor( new HardwareSensor(list[i]) );
87 switch (list[i].type) {
88 case SENSOR_TYPE_ORIENTATION:
89 orientationIndex = i;
90 break;
91 case SENSOR_TYPE_GYROSCOPE:
92 hasGyro = true;
93 break;
94 case SENSOR_TYPE_GRAVITY:
95 case SENSOR_TYPE_LINEAR_ACCELERATION:
96 case SENSOR_TYPE_ROTATION_VECTOR:
97 virtualSensorsNeeds &= ~(1<<list[i].type);
98 break;
99 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700100 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700101
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700102 // it's safe to instantiate the SensorFusion object here
103 // (it wants to be instantiated after h/w sensors have been
104 // registered)
105 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700106
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700107 if (hasGyro) {
108 // Always instantiate Android's virtual sensors. Since they are
109 // instantiated behind sensors from the HAL, they won't
110 // interfere with applications, unless they looks specifically
111 // for them (by name).
Mathias Agopian984826c2011-05-17 22:54:42 -0700112
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700113 registerVirtualSensor( new RotationVectorSensor() );
114 registerVirtualSensor( new GravitySensor(list, count) );
115 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian984826c2011-05-17 22:54:42 -0700116
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700117 // these are optional
118 registerVirtualSensor( new OrientationSensor() );
119 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33015422011-05-27 18:18:13 -0700120
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700121 // virtual debugging sensors...
122 char value[PROPERTY_VALUE_MAX];
123 property_get("debug.sensors", value, "0");
124 if (atoi(value)) {
125 registerVirtualSensor( new GyroDriftSensor() );
126 }
Mathias Agopian33015422011-05-27 18:18:13 -0700127 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800128
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700129 // build the sensor list returned to users
130 mUserSensorList = mSensorList;
131 if (hasGyro &&
132 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
133 // if we have the fancy sensor fusion, and it's not provided by the
134 // HAL, use our own (fused) orientation sensor by removing the
135 // HAL supplied one form the user list.
136 if (orientationIndex >= 0) {
137 mUserSensorList.removeItemsAt(orientationIndex);
138 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700139 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700140
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700141 run("SensorService", PRIORITY_URGENT_DISPLAY);
142 mInitCheck = NO_ERROR;
143 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700144 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700145}
146
Mathias Agopianf001c922010-11-11 17:58:51 -0800147void SensorService::registerSensor(SensorInterface* s)
148{
149 sensors_event_t event;
150 memset(&event, 0, sizeof(event));
151
152 const Sensor sensor(s->getSensor());
153 // add to the sensor list (returned to clients)
154 mSensorList.add(sensor);
155 // add to our handle->SensorInterface mapping
156 mSensorMap.add(sensor.getHandle(), s);
157 // create an entry in the mLastEventSeen array
158 mLastEventSeen.add(sensor.getHandle(), event);
159}
160
161void SensorService::registerVirtualSensor(SensorInterface* s)
162{
163 registerSensor(s);
164 mVirtualSensorList.add( s );
165}
166
Mathias Agopianfc328812010-07-14 23:41:37 -0700167SensorService::~SensorService()
168{
Mathias Agopianf001c922010-11-11 17:58:51 -0800169 for (size_t i=0 ; i<mSensorMap.size() ; i++)
170 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700171}
172
Mathias Agopian1cb13462011-06-27 16:05:52 -0700173static const String16 sDump("android.permission.DUMP");
174
Mathias Agopianfc328812010-07-14 23:41:37 -0700175status_t SensorService::dump(int fd, const Vector<String16>& args)
176{
177 const size_t SIZE = 1024;
178 char buffer[SIZE];
179 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700180 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700181 snprintf(buffer, SIZE, "Permission Denial: "
182 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
183 IPCThreadState::self()->getCallingPid(),
184 IPCThreadState::self()->getCallingUid());
185 result.append(buffer);
186 } else {
187 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700188 snprintf(buffer, SIZE, "Sensor List:\n");
189 result.append(buffer);
190 for (size_t i=0 ; i<mSensorList.size() ; i++) {
191 const Sensor& s(mSensorList[i]);
192 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700193 snprintf(buffer, SIZE,
194 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
195 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700196 s.getName().string(),
197 s.getVendor().string(),
198 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700199 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700200 e.data[0], e.data[1], e.data[2]);
201 result.append(buffer);
202 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700203 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800204 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700205
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700206 snprintf(buffer, SIZE, "%d active connections\n",
207 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700208 result.append(buffer);
209 snprintf(buffer, SIZE, "Active sensors:\n");
210 result.append(buffer);
211 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700212 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800213 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700214 getSensorName(handle).string(),
215 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700216 mActiveSensors.valueAt(i)->getNumConnections());
217 result.append(buffer);
218 }
219 }
220 write(fd, result.string(), result.size());
221 return NO_ERROR;
222}
223
224bool SensorService::threadLoop()
225{
Steve Blocka5512372011-12-20 16:23:08 +0000226 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700227
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700228 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700229 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700230 sensors_event_t buffer[minBufferSize];
231 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800232 SensorDevice& device(SensorDevice::getInstance());
233 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700234
Mathias Agopianf001c922010-11-11 17:58:51 -0800235 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700236 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800237 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700238 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000239 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700240 break;
241 }
242
Mathias Agopian94e8f682010-11-10 17:50:28 -0800243 recordLastValue(buffer, count);
244
Mathias Agopianf001c922010-11-11 17:58:51 -0800245 // handle virtual sensors
246 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700247 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800248 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
249 getActiveVirtualSensors());
250 const size_t activeVirtualSensorCount = virtualSensors.size();
251 if (activeVirtualSensorCount) {
252 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700253 SensorFusion& fusion(SensorFusion::getInstance());
254 if (fusion.isEnabled()) {
255 for (size_t i=0 ; i<size_t(count) ; i++) {
256 fusion.process(event[i]);
257 }
258 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700259 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800260 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700261 if (count + k >= minBufferSize) {
262 ALOGE("buffer too small to hold all events: "
263 "count=%u, k=%u, size=%u",
264 count, k, minBufferSize);
265 break;
266 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800267 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700268 SensorInterface* si = virtualSensors.valueAt(j);
269 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800270 buffer[count + k] = out;
271 k++;
272 }
273 }
274 }
275 if (k) {
276 // record the last synthesized values
277 recordLastValue(&buffer[count], k);
278 count += k;
279 // sort the buffer by time-stamps
280 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700281 }
282 }
283 }
284
Mathias Agopianf001c922010-11-11 17:58:51 -0800285 // send our events to clients...
286 const SortedVector< wp<SensorEventConnection> > activeConnections(
287 getActiveConnections());
288 size_t numConnections = activeConnections.size();
289 for (size_t i=0 ; i<numConnections ; i++) {
290 sp<SensorEventConnection> connection(
291 activeConnections[i].promote());
292 if (connection != 0) {
293 connection->sendEvents(buffer, count, scratch);
294 }
295 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700296 } while (count >= 0 || Thread::exitPending());
297
Steve Block3c20fbe2012-01-05 23:22:43 +0000298 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800299 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700300 return false;
301}
302
Mathias Agopian94e8f682010-11-10 17:50:28 -0800303void SensorService::recordLastValue(
304 sensors_event_t const * buffer, size_t count)
305{
306 Mutex::Autolock _l(mLock);
307
308 // record the last event for each sensor
309 int32_t prev = buffer[0].sensor;
310 for (size_t i=1 ; i<count ; i++) {
311 // record the last event of each sensor type in this buffer
312 int32_t curr = buffer[i].sensor;
313 if (curr != prev) {
314 mLastEventSeen.editValueFor(prev) = buffer[i-1];
315 prev = curr;
316 }
317 }
318 mLastEventSeen.editValueFor(prev) = buffer[count-1];
319}
320
Mathias Agopianf001c922010-11-11 17:58:51 -0800321void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
322{
323 struct compar {
324 static int cmp(void const* lhs, void const* rhs) {
325 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
326 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700327 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800328 }
329 };
330 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
331}
332
Mathias Agopianfc328812010-07-14 23:41:37 -0700333SortedVector< wp<SensorService::SensorEventConnection> >
334SensorService::getActiveConnections() const
335{
336 Mutex::Autolock _l(mLock);
337 return mActiveConnections;
338}
339
Mathias Agopianf001c922010-11-11 17:58:51 -0800340DefaultKeyedVector<int, SensorInterface*>
341SensorService::getActiveVirtualSensors() const
342{
343 Mutex::Autolock _l(mLock);
344 return mActiveVirtualSensors;
345}
346
Mathias Agopian5d270722010-07-19 15:20:39 -0700347String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700348 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700349 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700350 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700351 if (sensor.getHandle() == handle) {
352 return sensor.getName();
353 }
354 }
355 String8 result("unknown");
356 return result;
357}
358
Mathias Agopianfc328812010-07-14 23:41:37 -0700359Vector<Sensor> SensorService::getSensorList()
360{
Mathias Agopian010e4222011-06-08 20:06:50 -0700361 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700362}
363
364sp<ISensorEventConnection> SensorService::createSensorEventConnection()
365{
366 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700367 return result;
368}
369
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800370void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700371{
372 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800373 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700374 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000375 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700376 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800377 int handle = mActiveSensors.keyAt(i);
378 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000379 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800380 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000381 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800382 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800383 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800384 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800385 }
386 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000387 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000388 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700389 "removing connection %p for sensor[%d].handle=0x%08x",
390 c, i, handle);
391
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800392 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000393 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700394 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800395 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700396 delete rec;
397 size--;
398 } else {
399 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700400 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700401 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700402 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700403}
404
405status_t SensorService::enable(const sp<SensorEventConnection>& connection,
406 int handle)
407{
Mathias Agopian50df2952010-07-19 19:09:10 -0700408 if (mInitCheck != NO_ERROR)
409 return mInitCheck;
410
Mathias Agopianfc328812010-07-14 23:41:37 -0700411 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800412 SensorInterface* sensor = mSensorMap.valueFor(handle);
413 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700414 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800415 SensorRecord* rec = mActiveSensors.valueFor(handle);
416 if (rec == 0) {
417 rec = new SensorRecord(connection);
418 mActiveSensors.add(handle, rec);
419 if (sensor->isVirtual()) {
420 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700421 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800422 } else {
423 if (rec->addConnection(connection)) {
424 // this sensor is already activated, but we are adding a
425 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800426 // known value of the requested sensor if it's not a
427 // "continuous" sensor.
428 if (sensor->getSensor().getMinDelay() == 0) {
429 sensors_event_t scratch;
430 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
431 if (event.version == sizeof(sensors_event_t)) {
432 connection->sendEvents(&event, 1);
433 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800434 }
435 }
436 }
437 if (err == NO_ERROR) {
438 // connection now active
439 if (connection->addSensor(handle)) {
440 // the sensor was added (which means it wasn't already there)
441 // so, see if this connection becomes active
442 if (mActiveConnections.indexOf(connection) < 0) {
443 mActiveConnections.add(connection);
444 }
445 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700446 }
447 }
448 return err;
449}
450
451status_t SensorService::disable(const sp<SensorEventConnection>& connection,
452 int handle)
453{
Mathias Agopian50df2952010-07-19 19:09:10 -0700454 if (mInitCheck != NO_ERROR)
455 return mInitCheck;
456
Mathias Agopianfc328812010-07-14 23:41:37 -0700457 status_t err = NO_ERROR;
458 Mutex::Autolock _l(mLock);
459 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700460 if (rec) {
461 // see if this connection becomes inactive
462 connection->removeSensor(handle);
463 if (connection->hasAnySensor() == false) {
464 mActiveConnections.remove(connection);
465 }
466 // see if this sensor becomes inactive
467 if (rec->removeConnection(connection)) {
468 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800469 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700470 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700471 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800472 SensorInterface* sensor = mSensorMap.valueFor(handle);
473 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700474 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700475 return err;
476}
477
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700478status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700479 int handle, nsecs_t ns)
480{
Mathias Agopian50df2952010-07-19 19:09:10 -0700481 if (mInitCheck != NO_ERROR)
482 return mInitCheck;
483
Mathias Agopianae09d652011-11-01 17:37:49 -0700484 SensorInterface* sensor = mSensorMap.valueFor(handle);
485 if (!sensor)
486 return BAD_VALUE;
487
Mathias Agopian1cd70002010-07-21 15:59:50 -0700488 if (ns < 0)
489 return BAD_VALUE;
490
Mathias Agopian62569ec2011-11-07 21:21:47 -0800491 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
492 if (ns < minDelayNs) {
493 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700494 }
495
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700496 if (ns < MINIMUM_EVENTS_PERIOD)
497 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700498
Mathias Agopianf001c922010-11-11 17:58:51 -0800499 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700500}
501
502// ---------------------------------------------------------------------------
503
504SensorService::SensorRecord::SensorRecord(
505 const sp<SensorEventConnection>& connection)
506{
507 mConnections.add(connection);
508}
509
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700510bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700511 const sp<SensorEventConnection>& connection)
512{
513 if (mConnections.indexOf(connection) < 0) {
514 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700515 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700516 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700517 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700518}
519
520bool SensorService::SensorRecord::removeConnection(
521 const wp<SensorEventConnection>& connection)
522{
523 ssize_t index = mConnections.indexOf(connection);
524 if (index >= 0) {
525 mConnections.removeItemsAt(index, 1);
526 }
527 return mConnections.size() ? false : true;
528}
529
530// ---------------------------------------------------------------------------
531
532SensorService::SensorEventConnection::SensorEventConnection(
533 const sp<SensorService>& service)
Mathias Agopianb3989272011-10-20 18:42:02 -0700534 : mService(service), mChannel(new BitTube())
Mathias Agopianfc328812010-07-14 23:41:37 -0700535{
536}
537
538SensorService::SensorEventConnection::~SensorEventConnection()
539{
Steve Blocka5512372011-12-20 16:23:08 +0000540 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700541 mService->cleanupConnection(this);
542}
543
544void SensorService::SensorEventConnection::onFirstRef()
545{
546}
547
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700548bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800549 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800550 if (mSensorInfo.indexOf(handle) <= 0) {
551 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700552 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700553 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700554 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700555}
556
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700557bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800558 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800559 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700560 return true;
561 }
562 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700563}
564
565bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800566 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800567 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700568}
569
570bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800571 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700572 return mSensorInfo.size() ? true : false;
573}
574
Mathias Agopianfc328812010-07-14 23:41:37 -0700575status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700576 sensors_event_t const* buffer, size_t numEvents,
577 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700578{
Mathias Agopiancf510012010-07-22 16:18:10 -0700579 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700580 size_t count = 0;
581 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800582 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700583 size_t i=0;
584 while (i<numEvents) {
585 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800586 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700587 do {
588 scratch[count++] = buffer[i++];
589 } while ((i<numEvents) && (buffer[i].sensor == curr));
590 } else {
591 i++;
592 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700593 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700594 } else {
595 scratch = const_cast<sensors_event_t *>(buffer);
596 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700597 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700598
Mathias Agopian907103b2012-04-02 18:38:02 -0700599 // NOTE: ASensorEvent and sensors_event_t are the same type
600 ssize_t size = SensorEventQueue::write(mChannel,
601 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700602 if (size == -EAGAIN) {
603 // the destination doesn't accept events anymore, it's probably
604 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000605 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700606 return size;
607 }
608
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700609 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700610}
611
Mathias Agopianb3989272011-10-20 18:42:02 -0700612sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700613{
614 return mChannel;
615}
616
617status_t SensorService::SensorEventConnection::enableDisable(
618 int handle, bool enabled)
619{
620 status_t err;
621 if (enabled) {
622 err = mService->enable(this, handle);
623 } else {
624 err = mService->disable(this, handle);
625 }
626 return err;
627}
628
629status_t SensorService::SensorEventConnection::setEventRate(
630 int handle, nsecs_t ns)
631{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700632 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700633}
634
635// ---------------------------------------------------------------------------
636}; // namespace android
637