blob: 39e61b70b44839f941d746a87093a556b2c5141a [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:
Mathias Agopian03193062013-05-10 19:32:39 -070096 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070097 hasGyro = true;
98 break;
99 case SENSOR_TYPE_GRAVITY:
100 case SENSOR_TYPE_LINEAR_ACCELERATION:
101 case SENSOR_TYPE_ROTATION_VECTOR:
102 virtualSensorsNeeds &= ~(1<<list[i].type);
103 break;
104 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700105 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700106
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700107 // it's safe to instantiate the SensorFusion object here
108 // (it wants to be instantiated after h/w sensors have been
109 // registered)
110 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700111
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700112 // build the sensor list returned to users
113 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700114
115 if (hasGyro) {
Mathias Agopian03193062013-05-10 19:32:39 -0700116 Sensor aSensor;
117
118 // Add Android virtual sensors if they're not already
119 // available in the HAL
120
121 aSensor = registerVirtualSensor( new RotationVectorSensor() );
122 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
123 mUserSensorList.add(aSensor);
124 }
125
126 aSensor = registerVirtualSensor( new GravitySensor(list, count) );
127 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
128 mUserSensorList.add(aSensor);
129 }
130
131 aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
132 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
133 mUserSensorList.add(aSensor);
134 }
135
136 aSensor = registerVirtualSensor( new OrientationSensor() );
137 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
138 // if we are doing our own rotation-vector, also add
139 // the orientation sensor and remove the HAL provided one.
140 mUserSensorList.replaceAt(aSensor, orientationIndex);
141 }
142
Mathias Agopian33264862012-06-28 19:46:54 -0700143 // virtual debugging sensors are not added to mUserSensorList
Mathias Agopian03193062013-05-10 19:32:39 -0700144 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33264862012-06-28 19:46:54 -0700145 registerVirtualSensor( new GyroDriftSensor() );
146 }
147
Mathias Agopian33264862012-06-28 19:46:54 -0700148 // debugging sensor list
Mathias Agopian03193062013-05-10 19:32:39 -0700149 mUserSensorListDebug = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700150
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700151 run("SensorService", PRIORITY_URGENT_DISPLAY);
152 mInitCheck = NO_ERROR;
153 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700154 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700155}
156
Mathias Agopian03193062013-05-10 19:32:39 -0700157Sensor SensorService::registerSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800158{
159 sensors_event_t event;
160 memset(&event, 0, sizeof(event));
161
162 const Sensor sensor(s->getSensor());
163 // add to the sensor list (returned to clients)
164 mSensorList.add(sensor);
165 // add to our handle->SensorInterface mapping
166 mSensorMap.add(sensor.getHandle(), s);
167 // create an entry in the mLastEventSeen array
168 mLastEventSeen.add(sensor.getHandle(), event);
Mathias Agopian03193062013-05-10 19:32:39 -0700169
170 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800171}
172
Mathias Agopian03193062013-05-10 19:32:39 -0700173Sensor SensorService::registerVirtualSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800174{
Mathias Agopian03193062013-05-10 19:32:39 -0700175 Sensor sensor = registerSensor(s);
Mathias Agopianf001c922010-11-11 17:58:51 -0800176 mVirtualSensorList.add( s );
Mathias Agopian03193062013-05-10 19:32:39 -0700177 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800178}
179
Mathias Agopianfc328812010-07-14 23:41:37 -0700180SensorService::~SensorService()
181{
Mathias Agopianf001c922010-11-11 17:58:51 -0800182 for (size_t i=0 ; i<mSensorMap.size() ; i++)
183 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700184}
185
Mathias Agopian1cb13462011-06-27 16:05:52 -0700186static const String16 sDump("android.permission.DUMP");
187
Mathias Agopianfc328812010-07-14 23:41:37 -0700188status_t SensorService::dump(int fd, const Vector<String16>& args)
189{
190 const size_t SIZE = 1024;
191 char buffer[SIZE];
192 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700193 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700194 snprintf(buffer, SIZE, "Permission Denial: "
195 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
196 IPCThreadState::self()->getCallingPid(),
197 IPCThreadState::self()->getCallingUid());
198 result.append(buffer);
199 } else {
200 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700201 snprintf(buffer, SIZE, "Sensor List:\n");
202 result.append(buffer);
203 for (size_t i=0 ; i<mSensorList.size() ; i++) {
204 const Sensor& s(mSensorList[i]);
205 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700206 snprintf(buffer, SIZE,
207 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
208 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700209 s.getName().string(),
210 s.getVendor().string(),
211 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700212 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700213 e.data[0], e.data[1], e.data[2]);
214 result.append(buffer);
215 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700216 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800217 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700218
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700219 snprintf(buffer, SIZE, "%d active connections\n",
220 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700221 result.append(buffer);
222 snprintf(buffer, SIZE, "Active sensors:\n");
223 result.append(buffer);
224 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700225 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800226 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700227 getSensorName(handle).string(),
228 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700229 mActiveSensors.valueAt(i)->getNumConnections());
230 result.append(buffer);
231 }
232 }
233 write(fd, result.string(), result.size());
234 return NO_ERROR;
235}
236
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700237void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
238 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700239 SensorInterface* sensor;
240 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700241 for (int i=0 ; i<count ; i++) {
242 int handle = buffer[i].sensor;
Mathias Agopian7438fd12013-07-08 12:50:39 -0700243 int type = buffer[i].type;
244 if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700245 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700246 sensor = mSensorMap.valueFor(handle);
247 err = sensor ?sensor->resetStateWithoutActuatingHardware(connection.get(), handle)
248 : status_t(BAD_VALUE);
249 if (err != NO_ERROR) {
250 ALOGE("Sensor Inteface: Resetting state failed with err: %d", err);
251 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700252 cleanupWithoutDisable(connection, handle);
253 }
254 }
255 }
256}
257
Mathias Agopianfc328812010-07-14 23:41:37 -0700258bool SensorService::threadLoop()
259{
Steve Blocka5512372011-12-20 16:23:08 +0000260 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700261
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700262 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700263 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700264 sensors_event_t buffer[minBufferSize];
265 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800266 SensorDevice& device(SensorDevice::getInstance());
267 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700268
Mathias Agopianf001c922010-11-11 17:58:51 -0800269 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700270 bool wakeLockAcquired = false;
271 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700272 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800273 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700274 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000275 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700276 break;
277 }
278
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700279 // Poll has returned. Hold a wakelock.
280 // Todo(): add a flag to the sensors definitions to indicate
281 // the sensors which can wake up the AP
282 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700283 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700284 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
285 wakeLockAcquired = true;
286 break;
287 }
288 }
289
Mathias Agopian94e8f682010-11-10 17:50:28 -0800290 recordLastValue(buffer, count);
291
Mathias Agopianf001c922010-11-11 17:58:51 -0800292 // handle virtual sensors
293 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700294 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800295 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
296 getActiveVirtualSensors());
297 const size_t activeVirtualSensorCount = virtualSensors.size();
298 if (activeVirtualSensorCount) {
299 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700300 SensorFusion& fusion(SensorFusion::getInstance());
301 if (fusion.isEnabled()) {
302 for (size_t i=0 ; i<size_t(count) ; i++) {
303 fusion.process(event[i]);
304 }
305 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700306 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800307 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700308 if (count + k >= minBufferSize) {
309 ALOGE("buffer too small to hold all events: "
310 "count=%u, k=%u, size=%u",
311 count, k, minBufferSize);
312 break;
313 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800314 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700315 SensorInterface* si = virtualSensors.valueAt(j);
316 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800317 buffer[count + k] = out;
318 k++;
319 }
320 }
321 }
322 if (k) {
323 // record the last synthesized values
324 recordLastValue(&buffer[count], k);
325 count += k;
326 // sort the buffer by time-stamps
327 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700328 }
329 }
330 }
331
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700332 // handle backward compatibility for RotationVector sensor
333 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
334 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700335 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700336 // All the 4 components of the quaternion should be available
337 // No heading accuracy. Set it to -1
338 buffer[i].data[4] = -1;
339 }
340 }
341 }
342
Mathias Agopianf001c922010-11-11 17:58:51 -0800343 // send our events to clients...
344 const SortedVector< wp<SensorEventConnection> > activeConnections(
345 getActiveConnections());
346 size_t numConnections = activeConnections.size();
347 for (size_t i=0 ; i<numConnections ; i++) {
348 sp<SensorEventConnection> connection(
349 activeConnections[i].promote());
350 if (connection != 0) {
351 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700352 // Some sensors need to be auto disabled after the trigger
353 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800354 }
355 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700356
357 // We have read the data, upper layers should hold the wakelock.
358 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
359
Mathias Agopianfc328812010-07-14 23:41:37 -0700360 } while (count >= 0 || Thread::exitPending());
361
Steve Block3c20fbe2012-01-05 23:22:43 +0000362 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800363 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700364 return false;
365}
366
Mathias Agopian94e8f682010-11-10 17:50:28 -0800367void SensorService::recordLastValue(
368 sensors_event_t const * buffer, size_t count)
369{
370 Mutex::Autolock _l(mLock);
371
372 // record the last event for each sensor
373 int32_t prev = buffer[0].sensor;
374 for (size_t i=1 ; i<count ; i++) {
375 // record the last event of each sensor type in this buffer
376 int32_t curr = buffer[i].sensor;
377 if (curr != prev) {
378 mLastEventSeen.editValueFor(prev) = buffer[i-1];
379 prev = curr;
380 }
381 }
382 mLastEventSeen.editValueFor(prev) = buffer[count-1];
383}
384
Mathias Agopianf001c922010-11-11 17:58:51 -0800385void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
386{
387 struct compar {
388 static int cmp(void const* lhs, void const* rhs) {
389 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
390 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700391 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800392 }
393 };
394 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
395}
396
Mathias Agopianfc328812010-07-14 23:41:37 -0700397SortedVector< wp<SensorService::SensorEventConnection> >
398SensorService::getActiveConnections() const
399{
400 Mutex::Autolock _l(mLock);
401 return mActiveConnections;
402}
403
Mathias Agopianf001c922010-11-11 17:58:51 -0800404DefaultKeyedVector<int, SensorInterface*>
405SensorService::getActiveVirtualSensors() const
406{
407 Mutex::Autolock _l(mLock);
408 return mActiveVirtualSensors;
409}
410
Mathias Agopian5d270722010-07-19 15:20:39 -0700411String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700412 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700413 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700414 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700415 if (sensor.getHandle() == handle) {
416 return sensor.getName();
417 }
418 }
419 String8 result("unknown");
420 return result;
421}
422
Mathias Agopianfc328812010-07-14 23:41:37 -0700423Vector<Sensor> SensorService::getSensorList()
424{
Mathias Agopian33264862012-06-28 19:46:54 -0700425 char value[PROPERTY_VALUE_MAX];
426 property_get("debug.sensors", value, "0");
427 if (atoi(value)) {
428 return mUserSensorListDebug;
429 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700430 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700431}
432
433sp<ISensorEventConnection> SensorService::createSensorEventConnection()
434{
Mathias Agopian5307d172012-09-18 17:02:43 -0700435 uid_t uid = IPCThreadState::self()->getCallingUid();
436 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700437 return result;
438}
439
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800440void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700441{
442 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800443 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700444 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000445 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700446 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800447 int handle = mActiveSensors.keyAt(i);
448 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000449 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800450 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000451 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800452 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800453 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800454 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800455 }
456 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000457 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000458 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700459 "removing connection %p for sensor[%d].handle=0x%08x",
460 c, i, handle);
461
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800462 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000463 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700464 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800465 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700466 delete rec;
467 size--;
468 } else {
469 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700470 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700471 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700472 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700473 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700474}
475
476status_t SensorService::enable(const sp<SensorEventConnection>& connection,
477 int handle)
478{
Mathias Agopian50df2952010-07-19 19:09:10 -0700479 if (mInitCheck != NO_ERROR)
480 return mInitCheck;
481
Mathias Agopianfc328812010-07-14 23:41:37 -0700482 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800483 SensorInterface* sensor = mSensorMap.valueFor(handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700484 SensorRecord* rec = mActiveSensors.valueFor(handle);
485 if (rec == 0) {
486 rec = new SensorRecord(connection);
487 mActiveSensors.add(handle, rec);
488 if (sensor->isVirtual()) {
489 mActiveVirtualSensors.add(handle, sensor);
490 }
491 } else {
492 if (rec->addConnection(connection)) {
493 // this sensor is already activated, but we are adding a
494 // connection that uses it. Immediately send down the last
495 // known value of the requested sensor if it's not a
496 // "continuous" sensor.
497 if (sensor->getSensor().getMinDelay() == 0) {
498 sensors_event_t scratch;
499 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
500 if (event.version == sizeof(sensors_event_t)) {
501 connection->sendEvents(&event, 1);
502 }
503 }
504 }
505 }
506
507 if (connection->addSensor(handle)) {
508 BatteryService::enableSensor(connection->getUid(), handle);
509 // the sensor was added (which means it wasn't already there)
510 // so, see if this connection becomes active
511 if (mActiveConnections.indexOf(connection) < 0) {
512 mActiveConnections.add(connection);
513 }
514 } else {
515 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
516 handle, connection.get());
517 }
518
519
520 // we are setup, now enable the sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800521 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700522
523 if (err != NO_ERROR) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700524 // enable has failed, reset state in SensorDevice.
525 status_t resetErr = sensor ? sensor->resetStateWithoutActuatingHardware(connection.get(),
526 handle) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700527 // enable has failed, reset our state.
528 cleanupWithoutDisable(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700529 }
530 return err;
531}
532
533status_t SensorService::disable(const sp<SensorEventConnection>& connection,
534 int handle)
535{
Mathias Agopian50df2952010-07-19 19:09:10 -0700536 if (mInitCheck != NO_ERROR)
537 return mInitCheck;
538
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700539 status_t err = cleanupWithoutDisable(connection, handle);
540 if (err == NO_ERROR) {
541 SensorInterface* sensor = mSensorMap.valueFor(handle);
542 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
543 }
544 return err;
545}
546
547status_t SensorService::cleanupWithoutDisable(const sp<SensorEventConnection>& connection,
548 int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700549 Mutex::Autolock _l(mLock);
550 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700551 if (rec) {
552 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700553 if (connection->removeSensor(handle)) {
554 BatteryService::disableSensor(connection->getUid(), handle);
555 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700556 if (connection->hasAnySensor() == false) {
557 mActiveConnections.remove(connection);
558 }
559 // see if this sensor becomes inactive
560 if (rec->removeConnection(connection)) {
561 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800562 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700563 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700564 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700565 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700566 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700567 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700568}
569
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700570status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700571 int handle, nsecs_t ns)
572{
Mathias Agopian50df2952010-07-19 19:09:10 -0700573 if (mInitCheck != NO_ERROR)
574 return mInitCheck;
575
Mathias Agopianae09d652011-11-01 17:37:49 -0700576 SensorInterface* sensor = mSensorMap.valueFor(handle);
577 if (!sensor)
578 return BAD_VALUE;
579
Mathias Agopian1cd70002010-07-21 15:59:50 -0700580 if (ns < 0)
581 return BAD_VALUE;
582
Mathias Agopian62569ec2011-11-07 21:21:47 -0800583 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
584 if (ns < minDelayNs) {
585 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700586 }
587
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700588 if (ns < MINIMUM_EVENTS_PERIOD)
589 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700590
Mathias Agopianf001c922010-11-11 17:58:51 -0800591 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700592}
593
594// ---------------------------------------------------------------------------
595
596SensorService::SensorRecord::SensorRecord(
597 const sp<SensorEventConnection>& connection)
598{
599 mConnections.add(connection);
600}
601
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700602bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700603 const sp<SensorEventConnection>& connection)
604{
605 if (mConnections.indexOf(connection) < 0) {
606 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700607 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700608 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700609 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700610}
611
612bool SensorService::SensorRecord::removeConnection(
613 const wp<SensorEventConnection>& connection)
614{
615 ssize_t index = mConnections.indexOf(connection);
616 if (index >= 0) {
617 mConnections.removeItemsAt(index, 1);
618 }
619 return mConnections.size() ? false : true;
620}
621
622// ---------------------------------------------------------------------------
623
624SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700625 const sp<SensorService>& service, uid_t uid)
626 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700627{
628}
629
630SensorService::SensorEventConnection::~SensorEventConnection()
631{
Steve Blocka5512372011-12-20 16:23:08 +0000632 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700633 mService->cleanupConnection(this);
634}
635
636void SensorService::SensorEventConnection::onFirstRef()
637{
638}
639
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700640bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800641 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700642 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800643 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700644 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700645 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700646 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700647}
648
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700649bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800650 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800651 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700652 return true;
653 }
654 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700655}
656
657bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800658 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800659 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700660}
661
662bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800663 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700664 return mSensorInfo.size() ? true : false;
665}
666
Mathias Agopianfc328812010-07-14 23:41:37 -0700667status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700668 sensors_event_t const* buffer, size_t numEvents,
669 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700670{
Mathias Agopiancf510012010-07-22 16:18:10 -0700671 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700672 size_t count = 0;
673 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800674 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700675 size_t i=0;
676 while (i<numEvents) {
677 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800678 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700679 do {
680 scratch[count++] = buffer[i++];
681 } while ((i<numEvents) && (buffer[i].sensor == curr));
682 } else {
683 i++;
684 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700685 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700686 } else {
687 scratch = const_cast<sensors_event_t *>(buffer);
688 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700689 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700690
Mathias Agopian907103b2012-04-02 18:38:02 -0700691 // NOTE: ASensorEvent and sensors_event_t are the same type
692 ssize_t size = SensorEventQueue::write(mChannel,
693 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700694 if (size == -EAGAIN) {
695 // the destination doesn't accept events anymore, it's probably
696 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000697 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700698 return size;
699 }
700
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700701 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700702}
703
Mathias Agopianb3989272011-10-20 18:42:02 -0700704sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700705{
706 return mChannel;
707}
708
709status_t SensorService::SensorEventConnection::enableDisable(
710 int handle, bool enabled)
711{
712 status_t err;
713 if (enabled) {
714 err = mService->enable(this, handle);
715 } else {
716 err = mService->disable(this, handle);
717 }
718 return err;
719}
720
721status_t SensorService::SensorEventConnection::setEventRate(
722 int handle, nsecs_t ns)
723{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700724 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700725}
726
727// ---------------------------------------------------------------------------
728}; // namespace android
729