blob: 3e4a2f59efec573e873e6f562670fcca1938f5fc [file] [log] [blame]
Mathias Agopian1bf79782010-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 Agopian671a6ff2010-11-11 17:58:51 -080018#include <math.h>
Mathias Agopian1bf79782010-07-14 23:41:37 -070019#include <sys/types.h>
20
Mathias Agopian6043e532011-05-27 18:18:13 -070021#include <cutils/properties.h>
22
Mathias Agopian1bf79782010-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 Agopian6f8b4d22010-07-19 15:03:55 -070029#include <utils/Singleton.h>
Mathias Agopian32123fde2010-07-22 22:19:43 -070030#include <utils/String16.h>
Mathias Agopian1bf79782010-07-14 23:41:37 -070031
32#include <binder/BinderService.h>
Mathias Agopian6f8b4d22010-07-19 15:03:55 -070033#include <binder/IServiceManager.h>
Mathias Agopian0dd593f2011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian1bf79782010-07-14 23:41:37 -070035
36#include <gui/ISensorServer.h>
37#include <gui/ISensorEventConnection.h>
38
39#include <hardware/sensors.h>
40
Mathias Agopian73e0bc82011-05-17 22:54:42 -070041#include "CorrectedGyroSensor.h"
Mathias Agopian671a6ff2010-11-11 17:58:51 -080042#include "GravitySensor.h"
43#include "LinearAccelerationSensor.h"
Mathias Agopian73e0bc82011-05-17 22:54:42 -070044#include "OrientationSensor.h"
Mathias Agopian671a6ff2010-11-11 17:58:51 -080045#include "RotationVectorSensor.h"
Mathias Agopian73e0bc82011-05-17 22:54:42 -070046#include "SensorFusion.h"
47#include "SensorService.h"
Mathias Agopian1bf79782010-07-14 23:41:37 -070048
49namespace android {
50// ---------------------------------------------------------------------------
51
Mathias Agopian6043e532011-05-27 18:18:13 -070052/*
53 * Notes:
54 *
55 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian6043e532011-05-27 18:18:13 -070056 * - run mag sensor from time to time to force calibration
57 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
58 *
59 */
60
Mathias Agopian1bf79782010-07-14 23:41:37 -070061SensorService::SensorService()
Mathias Agopian0dd593f2011-06-27 16:05:52 -070062 : mInitCheck(NO_INIT)
Mathias Agopian1bf79782010-07-14 23:41:37 -070063{
64}
65
66void SensorService::onFirstRef()
67{
Steve Block5baa3a62011-12-20 16:23:08 +000068 ALOGD("nuSensorService starting...");
Mathias Agopian5df13ef2010-07-19 19:09:10 -070069
Mathias Agopian671a6ff2010-11-11 17:58:51 -080070 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopian1bf79782010-07-14 23:41:37 -070071
Mathias Agopian671a6ff2010-11-11 17:58:51 -080072 if (dev.initCheck() == NO_ERROR) {
Mathias Agopian671a6ff2010-11-11 17:58:51 -080073 sensor_t const* list;
Mathias Agopian29c176f2011-07-14 16:39:46 -070074 ssize_t count = dev.getSensorList(&list);
75 if (count > 0) {
76 ssize_t orientationIndex = -1;
77 bool hasGyro = false;
78 uint32_t virtualSensorsNeeds =
79 (1<<SENSOR_TYPE_GRAVITY) |
80 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
81 (1<<SENSOR_TYPE_ROTATION_VECTOR);
82
83 mLastEventSeen.setCapacity(count);
84 for (ssize_t i=0 ; i<count ; i++) {
85 registerSensor( new HardwareSensor(list[i]) );
86 switch (list[i].type) {
87 case SENSOR_TYPE_ORIENTATION:
88 orientationIndex = i;
89 break;
90 case SENSOR_TYPE_GYROSCOPE:
91 hasGyro = true;
92 break;
93 case SENSOR_TYPE_GRAVITY:
94 case SENSOR_TYPE_LINEAR_ACCELERATION:
95 case SENSOR_TYPE_ROTATION_VECTOR:
96 virtualSensorsNeeds &= ~(1<<list[i].type);
97 break;
98 }
Mathias Agopian5df13ef2010-07-19 19:09:10 -070099 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700100
Mathias Agopian29c176f2011-07-14 16:39:46 -0700101 // it's safe to instantiate the SensorFusion object here
102 // (it wants to be instantiated after h/w sensors have been
103 // registered)
104 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700105
Mathias Agopian29c176f2011-07-14 16:39:46 -0700106 if (hasGyro) {
107 // Always instantiate Android's virtual sensors. Since they are
108 // instantiated behind sensors from the HAL, they won't
109 // interfere with applications, unless they looks specifically
110 // for them (by name).
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700111
Mathias Agopian29c176f2011-07-14 16:39:46 -0700112 registerVirtualSensor( new RotationVectorSensor() );
113 registerVirtualSensor( new GravitySensor(list, count) );
114 registerVirtualSensor( new LinearAccelerationSensor(list, count) );
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700115
Mathias Agopian29c176f2011-07-14 16:39:46 -0700116 // these are optional
117 registerVirtualSensor( new OrientationSensor() );
118 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian6043e532011-05-27 18:18:13 -0700119
Mathias Agopian29c176f2011-07-14 16:39:46 -0700120 // virtual debugging sensors...
121 char value[PROPERTY_VALUE_MAX];
122 property_get("debug.sensors", value, "0");
123 if (atoi(value)) {
124 registerVirtualSensor( new GyroDriftSensor() );
125 }
Mathias Agopian6043e532011-05-27 18:18:13 -0700126 }
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800127
Mathias Agopian29c176f2011-07-14 16:39:46 -0700128 // build the sensor list returned to users
129 mUserSensorList = mSensorList;
130 if (hasGyro &&
131 (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) {
132 // if we have the fancy sensor fusion, and it's not provided by the
133 // HAL, use our own (fused) orientation sensor by removing the
134 // HAL supplied one form the user list.
135 if (orientationIndex >= 0) {
136 mUserSensorList.removeItemsAt(orientationIndex);
137 }
Mathias Agopianf8a67372011-06-08 20:06:50 -0700138 }
Mathias Agopianf8a67372011-06-08 20:06:50 -0700139
Mathias Agopian29c176f2011-07-14 16:39:46 -0700140 run("SensorService", PRIORITY_URGENT_DISPLAY);
141 mInitCheck = NO_ERROR;
142 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700143 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700144}
145
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800146void SensorService::registerSensor(SensorInterface* s)
147{
148 sensors_event_t event;
149 memset(&event, 0, sizeof(event));
150
151 const Sensor sensor(s->getSensor());
152 // add to the sensor list (returned to clients)
153 mSensorList.add(sensor);
154 // add to our handle->SensorInterface mapping
155 mSensorMap.add(sensor.getHandle(), s);
156 // create an entry in the mLastEventSeen array
157 mLastEventSeen.add(sensor.getHandle(), event);
158}
159
160void SensorService::registerVirtualSensor(SensorInterface* s)
161{
162 registerSensor(s);
163 mVirtualSensorList.add( s );
164}
165
Mathias Agopian1bf79782010-07-14 23:41:37 -0700166SensorService::~SensorService()
167{
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800168 for (size_t i=0 ; i<mSensorMap.size() ; i++)
169 delete mSensorMap.valueAt(i);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700170}
171
Mathias Agopian0dd593f2011-06-27 16:05:52 -0700172static const String16 sDump("android.permission.DUMP");
173
Mathias Agopian1bf79782010-07-14 23:41:37 -0700174status_t SensorService::dump(int fd, const Vector<String16>& args)
175{
176 const size_t SIZE = 1024;
177 char buffer[SIZE];
178 String8 result;
Mathias Agopian0dd593f2011-06-27 16:05:52 -0700179 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopian1bf79782010-07-14 23:41:37 -0700180 snprintf(buffer, SIZE, "Permission Denial: "
181 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
182 IPCThreadState::self()->getCallingPid(),
183 IPCThreadState::self()->getCallingUid());
184 result.append(buffer);
185 } else {
186 Mutex::Autolock _l(mLock);
Mathias Agopianaea786f2010-07-22 21:24:39 -0700187 snprintf(buffer, SIZE, "Sensor List:\n");
188 result.append(buffer);
189 for (size_t i=0 ; i<mSensorList.size() ; i++) {
190 const Sensor& s(mSensorList[i]);
191 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700192 snprintf(buffer, SIZE,
193 "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | "
194 "last=<%5.1f,%5.1f,%5.1f>\n",
Mathias Agopianaea786f2010-07-22 21:24:39 -0700195 s.getName().string(),
196 s.getVendor().string(),
197 s.getHandle(),
Mathias Agopian94c4f5c2010-11-05 19:12:58 -0700198 s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f,
Mathias Agopianaea786f2010-07-22 21:24:39 -0700199 e.data[0], e.data[1], e.data[2]);
200 result.append(buffer);
201 }
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700202 SensorFusion::getInstance().dump(result, buffer, SIZE);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800203 SensorDevice::getInstance().dump(result, buffer, SIZE);
Mathias Agopianaea786f2010-07-22 21:24:39 -0700204
Mathias Agopiand8653512010-07-21 15:59:50 -0700205 snprintf(buffer, SIZE, "%d active connections\n",
206 mActiveConnections.size());
Mathias Agopian1bf79782010-07-14 23:41:37 -0700207 result.append(buffer);
208 snprintf(buffer, SIZE, "Active sensors:\n");
209 result.append(buffer);
210 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian42e1b392010-07-19 15:20:39 -0700211 int handle = mActiveSensors.keyAt(i);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800212 snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian42e1b392010-07-19 15:20:39 -0700213 getSensorName(handle).string(),
214 handle,
Mathias Agopian1bf79782010-07-14 23:41:37 -0700215 mActiveSensors.valueAt(i)->getNumConnections());
216 result.append(buffer);
217 }
218 }
219 write(fd, result.string(), result.size());
220 return NO_ERROR;
221}
222
223bool SensorService::threadLoop()
224{
Steve Block5baa3a62011-12-20 16:23:08 +0000225 ALOGD("nuSensorService thread starting...");
Mathias Agopian1bf79782010-07-14 23:41:37 -0700226
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800227 const size_t numEventMax = 16 * (1 + mVirtualSensorList.size());
228 sensors_event_t buffer[numEventMax];
229 sensors_event_t scratch[numEventMax];
230 SensorDevice& device(SensorDevice::getInstance());
231 const size_t vcount = mVirtualSensorList.size();
Mathias Agopian1bf79782010-07-14 23:41:37 -0700232
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800233 ssize_t count;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700234 do {
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800235 count = device.poll(buffer, numEventMax);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700236 if (count<0) {
237 LOGE("sensor poll failed (%s)", strerror(-count));
238 break;
239 }
240
Mathias Agopian3a9223e2010-11-10 17:50:28 -0800241 recordLastValue(buffer, count);
242
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800243 // handle virtual sensors
244 if (count && vcount) {
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700245 sensors_event_t const * const event = buffer;
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800246 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
247 getActiveVirtualSensors());
248 const size_t activeVirtualSensorCount = virtualSensors.size();
249 if (activeVirtualSensorCount) {
250 size_t k = 0;
Mathias Agopian73e0bc82011-05-17 22:54:42 -0700251 SensorFusion& fusion(SensorFusion::getInstance());
252 if (fusion.isEnabled()) {
253 for (size_t i=0 ; i<size_t(count) ; i++) {
254 fusion.process(event[i]);
255 }
256 }
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800257 for (size_t i=0 ; i<size_t(count) ; i++) {
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800258 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
259 sensors_event_t out;
260 if (virtualSensors.valueAt(j)->process(&out, event[i])) {
261 buffer[count + k] = out;
262 k++;
263 }
264 }
265 }
266 if (k) {
267 // record the last synthesized values
268 recordLastValue(&buffer[count], k);
269 count += k;
270 // sort the buffer by time-stamps
271 sortEventBuffer(buffer, count);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700272 }
273 }
274 }
275
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800276 // send our events to clients...
277 const SortedVector< wp<SensorEventConnection> > activeConnections(
278 getActiveConnections());
279 size_t numConnections = activeConnections.size();
280 for (size_t i=0 ; i<numConnections ; i++) {
281 sp<SensorEventConnection> connection(
282 activeConnections[i].promote());
283 if (connection != 0) {
284 connection->sendEvents(buffer, count, scratch);
285 }
286 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700287 } while (count >= 0 || Thread::exitPending());
288
Mathias Agopian37d95f62011-11-09 17:50:15 -0800289 LOGW("Exiting SensorService::threadLoop => aborting...");
290 abort();
Mathias Agopian1bf79782010-07-14 23:41:37 -0700291 return false;
292}
293
Mathias Agopian3a9223e2010-11-10 17:50:28 -0800294void SensorService::recordLastValue(
295 sensors_event_t const * buffer, size_t count)
296{
297 Mutex::Autolock _l(mLock);
298
299 // record the last event for each sensor
300 int32_t prev = buffer[0].sensor;
301 for (size_t i=1 ; i<count ; i++) {
302 // record the last event of each sensor type in this buffer
303 int32_t curr = buffer[i].sensor;
304 if (curr != prev) {
305 mLastEventSeen.editValueFor(prev) = buffer[i-1];
306 prev = curr;
307 }
308 }
309 mLastEventSeen.editValueFor(prev) = buffer[count-1];
310}
311
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800312void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
313{
314 struct compar {
315 static int cmp(void const* lhs, void const* rhs) {
316 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
317 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
318 return r->timestamp - l->timestamp;
319 }
320 };
321 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
322}
323
Mathias Agopian1bf79782010-07-14 23:41:37 -0700324SortedVector< wp<SensorService::SensorEventConnection> >
325SensorService::getActiveConnections() const
326{
327 Mutex::Autolock _l(mLock);
328 return mActiveConnections;
329}
330
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800331DefaultKeyedVector<int, SensorInterface*>
332SensorService::getActiveVirtualSensors() const
333{
334 Mutex::Autolock _l(mLock);
335 return mActiveVirtualSensors;
336}
337
Mathias Agopian42e1b392010-07-19 15:20:39 -0700338String8 SensorService::getSensorName(int handle) const {
Mathias Agopianf8a67372011-06-08 20:06:50 -0700339 size_t count = mUserSensorList.size();
Mathias Agopian42e1b392010-07-19 15:20:39 -0700340 for (size_t i=0 ; i<count ; i++) {
Mathias Agopianf8a67372011-06-08 20:06:50 -0700341 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian42e1b392010-07-19 15:20:39 -0700342 if (sensor.getHandle() == handle) {
343 return sensor.getName();
344 }
345 }
346 String8 result("unknown");
347 return result;
348}
349
Mathias Agopian1bf79782010-07-14 23:41:37 -0700350Vector<Sensor> SensorService::getSensorList()
351{
Mathias Agopianf8a67372011-06-08 20:06:50 -0700352 return mUserSensorList;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700353}
354
355sp<ISensorEventConnection> SensorService::createSensorEventConnection()
356{
357 sp<SensorEventConnection> result(new SensorEventConnection(this));
Mathias Agopian1bf79782010-07-14 23:41:37 -0700358 return result;
359}
360
Mathias Agopianf0aec212011-02-03 14:52:47 -0800361void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopian1bf79782010-07-14 23:41:37 -0700362{
363 Mutex::Autolock _l(mLock);
Mathias Agopianf0aec212011-02-03 14:52:47 -0800364 const wp<SensorEventConnection> connection(c);
Mathias Agopiand8653512010-07-21 15:59:50 -0700365 size_t size = mActiveSensors.size();
Steve Block5baa3a62011-12-20 16:23:08 +0000366 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopiand8653512010-07-21 15:59:50 -0700367 for (size_t i=0 ; i<size ; ) {
Mathias Agopianf0aec212011-02-03 14:52:47 -0800368 int handle = mActiveSensors.keyAt(i);
369 if (c->hasSensor(handle)) {
Steve Block5baa3a62011-12-20 16:23:08 +0000370 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800371 SensorInterface* sensor = mSensorMap.valueFor( handle );
Mathias Agopian9ff73de2011-05-27 16:23:58 -0700372 LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800373 if (sensor) {
Mathias Agopianf0aec212011-02-03 14:52:47 -0800374 sensor->activate(c, false);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800375 }
Mathias Agopianf0aec212011-02-03 14:52:47 -0800376 }
377 SensorRecord* rec = mActiveSensors.valueAt(i);
Mathias Agopian9ff73de2011-05-27 16:23:58 -0700378 LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Block5baa3a62011-12-20 16:23:08 +0000379 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopian9ff73de2011-05-27 16:23:58 -0700380 "removing connection %p for sensor[%d].handle=0x%08x",
381 c, i, handle);
382
Mathias Agopianf0aec212011-02-03 14:52:47 -0800383 if (rec && rec->removeConnection(connection)) {
Steve Block5baa3a62011-12-20 16:23:08 +0000384 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopiand8653512010-07-21 15:59:50 -0700385 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800386 mActiveVirtualSensors.removeItem(handle);
Mathias Agopiand8653512010-07-21 15:59:50 -0700387 delete rec;
388 size--;
389 } else {
390 i++;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700391 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700392 }
Mathias Agopiand8653512010-07-21 15:59:50 -0700393 mActiveConnections.remove(connection);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700394}
395
396status_t SensorService::enable(const sp<SensorEventConnection>& connection,
397 int handle)
398{
Mathias Agopian5df13ef2010-07-19 19:09:10 -0700399 if (mInitCheck != NO_ERROR)
400 return mInitCheck;
401
Mathias Agopian1bf79782010-07-14 23:41:37 -0700402 Mutex::Autolock _l(mLock);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800403 SensorInterface* sensor = mSensorMap.valueFor(handle);
404 status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700405 if (err == NO_ERROR) {
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800406 SensorRecord* rec = mActiveSensors.valueFor(handle);
407 if (rec == 0) {
408 rec = new SensorRecord(connection);
409 mActiveSensors.add(handle, rec);
410 if (sensor->isVirtual()) {
411 mActiveVirtualSensors.add(handle, sensor);
Mathias Agopiand8653512010-07-21 15:59:50 -0700412 }
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800413 } else {
414 if (rec->addConnection(connection)) {
415 // this sensor is already activated, but we are adding a
416 // connection that uses it. Immediately send down the last
Mathias Agopian8d94d822011-03-10 15:23:28 -0800417 // known value of the requested sensor if it's not a
418 // "continuous" sensor.
419 if (sensor->getSensor().getMinDelay() == 0) {
420 sensors_event_t scratch;
421 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
422 if (event.version == sizeof(sensors_event_t)) {
423 connection->sendEvents(&event, 1);
424 }
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800425 }
426 }
427 }
428 if (err == NO_ERROR) {
429 // connection now active
430 if (connection->addSensor(handle)) {
431 // the sensor was added (which means it wasn't already there)
432 // so, see if this connection becomes active
433 if (mActiveConnections.indexOf(connection) < 0) {
434 mActiveConnections.add(connection);
435 }
436 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700437 }
438 }
439 return err;
440}
441
442status_t SensorService::disable(const sp<SensorEventConnection>& connection,
443 int handle)
444{
Mathias Agopian5df13ef2010-07-19 19:09:10 -0700445 if (mInitCheck != NO_ERROR)
446 return mInitCheck;
447
Mathias Agopian1bf79782010-07-14 23:41:37 -0700448 status_t err = NO_ERROR;
449 Mutex::Autolock _l(mLock);
450 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700451 if (rec) {
452 // see if this connection becomes inactive
453 connection->removeSensor(handle);
454 if (connection->hasAnySensor() == false) {
455 mActiveConnections.remove(connection);
456 }
457 // see if this sensor becomes inactive
458 if (rec->removeConnection(connection)) {
459 mActiveSensors.removeItem(handle);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800460 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700461 delete rec;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700462 }
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800463 SensorInterface* sensor = mSensorMap.valueFor(handle);
464 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Mathias Agopiand8653512010-07-21 15:59:50 -0700465 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700466 return err;
467}
468
Mathias Agopiand8653512010-07-21 15:59:50 -0700469status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopian1bf79782010-07-14 23:41:37 -0700470 int handle, nsecs_t ns)
471{
Mathias Agopian5df13ef2010-07-19 19:09:10 -0700472 if (mInitCheck != NO_ERROR)
473 return mInitCheck;
474
Mathias Agopianf61acda2011-11-01 17:37:49 -0700475 SensorInterface* sensor = mSensorMap.valueFor(handle);
476 if (!sensor)
477 return BAD_VALUE;
478
Mathias Agopian23e8de22010-07-21 15:59:50 -0700479 if (ns < 0)
480 return BAD_VALUE;
481
Mathias Agopianeed23732011-11-07 21:21:47 -0800482 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
483 if (ns < minDelayNs) {
484 ns = minDelayNs;
Mathias Agopianf61acda2011-11-01 17:37:49 -0700485 }
486
Mathias Agopiand8653512010-07-21 15:59:50 -0700487 if (ns < MINIMUM_EVENTS_PERIOD)
488 ns = MINIMUM_EVENTS_PERIOD;
Mathias Agopian23e8de22010-07-21 15:59:50 -0700489
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800490 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700491}
492
493// ---------------------------------------------------------------------------
494
495SensorService::SensorRecord::SensorRecord(
496 const sp<SensorEventConnection>& connection)
497{
498 mConnections.add(connection);
499}
500
Mathias Agopiand8653512010-07-21 15:59:50 -0700501bool SensorService::SensorRecord::addConnection(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700502 const sp<SensorEventConnection>& connection)
503{
504 if (mConnections.indexOf(connection) < 0) {
505 mConnections.add(connection);
Mathias Agopiand8653512010-07-21 15:59:50 -0700506 return true;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700507 }
Mathias Agopiand8653512010-07-21 15:59:50 -0700508 return false;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700509}
510
511bool SensorService::SensorRecord::removeConnection(
512 const wp<SensorEventConnection>& connection)
513{
514 ssize_t index = mConnections.indexOf(connection);
515 if (index >= 0) {
516 mConnections.removeItemsAt(index, 1);
517 }
518 return mConnections.size() ? false : true;
519}
520
521// ---------------------------------------------------------------------------
522
523SensorService::SensorEventConnection::SensorEventConnection(
524 const sp<SensorService>& service)
Mathias Agopian12cdf512011-10-20 18:42:02 -0700525 : mService(service), mChannel(new BitTube())
Mathias Agopian1bf79782010-07-14 23:41:37 -0700526{
527}
528
529SensorService::SensorEventConnection::~SensorEventConnection()
530{
Steve Block5baa3a62011-12-20 16:23:08 +0000531 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700532 mService->cleanupConnection(this);
533}
534
535void SensorService::SensorEventConnection::onFirstRef()
536{
537}
538
Mathias Agopiand8653512010-07-21 15:59:50 -0700539bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopianf33a6e92010-11-14 20:55:25 -0800540 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800541 if (mSensorInfo.indexOf(handle) <= 0) {
542 mSensorInfo.add(handle);
Mathias Agopiand8653512010-07-21 15:59:50 -0700543 return true;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700544 }
Mathias Agopiand8653512010-07-21 15:59:50 -0700545 return false;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700546}
547
Mathias Agopiand8653512010-07-21 15:59:50 -0700548bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopianf33a6e92010-11-14 20:55:25 -0800549 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800550 if (mSensorInfo.remove(handle) >= 0) {
Mathias Agopiand8653512010-07-21 15:59:50 -0700551 return true;
552 }
553 return false;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700554}
555
556bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopianf33a6e92010-11-14 20:55:25 -0800557 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800558 return mSensorInfo.indexOf(handle) >= 0;
Mathias Agopian1bf79782010-07-14 23:41:37 -0700559}
560
561bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopianf33a6e92010-11-14 20:55:25 -0800562 Mutex::Autolock _l(mConnectionLock);
Mathias Agopiand8653512010-07-21 15:59:50 -0700563 return mSensorInfo.size() ? true : false;
564}
565
Mathias Agopian1bf79782010-07-14 23:41:37 -0700566status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopianefba8bf2010-07-22 16:18:10 -0700567 sensors_event_t const* buffer, size_t numEvents,
568 sensors_event_t* scratch)
Mathias Agopian1bf79782010-07-14 23:41:37 -0700569{
Mathias Agopianefba8bf2010-07-22 16:18:10 -0700570 // filter out events not for this connection
Mathias Agopianaea786f2010-07-22 21:24:39 -0700571 size_t count = 0;
572 if (scratch) {
Mathias Agopianf33a6e92010-11-14 20:55:25 -0800573 Mutex::Autolock _l(mConnectionLock);
Mathias Agopianaea786f2010-07-22 21:24:39 -0700574 size_t i=0;
575 while (i<numEvents) {
576 const int32_t curr = buffer[i].sensor;
Mathias Agopian671a6ff2010-11-11 17:58:51 -0800577 if (mSensorInfo.indexOf(curr) >= 0) {
Mathias Agopianaea786f2010-07-22 21:24:39 -0700578 do {
579 scratch[count++] = buffer[i++];
580 } while ((i<numEvents) && (buffer[i].sensor == curr));
581 } else {
582 i++;
583 }
Mathias Agopianefba8bf2010-07-22 16:18:10 -0700584 }
Mathias Agopianaea786f2010-07-22 21:24:39 -0700585 } else {
586 scratch = const_cast<sensors_event_t *>(buffer);
587 count = numEvents;
Mathias Agopianefba8bf2010-07-22 16:18:10 -0700588 }
Mathias Agopian1bf79782010-07-14 23:41:37 -0700589
Mathias Agopianaea786f2010-07-22 21:24:39 -0700590 if (count == 0)
591 return 0;
592
Mathias Agopianefba8bf2010-07-22 16:18:10 -0700593 ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t));
Mathias Agopian1bf79782010-07-14 23:41:37 -0700594 if (size == -EAGAIN) {
595 // the destination doesn't accept events anymore, it's probably
596 // full. For now, we just drop the events on the floor.
Mathias Agopianf9a4b132011-07-20 18:51:15 -0700597 //LOGW("dropping %d events on the floor", count);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700598 return size;
599 }
600
Mathias Agopianf9a4b132011-07-20 18:51:15 -0700601 //LOGE_IF(size<0, "dropping %d events on the floor (%s)",
602 // count, strerror(-size));
Mathias Agopian1bf79782010-07-14 23:41:37 -0700603
Jeff Brown4fe6c3e2010-09-13 23:17:30 -0700604 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700605}
606
Mathias Agopian12cdf512011-10-20 18:42:02 -0700607sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopian1bf79782010-07-14 23:41:37 -0700608{
609 return mChannel;
610}
611
612status_t SensorService::SensorEventConnection::enableDisable(
613 int handle, bool enabled)
614{
615 status_t err;
616 if (enabled) {
617 err = mService->enable(this, handle);
618 } else {
619 err = mService->disable(this, handle);
620 }
621 return err;
622}
623
624status_t SensorService::SensorEventConnection::setEventRate(
625 int handle, nsecs_t ns)
626{
Mathias Agopiand8653512010-07-21 15:59:50 -0700627 return mService->setEventRate(this, handle, ns);
Mathias Agopian1bf79782010-07-14 23:41:37 -0700628}
629
630// ---------------------------------------------------------------------------
631}; // namespace android
632