blob: 6481584a627c282561d1229441abae26f26ea49d [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>
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070041#include <hardware_legacy/power.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070042
Mathias Agopian787ac1b2012-09-18 18:49:18 -070043#include "BatteryService.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070044#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080045#include "GravitySensor.h"
46#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070047#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080048#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070049#include "SensorFusion.h"
50#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070051
52namespace android {
53// ---------------------------------------------------------------------------
54
Mathias Agopian33015422011-05-27 18:18:13 -070055/*
56 * Notes:
57 *
58 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070059 * - run mag sensor from time to time to force calibration
60 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
61 *
62 */
63
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070064const char* SensorService::WAKE_LOCK_NAME = "SensorService";
65
Mathias Agopianfc328812010-07-14 23:41:37 -070066SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -070067 : mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070068{
69}
70
71void SensorService::onFirstRef()
72{
Steve Blocka5512372011-12-20 16:23:08 +000073 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070074
Mathias Agopianf001c922010-11-11 17:58:51 -080075 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070076
Mathias Agopianf001c922010-11-11 17:58:51 -080077 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080078 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070079 ssize_t count = dev.getSensorList(&list);
80 if (count > 0) {
81 ssize_t orientationIndex = -1;
82 bool hasGyro = false;
83 uint32_t virtualSensorsNeeds =
84 (1<<SENSOR_TYPE_GRAVITY) |
85 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
86 (1<<SENSOR_TYPE_ROTATION_VECTOR);
87
88 mLastEventSeen.setCapacity(count);
89 for (ssize_t i=0 ; i<count ; i++) {
90 registerSensor( new HardwareSensor(list[i]) );
91 switch (list[i].type) {
92 case SENSOR_TYPE_ORIENTATION:
93 orientationIndex = i;
94 break;
95 case SENSOR_TYPE_GYROSCOPE:
96 hasGyro = true;
97 break;
98 case SENSOR_TYPE_GRAVITY:
99 case SENSOR_TYPE_LINEAR_ACCELERATION:
100 case SENSOR_TYPE_ROTATION_VECTOR:
101 virtualSensorsNeeds &= ~(1<<list[i].type);
102 break;
103 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700104 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700105
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700106 // it's safe to instantiate the SensorFusion object here
107 // (it wants to be instantiated after h/w sensors have been
108 // registered)
109 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700110
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700111 if (hasGyro) {
112 // Always instantiate Android's virtual sensors. Since they are
113 // instantiated behind sensors from the HAL, they won't
114 // interfere with applications, unless they looks specifically
115 // for them (by name).
Mathias Agopian984826c2011-05-17 22:54:42 -0700116
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700117 registerVirtualSensor( new RotationVectorSensor() );
118 registerVirtualSensor( new GravitySensor(list, count) );
119 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian984826c2011-05-17 22:54:42 -0700120
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700121 // these are optional
122 registerVirtualSensor( new OrientationSensor() );
123 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33015422011-05-27 18:18:13 -0700124 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800125
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700126 // build the sensor list returned to users
127 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700128
129 if (hasGyro) {
130 // virtual debugging sensors are not added to mUserSensorList
131 registerVirtualSensor( new GyroDriftSensor() );
132 }
133
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700134 if (hasGyro &&
135 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
136 // if we have the fancy sensor fusion, and it's not provided by the
137 // HAL, use our own (fused) orientation sensor by removing the
138 // HAL supplied one form the user list.
139 if (orientationIndex >= 0) {
140 mUserSensorList.removeItemsAt(orientationIndex);
141 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700142 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700143
Mathias Agopian33264862012-06-28 19:46:54 -0700144 // debugging sensor list
145 for (size_t i=0 ; i<mSensorList.size() ; i++) {
146 switch (mSensorList[i].getType()) {
147 case SENSOR_TYPE_GRAVITY:
148 case SENSOR_TYPE_LINEAR_ACCELERATION:
149 case SENSOR_TYPE_ROTATION_VECTOR:
150 if (strstr(mSensorList[i].getVendor().string(), "Google")) {
151 mUserSensorListDebug.add(mSensorList[i]);
152 }
153 break;
154 default:
155 mUserSensorListDebug.add(mSensorList[i]);
156 break;
157 }
158 }
159
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700160 run("SensorService", PRIORITY_URGENT_DISPLAY);
161 mInitCheck = NO_ERROR;
162 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700163 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700164}
165
Mathias Agopianf001c922010-11-11 17:58:51 -0800166void SensorService::registerSensor(SensorInterface* s)
167{
168 sensors_event_t event;
169 memset(&event, 0, sizeof(event));
170
171 const Sensor sensor(s->getSensor());
172 // add to the sensor list (returned to clients)
173 mSensorList.add(sensor);
174 // add to our handle->SensorInterface mapping
175 mSensorMap.add(sensor.getHandle(), s);
176 // create an entry in the mLastEventSeen array
177 mLastEventSeen.add(sensor.getHandle(), event);
178}
179
180void SensorService::registerVirtualSensor(SensorInterface* s)
181{
182 registerSensor(s);
183 mVirtualSensorList.add( s );
184}
185
Mathias Agopianfc328812010-07-14 23:41:37 -0700186SensorService::~SensorService()
187{
Mathias Agopianf001c922010-11-11 17:58:51 -0800188 for (size_t i=0 ; i<mSensorMap.size() ; i++)
189 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700190}
191
Mathias Agopian1cb13462011-06-27 16:05:52 -0700192static const String16 sDump("android.permission.DUMP");
193
Mathias Agopianfc328812010-07-14 23:41:37 -0700194status_t SensorService::dump(int fd, const Vector<String16>& args)
195{
196 const size_t SIZE = 1024;
197 char buffer[SIZE];
198 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700199 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700200 snprintf(buffer, SIZE, "Permission Denial: "
201 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
202 IPCThreadState::self()->getCallingPid(),
203 IPCThreadState::self()->getCallingUid());
204 result.append(buffer);
205 } else {
206 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700207 snprintf(buffer, SIZE, "Sensor List:\n");
208 result.append(buffer);
209 for (size_t i=0 ; i<mSensorList.size() ; i++) {
210 const Sensor& s(mSensorList[i]);
211 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700212 snprintf(buffer, SIZE,
213 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
214 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700215 s.getName().string(),
216 s.getVendor().string(),
217 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700218 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700219 e.data[0], e.data[1], e.data[2]);
220 result.append(buffer);
221 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700222 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800223 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700224
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700225 snprintf(buffer, SIZE, "%d active connections\n",
226 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700227 result.append(buffer);
228 snprintf(buffer, SIZE, "Active sensors:\n");
229 result.append(buffer);
230 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700231 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800232 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700233 getSensorName(handle).string(),
234 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700235 mActiveSensors.valueAt(i)->getNumConnections());
236 result.append(buffer);
237 }
238 }
239 write(fd, result.string(), result.size());
240 return NO_ERROR;
241}
242
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700243void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
244 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700245 SensorInterface* sensor;
246 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700247 for (int i=0 ; i<count ; i++) {
248 int handle = buffer[i].sensor;
249 if (getSensorType(handle) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
250 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700251 sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700252 if (sensor != NULL) {
253 sensor->autoDisable(connection.get(), handle);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700254 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700255 cleanupWithoutDisable(connection, handle);
256 }
257 }
258 }
259}
260
Mathias Agopianfc328812010-07-14 23:41:37 -0700261bool SensorService::threadLoop()
262{
Steve Blocka5512372011-12-20 16:23:08 +0000263 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700264
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700265 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700266 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700267 sensors_event_t buffer[minBufferSize];
268 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800269 SensorDevice& device(SensorDevice::getInstance());
270 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700271
Mathias Agopianf001c922010-11-11 17:58:51 -0800272 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700273 bool wakeLockAcquired = false;
274 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700275 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800276 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700277 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000278 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700279 break;
280 }
281
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700282 // Poll has returned. Hold a wakelock.
283 // Todo(): add a flag to the sensors definitions to indicate
284 // the sensors which can wake up the AP
285 for (int i = 0; i < count; i++) {
286 if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
287 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
288 wakeLockAcquired = true;
289 break;
290 }
291 }
292
Mathias Agopian94e8f682010-11-10 17:50:28 -0800293 recordLastValue(buffer, count);
294
Mathias Agopianf001c922010-11-11 17:58:51 -0800295 // handle virtual sensors
296 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700297 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800298 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
299 getActiveVirtualSensors());
300 const size_t activeVirtualSensorCount = virtualSensors.size();
301 if (activeVirtualSensorCount) {
302 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700303 SensorFusion& fusion(SensorFusion::getInstance());
304 if (fusion.isEnabled()) {
305 for (size_t i=0 ; i<size_t(count) ; i++) {
306 fusion.process(event[i]);
307 }
308 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700309 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800310 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700311 if (count + k >= minBufferSize) {
312 ALOGE("buffer too small to hold all events: "
313 "count=%u, k=%u, size=%u",
314 count, k, minBufferSize);
315 break;
316 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800317 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700318 SensorInterface* si = virtualSensors.valueAt(j);
319 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800320 buffer[count + k] = out;
321 k++;
322 }
323 }
324 }
325 if (k) {
326 // record the last synthesized values
327 recordLastValue(&buffer[count], k);
328 count += k;
329 // sort the buffer by time-stamps
330 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700331 }
332 }
333 }
334
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700335 // handle backward compatibility for RotationVector sensor
336 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
337 for (int i = 0; i < count; i++) {
338 if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_ROTATION_VECTOR) {
339 // All the 4 components of the quaternion should be available
340 // No heading accuracy. Set it to -1
341 buffer[i].data[4] = -1;
342 }
343 }
344 }
345
Mathias Agopianf001c922010-11-11 17:58:51 -0800346 // send our events to clients...
347 const SortedVector< wp<SensorEventConnection> > activeConnections(
348 getActiveConnections());
349 size_t numConnections = activeConnections.size();
350 for (size_t i=0 ; i<numConnections ; i++) {
351 sp<SensorEventConnection> connection(
352 activeConnections[i].promote());
353 if (connection != 0) {
354 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700355 // Some sensors need to be auto disabled after the trigger
356 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800357 }
358 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700359
360 // We have read the data, upper layers should hold the wakelock.
361 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
362
Mathias Agopianfc328812010-07-14 23:41:37 -0700363 } while (count >= 0 || Thread::exitPending());
364
Steve Block3c20fbe2012-01-05 23:22:43 +0000365 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800366 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700367 return false;
368}
369
Mathias Agopian94e8f682010-11-10 17:50:28 -0800370void SensorService::recordLastValue(
371 sensors_event_t const * buffer, size_t count)
372{
373 Mutex::Autolock _l(mLock);
374
375 // record the last event for each sensor
376 int32_t prev = buffer[0].sensor;
377 for (size_t i=1 ; i<count ; i++) {
378 // record the last event of each sensor type in this buffer
379 int32_t curr = buffer[i].sensor;
380 if (curr != prev) {
381 mLastEventSeen.editValueFor(prev) = buffer[i-1];
382 prev = curr;
383 }
384 }
385 mLastEventSeen.editValueFor(prev) = buffer[count-1];
386}
387
Mathias Agopianf001c922010-11-11 17:58:51 -0800388void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
389{
390 struct compar {
391 static int cmp(void const* lhs, void const* rhs) {
392 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
393 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700394 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800395 }
396 };
397 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
398}
399
Mathias Agopianfc328812010-07-14 23:41:37 -0700400SortedVector< wp<SensorService::SensorEventConnection> >
401SensorService::getActiveConnections() const
402{
403 Mutex::Autolock _l(mLock);
404 return mActiveConnections;
405}
406
Mathias Agopianf001c922010-11-11 17:58:51 -0800407DefaultKeyedVector<int, SensorInterface*>
408SensorService::getActiveVirtualSensors() const
409{
410 Mutex::Autolock _l(mLock);
411 return mActiveVirtualSensors;
412}
413
Mathias Agopian5d270722010-07-19 15:20:39 -0700414String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700415 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700416 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700417 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700418 if (sensor.getHandle() == handle) {
419 return sensor.getName();
420 }
421 }
422 String8 result("unknown");
423 return result;
424}
425
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700426int SensorService::getSensorType(int handle) const {
427 size_t count = mUserSensorList.size();
428 for (size_t i=0 ; i<count ; i++) {
429 const Sensor& sensor(mUserSensorList[i]);
430 if (sensor.getHandle() == handle) {
431 return sensor.getType();
432 }
433 }
434 return -1;
435}
436
437
Mathias Agopianfc328812010-07-14 23:41:37 -0700438Vector<Sensor> SensorService::getSensorList()
439{
Mathias Agopian33264862012-06-28 19:46:54 -0700440 char value[PROPERTY_VALUE_MAX];
441 property_get("debug.sensors", value, "0");
442 if (atoi(value)) {
443 return mUserSensorListDebug;
444 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700445 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700446}
447
448sp<ISensorEventConnection> SensorService::createSensorEventConnection()
449{
Mathias Agopian5307d172012-09-18 17:02:43 -0700450 uid_t uid = IPCThreadState::self()->getCallingUid();
451 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700452 return result;
453}
454
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800455void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700456{
457 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800458 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700459 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000460 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700461 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800462 int handle = mActiveSensors.keyAt(i);
463 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000464 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800465 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000466 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800467 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800468 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800469 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800470 }
471 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000472 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000473 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700474 "removing connection %p for sensor[%d].handle=0x%08x",
475 c, i, handle);
476
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800477 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000478 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700479 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800480 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700481 delete rec;
482 size--;
483 } else {
484 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700485 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700486 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700487 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700488 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700489}
490
491status_t SensorService::enable(const sp<SensorEventConnection>& connection,
492 int handle)
493{
Mathias Agopian50df2952010-07-19 19:09:10 -0700494 if (mInitCheck != NO_ERROR)
495 return mInitCheck;
496
Mathias Agopianf001c922010-11-11 17:58:51 -0800497 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700498 if (sensor == NULL) {
499 return BAD_VALUE;
500 }
501
502 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700503 SensorRecord* rec = mActiveSensors.valueFor(handle);
504 if (rec == 0) {
505 rec = new SensorRecord(connection);
506 mActiveSensors.add(handle, rec);
507 if (sensor->isVirtual()) {
508 mActiveVirtualSensors.add(handle, sensor);
509 }
510 } else {
511 if (rec->addConnection(connection)) {
512 // this sensor is already activated, but we are adding a
513 // connection that uses it. Immediately send down the last
514 // known value of the requested sensor if it's not a
515 // "continuous" sensor.
516 if (sensor->getSensor().getMinDelay() == 0) {
517 sensors_event_t scratch;
518 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
519 if (event.version == sizeof(sensors_event_t)) {
520 connection->sendEvents(&event, 1);
521 }
522 }
523 }
524 }
525
526 if (connection->addSensor(handle)) {
527 BatteryService::enableSensor(connection->getUid(), handle);
528 // the sensor was added (which means it wasn't already there)
529 // so, see if this connection becomes active
530 if (mActiveConnections.indexOf(connection) < 0) {
531 mActiveConnections.add(connection);
532 }
533 } else {
534 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
535 handle, connection.get());
536 }
537
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700538 // we are setup, now enable the sensor.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700539 status_t err = sensor->activate(connection.get(), true);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700540 if (err != NO_ERROR) {
541 // enable has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700542 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700543 }
544 return err;
545}
546
547status_t SensorService::disable(const sp<SensorEventConnection>& connection,
548 int handle)
549{
Mathias Agopian50df2952010-07-19 19:09:10 -0700550 if (mInitCheck != NO_ERROR)
551 return mInitCheck;
552
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700553 Mutex::Autolock _l(mLock);
554 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700555 if (err == NO_ERROR) {
556 SensorInterface* sensor = mSensorMap.valueFor(handle);
557 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
558 }
559 return err;
560}
561
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700562status_t SensorService::cleanupWithoutDisable(
563 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700564 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700565 return cleanupWithoutDisableLocked(connection, handle);
566}
567
568status_t SensorService::cleanupWithoutDisableLocked(
569 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700570 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700571 if (rec) {
572 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700573 if (connection->removeSensor(handle)) {
574 BatteryService::disableSensor(connection->getUid(), handle);
575 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700576 if (connection->hasAnySensor() == false) {
577 mActiveConnections.remove(connection);
578 }
579 // see if this sensor becomes inactive
580 if (rec->removeConnection(connection)) {
581 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800582 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700583 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700584 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700585 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700586 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700587 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700588}
589
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700590status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700591 int handle, nsecs_t ns)
592{
Mathias Agopian50df2952010-07-19 19:09:10 -0700593 if (mInitCheck != NO_ERROR)
594 return mInitCheck;
595
Mathias Agopianae09d652011-11-01 17:37:49 -0700596 SensorInterface* sensor = mSensorMap.valueFor(handle);
597 if (!sensor)
598 return BAD_VALUE;
599
Mathias Agopian1cd70002010-07-21 15:59:50 -0700600 if (ns < 0)
601 return BAD_VALUE;
602
Mathias Agopian62569ec2011-11-07 21:21:47 -0800603 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
604 if (ns < minDelayNs) {
605 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700606 }
607
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700608 if (ns < MINIMUM_EVENTS_PERIOD)
609 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700610
Mathias Agopianf001c922010-11-11 17:58:51 -0800611 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700612}
613
614// ---------------------------------------------------------------------------
615
616SensorService::SensorRecord::SensorRecord(
617 const sp<SensorEventConnection>& connection)
618{
619 mConnections.add(connection);
620}
621
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700622bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700623 const sp<SensorEventConnection>& connection)
624{
625 if (mConnections.indexOf(connection) < 0) {
626 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700627 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700628 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700629 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700630}
631
632bool SensorService::SensorRecord::removeConnection(
633 const wp<SensorEventConnection>& connection)
634{
635 ssize_t index = mConnections.indexOf(connection);
636 if (index >= 0) {
637 mConnections.removeItemsAt(index, 1);
638 }
639 return mConnections.size() ? false : true;
640}
641
642// ---------------------------------------------------------------------------
643
644SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700645 const sp<SensorService>& service, uid_t uid)
646 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700647{
648}
649
650SensorService::SensorEventConnection::~SensorEventConnection()
651{
Steve Blocka5512372011-12-20 16:23:08 +0000652 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700653 mService->cleanupConnection(this);
654}
655
656void SensorService::SensorEventConnection::onFirstRef()
657{
658}
659
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700660bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800661 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700662 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800663 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700664 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700665 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700666 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700667}
668
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700669bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800670 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800671 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700672 return true;
673 }
674 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700675}
676
677bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800678 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800679 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700680}
681
682bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800683 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700684 return mSensorInfo.size() ? true : false;
685}
686
Mathias Agopianfc328812010-07-14 23:41:37 -0700687status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700688 sensors_event_t const* buffer, size_t numEvents,
689 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700690{
Mathias Agopiancf510012010-07-22 16:18:10 -0700691 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700692 size_t count = 0;
693 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800694 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700695 size_t i=0;
696 while (i<numEvents) {
697 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800698 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700699 do {
700 scratch[count++] = buffer[i++];
701 } while ((i<numEvents) && (buffer[i].sensor == curr));
702 } else {
703 i++;
704 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700705 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700706 } else {
707 scratch = const_cast<sensors_event_t *>(buffer);
708 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700709 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700710
Mathias Agopian907103b2012-04-02 18:38:02 -0700711 // NOTE: ASensorEvent and sensors_event_t are the same type
712 ssize_t size = SensorEventQueue::write(mChannel,
713 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700714 if (size == -EAGAIN) {
715 // the destination doesn't accept events anymore, it's probably
716 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000717 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700718 return size;
719 }
720
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700721 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700722}
723
Mathias Agopianb3989272011-10-20 18:42:02 -0700724sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700725{
726 return mChannel;
727}
728
729status_t SensorService::SensorEventConnection::enableDisable(
730 int handle, bool enabled)
731{
732 status_t err;
733 if (enabled) {
734 err = mService->enable(this, handle);
735 } else {
736 err = mService->disable(this, handle);
737 }
738 return err;
739}
740
741status_t SensorService::SensorEventConnection::setEventRate(
742 int handle, nsecs_t ns)
743{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700744 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700745}
746
747// ---------------------------------------------------------------------------
748}; // namespace android
749