blob: c9b0f7c27e9095f725c34b482f15df21b73a131b [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 Agopianf001c922010-11-11 17:58:51 -0800121
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700122 // build the sensor list returned to users
123 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700124
125 if (hasGyro) {
126 // virtual debugging sensors are not added to mUserSensorList
127 registerVirtualSensor( new GyroDriftSensor() );
128 }
129
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700130 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 Agopian010e4222011-06-08 20:06:50 -0700138 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700139
Mathias Agopian33264862012-06-28 19:46:54 -0700140 // debugging sensor list
141 for (size_t i=0 ; i<mSensorList.size() ; i++) {
142 switch (mSensorList[i].getType()) {
143 case SENSOR_TYPE_GRAVITY:
144 case SENSOR_TYPE_LINEAR_ACCELERATION:
145 case SENSOR_TYPE_ROTATION_VECTOR:
146 if (strstr(mSensorList[i].getVendor().string(), "Google")) {
147 mUserSensorListDebug.add(mSensorList[i]);
148 }
149 break;
150 default:
151 mUserSensorListDebug.add(mSensorList[i]);
152 break;
153 }
154 }
155
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700156 run("SensorService", PRIORITY_URGENT_DISPLAY);
157 mInitCheck = NO_ERROR;
158 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700159 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700160}
161
Mathias Agopianf001c922010-11-11 17:58:51 -0800162void SensorService::registerSensor(SensorInterface* s)
163{
164 sensors_event_t event;
165 memset(&event, 0, sizeof(event));
166
167 const Sensor sensor(s->getSensor());
168 // add to the sensor list (returned to clients)
169 mSensorList.add(sensor);
170 // add to our handle->SensorInterface mapping
171 mSensorMap.add(sensor.getHandle(), s);
172 // create an entry in the mLastEventSeen array
173 mLastEventSeen.add(sensor.getHandle(), event);
174}
175
176void SensorService::registerVirtualSensor(SensorInterface* s)
177{
178 registerSensor(s);
179 mVirtualSensorList.add( s );
180}
181
Mathias Agopianfc328812010-07-14 23:41:37 -0700182SensorService::~SensorService()
183{
Mathias Agopianf001c922010-11-11 17:58:51 -0800184 for (size_t i=0 ; i<mSensorMap.size() ; i++)
185 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700186}
187
Mathias Agopian1cb13462011-06-27 16:05:52 -0700188static const String16 sDump("android.permission.DUMP");
189
Mathias Agopianfc328812010-07-14 23:41:37 -0700190status_t SensorService::dump(int fd, const Vector<String16>& args)
191{
192 const size_t SIZE = 1024;
193 char buffer[SIZE];
194 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700195 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700196 snprintf(buffer, SIZE, "Permission Denial: "
197 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
198 IPCThreadState::self()->getCallingPid(),
199 IPCThreadState::self()->getCallingUid());
200 result.append(buffer);
201 } else {
202 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700203 snprintf(buffer, SIZE, "Sensor List:\n");
204 result.append(buffer);
205 for (size_t i=0 ; i<mSensorList.size() ; i++) {
206 const Sensor& s(mSensorList[i]);
207 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700208 snprintf(buffer, SIZE,
209 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
210 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700211 s.getName().string(),
212 s.getVendor().string(),
213 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700214 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700215 e.data[0], e.data[1], e.data[2]);
216 result.append(buffer);
217 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700218 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800219 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700220
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700221 snprintf(buffer, SIZE, "%d active connections\n",
222 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700223 result.append(buffer);
224 snprintf(buffer, SIZE, "Active sensors:\n");
225 result.append(buffer);
226 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700227 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800228 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700229 getSensorName(handle).string(),
230 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700231 mActiveSensors.valueAt(i)->getNumConnections());
232 result.append(buffer);
233 }
234 }
235 write(fd, result.string(), result.size());
236 return NO_ERROR;
237}
238
239bool SensorService::threadLoop()
240{
Steve Blocka5512372011-12-20 16:23:08 +0000241 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700242
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700243 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700244 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700245 sensors_event_t buffer[minBufferSize];
246 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800247 SensorDevice& device(SensorDevice::getInstance());
248 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700249
Mathias Agopianf001c922010-11-11 17:58:51 -0800250 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700251 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800252 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700253 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000254 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700255 break;
256 }
257
Mathias Agopian94e8f682010-11-10 17:50:28 -0800258 recordLastValue(buffer, count);
259
Mathias Agopianf001c922010-11-11 17:58:51 -0800260 // handle virtual sensors
261 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700262 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800263 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
264 getActiveVirtualSensors());
265 const size_t activeVirtualSensorCount = virtualSensors.size();
266 if (activeVirtualSensorCount) {
267 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700268 SensorFusion& fusion(SensorFusion::getInstance());
269 if (fusion.isEnabled()) {
270 for (size_t i=0 ; i<size_t(count) ; i++) {
271 fusion.process(event[i]);
272 }
273 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700274 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800275 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700276 if (count + k >= minBufferSize) {
277 ALOGE("buffer too small to hold all events: "
278 "count=%u, k=%u, size=%u",
279 count, k, minBufferSize);
280 break;
281 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800282 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700283 SensorInterface* si = virtualSensors.valueAt(j);
284 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800285 buffer[count + k] = out;
286 k++;
287 }
288 }
289 }
290 if (k) {
291 // record the last synthesized values
292 recordLastValue(&buffer[count], k);
293 count += k;
294 // sort the buffer by time-stamps
295 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700296 }
297 }
298 }
299
Mathias Agopianf001c922010-11-11 17:58:51 -0800300 // send our events to clients...
301 const SortedVector< wp<SensorEventConnection> > activeConnections(
302 getActiveConnections());
303 size_t numConnections = activeConnections.size();
304 for (size_t i=0 ; i<numConnections ; i++) {
305 sp<SensorEventConnection> connection(
306 activeConnections[i].promote());
307 if (connection != 0) {
308 connection->sendEvents(buffer, count, scratch);
309 }
310 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700311 } while (count >= 0 || Thread::exitPending());
312
Steve Block3c20fbe2012-01-05 23:22:43 +0000313 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800314 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700315 return false;
316}
317
Mathias Agopian94e8f682010-11-10 17:50:28 -0800318void SensorService::recordLastValue(
319 sensors_event_t const * buffer, size_t count)
320{
321 Mutex::Autolock _l(mLock);
322
323 // record the last event for each sensor
324 int32_t prev = buffer[0].sensor;
325 for (size_t i=1 ; i<count ; i++) {
326 // record the last event of each sensor type in this buffer
327 int32_t curr = buffer[i].sensor;
328 if (curr != prev) {
329 mLastEventSeen.editValueFor(prev) = buffer[i-1];
330 prev = curr;
331 }
332 }
333 mLastEventSeen.editValueFor(prev) = buffer[count-1];
334}
335
Mathias Agopianf001c922010-11-11 17:58:51 -0800336void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
337{
338 struct compar {
339 static int cmp(void const* lhs, void const* rhs) {
340 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
341 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700342 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800343 }
344 };
345 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
346}
347
Mathias Agopianfc328812010-07-14 23:41:37 -0700348SortedVector< wp<SensorService::SensorEventConnection> >
349SensorService::getActiveConnections() const
350{
351 Mutex::Autolock _l(mLock);
352 return mActiveConnections;
353}
354
Mathias Agopianf001c922010-11-11 17:58:51 -0800355DefaultKeyedVector<int, SensorInterface*>
356SensorService::getActiveVirtualSensors() const
357{
358 Mutex::Autolock _l(mLock);
359 return mActiveVirtualSensors;
360}
361
Mathias Agopian5d270722010-07-19 15:20:39 -0700362String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700363 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700364 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700365 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700366 if (sensor.getHandle() == handle) {
367 return sensor.getName();
368 }
369 }
370 String8 result("unknown");
371 return result;
372}
373
Mathias Agopianfc328812010-07-14 23:41:37 -0700374Vector<Sensor> SensorService::getSensorList()
375{
Mathias Agopian33264862012-06-28 19:46:54 -0700376 char value[PROPERTY_VALUE_MAX];
377 property_get("debug.sensors", value, "0");
378 if (atoi(value)) {
379 return mUserSensorListDebug;
380 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700381 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700382}
383
384sp<ISensorEventConnection> SensorService::createSensorEventConnection()
385{
386 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700387 return result;
388}
389
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800390void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700391{
392 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800393 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700394 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000395 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700396 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800397 int handle = mActiveSensors.keyAt(i);
398 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000399 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800400 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000401 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800402 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800403 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800404 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800405 }
406 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000407 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000408 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700409 "removing connection %p for sensor[%d].handle=0x%08x",
410 c, i, handle);
411
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800412 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000413 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700414 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800415 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700416 delete rec;
417 size--;
418 } else {
419 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700420 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700421 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700422 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700423}
424
425status_t SensorService::enable(const sp<SensorEventConnection>& connection,
426 int handle)
427{
Mathias Agopian50df2952010-07-19 19:09:10 -0700428 if (mInitCheck != NO_ERROR)
429 return mInitCheck;
430
Mathias Agopianfc328812010-07-14 23:41:37 -0700431 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800432 SensorInterface* sensor = mSensorMap.valueFor(handle);
433 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700434 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800435 SensorRecord* rec = mActiveSensors.valueFor(handle);
436 if (rec == 0) {
437 rec = new SensorRecord(connection);
438 mActiveSensors.add(handle, rec);
439 if (sensor->isVirtual()) {
440 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700441 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800442 } else {
443 if (rec->addConnection(connection)) {
444 // this sensor is already activated, but we are adding a
445 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800446 // known value of the requested sensor if it's not a
447 // "continuous" sensor.
448 if (sensor->getSensor().getMinDelay() == 0) {
449 sensors_event_t scratch;
450 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
451 if (event.version == sizeof(sensors_event_t)) {
452 connection->sendEvents(&event, 1);
453 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800454 }
455 }
456 }
457 if (err == NO_ERROR) {
458 // connection now active
459 if (connection->addSensor(handle)) {
460 // the sensor was added (which means it wasn't already there)
461 // so, see if this connection becomes active
462 if (mActiveConnections.indexOf(connection) < 0) {
463 mActiveConnections.add(connection);
464 }
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700465 } else {
466 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
467 handle, connection.get());
Mathias Agopianf001c922010-11-11 17:58:51 -0800468 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700469 }
470 }
471 return err;
472}
473
474status_t SensorService::disable(const sp<SensorEventConnection>& connection,
475 int handle)
476{
Mathias Agopian50df2952010-07-19 19:09:10 -0700477 if (mInitCheck != NO_ERROR)
478 return mInitCheck;
479
Mathias Agopianfc328812010-07-14 23:41:37 -0700480 status_t err = NO_ERROR;
481 Mutex::Autolock _l(mLock);
482 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700483 if (rec) {
484 // see if this connection becomes inactive
485 connection->removeSensor(handle);
486 if (connection->hasAnySensor() == false) {
487 mActiveConnections.remove(connection);
488 }
489 // see if this sensor becomes inactive
490 if (rec->removeConnection(connection)) {
491 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800492 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700493 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700494 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800495 SensorInterface* sensor = mSensorMap.valueFor(handle);
496 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700497 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700498 return err;
499}
500
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700501status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700502 int handle, nsecs_t ns)
503{
Mathias Agopian50df2952010-07-19 19:09:10 -0700504 if (mInitCheck != NO_ERROR)
505 return mInitCheck;
506
Mathias Agopianae09d652011-11-01 17:37:49 -0700507 SensorInterface* sensor = mSensorMap.valueFor(handle);
508 if (!sensor)
509 return BAD_VALUE;
510
Mathias Agopian1cd70002010-07-21 15:59:50 -0700511 if (ns < 0)
512 return BAD_VALUE;
513
Mathias Agopian62569ec2011-11-07 21:21:47 -0800514 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
515 if (ns < minDelayNs) {
516 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700517 }
518
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700519 if (ns < MINIMUM_EVENTS_PERIOD)
520 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700521
Mathias Agopianf001c922010-11-11 17:58:51 -0800522 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700523}
524
525// ---------------------------------------------------------------------------
526
527SensorService::SensorRecord::SensorRecord(
528 const sp<SensorEventConnection>& connection)
529{
530 mConnections.add(connection);
531}
532
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700533bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700534 const sp<SensorEventConnection>& connection)
535{
536 if (mConnections.indexOf(connection) < 0) {
537 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700538 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700539 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700540 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700541}
542
543bool SensorService::SensorRecord::removeConnection(
544 const wp<SensorEventConnection>& connection)
545{
546 ssize_t index = mConnections.indexOf(connection);
547 if (index >= 0) {
548 mConnections.removeItemsAt(index, 1);
549 }
550 return mConnections.size() ? false : true;
551}
552
553// ---------------------------------------------------------------------------
554
555SensorService::SensorEventConnection::SensorEventConnection(
556 const sp<SensorService>& service)
Mathias Agopianb3989272011-10-20 18:42:02 -0700557 : mService(service), mChannel(new BitTube())
Mathias Agopianfc328812010-07-14 23:41:37 -0700558{
559}
560
561SensorService::SensorEventConnection::~SensorEventConnection()
562{
Steve Blocka5512372011-12-20 16:23:08 +0000563 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700564 mService->cleanupConnection(this);
565}
566
567void SensorService::SensorEventConnection::onFirstRef()
568{
569}
570
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700571bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800572 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700573 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800574 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700575 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700576 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700577 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700578}
579
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700580bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800581 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800582 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700583 return true;
584 }
585 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700586}
587
588bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800589 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800590 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700591}
592
593bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800594 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700595 return mSensorInfo.size() ? true : false;
596}
597
Mathias Agopianfc328812010-07-14 23:41:37 -0700598status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700599 sensors_event_t const* buffer, size_t numEvents,
600 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700601{
Mathias Agopiancf510012010-07-22 16:18:10 -0700602 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700603 size_t count = 0;
604 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800605 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700606 size_t i=0;
607 while (i<numEvents) {
608 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800609 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700610 do {
611 scratch[count++] = buffer[i++];
612 } while ((i<numEvents) && (buffer[i].sensor == curr));
613 } else {
614 i++;
615 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700616 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700617 } else {
618 scratch = const_cast<sensors_event_t *>(buffer);
619 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700620 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700621
Mathias Agopian907103b2012-04-02 18:38:02 -0700622 // NOTE: ASensorEvent and sensors_event_t are the same type
623 ssize_t size = SensorEventQueue::write(mChannel,
624 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700625 if (size == -EAGAIN) {
626 // the destination doesn't accept events anymore, it's probably
627 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000628 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700629 return size;
630 }
631
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700632 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700633}
634
Mathias Agopianb3989272011-10-20 18:42:02 -0700635sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700636{
637 return mChannel;
638}
639
640status_t SensorService::SensorEventConnection::enableDisable(
641 int handle, bool enabled)
642{
643 status_t err;
644 if (enabled) {
645 err = mService->enable(this, handle);
646 } else {
647 err = mService->disable(this, handle);
648 }
649 return err;
650}
651
652status_t SensorService::SensorEventConnection::setEventRate(
653 int handle, nsecs_t ns)
654{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700655 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700656}
657
658// ---------------------------------------------------------------------------
659}; // namespace android
660