blob: 7bb6e86493bf7fbc0c805be9b13d229c22da436b [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 Agopiand1920ff2012-05-29 19:46:14 -0700295 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700296 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700297 sensors_event_t buffer[minBufferSize];
298 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800299 SensorDevice& device(SensorDevice::getInstance());
300 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700301
Mathias Agopianf001c922010-11-11 17:58:51 -0800302 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700303 bool wakeLockAcquired = false;
304 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700305 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800306 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700307 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000308 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700309 break;
310 }
311
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700312 // Poll has returned. Hold a wakelock.
313 // Todo(): add a flag to the sensors definitions to indicate
314 // the sensors which can wake up the AP
315 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700316 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700317 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
318 wakeLockAcquired = true;
319 break;
320 }
321 }
322
Mathias Agopian94e8f682010-11-10 17:50:28 -0800323 recordLastValue(buffer, count);
324
Mathias Agopianf001c922010-11-11 17:58:51 -0800325 // handle virtual sensors
326 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700327 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800328 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
329 getActiveVirtualSensors());
330 const size_t activeVirtualSensorCount = virtualSensors.size();
331 if (activeVirtualSensorCount) {
332 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700333 SensorFusion& fusion(SensorFusion::getInstance());
334 if (fusion.isEnabled()) {
335 for (size_t i=0 ; i<size_t(count) ; i++) {
336 fusion.process(event[i]);
337 }
338 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700339 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800340 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700341 if (count + k >= minBufferSize) {
342 ALOGE("buffer too small to hold all events: "
343 "count=%u, k=%u, size=%u",
344 count, k, minBufferSize);
345 break;
346 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800347 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700348 SensorInterface* si = virtualSensors.valueAt(j);
349 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800350 buffer[count + k] = out;
351 k++;
352 }
353 }
354 }
355 if (k) {
356 // record the last synthesized values
357 recordLastValue(&buffer[count], k);
358 count += k;
359 // sort the buffer by time-stamps
360 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700361 }
362 }
363 }
364
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700365 // handle backward compatibility for RotationVector sensor
366 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
367 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700368 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700369 // All the 4 components of the quaternion should be available
370 // No heading accuracy. Set it to -1
371 buffer[i].data[4] = -1;
372 }
373 }
374 }
375
Mathias Agopianf001c922010-11-11 17:58:51 -0800376 // send our events to clients...
377 const SortedVector< wp<SensorEventConnection> > activeConnections(
378 getActiveConnections());
379 size_t numConnections = activeConnections.size();
380 for (size_t i=0 ; i<numConnections ; i++) {
381 sp<SensorEventConnection> connection(
382 activeConnections[i].promote());
383 if (connection != 0) {
384 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700385 // Some sensors need to be auto disabled after the trigger
386 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800387 }
388 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700389
390 // We have read the data, upper layers should hold the wakelock.
391 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
Mathias Agopianfc328812010-07-14 23:41:37 -0700392 } while (count >= 0 || Thread::exitPending());
393
Steve Block3c20fbe2012-01-05 23:22:43 +0000394 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800395 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700396 return false;
397}
398
Mathias Agopian94e8f682010-11-10 17:50:28 -0800399void SensorService::recordLastValue(
400 sensors_event_t const * buffer, size_t count)
401{
402 Mutex::Autolock _l(mLock);
403
404 // record the last event for each sensor
405 int32_t prev = buffer[0].sensor;
406 for (size_t i=1 ; i<count ; i++) {
407 // record the last event of each sensor type in this buffer
408 int32_t curr = buffer[i].sensor;
409 if (curr != prev) {
410 mLastEventSeen.editValueFor(prev) = buffer[i-1];
411 prev = curr;
412 }
413 }
414 mLastEventSeen.editValueFor(prev) = buffer[count-1];
415}
416
Mathias Agopianf001c922010-11-11 17:58:51 -0800417void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
418{
419 struct compar {
420 static int cmp(void const* lhs, void const* rhs) {
421 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
422 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700423 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800424 }
425 };
426 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
427}
428
Mathias Agopianfc328812010-07-14 23:41:37 -0700429SortedVector< wp<SensorService::SensorEventConnection> >
430SensorService::getActiveConnections() const
431{
432 Mutex::Autolock _l(mLock);
433 return mActiveConnections;
434}
435
Mathias Agopianf001c922010-11-11 17:58:51 -0800436DefaultKeyedVector<int, SensorInterface*>
437SensorService::getActiveVirtualSensors() const
438{
439 Mutex::Autolock _l(mLock);
440 return mActiveVirtualSensors;
441}
442
Mathias Agopian5d270722010-07-19 15:20:39 -0700443String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700444 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700445 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700446 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700447 if (sensor.getHandle() == handle) {
448 return sensor.getName();
449 }
450 }
451 String8 result("unknown");
452 return result;
453}
454
Mathias Agopianfc328812010-07-14 23:41:37 -0700455Vector<Sensor> SensorService::getSensorList()
456{
Mathias Agopian33264862012-06-28 19:46:54 -0700457 char value[PROPERTY_VALUE_MAX];
458 property_get("debug.sensors", value, "0");
459 if (atoi(value)) {
460 return mUserSensorListDebug;
461 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700462 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700463}
464
465sp<ISensorEventConnection> SensorService::createSensorEventConnection()
466{
Mathias Agopian5307d172012-09-18 17:02:43 -0700467 uid_t uid = IPCThreadState::self()->getCallingUid();
468 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700469 return result;
470}
471
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800472void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700473{
474 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800475 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700476 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000477 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700478 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800479 int handle = mActiveSensors.keyAt(i);
480 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000481 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800482 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000483 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800484 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800485 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800486 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800487 }
488 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000489 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000490 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700491 "removing connection %p for sensor[%d].handle=0x%08x",
492 c, i, handle);
493
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800494 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000495 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700496 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800497 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700498 delete rec;
499 size--;
500 } else {
501 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700502 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700503 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700504 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700505 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700506}
507
508status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700509 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700510{
Mathias Agopian50df2952010-07-19 19:09:10 -0700511 if (mInitCheck != NO_ERROR)
512 return mInitCheck;
513
Mathias Agopianf001c922010-11-11 17:58:51 -0800514 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700515 if (sensor == NULL) {
516 return BAD_VALUE;
517 }
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700518 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700519 SensorRecord* rec = mActiveSensors.valueFor(handle);
520 if (rec == 0) {
521 rec = new SensorRecord(connection);
522 mActiveSensors.add(handle, rec);
523 if (sensor->isVirtual()) {
524 mActiveVirtualSensors.add(handle, sensor);
525 }
526 } else {
527 if (rec->addConnection(connection)) {
528 // this sensor is already activated, but we are adding a
529 // connection that uses it. Immediately send down the last
530 // known value of the requested sensor if it's not a
531 // "continuous" sensor.
532 if (sensor->getSensor().getMinDelay() == 0) {
533 sensors_event_t scratch;
534 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
535 if (event.version == sizeof(sensors_event_t)) {
536 connection->sendEvents(&event, 1);
537 }
538 }
539 }
540 }
541
542 if (connection->addSensor(handle)) {
543 BatteryService::enableSensor(connection->getUid(), handle);
544 // the sensor was added (which means it wasn't already there)
545 // so, see if this connection becomes active
546 if (mActiveConnections.indexOf(connection) < 0) {
547 mActiveConnections.add(connection);
548 }
549 } else {
550 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
551 handle, connection.get());
552 }
553
Aravind Akella724d91d2013-06-27 12:04:23 -0700554 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
555 if (samplingPeriodNs < minDelayNs) {
556 samplingPeriodNs = minDelayNs;
557 }
558
559 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d rate=%lld timeout== %lld",
560 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
561
562 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
563 maxBatchReportLatencyNs);
564
565 if (err == NO_ERROR) {
566 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
567 err = sensor->activate(connection.get(), true);
568 }
569
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700570 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700571 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700572 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700573 }
574 return err;
575}
576
577status_t SensorService::disable(const sp<SensorEventConnection>& connection,
578 int handle)
579{
Mathias Agopian50df2952010-07-19 19:09:10 -0700580 if (mInitCheck != NO_ERROR)
581 return mInitCheck;
582
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700583 Mutex::Autolock _l(mLock);
584 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700585 if (err == NO_ERROR) {
586 SensorInterface* sensor = mSensorMap.valueFor(handle);
587 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
588 }
589 return err;
590}
591
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700592status_t SensorService::cleanupWithoutDisable(
593 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700594 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700595 return cleanupWithoutDisableLocked(connection, handle);
596}
597
598status_t SensorService::cleanupWithoutDisableLocked(
599 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700600 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700601 if (rec) {
602 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700603 if (connection->removeSensor(handle)) {
604 BatteryService::disableSensor(connection->getUid(), handle);
605 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700606 if (connection->hasAnySensor() == false) {
607 mActiveConnections.remove(connection);
608 }
609 // see if this sensor becomes inactive
610 if (rec->removeConnection(connection)) {
611 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800612 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700613 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700614 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700615 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700616 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700617 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700618}
619
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700620status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700621 int handle, nsecs_t ns)
622{
Mathias Agopian50df2952010-07-19 19:09:10 -0700623 if (mInitCheck != NO_ERROR)
624 return mInitCheck;
625
Mathias Agopianae09d652011-11-01 17:37:49 -0700626 SensorInterface* sensor = mSensorMap.valueFor(handle);
627 if (!sensor)
628 return BAD_VALUE;
629
Mathias Agopian1cd70002010-07-21 15:59:50 -0700630 if (ns < 0)
631 return BAD_VALUE;
632
Mathias Agopian62569ec2011-11-07 21:21:47 -0800633 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
634 if (ns < minDelayNs) {
635 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700636 }
637
Mathias Agopianf001c922010-11-11 17:58:51 -0800638 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700639}
640
Aravind Akella724d91d2013-06-27 12:04:23 -0700641status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
642 int handle) {
643 if (mInitCheck != NO_ERROR) return mInitCheck;
644 SensorInterface* sensor = mSensorMap.valueFor(handle);
645 if (sensor == NULL) {
646 return BAD_VALUE;
647 }
648 return sensor->flush(connection.get(), handle);
649}
Mathias Agopianfc328812010-07-14 23:41:37 -0700650// ---------------------------------------------------------------------------
651
652SensorService::SensorRecord::SensorRecord(
653 const sp<SensorEventConnection>& connection)
654{
655 mConnections.add(connection);
656}
657
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700658bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700659 const sp<SensorEventConnection>& connection)
660{
661 if (mConnections.indexOf(connection) < 0) {
662 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700663 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700664 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700665 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700666}
667
668bool SensorService::SensorRecord::removeConnection(
669 const wp<SensorEventConnection>& connection)
670{
671 ssize_t index = mConnections.indexOf(connection);
672 if (index >= 0) {
673 mConnections.removeItemsAt(index, 1);
674 }
675 return mConnections.size() ? false : true;
676}
677
678// ---------------------------------------------------------------------------
679
680SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700681 const sp<SensorService>& service, uid_t uid)
682 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700683{
684}
685
686SensorService::SensorEventConnection::~SensorEventConnection()
687{
Steve Blocka5512372011-12-20 16:23:08 +0000688 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700689 mService->cleanupConnection(this);
690}
691
692void SensorService::SensorEventConnection::onFirstRef()
693{
694}
695
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700696bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800697 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700698 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800699 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700700 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700701 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700702 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700703}
704
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700705bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800706 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800707 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700708 return true;
709 }
710 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700711}
712
713bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800714 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800715 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700716}
717
718bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800719 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700720 return mSensorInfo.size() ? true : false;
721}
722
Mathias Agopianfc328812010-07-14 23:41:37 -0700723status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700724 sensors_event_t const* buffer, size_t numEvents,
725 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700726{
Mathias Agopiancf510012010-07-22 16:18:10 -0700727 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700728 size_t count = 0;
729 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800730 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700731 size_t i=0;
732 while (i<numEvents) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700733 int32_t curr = buffer[i].sensor;
734 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
735 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
736 buffer[i].meta_data.sensor);
737 // Setting curr to the correct sensor to ensure the sensor events per connection are
738 // filtered correctly. buffer[i].sensor is zero for meta_data events.
739 curr = buffer[i].meta_data.sensor;
740 }
741 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700742 do {
Aravind Akella724d91d2013-06-27 12:04:23 -0700743 scratch[count] = buffer[i];
744 ++count; ++i;
745 } while ((i<numEvents) && ((buffer[i].sensor == curr) ||
746 (buffer[i].type == SENSOR_TYPE_META_DATA &&
747 buffer[i].meta_data.sensor == curr)));
Mathias Agopian3560fb22010-07-22 21:24:39 -0700748 } else {
749 i++;
750 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700751 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700752 } else {
753 scratch = const_cast<sensors_event_t *>(buffer);
754 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700755 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700756
Mathias Agopian907103b2012-04-02 18:38:02 -0700757 // NOTE: ASensorEvent and sensors_event_t are the same type
758 ssize_t size = SensorEventQueue::write(mChannel,
759 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700760 if (size == -EAGAIN) {
761 // the destination doesn't accept events anymore, it's probably
762 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000763 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700764 return size;
765 }
766
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700767 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700768}
769
Mathias Agopianb3989272011-10-20 18:42:02 -0700770sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700771{
772 return mChannel;
773}
774
775status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -0700776 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
777 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700778{
779 status_t err;
780 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700781 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
782 reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700783 } else {
784 err = mService->disable(this, handle);
785 }
786 return err;
787}
788
789status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -0700790 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -0700791{
Aravind Akella724d91d2013-06-27 12:04:23 -0700792 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -0700793}
794
Aravind Akella724d91d2013-06-27 12:04:23 -0700795status_t SensorService::SensorEventConnection::flushSensor(int handle) {
796 return mService->flushSensor(this, handle);
797}
Mathias Agopianfc328812010-07-14 23:41:37 -0700798// ---------------------------------------------------------------------------
799}; // namespace android
800