blob: cb99531f93f55ea1fb56dc621d3c039c6192cbb1 [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 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700216 if (s.getFifoMaxEventCount() > 0) {
217 result.appendFormat("getFifoMaxEventCount=%d events | ", s.getFifoMaxEventCount());
218 } else {
219 result.append("no batching support | ");
220 }
Mathias Agopianba02cd22013-07-03 16:20:57 -0700221
222 switch (s.getType()) {
223 case SENSOR_TYPE_ROTATION_VECTOR:
224 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
225 result.appendFormat(
226 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
227 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]);
228 break;
229 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
230 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
231 result.appendFormat(
232 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
233 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]);
234 break;
235 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
236 result.appendFormat(
237 "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n",
238 e.data[0], e.data[1], e.data[2], e.data[3]);
239 break;
240 case SENSOR_TYPE_SIGNIFICANT_MOTION:
241 case SENSOR_TYPE_STEP_DETECTOR:
242 result.appendFormat( "last=<%f>\n", e.data[0]);
243 break;
244 case SENSOR_TYPE_STEP_COUNTER:
245 result.appendFormat( "last=<%llu>\n", e.u64.step_counter);
246 break;
247 default:
248 // default to 3 values
249 result.appendFormat(
250 "last=<%5.1f,%5.1f,%5.1f>\n",
251 e.data[0], e.data[1], e.data[2]);
252 break;
253 }
254 }
255 SensorFusion::getInstance().dump(result);
256 SensorDevice::getInstance().dump(result);
257
258 result.appendFormat("%d active connections\n", mActiveConnections.size());
259 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700260 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700261 int handle = mActiveSensors.keyAt(i);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700262 result.appendFormat("%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700263 getSensorName(handle).string(),
264 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700265 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700266 }
267 }
268 write(fd, result.string(), result.size());
269 return NO_ERROR;
270}
271
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700272void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
273 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700274 SensorInterface* sensor;
275 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700276 for (int i=0 ; i<count ; i++) {
277 int handle = buffer[i].sensor;
Mathias Agopian7438fd12013-07-08 12:50:39 -0700278 int type = buffer[i].type;
279 if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700280 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700281 sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700282 if (sensor != NULL) {
283 sensor->autoDisable(connection.get(), handle);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700284 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700285 cleanupWithoutDisable(connection, handle);
286 }
287 }
288 }
289}
290
Mathias Agopianfc328812010-07-14 23:41:37 -0700291bool SensorService::threadLoop()
292{
Steve Blocka5512372011-12-20 16:23:08 +0000293 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700294
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700295 // each virtual sensor could generate an event per "real" event, that's why we need
296 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
297 // in practice, this is too aggressive, but guaranteed to be enough.
298 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
299 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
300
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700301 sensors_event_t buffer[minBufferSize];
302 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800303 SensorDevice& device(SensorDevice::getInstance());
304 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700305
Mathias Agopianf001c922010-11-11 17:58:51 -0800306 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700307 bool wakeLockAcquired = false;
308 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700309 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800310 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700311 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000312 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700313 break;
314 }
315
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700316 // Poll has returned. Hold a wakelock.
317 // Todo(): add a flag to the sensors definitions to indicate
318 // the sensors which can wake up the AP
319 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700320 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700321 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
322 wakeLockAcquired = true;
323 break;
324 }
325 }
326
Mathias Agopian94e8f682010-11-10 17:50:28 -0800327 recordLastValue(buffer, count);
328
Mathias Agopianf001c922010-11-11 17:58:51 -0800329 // handle virtual sensors
330 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700331 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800332 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
333 getActiveVirtualSensors());
334 const size_t activeVirtualSensorCount = virtualSensors.size();
335 if (activeVirtualSensorCount) {
336 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700337 SensorFusion& fusion(SensorFusion::getInstance());
338 if (fusion.isEnabled()) {
339 for (size_t i=0 ; i<size_t(count) ; i++) {
340 fusion.process(event[i]);
341 }
342 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700343 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800344 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700345 if (count + k >= minBufferSize) {
346 ALOGE("buffer too small to hold all events: "
347 "count=%u, k=%u, size=%u",
348 count, k, minBufferSize);
349 break;
350 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800351 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700352 SensorInterface* si = virtualSensors.valueAt(j);
353 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800354 buffer[count + k] = out;
355 k++;
356 }
357 }
358 }
359 if (k) {
360 // record the last synthesized values
361 recordLastValue(&buffer[count], k);
362 count += k;
363 // sort the buffer by time-stamps
364 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700365 }
366 }
367 }
368
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700369 // handle backward compatibility for RotationVector sensor
370 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
371 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700372 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700373 // All the 4 components of the quaternion should be available
374 // No heading accuracy. Set it to -1
375 buffer[i].data[4] = -1;
376 }
377 }
378 }
379
Mathias Agopianf001c922010-11-11 17:58:51 -0800380 // send our events to clients...
381 const SortedVector< wp<SensorEventConnection> > activeConnections(
382 getActiveConnections());
383 size_t numConnections = activeConnections.size();
384 for (size_t i=0 ; i<numConnections ; i++) {
385 sp<SensorEventConnection> connection(
386 activeConnections[i].promote());
387 if (connection != 0) {
388 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700389 // Some sensors need to be auto disabled after the trigger
390 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800391 }
392 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700393
394 // We have read the data, upper layers should hold the wakelock.
395 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
Mathias Agopianfc328812010-07-14 23:41:37 -0700396 } while (count >= 0 || Thread::exitPending());
397
Steve Block3c20fbe2012-01-05 23:22:43 +0000398 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800399 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700400 return false;
401}
402
Mathias Agopian94e8f682010-11-10 17:50:28 -0800403void SensorService::recordLastValue(
404 sensors_event_t const * buffer, size_t count)
405{
406 Mutex::Autolock _l(mLock);
407
408 // record the last event for each sensor
409 int32_t prev = buffer[0].sensor;
410 for (size_t i=1 ; i<count ; i++) {
411 // record the last event of each sensor type in this buffer
412 int32_t curr = buffer[i].sensor;
413 if (curr != prev) {
414 mLastEventSeen.editValueFor(prev) = buffer[i-1];
415 prev = curr;
416 }
417 }
418 mLastEventSeen.editValueFor(prev) = buffer[count-1];
419}
420
Mathias Agopianf001c922010-11-11 17:58:51 -0800421void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
422{
423 struct compar {
424 static int cmp(void const* lhs, void const* rhs) {
425 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
426 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700427 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800428 }
429 };
430 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
431}
432
Mathias Agopianfc328812010-07-14 23:41:37 -0700433SortedVector< wp<SensorService::SensorEventConnection> >
434SensorService::getActiveConnections() const
435{
436 Mutex::Autolock _l(mLock);
437 return mActiveConnections;
438}
439
Mathias Agopianf001c922010-11-11 17:58:51 -0800440DefaultKeyedVector<int, SensorInterface*>
441SensorService::getActiveVirtualSensors() const
442{
443 Mutex::Autolock _l(mLock);
444 return mActiveVirtualSensors;
445}
446
Mathias Agopian5d270722010-07-19 15:20:39 -0700447String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700448 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700449 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700450 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700451 if (sensor.getHandle() == handle) {
452 return sensor.getName();
453 }
454 }
455 String8 result("unknown");
456 return result;
457}
458
Mathias Agopianfc328812010-07-14 23:41:37 -0700459Vector<Sensor> SensorService::getSensorList()
460{
Mathias Agopian33264862012-06-28 19:46:54 -0700461 char value[PROPERTY_VALUE_MAX];
462 property_get("debug.sensors", value, "0");
463 if (atoi(value)) {
464 return mUserSensorListDebug;
465 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700466 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700467}
468
469sp<ISensorEventConnection> SensorService::createSensorEventConnection()
470{
Mathias Agopian5307d172012-09-18 17:02:43 -0700471 uid_t uid = IPCThreadState::self()->getCallingUid();
472 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700473 return result;
474}
475
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800476void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700477{
478 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800479 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700480 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000481 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700482 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800483 int handle = mActiveSensors.keyAt(i);
484 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000485 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800486 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000487 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800488 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800489 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800490 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800491 }
492 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000493 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000494 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700495 "removing connection %p for sensor[%d].handle=0x%08x",
496 c, i, handle);
497
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800498 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000499 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700500 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800501 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700502 delete rec;
503 size--;
504 } else {
505 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700506 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700507 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700508 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700509 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700510}
511
512status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700513 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700514{
Mathias Agopian50df2952010-07-19 19:09:10 -0700515 if (mInitCheck != NO_ERROR)
516 return mInitCheck;
517
Mathias Agopianf001c922010-11-11 17:58:51 -0800518 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700519 if (sensor == NULL) {
520 return BAD_VALUE;
521 }
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700522 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700523 SensorRecord* rec = mActiveSensors.valueFor(handle);
524 if (rec == 0) {
525 rec = new SensorRecord(connection);
526 mActiveSensors.add(handle, rec);
527 if (sensor->isVirtual()) {
528 mActiveVirtualSensors.add(handle, sensor);
529 }
530 } else {
531 if (rec->addConnection(connection)) {
532 // this sensor is already activated, but we are adding a
533 // connection that uses it. Immediately send down the last
534 // known value of the requested sensor if it's not a
535 // "continuous" sensor.
536 if (sensor->getSensor().getMinDelay() == 0) {
537 sensors_event_t scratch;
538 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
539 if (event.version == sizeof(sensors_event_t)) {
540 connection->sendEvents(&event, 1);
541 }
542 }
543 }
544 }
545
546 if (connection->addSensor(handle)) {
547 BatteryService::enableSensor(connection->getUid(), handle);
548 // the sensor was added (which means it wasn't already there)
549 // so, see if this connection becomes active
550 if (mActiveConnections.indexOf(connection) < 0) {
551 mActiveConnections.add(connection);
552 }
553 } else {
554 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
555 handle, connection.get());
556 }
557
Aravind Akella724d91d2013-06-27 12:04:23 -0700558 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
559 if (samplingPeriodNs < minDelayNs) {
560 samplingPeriodNs = minDelayNs;
561 }
562
563 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d rate=%lld timeout== %lld",
564 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
565
566 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
567 maxBatchReportLatencyNs);
568
569 if (err == NO_ERROR) {
570 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
571 err = sensor->activate(connection.get(), true);
572 }
573
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700574 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700575 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700576 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700577 }
578 return err;
579}
580
581status_t SensorService::disable(const sp<SensorEventConnection>& connection,
582 int handle)
583{
Mathias Agopian50df2952010-07-19 19:09:10 -0700584 if (mInitCheck != NO_ERROR)
585 return mInitCheck;
586
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700587 Mutex::Autolock _l(mLock);
588 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700589 if (err == NO_ERROR) {
590 SensorInterface* sensor = mSensorMap.valueFor(handle);
591 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
592 }
593 return err;
594}
595
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700596status_t SensorService::cleanupWithoutDisable(
597 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700598 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700599 return cleanupWithoutDisableLocked(connection, handle);
600}
601
602status_t SensorService::cleanupWithoutDisableLocked(
603 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700604 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700605 if (rec) {
606 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700607 if (connection->removeSensor(handle)) {
608 BatteryService::disableSensor(connection->getUid(), handle);
609 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700610 if (connection->hasAnySensor() == false) {
611 mActiveConnections.remove(connection);
612 }
613 // see if this sensor becomes inactive
614 if (rec->removeConnection(connection)) {
615 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800616 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700617 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700618 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700619 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700620 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700621 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700622}
623
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700624status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700625 int handle, nsecs_t ns)
626{
Mathias Agopian50df2952010-07-19 19:09:10 -0700627 if (mInitCheck != NO_ERROR)
628 return mInitCheck;
629
Mathias Agopianae09d652011-11-01 17:37:49 -0700630 SensorInterface* sensor = mSensorMap.valueFor(handle);
631 if (!sensor)
632 return BAD_VALUE;
633
Mathias Agopian1cd70002010-07-21 15:59:50 -0700634 if (ns < 0)
635 return BAD_VALUE;
636
Mathias Agopian62569ec2011-11-07 21:21:47 -0800637 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
638 if (ns < minDelayNs) {
639 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700640 }
641
Mathias Agopianf001c922010-11-11 17:58:51 -0800642 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700643}
644
Aravind Akella724d91d2013-06-27 12:04:23 -0700645status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
646 int handle) {
647 if (mInitCheck != NO_ERROR) return mInitCheck;
648 SensorInterface* sensor = mSensorMap.valueFor(handle);
649 if (sensor == NULL) {
650 return BAD_VALUE;
651 }
652 return sensor->flush(connection.get(), handle);
653}
Mathias Agopianfc328812010-07-14 23:41:37 -0700654// ---------------------------------------------------------------------------
655
656SensorService::SensorRecord::SensorRecord(
657 const sp<SensorEventConnection>& connection)
658{
659 mConnections.add(connection);
660}
661
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700662bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700663 const sp<SensorEventConnection>& connection)
664{
665 if (mConnections.indexOf(connection) < 0) {
666 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700667 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700668 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700669 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700670}
671
672bool SensorService::SensorRecord::removeConnection(
673 const wp<SensorEventConnection>& connection)
674{
675 ssize_t index = mConnections.indexOf(connection);
676 if (index >= 0) {
677 mConnections.removeItemsAt(index, 1);
678 }
679 return mConnections.size() ? false : true;
680}
681
682// ---------------------------------------------------------------------------
683
684SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700685 const sp<SensorService>& service, uid_t uid)
686 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700687{
688}
689
690SensorService::SensorEventConnection::~SensorEventConnection()
691{
Steve Blocka5512372011-12-20 16:23:08 +0000692 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700693 mService->cleanupConnection(this);
694}
695
696void SensorService::SensorEventConnection::onFirstRef()
697{
698}
699
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700700bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800701 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700702 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800703 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700704 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700705 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700706 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700707}
708
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700709bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800710 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800711 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700712 return true;
713 }
714 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700715}
716
717bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800718 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800719 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700720}
721
722bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800723 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700724 return mSensorInfo.size() ? true : false;
725}
726
Mathias Agopianfc328812010-07-14 23:41:37 -0700727status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700728 sensors_event_t const* buffer, size_t numEvents,
729 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700730{
Mathias Agopiancf510012010-07-22 16:18:10 -0700731 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700732 size_t count = 0;
733 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800734 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700735 size_t i=0;
736 while (i<numEvents) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700737 int32_t curr = buffer[i].sensor;
738 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
739 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
740 buffer[i].meta_data.sensor);
741 // Setting curr to the correct sensor to ensure the sensor events per connection are
742 // filtered correctly. buffer[i].sensor is zero for meta_data events.
743 curr = buffer[i].meta_data.sensor;
744 }
745 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700746 do {
Aravind Akella724d91d2013-06-27 12:04:23 -0700747 scratch[count] = buffer[i];
748 ++count; ++i;
749 } while ((i<numEvents) && ((buffer[i].sensor == curr) ||
750 (buffer[i].type == SENSOR_TYPE_META_DATA &&
751 buffer[i].meta_data.sensor == curr)));
Mathias Agopian3560fb22010-07-22 21:24:39 -0700752 } else {
753 i++;
754 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700755 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700756 } else {
757 scratch = const_cast<sensors_event_t *>(buffer);
758 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700759 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700760
Mathias Agopian907103b2012-04-02 18:38:02 -0700761 // NOTE: ASensorEvent and sensors_event_t are the same type
762 ssize_t size = SensorEventQueue::write(mChannel,
763 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700764 if (size == -EAGAIN) {
765 // the destination doesn't accept events anymore, it's probably
766 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000767 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700768 return size;
769 }
770
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700771 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700772}
773
Mathias Agopianb3989272011-10-20 18:42:02 -0700774sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700775{
776 return mChannel;
777}
778
779status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -0700780 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
781 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700782{
783 status_t err;
784 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700785 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
786 reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700787 } else {
788 err = mService->disable(this, handle);
789 }
790 return err;
791}
792
793status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -0700794 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -0700795{
Aravind Akella724d91d2013-06-27 12:04:23 -0700796 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -0700797}
798
Aravind Akella724d91d2013-06-27 12:04:23 -0700799status_t SensorService::SensorEventConnection::flushSensor(int handle) {
800 return mService->flushSensor(this, handle);
801}
Mathias Agopianfc328812010-07-14 23:41:37 -0700802// ---------------------------------------------------------------------------
803}; // namespace android
804