blob: e3dcd02875a49de28424583fdb36d6eb0068850f [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>
41
Mathias Agopian787ac1b2012-09-18 18:49:18 -070042#include "BatteryService.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070043#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080044#include "GravitySensor.h"
45#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070046#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080047#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070048#include "SensorFusion.h"
49#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070050
51namespace android {
52// ---------------------------------------------------------------------------
53
Mathias Agopian33015422011-05-27 18:18:13 -070054/*
55 * Notes:
56 *
57 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070058 * - run mag sensor from time to time to force calibration
59 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
60 *
61 */
62
Mathias Agopianfc328812010-07-14 23:41:37 -070063SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -070064 : mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070065{
66}
67
68void SensorService::onFirstRef()
69{
Steve Blocka5512372011-12-20 16:23:08 +000070 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070071
Mathias Agopianf001c922010-11-11 17:58:51 -080072 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070073
Mathias Agopianf001c922010-11-11 17:58:51 -080074 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080075 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070076 ssize_t count = dev.getSensorList(&list);
77 if (count > 0) {
78 ssize_t orientationIndex = -1;
79 bool hasGyro = false;
80 uint32_t virtualSensorsNeeds =
81 (1<<SENSOR_TYPE_GRAVITY) |
82 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
83 (1<<SENSOR_TYPE_ROTATION_VECTOR);
84
85 mLastEventSeen.setCapacity(count);
86 for (ssize_t i=0 ; i<count ; i++) {
87 registerSensor( new HardwareSensor(list[i]) );
88 switch (list[i].type) {
89 case SENSOR_TYPE_ORIENTATION:
90 orientationIndex = i;
91 break;
92 case SENSOR_TYPE_GYROSCOPE:
93 hasGyro = true;
94 break;
95 case SENSOR_TYPE_GRAVITY:
96 case SENSOR_TYPE_LINEAR_ACCELERATION:
97 case SENSOR_TYPE_ROTATION_VECTOR:
98 virtualSensorsNeeds &= ~(1<<list[i].type);
99 break;
100 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700101 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700102
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700103 // it's safe to instantiate the SensorFusion object here
104 // (it wants to be instantiated after h/w sensors have been
105 // registered)
106 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700107
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700108 if (hasGyro) {
109 // Always instantiate Android's virtual sensors. Since they are
110 // instantiated behind sensors from the HAL, they won't
111 // interfere with applications, unless they looks specifically
112 // for them (by name).
Mathias Agopian984826c2011-05-17 22:54:42 -0700113
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700114 registerVirtualSensor( new RotationVectorSensor() );
115 registerVirtualSensor( new GravitySensor(list, count) );
116 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian984826c2011-05-17 22:54:42 -0700117
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700118 // these are optional
119 registerVirtualSensor( new OrientationSensor() );
120 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33015422011-05-27 18:18:13 -0700121 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800122
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700123 // build the sensor list returned to users
124 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700125
126 if (hasGyro) {
127 // virtual debugging sensors are not added to mUserSensorList
128 registerVirtualSensor( new GyroDriftSensor() );
129 }
130
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700131 if (hasGyro &&
132 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
133 // if we have the fancy sensor fusion, and it's not provided by the
134 // HAL, use our own (fused) orientation sensor by removing the
135 // HAL supplied one form the user list.
136 if (orientationIndex >= 0) {
137 mUserSensorList.removeItemsAt(orientationIndex);
138 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700139 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700140
Mathias Agopian33264862012-06-28 19:46:54 -0700141 // debugging sensor list
142 for (size_t i=0 ; i<mSensorList.size() ; i++) {
143 switch (mSensorList[i].getType()) {
144 case SENSOR_TYPE_GRAVITY:
145 case SENSOR_TYPE_LINEAR_ACCELERATION:
146 case SENSOR_TYPE_ROTATION_VECTOR:
147 if (strstr(mSensorList[i].getVendor().string(), "Google")) {
148 mUserSensorListDebug.add(mSensorList[i]);
149 }
150 break;
151 default:
152 mUserSensorListDebug.add(mSensorList[i]);
153 break;
154 }
155 }
156
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700157 run("SensorService", PRIORITY_URGENT_DISPLAY);
158 mInitCheck = NO_ERROR;
159 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700160 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700161}
162
Mathias Agopianf001c922010-11-11 17:58:51 -0800163void SensorService::registerSensor(SensorInterface* s)
164{
165 sensors_event_t event;
166 memset(&event, 0, sizeof(event));
167
168 const Sensor sensor(s->getSensor());
169 // add to the sensor list (returned to clients)
170 mSensorList.add(sensor);
171 // add to our handle->SensorInterface mapping
172 mSensorMap.add(sensor.getHandle(), s);
173 // create an entry in the mLastEventSeen array
174 mLastEventSeen.add(sensor.getHandle(), event);
175}
176
177void SensorService::registerVirtualSensor(SensorInterface* s)
178{
179 registerSensor(s);
180 mVirtualSensorList.add( s );
181}
182
Mathias Agopianfc328812010-07-14 23:41:37 -0700183SensorService::~SensorService()
184{
Mathias Agopianf001c922010-11-11 17:58:51 -0800185 for (size_t i=0 ; i<mSensorMap.size() ; i++)
186 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700187}
188
Mathias Agopian1cb13462011-06-27 16:05:52 -0700189static const String16 sDump("android.permission.DUMP");
190
Mathias Agopianfc328812010-07-14 23:41:37 -0700191status_t SensorService::dump(int fd, const Vector<String16>& args)
192{
193 const size_t SIZE = 1024;
194 char buffer[SIZE];
195 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700196 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700197 snprintf(buffer, SIZE, "Permission Denial: "
198 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
199 IPCThreadState::self()->getCallingPid(),
200 IPCThreadState::self()->getCallingUid());
201 result.append(buffer);
202 } else {
203 Mutex::Autolock _l(mLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700204 snprintf(buffer, SIZE, "Sensor List:\n");
205 result.append(buffer);
206 for (size_t i=0 ; i<mSensorList.size() ; i++) {
207 const Sensor& s(mSensorList[i]);
208 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian984826c2011-05-17 22:54:42 -0700209 snprintf(buffer, SIZE,
210 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
211 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700212 s.getName().string(),
213 s.getVendor().string(),
214 s.getHandle(),
Mathias Agopian24d72352010-11-05 19:12:58 -0700215 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700216 e.data[0], e.data[1], e.data[2]);
217 result.append(buffer);
218 }
Mathias Agopian984826c2011-05-17 22:54:42 -0700219 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopianf001c922010-11-11 17:58:51 -0800220 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700221
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700222 snprintf(buffer, SIZE, "%d active connections\n",
223 mActiveConnections.size());
Mathias Agopianfc328812010-07-14 23:41:37 -0700224 result.append(buffer);
225 snprintf(buffer, SIZE, "Active sensors:\n");
226 result.append(buffer);
227 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700228 int handle = mActiveSensors.keyAt(i);
Mathias Agopianf001c922010-11-11 17:58:51 -0800229 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700230 getSensorName(handle).string(),
231 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700232 mActiveSensors.valueAt(i)->getNumConnections());
233 result.append(buffer);
234 }
235 }
236 write(fd, result.string(), result.size());
237 return NO_ERROR;
238}
239
240bool SensorService::threadLoop()
241{
Steve Blocka5512372011-12-20 16:23:08 +0000242 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700243
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700244 const size_t numEventMax = 16;
Mathias Agopian8dd4fe82012-05-30 18:08:30 -0700245 const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700246 sensors_event_t buffer[minBufferSize];
247 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800248 SensorDevice& device(SensorDevice::getInstance());
249 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700250
Mathias Agopianf001c922010-11-11 17:58:51 -0800251 ssize_t count;
Mathias Agopianfc328812010-07-14 23:41:37 -0700252 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800253 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700254 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000255 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700256 break;
257 }
258
Mathias Agopian94e8f682010-11-10 17:50:28 -0800259 recordLastValue(buffer, count);
260
Mathias Agopianf001c922010-11-11 17:58:51 -0800261 // handle virtual sensors
262 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700263 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800264 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
265 getActiveVirtualSensors());
266 const size_t activeVirtualSensorCount = virtualSensors.size();
267 if (activeVirtualSensorCount) {
268 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700269 SensorFusion& fusion(SensorFusion::getInstance());
270 if (fusion.isEnabled()) {
271 for (size_t i=0 ; i<size_t(count) ; i++) {
272 fusion.process(event[i]);
273 }
274 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700275 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800276 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700277 if (count + k >= minBufferSize) {
278 ALOGE("buffer too small to hold all events: "
279 "count=%u, k=%u, size=%u",
280 count, k, minBufferSize);
281 break;
282 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800283 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700284 SensorInterface* si = virtualSensors.valueAt(j);
285 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800286 buffer[count + k] = out;
287 k++;
288 }
289 }
290 }
291 if (k) {
292 // record the last synthesized values
293 recordLastValue(&buffer[count], k);
294 count += k;
295 // sort the buffer by time-stamps
296 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700297 }
298 }
299 }
300
Mathias Agopianf001c922010-11-11 17:58:51 -0800301 // send our events to clients...
302 const SortedVector< wp<SensorEventConnection> > activeConnections(
303 getActiveConnections());
304 size_t numConnections = activeConnections.size();
305 for (size_t i=0 ; i<numConnections ; i++) {
306 sp<SensorEventConnection> connection(
307 activeConnections[i].promote());
308 if (connection != 0) {
309 connection->sendEvents(buffer, count, scratch);
310 }
311 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700312 } while (count >= 0 || Thread::exitPending());
313
Steve Block3c20fbe2012-01-05 23:22:43 +0000314 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800315 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700316 return false;
317}
318
Mathias Agopian94e8f682010-11-10 17:50:28 -0800319void SensorService::recordLastValue(
320 sensors_event_t const * buffer, size_t count)
321{
322 Mutex::Autolock _l(mLock);
323
324 // record the last event for each sensor
325 int32_t prev = buffer[0].sensor;
326 for (size_t i=1 ; i<count ; i++) {
327 // record the last event of each sensor type in this buffer
328 int32_t curr = buffer[i].sensor;
329 if (curr != prev) {
330 mLastEventSeen.editValueFor(prev) = buffer[i-1];
331 prev = curr;
332 }
333 }
334 mLastEventSeen.editValueFor(prev) = buffer[count-1];
335}
336
Mathias Agopianf001c922010-11-11 17:58:51 -0800337void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
338{
339 struct compar {
340 static int cmp(void const* lhs, void const* rhs) {
341 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
342 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700343 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800344 }
345 };
346 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
347}
348
Mathias Agopianfc328812010-07-14 23:41:37 -0700349SortedVector< wp<SensorService::SensorEventConnection> >
350SensorService::getActiveConnections() const
351{
352 Mutex::Autolock _l(mLock);
353 return mActiveConnections;
354}
355
Mathias Agopianf001c922010-11-11 17:58:51 -0800356DefaultKeyedVector<int, SensorInterface*>
357SensorService::getActiveVirtualSensors() const
358{
359 Mutex::Autolock _l(mLock);
360 return mActiveVirtualSensors;
361}
362
Mathias Agopian5d270722010-07-19 15:20:39 -0700363String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700364 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700365 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700366 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700367 if (sensor.getHandle() == handle) {
368 return sensor.getName();
369 }
370 }
371 String8 result("unknown");
372 return result;
373}
374
Mathias Agopianfc328812010-07-14 23:41:37 -0700375Vector<Sensor> SensorService::getSensorList()
376{
Mathias Agopian33264862012-06-28 19:46:54 -0700377 char value[PROPERTY_VALUE_MAX];
378 property_get("debug.sensors", value, "0");
379 if (atoi(value)) {
380 return mUserSensorListDebug;
381 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700382 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700383}
384
385sp<ISensorEventConnection> SensorService::createSensorEventConnection()
386{
Mathias Agopian5307d172012-09-18 17:02:43 -0700387 uid_t uid = IPCThreadState::self()->getCallingUid();
388 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700389 return result;
390}
391
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800392void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700393{
394 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800395 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700396 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000397 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700398 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800399 int handle = mActiveSensors.keyAt(i);
400 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000401 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800402 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000403 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800404 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800405 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800406 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800407 }
408 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000409 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000410 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700411 "removing connection %p for sensor[%d].handle=0x%08x",
412 c, i, handle);
413
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800414 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000415 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700416 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800417 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700418 delete rec;
419 size--;
420 } else {
421 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700422 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700423 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700424 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700425 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700426}
427
428status_t SensorService::enable(const sp<SensorEventConnection>& connection,
429 int handle)
430{
Mathias Agopian50df2952010-07-19 19:09:10 -0700431 if (mInitCheck != NO_ERROR)
432 return mInitCheck;
433
Mathias Agopianfc328812010-07-14 23:41:37 -0700434 Mutex::Autolock _l(mLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800435 SensorInterface* sensor = mSensorMap.valueFor(handle);
436 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopianfc328812010-07-14 23:41:37 -0700437 if (err == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800438 SensorRecord* rec = mActiveSensors.valueFor(handle);
439 if (rec == 0) {
440 rec = new SensorRecord(connection);
441 mActiveSensors.add(handle, rec);
442 if (sensor->isVirtual()) {
443 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700444 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800445 } else {
446 if (rec->addConnection(connection)) {
447 // this sensor is already activated, but we are adding a
448 // connection that uses it. Immediately send down the last
Mathias Agopian3f2f8912011-03-10 15:23:28 -0800449 // known value of the requested sensor if it's not a
450 // "continuous" sensor.
451 if (sensor->getSensor().getMinDelay() == 0) {
452 sensors_event_t scratch;
453 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
454 if (event.version == sizeof(sensors_event_t)) {
455 connection->sendEvents(&event, 1);
456 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800457 }
458 }
459 }
460 if (err == NO_ERROR) {
461 // connection now active
462 if (connection->addSensor(handle)) {
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700463 BatteryService::enableSensor(connection->getUid(), handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800464 // the sensor was added (which means it wasn't already there)
465 // so, see if this connection becomes active
466 if (mActiveConnections.indexOf(connection) < 0) {
467 mActiveConnections.add(connection);
468 }
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700469 } else {
470 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
471 handle, connection.get());
Mathias Agopianf001c922010-11-11 17:58:51 -0800472 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700473 }
474 }
475 return err;
476}
477
478status_t SensorService::disable(const sp<SensorEventConnection>& connection,
479 int handle)
480{
Mathias Agopian50df2952010-07-19 19:09:10 -0700481 if (mInitCheck != NO_ERROR)
482 return mInitCheck;
483
Mathias Agopianfc328812010-07-14 23:41:37 -0700484 status_t err = NO_ERROR;
485 Mutex::Autolock _l(mLock);
486 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700487 if (rec) {
488 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700489 if (connection->removeSensor(handle)) {
490 BatteryService::disableSensor(connection->getUid(), handle);
491 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700492 if (connection->hasAnySensor() == false) {
493 mActiveConnections.remove(connection);
494 }
495 // see if this sensor becomes inactive
496 if (rec->removeConnection(connection)) {
497 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800498 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700499 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700500 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800501 SensorInterface* sensor = mSensorMap.valueFor(handle);
502 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700503 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700504 return err;
505}
506
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700507status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700508 int handle, nsecs_t ns)
509{
Mathias Agopian50df2952010-07-19 19:09:10 -0700510 if (mInitCheck != NO_ERROR)
511 return mInitCheck;
512
Mathias Agopianae09d652011-11-01 17:37:49 -0700513 SensorInterface* sensor = mSensorMap.valueFor(handle);
514 if (!sensor)
515 return BAD_VALUE;
516
Mathias Agopian1cd70002010-07-21 15:59:50 -0700517 if (ns < 0)
518 return BAD_VALUE;
519
Mathias Agopian62569ec2011-11-07 21:21:47 -0800520 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
521 if (ns < minDelayNs) {
522 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700523 }
524
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700525 if (ns < MINIMUM_EVENTS_PERIOD)
526 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian1cd70002010-07-21 15:59:50 -0700527
Mathias Agopianf001c922010-11-11 17:58:51 -0800528 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700529}
530
531// ---------------------------------------------------------------------------
532
533SensorService::SensorRecord::SensorRecord(
534 const sp<SensorEventConnection>& connection)
535{
536 mConnections.add(connection);
537}
538
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700539bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700540 const sp<SensorEventConnection>& connection)
541{
542 if (mConnections.indexOf(connection) < 0) {
543 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700544 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700545 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700546 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700547}
548
549bool SensorService::SensorRecord::removeConnection(
550 const wp<SensorEventConnection>& connection)
551{
552 ssize_t index = mConnections.indexOf(connection);
553 if (index >= 0) {
554 mConnections.removeItemsAt(index, 1);
555 }
556 return mConnections.size() ? false : true;
557}
558
559// ---------------------------------------------------------------------------
560
561SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700562 const sp<SensorService>& service, uid_t uid)
563 : mService(service), mChannel(new BitTube()), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700564{
565}
566
567SensorService::SensorEventConnection::~SensorEventConnection()
568{
Steve Blocka5512372011-12-20 16:23:08 +0000569 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700570 mService->cleanupConnection(this);
571}
572
573void SensorService::SensorEventConnection::onFirstRef()
574{
575}
576
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700577bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800578 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiana5b8e8b2012-09-18 17:18:54 -0700579 if (mSensorInfo.indexOf(handle) < 0) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800580 mSensorInfo.add(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700581 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700582 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700583 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700584}
585
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700586bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800587 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800588 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700589 return true;
590 }
591 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700592}
593
594bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800595 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianf001c922010-11-11 17:58:51 -0800596 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700597}
598
599bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800600 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700601 return mSensorInfo.size() ? true : false;
602}
603
Mathias Agopianfc328812010-07-14 23:41:37 -0700604status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700605 sensors_event_t const* buffer, size_t numEvents,
606 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700607{
Mathias Agopiancf510012010-07-22 16:18:10 -0700608 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700609 size_t count = 0;
610 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800611 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700612 size_t i=0;
613 while (i<numEvents) {
614 const int32_t curr = buffer[i].sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800615 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700616 do {
617 scratch[count++] = buffer[i++];
618 } while ((i<numEvents) && (buffer[i].sensor == curr));
619 } else {
620 i++;
621 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700622 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700623 } else {
624 scratch = const_cast<sensors_event_t *>(buffer);
625 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700626 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700627
Mathias Agopian907103b2012-04-02 18:38:02 -0700628 // NOTE: ASensorEvent and sensors_event_t are the same type
629 ssize_t size = SensorEventQueue::write(mChannel,
630 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700631 if (size == -EAGAIN) {
632 // the destination doesn't accept events anymore, it's probably
633 // full. For now, we just drop the events on the floor.
Steve Block3c20fbe2012-01-05 23:22:43 +0000634 //ALOGW("dropping %d events on the floor", count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700635 return size;
636 }
637
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700638 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700639}
640
Mathias Agopianb3989272011-10-20 18:42:02 -0700641sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700642{
643 return mChannel;
644}
645
646status_t SensorService::SensorEventConnection::enableDisable(
647 int handle, bool enabled)
648{
649 status_t err;
650 if (enabled) {
651 err = mService->enable(this, handle);
652 } else {
653 err = mService->disable(this, handle);
654 }
655 return err;
656}
657
658status_t SensorService::SensorEventConnection::setEventRate(
659 int handle, nsecs_t ns)
660{
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700661 return mService->setEventRate(this, handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700662}
663
664// ---------------------------------------------------------------------------
665}; // namespace android
666