blob: 99993ba38428bd2fada045cccd605db2e8b8f3e7 [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{
Mathias Agopianfc328812010-07-14 23:41:37 -0700190 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700191 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700192 result.appendFormat("Permission Denial: "
Mathias Agopianfc328812010-07-14 23:41:37 -0700193 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
194 IPCThreadState::self()->getCallingPid(),
195 IPCThreadState::self()->getCallingUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700196 } else {
197 Mutex::Autolock _l(mLock);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700198 result.append("Sensor List:\n");
Mathias Agopian3560fb22010-07-22 21:24:39 -0700199 for (size_t i=0 ; i<mSensorList.size() ; i++) {
200 const Sensor& s(mSensorList[i]);
201 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopianba02cd22013-07-03 16:20:57 -0700202 result.appendFormat(
203 "%-48s| %-32s | 0x%08x | ",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700204 s.getName().string(),
205 s.getVendor().string(),
Mathias Agopianba02cd22013-07-03 16:20:57 -0700206 s.getHandle());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700207
Mathias Agopianba02cd22013-07-03 16:20:57 -0700208 if (s.getMinDelay() > 0) {
209 result.appendFormat(
210 "maxRate=%7.2fHz | ", 1e6f / s.getMinDelay());
211 } else {
212 result.append(s.getMinDelay() == 0
213 ? "on-demand | "
214 : "one-shot | ");
215 }
216
217 switch (s.getType()) {
218 case SENSOR_TYPE_ROTATION_VECTOR:
219 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
220 result.appendFormat(
221 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
222 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]);
223 break;
224 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
225 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
226 result.appendFormat(
227 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
228 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]);
229 break;
230 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
231 result.appendFormat(
232 "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n",
233 e.data[0], e.data[1], e.data[2], e.data[3]);
234 break;
235 case SENSOR_TYPE_SIGNIFICANT_MOTION:
236 case SENSOR_TYPE_STEP_DETECTOR:
237 result.appendFormat( "last=<%f>\n", e.data[0]);
238 break;
239 case SENSOR_TYPE_STEP_COUNTER:
240 result.appendFormat( "last=<%llu>\n", e.u64.step_counter);
241 break;
242 default:
243 // default to 3 values
244 result.appendFormat(
245 "last=<%5.1f,%5.1f,%5.1f>\n",
246 e.data[0], e.data[1], e.data[2]);
247 break;
248 }
249 }
250 SensorFusion::getInstance().dump(result);
251 SensorDevice::getInstance().dump(result);
252
253 result.appendFormat("%d active connections\n", mActiveConnections.size());
254 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700255 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700256 int handle = mActiveSensors.keyAt(i);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700257 result.appendFormat("%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700258 getSensorName(handle).string(),
259 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700260 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700261 }
262 }
263 write(fd, result.string(), result.size());
264 return NO_ERROR;
265}
266
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700267void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
268 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700269 SensorInterface* sensor;
270 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700271 for (int i=0 ; i<count ; i++) {
272 int handle = buffer[i].sensor;
Mathias Agopian7438fd12013-07-08 12:50:39 -0700273 int type = buffer[i].type;
274 if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700275 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700276 sensor = mSensorMap.valueFor(handle);
277 err = sensor ?sensor->resetStateWithoutActuatingHardware(connection.get(), handle)
278 : status_t(BAD_VALUE);
279 if (err != NO_ERROR) {
280 ALOGE("Sensor Inteface: Resetting state failed with err: %d", err);
281 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700282 cleanupWithoutDisable(connection, handle);
283 }
284 }
285 }
286}
287
Mathias Agopianfc328812010-07-14 23:41:37 -0700288bool SensorService::threadLoop()
289{
Steve Blocka5512372011-12-20 16:23:08 +0000290 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700291
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700292 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700293 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700294 sensors_event_t buffer[minBufferSize];
295 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800296 SensorDevice& device(SensorDevice::getInstance());
297 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700298
Mathias Agopianf001c922010-11-11 17:58:51 -0800299 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700300 bool wakeLockAcquired = false;
301 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700302 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800303 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700304 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000305 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700306 break;
307 }
308
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700309 // Poll has returned. Hold a wakelock.
310 // Todo(): add a flag to the sensors definitions to indicate
311 // the sensors which can wake up the AP
312 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700313 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700314 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
315 wakeLockAcquired = true;
316 break;
317 }
318 }
319
Mathias Agopian94e8f682010-11-10 17:50:28 -0800320 recordLastValue(buffer, count);
321
Mathias Agopianf001c922010-11-11 17:58:51 -0800322 // handle virtual sensors
323 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700324 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800325 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
326 getActiveVirtualSensors());
327 const size_t activeVirtualSensorCount = virtualSensors.size();
328 if (activeVirtualSensorCount) {
329 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700330 SensorFusion& fusion(SensorFusion::getInstance());
331 if (fusion.isEnabled()) {
332 for (size_t i=0 ; i<size_t(count) ; i++) {
333 fusion.process(event[i]);
334 }
335 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700336 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800337 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700338 if (count + k >= minBufferSize) {
339 ALOGE("buffer too small to hold all events: "
340 "count=%u, k=%u, size=%u",
341 count, k, minBufferSize);
342 break;
343 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800344 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700345 SensorInterface* si = virtualSensors.valueAt(j);
346 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800347 buffer[count + k] = out;
348 k++;
349 }
350 }
351 }
352 if (k) {
353 // record the last synthesized values
354 recordLastValue(&buffer[count], k);
355 count += k;
356 // sort the buffer by time-stamps
357 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700358 }
359 }
360 }
361
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700362 // handle backward compatibility for RotationVector sensor
363 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
364 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700365 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700366 // All the 4 components of the quaternion should be available
367 // No heading accuracy. Set it to -1
368 buffer[i].data[4] = -1;
369 }
370 }
371 }
372
Mathias Agopianf001c922010-11-11 17:58:51 -0800373 // send our events to clients...
374 const SortedVector< wp<SensorEventConnection> > activeConnections(
375 getActiveConnections());
376 size_t numConnections = activeConnections.size();
377 for (size_t i=0 ; i<numConnections ; i++) {
378 sp<SensorEventConnection> connection(
379 activeConnections[i].promote());
380 if (connection != 0) {
381 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700382 // Some sensors need to be auto disabled after the trigger
383 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800384 }
385 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700386
387 // We have read the data, upper layers should hold the wakelock.
388 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
389
Mathias Agopianfc328812010-07-14 23:41:37 -0700390 } while (count >= 0 || Thread::exitPending());
391
Steve Block3c20fbe2012-01-05 23:22:43 +0000392 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800393 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700394 return false;
395}
396
Mathias Agopian94e8f682010-11-10 17:50:28 -0800397void SensorService::recordLastValue(
398 sensors_event_t const * buffer, size_t count)
399{
400 Mutex::Autolock _l(mLock);
401
402 // record the last event for each sensor
403 int32_t prev = buffer[0].sensor;
404 for (size_t i=1 ; i<count ; i++) {
405 // record the last event of each sensor type in this buffer
406 int32_t curr = buffer[i].sensor;
407 if (curr != prev) {
408 mLastEventSeen.editValueFor(prev) = buffer[i-1];
409 prev = curr;
410 }
411 }
412 mLastEventSeen.editValueFor(prev) = buffer[count-1];
413}
414
Mathias Agopianf001c922010-11-11 17:58:51 -0800415void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
416{
417 struct compar {
418 static int cmp(void const* lhs, void const* rhs) {
419 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
420 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700421 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800422 }
423 };
424 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
425}
426
Mathias Agopianfc328812010-07-14 23:41:37 -0700427SortedVector< wp<SensorService::SensorEventConnection> >
428SensorService::getActiveConnections() const
429{
430 Mutex::Autolock _l(mLock);
431 return mActiveConnections;
432}
433
Mathias Agopianf001c922010-11-11 17:58:51 -0800434DefaultKeyedVector<int, SensorInterface*>
435SensorService::getActiveVirtualSensors() const
436{
437 Mutex::Autolock _l(mLock);
438 return mActiveVirtualSensors;
439}
440
Mathias Agopian5d270722010-07-19 15:20:39 -0700441String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700442 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700443 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700444 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700445 if (sensor.getHandle() == handle) {
446 return sensor.getName();
447 }
448 }
449 String8 result("unknown");
450 return result;
451}
452
Mathias Agopianfc328812010-07-14 23:41:37 -0700453Vector<Sensor> SensorService::getSensorList()
454{
Mathias Agopian33264862012-06-28 19:46:54 -0700455 char value[PROPERTY_VALUE_MAX];
456 property_get("debug.sensors", value, "0");
457 if (atoi(value)) {
458 return mUserSensorListDebug;
459 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700460 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700461}
462
463sp<ISensorEventConnection> SensorService::createSensorEventConnection()
464{
Mathias Agopian5307d172012-09-18 17:02:43 -0700465 uid_t uid = IPCThreadState::self()->getCallingUid();
466 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700467 return result;
468}
469
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800470void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700471{
472 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800473 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700474 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000475 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700476 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800477 int handle = mActiveSensors.keyAt(i);
478 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000479 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800480 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000481 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800482 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800483 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800484 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800485 }
486 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000487 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000488 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700489 "removing connection %p for sensor[%d].handle=0x%08x",
490 c, i, handle);
491
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800492 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000493 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700494 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800495 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700496 delete rec;
497 size--;
498 } else {
499 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700500 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700501 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700502 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700503 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700504}
505
506status_t SensorService::enable(const sp<SensorEventConnection>& connection,
507 int handle)
508{
Mathias Agopian50df2952010-07-19 19:09:10 -0700509 if (mInitCheck != NO_ERROR)
510 return mInitCheck;
511
Mathias Agopianfc328812010-07-14 23:41:37 -0700512 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800513 SensorInterface* sensor = mSensorMap.valueFor(handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700514 SensorRecord* rec = mActiveSensors.valueFor(handle);
515 if (rec == 0) {
516 rec = new SensorRecord(connection);
517 mActiveSensors.add(handle, rec);
518 if (sensor->isVirtual()) {
519 mActiveVirtualSensors.add(handle, sensor);
520 }
521 } else {
522 if (rec->addConnection(connection)) {
523 // this sensor is already activated, but we are adding a
524 // connection that uses it. Immediately send down the last
525 // known value of the requested sensor if it's not a
526 // "continuous" sensor.
527 if (sensor->getSensor().getMinDelay() == 0) {
528 sensors_event_t scratch;
529 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
530 if (event.version == sizeof(sensors_event_t)) {
531 connection->sendEvents(&event, 1);
532 }
533 }
534 }
535 }
536
537 if (connection->addSensor(handle)) {
538 BatteryService::enableSensor(connection->getUid(), handle);
539 // the sensor was added (which means it wasn't already there)
540 // so, see if this connection becomes active
541 if (mActiveConnections.indexOf(connection) < 0) {
542 mActiveConnections.add(connection);
543 }
544 } else {
545 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
546 handle, connection.get());
547 }
548
549
550 // we are setup, now enable the sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800551 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700552
553 if (err != NO_ERROR) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700554 // enable has failed, reset state in SensorDevice.
555 status_t resetErr = sensor ? sensor->resetStateWithoutActuatingHardware(connection.get(),
556 handle) : status_t(BAD_VALUE);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700557 // enable has failed, reset our state.
558 cleanupWithoutDisable(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700559 }
560 return err;
561}
562
563status_t SensorService::disable(const sp<SensorEventConnection>& connection,
564 int handle)
565{
Mathias Agopian50df2952010-07-19 19:09:10 -0700566 if (mInitCheck != NO_ERROR)
567 return mInitCheck;
568
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700569 status_t err = cleanupWithoutDisable(connection, handle);
570 if (err == NO_ERROR) {
571 SensorInterface* sensor = mSensorMap.valueFor(handle);
572 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
573 }
574 return err;
575}
576
577status_t SensorService::cleanupWithoutDisable(const sp<SensorEventConnection>& connection,
578 int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700579 Mutex::Autolock _l(mLock);
580 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700581 if (rec) {
582 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700583 if (connection->removeSensor(handle)) {
584 BatteryService::disableSensor(connection->getUid(), handle);
585 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700586 if (connection->hasAnySensor() == false) {
587 mActiveConnections.remove(connection);
588 }
589 // see if this sensor becomes inactive
590 if (rec->removeConnection(connection)) {
591 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800592 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700593 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700594 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700595 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700596 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700597 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700598}
599
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700600status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700601 int handle, nsecs_t ns)
602{
Mathias Agopian50df2952010-07-19 19:09:10 -0700603 if (mInitCheck != NO_ERROR)
604 return mInitCheck;
605
Mathias Agopianae09d652011-11-01 17:37:49 -0700606 SensorInterface* sensor = mSensorMap.valueFor(handle);
607 if (!sensor)
608 return BAD_VALUE;
609
Mathias Agopian1cd70002010-07-21 15:59:50 -0700610 if (ns < 0)
611 return BAD_VALUE;
612
Mathias Agopian62569ec2011-11-07 21:21:47 -0800613 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
614 if (ns < minDelayNs) {
615 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700616 }
617
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700618 if (ns < MINIMUM_EVENTS_PERIOD)
619 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700620
Mathias Agopianf001c922010-11-11 17:58:51 -0800621 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700622}
623
624// ---------------------------------------------------------------------------
625
626SensorService::SensorRecord::SensorRecord(
627 const sp<SensorEventConnection>& connection)
628{
629 mConnections.add(connection);
630}
631
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700632bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700633 const sp<SensorEventConnection>& connection)
634{
635 if (mConnections.indexOf(connection) < 0) {
636 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700637 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700638 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700639 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700640}
641
642bool SensorService::SensorRecord::removeConnection(
643 const wp<SensorEventConnection>& connection)
644{
645 ssize_t index = mConnections.indexOf(connection);
646 if (index >= 0) {
647 mConnections.removeItemsAt(index, 1);
648 }
649 return mConnections.size() ? false : true;
650}
651
652// ---------------------------------------------------------------------------
653
654SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700655 const sp<SensorService>& service, uid_t uid)
656 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700657{
658}
659
660SensorService::SensorEventConnection::~SensorEventConnection()
661{
Steve Blocka5512372011-12-20 16:23:08 +0000662 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700663 mService->cleanupConnection(this);
664}
665
666void SensorService::SensorEventConnection::onFirstRef()
667{
668}
669
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700670bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800671 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700672 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800673 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700674 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700675 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700676 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700677}
678
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700679bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800680 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800681 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700682 return true;
683 }
684 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700685}
686
687bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800688 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800689 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700690}
691
692bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800693 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700694 return mSensorInfo.size() ? true : false;
695}
696
Mathias Agopianfc328812010-07-14 23:41:37 -0700697status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700698 sensors_event_t const* buffer, size_t numEvents,
699 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700700{
Mathias Agopiancf510012010-07-22 16:18:10 -0700701 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700702 size_t count = 0;
703 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800704 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700705 size_t i=0;
706 while (i<numEvents) {
707 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800708 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700709 do {
710 scratch[count++] = buffer[i++];
711 } while ((i<numEvents) && (buffer[i].sensor == curr));
712 } else {
713 i++;
714 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700715 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700716 } else {
717 scratch = const_cast<sensors_event_t *>(buffer);
718 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700719 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700720
Mathias Agopian907103b2012-04-02 18:38:02 -0700721 // NOTE: ASensorEvent and sensors_event_t are the same type
722 ssize_t size = SensorEventQueue::write(mChannel,
723 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700724 if (size == -EAGAIN) {
725 // the destination doesn't accept events anymore, it's probably
726 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000727 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700728 return size;
729 }
730
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700731 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700732}
733
Mathias Agopianb3989272011-10-20 18:42:02 -0700734sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700735{
736 return mChannel;
737}
738
739status_t SensorService::SensorEventConnection::enableDisable(
740 int handle, bool enabled)
741{
742 status_t err;
743 if (enabled) {
744 err = mService->enable(this, handle);
745 } else {
746 err = mService->disable(this, handle);
747 }
748 return err;
749}
750
751status_t SensorService::SensorEventConnection::setEventRate(
752 int handle, nsecs_t ns)
753{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700754 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700755}
756
757// ---------------------------------------------------------------------------
758}; // namespace android
759