blob: 64d214b5487a0cc5c0294f6b1f004895fd696e93 [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080018#include <math.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070019#include <sys/types.h>
20
Mathias Agopian33015422011-05-27 18:18:13 -070021#include <cutils/properties.h>
22
Mathias Agopianfc328812010-07-14 23:41:37 -070023#include <utils/SortedVector.h>
24#include <utils/KeyedVector.h>
25#include <utils/threads.h>
26#include <utils/Atomic.h>
27#include <utils/Errors.h>
28#include <utils/RefBase.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070029#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070030#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070031
32#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070033#include <binder/IServiceManager.h>
Mathias Agopian1cb13462011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070035
36#include <gui/ISensorServer.h>
37#include <gui/ISensorEventConnection.h>
38
39#include <hardware/sensors.h>
40
Mathias Agopian984826c2011-05-17 22:54:42 -070041#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080042#include "GravitySensor.h"
43#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070044#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080045#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070046#include "SensorFusion.h"
47#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070048
49namespace android {
50// ---------------------------------------------------------------------------
51
Mathias Agopian33015422011-05-27 18:18:13 -070052/*
53 * Notes:
54 *
55 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070056 * - run mag sensor from time to time to force calibration
57 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
58 *
59 */
60
Mathias Agopianfc328812010-07-14 23:41:37 -070061SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -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
Mathias Agopian1cb13462011-06-27 16:05:52 -0700169static const String16 sDump("android.permission.DUMP");
170
Mathias Agopianfc328812010-07-14 23:41:37 -0700171status_t SensorService::dump(int fd, const Vector<String16>& args)
172{
173 const size_t SIZE = 1024;
174 char buffer[SIZE];
175 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700176 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700177 snprintf(buffer, SIZE, "Permission Denial: "
178 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
179 IPCThreadState::self()->getCallingPid(),
180 IPCThreadState::self()->getCallingUid());
181 result.append(buffer);
182 } else {
183 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700184 snprintf(buffer, SIZE, "Sensor List:\n");
185 result.append(buffer);
186 for (size_t i=0 ; i<mSensorList.size() ; i++) {
187 const Sensor& s(mSensorList[i]);
188 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700189 snprintf(buffer, SIZE,
190 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
191 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700192 s.getName().string(),
193 s.getVendor().string(),
194 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700195 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700196 e.data[0], e.data[1], e.data[2]);
197 result.append(buffer);
198 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700199 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800200 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700201
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700202 snprintf(buffer, SIZE, "%d active connections\n",
203 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700204 result.append(buffer);
205 snprintf(buffer, SIZE, "Active sensors:\n");
206 result.append(buffer);
207 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700208 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800209 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700210 getSensorName(handle).string(),
211 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700212 mActiveSensors.valueAt(i)->getNumConnections());
213 result.append(buffer);
214 }
215 }
216 write(fd, result.string(), result.size());
217 return NO_ERROR;
218}
219
220bool SensorService::threadLoop()
221{
222 LOGD("nuSensorService thread starting...");
223
Mathias Agopianf001c922010-11-11 17:58:51 -0800224 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
225 sensors_event_t buffer[numEventMax];
226 sensors_event_t scratch[numEventMax];
227 SensorDevice& device(SensorDevice::getInstance());
228 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700229
Mathias Agopianf001c922010-11-11 17:58:51 -0800230 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700231 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800232 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700233 if (count<0) {
234 LOGE("sensor poll failed (%s)", strerror(-count));
235 break;
236 }
237
Mathias Agopian94e8f682010-11-10 17:50:28 -0800238 recordLastValue(buffer, count);
239
Mathias Agopianf001c922010-11-11 17:58:51 -0800240 // handle virtual sensors
241 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700242 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800243 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
244 getActiveVirtualSensors());
245 const size_t activeVirtualSensorCount = virtualSensors.size();
246 if (activeVirtualSensorCount) {
247 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700248 SensorFusion& fusion(SensorFusion::getInstance());
249 if (fusion.isEnabled()) {
250 for (size_t i=0 ; i<size_t(count) ; i++) {
251 fusion.process(event[i]);
252 }
253 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800254 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800255 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
256 sensors_event_t out;
257 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
258 buffer[count + k] = out;
259 k++;
260 }
261 }
262 }
263 if (k) {
264 // record the last synthesized values
265 recordLastValue(&buffer[count], k);
266 count += k;
267 // sort the buffer by time-stamps
268 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700269 }
270 }
271 }
272
Mathias Agopianf001c922010-11-11 17:58:51 -0800273 // send our events to clients...
274 const SortedVector< wp<SensorEventConnection> > activeConnections(
275 getActiveConnections());
276 size_t numConnections = activeConnections.size();
277 for (size_t i=0 ; i<numConnections ; i++) {
278 sp<SensorEventConnection> connection(
279 activeConnections[i].promote());
280 if (connection != 0) {
281 connection->sendEvents(buffer, count, scratch);
282 }
283 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700284 } while (count >= 0 || Thread::exitPending());
285
286 LOGW("Exiting SensorService::threadLoop!");
287 return false;
288}
289
Mathias Agopian94e8f682010-11-10 17:50:28 -0800290void SensorService::recordLastValue(
291 sensors_event_t const * buffer, size_t count)
292{
293 Mutex::Autolock _l(mLock);
294
295 // record the last event for each sensor
296 int32_t prev = buffer[0].sensor;
297 for (size_t i=1 ; i<count ; i++) {
298 // record the last event of each sensor type in this buffer
299 int32_t curr = buffer[i].sensor;
300 if (curr != prev) {
301 mLastEventSeen.editValueFor(prev) = buffer[i-1];
302 prev = curr;
303 }
304 }
305 mLastEventSeen.editValueFor(prev) = buffer[count-1];
306}
307
Mathias Agopianf001c922010-11-11 17:58:51 -0800308void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
309{
310 struct compar {
311 static int cmp(void const* lhs, void const* rhs) {
312 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
313 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
314 return r->timestamp - l->timestamp;
315 }
316 };
317 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
318}
319
Mathias Agopianfc328812010-07-14 23:41:37 -0700320SortedVector< wp<SensorService::SensorEventConnection> >
321SensorService::getActiveConnections() const
322{
323 Mutex::Autolock _l(mLock);
324 return mActiveConnections;
325}
326
Mathias Agopianf001c922010-11-11 17:58:51 -0800327DefaultKeyedVector<int, SensorInterface*>
328SensorService::getActiveVirtualSensors() const
329{
330 Mutex::Autolock _l(mLock);
331 return mActiveVirtualSensors;
332}
333
Mathias Agopian5d270722010-07-19 15:20:39 -0700334String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700335 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700336 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700337 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700338 if (sensor.getHandle() == handle) {
339 return sensor.getName();
340 }
341 }
342 String8 result("unknown");
343 return result;
344}
345
Mathias Agopianfc328812010-07-14 23:41:37 -0700346Vector<Sensor> SensorService::getSensorList()
347{
Mathias Agopian010e4222011-06-08 20:06:50 -0700348 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700349}
350
351sp<ISensorEventConnection> SensorService::createSensorEventConnection()
352{
353 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700354 return result;
355}
356
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800357void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700358{
359 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800360 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700361 size_t size = mActiveSensors.size();
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700362 LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700363 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800364 int handle = mActiveSensors.keyAt(i);
365 if (c->hasSensor(handle)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700366 LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800367 SensorInterface* sensor = mSensorMap.valueFor( handle );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700368 LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800369 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800370 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800371 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800372 }
373 SensorRecord* rec = mActiveSensors.valueAt(i);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700374 LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
375 LOGD_IF(DEBUG_CONNECTIONS,
376 "removing connection %p for sensor[%d].handle=0x%08x",
377 c, i, handle);
378
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800379 if (rec && rec->removeConnection(connection)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700380 LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700381 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800382 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700383 delete rec;
384 size--;
385 } else {
386 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700387 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700388 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700389 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700390}
391
392status_t SensorService::enable(const sp<SensorEventConnection>& connection,
393 int handle)
394{
Mathias Agopian50df2952010-07-19 19:09:10 -0700395 if (mInitCheck != NO_ERROR)
396 return mInitCheck;
397
Mathias Agopianfc328812010-07-14 23:41:37 -0700398 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800399 SensorInterface* sensor = mSensorMap.valueFor(handle);
400 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700401 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800402 SensorRecord* rec = mActiveSensors.valueFor(handle);
403 if (rec == 0) {
404 rec = new SensorRecord(connection);
405 mActiveSensors.add(handle, rec);
406 if (sensor->isVirtual()) {
407 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700408 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800409 } else {
410 if (rec->addConnection(connection)) {
411 // this sensor is already activated, but we are adding a
412 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800413 // known value of the requested sensor if it's not a
414 // "continuous" sensor.
415 if (sensor->getSensor().getMinDelay() == 0) {
416 sensors_event_t scratch;
417 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
418 if (event.version == sizeof(sensors_event_t)) {
419 connection->sendEvents(&event, 1);
420 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800421 }
422 }
423 }
424 if (err == NO_ERROR) {
425 // connection now active
426 if (connection->addSensor(handle)) {
427 // the sensor was added (which means it wasn't already there)
428 // so, see if this connection becomes active
429 if (mActiveConnections.indexOf(connection) < 0) {
430 mActiveConnections.add(connection);
431 }
432 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700433 }
434 }
435 return err;
436}
437
438status_t SensorService::disable(const sp<SensorEventConnection>& connection,
439 int handle)
440{
Mathias Agopian50df2952010-07-19 19:09:10 -0700441 if (mInitCheck != NO_ERROR)
442 return mInitCheck;
443
Mathias Agopianfc328812010-07-14 23:41:37 -0700444 status_t err = NO_ERROR;
445 Mutex::Autolock _l(mLock);
446 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700447 if (rec) {
448 // see if this connection becomes inactive
449 connection->removeSensor(handle);
450 if (connection->hasAnySensor() == false) {
451 mActiveConnections.remove(connection);
452 }
453 // see if this sensor becomes inactive
454 if (rec->removeConnection(connection)) {
455 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800456 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700457 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700458 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800459 SensorInterface* sensor = mSensorMap.valueFor(handle);
460 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700461 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700462 return err;
463}
464
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700465status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700466 int handle, nsecs_t ns)
467{
Mathias Agopian50df2952010-07-19 19:09:10 -0700468 if (mInitCheck != NO_ERROR)
469 return mInitCheck;
470
Mathias Agopian1cd70002010-07-21 15:59:50 -0700471 if (ns < 0)
472 return BAD_VALUE;
473
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700474 if (ns < MINIMUM_EVENTS_PERIOD)
475 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700476
Mathias Agopianf001c922010-11-11 17:58:51 -0800477 SensorInterface* sensor = mSensorMap.valueFor(handle);
478 if (!sensor) return BAD_VALUE;
479 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700480}
481
482// ---------------------------------------------------------------------------
483
484SensorService::SensorRecord::SensorRecord(
485 const sp<SensorEventConnection>& connection)
486{
487 mConnections.add(connection);
488}
489
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700490bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700491 const sp<SensorEventConnection>& connection)
492{
493 if (mConnections.indexOf(connection) < 0) {
494 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700495 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700496 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700497 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700498}
499
500bool SensorService::SensorRecord::removeConnection(
501 const wp<SensorEventConnection>& connection)
502{
503 ssize_t index = mConnections.indexOf(connection);
504 if (index >= 0) {
505 mConnections.removeItemsAt(index, 1);
506 }
507 return mConnections.size() ? false : true;
508}
509
510// ---------------------------------------------------------------------------
511
512SensorService::SensorEventConnection::SensorEventConnection(
513 const sp<SensorService>& service)
514 : mService(service), mChannel(new SensorChannel())
515{
516}
517
518SensorService::SensorEventConnection::~SensorEventConnection()
519{
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700520 LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700521 mService->cleanupConnection(this);
522}
523
524void SensorService::SensorEventConnection::onFirstRef()
525{
526}
527
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700528bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800529 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800530 if (mSensorInfo.indexOf(handle) <= 0) {
531 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700532 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700533 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700534 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700535}
536
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700537bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800538 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800539 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700540 return true;
541 }
542 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700543}
544
545bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800546 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800547 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700548}
549
550bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800551 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700552 return mSensorInfo.size() ? true : false;
553}
554
Mathias Agopianfc328812010-07-14 23:41:37 -0700555status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700556 sensors_event_t const* buffer, size_t numEvents,
557 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700558{
Mathias Agopiancf510012010-07-22 16:18:10 -0700559 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700560 size_t count = 0;
561 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800562 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700563 size_t i=0;
564 while (i<numEvents) {
565 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800566 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700567 do {
568 scratch[count++] = buffer[i++];
569 } while ((i<numEvents) && (buffer[i].sensor == curr));
570 } else {
571 i++;
572 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700573 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700574 } else {
575 scratch = const_cast<sensors_event_t *>(buffer);
576 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700577 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700578
Mathias Agopian3560fb22010-07-22 21:24:39 -0700579 if (count == 0)
580 return 0;
581
Mathias Agopiancf510012010-07-22 16:18:10 -0700582 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700583 if (size == -EAGAIN) {
584 // the destination doesn't accept events anymore, it's probably
585 // full. For now, we just drop the events on the floor.
586 LOGW("dropping %d events on the floor", count);
587 return size;
588 }
589
590 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
591 count, strerror(-size));
592
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700593 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700594}
595
596sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
597{
598 return mChannel;
599}
600
601status_t SensorService::SensorEventConnection::enableDisable(
602 int handle, bool enabled)
603{
604 status_t err;
605 if (enabled) {
606 err = mService->enable(this, handle);
607 } else {
608 err = mService->disable(this, handle);
609 }
610 return err;
611}
612
613status_t SensorService::SensorEventConnection::setEventRate(
614 int handle, nsecs_t ns)
615{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700616 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700617}
618
619// ---------------------------------------------------------------------------
620}; // namespace android
621