blob: ebf5cf0c7dd228c33e8dba38d1b58ffbbed61b5c [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);
252 err = sensor ?sensor->resetStateWithoutActuatingHardware(connection.get(), handle)
253 : status_t(BAD_VALUE);
254 if (err != NO_ERROR) {
255 ALOGE("Sensor Inteface: Resetting state failed with err: %d", err);
256 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700257 cleanupWithoutDisable(connection, handle);
258 }
259 }
260 }
261}
262
Mathias Agopianfc328812010-07-14 23:41:37 -0700263bool SensorService::threadLoop()
264{
Steve Blocka5512372011-12-20 16:23:08 +0000265 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700266
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700267 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700268 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700269 sensors_event_t buffer[minBufferSize];
270 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800271 SensorDevice& device(SensorDevice::getInstance());
272 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700273
Mathias Agopianf001c922010-11-11 17:58:51 -0800274 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700275 bool wakeLockAcquired = false;
276 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700277 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800278 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700279 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000280 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700281 break;
282 }
283
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700284 // Poll has returned. Hold a wakelock.
285 // Todo(): add a flag to the sensors definitions to indicate
286 // the sensors which can wake up the AP
287 for (int i = 0; i < count; i++) {
288 if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
289 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
290 wakeLockAcquired = true;
291 break;
292 }
293 }
294
Mathias Agopian94e8f682010-11-10 17:50:28 -0800295 recordLastValue(buffer, count);
296
Mathias Agopianf001c922010-11-11 17:58:51 -0800297 // handle virtual sensors
298 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700299 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800300 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
301 getActiveVirtualSensors());
302 const size_t activeVirtualSensorCount = virtualSensors.size();
303 if (activeVirtualSensorCount) {
304 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700305 SensorFusion& fusion(SensorFusion::getInstance());
306 if (fusion.isEnabled()) {
307 for (size_t i=0 ; i<size_t(count) ; i++) {
308 fusion.process(event[i]);
309 }
310 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700311 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800312 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700313 if (count + k >= minBufferSize) {
314 ALOGE("buffer too small to hold all events: "
315 "count=%u, k=%u, size=%u",
316 count, k, minBufferSize);
317 break;
318 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800319 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700320 SensorInterface* si = virtualSensors.valueAt(j);
321 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800322 buffer[count + k] = out;
323 k++;
324 }
325 }
326 }
327 if (k) {
328 // record the last synthesized values
329 recordLastValue(&buffer[count], k);
330 count += k;
331 // sort the buffer by time-stamps
332 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700333 }
334 }
335 }
336
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700337 // handle backward compatibility for RotationVector sensor
338 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
339 for (int i = 0; i < count; i++) {
340 if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_ROTATION_VECTOR) {
341 // All the 4 components of the quaternion should be available
342 // No heading accuracy. Set it to -1
343 buffer[i].data[4] = -1;
344 }
345 }
346 }
347
Mathias Agopianf001c922010-11-11 17:58:51 -0800348 // send our events to clients...
349 const SortedVector< wp<SensorEventConnection> > activeConnections(
350 getActiveConnections());
351 size_t numConnections = activeConnections.size();
352 for (size_t i=0 ; i<numConnections ; i++) {
353 sp<SensorEventConnection> connection(
354 activeConnections[i].promote());
355 if (connection != 0) {
356 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700357 // Some sensors need to be auto disabled after the trigger
358 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800359 }
360 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700361
362 // We have read the data, upper layers should hold the wakelock.
363 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
364
Mathias Agopianfc328812010-07-14 23:41:37 -0700365 } while (count >= 0 || Thread::exitPending());
366
Steve Block3c20fbe2012-01-05 23:22:43 +0000367 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800368 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700369 return false;
370}
371
Mathias Agopian94e8f682010-11-10 17:50:28 -0800372void SensorService::recordLastValue(
373 sensors_event_t const * buffer, size_t count)
374{
375 Mutex::Autolock _l(mLock);
376
377 // record the last event for each sensor
378 int32_t prev = buffer[0].sensor;
379 for (size_t i=1 ; i<count ; i++) {
380 // record the last event of each sensor type in this buffer
381 int32_t curr = buffer[i].sensor;
382 if (curr != prev) {
383 mLastEventSeen.editValueFor(prev) = buffer[i-1];
384 prev = curr;
385 }
386 }
387 mLastEventSeen.editValueFor(prev) = buffer[count-1];
388}
389
Mathias Agopianf001c922010-11-11 17:58:51 -0800390void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
391{
392 struct compar {
393 static int cmp(void const* lhs, void const* rhs) {
394 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
395 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700396 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800397 }
398 };
399 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
400}
401
Mathias Agopianfc328812010-07-14 23:41:37 -0700402SortedVector< wp<SensorService::SensorEventConnection> >
403SensorService::getActiveConnections() const
404{
405 Mutex::Autolock _l(mLock);
406 return mActiveConnections;
407}
408
Mathias Agopianf001c922010-11-11 17:58:51 -0800409DefaultKeyedVector<int, SensorInterface*>
410SensorService::getActiveVirtualSensors() const
411{
412 Mutex::Autolock _l(mLock);
413 return mActiveVirtualSensors;
414}
415
Mathias Agopian5d270722010-07-19 15:20:39 -0700416String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700417 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700418 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700419 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700420 if (sensor.getHandle() == handle) {
421 return sensor.getName();
422 }
423 }
424 String8 result("unknown");
425 return result;
426}
427
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700428int SensorService::getSensorType(int handle) const {
429 size_t count = mUserSensorList.size();
430 for (size_t i=0 ; i<count ; i++) {
431 const Sensor& sensor(mUserSensorList[i]);
432 if (sensor.getHandle() == handle) {
433 return sensor.getType();
434 }
435 }
436 return -1;
437}
438
439
Mathias Agopianfc328812010-07-14 23:41:37 -0700440Vector<Sensor> SensorService::getSensorList()
441{
Mathias Agopian33264862012-06-28 19:46:54 -0700442 char value[PROPERTY_VALUE_MAX];
443 property_get("debug.sensors", value, "0");
444 if (atoi(value)) {
445 return mUserSensorListDebug;
446 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700447 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700448}
449
450sp<ISensorEventConnection> SensorService::createSensorEventConnection()
451{
Mathias Agopian5307d172012-09-18 17:02:43 -0700452 uid_t uid = IPCThreadState::self()->getCallingUid();
453 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700454 return result;
455}
456
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800457void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700458{
459 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800460 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700461 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000462 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700463 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800464 int handle = mActiveSensors.keyAt(i);
465 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000466 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800467 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000468 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800469 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800470 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800471 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800472 }
473 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000474 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000475 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700476 "removing connection %p for sensor[%d].handle=0x%08x",
477 c, i, handle);
478
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800479 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000480 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700481 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800482 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700483 delete rec;
484 size--;
485 } else {
486 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700487 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700488 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700489 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700490 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700491}
492
493status_t SensorService::enable(const sp<SensorEventConnection>& connection,
494 int handle)
495{
Mathias Agopian50df2952010-07-19 19:09:10 -0700496 if (mInitCheck != NO_ERROR)
497 return mInitCheck;
498
Mathias Agopianfc328812010-07-14 23:41:37 -0700499 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800500 SensorInterface* sensor = mSensorMap.valueFor(handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700501 SensorRecord* rec = mActiveSensors.valueFor(handle);
502 if (rec == 0) {
503 rec = new SensorRecord(connection);
504 mActiveSensors.add(handle, rec);
505 if (sensor->isVirtual()) {
506 mActiveVirtualSensors.add(handle, sensor);
507 }
508 } else {
509 if (rec->addConnection(connection)) {
510 // this sensor is already activated, but we are adding a
511 // connection that uses it. Immediately send down the last
512 // known value of the requested sensor if it's not a
513 // "continuous" sensor.
514 if (sensor->getSensor().getMinDelay() == 0) {
515 sensors_event_t scratch;
516 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
517 if (event.version == sizeof(sensors_event_t)) {
518 connection->sendEvents(&event, 1);
519 }
520 }
521 }
522 }
523
524 if (connection->addSensor(handle)) {
525 BatteryService::enableSensor(connection->getUid(), handle);
526 // the sensor was added (which means it wasn't already there)
527 // so, see if this connection becomes active
528 if (mActiveConnections.indexOf(connection) < 0) {
529 mActiveConnections.add(connection);
530 }
531 } else {
532 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
533 handle, connection.get());
534 }
535
536
537 // we are setup, now enable the sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800538 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700539
540 if (err != NO_ERROR) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700541 // enable has failed, reset state in SensorDevice.
542 status_t resetErr = sensor ? sensor->resetStateWithoutActuatingHardware(connection.get(),
543 handle) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700544 // enable has failed, reset our state.
545 cleanupWithoutDisable(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700546 }
547 return err;
548}
549
550status_t SensorService::disable(const sp<SensorEventConnection>& connection,
551 int handle)
552{
Mathias Agopian50df2952010-07-19 19:09:10 -0700553 if (mInitCheck != NO_ERROR)
554 return mInitCheck;
555
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700556 status_t err = cleanupWithoutDisable(connection, handle);
557 if (err == NO_ERROR) {
558 SensorInterface* sensor = mSensorMap.valueFor(handle);
559 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
560 }
561 return err;
562}
563
564status_t SensorService::cleanupWithoutDisable(const sp<SensorEventConnection>& connection,
565 int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700566 Mutex::Autolock _l(mLock);
567 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700568 if (rec) {
569 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700570 if (connection->removeSensor(handle)) {
571 BatteryService::disableSensor(connection->getUid(), handle);
572 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700573 if (connection->hasAnySensor() == false) {
574 mActiveConnections.remove(connection);
575 }
576 // see if this sensor becomes inactive
577 if (rec->removeConnection(connection)) {
578 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800579 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700580 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700581 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700582 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700583 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700584 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700585}
586
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700587status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700588 int handle, nsecs_t ns)
589{
Mathias Agopian50df2952010-07-19 19:09:10 -0700590 if (mInitCheck != NO_ERROR)
591 return mInitCheck;
592
Mathias Agopianae09d652011-11-01 17:37:49 -0700593 SensorInterface* sensor = mSensorMap.valueFor(handle);
594 if (!sensor)
595 return BAD_VALUE;
596
Mathias Agopian1cd70002010-07-21 15:59:50 -0700597 if (ns < 0)
598 return BAD_VALUE;
599
Mathias Agopian62569ec2011-11-07 21:21:47 -0800600 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
601 if (ns < minDelayNs) {
602 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700603 }
604
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700605 if (ns < MINIMUM_EVENTS_PERIOD)
606 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700607
Mathias Agopianf001c922010-11-11 17:58:51 -0800608 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700609}
610
611// ---------------------------------------------------------------------------
612
613SensorService::SensorRecord::SensorRecord(
614 const sp<SensorEventConnection>& connection)
615{
616 mConnections.add(connection);
617}
618
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700619bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700620 const sp<SensorEventConnection>& connection)
621{
622 if (mConnections.indexOf(connection) < 0) {
623 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700624 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700625 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700626 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700627}
628
629bool SensorService::SensorRecord::removeConnection(
630 const wp<SensorEventConnection>& connection)
631{
632 ssize_t index = mConnections.indexOf(connection);
633 if (index >= 0) {
634 mConnections.removeItemsAt(index, 1);
635 }
636 return mConnections.size() ? false : true;
637}
638
639// ---------------------------------------------------------------------------
640
641SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700642 const sp<SensorService>& service, uid_t uid)
643 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700644{
645}
646
647SensorService::SensorEventConnection::~SensorEventConnection()
648{
Steve Blocka5512372011-12-20 16:23:08 +0000649 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700650 mService->cleanupConnection(this);
651}
652
653void SensorService::SensorEventConnection::onFirstRef()
654{
655}
656
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700657bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800658 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700659 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800660 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700661 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700662 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700663 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700664}
665
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700666bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800667 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800668 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700669 return true;
670 }
671 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700672}
673
674bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800675 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800676 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700677}
678
679bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800680 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700681 return mSensorInfo.size() ? true : false;
682}
683
Mathias Agopianfc328812010-07-14 23:41:37 -0700684status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700685 sensors_event_t const* buffer, size_t numEvents,
686 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700687{
Mathias Agopiancf510012010-07-22 16:18:10 -0700688 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700689 size_t count = 0;
690 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800691 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700692 size_t i=0;
693 while (i<numEvents) {
694 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800695 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700696 do {
697 scratch[count++] = buffer[i++];
698 } while ((i<numEvents) && (buffer[i].sensor == curr));
699 } else {
700 i++;
701 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700702 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700703 } else {
704 scratch = const_cast<sensors_event_t *>(buffer);
705 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700706 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700707
Mathias Agopian907103b2012-04-02 18:38:02 -0700708 // NOTE: ASensorEvent and sensors_event_t are the same type
709 ssize_t size = SensorEventQueue::write(mChannel,
710 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700711 if (size == -EAGAIN) {
712 // the destination doesn't accept events anymore, it's probably
713 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000714 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700715 return size;
716 }
717
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700718 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700719}
720
Mathias Agopianb3989272011-10-20 18:42:02 -0700721sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700722{
723 return mChannel;
724}
725
726status_t SensorService::SensorEventConnection::enableDisable(
727 int handle, bool enabled)
728{
729 status_t err;
730 if (enabled) {
731 err = mService->enable(this, handle);
732 } else {
733 err = mService->disable(this, handle);
734 }
735 return err;
736}
737
738status_t SensorService::SensorEventConnection::setEventRate(
739 int handle, nsecs_t ns)
740{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700741 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700742}
743
744// ---------------------------------------------------------------------------
745}; // namespace android
746