blob: cc2f745ea8738a9722e673316215854154b3c478 [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 Agopianf001c922010-11-11 17:58:51 -0800121
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700122 // build the sensor list returned to users
123 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700124
125 if (hasGyro) {
126 // virtual debugging sensors are not added to mUserSensorList
127 registerVirtualSensor( new GyroDriftSensor() );
128 }
129
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700130 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 Agopian33264862012-06-28 19:46:54 -0700140 // debugging sensor list
141 for (size_t i=0 ; i<mSensorList.size() ; i++) {
142 switch (mSensorList[i].getType()) {
143 case SENSOR_TYPE_GRAVITY:
144 case SENSOR_TYPE_LINEAR_ACCELERATION:
145 case SENSOR_TYPE_ROTATION_VECTOR:
146 if (strstr(mSensorList[i].getVendor().string(), "Google")) {
147 mUserSensorListDebug.add(mSensorList[i]);
148 }
149 break;
150 default:
151 mUserSensorListDebug.add(mSensorList[i]);
152 break;
153 }
154 }
155
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700156 run("SensorService", PRIORITY_URGENT_DISPLAY);
157 mInitCheck = NO_ERROR;
158 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700159 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700160}
161
Mathias Agopianf001c922010-11-11 17:58:51 -0800162void SensorService::registerSensor(SensorInterface* s)
163{
164 sensors_event_t event;
165 memset(&event, 0, sizeof(event));
166
167 const Sensor sensor(s->getSensor());
168 // add to the sensor list (returned to clients)
169 mSensorList.add(sensor);
170 // add to our handle->SensorInterface mapping
171 mSensorMap.add(sensor.getHandle(), s);
172 // create an entry in the mLastEventSeen array
173 mLastEventSeen.add(sensor.getHandle(), event);
174}
175
176void SensorService::registerVirtualSensor(SensorInterface* s)
177{
178 registerSensor(s);
179 mVirtualSensorList.add( s );
180}
181
Mathias Agopianfc328812010-07-14 23:41:37 -0700182SensorService::~SensorService()
183{
Mathias Agopianf001c922010-11-11 17:58:51 -0800184 for (size_t i=0 ; i<mSensorMap.size() ; i++)
185 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700186}
187
Mathias Agopian1cb13462011-06-27 16:05:52 -0700188static const String16 sDump("android.permission.DUMP");
189
Mathias Agopianfc328812010-07-14 23:41:37 -0700190status_t SensorService::dump(int fd, const Vector<String16>& args)
191{
192 const size_t SIZE = 1024;
193 char buffer[SIZE];
194 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700195 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700196 snprintf(buffer, SIZE, "Permission Denial: "
197 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
198 IPCThreadState::self()->getCallingPid(),
199 IPCThreadState::self()->getCallingUid());
200 result.append(buffer);
201 } else {
202 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700203 snprintf(buffer, SIZE, "Sensor List:\n");
204 result.append(buffer);
205 for (size_t i=0 ; i<mSensorList.size() ; i++) {
206 const Sensor& s(mSensorList[i]);
207 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700208 snprintf(buffer, SIZE,
209 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
210 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700211 s.getName().string(),
212 s.getVendor().string(),
213 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700214 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700215 e.data[0], e.data[1], e.data[2]);
216 result.append(buffer);
217 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700218 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800219 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700220
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700221 snprintf(buffer, SIZE, "%d active connections\n",
222 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700223 result.append(buffer);
224 snprintf(buffer, SIZE, "Active sensors:\n");
225 result.append(buffer);
226 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700227 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800228 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700229 getSensorName(handle).string(),
230 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700231 mActiveSensors.valueAt(i)->getNumConnections());
232 result.append(buffer);
233 }
234 }
235 write(fd, result.string(), result.size());
236 return NO_ERROR;
237}
238
239bool SensorService::threadLoop()
240{
Steve Blocka5512372011-12-20 16:23:08 +0000241 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700242
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700243 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700244 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700245 sensors_event_t buffer[minBufferSize];
246 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800247 SensorDevice& device(SensorDevice::getInstance());
248 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700249
Mathias Agopianf001c922010-11-11 17:58:51 -0800250 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700251 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800252 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700253 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000254 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700255 break;
256 }
257
Mathias Agopian94e8f682010-11-10 17:50:28 -0800258 recordLastValue(buffer, count);
259
Mathias Agopianf001c922010-11-11 17:58:51 -0800260 // handle virtual sensors
261 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700262 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800263 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
264 getActiveVirtualSensors());
265 const size_t activeVirtualSensorCount = virtualSensors.size();
266 if (activeVirtualSensorCount) {
267 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700268 SensorFusion& fusion(SensorFusion::getInstance());
269 if (fusion.isEnabled()) {
270 for (size_t i=0 ; i<size_t(count) ; i++) {
271 fusion.process(event[i]);
272 }
273 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700274 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800275 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700276 if (count + k >= minBufferSize) {
277 ALOGE("buffer too small to hold all events: "
278 "count=%u, k=%u, size=%u",
279 count, k, minBufferSize);
280 break;
281 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800282 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700283 SensorInterface* si = virtualSensors.valueAt(j);
284 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800285 buffer[count + k] = out;
286 k++;
287 }
288 }
289 }
290 if (k) {
291 // record the last synthesized values
292 recordLastValue(&buffer[count], k);
293 count += k;
294 // sort the buffer by time-stamps
295 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700296 }
297 }
298 }
299
Mathias Agopianf001c922010-11-11 17:58:51 -0800300 // send our events to clients...
301 const SortedVector< wp<SensorEventConnection> > activeConnections(
302 getActiveConnections());
303 size_t numConnections = activeConnections.size();
304 for (size_t i=0 ; i<numConnections ; i++) {
305 sp<SensorEventConnection> connection(
306 activeConnections[i].promote());
307 if (connection != 0) {
308 connection->sendEvents(buffer, count, scratch);
309 }
310 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700311 } while (count >= 0 || Thread::exitPending());
312
Steve Block3c20fbe2012-01-05 23:22:43 +0000313 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800314 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700315 return false;
316}
317
Mathias Agopian94e8f682010-11-10 17:50:28 -0800318void SensorService::recordLastValue(
319 sensors_event_t const * buffer, size_t count)
320{
321 Mutex::Autolock _l(mLock);
322
323 // record the last event for each sensor
324 int32_t prev = buffer[0].sensor;
325 for (size_t i=1 ; i<count ; i++) {
326 // record the last event of each sensor type in this buffer
327 int32_t curr = buffer[i].sensor;
328 if (curr != prev) {
329 mLastEventSeen.editValueFor(prev) = buffer[i-1];
330 prev = curr;
331 }
332 }
333 mLastEventSeen.editValueFor(prev) = buffer[count-1];
334}
335
Mathias Agopianf001c922010-11-11 17:58:51 -0800336void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
337{
338 struct compar {
339 static int cmp(void const* lhs, void const* rhs) {
340 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
341 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700342 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800343 }
344 };
345 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
346}
347
Mathias Agopianfc328812010-07-14 23:41:37 -0700348SortedVector< wp<SensorService::SensorEventConnection> >
349SensorService::getActiveConnections() const
350{
351 Mutex::Autolock _l(mLock);
352 return mActiveConnections;
353}
354
Mathias Agopianf001c922010-11-11 17:58:51 -0800355DefaultKeyedVector<int, SensorInterface*>
356SensorService::getActiveVirtualSensors() const
357{
358 Mutex::Autolock _l(mLock);
359 return mActiveVirtualSensors;
360}
361
Mathias Agopian5d270722010-07-19 15:20:39 -0700362String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700363 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700364 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700365 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700366 if (sensor.getHandle() == handle) {
367 return sensor.getName();
368 }
369 }
370 String8 result("unknown");
371 return result;
372}
373
Mathias Agopianfc328812010-07-14 23:41:37 -0700374Vector<Sensor> SensorService::getSensorList()
375{
Mathias Agopian33264862012-06-28 19:46:54 -0700376 char value[PROPERTY_VALUE_MAX];
377 property_get("debug.sensors", value, "0");
378 if (atoi(value)) {
379 return mUserSensorListDebug;
380 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700381 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700382}
383
384sp<ISensorEventConnection> SensorService::createSensorEventConnection()
385{
Mathias Agopian5307d172012-09-18 17:02:43 -0700386 uid_t uid = IPCThreadState::self()->getCallingUid();
387 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700388 return result;
389}
390
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800391void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700392{
393 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800394 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700395 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000396 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700397 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800398 int handle = mActiveSensors.keyAt(i);
399 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000400 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800401 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000402 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800403 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800404 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800405 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800406 }
407 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000408 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000409 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700410 "removing connection %p for sensor[%d].handle=0x%08x",
411 c, i, handle);
412
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800413 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000414 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700415 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800416 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700417 delete rec;
418 size--;
419 } else {
420 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700421 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700422 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700423 mActiveConnections.remove(connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700424}
425
426status_t SensorService::enable(const sp<SensorEventConnection>& connection,
427 int handle)
428{
Mathias Agopian50df2952010-07-19 19:09:10 -0700429 if (mInitCheck != NO_ERROR)
430 return mInitCheck;
431
Mathias Agopianfc328812010-07-14 23:41:37 -0700432 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800433 SensorInterface* sensor = mSensorMap.valueFor(handle);
434 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700435 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800436 SensorRecord* rec = mActiveSensors.valueFor(handle);
437 if (rec == 0) {
438 rec = new SensorRecord(connection);
439 mActiveSensors.add(handle, rec);
440 if (sensor->isVirtual()) {
441 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700442 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800443 } else {
444 if (rec->addConnection(connection)) {
445 // this sensor is already activated, but we are adding a
446 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800447 // known value of the requested sensor if it's not a
448 // "continuous" sensor.
449 if (sensor->getSensor().getMinDelay() == 0) {
450 sensors_event_t scratch;
451 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
452 if (event.version == sizeof(sensors_event_t)) {
453 connection->sendEvents(&event, 1);
454 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800455 }
456 }
457 }
458 if (err == NO_ERROR) {
459 // connection now active
460 if (connection->addSensor(handle)) {
461 // the sensor was added (which means it wasn't already there)
462 // so, see if this connection becomes active
463 if (mActiveConnections.indexOf(connection) < 0) {
464 mActiveConnections.add(connection);
465 }
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700466 } else {
467 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
468 handle, connection.get());
Mathias Agopianf001c922010-11-11 17:58:51 -0800469 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700470 }
471 }
472 return err;
473}
474
475status_t SensorService::disable(const sp<SensorEventConnection>& connection,
476 int handle)
477{
Mathias Agopian50df2952010-07-19 19:09:10 -0700478 if (mInitCheck != NO_ERROR)
479 return mInitCheck;
480
Mathias Agopianfc328812010-07-14 23:41:37 -0700481 status_t err = NO_ERROR;
482 Mutex::Autolock _l(mLock);
483 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700484 if (rec) {
485 // see if this connection becomes inactive
486 connection->removeSensor(handle);
487 if (connection->hasAnySensor() == false) {
488 mActiveConnections.remove(connection);
489 }
490 // see if this sensor becomes inactive
491 if (rec->removeConnection(connection)) {
492 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800493 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700494 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700495 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800496 SensorInterface* sensor = mSensorMap.valueFor(handle);
497 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700498 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700499 return err;
500}
501
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700502status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700503 int handle, nsecs_t ns)
504{
Mathias Agopian50df2952010-07-19 19:09:10 -0700505 if (mInitCheck != NO_ERROR)
506 return mInitCheck;
507
Mathias Agopianae09d652011-11-01 17:37:49 -0700508 SensorInterface* sensor = mSensorMap.valueFor(handle);
509 if (!sensor)
510 return BAD_VALUE;
511
Mathias Agopian1cd70002010-07-21 15:59:50 -0700512 if (ns < 0)
513 return BAD_VALUE;
514
Mathias Agopian62569ec2011-11-07 21:21:47 -0800515 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
516 if (ns < minDelayNs) {
517 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700518 }
519
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700520 if (ns < MINIMUM_EVENTS_PERIOD)
521 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700522
Mathias Agopianf001c922010-11-11 17:58:51 -0800523 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700524}
525
526// ---------------------------------------------------------------------------
527
528SensorService::SensorRecord::SensorRecord(
529 const sp<SensorEventConnection>& connection)
530{
531 mConnections.add(connection);
532}
533
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700534bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700535 const sp<SensorEventConnection>& connection)
536{
537 if (mConnections.indexOf(connection) < 0) {
538 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700539 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700540 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700541 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700542}
543
544bool SensorService::SensorRecord::removeConnection(
545 const wp<SensorEventConnection>& connection)
546{
547 ssize_t index = mConnections.indexOf(connection);
548 if (index >= 0) {
549 mConnections.removeItemsAt(index, 1);
550 }
551 return mConnections.size() ? false : true;
552}
553
554// ---------------------------------------------------------------------------
555
556SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700557 const sp<SensorService>& service, uid_t uid)
558 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700559{
560}
561
562SensorService::SensorEventConnection::~SensorEventConnection()
563{
Steve Blocka5512372011-12-20 16:23:08 +0000564 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700565 mService->cleanupConnection(this);
566}
567
568void SensorService::SensorEventConnection::onFirstRef()
569{
570}
571
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700572bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800573 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700574 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800575 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700576 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700577 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700578 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700579}
580
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700581bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800582 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800583 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700584 return true;
585 }
586 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700587}
588
589bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800590 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800591 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700592}
593
594bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800595 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700596 return mSensorInfo.size() ? true : false;
597}
598
Mathias Agopianfc328812010-07-14 23:41:37 -0700599status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700600 sensors_event_t const* buffer, size_t numEvents,
601 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700602{
Mathias Agopiancf510012010-07-22 16:18:10 -0700603 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700604 size_t count = 0;
605 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800606 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700607 size_t i=0;
608 while (i<numEvents) {
609 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800610 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700611 do {
612 scratch[count++] = buffer[i++];
613 } while ((i<numEvents) && (buffer[i].sensor == curr));
614 } else {
615 i++;
616 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700617 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700618 } else {
619 scratch = const_cast<sensors_event_t *>(buffer);
620 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700621 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700622
Mathias Agopian907103b2012-04-02 18:38:02 -0700623 // NOTE: ASensorEvent and sensors_event_t are the same type
624 ssize_t size = SensorEventQueue::write(mChannel,
625 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700626 if (size == -EAGAIN) {
627 // the destination doesn't accept events anymore, it's probably
628 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000629 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700630 return size;
631 }
632
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700633 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700634}
635
Mathias Agopianb3989272011-10-20 18:42:02 -0700636sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700637{
638 return mChannel;
639}
640
641status_t SensorService::SensorEventConnection::enableDisable(
642 int handle, bool enabled)
643{
644 status_t err;
645 if (enabled) {
646 err = mService->enable(this, handle);
647 } else {
648 err = mService->disable(this, handle);
649 }
650 return err;
651}
652
653status_t SensorService::SensorEventConnection::setEventRate(
654 int handle, nsecs_t ns)
655{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700656 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700657}
658
659// ---------------------------------------------------------------------------
660}; // namespace android
661