blob: e0dce1ff9d25624d0349b2f8d61dda80d831bfb1 [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 Agopianf001c922010-11-11 17:58:51 -080073 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070074 ssize_t count = dev.getSensorList(&list);
75 if (count > 0) {
76 ssize_t orientationIndex = -1;
77 bool hasGyro = false;
78 uint32_t virtualSensorsNeeds =
79 (1<<SENSOR_TYPE_GRAVITY) |
80 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
81 (1<<SENSOR_TYPE_ROTATION_VECTOR);
82
83 mLastEventSeen.setCapacity(count);
84 for (ssize_t i=0 ; i<count ; i++) {
85 registerSensor( new HardwareSensor(list[i]) );
86 switch (list[i].type) {
87 case SENSOR_TYPE_ORIENTATION:
88 orientationIndex = i;
89 break;
90 case SENSOR_TYPE_GYROSCOPE:
91 hasGyro = true;
92 break;
93 case SENSOR_TYPE_GRAVITY:
94 case SENSOR_TYPE_LINEAR_ACCELERATION:
95 case SENSOR_TYPE_ROTATION_VECTOR:
96 virtualSensorsNeeds &= ~(1<<list[i].type);
97 break;
98 }
Mathias Agopian50df2952010-07-19 19:09:10 -070099 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700100
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700101 // it's safe to instantiate the SensorFusion object here
102 // (it wants to be instantiated after h/w sensors have been
103 // registered)
104 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700105
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700106 if (hasGyro) {
107 // Always instantiate Android's virtual sensors. Since they are
108 // instantiated behind sensors from the HAL, they won't
109 // interfere with applications, unless they looks specifically
110 // for them (by name).
Mathias Agopian984826c2011-05-17 22:54:42 -0700111
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700112 registerVirtualSensor( new RotationVectorSensor() );
113 registerVirtualSensor( new GravitySensor(list, count) );
114 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian984826c2011-05-17 22:54:42 -0700115
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700116 // these are optional
117 registerVirtualSensor( new OrientationSensor() );
118 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33015422011-05-27 18:18:13 -0700119
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700120 // virtual debugging sensors...
121 char value[PROPERTY_VALUE_MAX];
122 property_get("debug.sensors", value, "0");
123 if (atoi(value)) {
124 registerVirtualSensor( new GyroDriftSensor() );
125 }
Mathias Agopian33015422011-05-27 18:18:13 -0700126 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800127
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700128 // build the sensor list returned to users
129 mUserSensorList = mSensorList;
130 if (hasGyro &&
131 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
132 // if we have the fancy sensor fusion, and it's not provided by the
133 // HAL, use our own (fused) orientation sensor by removing the
134 // HAL supplied one form the user list.
135 if (orientationIndex >= 0) {
136 mUserSensorList.removeItemsAt(orientationIndex);
137 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700138 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700139
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700140 run("SensorService", PRIORITY_URGENT_DISPLAY);
141 mInitCheck = NO_ERROR;
142 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700143 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700144}
145
Mathias Agopianf001c922010-11-11 17:58:51 -0800146void SensorService::registerSensor(SensorInterface* s)
147{
148 sensors_event_t event;
149 memset(&event, 0, sizeof(event));
150
151 const Sensor sensor(s->getSensor());
152 // add to the sensor list (returned to clients)
153 mSensorList.add(sensor);
154 // add to our handle->SensorInterface mapping
155 mSensorMap.add(sensor.getHandle(), s);
156 // create an entry in the mLastEventSeen array
157 mLastEventSeen.add(sensor.getHandle(), event);
158}
159
160void SensorService::registerVirtualSensor(SensorInterface* s)
161{
162 registerSensor(s);
163 mVirtualSensorList.add( s );
164}
165
Mathias Agopianfc328812010-07-14 23:41:37 -0700166SensorService::~SensorService()
167{
Mathias Agopianf001c922010-11-11 17:58:51 -0800168 for (size_t i=0 ; i<mSensorMap.size() ; i++)
169 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700170}
171
Mathias Agopian1cb13462011-06-27 16:05:52 -0700172static const String16 sDump("android.permission.DUMP");
173
Mathias Agopianfc328812010-07-14 23:41:37 -0700174status_t SensorService::dump(int fd, const Vector<String16>& args)
175{
176 const size_t SIZE = 1024;
177 char buffer[SIZE];
178 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700179 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700180 snprintf(buffer, SIZE, "Permission Denial: "
181 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
182 IPCThreadState::self()->getCallingPid(),
183 IPCThreadState::self()->getCallingUid());
184 result.append(buffer);
185 } else {
186 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700187 snprintf(buffer, SIZE, "Sensor List:\n");
188 result.append(buffer);
189 for (size_t i=0 ; i<mSensorList.size() ; i++) {
190 const Sensor& s(mSensorList[i]);
191 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700192 snprintf(buffer, SIZE,
193 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
194 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700195 s.getName().string(),
196 s.getVendor().string(),
197 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700198 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700199 e.data[0], e.data[1], e.data[2]);
200 result.append(buffer);
201 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700202 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800203 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700204
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700205 snprintf(buffer, SIZE, "%d active connections\n",
206 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700207 result.append(buffer);
208 snprintf(buffer, SIZE, "Active sensors:\n");
209 result.append(buffer);
210 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700211 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800212 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700213 getSensorName(handle).string(),
214 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700215 mActiveSensors.valueAt(i)->getNumConnections());
216 result.append(buffer);
217 }
218 }
219 write(fd, result.string(), result.size());
220 return NO_ERROR;
221}
222
223bool SensorService::threadLoop()
224{
225 LOGD("nuSensorService thread starting...");
226
Mathias Agopianf001c922010-11-11 17:58:51 -0800227 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
228 sensors_event_t buffer[numEventMax];
229 sensors_event_t scratch[numEventMax];
230 SensorDevice& device(SensorDevice::getInstance());
231 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700232
Mathias Agopianf001c922010-11-11 17:58:51 -0800233 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700234 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800235 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700236 if (count<0) {
237 LOGE("sensor poll failed (%s)", strerror(-count));
238 break;
239 }
240
Mathias Agopian94e8f682010-11-10 17:50:28 -0800241 recordLastValue(buffer, count);
242
Mathias Agopianf001c922010-11-11 17:58:51 -0800243 // handle virtual sensors
244 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700245 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800246 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
247 getActiveVirtualSensors());
248 const size_t activeVirtualSensorCount = virtualSensors.size();
249 if (activeVirtualSensorCount) {
250 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700251 SensorFusion& fusion(SensorFusion::getInstance());
252 if (fusion.isEnabled()) {
253 for (size_t i=0 ; i<size_t(count) ; i++) {
254 fusion.process(event[i]);
255 }
256 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800257 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800258 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
259 sensors_event_t out;
260 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
261 buffer[count + k] = out;
262 k++;
263 }
264 }
265 }
266 if (k) {
267 // record the last synthesized values
268 recordLastValue(&buffer[count], k);
269 count += k;
270 // sort the buffer by time-stamps
271 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700272 }
273 }
274 }
275
Mathias Agopianf001c922010-11-11 17:58:51 -0800276 // send our events to clients...
277 const SortedVector< wp<SensorEventConnection> > activeConnections(
278 getActiveConnections());
279 size_t numConnections = activeConnections.size();
280 for (size_t i=0 ; i<numConnections ; i++) {
281 sp<SensorEventConnection> connection(
282 activeConnections[i].promote());
283 if (connection != 0) {
284 connection->sendEvents(buffer, count, scratch);
285 }
286 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700287 } while (count >= 0 || Thread::exitPending());
288
289 LOGW("Exiting SensorService::threadLoop!");
290 return false;
291}
292
Mathias Agopian94e8f682010-11-10 17:50:28 -0800293void SensorService::recordLastValue(
294 sensors_event_t const * buffer, size_t count)
295{
296 Mutex::Autolock _l(mLock);
297
298 // record the last event for each sensor
299 int32_t prev = buffer[0].sensor;
300 for (size_t i=1 ; i<count ; i++) {
301 // record the last event of each sensor type in this buffer
302 int32_t curr = buffer[i].sensor;
303 if (curr != prev) {
304 mLastEventSeen.editValueFor(prev) = buffer[i-1];
305 prev = curr;
306 }
307 }
308 mLastEventSeen.editValueFor(prev) = buffer[count-1];
309}
310
Mathias Agopianf001c922010-11-11 17:58:51 -0800311void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
312{
313 struct compar {
314 static int cmp(void const* lhs, void const* rhs) {
315 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
316 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
317 return r->timestamp - l->timestamp;
318 }
319 };
320 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
321}
322
Mathias Agopianfc328812010-07-14 23:41:37 -0700323SortedVector< wp<SensorService::SensorEventConnection> >
324SensorService::getActiveConnections() const
325{
326 Mutex::Autolock _l(mLock);
327 return mActiveConnections;
328}
329
Mathias Agopianf001c922010-11-11 17:58:51 -0800330DefaultKeyedVector<int, SensorInterface*>
331SensorService::getActiveVirtualSensors() const
332{
333 Mutex::Autolock _l(mLock);
334 return mActiveVirtualSensors;
335}
336
Mathias Agopian5d270722010-07-19 15:20:39 -0700337String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700338 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700339 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700340 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700341 if (sensor.getHandle() == handle) {
342 return sensor.getName();
343 }
344 }
345 String8 result("unknown");
346 return result;
347}
348
Mathias Agopianfc328812010-07-14 23:41:37 -0700349Vector<Sensor> SensorService::getSensorList()
350{
Mathias Agopian010e4222011-06-08 20:06:50 -0700351 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700352}
353
354sp<ISensorEventConnection> SensorService::createSensorEventConnection()
355{
356 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700357 return result;
358}
359
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800360void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700361{
362 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800363 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700364 size_t size = mActiveSensors.size();
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700365 LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700366 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800367 int handle = mActiveSensors.keyAt(i);
368 if (c->hasSensor(handle)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700369 LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800370 SensorInterface* sensor = mSensorMap.valueFor( handle );
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700371 LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800372 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800373 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800374 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800375 }
376 SensorRecord* rec = mActiveSensors.valueAt(i);
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700377 LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
378 LOGD_IF(DEBUG_CONNECTIONS,
379 "removing connection %p for sensor[%d].handle=0x%08x",
380 c, i, handle);
381
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800382 if (rec && rec->removeConnection(connection)) {
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700383 LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700384 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800385 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700386 delete rec;
387 size--;
388 } else {
389 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700390 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700391 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700392 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700393}
394
395status_t SensorService::enable(const sp<SensorEventConnection>& connection,
396 int handle)
397{
Mathias Agopian50df2952010-07-19 19:09:10 -0700398 if (mInitCheck != NO_ERROR)
399 return mInitCheck;
400
Mathias Agopianfc328812010-07-14 23:41:37 -0700401 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800402 SensorInterface* sensor = mSensorMap.valueFor(handle);
403 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700404 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800405 SensorRecord* rec = mActiveSensors.valueFor(handle);
406 if (rec == 0) {
407 rec = new SensorRecord(connection);
408 mActiveSensors.add(handle, rec);
409 if (sensor->isVirtual()) {
410 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700411 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800412 } else {
413 if (rec->addConnection(connection)) {
414 // this sensor is already activated, but we are adding a
415 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800416 // known value of the requested sensor if it's not a
417 // "continuous" sensor.
418 if (sensor->getSensor().getMinDelay() == 0) {
419 sensors_event_t scratch;
420 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
421 if (event.version == sizeof(sensors_event_t)) {
422 connection->sendEvents(&event, 1);
423 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800424 }
425 }
426 }
427 if (err == NO_ERROR) {
428 // connection now active
429 if (connection->addSensor(handle)) {
430 // the sensor was added (which means it wasn't already there)
431 // so, see if this connection becomes active
432 if (mActiveConnections.indexOf(connection) < 0) {
433 mActiveConnections.add(connection);
434 }
435 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700436 }
437 }
438 return err;
439}
440
441status_t SensorService::disable(const sp<SensorEventConnection>& connection,
442 int handle)
443{
Mathias Agopian50df2952010-07-19 19:09:10 -0700444 if (mInitCheck != NO_ERROR)
445 return mInitCheck;
446
Mathias Agopianfc328812010-07-14 23:41:37 -0700447 status_t err = NO_ERROR;
448 Mutex::Autolock _l(mLock);
449 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700450 if (rec) {
451 // see if this connection becomes inactive
452 connection->removeSensor(handle);
453 if (connection->hasAnySensor() == false) {
454 mActiveConnections.remove(connection);
455 }
456 // see if this sensor becomes inactive
457 if (rec->removeConnection(connection)) {
458 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800459 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700460 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700461 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800462 SensorInterface* sensor = mSensorMap.valueFor(handle);
463 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700464 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700465 return err;
466}
467
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700468status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700469 int handle, nsecs_t ns)
470{
Mathias Agopian50df2952010-07-19 19:09:10 -0700471 if (mInitCheck != NO_ERROR)
472 return mInitCheck;
473
Mathias Agopian1cd70002010-07-21 15:59:50 -0700474 if (ns < 0)
475 return BAD_VALUE;
476
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700477 if (ns < MINIMUM_EVENTS_PERIOD)
478 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700479
Mathias Agopianf001c922010-11-11 17:58:51 -0800480 SensorInterface* sensor = mSensorMap.valueFor(handle);
481 if (!sensor) return BAD_VALUE;
482 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700483}
484
485// ---------------------------------------------------------------------------
486
487SensorService::SensorRecord::SensorRecord(
488 const sp<SensorEventConnection>& connection)
489{
490 mConnections.add(connection);
491}
492
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700493bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700494 const sp<SensorEventConnection>& connection)
495{
496 if (mConnections.indexOf(connection) < 0) {
497 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700498 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700499 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700500 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700501}
502
503bool SensorService::SensorRecord::removeConnection(
504 const wp<SensorEventConnection>& connection)
505{
506 ssize_t index = mConnections.indexOf(connection);
507 if (index >= 0) {
508 mConnections.removeItemsAt(index, 1);
509 }
510 return mConnections.size() ? false : true;
511}
512
513// ---------------------------------------------------------------------------
514
515SensorService::SensorEventConnection::SensorEventConnection(
516 const sp<SensorService>& service)
517 : mService(service), mChannel(new SensorChannel())
518{
519}
520
521SensorService::SensorEventConnection::~SensorEventConnection()
522{
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700523 LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700524 mService->cleanupConnection(this);
525}
526
527void SensorService::SensorEventConnection::onFirstRef()
528{
529}
530
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700531bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800532 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800533 if (mSensorInfo.indexOf(handle) <= 0) {
534 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700535 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700536 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700537 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700538}
539
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700540bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800541 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800542 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700543 return true;
544 }
545 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700546}
547
548bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800549 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800550 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700551}
552
553bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800554 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700555 return mSensorInfo.size() ? true : false;
556}
557
Mathias Agopianfc328812010-07-14 23:41:37 -0700558status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700559 sensors_event_t const* buffer, size_t numEvents,
560 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700561{
Mathias Agopiancf510012010-07-22 16:18:10 -0700562 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700563 size_t count = 0;
564 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800565 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700566 size_t i=0;
567 while (i<numEvents) {
568 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800569 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700570 do {
571 scratch[count++] = buffer[i++];
572 } while ((i<numEvents) && (buffer[i].sensor == curr));
573 } else {
574 i++;
575 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700576 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700577 } else {
578 scratch = const_cast<sensors_event_t *>(buffer);
579 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700580 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700581
Mathias Agopian3560fb22010-07-22 21:24:39 -0700582 if (count == 0)
583 return 0;
584
Mathias Agopiancf510012010-07-22 16:18:10 -0700585 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopianfc328812010-07-14 23:41:37 -0700586 if (size == -EAGAIN) {
587 // the destination doesn't accept events anymore, it's probably
588 // full. For now, we just drop the events on the floor.
589 LOGW("dropping %d events on the floor", count);
590 return size;
591 }
592
593 LOGE_IF(size<0, "dropping %d events on the floor (%s)",
594 count, strerror(-size));
595
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700596 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700597}
598
599sp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const
600{
601 return mChannel;
602}
603
604status_t SensorService::SensorEventConnection::enableDisable(
605 int handle, bool enabled)
606{
607 status_t err;
608 if (enabled) {
609 err = mService->enable(this, handle);
610 } else {
611 err = mService->disable(this, handle);
612 }
613 return err;
614}
615
616status_t SensorService::SensorEventConnection::setEventRate(
617 int handle, nsecs_t ns)
618{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700619 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700620}
621
622// ---------------------------------------------------------------------------
623}; // namespace android
624