blob: 9cc75c637c30e7f4941b1d3559e9fa9bf2bf57d7 [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
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080018#include <math.h>
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070019#include <stdint.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070020#include <sys/types.h>
21
Mathias Agopian33015422011-05-27 18:18:13 -070022#include <cutils/properties.h>
23
Mathias Agopianfc328812010-07-14 23:41:37 -070024#include <utils/SortedVector.h>
25#include <utils/KeyedVector.h>
26#include <utils/threads.h>
27#include <utils/Atomic.h>
28#include <utils/Errors.h>
29#include <utils/RefBase.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070030#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070031#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070032
33#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070034#include <binder/IServiceManager.h>
Mathias Agopian1cb13462011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070036
37#include <gui/ISensorServer.h>
38#include <gui/ISensorEventConnection.h>
Mathias Agopian907103b2012-04-02 18:38:02 -070039#include <gui/SensorEventQueue.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070040
41#include <hardware/sensors.h>
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070042#include <hardware_legacy/power.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070043
Mathias Agopian787ac1b2012-09-18 18:49:18 -070044#include "BatteryService.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070045#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080046#include "GravitySensor.h"
47#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070048#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080049#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070050#include "SensorFusion.h"
51#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070052
53namespace android {
54// ---------------------------------------------------------------------------
55
Mathias Agopian33015422011-05-27 18:18:13 -070056/*
57 * Notes:
58 *
59 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070060 * - run mag sensor from time to time to force calibration
61 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
62 *
63 */
64
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070065const char* SensorService::WAKE_LOCK_NAME = "SensorService";
66
Mathias Agopianfc328812010-07-14 23:41:37 -070067SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -070068 : mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070069{
70}
71
72void SensorService::onFirstRef()
73{
Steve Blocka5512372011-12-20 16:23:08 +000074 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070075
Mathias Agopianf001c922010-11-11 17:58:51 -080076 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070077
Mathias Agopianf001c922010-11-11 17:58:51 -080078 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080079 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070080 ssize_t count = dev.getSensorList(&list);
81 if (count > 0) {
82 ssize_t orientationIndex = -1;
83 bool hasGyro = false;
84 uint32_t virtualSensorsNeeds =
85 (1<<SENSOR_TYPE_GRAVITY) |
86 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
87 (1<<SENSOR_TYPE_ROTATION_VECTOR);
88
89 mLastEventSeen.setCapacity(count);
90 for (ssize_t i=0 ; i<count ; i++) {
91 registerSensor( new HardwareSensor(list[i]) );
92 switch (list[i].type) {
93 case SENSOR_TYPE_ORIENTATION:
94 orientationIndex = i;
95 break;
96 case SENSOR_TYPE_GYROSCOPE:
Mathias Agopian03193062013-05-10 19:32:39 -070097 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070098 hasGyro = true;
99 break;
100 case SENSOR_TYPE_GRAVITY:
101 case SENSOR_TYPE_LINEAR_ACCELERATION:
102 case SENSOR_TYPE_ROTATION_VECTOR:
103 virtualSensorsNeeds &= ~(1<<list[i].type);
104 break;
105 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700106 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700107
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700108 // it's safe to instantiate the SensorFusion object here
109 // (it wants to be instantiated after h/w sensors have been
110 // registered)
111 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700112
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700113 // build the sensor list returned to users
114 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700115
116 if (hasGyro) {
Mathias Agopian03193062013-05-10 19:32:39 -0700117 Sensor aSensor;
118
119 // Add Android virtual sensors if they're not already
120 // available in the HAL
121
122 aSensor = registerVirtualSensor( new RotationVectorSensor() );
123 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
124 mUserSensorList.add(aSensor);
125 }
126
127 aSensor = registerVirtualSensor( new GravitySensor(list, count) );
128 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
129 mUserSensorList.add(aSensor);
130 }
131
132 aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
133 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
134 mUserSensorList.add(aSensor);
135 }
136
137 aSensor = registerVirtualSensor( new OrientationSensor() );
138 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
139 // if we are doing our own rotation-vector, also add
140 // the orientation sensor and remove the HAL provided one.
141 mUserSensorList.replaceAt(aSensor, orientationIndex);
142 }
143
Mathias Agopian33264862012-06-28 19:46:54 -0700144 // virtual debugging sensors are not added to mUserSensorList
Mathias Agopian03193062013-05-10 19:32:39 -0700145 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33264862012-06-28 19:46:54 -0700146 registerVirtualSensor( new GyroDriftSensor() );
147 }
148
Mathias Agopian33264862012-06-28 19:46:54 -0700149 // debugging sensor list
Mathias Agopian03193062013-05-10 19:32:39 -0700150 mUserSensorListDebug = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700151
Aravind Akella4c8b9512013-09-05 17:03:38 -0700152 mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
153 FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r");
154 char line[128];
155 if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
156 line[sizeof(line) - 1] = '\0';
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700157 sscanf(line, "%zu", &mSocketBufferSize);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700158 if (mSocketBufferSize > MAX_SOCKET_BUFFER_SIZE_BATCHED) {
159 mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
160 }
161 }
162 ALOGD("Max socket buffer size %u", mSocketBufferSize);
163 if (fp) {
164 fclose(fp);
165 }
166
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700167 run("SensorService", PRIORITY_URGENT_DISPLAY);
168 mInitCheck = NO_ERROR;
169 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700170 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700171}
172
Mathias Agopian03193062013-05-10 19:32:39 -0700173Sensor SensorService::registerSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800174{
175 sensors_event_t event;
176 memset(&event, 0, sizeof(event));
177
178 const Sensor sensor(s->getSensor());
179 // add to the sensor list (returned to clients)
180 mSensorList.add(sensor);
181 // add to our handle->SensorInterface mapping
182 mSensorMap.add(sensor.getHandle(), s);
183 // create an entry in the mLastEventSeen array
184 mLastEventSeen.add(sensor.getHandle(), event);
Mathias Agopian03193062013-05-10 19:32:39 -0700185
186 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800187}
188
Mathias Agopian03193062013-05-10 19:32:39 -0700189Sensor SensorService::registerVirtualSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800190{
Mathias Agopian03193062013-05-10 19:32:39 -0700191 Sensor sensor = registerSensor(s);
Mathias Agopianf001c922010-11-11 17:58:51 -0800192 mVirtualSensorList.add( s );
Mathias Agopian03193062013-05-10 19:32:39 -0700193 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800194}
195
Mathias Agopianfc328812010-07-14 23:41:37 -0700196SensorService::~SensorService()
197{
Mathias Agopianf001c922010-11-11 17:58:51 -0800198 for (size_t i=0 ; i<mSensorMap.size() ; i++)
199 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700200}
201
Mathias Agopian1cb13462011-06-27 16:05:52 -0700202static const String16 sDump("android.permission.DUMP");
203
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700204status_t SensorService::dump(int fd, const Vector<String16>& /*args*/)
Mathias Agopianfc328812010-07-14 23:41:37 -0700205{
Mathias Agopianfc328812010-07-14 23:41:37 -0700206 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700207 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700208 result.appendFormat("Permission Denial: "
Mathias Agopianfc328812010-07-14 23:41:37 -0700209 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
210 IPCThreadState::self()->getCallingPid(),
211 IPCThreadState::self()->getCallingUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700212 } else {
213 Mutex::Autolock _l(mLock);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700214 result.append("Sensor List:\n");
Mathias Agopian3560fb22010-07-22 21:24:39 -0700215 for (size_t i=0 ; i<mSensorList.size() ; i++) {
216 const Sensor& s(mSensorList[i]);
217 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopianba02cd22013-07-03 16:20:57 -0700218 result.appendFormat(
219 "%-48s| %-32s | 0x%08x | ",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700220 s.getName().string(),
221 s.getVendor().string(),
Mathias Agopianba02cd22013-07-03 16:20:57 -0700222 s.getHandle());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700223
Mathias Agopianba02cd22013-07-03 16:20:57 -0700224 if (s.getMinDelay() > 0) {
225 result.appendFormat(
226 "maxRate=%7.2fHz | ", 1e6f / s.getMinDelay());
227 } else {
228 result.append(s.getMinDelay() == 0
229 ? "on-demand | "
230 : "one-shot | ");
231 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700232 if (s.getFifoMaxEventCount() > 0) {
233 result.appendFormat("getFifoMaxEventCount=%d events | ", s.getFifoMaxEventCount());
234 } else {
235 result.append("no batching support | ");
236 }
Mathias Agopianba02cd22013-07-03 16:20:57 -0700237
238 switch (s.getType()) {
239 case SENSOR_TYPE_ROTATION_VECTOR:
240 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
241 result.appendFormat(
242 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
243 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]);
244 break;
245 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
246 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
247 result.appendFormat(
248 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
249 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]);
250 break;
251 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
252 result.appendFormat(
253 "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n",
254 e.data[0], e.data[1], e.data[2], e.data[3]);
255 break;
256 case SENSOR_TYPE_SIGNIFICANT_MOTION:
257 case SENSOR_TYPE_STEP_DETECTOR:
258 result.appendFormat( "last=<%f>\n", e.data[0]);
259 break;
260 case SENSOR_TYPE_STEP_COUNTER:
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700261 result.appendFormat( "last=<%" PRIu64 ">\n", e.u64.step_counter);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700262 break;
263 default:
264 // default to 3 values
265 result.appendFormat(
266 "last=<%5.1f,%5.1f,%5.1f>\n",
267 e.data[0], e.data[1], e.data[2]);
268 break;
269 }
270 }
271 SensorFusion::getInstance().dump(result);
272 SensorDevice::getInstance().dump(result);
273
Mathias Agopianba02cd22013-07-03 16:20:57 -0700274 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700275 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700276 int handle = mActiveSensors.keyAt(i);
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700277 result.appendFormat("%s (handle=0x%08x, connections=%zu)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700278 getSensorName(handle).string(),
279 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700280 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700281 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700282
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700283 result.appendFormat("%zu Max Socket Buffer size\n", mSocketBufferSize);
284 result.appendFormat("%zd active connections\n", mActiveConnections.size());
Aravind Akella4c8b9512013-09-05 17:03:38 -0700285
286 for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
287 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
288 if (connection != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700289 result.appendFormat("Connection Number: %zu \n", i);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700290 connection->dump(result);
291 }
292 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700293 }
294 write(fd, result.string(), result.size());
295 return NO_ERROR;
296}
297
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700298void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
299 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700300 SensorInterface* sensor;
301 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700302 for (int i=0 ; i<count ; i++) {
303 int handle = buffer[i].sensor;
Mathias Agopian7438fd12013-07-08 12:50:39 -0700304 int type = buffer[i].type;
305 if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700306 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700307 sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700308 if (sensor != NULL) {
309 sensor->autoDisable(connection.get(), handle);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700310 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700311 cleanupWithoutDisable(connection, handle);
312 }
313 }
314 }
315}
316
Mathias Agopianfc328812010-07-14 23:41:37 -0700317bool SensorService::threadLoop()
318{
Steve Blocka5512372011-12-20 16:23:08 +0000319 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700320
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700321 // each virtual sensor could generate an event per "real" event, that's why we need
322 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
323 // in practice, this is too aggressive, but guaranteed to be enough.
324 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
325 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
326
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700327 sensors_event_t buffer[minBufferSize];
328 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800329 SensorDevice& device(SensorDevice::getInstance());
330 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700331
Mathias Agopianf001c922010-11-11 17:58:51 -0800332 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700333 bool wakeLockAcquired = false;
334 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700335 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800336 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700337 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000338 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700339 break;
340 }
341
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700342 // Poll has returned. Hold a wakelock.
343 // Todo(): add a flag to the sensors definitions to indicate
344 // the sensors which can wake up the AP
345 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700346 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700347 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
348 wakeLockAcquired = true;
349 break;
350 }
351 }
352
Mathias Agopian94e8f682010-11-10 17:50:28 -0800353 recordLastValue(buffer, count);
354
Mathias Agopianf001c922010-11-11 17:58:51 -0800355 // handle virtual sensors
356 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700357 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800358 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
359 getActiveVirtualSensors());
360 const size_t activeVirtualSensorCount = virtualSensors.size();
361 if (activeVirtualSensorCount) {
362 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700363 SensorFusion& fusion(SensorFusion::getInstance());
364 if (fusion.isEnabled()) {
365 for (size_t i=0 ; i<size_t(count) ; i++) {
366 fusion.process(event[i]);
367 }
368 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700369 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800370 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700371 if (count + k >= minBufferSize) {
372 ALOGE("buffer too small to hold all events: "
373 "count=%u, k=%u, size=%u",
374 count, k, minBufferSize);
375 break;
376 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800377 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700378 SensorInterface* si = virtualSensors.valueAt(j);
379 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800380 buffer[count + k] = out;
381 k++;
382 }
383 }
384 }
385 if (k) {
386 // record the last synthesized values
387 recordLastValue(&buffer[count], k);
388 count += k;
389 // sort the buffer by time-stamps
390 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700391 }
392 }
393 }
394
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700395 // handle backward compatibility for RotationVector sensor
396 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
397 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700398 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700399 // All the 4 components of the quaternion should be available
400 // No heading accuracy. Set it to -1
401 buffer[i].data[4] = -1;
402 }
403 }
404 }
405
Mathias Agopianf001c922010-11-11 17:58:51 -0800406 // send our events to clients...
407 const SortedVector< wp<SensorEventConnection> > activeConnections(
408 getActiveConnections());
409 size_t numConnections = activeConnections.size();
410 for (size_t i=0 ; i<numConnections ; i++) {
411 sp<SensorEventConnection> connection(
412 activeConnections[i].promote());
413 if (connection != 0) {
414 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700415 // Some sensors need to be auto disabled after the trigger
416 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800417 }
418 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700419
420 // We have read the data, upper layers should hold the wakelock.
421 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
Mathias Agopianfc328812010-07-14 23:41:37 -0700422 } while (count >= 0 || Thread::exitPending());
423
Steve Block3c20fbe2012-01-05 23:22:43 +0000424 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800425 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700426 return false;
427}
428
Mathias Agopian94e8f682010-11-10 17:50:28 -0800429void SensorService::recordLastValue(
430 sensors_event_t const * buffer, size_t count)
431{
432 Mutex::Autolock _l(mLock);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800433 // record the last event for each sensor
434 int32_t prev = buffer[0].sensor;
435 for (size_t i=1 ; i<count ; i++) {
436 // record the last event of each sensor type in this buffer
437 int32_t curr = buffer[i].sensor;
438 if (curr != prev) {
439 mLastEventSeen.editValueFor(prev) = buffer[i-1];
440 prev = curr;
441 }
442 }
443 mLastEventSeen.editValueFor(prev) = buffer[count-1];
444}
445
Mathias Agopianf001c922010-11-11 17:58:51 -0800446void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
447{
448 struct compar {
449 static int cmp(void const* lhs, void const* rhs) {
450 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
451 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700452 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800453 }
454 };
455 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
456}
457
Mathias Agopianfc328812010-07-14 23:41:37 -0700458SortedVector< wp<SensorService::SensorEventConnection> >
459SensorService::getActiveConnections() const
460{
461 Mutex::Autolock _l(mLock);
462 return mActiveConnections;
463}
464
Mathias Agopianf001c922010-11-11 17:58:51 -0800465DefaultKeyedVector<int, SensorInterface*>
466SensorService::getActiveVirtualSensors() const
467{
468 Mutex::Autolock _l(mLock);
469 return mActiveVirtualSensors;
470}
471
Mathias Agopian5d270722010-07-19 15:20:39 -0700472String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700473 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700474 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700475 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700476 if (sensor.getHandle() == handle) {
477 return sensor.getName();
478 }
479 }
480 String8 result("unknown");
481 return result;
482}
483
Aravind Akellab4099e72013-10-15 15:43:10 -0700484bool SensorService::isVirtualSensor(int handle) const {
485 SensorInterface* sensor = mSensorMap.valueFor(handle);
486 return sensor->isVirtual();
487}
488
Mathias Agopianfc328812010-07-14 23:41:37 -0700489Vector<Sensor> SensorService::getSensorList()
490{
Mathias Agopian33264862012-06-28 19:46:54 -0700491 char value[PROPERTY_VALUE_MAX];
492 property_get("debug.sensors", value, "0");
493 if (atoi(value)) {
494 return mUserSensorListDebug;
495 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700496 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700497}
498
499sp<ISensorEventConnection> SensorService::createSensorEventConnection()
500{
Mathias Agopian5307d172012-09-18 17:02:43 -0700501 uid_t uid = IPCThreadState::self()->getCallingUid();
502 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700503 return result;
504}
505
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800506void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700507{
508 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800509 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700510 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000511 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700512 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800513 int handle = mActiveSensors.keyAt(i);
514 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000515 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800516 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000517 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800518 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800519 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800520 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800521 }
522 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000523 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000524 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700525 "removing connection %p for sensor[%d].handle=0x%08x",
526 c, i, handle);
527
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800528 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000529 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700530 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800531 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700532 delete rec;
533 size--;
534 } else {
535 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700536 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700537 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700538 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700539 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700540}
541
542status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700543 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700544{
Mathias Agopian50df2952010-07-19 19:09:10 -0700545 if (mInitCheck != NO_ERROR)
546 return mInitCheck;
547
Mathias Agopianf001c922010-11-11 17:58:51 -0800548 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700549 if (sensor == NULL) {
550 return BAD_VALUE;
551 }
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700552 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700553 SensorRecord* rec = mActiveSensors.valueFor(handle);
554 if (rec == 0) {
555 rec = new SensorRecord(connection);
556 mActiveSensors.add(handle, rec);
557 if (sensor->isVirtual()) {
558 mActiveVirtualSensors.add(handle, sensor);
559 }
560 } else {
561 if (rec->addConnection(connection)) {
562 // this sensor is already activated, but we are adding a
563 // connection that uses it. Immediately send down the last
564 // known value of the requested sensor if it's not a
565 // "continuous" sensor.
566 if (sensor->getSensor().getMinDelay() == 0) {
567 sensors_event_t scratch;
568 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
569 if (event.version == sizeof(sensors_event_t)) {
570 connection->sendEvents(&event, 1);
571 }
572 }
573 }
574 }
575
576 if (connection->addSensor(handle)) {
577 BatteryService::enableSensor(connection->getUid(), handle);
578 // the sensor was added (which means it wasn't already there)
579 // so, see if this connection becomes active
580 if (mActiveConnections.indexOf(connection) < 0) {
581 mActiveConnections.add(connection);
582 }
583 } else {
584 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
585 handle, connection.get());
586 }
587
Aravind Akella724d91d2013-06-27 12:04:23 -0700588 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
589 if (samplingPeriodNs < minDelayNs) {
590 samplingPeriodNs = minDelayNs;
591 }
592
593 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d rate=%lld timeout== %lld",
594 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
595
596 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
597 maxBatchReportLatencyNs);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700598 if (err == NO_ERROR) {
599 connection->setFirstFlushPending(handle, true);
600 status_t err_flush = sensor->flush(connection.get(), handle);
601 // Flush may return error if the sensor is not activated or the underlying h/w sensor does
602 // not support flush.
603 if (err_flush != NO_ERROR) {
604 connection->setFirstFlushPending(handle, false);
605 }
606 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700607
608 if (err == NO_ERROR) {
609 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
610 err = sensor->activate(connection.get(), true);
611 }
612
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700613 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700614 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700615 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700616 }
617 return err;
618}
619
620status_t SensorService::disable(const sp<SensorEventConnection>& connection,
621 int handle)
622{
Mathias Agopian50df2952010-07-19 19:09:10 -0700623 if (mInitCheck != NO_ERROR)
624 return mInitCheck;
625
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700626 Mutex::Autolock _l(mLock);
627 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700628 if (err == NO_ERROR) {
629 SensorInterface* sensor = mSensorMap.valueFor(handle);
630 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
631 }
632 return err;
633}
634
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700635status_t SensorService::cleanupWithoutDisable(
636 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700637 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700638 return cleanupWithoutDisableLocked(connection, handle);
639}
640
641status_t SensorService::cleanupWithoutDisableLocked(
642 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700643 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700644 if (rec) {
645 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700646 if (connection->removeSensor(handle)) {
647 BatteryService::disableSensor(connection->getUid(), handle);
648 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700649 if (connection->hasAnySensor() == false) {
650 mActiveConnections.remove(connection);
651 }
652 // see if this sensor becomes inactive
653 if (rec->removeConnection(connection)) {
654 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800655 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700656 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700657 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700658 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700659 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700660 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700661}
662
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700663status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700664 int handle, nsecs_t ns)
665{
Mathias Agopian50df2952010-07-19 19:09:10 -0700666 if (mInitCheck != NO_ERROR)
667 return mInitCheck;
668
Mathias Agopianae09d652011-11-01 17:37:49 -0700669 SensorInterface* sensor = mSensorMap.valueFor(handle);
670 if (!sensor)
671 return BAD_VALUE;
672
Mathias Agopian1cd70002010-07-21 15:59:50 -0700673 if (ns < 0)
674 return BAD_VALUE;
675
Mathias Agopian62569ec2011-11-07 21:21:47 -0800676 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
677 if (ns < minDelayNs) {
678 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700679 }
680
Mathias Agopianf001c922010-11-11 17:58:51 -0800681 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700682}
683
Aravind Akella724d91d2013-06-27 12:04:23 -0700684status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
685 int handle) {
686 if (mInitCheck != NO_ERROR) return mInitCheck;
687 SensorInterface* sensor = mSensorMap.valueFor(handle);
Aravind Akella701166d2013-10-08 14:59:26 -0700688 if (sensor == NULL) {
689 return BAD_VALUE;
690 }
691 if (sensor->getSensor().getType() == SENSOR_TYPE_SIGNIFICANT_MOTION) {
692 ALOGE("flush called on Significant Motion sensor");
693 return INVALID_OPERATION;
Aravind Akella724d91d2013-06-27 12:04:23 -0700694 }
695 return sensor->flush(connection.get(), handle);
696}
Mathias Agopianfc328812010-07-14 23:41:37 -0700697// ---------------------------------------------------------------------------
698
699SensorService::SensorRecord::SensorRecord(
700 const sp<SensorEventConnection>& connection)
701{
702 mConnections.add(connection);
703}
704
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700705bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700706 const sp<SensorEventConnection>& connection)
707{
708 if (mConnections.indexOf(connection) < 0) {
709 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700710 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700711 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700712 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700713}
714
715bool SensorService::SensorRecord::removeConnection(
716 const wp<SensorEventConnection>& connection)
717{
718 ssize_t index = mConnections.indexOf(connection);
719 if (index >= 0) {
720 mConnections.removeItemsAt(index, 1);
721 }
722 return mConnections.size() ? false : true;
723}
724
725// ---------------------------------------------------------------------------
726
727SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700728 const sp<SensorService>& service, uid_t uid)
Aravind Akella4c8b9512013-09-05 17:03:38 -0700729 : mService(service), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700730{
Aravind Akella4c8b9512013-09-05 17:03:38 -0700731 const SensorDevice& device(SensorDevice::getInstance());
732 if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) {
733 // Increase socket buffer size to 1MB for batching capabilities.
734 mChannel = new BitTube(service->mSocketBufferSize);
735 } else {
736 mChannel = new BitTube(SOCKET_BUFFER_SIZE_NON_BATCHED);
737 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700738}
739
740SensorService::SensorEventConnection::~SensorEventConnection()
741{
Steve Blocka5512372011-12-20 16:23:08 +0000742 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700743 mService->cleanupConnection(this);
744}
745
746void SensorService::SensorEventConnection::onFirstRef()
747{
748}
749
Aravind Akella4c8b9512013-09-05 17:03:38 -0700750void SensorService::SensorEventConnection::dump(String8& result) {
751 Mutex::Autolock _l(mConnectionLock);
752 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
753 const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
754 result.appendFormat("\t %s | status: %s | pending flush events %d\n",
755 mService->getSensorName(mSensorInfo.keyAt(i)).string(),
756 flushInfo.mFirstFlushPending ? "First flush pending" :
757 "active",
758 flushInfo.mPendingFlushEventsToSend);
759 }
760}
761
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700762bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800763 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700764 if (mSensorInfo.indexOfKey(handle) < 0) {
765 mSensorInfo.add(handle, FlushInfo());
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700766 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700767 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700768 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700769}
770
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700771bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800772 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700773 if (mSensorInfo.removeItem(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700774 return true;
775 }
776 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700777}
778
779bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800780 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700781 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700782}
783
784bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800785 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700786 return mSensorInfo.size() ? true : false;
787}
788
Aravind Akella4c8b9512013-09-05 17:03:38 -0700789void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
790 bool value) {
791 Mutex::Autolock _l(mConnectionLock);
792 ssize_t index = mSensorInfo.indexOfKey(handle);
793 if (index >= 0) {
794 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
795 flushInfo.mFirstFlushPending = value;
796 }
797}
798
Mathias Agopianfc328812010-07-14 23:41:37 -0700799status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700800 sensors_event_t const* buffer, size_t numEvents,
801 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700802{
Mathias Agopiancf510012010-07-22 16:18:10 -0700803 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700804 size_t count = 0;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700805
Mathias Agopian3560fb22010-07-22 21:24:39 -0700806 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800807 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700808 size_t i=0;
809 while (i<numEvents) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700810 int32_t curr = buffer[i].sensor;
811 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
812 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
813 buffer[i].meta_data.sensor);
814 // Setting curr to the correct sensor to ensure the sensor events per connection are
815 // filtered correctly. buffer[i].sensor is zero for meta_data events.
816 curr = buffer[i].meta_data.sensor;
817 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700818 ssize_t index = mSensorInfo.indexOfKey(curr);
819 if (index >= 0 && mSensorInfo[index].mFirstFlushPending == true &&
820 buffer[i].type == SENSOR_TYPE_META_DATA) {
821 // This is the first flush before activate is called. Events can now be sent for
822 // this sensor on this connection.
823 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
824 buffer[i].meta_data.sensor);
825 mSensorInfo.editValueAt(index).mFirstFlushPending = false;
826 }
827 if (index >= 0 && mSensorInfo[index].mFirstFlushPending == false) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700828 do {
Aravind Akella4c8b9512013-09-05 17:03:38 -0700829 scratch[count++] = buffer[i++];
Aravind Akella724d91d2013-06-27 12:04:23 -0700830 } while ((i<numEvents) && ((buffer[i].sensor == curr) ||
831 (buffer[i].type == SENSOR_TYPE_META_DATA &&
832 buffer[i].meta_data.sensor == curr)));
Mathias Agopian3560fb22010-07-22 21:24:39 -0700833 } else {
834 i++;
835 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700836 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700837 } else {
838 scratch = const_cast<sensors_event_t *>(buffer);
839 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700840 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700841
Aravind Akella4c8b9512013-09-05 17:03:38 -0700842 // Send pending flush events (if any) before sending events from the cache.
843 {
844 ASensorEvent flushCompleteEvent;
845 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
846 flushCompleteEvent.sensor = 0;
847 Mutex::Autolock _l(mConnectionLock);
848 // Loop through all the sensors for this connection and check if there are any pending
849 // flush complete events to be sent.
850 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
851 FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
852 while (flushInfo.mPendingFlushEventsToSend > 0) {
853 flushCompleteEvent.meta_data.sensor = mSensorInfo.keyAt(i);
854 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
855 if (size < 0) {
856 // ALOGW("dropping %d events on the floor", count);
Aravind Akellac551eac2013-10-14 17:04:42 -0700857 countFlushCompleteEventsLocked(scratch, count);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700858 return size;
859 }
860 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
861 flushCompleteEvent.meta_data.sensor);
862 flushInfo.mPendingFlushEventsToSend--;
863 }
864 }
865 }
866
Aravind Akellab4099e72013-10-15 15:43:10 -0700867 // Early return if there are no events for this connection.
868 if (count == 0) {
869 return status_t(NO_ERROR);
870 }
871
Mathias Agopian907103b2012-04-02 18:38:02 -0700872 // NOTE: ASensorEvent and sensors_event_t are the same type
873 ssize_t size = SensorEventQueue::write(mChannel,
874 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700875 if (size == -EAGAIN) {
876 // the destination doesn't accept events anymore, it's probably
877 // full. For now, we just drop the events on the floor.
Aravind Akella4c8b9512013-09-05 17:03:38 -0700878 // ALOGW("dropping %d events on the floor", count);
Aravind Akellac551eac2013-10-14 17:04:42 -0700879 Mutex::Autolock _l(mConnectionLock);
880 countFlushCompleteEventsLocked(scratch, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700881 return size;
882 }
883
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700884 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700885}
886
Aravind Akellac551eac2013-10-14 17:04:42 -0700887void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
Aravind Akella4c8b9512013-09-05 17:03:38 -0700888 sensors_event_t* scratch, const int numEventsDropped) {
889 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700890 // Count flushComplete events in the events that are about to the dropped. These will be sent
891 // separately before the next batch of events.
892 for (int j = 0; j < numEventsDropped; ++j) {
893 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
894 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
895 flushInfo.mPendingFlushEventsToSend++;
896 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
897 flushInfo.mPendingFlushEventsToSend);
898 }
899 }
900 return;
901}
902
Mathias Agopianb3989272011-10-20 18:42:02 -0700903sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700904{
905 return mChannel;
906}
907
908status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -0700909 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
910 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700911{
912 status_t err;
913 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700914 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
915 reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700916 } else {
917 err = mService->disable(this, handle);
918 }
919 return err;
920}
921
922status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -0700923 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -0700924{
Aravind Akella724d91d2013-06-27 12:04:23 -0700925 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -0700926}
927
Aravind Akella701166d2013-10-08 14:59:26 -0700928status_t SensorService::SensorEventConnection::flush() {
Aravind Akellac551eac2013-10-14 17:04:42 -0700929 SensorDevice& dev(SensorDevice::getInstance());
930 const int halVersion = dev.getHalDeviceVersion();
Aravind Akella701166d2013-10-08 14:59:26 -0700931 Mutex::Autolock _l(mConnectionLock);
932 status_t err(NO_ERROR);
Aravind Akellac551eac2013-10-14 17:04:42 -0700933 // Loop through all sensors for this connection and call flush on each of them.
Aravind Akella701166d2013-10-08 14:59:26 -0700934 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
935 const int handle = mSensorInfo.keyAt(i);
Aravind Akellab4099e72013-10-15 15:43:10 -0700936 if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 || mService->isVirtualSensor(handle)) {
Aravind Akellac551eac2013-10-14 17:04:42 -0700937 // For older devices just increment pending flush count which will send a trivial
938 // flush complete event.
939 FlushInfo& flushInfo = mSensorInfo.editValueFor(handle);
940 flushInfo.mPendingFlushEventsToSend++;
941 } else {
942 status_t err_flush = mService->flushSensor(this, handle);
943 if (err_flush != NO_ERROR) {
944 ALOGE("Flush error handle=%d %s", handle, strerror(-err_flush));
945 }
946 err = (err_flush != NO_ERROR) ? err_flush : err;
Aravind Akella701166d2013-10-08 14:59:26 -0700947 }
Aravind Akella701166d2013-10-08 14:59:26 -0700948 }
949 return err;
Aravind Akella724d91d2013-06-27 12:04:23 -0700950}
Aravind Akella4c8b9512013-09-05 17:03:38 -0700951
Mathias Agopianfc328812010-07-14 23:41:37 -0700952// ---------------------------------------------------------------------------
953}; // namespace android
954