blob: 5b86d10d0492b68d25b4cc8203995f4f11086f6e [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
Mathias Agopian984826c2011-05-17 22:54:42 -070038#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080039#include "GravitySensor.h"
40#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070041#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080042#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070043#include "SensorFusion.h"
44#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070045
46namespace android {
47// ---------------------------------------------------------------------------
48
Mathias Agopianfc328812010-07-14 23:41:37 -070049SensorService::SensorService()
Mathias Agopiane04a63b2011-05-19 16:21:32 -070050 : mDump("android.permission.DUMP"),
Mathias Agopian50df2952010-07-19 19:09:10 -070051 mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070052{
53}
54
55void SensorService::onFirstRef()
56{
Mathias Agopian50df2952010-07-19 19:09:10 -070057 LOGD("nuSensorService starting...");
58
Mathias Agopianf001c922010-11-11 17:58:51 -080059 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070060
Mathias Agopianf001c922010-11-11 17:58:51 -080061 if (dev.initCheck() == NO_ERROR) {
62 uint32_t virtualSensorsNeeds =
63 (1<<SENSOR_TYPE_GRAVITY) |
64 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
65 (1<<SENSOR_TYPE_ROTATION_VECTOR);
66 sensor_t const* list;
67 int count = dev.getSensorList(&list);
Mathias Agopian3560fb22010-07-22 21:24:39 -070068 mLastEventSeen.setCapacity(count);
Mathias Agopian50df2952010-07-19 19:09:10 -070069 for (int i=0 ; i<count ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -080070 registerSensor( new HardwareSensor(list[i]) );
71 switch (list[i].type) {
72 case SENSOR_TYPE_GRAVITY:
73 case SENSOR_TYPE_LINEAR_ACCELERATION:
74 case SENSOR_TYPE_ROTATION_VECTOR:
75 virtualSensorsNeeds &= ~(1<<list[i].type);
76 break;
Mathias Agopian50df2952010-07-19 19:09:10 -070077 }
78 }
Mathias Agopianfc328812010-07-14 23:41:37 -070079
Mathias Agopian984826c2011-05-17 22:54:42 -070080 // it's safe to instantiate the SensorFusion object here
81 // (it wants to be instantiated after h/w sensors have been
82 // registered)
83 const SensorFusion& fusion(SensorFusion::getInstance());
84
85 // Always instantiate Android's virtual sensors. Since they are
86 // instantiated behind sensors from the HAL, they won't
87 // interfere with applications, unless they looks specifically
88 // for them (by name).
89
90 registerVirtualSensor( new RotationVectorSensor() );
91 registerVirtualSensor( new GravitySensor(list, count) );
92 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
93
94 // if we have a gyro, we have the option of enabling these
95 // "better" orientation and gyro sensors
96 if (fusion.hasGyro()) {
97 // FIXME: OrientationSensor buggy when not pointing north
98 registerVirtualSensor( new OrientationSensor() );
99 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopianf001c922010-11-11 17:58:51 -0800100 }
101
102 run("SensorService", PRIORITY_URGENT_DISPLAY);
103 mInitCheck = NO_ERROR;
Mathias Agopianfc328812010-07-14 23:41:37 -0700104 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700105}
106
Mathias Agopianf001c922010-11-11 17:58:51 -0800107void SensorService::registerSensor(SensorInterface* s)
108{
109 sensors_event_t event;
110 memset(&event, 0, sizeof(event));
111
112 const Sensor sensor(s->getSensor());
113 // add to the sensor list (returned to clients)
114 mSensorList.add(sensor);
115 // add to our handle->SensorInterface mapping
116 mSensorMap.add(sensor.getHandle(), s);
117 // create an entry in the mLastEventSeen array
118 mLastEventSeen.add(sensor.getHandle(), event);
119}
120
121void SensorService::registerVirtualSensor(SensorInterface* s)
122{
123 registerSensor(s);
124 mVirtualSensorList.add( s );
125}
126
Mathias Agopianfc328812010-07-14 23:41:37 -0700127SensorService::~SensorService()
128{
Mathias Agopianf001c922010-11-11 17:58:51 -0800129 for (size_t i=0 ; i<mSensorMap.size() ; i++)
130 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700131}
132
133status_t SensorService::dump(int fd, const Vector<String16>& args)
134{
135 const size_t SIZE = 1024;
136 char buffer[SIZE];
137 String8 result;
138 if (!mDump.checkCalling()) {
139 snprintf(buffer, SIZE, "Permission Denial: "
140 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
141 IPCThreadState::self()->getCallingPid(),
142 IPCThreadState::self()->getCallingUid());
143 result.append(buffer);
144 } else {
145 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700146 snprintf(buffer, SIZE, "Sensor List:\n");
147 result.append(buffer);
148 for (size_t i=0 ; i<mSensorList.size() ; i++) {
149 const Sensor& s(mSensorList[i]);
150 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700151 snprintf(buffer, SIZE,
152 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
153 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700154 s.getName().string(),
155 s.getVendor().string(),
156 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700157 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700158 e.data[0], e.data[1], e.data[2]);
159 result.append(buffer);
160 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700161 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800162 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700163
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700164 snprintf(buffer, SIZE, "%d active connections\n",
165 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700166 result.append(buffer);
167 snprintf(buffer, SIZE, "Active sensors:\n");
168 result.append(buffer);
169 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700170 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800171 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700172 getSensorName(handle).string(),
173 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700174 mActiveSensors.valueAt(i)->getNumConnections());
175 result.append(buffer);
176 }
177 }
178 write(fd, result.string(), result.size());
179 return NO_ERROR;
180}
181
182bool SensorService::threadLoop()
183{
184 LOGD("nuSensorService thread starting...");
185
Mathias Agopianf001c922010-11-11 17:58:51 -0800186 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
187 sensors_event_t buffer[numEventMax];
188 sensors_event_t scratch[numEventMax];
189 SensorDevice& device(SensorDevice::getInstance());
190 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700191
Mathias Agopianf001c922010-11-11 17:58:51 -0800192 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700193 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800194 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700195 if (count<0) {
196 LOGE("sensor poll failed (%s)", strerror(-count));
197 break;
198 }
199
Mathias Agopian94e8f682010-11-10 17:50:28 -0800200 recordLastValue(buffer, count);
201
Mathias Agopianf001c922010-11-11 17:58:51 -0800202 // handle virtual sensors
203 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700204 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800205 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
206 getActiveVirtualSensors());
207 const size_t activeVirtualSensorCount = virtualSensors.size();
208 if (activeVirtualSensorCount) {
209 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700210 SensorFusion& fusion(SensorFusion::getInstance());
211 if (fusion.isEnabled()) {
212 for (size_t i=0 ; i<size_t(count) ; i++) {
213 fusion.process(event[i]);
214 }
215 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800216 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800217 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
218 sensors_event_t out;
219 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
220 buffer[count + k] = out;
221 k++;
222 }
223 }
224 }
225 if (k) {
226 // record the last synthesized values
227 recordLastValue(&buffer[count], k);
228 count += k;
229 // sort the buffer by time-stamps
230 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700231 }
232 }
233 }
234
Mathias Agopianf001c922010-11-11 17:58:51 -0800235 // send our events to clients...
236 const SortedVector< wp<SensorEventConnection> > activeConnections(
237 getActiveConnections());
238 size_t numConnections = activeConnections.size();
239 for (size_t i=0 ; i<numConnections ; i++) {
240 sp<SensorEventConnection> connection(
241 activeConnections[i].promote());
242 if (connection != 0) {
243 connection->sendEvents(buffer, count, scratch);
244 }
245 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700246 } while (count >= 0 || Thread::exitPending());
247
248 LOGW("Exiting SensorService::threadLoop!");
249 return false;
250}
251
Mathias Agopian94e8f682010-11-10 17:50:28 -0800252void SensorService::recordLastValue(
253 sensors_event_t const * buffer, size_t count)
254{
255 Mutex::Autolock _l(mLock);
256
257 // record the last event for each sensor
258 int32_t prev = buffer[0].sensor;
259 for (size_t i=1 ; i<count ; i++) {
260 // record the last event of each sensor type in this buffer
261 int32_t curr = buffer[i].sensor;
262 if (curr != prev) {
263 mLastEventSeen.editValueFor(prev) = buffer[i-1];
264 prev = curr;
265 }
266 }
267 mLastEventSeen.editValueFor(prev) = buffer[count-1];
268}
269
Mathias Agopianf001c922010-11-11 17:58:51 -0800270void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
271{
272 struct compar {
273 static int cmp(void const* lhs, void const* rhs) {
274 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
275 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
276 return r->timestamp - l->timestamp;
277 }
278 };
279 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
280}
281
Mathias Agopianfc328812010-07-14 23:41:37 -0700282SortedVector< wp<SensorService::SensorEventConnection> >
283SensorService::getActiveConnections() const
284{
285 Mutex::Autolock _l(mLock);
286 return mActiveConnections;
287}
288
Mathias Agopianf001c922010-11-11 17:58:51 -0800289DefaultKeyedVector<int, SensorInterface*>
290SensorService::getActiveVirtualSensors() const
291{
292 Mutex::Autolock _l(mLock);
293 return mActiveVirtualSensors;
294}
295
Mathias Agopian5d270722010-07-19 15:20:39 -0700296String8 SensorService::getSensorName(int handle) const {
297 size_t count = mSensorList.size();
298 for (size_t i=0 ; i<count ; i++) {
299 const Sensor& sensor(mSensorList[i]);
300 if (sensor.getHandle() == handle) {
301 return sensor.getName();
302 }
303 }
304 String8 result("unknown");
305 return result;
306}
307
Mathias Agopianfc328812010-07-14 23:41:37 -0700308Vector<Sensor> SensorService::getSensorList()
309{
310 return mSensorList;
311}
312
313sp<ISensorEventConnection> SensorService::createSensorEventConnection()
314{
315 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700316 return result;
317}
318
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800319void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700320{
321 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800322 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700323 size_t size = mActiveSensors.size();
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700324 LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700325 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800326 int handle = mActiveSensors.keyAt(i);
327 if (c->hasSensor(handle)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700328 LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800329 SensorInterface* sensor = mSensorMap.valueFor( handle );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700330 LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800331 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800332 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800333 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800334 }
335 SensorRecord* rec = mActiveSensors.valueAt(i);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700336 LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
337 LOGD_IF(DEBUG_CONNECTIONS,
338 "removing connection %p for sensor[%d].handle=0x%08x",
339 c, i, handle);
340
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800341 if (rec && rec->removeConnection(connection)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700342 LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700343 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800344 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700345 delete rec;
346 size--;
347 } else {
348 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700349 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700350 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700351 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700352}
353
354status_t SensorService::enable(const sp<SensorEventConnection>& connection,
355 int handle)
356{
Mathias Agopian50df2952010-07-19 19:09:10 -0700357 if (mInitCheck != NO_ERROR)
358 return mInitCheck;
359
Mathias Agopianfc328812010-07-14 23:41:37 -0700360 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800361 SensorInterface* sensor = mSensorMap.valueFor(handle);
362 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700363 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800364 SensorRecord* rec = mActiveSensors.valueFor(handle);
365 if (rec == 0) {
366 rec = new SensorRecord(connection);
367 mActiveSensors.add(handle, rec);
368 if (sensor->isVirtual()) {
369 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700370 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800371 } else {
372 if (rec->addConnection(connection)) {
373 // this sensor is already activated, but we are adding a
374 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800375 // known value of the requested sensor if it's not a
376 // "continuous" sensor.
377 if (sensor->getSensor().getMinDelay() == 0) {
378 sensors_event_t scratch;
379 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
380 if (event.version == sizeof(sensors_event_t)) {
381 connection->sendEvents(&event, 1);
382 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800383 }
384 }
385 }
386 if (err == NO_ERROR) {
387 // connection now active
388 if (connection->addSensor(handle)) {
389 // the sensor was added (which means it wasn't already there)
390 // so, see if this connection becomes active
391 if (mActiveConnections.indexOf(connection) < 0) {
392 mActiveConnections.add(connection);
393 }
394 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700395 }
396 }
397 return err;
398}
399
400status_t SensorService::disable(const sp<SensorEventConnection>& connection,
401 int handle)
402{
Mathias Agopian50df2952010-07-19 19:09:10 -0700403 if (mInitCheck != NO_ERROR)
404 return mInitCheck;
405
Mathias Agopianfc328812010-07-14 23:41:37 -0700406 status_t err = NO_ERROR;
407 Mutex::Autolock _l(mLock);
408 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700409 if (rec) {
410 // see if this connection becomes inactive
411 connection->removeSensor(handle);
412 if (connection->hasAnySensor() == false) {
413 mActiveConnections.remove(connection);
414 }
415 // see if this sensor becomes inactive
416 if (rec->removeConnection(connection)) {
417 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800418 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700419 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700420 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800421 SensorInterface* sensor = mSensorMap.valueFor(handle);
422 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700423 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700424 return err;
425}
426
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700427status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700428 int handle, nsecs_t ns)
429{
Mathias Agopian50df2952010-07-19 19:09:10 -0700430 if (mInitCheck != NO_ERROR)
431 return mInitCheck;
432
Mathias Agopian1cd70002010-07-21 15:59:50 -0700433 if (ns < 0)
434 return BAD_VALUE;
435
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700436 if (ns < MINIMUM_EVENTS_PERIOD)
437 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700438
Mathias Agopianf001c922010-11-11 17:58:51 -0800439 SensorInterface* sensor = mSensorMap.valueFor(handle);
440 if (!sensor) return BAD_VALUE;
441 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700442}
443
444// ---------------------------------------------------------------------------
445
446SensorService::SensorRecord::SensorRecord(
447 const sp<SensorEventConnection>& connection)
448{
449 mConnections.add(connection);
450}
451
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700452bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700453 const sp<SensorEventConnection>& connection)
454{
455 if (mConnections.indexOf(connection) < 0) {
456 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700457 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700458 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700459 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700460}
461
462bool SensorService::SensorRecord::removeConnection(
463 const wp<SensorEventConnection>& connection)
464{
465 ssize_t index = mConnections.indexOf(connection);
466 if (index >= 0) {
467 mConnections.removeItemsAt(index, 1);
468 }
469 return mConnections.size() ? false : true;
470}
471
472// ---------------------------------------------------------------------------
473
474SensorService::SensorEventConnection::SensorEventConnection(
475 const sp<SensorService>& service)
476 : mService(service), mChannel(new SensorChannel())
477{
478}
479
480SensorService::SensorEventConnection::~SensorEventConnection()
481{
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700482 LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700483 mService->cleanupConnection(this);
484}
485
486void SensorService::SensorEventConnection::onFirstRef()
487{
488}
489
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700490bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800491 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800492 if (mSensorInfo.indexOf(handle) <= 0) {
493 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700494 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700495 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700496 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700497}
498
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700499bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800500 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800501 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700502 return true;
503 }
504 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700505}
506
507bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800508 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800509 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700510}
511
512bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800513 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700514 return mSensorInfo.size() ? true : false;
515}
516
Mathias Agopianfc328812010-07-14 23:41:37 -0700517status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700518 sensors_event_t const* buffer, size_t numEvents,
519 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700520{
Mathias Agopiancf510012010-07-22 16:18:10 -0700521 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700522 size_t count = 0;
523 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800524 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700525 size_t i=0;
526 while (i<numEvents) {
527 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800528 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700529 do {
530 scratch[count++] = buffer[i++];
531 } while ((i<numEvents) && (buffer[i].sensor == curr));
532 } else {
533 i++;
534 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700535 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700536 } else {
537 scratch = const_cast<sensors_event_t *>(buffer);
538 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700539 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700540
Mathias Agopian3560fb22010-07-22 21:24:39 -0700541 if (count == 0)
542 return 0;
543
Mathias Agopiancf510012010-07-22 16:18:10 -0700544 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700545 if (size == -EAGAIN) {
546 // the destination doesn't accept events anymore, it's probably
547 // full. For now, we just drop the events on the floor.
548 LOGW("dropping %d events on the floor", count);
549 return size;
550 }
551
552 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
553 count, strerror(-size));
554
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700555 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700556}
557
558sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
559{
560 return mChannel;
561}
562
563status_t SensorService::SensorEventConnection::enableDisable(
564 int handle, bool enabled)
565{
566 status_t err;
567 if (enabled) {
568 err = mService->enable(this, handle);
569 } else {
570 err = mService->disable(this, handle);
571 }
572 return err;
573}
574
575status_t SensorService::SensorEventConnection::setEventRate(
576 int handle, nsecs_t ns)
577{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700578 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700579}
580
581// ---------------------------------------------------------------------------
582}; // namespace android
583