blob: 9ca6b457c34252c920f92bb3cdeca51262f2b2d0 [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) {
245 for (int i=0 ; i<count ; i++) {
246 int handle = buffer[i].sensor;
247 if (getSensorType(handle) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
248 if (connection->hasSensor(handle)) {
249 cleanupWithoutDisable(connection, handle);
250 }
251 }
252 }
253}
254
Mathias Agopianfc328812010-07-14 23:41:37 -0700255bool SensorService::threadLoop()
256{
Steve Blocka5512372011-12-20 16:23:08 +0000257 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700258
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700259 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700260 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700261 sensors_event_t buffer[minBufferSize];
262 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800263 SensorDevice& device(SensorDevice::getInstance());
264 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700265
Mathias Agopianf001c922010-11-11 17:58:51 -0800266 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700267 bool wakeLockAcquired = false;
268 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700269 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800270 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700271 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000272 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700273 break;
274 }
275
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700276 // Poll has returned. Hold a wakelock.
277 // Todo(): add a flag to the sensors definitions to indicate
278 // the sensors which can wake up the AP
279 for (int i = 0; i < count; i++) {
280 if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_SIGNIFICANT_MOTION) {
281 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
282 wakeLockAcquired = true;
283 break;
284 }
285 }
286
Mathias Agopian94e8f682010-11-10 17:50:28 -0800287 recordLastValue(buffer, count);
288
Mathias Agopianf001c922010-11-11 17:58:51 -0800289 // handle virtual sensors
290 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700291 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800292 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
293 getActiveVirtualSensors());
294 const size_t activeVirtualSensorCount = virtualSensors.size();
295 if (activeVirtualSensorCount) {
296 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700297 SensorFusion& fusion(SensorFusion::getInstance());
298 if (fusion.isEnabled()) {
299 for (size_t i=0 ; i<size_t(count) ; i++) {
300 fusion.process(event[i]);
301 }
302 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700303 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800304 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700305 if (count + k >= minBufferSize) {
306 ALOGE("buffer too small to hold all events: "
307 "count=%u, k=%u, size=%u",
308 count, k, minBufferSize);
309 break;
310 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800311 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700312 SensorInterface* si = virtualSensors.valueAt(j);
313 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800314 buffer[count + k] = out;
315 k++;
316 }
317 }
318 }
319 if (k) {
320 // record the last synthesized values
321 recordLastValue(&buffer[count], k);
322 count += k;
323 // sort the buffer by time-stamps
324 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700325 }
326 }
327 }
328
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700329 // handle backward compatibility for RotationVector sensor
330 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
331 for (int i = 0; i < count; i++) {
332 if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_ROTATION_VECTOR) {
333 // All the 4 components of the quaternion should be available
334 // No heading accuracy. Set it to -1
335 buffer[i].data[4] = -1;
336 }
337 }
338 }
339
Mathias Agopianf001c922010-11-11 17:58:51 -0800340 // send our events to clients...
341 const SortedVector< wp<SensorEventConnection> > activeConnections(
342 getActiveConnections());
343 size_t numConnections = activeConnections.size();
344 for (size_t i=0 ; i<numConnections ; i++) {
345 sp<SensorEventConnection> connection(
346 activeConnections[i].promote());
347 if (connection != 0) {
348 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700349 // Some sensors need to be auto disabled after the trigger
350 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800351 }
352 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700353
354 // We have read the data, upper layers should hold the wakelock.
355 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
356
Mathias Agopianfc328812010-07-14 23:41:37 -0700357 } while (count >= 0 || Thread::exitPending());
358
Steve Block3c20fbe2012-01-05 23:22:43 +0000359 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800360 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700361 return false;
362}
363
Mathias Agopian94e8f682010-11-10 17:50:28 -0800364void SensorService::recordLastValue(
365 sensors_event_t const * buffer, size_t count)
366{
367 Mutex::Autolock _l(mLock);
368
369 // record the last event for each sensor
370 int32_t prev = buffer[0].sensor;
371 for (size_t i=1 ; i<count ; i++) {
372 // record the last event of each sensor type in this buffer
373 int32_t curr = buffer[i].sensor;
374 if (curr != prev) {
375 mLastEventSeen.editValueFor(prev) = buffer[i-1];
376 prev = curr;
377 }
378 }
379 mLastEventSeen.editValueFor(prev) = buffer[count-1];
380}
381
Mathias Agopianf001c922010-11-11 17:58:51 -0800382void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
383{
384 struct compar {
385 static int cmp(void const* lhs, void const* rhs) {
386 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
387 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700388 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800389 }
390 };
391 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
392}
393
Mathias Agopianfc328812010-07-14 23:41:37 -0700394SortedVector< wp<SensorService::SensorEventConnection> >
395SensorService::getActiveConnections() const
396{
397 Mutex::Autolock _l(mLock);
398 return mActiveConnections;
399}
400
Mathias Agopianf001c922010-11-11 17:58:51 -0800401DefaultKeyedVector<int, SensorInterface*>
402SensorService::getActiveVirtualSensors() const
403{
404 Mutex::Autolock _l(mLock);
405 return mActiveVirtualSensors;
406}
407
Mathias Agopian5d270722010-07-19 15:20:39 -0700408String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700409 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700410 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700411 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700412 if (sensor.getHandle() == handle) {
413 return sensor.getName();
414 }
415 }
416 String8 result("unknown");
417 return result;
418}
419
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700420int SensorService::getSensorType(int handle) const {
421 size_t count = mUserSensorList.size();
422 for (size_t i=0 ; i<count ; i++) {
423 const Sensor& sensor(mUserSensorList[i]);
424 if (sensor.getHandle() == handle) {
425 return sensor.getType();
426 }
427 }
428 return -1;
429}
430
431
Mathias Agopianfc328812010-07-14 23:41:37 -0700432Vector<Sensor> SensorService::getSensorList()
433{
Mathias Agopian33264862012-06-28 19:46:54 -0700434 char value[PROPERTY_VALUE_MAX];
435 property_get("debug.sensors", value, "0");
436 if (atoi(value)) {
437 return mUserSensorListDebug;
438 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700439 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700440}
441
442sp<ISensorEventConnection> SensorService::createSensorEventConnection()
443{
Mathias Agopian5307d172012-09-18 17:02:43 -0700444 uid_t uid = IPCThreadState::self()->getCallingUid();
445 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700446 return result;
447}
448
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800449void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700450{
451 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800452 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700453 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000454 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700455 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800456 int handle = mActiveSensors.keyAt(i);
457 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000458 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800459 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000460 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800461 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800462 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800463 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800464 }
465 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000466 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000467 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700468 "removing connection %p for sensor[%d].handle=0x%08x",
469 c, i, handle);
470
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800471 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000472 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700473 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800474 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700475 delete rec;
476 size--;
477 } else {
478 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700479 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700480 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700481 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700482 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700483}
484
485status_t SensorService::enable(const sp<SensorEventConnection>& connection,
486 int handle)
487{
Mathias Agopian50df2952010-07-19 19:09:10 -0700488 if (mInitCheck != NO_ERROR)
489 return mInitCheck;
490
Mathias Agopianfc328812010-07-14 23:41:37 -0700491 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800492 SensorInterface* sensor = mSensorMap.valueFor(handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700493 SensorRecord* rec = mActiveSensors.valueFor(handle);
494 if (rec == 0) {
495 rec = new SensorRecord(connection);
496 mActiveSensors.add(handle, rec);
497 if (sensor->isVirtual()) {
498 mActiveVirtualSensors.add(handle, sensor);
499 }
500 } else {
501 if (rec->addConnection(connection)) {
502 // this sensor is already activated, but we are adding a
503 // connection that uses it. Immediately send down the last
504 // known value of the requested sensor if it's not a
505 // "continuous" sensor.
506 if (sensor->getSensor().getMinDelay() == 0) {
507 sensors_event_t scratch;
508 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
509 if (event.version == sizeof(sensors_event_t)) {
510 connection->sendEvents(&event, 1);
511 }
512 }
513 }
514 }
515
516 if (connection->addSensor(handle)) {
517 BatteryService::enableSensor(connection->getUid(), handle);
518 // the sensor was added (which means it wasn't already there)
519 // so, see if this connection becomes active
520 if (mActiveConnections.indexOf(connection) < 0) {
521 mActiveConnections.add(connection);
522 }
523 } else {
524 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
525 handle, connection.get());
526 }
527
528
529 // we are setup, now enable the sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800530 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700531
532 if (err != NO_ERROR) {
533 // enable has failed, reset our state.
534 cleanupWithoutDisable(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700535 }
536 return err;
537}
538
539status_t SensorService::disable(const sp<SensorEventConnection>& connection,
540 int handle)
541{
Mathias Agopian50df2952010-07-19 19:09:10 -0700542 if (mInitCheck != NO_ERROR)
543 return mInitCheck;
544
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700545 status_t err = cleanupWithoutDisable(connection, handle);
546 if (err == NO_ERROR) {
547 SensorInterface* sensor = mSensorMap.valueFor(handle);
548 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
549 }
550 return err;
551}
552
553status_t SensorService::cleanupWithoutDisable(const sp<SensorEventConnection>& connection,
554 int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700555 Mutex::Autolock _l(mLock);
556 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700557 if (rec) {
558 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700559 if (connection->removeSensor(handle)) {
560 BatteryService::disableSensor(connection->getUid(), handle);
561 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700562 if (connection->hasAnySensor() == false) {
563 mActiveConnections.remove(connection);
564 }
565 // see if this sensor becomes inactive
566 if (rec->removeConnection(connection)) {
567 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800568 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700569 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700570 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700571 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700572 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700573 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700574}
575
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700576status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700577 int handle, nsecs_t ns)
578{
Mathias Agopian50df2952010-07-19 19:09:10 -0700579 if (mInitCheck != NO_ERROR)
580 return mInitCheck;
581
Mathias Agopianae09d652011-11-01 17:37:49 -0700582 SensorInterface* sensor = mSensorMap.valueFor(handle);
583 if (!sensor)
584 return BAD_VALUE;
585
Mathias Agopian1cd70002010-07-21 15:59:50 -0700586 if (ns < 0)
587 return BAD_VALUE;
588
Mathias Agopian62569ec2011-11-07 21:21:47 -0800589 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
590 if (ns < minDelayNs) {
591 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700592 }
593
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700594 if (ns < MINIMUM_EVENTS_PERIOD)
595 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700596
Mathias Agopianf001c922010-11-11 17:58:51 -0800597 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700598}
599
600// ---------------------------------------------------------------------------
601
602SensorService::SensorRecord::SensorRecord(
603 const sp<SensorEventConnection>& connection)
604{
605 mConnections.add(connection);
606}
607
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700608bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700609 const sp<SensorEventConnection>& connection)
610{
611 if (mConnections.indexOf(connection) < 0) {
612 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700613 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700614 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700615 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700616}
617
618bool SensorService::SensorRecord::removeConnection(
619 const wp<SensorEventConnection>& connection)
620{
621 ssize_t index = mConnections.indexOf(connection);
622 if (index >= 0) {
623 mConnections.removeItemsAt(index, 1);
624 }
625 return mConnections.size() ? false : true;
626}
627
628// ---------------------------------------------------------------------------
629
630SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700631 const sp<SensorService>& service, uid_t uid)
632 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700633{
634}
635
636SensorService::SensorEventConnection::~SensorEventConnection()
637{
Steve Blocka5512372011-12-20 16:23:08 +0000638 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700639 mService->cleanupConnection(this);
640}
641
642void SensorService::SensorEventConnection::onFirstRef()
643{
644}
645
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700646bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800647 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700648 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800649 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700650 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700651 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700652 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700653}
654
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700655bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800656 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800657 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700658 return true;
659 }
660 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700661}
662
663bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800664 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800665 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700666}
667
668bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800669 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700670 return mSensorInfo.size() ? true : false;
671}
672
Mathias Agopianfc328812010-07-14 23:41:37 -0700673status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700674 sensors_event_t const* buffer, size_t numEvents,
675 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700676{
Mathias Agopiancf510012010-07-22 16:18:10 -0700677 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700678 size_t count = 0;
679 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800680 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700681 size_t i=0;
682 while (i<numEvents) {
683 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800684 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700685 do {
686 scratch[count++] = buffer[i++];
687 } while ((i<numEvents) && (buffer[i].sensor == curr));
688 } else {
689 i++;
690 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700691 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700692 } else {
693 scratch = const_cast<sensors_event_t *>(buffer);
694 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700695 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700696
Mathias Agopian907103b2012-04-02 18:38:02 -0700697 // NOTE: ASensorEvent and sensors_event_t are the same type
698 ssize_t size = SensorEventQueue::write(mChannel,
699 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700700 if (size == -EAGAIN) {
701 // the destination doesn't accept events anymore, it's probably
702 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000703 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700704 return size;
705 }
706
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700707 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700708}
709
Mathias Agopianb3989272011-10-20 18:42:02 -0700710sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700711{
712 return mChannel;
713}
714
715status_t SensorService::SensorEventConnection::enableDisable(
716 int handle, bool enabled)
717{
718 status_t err;
719 if (enabled) {
720 err = mService->enable(this, handle);
721 } else {
722 err = mService->disable(this, handle);
723 }
724 return err;
725}
726
727status_t SensorService::SensorEventConnection::setEventRate(
728 int handle, nsecs_t ns)
729{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700730 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700731}
732
733// ---------------------------------------------------------------------------
734}; // namespace android
735