blob: e3d2a60bd04e6a9b76086adc1d9d58ba799de6f9 [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);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700277 if (sensor != NULL) {
278 sensor->autoDisable(connection.get(), handle);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700279 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700280 cleanupWithoutDisable(connection, handle);
281 }
282 }
283 }
284}
285
Mathias Agopianfc328812010-07-14 23:41:37 -0700286bool SensorService::threadLoop()
287{
Steve Blocka5512372011-12-20 16:23:08 +0000288 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700289
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700290 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700291 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700292 sensors_event_t buffer[minBufferSize];
293 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800294 SensorDevice& device(SensorDevice::getInstance());
295 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700296
Mathias Agopianf001c922010-11-11 17:58:51 -0800297 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700298 bool wakeLockAcquired = false;
299 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700300 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800301 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700302 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000303 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700304 break;
305 }
306
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700307 // Poll has returned. Hold a wakelock.
308 // Todo(): add a flag to the sensors definitions to indicate
309 // the sensors which can wake up the AP
310 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700311 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700312 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
313 wakeLockAcquired = true;
314 break;
315 }
316 }
317
Mathias Agopian94e8f682010-11-10 17:50:28 -0800318 recordLastValue(buffer, count);
319
Mathias Agopianf001c922010-11-11 17:58:51 -0800320 // handle virtual sensors
321 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700322 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800323 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
324 getActiveVirtualSensors());
325 const size_t activeVirtualSensorCount = virtualSensors.size();
326 if (activeVirtualSensorCount) {
327 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700328 SensorFusion& fusion(SensorFusion::getInstance());
329 if (fusion.isEnabled()) {
330 for (size_t i=0 ; i<size_t(count) ; i++) {
331 fusion.process(event[i]);
332 }
333 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700334 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800335 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700336 if (count + k >= minBufferSize) {
337 ALOGE("buffer too small to hold all events: "
338 "count=%u, k=%u, size=%u",
339 count, k, minBufferSize);
340 break;
341 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800342 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700343 SensorInterface* si = virtualSensors.valueAt(j);
344 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800345 buffer[count + k] = out;
346 k++;
347 }
348 }
349 }
350 if (k) {
351 // record the last synthesized values
352 recordLastValue(&buffer[count], k);
353 count += k;
354 // sort the buffer by time-stamps
355 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700356 }
357 }
358 }
359
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700360 // handle backward compatibility for RotationVector sensor
361 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
362 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700363 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700364 // All the 4 components of the quaternion should be available
365 // No heading accuracy. Set it to -1
366 buffer[i].data[4] = -1;
367 }
368 }
369 }
370
Mathias Agopianf001c922010-11-11 17:58:51 -0800371 // send our events to clients...
372 const SortedVector< wp<SensorEventConnection> > activeConnections(
373 getActiveConnections());
374 size_t numConnections = activeConnections.size();
375 for (size_t i=0 ; i<numConnections ; i++) {
376 sp<SensorEventConnection> connection(
377 activeConnections[i].promote());
378 if (connection != 0) {
379 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700380 // Some sensors need to be auto disabled after the trigger
381 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800382 }
383 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700384
385 // We have read the data, upper layers should hold the wakelock.
386 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
387
Mathias Agopianfc328812010-07-14 23:41:37 -0700388 } while (count >= 0 || Thread::exitPending());
389
Steve Block3c20fbe2012-01-05 23:22:43 +0000390 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800391 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700392 return false;
393}
394
Mathias Agopian94e8f682010-11-10 17:50:28 -0800395void SensorService::recordLastValue(
396 sensors_event_t const * buffer, size_t count)
397{
398 Mutex::Autolock _l(mLock);
399
400 // record the last event for each sensor
401 int32_t prev = buffer[0].sensor;
402 for (size_t i=1 ; i<count ; i++) {
403 // record the last event of each sensor type in this buffer
404 int32_t curr = buffer[i].sensor;
405 if (curr != prev) {
406 mLastEventSeen.editValueFor(prev) = buffer[i-1];
407 prev = curr;
408 }
409 }
410 mLastEventSeen.editValueFor(prev) = buffer[count-1];
411}
412
Mathias Agopianf001c922010-11-11 17:58:51 -0800413void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
414{
415 struct compar {
416 static int cmp(void const* lhs, void const* rhs) {
417 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
418 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700419 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800420 }
421 };
422 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
423}
424
Mathias Agopianfc328812010-07-14 23:41:37 -0700425SortedVector< wp<SensorService::SensorEventConnection> >
426SensorService::getActiveConnections() const
427{
428 Mutex::Autolock _l(mLock);
429 return mActiveConnections;
430}
431
Mathias Agopianf001c922010-11-11 17:58:51 -0800432DefaultKeyedVector<int, SensorInterface*>
433SensorService::getActiveVirtualSensors() const
434{
435 Mutex::Autolock _l(mLock);
436 return mActiveVirtualSensors;
437}
438
Mathias Agopian5d270722010-07-19 15:20:39 -0700439String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700440 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700441 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700442 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700443 if (sensor.getHandle() == handle) {
444 return sensor.getName();
445 }
446 }
447 String8 result("unknown");
448 return result;
449}
450
Mathias Agopianfc328812010-07-14 23:41:37 -0700451Vector<Sensor> SensorService::getSensorList()
452{
Mathias Agopian33264862012-06-28 19:46:54 -0700453 char value[PROPERTY_VALUE_MAX];
454 property_get("debug.sensors", value, "0");
455 if (atoi(value)) {
456 return mUserSensorListDebug;
457 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700458 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700459}
460
461sp<ISensorEventConnection> SensorService::createSensorEventConnection()
462{
Mathias Agopian5307d172012-09-18 17:02:43 -0700463 uid_t uid = IPCThreadState::self()->getCallingUid();
464 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700465 return result;
466}
467
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800468void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700469{
470 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800471 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700472 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000473 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700474 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800475 int handle = mActiveSensors.keyAt(i);
476 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000477 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800478 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000479 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800480 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800481 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800482 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800483 }
484 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000485 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000486 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700487 "removing connection %p for sensor[%d].handle=0x%08x",
488 c, i, handle);
489
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800490 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000491 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700492 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800493 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700494 delete rec;
495 size--;
496 } else {
497 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700498 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700499 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700500 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700501 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700502}
503
504status_t SensorService::enable(const sp<SensorEventConnection>& connection,
505 int handle)
506{
Mathias Agopian50df2952010-07-19 19:09:10 -0700507 if (mInitCheck != NO_ERROR)
508 return mInitCheck;
509
Mathias Agopianf001c922010-11-11 17:58:51 -0800510 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700511 if (sensor == NULL) {
512 return BAD_VALUE;
513 }
514
515 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700516 SensorRecord* rec = mActiveSensors.valueFor(handle);
517 if (rec == 0) {
518 rec = new SensorRecord(connection);
519 mActiveSensors.add(handle, rec);
520 if (sensor->isVirtual()) {
521 mActiveVirtualSensors.add(handle, sensor);
522 }
523 } else {
524 if (rec->addConnection(connection)) {
525 // this sensor is already activated, but we are adding a
526 // connection that uses it. Immediately send down the last
527 // known value of the requested sensor if it's not a
528 // "continuous" sensor.
529 if (sensor->getSensor().getMinDelay() == 0) {
530 sensors_event_t scratch;
531 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
532 if (event.version == sizeof(sensors_event_t)) {
533 connection->sendEvents(&event, 1);
534 }
535 }
536 }
537 }
538
539 if (connection->addSensor(handle)) {
540 BatteryService::enableSensor(connection->getUid(), handle);
541 // the sensor was added (which means it wasn't already there)
542 // so, see if this connection becomes active
543 if (mActiveConnections.indexOf(connection) < 0) {
544 mActiveConnections.add(connection);
545 }
546 } else {
547 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
548 handle, connection.get());
549 }
550
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700551 // we are setup, now enable the sensor.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700552 status_t err = sensor->activate(connection.get(), true);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700553 if (err != NO_ERROR) {
554 // enable has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700555 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700556 }
557 return err;
558}
559
560status_t SensorService::disable(const sp<SensorEventConnection>& connection,
561 int handle)
562{
Mathias Agopian50df2952010-07-19 19:09:10 -0700563 if (mInitCheck != NO_ERROR)
564 return mInitCheck;
565
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700566 Mutex::Autolock _l(mLock);
567 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700568 if (err == NO_ERROR) {
569 SensorInterface* sensor = mSensorMap.valueFor(handle);
570 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
571 }
572 return err;
573}
574
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700575status_t SensorService::cleanupWithoutDisable(
576 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700577 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700578 return cleanupWithoutDisableLocked(connection, handle);
579}
580
581status_t SensorService::cleanupWithoutDisableLocked(
582 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700583 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700584 if (rec) {
585 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700586 if (connection->removeSensor(handle)) {
587 BatteryService::disableSensor(connection->getUid(), handle);
588 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700589 if (connection->hasAnySensor() == false) {
590 mActiveConnections.remove(connection);
591 }
592 // see if this sensor becomes inactive
593 if (rec->removeConnection(connection)) {
594 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800595 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700596 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700597 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700598 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700599 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700600 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700601}
602
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700603status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700604 int handle, nsecs_t ns)
605{
Mathias Agopian50df2952010-07-19 19:09:10 -0700606 if (mInitCheck != NO_ERROR)
607 return mInitCheck;
608
Mathias Agopianae09d652011-11-01 17:37:49 -0700609 SensorInterface* sensor = mSensorMap.valueFor(handle);
610 if (!sensor)
611 return BAD_VALUE;
612
Mathias Agopian1cd70002010-07-21 15:59:50 -0700613 if (ns < 0)
614 return BAD_VALUE;
615
Mathias Agopian62569ec2011-11-07 21:21:47 -0800616 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
617 if (ns < minDelayNs) {
618 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700619 }
620
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700621 if (ns < MINIMUM_EVENTS_PERIOD)
622 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700623
Mathias Agopianf001c922010-11-11 17:58:51 -0800624 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700625}
626
627// ---------------------------------------------------------------------------
628
629SensorService::SensorRecord::SensorRecord(
630 const sp<SensorEventConnection>& connection)
631{
632 mConnections.add(connection);
633}
634
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700635bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700636 const sp<SensorEventConnection>& connection)
637{
638 if (mConnections.indexOf(connection) < 0) {
639 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700640 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700641 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700642 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700643}
644
645bool SensorService::SensorRecord::removeConnection(
646 const wp<SensorEventConnection>& connection)
647{
648 ssize_t index = mConnections.indexOf(connection);
649 if (index >= 0) {
650 mConnections.removeItemsAt(index, 1);
651 }
652 return mConnections.size() ? false : true;
653}
654
655// ---------------------------------------------------------------------------
656
657SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700658 const sp<SensorService>& service, uid_t uid)
659 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700660{
661}
662
663SensorService::SensorEventConnection::~SensorEventConnection()
664{
Steve Blocka5512372011-12-20 16:23:08 +0000665 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700666 mService->cleanupConnection(this);
667}
668
669void SensorService::SensorEventConnection::onFirstRef()
670{
671}
672
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700673bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800674 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700675 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800676 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700677 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700678 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700679 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700680}
681
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700682bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800683 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800684 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700685 return true;
686 }
687 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700688}
689
690bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800691 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800692 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700693}
694
695bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800696 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700697 return mSensorInfo.size() ? true : false;
698}
699
Mathias Agopianfc328812010-07-14 23:41:37 -0700700status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700701 sensors_event_t const* buffer, size_t numEvents,
702 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700703{
Mathias Agopiancf510012010-07-22 16:18:10 -0700704 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700705 size_t count = 0;
706 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800707 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700708 size_t i=0;
709 while (i<numEvents) {
710 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800711 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700712 do {
713 scratch[count++] = buffer[i++];
714 } while ((i<numEvents) && (buffer[i].sensor == curr));
715 } else {
716 i++;
717 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700718 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700719 } else {
720 scratch = const_cast<sensors_event_t *>(buffer);
721 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700722 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700723
Mathias Agopian907103b2012-04-02 18:38:02 -0700724 // NOTE: ASensorEvent and sensors_event_t are the same type
725 ssize_t size = SensorEventQueue::write(mChannel,
726 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700727 if (size == -EAGAIN) {
728 // the destination doesn't accept events anymore, it's probably
729 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000730 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700731 return size;
732 }
733
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700734 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700735}
736
Mathias Agopianb3989272011-10-20 18:42:02 -0700737sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700738{
739 return mChannel;
740}
741
742status_t SensorService::SensorEventConnection::enableDisable(
743 int handle, bool enabled)
744{
745 status_t err;
746 if (enabled) {
747 err = mService->enable(this, handle);
748 } else {
749 err = mService->disable(this, handle);
750 }
751 return err;
752}
753
754status_t SensorService::SensorEventConnection::setEventRate(
755 int handle, nsecs_t ns)
756{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700757 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700758}
759
760// ---------------------------------------------------------------------------
761}; // namespace android
762