blob: f1db2f555162620e37907def4725c5f749ae9da6 [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
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 Agopian451beee2010-07-19 15:03:55 -070027#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070028#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070029
30#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070031#include <binder/IServiceManager.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070032
33#include <gui/ISensorServer.h>
34#include <gui/ISensorEventConnection.h>
35
36#include <hardware/sensors.h>
37
38#include "SensorService.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080039#include "GravitySensor.h"
40#include "LinearAccelerationSensor.h"
41#include "RotationVectorSensor.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070042
43namespace android {
44// ---------------------------------------------------------------------------
45
Mathias Agopianfc328812010-07-14 23:41:37 -070046SensorService::SensorService()
Mathias Agopiane04a63b2011-05-19 16:21:32 -070047 : mDump("android.permission.DUMP"),
Mathias Agopian50df2952010-07-19 19:09:10 -070048 mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070049{
50}
51
52void SensorService::onFirstRef()
53{
Mathias Agopian50df2952010-07-19 19:09:10 -070054 LOGD("nuSensorService starting...");
55
Mathias Agopianf001c922010-11-11 17:58:51 -080056 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070057
Mathias Agopianf001c922010-11-11 17:58:51 -080058 if (dev.initCheck() == NO_ERROR) {
59 uint32_t virtualSensorsNeeds =
60 (1<<SENSOR_TYPE_GRAVITY) |
61 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
62 (1<<SENSOR_TYPE_ROTATION_VECTOR);
63 sensor_t const* list;
64 int count = dev.getSensorList(&list);
Mathias Agopian3560fb22010-07-22 21:24:39 -070065 mLastEventSeen.setCapacity(count);
Mathias Agopian50df2952010-07-19 19:09:10 -070066 for (int i=0 ; i<count ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -080067 registerSensor( new HardwareSensor(list[i]) );
68 switch (list[i].type) {
69 case SENSOR_TYPE_GRAVITY:
70 case SENSOR_TYPE_LINEAR_ACCELERATION:
71 case SENSOR_TYPE_ROTATION_VECTOR:
72 virtualSensorsNeeds &= ~(1<<list[i].type);
73 break;
Mathias Agopian50df2952010-07-19 19:09:10 -070074 }
75 }
Mathias Agopianfc328812010-07-14 23:41:37 -070076
Mathias Agopianf001c922010-11-11 17:58:51 -080077 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
78 registerVirtualSensor( new GravitySensor(list, count) );
Mathias Agopian50df2952010-07-19 19:09:10 -070079 }
Mathias Agopianf001c922010-11-11 17:58:51 -080080 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
81 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
82 }
83 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
84 registerVirtualSensor( new RotationVectorSensor(list, count) );
85 }
86
87 run("SensorService", PRIORITY_URGENT_DISPLAY);
88 mInitCheck = NO_ERROR;
Mathias Agopianfc328812010-07-14 23:41:37 -070089 }
Mathias Agopianfc328812010-07-14 23:41:37 -070090}
91
Mathias Agopianf001c922010-11-11 17:58:51 -080092void SensorService::registerSensor(SensorInterface* s)
93{
94 sensors_event_t event;
95 memset(&event, 0, sizeof(event));
96
97 const Sensor sensor(s->getSensor());
98 // add to the sensor list (returned to clients)
99 mSensorList.add(sensor);
100 // add to our handle->SensorInterface mapping
101 mSensorMap.add(sensor.getHandle(), s);
102 // create an entry in the mLastEventSeen array
103 mLastEventSeen.add(sensor.getHandle(), event);
104}
105
106void SensorService::registerVirtualSensor(SensorInterface* s)
107{
108 registerSensor(s);
109 mVirtualSensorList.add( s );
110}
111
Mathias Agopianfc328812010-07-14 23:41:37 -0700112SensorService::~SensorService()
113{
Mathias Agopianf001c922010-11-11 17:58:51 -0800114 for (size_t i=0 ; i<mSensorMap.size() ; i++)
115 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700116}
117
118status_t SensorService::dump(int fd, const Vector<String16>& args)
119{
120 const size_t SIZE = 1024;
121 char buffer[SIZE];
122 String8 result;
123 if (!mDump.checkCalling()) {
124 snprintf(buffer, SIZE, "Permission Denial: "
125 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
126 IPCThreadState::self()->getCallingPid(),
127 IPCThreadState::self()->getCallingUid());
128 result.append(buffer);
129 } else {
130 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700131 snprintf(buffer, SIZE, "Sensor List:\n");
132 result.append(buffer);
133 for (size_t i=0 ; i<mSensorList.size() ; i++) {
134 const Sensor& s(mSensorList[i]);
135 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopianf001c922010-11-11 17:58:51 -0800136 snprintf(buffer, SIZE, "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700137 s.getName().string(),
138 s.getVendor().string(),
139 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700140 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700141 e.data[0], e.data[1], e.data[2]);
142 result.append(buffer);
143 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800144 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700145
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700146 snprintf(buffer, SIZE, "%d active connections\n",
147 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700148 result.append(buffer);
149 snprintf(buffer, SIZE, "Active sensors:\n");
150 result.append(buffer);
151 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700152 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800153 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700154 getSensorName(handle).string(),
155 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700156 mActiveSensors.valueAt(i)->getNumConnections());
157 result.append(buffer);
158 }
159 }
160 write(fd, result.string(), result.size());
161 return NO_ERROR;
162}
163
164bool SensorService::threadLoop()
165{
166 LOGD("nuSensorService thread starting...");
167
Mathias Agopianf001c922010-11-11 17:58:51 -0800168 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
169 sensors_event_t buffer[numEventMax];
170 sensors_event_t scratch[numEventMax];
171 SensorDevice& device(SensorDevice::getInstance());
172 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700173
Mathias Agopianf001c922010-11-11 17:58:51 -0800174 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700175 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800176 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700177 if (count<0) {
178 LOGE("sensor poll failed (%s)", strerror(-count));
179 break;
180 }
181
Mathias Agopian94e8f682010-11-10 17:50:28 -0800182 recordLastValue(buffer, count);
183
Mathias Agopianf001c922010-11-11 17:58:51 -0800184 // handle virtual sensors
185 if (count && vcount) {
186 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
187 getActiveVirtualSensors());
188 const size_t activeVirtualSensorCount = virtualSensors.size();
189 if (activeVirtualSensorCount) {
190 size_t k = 0;
191 for (size_t i=0 ; i<size_t(count) ; i++) {
192 sensors_event_t const * const event = buffer;
193 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
194 sensors_event_t out;
195 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
196 buffer[count + k] = out;
197 k++;
198 }
199 }
200 }
201 if (k) {
202 // record the last synthesized values
203 recordLastValue(&buffer[count], k);
204 count += k;
205 // sort the buffer by time-stamps
206 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700207 }
208 }
209 }
210
Mathias Agopianf001c922010-11-11 17:58:51 -0800211 // send our events to clients...
212 const SortedVector< wp<SensorEventConnection> > activeConnections(
213 getActiveConnections());
214 size_t numConnections = activeConnections.size();
215 for (size_t i=0 ; i<numConnections ; i++) {
216 sp<SensorEventConnection> connection(
217 activeConnections[i].promote());
218 if (connection != 0) {
219 connection->sendEvents(buffer, count, scratch);
220 }
221 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700222 } while (count >= 0 || Thread::exitPending());
223
224 LOGW("Exiting SensorService::threadLoop!");
225 return false;
226}
227
Mathias Agopian94e8f682010-11-10 17:50:28 -0800228void SensorService::recordLastValue(
229 sensors_event_t const * buffer, size_t count)
230{
231 Mutex::Autolock _l(mLock);
232
233 // record the last event for each sensor
234 int32_t prev = buffer[0].sensor;
235 for (size_t i=1 ; i<count ; i++) {
236 // record the last event of each sensor type in this buffer
237 int32_t curr = buffer[i].sensor;
238 if (curr != prev) {
239 mLastEventSeen.editValueFor(prev) = buffer[i-1];
240 prev = curr;
241 }
242 }
243 mLastEventSeen.editValueFor(prev) = buffer[count-1];
244}
245
Mathias Agopianf001c922010-11-11 17:58:51 -0800246void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
247{
248 struct compar {
249 static int cmp(void const* lhs, void const* rhs) {
250 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
251 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
252 return r->timestamp - l->timestamp;
253 }
254 };
255 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
256}
257
Mathias Agopianfc328812010-07-14 23:41:37 -0700258SortedVector< wp<SensorService::SensorEventConnection> >
259SensorService::getActiveConnections() const
260{
261 Mutex::Autolock _l(mLock);
262 return mActiveConnections;
263}
264
Mathias Agopianf001c922010-11-11 17:58:51 -0800265DefaultKeyedVector<int, SensorInterface*>
266SensorService::getActiveVirtualSensors() const
267{
268 Mutex::Autolock _l(mLock);
269 return mActiveVirtualSensors;
270}
271
Mathias Agopian5d270722010-07-19 15:20:39 -0700272String8 SensorService::getSensorName(int handle) const {
273 size_t count = mSensorList.size();
274 for (size_t i=0 ; i<count ; i++) {
275 const Sensor& sensor(mSensorList[i]);
276 if (sensor.getHandle() == handle) {
277 return sensor.getName();
278 }
279 }
280 String8 result("unknown");
281 return result;
282}
283
Mathias Agopianfc328812010-07-14 23:41:37 -0700284Vector<Sensor> SensorService::getSensorList()
285{
286 return mSensorList;
287}
288
289sp<ISensorEventConnection> SensorService::createSensorEventConnection()
290{
291 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700292 return result;
293}
294
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800295void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700296{
297 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800298 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700299 size_t size = mActiveSensors.size();
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700300 LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700301 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800302 int handle = mActiveSensors.keyAt(i);
303 if (c->hasSensor(handle)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700304 LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800305 SensorInterface* sensor = mSensorMap.valueFor( handle );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700306 LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800307 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800308 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800309 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800310 }
311 SensorRecord* rec = mActiveSensors.valueAt(i);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700312 LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
313 LOGD_IF(DEBUG_CONNECTIONS,
314 "removing connection %p for sensor[%d].handle=0x%08x",
315 c, i, handle);
316
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800317 if (rec && rec->removeConnection(connection)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700318 LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700319 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800320 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700321 delete rec;
322 size--;
323 } else {
324 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700325 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700326 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700327 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700328}
329
330status_t SensorService::enable(const sp<SensorEventConnection>& connection,
331 int handle)
332{
Mathias Agopian50df2952010-07-19 19:09:10 -0700333 if (mInitCheck != NO_ERROR)
334 return mInitCheck;
335
Mathias Agopianfc328812010-07-14 23:41:37 -0700336 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800337 SensorInterface* sensor = mSensorMap.valueFor(handle);
338 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700339 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800340 SensorRecord* rec = mActiveSensors.valueFor(handle);
341 if (rec == 0) {
342 rec = new SensorRecord(connection);
343 mActiveSensors.add(handle, rec);
344 if (sensor->isVirtual()) {
345 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700346 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800347 } else {
348 if (rec->addConnection(connection)) {
349 // this sensor is already activated, but we are adding a
350 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800351 // known value of the requested sensor if it's not a
352 // "continuous" sensor.
353 if (sensor->getSensor().getMinDelay() == 0) {
354 sensors_event_t scratch;
355 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
356 if (event.version == sizeof(sensors_event_t)) {
357 connection->sendEvents(&event, 1);
358 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800359 }
360 }
361 }
362 if (err == NO_ERROR) {
363 // connection now active
364 if (connection->addSensor(handle)) {
365 // the sensor was added (which means it wasn't already there)
366 // so, see if this connection becomes active
367 if (mActiveConnections.indexOf(connection) < 0) {
368 mActiveConnections.add(connection);
369 }
370 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700371 }
372 }
373 return err;
374}
375
376status_t SensorService::disable(const sp<SensorEventConnection>& connection,
377 int handle)
378{
Mathias Agopian50df2952010-07-19 19:09:10 -0700379 if (mInitCheck != NO_ERROR)
380 return mInitCheck;
381
Mathias Agopianfc328812010-07-14 23:41:37 -0700382 status_t err = NO_ERROR;
383 Mutex::Autolock _l(mLock);
384 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700385 if (rec) {
386 // see if this connection becomes inactive
387 connection->removeSensor(handle);
388 if (connection->hasAnySensor() == false) {
389 mActiveConnections.remove(connection);
390 }
391 // see if this sensor becomes inactive
392 if (rec->removeConnection(connection)) {
393 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800394 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700395 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700396 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800397 SensorInterface* sensor = mSensorMap.valueFor(handle);
398 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700399 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700400 return err;
401}
402
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700403status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700404 int handle, nsecs_t ns)
405{
Mathias Agopian50df2952010-07-19 19:09:10 -0700406 if (mInitCheck != NO_ERROR)
407 return mInitCheck;
408
Mathias Agopian1cd70002010-07-21 15:59:50 -0700409 if (ns < 0)
410 return BAD_VALUE;
411
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700412 if (ns < MINIMUM_EVENTS_PERIOD)
413 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700414
Mathias Agopianf001c922010-11-11 17:58:51 -0800415 SensorInterface* sensor = mSensorMap.valueFor(handle);
416 if (!sensor) return BAD_VALUE;
417 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700418}
419
420// ---------------------------------------------------------------------------
421
422SensorService::SensorRecord::SensorRecord(
423 const sp<SensorEventConnection>& connection)
424{
425 mConnections.add(connection);
426}
427
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700428bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700429 const sp<SensorEventConnection>& connection)
430{
431 if (mConnections.indexOf(connection) < 0) {
432 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700433 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700434 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700435 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700436}
437
438bool SensorService::SensorRecord::removeConnection(
439 const wp<SensorEventConnection>& connection)
440{
441 ssize_t index = mConnections.indexOf(connection);
442 if (index >= 0) {
443 mConnections.removeItemsAt(index, 1);
444 }
445 return mConnections.size() ? false : true;
446}
447
448// ---------------------------------------------------------------------------
449
450SensorService::SensorEventConnection::SensorEventConnection(
451 const sp<SensorService>& service)
452 : mService(service), mChannel(new SensorChannel())
453{
454}
455
456SensorService::SensorEventConnection::~SensorEventConnection()
457{
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700458 LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700459 mService->cleanupConnection(this);
460}
461
462void SensorService::SensorEventConnection::onFirstRef()
463{
464}
465
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700466bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800467 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800468 if (mSensorInfo.indexOf(handle) <= 0) {
469 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700470 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700471 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700472 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700473}
474
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700475bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800476 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800477 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700478 return true;
479 }
480 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700481}
482
483bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800484 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800485 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700486}
487
488bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800489 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700490 return mSensorInfo.size() ? true : false;
491}
492
Mathias Agopianfc328812010-07-14 23:41:37 -0700493status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700494 sensors_event_t const* buffer, size_t numEvents,
495 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700496{
Mathias Agopiancf510012010-07-22 16:18:10 -0700497 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700498 size_t count = 0;
499 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800500 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700501 size_t i=0;
502 while (i<numEvents) {
503 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800504 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700505 do {
506 scratch[count++] = buffer[i++];
507 } while ((i<numEvents) && (buffer[i].sensor == curr));
508 } else {
509 i++;
510 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700511 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700512 } else {
513 scratch = const_cast<sensors_event_t *>(buffer);
514 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700515 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700516
Mathias Agopian3560fb22010-07-22 21:24:39 -0700517 if (count == 0)
518 return 0;
519
Mathias Agopiancf510012010-07-22 16:18:10 -0700520 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700521 if (size == -EAGAIN) {
522 // the destination doesn't accept events anymore, it's probably
523 // full. For now, we just drop the events on the floor.
524 LOGW("dropping %d events on the floor", count);
525 return size;
526 }
527
528 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
529 count, strerror(-size));
530
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700531 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700532}
533
534sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
535{
536 return mChannel;
537}
538
539status_t SensorService::SensorEventConnection::enableDisable(
540 int handle, bool enabled)
541{
542 status_t err;
543 if (enabled) {
544 err = mService->enable(this, handle);
545 } else {
546 err = mService->disable(this, handle);
547 }
548 return err;
549}
550
551status_t SensorService::SensorEventConnection::setEventRate(
552 int handle, nsecs_t ns)
553{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700554 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700555}
556
557// ---------------------------------------------------------------------------
558}; // namespace android
559