blob: 0ae79294f9a7fba2750423d9072426fb0cbcb13c [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 Agopianfc328812010-07-14 23:41:37 -070034
35#include <gui/ISensorServer.h>
36#include <gui/ISensorEventConnection.h>
37
38#include <hardware/sensors.h>
39
Mathias Agopian984826c2011-05-17 22:54:42 -070040#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080041#include "GravitySensor.h"
42#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070043#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080044#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070045#include "SensorFusion.h"
46#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070047
48namespace android {
49// ---------------------------------------------------------------------------
50
Mathias Agopian33015422011-05-27 18:18:13 -070051/*
52 * Notes:
53 *
54 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070055 * - run mag sensor from time to time to force calibration
56 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
57 *
58 */
59
Mathias Agopianfc328812010-07-14 23:41:37 -070060SensorService::SensorService()
Mathias Agopiane04a63b2011-05-19 16:21:32 -070061 : mDump("android.permission.DUMP"),
Mathias Agopian50df2952010-07-19 19:09:10 -070062 mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070063{
64}
65
66void SensorService::onFirstRef()
67{
Mathias Agopian50df2952010-07-19 19:09:10 -070068 LOGD("nuSensorService starting...");
69
Mathias Agopianf001c922010-11-11 17:58:51 -080070 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070071
Mathias Agopianf001c922010-11-11 17:58:51 -080072 if (dev.initCheck() == NO_ERROR) {
Mathias Agopian010e4222011-06-08 20:06:50 -070073 ssize_t orientationIndex = -1;
Mathias Agopian33015422011-05-27 18:18:13 -070074 bool hasGyro = false;
Mathias Agopianf001c922010-11-11 17:58:51 -080075 uint32_t virtualSensorsNeeds =
76 (1<<SENSOR_TYPE_GRAVITY) |
77 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
78 (1<<SENSOR_TYPE_ROTATION_VECTOR);
79 sensor_t const* list;
80 int count = dev.getSensorList(&list);
Mathias Agopian3560fb22010-07-22 21:24:39 -070081 mLastEventSeen.setCapacity(count);
Mathias Agopian50df2952010-07-19 19:09:10 -070082 for (int i=0 ; i<count ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -080083 registerSensor( new HardwareSensor(list[i]) );
84 switch (list[i].type) {
Mathias Agopian010e4222011-06-08 20:06:50 -070085 case SENSOR_TYPE_ORIENTATION:
86 orientationIndex = i;
87 break;
Mathias Agopian33015422011-05-27 18:18:13 -070088 case SENSOR_TYPE_GYROSCOPE:
89 hasGyro = true;
90 break;
Mathias Agopianf001c922010-11-11 17:58:51 -080091 case SENSOR_TYPE_GRAVITY:
92 case SENSOR_TYPE_LINEAR_ACCELERATION:
93 case SENSOR_TYPE_ROTATION_VECTOR:
94 virtualSensorsNeeds &= ~(1<<list[i].type);
95 break;
Mathias Agopian50df2952010-07-19 19:09:10 -070096 }
97 }
Mathias Agopianfc328812010-07-14 23:41:37 -070098
Mathias Agopian984826c2011-05-17 22:54:42 -070099 // it's safe to instantiate the SensorFusion object here
100 // (it wants to be instantiated after h/w sensors have been
101 // registered)
102 const SensorFusion& fusion(SensorFusion::getInstance());
103
Mathias Agopian33015422011-05-27 18:18:13 -0700104 if (hasGyro) {
105 // Always instantiate Android's virtual sensors. Since they are
106 // instantiated behind sensors from the HAL, they won't
107 // interfere with applications, unless they looks specifically
108 // for them (by name).
Mathias Agopian984826c2011-05-17 22:54:42 -0700109
Mathias Agopian33015422011-05-27 18:18:13 -0700110 registerVirtualSensor( new RotationVectorSensor() );
111 registerVirtualSensor( new GravitySensor(list, count) );
112 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian984826c2011-05-17 22:54:42 -0700113
Mathias Agopian33015422011-05-27 18:18:13 -0700114 // these are optional
Mathias Agopian984826c2011-05-17 22:54:42 -0700115 registerVirtualSensor( new OrientationSensor() );
116 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33015422011-05-27 18:18:13 -0700117
118 // virtual debugging sensors...
119 char value[PROPERTY_VALUE_MAX];
120 property_get("debug.sensors", value, "0");
121 if (atoi(value)) {
122 registerVirtualSensor( new GyroDriftSensor() );
123 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800124 }
125
Mathias Agopian010e4222011-06-08 20:06:50 -0700126 // build the sensor list returned to users
127 mUserSensorList = mSensorList;
128 if (hasGyro &&
129 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
130 // if we have the fancy sensor fusion, and it's not provided by the
131 // HAL, use our own (fused) orientation sensor by removing the
132 // HAL supplied one form the user list.
133 if (orientationIndex >= 0) {
134 mUserSensorList.removeItemsAt(orientationIndex);
135 }
136 }
137
Mathias Agopianf001c922010-11-11 17:58:51 -0800138 run("SensorService", PRIORITY_URGENT_DISPLAY);
139 mInitCheck = NO_ERROR;
Mathias Agopianfc328812010-07-14 23:41:37 -0700140 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700141}
142
Mathias Agopianf001c922010-11-11 17:58:51 -0800143void SensorService::registerSensor(SensorInterface* s)
144{
145 sensors_event_t event;
146 memset(&event, 0, sizeof(event));
147
148 const Sensor sensor(s->getSensor());
149 // add to the sensor list (returned to clients)
150 mSensorList.add(sensor);
151 // add to our handle->SensorInterface mapping
152 mSensorMap.add(sensor.getHandle(), s);
153 // create an entry in the mLastEventSeen array
154 mLastEventSeen.add(sensor.getHandle(), event);
155}
156
157void SensorService::registerVirtualSensor(SensorInterface* s)
158{
159 registerSensor(s);
160 mVirtualSensorList.add( s );
161}
162
Mathias Agopianfc328812010-07-14 23:41:37 -0700163SensorService::~SensorService()
164{
Mathias Agopianf001c922010-11-11 17:58:51 -0800165 for (size_t i=0 ; i<mSensorMap.size() ; i++)
166 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700167}
168
169status_t SensorService::dump(int fd, const Vector<String16>& args)
170{
171 const size_t SIZE = 1024;
172 char buffer[SIZE];
173 String8 result;
174 if (!mDump.checkCalling()) {
175 snprintf(buffer, SIZE, "Permission Denial: "
176 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
177 IPCThreadState::self()->getCallingPid(),
178 IPCThreadState::self()->getCallingUid());
179 result.append(buffer);
180 } else {
181 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700182 snprintf(buffer, SIZE, "Sensor List:\n");
183 result.append(buffer);
184 for (size_t i=0 ; i<mSensorList.size() ; i++) {
185 const Sensor& s(mSensorList[i]);
186 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700187 snprintf(buffer, SIZE,
188 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
189 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700190 s.getName().string(),
191 s.getVendor().string(),
192 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700193 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700194 e.data[0], e.data[1], e.data[2]);
195 result.append(buffer);
196 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700197 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800198 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700199
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700200 snprintf(buffer, SIZE, "%d active connections\n",
201 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700202 result.append(buffer);
203 snprintf(buffer, SIZE, "Active sensors:\n");
204 result.append(buffer);
205 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700206 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800207 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700208 getSensorName(handle).string(),
209 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700210 mActiveSensors.valueAt(i)->getNumConnections());
211 result.append(buffer);
212 }
213 }
214 write(fd, result.string(), result.size());
215 return NO_ERROR;
216}
217
218bool SensorService::threadLoop()
219{
220 LOGD("nuSensorService thread starting...");
221
Mathias Agopianf001c922010-11-11 17:58:51 -0800222 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
223 sensors_event_t buffer[numEventMax];
224 sensors_event_t scratch[numEventMax];
225 SensorDevice& device(SensorDevice::getInstance());
226 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700227
Mathias Agopianf001c922010-11-11 17:58:51 -0800228 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700229 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800230 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700231 if (count<0) {
232 LOGE("sensor poll failed (%s)", strerror(-count));
233 break;
234 }
235
Mathias Agopian94e8f682010-11-10 17:50:28 -0800236 recordLastValue(buffer, count);
237
Mathias Agopianf001c922010-11-11 17:58:51 -0800238 // handle virtual sensors
239 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700240 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800241 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
242 getActiveVirtualSensors());
243 const size_t activeVirtualSensorCount = virtualSensors.size();
244 if (activeVirtualSensorCount) {
245 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700246 SensorFusion& fusion(SensorFusion::getInstance());
247 if (fusion.isEnabled()) {
248 for (size_t i=0 ; i<size_t(count) ; i++) {
249 fusion.process(event[i]);
250 }
251 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800252 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800253 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
254 sensors_event_t out;
255 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
256 buffer[count + k] = out;
257 k++;
258 }
259 }
260 }
261 if (k) {
262 // record the last synthesized values
263 recordLastValue(&buffer[count], k);
264 count += k;
265 // sort the buffer by time-stamps
266 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700267 }
268 }
269 }
270
Mathias Agopianf001c922010-11-11 17:58:51 -0800271 // send our events to clients...
272 const SortedVector< wp<SensorEventConnection> > activeConnections(
273 getActiveConnections());
274 size_t numConnections = activeConnections.size();
275 for (size_t i=0 ; i<numConnections ; i++) {
276 sp<SensorEventConnection> connection(
277 activeConnections[i].promote());
278 if (connection != 0) {
279 connection->sendEvents(buffer, count, scratch);
280 }
281 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700282 } while (count >= 0 || Thread::exitPending());
283
284 LOGW("Exiting SensorService::threadLoop!");
285 return false;
286}
287
Mathias Agopian94e8f682010-11-10 17:50:28 -0800288void SensorService::recordLastValue(
289 sensors_event_t const * buffer, size_t count)
290{
291 Mutex::Autolock _l(mLock);
292
293 // record the last event for each sensor
294 int32_t prev = buffer[0].sensor;
295 for (size_t i=1 ; i<count ; i++) {
296 // record the last event of each sensor type in this buffer
297 int32_t curr = buffer[i].sensor;
298 if (curr != prev) {
299 mLastEventSeen.editValueFor(prev) = buffer[i-1];
300 prev = curr;
301 }
302 }
303 mLastEventSeen.editValueFor(prev) = buffer[count-1];
304}
305
Mathias Agopianf001c922010-11-11 17:58:51 -0800306void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
307{
308 struct compar {
309 static int cmp(void const* lhs, void const* rhs) {
310 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
311 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
312 return r->timestamp - l->timestamp;
313 }
314 };
315 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
316}
317
Mathias Agopianfc328812010-07-14 23:41:37 -0700318SortedVector< wp<SensorService::SensorEventConnection> >
319SensorService::getActiveConnections() const
320{
321 Mutex::Autolock _l(mLock);
322 return mActiveConnections;
323}
324
Mathias Agopianf001c922010-11-11 17:58:51 -0800325DefaultKeyedVector<int, SensorInterface*>
326SensorService::getActiveVirtualSensors() const
327{
328 Mutex::Autolock _l(mLock);
329 return mActiveVirtualSensors;
330}
331
Mathias Agopian5d270722010-07-19 15:20:39 -0700332String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700333 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700334 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700335 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700336 if (sensor.getHandle() == handle) {
337 return sensor.getName();
338 }
339 }
340 String8 result("unknown");
341 return result;
342}
343
Mathias Agopianfc328812010-07-14 23:41:37 -0700344Vector<Sensor> SensorService::getSensorList()
345{
Mathias Agopian010e4222011-06-08 20:06:50 -0700346 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700347}
348
349sp<ISensorEventConnection> SensorService::createSensorEventConnection()
350{
351 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700352 return result;
353}
354
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800355void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700356{
357 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800358 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700359 size_t size = mActiveSensors.size();
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700360 LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700361 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800362 int handle = mActiveSensors.keyAt(i);
363 if (c->hasSensor(handle)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700364 LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800365 SensorInterface* sensor = mSensorMap.valueFor( handle );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700366 LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800367 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800368 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800369 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800370 }
371 SensorRecord* rec = mActiveSensors.valueAt(i);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700372 LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
373 LOGD_IF(DEBUG_CONNECTIONS,
374 "removing connection %p for sensor[%d].handle=0x%08x",
375 c, i, handle);
376
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800377 if (rec && rec->removeConnection(connection)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700378 LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700379 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800380 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700381 delete rec;
382 size--;
383 } else {
384 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700385 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700386 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700387 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700388}
389
390status_t SensorService::enable(const sp<SensorEventConnection>& connection,
391 int handle)
392{
Mathias Agopian50df2952010-07-19 19:09:10 -0700393 if (mInitCheck != NO_ERROR)
394 return mInitCheck;
395
Mathias Agopianfc328812010-07-14 23:41:37 -0700396 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800397 SensorInterface* sensor = mSensorMap.valueFor(handle);
398 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700399 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800400 SensorRecord* rec = mActiveSensors.valueFor(handle);
401 if (rec == 0) {
402 rec = new SensorRecord(connection);
403 mActiveSensors.add(handle, rec);
404 if (sensor->isVirtual()) {
405 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700406 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800407 } else {
408 if (rec->addConnection(connection)) {
409 // this sensor is already activated, but we are adding a
410 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800411 // known value of the requested sensor if it's not a
412 // "continuous" sensor.
413 if (sensor->getSensor().getMinDelay() == 0) {
414 sensors_event_t scratch;
415 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
416 if (event.version == sizeof(sensors_event_t)) {
417 connection->sendEvents(&event, 1);
418 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800419 }
420 }
421 }
422 if (err == NO_ERROR) {
423 // connection now active
424 if (connection->addSensor(handle)) {
425 // the sensor was added (which means it wasn't already there)
426 // so, see if this connection becomes active
427 if (mActiveConnections.indexOf(connection) < 0) {
428 mActiveConnections.add(connection);
429 }
430 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700431 }
432 }
433 return err;
434}
435
436status_t SensorService::disable(const sp<SensorEventConnection>& connection,
437 int handle)
438{
Mathias Agopian50df2952010-07-19 19:09:10 -0700439 if (mInitCheck != NO_ERROR)
440 return mInitCheck;
441
Mathias Agopianfc328812010-07-14 23:41:37 -0700442 status_t err = NO_ERROR;
443 Mutex::Autolock _l(mLock);
444 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700445 if (rec) {
446 // see if this connection becomes inactive
447 connection->removeSensor(handle);
448 if (connection->hasAnySensor() == false) {
449 mActiveConnections.remove(connection);
450 }
451 // see if this sensor becomes inactive
452 if (rec->removeConnection(connection)) {
453 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800454 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700455 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700456 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800457 SensorInterface* sensor = mSensorMap.valueFor(handle);
458 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700459 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700460 return err;
461}
462
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700463status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700464 int handle, nsecs_t ns)
465{
Mathias Agopian50df2952010-07-19 19:09:10 -0700466 if (mInitCheck != NO_ERROR)
467 return mInitCheck;
468
Mathias Agopian1cd70002010-07-21 15:59:50 -0700469 if (ns < 0)
470 return BAD_VALUE;
471
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700472 if (ns < MINIMUM_EVENTS_PERIOD)
473 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700474
Mathias Agopianf001c922010-11-11 17:58:51 -0800475 SensorInterface* sensor = mSensorMap.valueFor(handle);
476 if (!sensor) return BAD_VALUE;
477 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700478}
479
480// ---------------------------------------------------------------------------
481
482SensorService::SensorRecord::SensorRecord(
483 const sp<SensorEventConnection>& connection)
484{
485 mConnections.add(connection);
486}
487
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700488bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700489 const sp<SensorEventConnection>& connection)
490{
491 if (mConnections.indexOf(connection) < 0) {
492 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700493 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700494 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700495 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700496}
497
498bool SensorService::SensorRecord::removeConnection(
499 const wp<SensorEventConnection>& connection)
500{
501 ssize_t index = mConnections.indexOf(connection);
502 if (index >= 0) {
503 mConnections.removeItemsAt(index, 1);
504 }
505 return mConnections.size() ? false : true;
506}
507
508// ---------------------------------------------------------------------------
509
510SensorService::SensorEventConnection::SensorEventConnection(
511 const sp<SensorService>& service)
512 : mService(service), mChannel(new SensorChannel())
513{
514}
515
516SensorService::SensorEventConnection::~SensorEventConnection()
517{
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700518 LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700519 mService->cleanupConnection(this);
520}
521
522void SensorService::SensorEventConnection::onFirstRef()
523{
524}
525
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700526bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800527 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800528 if (mSensorInfo.indexOf(handle) <= 0) {
529 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700530 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700531 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700532 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700533}
534
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700535bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800536 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800537 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700538 return true;
539 }
540 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700541}
542
543bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800544 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800545 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700546}
547
548bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800549 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700550 return mSensorInfo.size() ? true : false;
551}
552
Mathias Agopianfc328812010-07-14 23:41:37 -0700553status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700554 sensors_event_t const* buffer, size_t numEvents,
555 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700556{
Mathias Agopiancf510012010-07-22 16:18:10 -0700557 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700558 size_t count = 0;
559 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800560 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700561 size_t i=0;
562 while (i<numEvents) {
563 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800564 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700565 do {
566 scratch[count++] = buffer[i++];
567 } while ((i<numEvents) && (buffer[i].sensor == curr));
568 } else {
569 i++;
570 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700571 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700572 } else {
573 scratch = const_cast<sensors_event_t *>(buffer);
574 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700575 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700576
Mathias Agopian3560fb22010-07-22 21:24:39 -0700577 if (count == 0)
578 return 0;
579
Mathias Agopiancf510012010-07-22 16:18:10 -0700580 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700581 if (size == -EAGAIN) {
582 // the destination doesn't accept events anymore, it's probably
583 // full. For now, we just drop the events on the floor.
584 LOGW("dropping %d events on the floor", count);
585 return size;
586 }
587
588 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
589 count, strerror(-size));
590
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700591 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700592}
593
594sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
595{
596 return mChannel;
597}
598
599status_t SensorService::SensorEventConnection::enableDisable(
600 int handle, bool enabled)
601{
602 status_t err;
603 if (enabled) {
604 err = mService->enable(this, handle);
605 } else {
606 err = mService->disable(this, handle);
607 }
608 return err;
609}
610
611status_t SensorService::SensorEventConnection::setEventRate(
612 int handle, nsecs_t ns)
613{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700614 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700615}
616
617// ---------------------------------------------------------------------------
618}; // namespace android
619