blob: ce1ab3df23d903a8268ad0e08aaef58a77021626 [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()
47 : Thread(false),
Mathias Agopian50df2952010-07-19 19:09:10 -070048 mDump("android.permission.DUMP"),
49 mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070050{
51}
52
53void SensorService::onFirstRef()
54{
Mathias Agopian50df2952010-07-19 19:09:10 -070055 LOGD("nuSensorService starting...");
56
Mathias Agopianf001c922010-11-11 17:58:51 -080057 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070058
Mathias Agopianf001c922010-11-11 17:58:51 -080059 if (dev.initCheck() == NO_ERROR) {
60 uint32_t virtualSensorsNeeds =
61 (1<<SENSOR_TYPE_GRAVITY) |
62 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
63 (1<<SENSOR_TYPE_ROTATION_VECTOR);
64 sensor_t const* list;
65 int count = dev.getSensorList(&list);
Mathias Agopian3560fb22010-07-22 21:24:39 -070066 mLastEventSeen.setCapacity(count);
Mathias Agopian50df2952010-07-19 19:09:10 -070067 for (int i=0 ; i<count ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -080068 registerSensor( new HardwareSensor(list[i]) );
69 switch (list[i].type) {
70 case SENSOR_TYPE_GRAVITY:
71 case SENSOR_TYPE_LINEAR_ACCELERATION:
72 case SENSOR_TYPE_ROTATION_VECTOR:
73 virtualSensorsNeeds &= ~(1<<list[i].type);
74 break;
Mathias Agopian50df2952010-07-19 19:09:10 -070075 }
76 }
Mathias Agopianfc328812010-07-14 23:41:37 -070077
Mathias Agopianf001c922010-11-11 17:58:51 -080078 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
79 registerVirtualSensor( new GravitySensor(list, count) );
Mathias Agopian50df2952010-07-19 19:09:10 -070080 }
Mathias Agopianf001c922010-11-11 17:58:51 -080081 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
82 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
83 }
84 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
85 registerVirtualSensor( new RotationVectorSensor(list, count) );
86 }
87
88 run("SensorService", PRIORITY_URGENT_DISPLAY);
89 mInitCheck = NO_ERROR;
Mathias Agopianfc328812010-07-14 23:41:37 -070090 }
Mathias Agopianfc328812010-07-14 23:41:37 -070091}
92
Mathias Agopianf001c922010-11-11 17:58:51 -080093void SensorService::registerSensor(SensorInterface* s)
94{
95 sensors_event_t event;
96 memset(&event, 0, sizeof(event));
97
98 const Sensor sensor(s->getSensor());
99 // add to the sensor list (returned to clients)
100 mSensorList.add(sensor);
101 // add to our handle->SensorInterface mapping
102 mSensorMap.add(sensor.getHandle(), s);
103 // create an entry in the mLastEventSeen array
104 mLastEventSeen.add(sensor.getHandle(), event);
105}
106
107void SensorService::registerVirtualSensor(SensorInterface* s)
108{
109 registerSensor(s);
110 mVirtualSensorList.add( s );
111}
112
Mathias Agopianfc328812010-07-14 23:41:37 -0700113SensorService::~SensorService()
114{
Mathias Agopianf001c922010-11-11 17:58:51 -0800115 for (size_t i=0 ; i<mSensorMap.size() ; i++)
116 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700117}
118
119status_t SensorService::dump(int fd, const Vector<String16>& args)
120{
121 const size_t SIZE = 1024;
122 char buffer[SIZE];
123 String8 result;
124 if (!mDump.checkCalling()) {
125 snprintf(buffer, SIZE, "Permission Denial: "
126 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
127 IPCThreadState::self()->getCallingPid(),
128 IPCThreadState::self()->getCallingUid());
129 result.append(buffer);
130 } else {
131 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700132 snprintf(buffer, SIZE, "Sensor List:\n");
133 result.append(buffer);
134 for (size_t i=0 ; i<mSensorList.size() ; i++) {
135 const Sensor& s(mSensorList[i]);
136 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopianf001c922010-11-11 17:58:51 -0800137 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 -0700138 s.getName().string(),
139 s.getVendor().string(),
140 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700141 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700142 e.data[0], e.data[1], e.data[2]);
143 result.append(buffer);
144 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800145 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700146
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700147 snprintf(buffer, SIZE, "%d active connections\n",
148 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700149 result.append(buffer);
150 snprintf(buffer, SIZE, "Active sensors:\n");
151 result.append(buffer);
152 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700153 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800154 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700155 getSensorName(handle).string(),
156 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700157 mActiveSensors.valueAt(i)->getNumConnections());
158 result.append(buffer);
159 }
160 }
161 write(fd, result.string(), result.size());
162 return NO_ERROR;
163}
164
165bool SensorService::threadLoop()
166{
167 LOGD("nuSensorService thread starting...");
168
Mathias Agopianf001c922010-11-11 17:58:51 -0800169 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
170 sensors_event_t buffer[numEventMax];
171 sensors_event_t scratch[numEventMax];
172 SensorDevice& device(SensorDevice::getInstance());
173 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700174
Mathias Agopianf001c922010-11-11 17:58:51 -0800175 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700176 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800177 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700178 if (count<0) {
179 LOGE("sensor poll failed (%s)", strerror(-count));
180 break;
181 }
182
Mathias Agopian94e8f682010-11-10 17:50:28 -0800183 recordLastValue(buffer, count);
184
Mathias Agopianf001c922010-11-11 17:58:51 -0800185 // handle virtual sensors
186 if (count && vcount) {
187 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
188 getActiveVirtualSensors());
189 const size_t activeVirtualSensorCount = virtualSensors.size();
190 if (activeVirtualSensorCount) {
191 size_t k = 0;
192 for (size_t i=0 ; i<size_t(count) ; i++) {
193 sensors_event_t const * const event = buffer;
194 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
195 sensors_event_t out;
196 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
197 buffer[count + k] = out;
198 k++;
199 }
200 }
201 }
202 if (k) {
203 // record the last synthesized values
204 recordLastValue(&buffer[count], k);
205 count += k;
206 // sort the buffer by time-stamps
207 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700208 }
209 }
210 }
211
Mathias Agopianf001c922010-11-11 17:58:51 -0800212 // send our events to clients...
213 const SortedVector< wp<SensorEventConnection> > activeConnections(
214 getActiveConnections());
215 size_t numConnections = activeConnections.size();
216 for (size_t i=0 ; i<numConnections ; i++) {
217 sp<SensorEventConnection> connection(
218 activeConnections[i].promote());
219 if (connection != 0) {
220 connection->sendEvents(buffer, count, scratch);
221 }
222 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700223 } while (count >= 0 || Thread::exitPending());
224
225 LOGW("Exiting SensorService::threadLoop!");
226 return false;
227}
228
Mathias Agopian94e8f682010-11-10 17:50:28 -0800229void SensorService::recordLastValue(
230 sensors_event_t const * buffer, size_t count)
231{
232 Mutex::Autolock _l(mLock);
233
234 // record the last event for each sensor
235 int32_t prev = buffer[0].sensor;
236 for (size_t i=1 ; i<count ; i++) {
237 // record the last event of each sensor type in this buffer
238 int32_t curr = buffer[i].sensor;
239 if (curr != prev) {
240 mLastEventSeen.editValueFor(prev) = buffer[i-1];
241 prev = curr;
242 }
243 }
244 mLastEventSeen.editValueFor(prev) = buffer[count-1];
245}
246
Mathias Agopianf001c922010-11-11 17:58:51 -0800247void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
248{
249 struct compar {
250 static int cmp(void const* lhs, void const* rhs) {
251 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
252 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
253 return r->timestamp - l->timestamp;
254 }
255 };
256 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
257}
258
Mathias Agopianfc328812010-07-14 23:41:37 -0700259SortedVector< wp<SensorService::SensorEventConnection> >
260SensorService::getActiveConnections() const
261{
262 Mutex::Autolock _l(mLock);
263 return mActiveConnections;
264}
265
Mathias Agopianf001c922010-11-11 17:58:51 -0800266DefaultKeyedVector<int, SensorInterface*>
267SensorService::getActiveVirtualSensors() const
268{
269 Mutex::Autolock _l(mLock);
270 return mActiveVirtualSensors;
271}
272
Mathias Agopian5d270722010-07-19 15:20:39 -0700273String8 SensorService::getSensorName(int handle) const {
274 size_t count = mSensorList.size();
275 for (size_t i=0 ; i<count ; i++) {
276 const Sensor& sensor(mSensorList[i]);
277 if (sensor.getHandle() == handle) {
278 return sensor.getName();
279 }
280 }
281 String8 result("unknown");
282 return result;
283}
284
Mathias Agopianfc328812010-07-14 23:41:37 -0700285Vector<Sensor> SensorService::getSensorList()
286{
287 return mSensorList;
288}
289
290sp<ISensorEventConnection> SensorService::createSensorEventConnection()
291{
292 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700293 return result;
294}
295
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800296void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700297{
298 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800299 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700300 size_t size = mActiveSensors.size();
301 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 Agopianf001c922010-11-11 17:58:51 -0800304 SensorInterface* sensor = mSensorMap.valueFor( handle );
305 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800306 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800307 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800308 }
309 SensorRecord* rec = mActiveSensors.valueAt(i);
310 if (rec && rec->removeConnection(connection)) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700311 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800312 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700313 delete rec;
314 size--;
315 } else {
316 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700317 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700318 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700319 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700320}
321
322status_t SensorService::enable(const sp<SensorEventConnection>& connection,
323 int handle)
324{
Mathias Agopian50df2952010-07-19 19:09:10 -0700325 if (mInitCheck != NO_ERROR)
326 return mInitCheck;
327
Mathias Agopianfc328812010-07-14 23:41:37 -0700328 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800329 SensorInterface* sensor = mSensorMap.valueFor(handle);
330 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700331 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800332 SensorRecord* rec = mActiveSensors.valueFor(handle);
333 if (rec == 0) {
334 rec = new SensorRecord(connection);
335 mActiveSensors.add(handle, rec);
336 if (sensor->isVirtual()) {
337 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700338 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800339 } else {
340 if (rec->addConnection(connection)) {
341 // this sensor is already activated, but we are adding a
342 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800343 // known value of the requested sensor if it's not a
344 // "continuous" sensor.
345 if (sensor->getSensor().getMinDelay() == 0) {
346 sensors_event_t scratch;
347 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
348 if (event.version == sizeof(sensors_event_t)) {
349 connection->sendEvents(&event, 1);
350 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800351 }
352 }
353 }
354 if (err == NO_ERROR) {
355 // connection now active
356 if (connection->addSensor(handle)) {
357 // the sensor was added (which means it wasn't already there)
358 // so, see if this connection becomes active
359 if (mActiveConnections.indexOf(connection) < 0) {
360 mActiveConnections.add(connection);
361 }
362 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700363 }
364 }
365 return err;
366}
367
368status_t SensorService::disable(const sp<SensorEventConnection>& connection,
369 int handle)
370{
Mathias Agopian50df2952010-07-19 19:09:10 -0700371 if (mInitCheck != NO_ERROR)
372 return mInitCheck;
373
Mathias Agopianfc328812010-07-14 23:41:37 -0700374 status_t err = NO_ERROR;
375 Mutex::Autolock _l(mLock);
376 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700377 if (rec) {
378 // see if this connection becomes inactive
379 connection->removeSensor(handle);
380 if (connection->hasAnySensor() == false) {
381 mActiveConnections.remove(connection);
382 }
383 // see if this sensor becomes inactive
384 if (rec->removeConnection(connection)) {
385 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800386 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700387 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700388 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800389 SensorInterface* sensor = mSensorMap.valueFor(handle);
390 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700391 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700392 return err;
393}
394
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700395status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700396 int handle, nsecs_t ns)
397{
Mathias Agopian50df2952010-07-19 19:09:10 -0700398 if (mInitCheck != NO_ERROR)
399 return mInitCheck;
400
Mathias Agopian1cd70002010-07-21 15:59:50 -0700401 if (ns < 0)
402 return BAD_VALUE;
403
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700404 if (ns < MINIMUM_EVENTS_PERIOD)
405 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700406
Mathias Agopianf001c922010-11-11 17:58:51 -0800407 SensorInterface* sensor = mSensorMap.valueFor(handle);
408 if (!sensor) return BAD_VALUE;
409 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700410}
411
412// ---------------------------------------------------------------------------
413
414SensorService::SensorRecord::SensorRecord(
415 const sp<SensorEventConnection>& connection)
416{
417 mConnections.add(connection);
418}
419
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700420bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700421 const sp<SensorEventConnection>& connection)
422{
423 if (mConnections.indexOf(connection) < 0) {
424 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700425 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700426 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700427 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700428}
429
430bool SensorService::SensorRecord::removeConnection(
431 const wp<SensorEventConnection>& connection)
432{
433 ssize_t index = mConnections.indexOf(connection);
434 if (index >= 0) {
435 mConnections.removeItemsAt(index, 1);
436 }
437 return mConnections.size() ? false : true;
438}
439
440// ---------------------------------------------------------------------------
441
442SensorService::SensorEventConnection::SensorEventConnection(
443 const sp<SensorService>& service)
444 : mService(service), mChannel(new SensorChannel())
445{
446}
447
448SensorService::SensorEventConnection::~SensorEventConnection()
449{
450 mService->cleanupConnection(this);
451}
452
453void SensorService::SensorEventConnection::onFirstRef()
454{
455}
456
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700457bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800458 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800459 if (mSensorInfo.indexOf(handle) <= 0) {
460 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700461 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700462 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700463 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700464}
465
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700466bool SensorService::SensorEventConnection::removeSensor(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.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700469 return true;
470 }
471 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700472}
473
474bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800475 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800476 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700477}
478
479bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800480 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700481 return mSensorInfo.size() ? true : false;
482}
483
Mathias Agopianfc328812010-07-14 23:41:37 -0700484status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700485 sensors_event_t const* buffer, size_t numEvents,
486 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700487{
Mathias Agopiancf510012010-07-22 16:18:10 -0700488 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700489 size_t count = 0;
490 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800491 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700492 size_t i=0;
493 while (i<numEvents) {
494 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800495 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700496 do {
497 scratch[count++] = buffer[i++];
498 } while ((i<numEvents) && (buffer[i].sensor == curr));
499 } else {
500 i++;
501 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700502 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700503 } else {
504 scratch = const_cast<sensors_event_t *>(buffer);
505 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700506 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700507
Mathias Agopian3560fb22010-07-22 21:24:39 -0700508 if (count == 0)
509 return 0;
510
Mathias Agopiancf510012010-07-22 16:18:10 -0700511 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700512 if (size == -EAGAIN) {
513 // the destination doesn't accept events anymore, it's probably
514 // full. For now, we just drop the events on the floor.
515 LOGW("dropping %d events on the floor", count);
516 return size;
517 }
518
519 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
520 count, strerror(-size));
521
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700522 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700523}
524
525sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
526{
527 return mChannel;
528}
529
530status_t SensorService::SensorEventConnection::enableDisable(
531 int handle, bool enabled)
532{
533 status_t err;
534 if (enabled) {
535 err = mService->enable(this, handle);
536 } else {
537 err = mService->disable(this, handle);
538 }
539 return err;
540}
541
542status_t SensorService::SensorEventConnection::setEventRate(
543 int handle, nsecs_t ns)
544{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700545 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700546}
547
548// ---------------------------------------------------------------------------
549}; // namespace android
550