blob: d3b667f13d04ac2213e67d2018be3eeb2f53e604 [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>
Mathias Agopian907103b2012-04-02 18:38:02 -070038#include <gui/SensorEventQueue.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070039
40#include <hardware/sensors.h>
41
Mathias Agopian984826c2011-05-17 22:54:42 -070042#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080043#include "GravitySensor.h"
44#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070045#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080046#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070047#include "SensorFusion.h"
48#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070049
50namespace android {
51// ---------------------------------------------------------------------------
52
Mathias Agopian33015422011-05-27 18:18:13 -070053/*
54 * Notes:
55 *
56 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070057 * - run mag sensor from time to time to force calibration
58 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
59 *
60 */
61
Mathias Agopianfc328812010-07-14 23:41:37 -070062SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -070063 : mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070064{
65}
66
67void SensorService::onFirstRef()
68{
Steve Blocka5512372011-12-20 16:23:08 +000069 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070070
Mathias Agopianf001c922010-11-11 17:58:51 -080071 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070072
Mathias Agopianf001c922010-11-11 17:58:51 -080073 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080074 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070075 ssize_t count = dev.getSensorList(&list);
76 if (count > 0) {
77 ssize_t orientationIndex = -1;
78 bool hasGyro = false;
79 uint32_t virtualSensorsNeeds =
80 (1<<SENSOR_TYPE_GRAVITY) |
81 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
82 (1<<SENSOR_TYPE_ROTATION_VECTOR);
83
84 mLastEventSeen.setCapacity(count);
85 for (ssize_t i=0 ; i<count ; i++) {
86 registerSensor( new HardwareSensor(list[i]) );
87 switch (list[i].type) {
88 case SENSOR_TYPE_ORIENTATION:
89 orientationIndex = i;
90 break;
91 case SENSOR_TYPE_GYROSCOPE:
92 hasGyro = true;
93 break;
94 case SENSOR_TYPE_GRAVITY:
95 case SENSOR_TYPE_LINEAR_ACCELERATION:
96 case SENSOR_TYPE_ROTATION_VECTOR:
97 virtualSensorsNeeds &= ~(1<<list[i].type);
98 break;
99 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700100 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700101
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700102 // it's safe to instantiate the SensorFusion object here
103 // (it wants to be instantiated after h/w sensors have been
104 // registered)
105 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700106
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700107 if (hasGyro) {
108 // Always instantiate Android's virtual sensors. Since they are
109 // instantiated behind sensors from the HAL, they won't
110 // interfere with applications, unless they looks specifically
111 // for them (by name).
Mathias Agopian984826c2011-05-17 22:54:42 -0700112
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700113 registerVirtualSensor( new RotationVectorSensor() );
114 registerVirtualSensor( new GravitySensor(list, count) );
115 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian984826c2011-05-17 22:54:42 -0700116
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700117 // these are optional
118 registerVirtualSensor( new OrientationSensor() );
119 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33015422011-05-27 18:18:13 -0700120
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700121 // virtual debugging sensors...
122 char value[PROPERTY_VALUE_MAX];
123 property_get("debug.sensors", value, "0");
124 if (atoi(value)) {
125 registerVirtualSensor( new GyroDriftSensor() );
126 }
Mathias Agopian33015422011-05-27 18:18:13 -0700127 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800128
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700129 // build the sensor list returned to users
130 mUserSensorList = mSensorList;
131 if (hasGyro &&
132 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
133 // if we have the fancy sensor fusion, and it's not provided by the
134 // HAL, use our own (fused) orientation sensor by removing the
135 // HAL supplied one form the user list.
136 if (orientationIndex >= 0) {
137 mUserSensorList.removeItemsAt(orientationIndex);
138 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700139 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700140
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700141 run("SensorService", PRIORITY_URGENT_DISPLAY);
142 mInitCheck = NO_ERROR;
143 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700144 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700145}
146
Mathias Agopianf001c922010-11-11 17:58:51 -0800147void SensorService::registerSensor(SensorInterface* s)
148{
149 sensors_event_t event;
150 memset(&event, 0, sizeof(event));
151
152 const Sensor sensor(s->getSensor());
153 // add to the sensor list (returned to clients)
154 mSensorList.add(sensor);
155 // add to our handle->SensorInterface mapping
156 mSensorMap.add(sensor.getHandle(), s);
157 // create an entry in the mLastEventSeen array
158 mLastEventSeen.add(sensor.getHandle(), event);
159}
160
161void SensorService::registerVirtualSensor(SensorInterface* s)
162{
163 registerSensor(s);
164 mVirtualSensorList.add( s );
165}
166
Mathias Agopianfc328812010-07-14 23:41:37 -0700167SensorService::~SensorService()
168{
Mathias Agopianf001c922010-11-11 17:58:51 -0800169 for (size_t i=0 ; i<mSensorMap.size() ; i++)
170 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700171}
172
Mathias Agopian1cb13462011-06-27 16:05:52 -0700173static const String16 sDump("android.permission.DUMP");
174
Mathias Agopianfc328812010-07-14 23:41:37 -0700175status_t SensorService::dump(int fd, const Vector<String16>& args)
176{
177 const size_t SIZE = 1024;
178 char buffer[SIZE];
179 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700180 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700181 snprintf(buffer, SIZE, "Permission Denial: "
182 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
183 IPCThreadState::self()->getCallingPid(),
184 IPCThreadState::self()->getCallingUid());
185 result.append(buffer);
186 } else {
187 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700188 snprintf(buffer, SIZE, "Sensor List:\n");
189 result.append(buffer);
190 for (size_t i=0 ; i<mSensorList.size() ; i++) {
191 const Sensor& s(mSensorList[i]);
192 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700193 snprintf(buffer, SIZE,
194 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
195 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700196 s.getName().string(),
197 s.getVendor().string(),
198 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700199 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700200 e.data[0], e.data[1], e.data[2]);
201 result.append(buffer);
202 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700203 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800204 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700205
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700206 snprintf(buffer, SIZE, "%d active connections\n",
207 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700208 result.append(buffer);
209 snprintf(buffer, SIZE, "Active sensors:\n");
210 result.append(buffer);
211 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700212 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800213 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700214 getSensorName(handle).string(),
215 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700216 mActiveSensors.valueAt(i)->getNumConnections());
217 result.append(buffer);
218 }
219 }
220 write(fd, result.string(), result.size());
221 return NO_ERROR;
222}
223
224bool SensorService::threadLoop()
225{
Steve Blocka5512372011-12-20 16:23:08 +0000226 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700227
Mathias Agopianf001c922010-11-11 17:58:51 -0800228 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
229 sensors_event_t buffer[numEventMax];
230 sensors_event_t scratch[numEventMax];
231 SensorDevice& device(SensorDevice::getInstance());
232 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700233
Mathias Agopianf001c922010-11-11 17:58:51 -0800234 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700235 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800236 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700237 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000238 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700239 break;
240 }
241
Mathias Agopian94e8f682010-11-10 17:50:28 -0800242 recordLastValue(buffer, count);
243
Mathias Agopianf001c922010-11-11 17:58:51 -0800244 // handle virtual sensors
245 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700246 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800247 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
248 getActiveVirtualSensors());
249 const size_t activeVirtualSensorCount = virtualSensors.size();
250 if (activeVirtualSensorCount) {
251 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700252 SensorFusion& fusion(SensorFusion::getInstance());
253 if (fusion.isEnabled()) {
254 for (size_t i=0 ; i<size_t(count) ; i++) {
255 fusion.process(event[i]);
256 }
257 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800258 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800259 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
260 sensors_event_t out;
261 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
262 buffer[count + k] = out;
263 k++;
264 }
265 }
266 }
267 if (k) {
268 // record the last synthesized values
269 recordLastValue(&buffer[count], k);
270 count += k;
271 // sort the buffer by time-stamps
272 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700273 }
274 }
275 }
276
Mathias Agopianf001c922010-11-11 17:58:51 -0800277 // send our events to clients...
278 const SortedVector< wp<SensorEventConnection> > activeConnections(
279 getActiveConnections());
280 size_t numConnections = activeConnections.size();
281 for (size_t i=0 ; i<numConnections ; i++) {
282 sp<SensorEventConnection> connection(
283 activeConnections[i].promote());
284 if (connection != 0) {
285 connection->sendEvents(buffer, count, scratch);
286 }
287 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700288 } while (count >= 0 || Thread::exitPending());
289
Steve Block3c20fbe2012-01-05 23:22:43 +0000290 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800291 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700292 return false;
293}
294
Mathias Agopian94e8f682010-11-10 17:50:28 -0800295void SensorService::recordLastValue(
296 sensors_event_t const * buffer, size_t count)
297{
298 Mutex::Autolock _l(mLock);
299
300 // record the last event for each sensor
301 int32_t prev = buffer[0].sensor;
302 for (size_t i=1 ; i<count ; i++) {
303 // record the last event of each sensor type in this buffer
304 int32_t curr = buffer[i].sensor;
305 if (curr != prev) {
306 mLastEventSeen.editValueFor(prev) = buffer[i-1];
307 prev = curr;
308 }
309 }
310 mLastEventSeen.editValueFor(prev) = buffer[count-1];
311}
312
Mathias Agopianf001c922010-11-11 17:58:51 -0800313void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
314{
315 struct compar {
316 static int cmp(void const* lhs, void const* rhs) {
317 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
318 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700319 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800320 }
321 };
322 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
323}
324
Mathias Agopianfc328812010-07-14 23:41:37 -0700325SortedVector< wp<SensorService::SensorEventConnection> >
326SensorService::getActiveConnections() const
327{
328 Mutex::Autolock _l(mLock);
329 return mActiveConnections;
330}
331
Mathias Agopianf001c922010-11-11 17:58:51 -0800332DefaultKeyedVector<int, SensorInterface*>
333SensorService::getActiveVirtualSensors() const
334{
335 Mutex::Autolock _l(mLock);
336 return mActiveVirtualSensors;
337}
338
Mathias Agopian5d270722010-07-19 15:20:39 -0700339String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700340 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700341 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700342 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700343 if (sensor.getHandle() == handle) {
344 return sensor.getName();
345 }
346 }
347 String8 result("unknown");
348 return result;
349}
350
Mathias Agopianfc328812010-07-14 23:41:37 -0700351Vector<Sensor> SensorService::getSensorList()
352{
Mathias Agopian010e4222011-06-08 20:06:50 -0700353 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700354}
355
356sp<ISensorEventConnection> SensorService::createSensorEventConnection()
357{
358 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopianfc328812010-07-14 23:41:37 -0700359 return result;
360}
361
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800362void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700363{
364 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800365 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700366 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000367 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700368 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800369 int handle = mActiveSensors.keyAt(i);
370 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000371 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800372 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000373 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800374 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800375 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800376 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800377 }
378 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000379 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000380 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700381 "removing connection %p for sensor[%d].handle=0x%08x",
382 c, i, handle);
383
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800384 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000385 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700386 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800387 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700388 delete rec;
389 size--;
390 } else {
391 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700392 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700393 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700394 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700395}
396
397status_t SensorService::enable(const sp<SensorEventConnection>& connection,
398 int handle)
399{
Mathias Agopian50df2952010-07-19 19:09:10 -0700400 if (mInitCheck != NO_ERROR)
401 return mInitCheck;
402
Mathias Agopianfc328812010-07-14 23:41:37 -0700403 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800404 SensorInterface* sensor = mSensorMap.valueFor(handle);
405 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700406 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800407 SensorRecord* rec = mActiveSensors.valueFor(handle);
408 if (rec == 0) {
409 rec = new SensorRecord(connection);
410 mActiveSensors.add(handle, rec);
411 if (sensor->isVirtual()) {
412 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700413 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800414 } else {
415 if (rec->addConnection(connection)) {
416 // this sensor is already activated, but we are adding a
417 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800418 // known value of the requested sensor if it's not a
419 // "continuous" sensor.
420 if (sensor->getSensor().getMinDelay() == 0) {
421 sensors_event_t scratch;
422 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
423 if (event.version == sizeof(sensors_event_t)) {
424 connection->sendEvents(&event, 1);
425 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800426 }
427 }
428 }
429 if (err == NO_ERROR) {
430 // connection now active
431 if (connection->addSensor(handle)) {
432 // the sensor was added (which means it wasn't already there)
433 // so, see if this connection becomes active
434 if (mActiveConnections.indexOf(connection) < 0) {
435 mActiveConnections.add(connection);
436 }
437 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700438 }
439 }
440 return err;
441}
442
443status_t SensorService::disable(const sp<SensorEventConnection>& connection,
444 int handle)
445{
Mathias Agopian50df2952010-07-19 19:09:10 -0700446 if (mInitCheck != NO_ERROR)
447 return mInitCheck;
448
Mathias Agopianfc328812010-07-14 23:41:37 -0700449 status_t err = NO_ERROR;
450 Mutex::Autolock _l(mLock);
451 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700452 if (rec) {
453 // see if this connection becomes inactive
454 connection->removeSensor(handle);
455 if (connection->hasAnySensor() == false) {
456 mActiveConnections.remove(connection);
457 }
458 // see if this sensor becomes inactive
459 if (rec->removeConnection(connection)) {
460 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800461 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700462 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700463 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800464 SensorInterface* sensor = mSensorMap.valueFor(handle);
465 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700466 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700467 return err;
468}
469
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700470status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700471 int handle, nsecs_t ns)
472{
Mathias Agopian50df2952010-07-19 19:09:10 -0700473 if (mInitCheck != NO_ERROR)
474 return mInitCheck;
475
Mathias Agopianae09d652011-11-01 17:37:49 -0700476 SensorInterface* sensor = mSensorMap.valueFor(handle);
477 if (!sensor)
478 return BAD_VALUE;
479
Mathias Agopian1cd70002010-07-21 15:59:50 -0700480 if (ns < 0)
481 return BAD_VALUE;
482
Mathias Agopian62569ec2011-11-07 21:21:47 -0800483 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
484 if (ns < minDelayNs) {
485 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700486 }
487
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700488 if (ns < MINIMUM_EVENTS_PERIOD)
489 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700490
Mathias Agopianf001c922010-11-11 17:58:51 -0800491 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700492}
493
494// ---------------------------------------------------------------------------
495
496SensorService::SensorRecord::SensorRecord(
497 const sp<SensorEventConnection>& connection)
498{
499 mConnections.add(connection);
500}
501
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700502bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700503 const sp<SensorEventConnection>& connection)
504{
505 if (mConnections.indexOf(connection) < 0) {
506 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700507 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700508 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700509 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700510}
511
512bool SensorService::SensorRecord::removeConnection(
513 const wp<SensorEventConnection>& connection)
514{
515 ssize_t index = mConnections.indexOf(connection);
516 if (index >= 0) {
517 mConnections.removeItemsAt(index, 1);
518 }
519 return mConnections.size() ? false : true;
520}
521
522// ---------------------------------------------------------------------------
523
524SensorService::SensorEventConnection::SensorEventConnection(
525 const sp<SensorService>& service)
Mathias Agopianb3989272011-10-20 18:42:02 -0700526 : mService(service), mChannel(new BitTube())
Mathias Agopianfc328812010-07-14 23:41:37 -0700527{
528}
529
530SensorService::SensorEventConnection::~SensorEventConnection()
531{
Steve Blocka5512372011-12-20 16:23:08 +0000532 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700533 mService->cleanupConnection(this);
534}
535
536void SensorService::SensorEventConnection::onFirstRef()
537{
538}
539
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700540bool SensorService::SensorEventConnection::addSensor(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.indexOf(handle) <= 0) {
543 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700544 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700545 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700546 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700547}
548
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700549bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800550 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800551 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700552 return true;
553 }
554 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700555}
556
557bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800558 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800559 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700560}
561
562bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800563 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700564 return mSensorInfo.size() ? true : false;
565}
566
Mathias Agopianfc328812010-07-14 23:41:37 -0700567status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700568 sensors_event_t const* buffer, size_t numEvents,
569 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700570{
Mathias Agopiancf510012010-07-22 16:18:10 -0700571 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700572 size_t count = 0;
573 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800574 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700575 size_t i=0;
576 while (i<numEvents) {
577 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800578 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700579 do {
580 scratch[count++] = buffer[i++];
581 } while ((i<numEvents) && (buffer[i].sensor == curr));
582 } else {
583 i++;
584 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700585 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700586 } else {
587 scratch = const_cast<sensors_event_t *>(buffer);
588 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700589 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700590
Mathias Agopian907103b2012-04-02 18:38:02 -0700591 // NOTE: ASensorEvent and sensors_event_t are the same type
592 ssize_t size = SensorEventQueue::write(mChannel,
593 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700594 if (size == -EAGAIN) {
595 // the destination doesn't accept events anymore, it's probably
596 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000597 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700598 return size;
599 }
600
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700601 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700602}
603
Mathias Agopianb3989272011-10-20 18:42:02 -0700604sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700605{
606 return mChannel;
607}
608
609status_t SensorService::SensorEventConnection::enableDisable(
610 int handle, bool enabled)
611{
612 status_t err;
613 if (enabled) {
614 err = mService->enable(this, handle);
615 } else {
616 err = mService->disable(this, handle);
617 }
618 return err;
619}
620
621status_t SensorService::SensorEventConnection::setEventRate(
622 int handle, nsecs_t ns)
623{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700624 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700625}
626
627// ---------------------------------------------------------------------------
628}; // namespace android
629