blob: 555d843549f3a4f504003671c97704b8fb5feacd [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdint.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080018#include <math.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070019#include <sys/types.h>
20
Mathias Agopian33015422011-05-27 18:18:13 -070021#include <cutils/properties.h>
22
Mathias Agopianfc328812010-07-14 23:41:37 -070023#include <utils/SortedVector.h>
24#include <utils/KeyedVector.h>
25#include <utils/threads.h>
26#include <utils/Atomic.h>
27#include <utils/Errors.h>
28#include <utils/RefBase.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070029#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070030#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070031
32#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070033#include <binder/IServiceManager.h>
Mathias Agopian1cb13462011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070035
36#include <gui/ISensorServer.h>
37#include <gui/ISensorEventConnection.h>
Mathias Agopian907103b2012-04-02 18:38:02 -070038#include <gui/SensorEventQueue.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070039
40#include <hardware/sensors.h>
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070041#include <hardware_legacy/power.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070042
Mathias Agopian787ac1b2012-09-18 18:49:18 -070043#include "BatteryService.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070044#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080045#include "GravitySensor.h"
46#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070047#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080048#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070049#include "SensorFusion.h"
50#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070051
52namespace android {
53// ---------------------------------------------------------------------------
54
Mathias Agopian33015422011-05-27 18:18:13 -070055/*
56 * Notes:
57 *
58 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070059 * - run mag sensor from time to time to force calibration
60 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
61 *
62 */
63
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070064const char* SensorService::WAKE_LOCK_NAME = "SensorService";
65
Mathias Agopianfc328812010-07-14 23:41:37 -070066SensorService::SensorService()
Mathias Agopian1cb13462011-06-27 16:05:52 -070067 : mInitCheck(NO_INIT)
Mathias Agopianfc328812010-07-14 23:41:37 -070068{
69}
70
71void SensorService::onFirstRef()
72{
Steve Blocka5512372011-12-20 16:23:08 +000073 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070074
Mathias Agopianf001c922010-11-11 17:58:51 -080075 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070076
Mathias Agopianf001c922010-11-11 17:58:51 -080077 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080078 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070079 ssize_t count = dev.getSensorList(&list);
80 if (count > 0) {
81 ssize_t orientationIndex = -1;
82 bool hasGyro = false;
83 uint32_t virtualSensorsNeeds =
84 (1<<SENSOR_TYPE_GRAVITY) |
85 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
86 (1<<SENSOR_TYPE_ROTATION_VECTOR);
87
88 mLastEventSeen.setCapacity(count);
89 for (ssize_t i=0 ; i<count ; i++) {
90 registerSensor( new HardwareSensor(list[i]) );
91 switch (list[i].type) {
92 case SENSOR_TYPE_ORIENTATION:
93 orientationIndex = i;
94 break;
95 case SENSOR_TYPE_GYROSCOPE:
Mathias Agopian03193062013-05-10 19:32:39 -070096 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070097 hasGyro = true;
98 break;
99 case SENSOR_TYPE_GRAVITY:
100 case SENSOR_TYPE_LINEAR_ACCELERATION:
101 case SENSOR_TYPE_ROTATION_VECTOR:
102 virtualSensorsNeeds &= ~(1<<list[i].type);
103 break;
104 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700105 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700106
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700107 // it's safe to instantiate the SensorFusion object here
108 // (it wants to be instantiated after h/w sensors have been
109 // registered)
110 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700111
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700112 // build the sensor list returned to users
113 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700114
115 if (hasGyro) {
Mathias Agopian03193062013-05-10 19:32:39 -0700116 Sensor aSensor;
117
118 // Add Android virtual sensors if they're not already
119 // available in the HAL
120
121 aSensor = registerVirtualSensor( new RotationVectorSensor() );
122 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
123 mUserSensorList.add(aSensor);
124 }
125
126 aSensor = registerVirtualSensor( new GravitySensor(list, count) );
127 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
128 mUserSensorList.add(aSensor);
129 }
130
131 aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
132 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
133 mUserSensorList.add(aSensor);
134 }
135
136 aSensor = registerVirtualSensor( new OrientationSensor() );
137 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
138 // if we are doing our own rotation-vector, also add
139 // the orientation sensor and remove the HAL provided one.
140 mUserSensorList.replaceAt(aSensor, orientationIndex);
141 }
142
Mathias Agopian33264862012-06-28 19:46:54 -0700143 // virtual debugging sensors are not added to mUserSensorList
Mathias Agopian03193062013-05-10 19:32:39 -0700144 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33264862012-06-28 19:46:54 -0700145 registerVirtualSensor( new GyroDriftSensor() );
146 }
147
Mathias Agopian33264862012-06-28 19:46:54 -0700148 // debugging sensor list
Mathias Agopian03193062013-05-10 19:32:39 -0700149 mUserSensorListDebug = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700150
Aravind Akella4c8b9512013-09-05 17:03:38 -0700151 mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
152 FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r");
153 char line[128];
154 if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
155 line[sizeof(line) - 1] = '\0';
156 sscanf(line, "%u", &mSocketBufferSize);
157 if (mSocketBufferSize > MAX_SOCKET_BUFFER_SIZE_BATCHED) {
158 mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
159 }
160 }
161 ALOGD("Max socket buffer size %u", mSocketBufferSize);
162 if (fp) {
163 fclose(fp);
164 }
165
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700166 run("SensorService", PRIORITY_URGENT_DISPLAY);
167 mInitCheck = NO_ERROR;
168 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700169 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700170}
171
Mathias Agopian03193062013-05-10 19:32:39 -0700172Sensor SensorService::registerSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800173{
174 sensors_event_t event;
175 memset(&event, 0, sizeof(event));
176
177 const Sensor sensor(s->getSensor());
178 // add to the sensor list (returned to clients)
179 mSensorList.add(sensor);
180 // add to our handle->SensorInterface mapping
181 mSensorMap.add(sensor.getHandle(), s);
182 // create an entry in the mLastEventSeen array
183 mLastEventSeen.add(sensor.getHandle(), event);
Mathias Agopian03193062013-05-10 19:32:39 -0700184
185 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800186}
187
Mathias Agopian03193062013-05-10 19:32:39 -0700188Sensor SensorService::registerVirtualSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800189{
Mathias Agopian03193062013-05-10 19:32:39 -0700190 Sensor sensor = registerSensor(s);
Mathias Agopianf001c922010-11-11 17:58:51 -0800191 mVirtualSensorList.add( s );
Mathias Agopian03193062013-05-10 19:32:39 -0700192 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800193}
194
Mathias Agopianfc328812010-07-14 23:41:37 -0700195SensorService::~SensorService()
196{
Mathias Agopianf001c922010-11-11 17:58:51 -0800197 for (size_t i=0 ; i<mSensorMap.size() ; i++)
198 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700199}
200
Mathias Agopian1cb13462011-06-27 16:05:52 -0700201static const String16 sDump("android.permission.DUMP");
202
Mathias Agopianfc328812010-07-14 23:41:37 -0700203status_t SensorService::dump(int fd, const Vector<String16>& args)
204{
Mathias Agopianfc328812010-07-14 23:41:37 -0700205 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700206 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700207 result.appendFormat("Permission Denial: "
Mathias Agopianfc328812010-07-14 23:41:37 -0700208 "can't dump SurfaceFlinger from pid=%d, uid=%d\n",
209 IPCThreadState::self()->getCallingPid(),
210 IPCThreadState::self()->getCallingUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700211 } else {
212 Mutex::Autolock _l(mLock);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700213 result.append("Sensor List:\n");
Mathias Agopian3560fb22010-07-22 21:24:39 -0700214 for (size_t i=0 ; i<mSensorList.size() ; i++) {
215 const Sensor& s(mSensorList[i]);
216 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopianba02cd22013-07-03 16:20:57 -0700217 result.appendFormat(
218 "%-48s| %-32s | 0x%08x | ",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700219 s.getName().string(),
220 s.getVendor().string(),
Mathias Agopianba02cd22013-07-03 16:20:57 -0700221 s.getHandle());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700222
Mathias Agopianba02cd22013-07-03 16:20:57 -0700223 if (s.getMinDelay() > 0) {
224 result.appendFormat(
225 "maxRate=%7.2fHz | ", 1e6f / s.getMinDelay());
226 } else {
227 result.append(s.getMinDelay() == 0
228 ? "on-demand | "
229 : "one-shot | ");
230 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700231 if (s.getFifoMaxEventCount() > 0) {
232 result.appendFormat("getFifoMaxEventCount=%d events | ", s.getFifoMaxEventCount());
233 } else {
234 result.append("no batching support | ");
235 }
Mathias Agopianba02cd22013-07-03 16:20:57 -0700236
237 switch (s.getType()) {
238 case SENSOR_TYPE_ROTATION_VECTOR:
239 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
240 result.appendFormat(
241 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
242 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]);
243 break;
244 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
245 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
246 result.appendFormat(
247 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
248 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]);
249 break;
250 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
251 result.appendFormat(
252 "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n",
253 e.data[0], e.data[1], e.data[2], e.data[3]);
254 break;
255 case SENSOR_TYPE_SIGNIFICANT_MOTION:
256 case SENSOR_TYPE_STEP_DETECTOR:
257 result.appendFormat( "last=<%f>\n", e.data[0]);
258 break;
259 case SENSOR_TYPE_STEP_COUNTER:
260 result.appendFormat( "last=<%llu>\n", e.u64.step_counter);
261 break;
262 default:
263 // default to 3 values
264 result.appendFormat(
265 "last=<%5.1f,%5.1f,%5.1f>\n",
266 e.data[0], e.data[1], e.data[2]);
267 break;
268 }
269 }
270 SensorFusion::getInstance().dump(result);
271 SensorDevice::getInstance().dump(result);
272
Mathias Agopianba02cd22013-07-03 16:20:57 -0700273 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700274 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700275 int handle = mActiveSensors.keyAt(i);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700276 result.appendFormat("%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700277 getSensorName(handle).string(),
278 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700279 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700280 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700281
282 result.appendFormat("%u Max Socket Buffer size\n", mSocketBufferSize);
283 result.appendFormat("%d active connections\n", mActiveConnections.size());
284
285 for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
286 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
287 if (connection != 0) {
288 result.appendFormat("Connection Number: %d \n", i);
289 connection->dump(result);
290 }
291 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700292 }
293 write(fd, result.string(), result.size());
294 return NO_ERROR;
295}
296
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700297void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
298 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700299 SensorInterface* sensor;
300 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700301 for (int i=0 ; i<count ; i++) {
302 int handle = buffer[i].sensor;
Mathias Agopian7438fd12013-07-08 12:50:39 -0700303 int type = buffer[i].type;
304 if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700305 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700306 sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700307 if (sensor != NULL) {
308 sensor->autoDisable(connection.get(), handle);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700309 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700310 cleanupWithoutDisable(connection, handle);
311 }
312 }
313 }
314}
315
Mathias Agopianfc328812010-07-14 23:41:37 -0700316bool SensorService::threadLoop()
317{
Steve Blocka5512372011-12-20 16:23:08 +0000318 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700319
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700320 // each virtual sensor could generate an event per "real" event, that's why we need
321 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
322 // in practice, this is too aggressive, but guaranteed to be enough.
323 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
324 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
325
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700326 sensors_event_t buffer[minBufferSize];
327 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800328 SensorDevice& device(SensorDevice::getInstance());
329 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700330
Mathias Agopianf001c922010-11-11 17:58:51 -0800331 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700332 bool wakeLockAcquired = false;
333 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700334 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800335 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700336 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000337 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700338 break;
339 }
340
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700341 // Poll has returned. Hold a wakelock.
342 // Todo(): add a flag to the sensors definitions to indicate
343 // the sensors which can wake up the AP
344 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700345 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700346 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
347 wakeLockAcquired = true;
348 break;
349 }
350 }
351
Mathias Agopian94e8f682010-11-10 17:50:28 -0800352 recordLastValue(buffer, count);
353
Mathias Agopianf001c922010-11-11 17:58:51 -0800354 // handle virtual sensors
355 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700356 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800357 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
358 getActiveVirtualSensors());
359 const size_t activeVirtualSensorCount = virtualSensors.size();
360 if (activeVirtualSensorCount) {
361 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700362 SensorFusion& fusion(SensorFusion::getInstance());
363 if (fusion.isEnabled()) {
364 for (size_t i=0 ; i<size_t(count) ; i++) {
365 fusion.process(event[i]);
366 }
367 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700368 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800369 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700370 if (count + k >= minBufferSize) {
371 ALOGE("buffer too small to hold all events: "
372 "count=%u, k=%u, size=%u",
373 count, k, minBufferSize);
374 break;
375 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800376 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700377 SensorInterface* si = virtualSensors.valueAt(j);
378 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800379 buffer[count + k] = out;
380 k++;
381 }
382 }
383 }
384 if (k) {
385 // record the last synthesized values
386 recordLastValue(&buffer[count], k);
387 count += k;
388 // sort the buffer by time-stamps
389 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700390 }
391 }
392 }
393
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700394 // handle backward compatibility for RotationVector sensor
395 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
396 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700397 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700398 // All the 4 components of the quaternion should be available
399 // No heading accuracy. Set it to -1
400 buffer[i].data[4] = -1;
401 }
402 }
403 }
404
Mathias Agopianf001c922010-11-11 17:58:51 -0800405 // send our events to clients...
406 const SortedVector< wp<SensorEventConnection> > activeConnections(
407 getActiveConnections());
408 size_t numConnections = activeConnections.size();
409 for (size_t i=0 ; i<numConnections ; i++) {
410 sp<SensorEventConnection> connection(
411 activeConnections[i].promote());
412 if (connection != 0) {
413 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700414 // Some sensors need to be auto disabled after the trigger
415 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800416 }
417 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700418
419 // We have read the data, upper layers should hold the wakelock.
420 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
Mathias Agopianfc328812010-07-14 23:41:37 -0700421 } while (count >= 0 || Thread::exitPending());
422
Steve Block3c20fbe2012-01-05 23:22:43 +0000423 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800424 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700425 return false;
426}
427
Mathias Agopian94e8f682010-11-10 17:50:28 -0800428void SensorService::recordLastValue(
429 sensors_event_t const * buffer, size_t count)
430{
431 Mutex::Autolock _l(mLock);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800432 // record the last event for each sensor
433 int32_t prev = buffer[0].sensor;
434 for (size_t i=1 ; i<count ; i++) {
435 // record the last event of each sensor type in this buffer
436 int32_t curr = buffer[i].sensor;
437 if (curr != prev) {
438 mLastEventSeen.editValueFor(prev) = buffer[i-1];
439 prev = curr;
440 }
441 }
442 mLastEventSeen.editValueFor(prev) = buffer[count-1];
443}
444
Mathias Agopianf001c922010-11-11 17:58:51 -0800445void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
446{
447 struct compar {
448 static int cmp(void const* lhs, void const* rhs) {
449 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
450 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700451 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800452 }
453 };
454 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
455}
456
Mathias Agopianfc328812010-07-14 23:41:37 -0700457SortedVector< wp<SensorService::SensorEventConnection> >
458SensorService::getActiveConnections() const
459{
460 Mutex::Autolock _l(mLock);
461 return mActiveConnections;
462}
463
Mathias Agopianf001c922010-11-11 17:58:51 -0800464DefaultKeyedVector<int, SensorInterface*>
465SensorService::getActiveVirtualSensors() const
466{
467 Mutex::Autolock _l(mLock);
468 return mActiveVirtualSensors;
469}
470
Mathias Agopian5d270722010-07-19 15:20:39 -0700471String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700472 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700473 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700474 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700475 if (sensor.getHandle() == handle) {
476 return sensor.getName();
477 }
478 }
479 String8 result("unknown");
480 return result;
481}
482
Mathias Agopianfc328812010-07-14 23:41:37 -0700483Vector<Sensor> SensorService::getSensorList()
484{
Mathias Agopian33264862012-06-28 19:46:54 -0700485 char value[PROPERTY_VALUE_MAX];
486 property_get("debug.sensors", value, "0");
487 if (atoi(value)) {
488 return mUserSensorListDebug;
489 }
Mathias Agopian010e4222011-06-08 20:06:50 -0700490 return mUserSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700491}
492
493sp<ISensorEventConnection> SensorService::createSensorEventConnection()
494{
Mathias Agopian5307d172012-09-18 17:02:43 -0700495 uid_t uid = IPCThreadState::self()->getCallingUid();
496 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700497 return result;
498}
499
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800500void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700501{
502 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800503 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700504 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000505 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700506 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800507 int handle = mActiveSensors.keyAt(i);
508 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000509 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800510 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000511 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800512 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800513 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800514 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800515 }
516 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000517 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000518 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700519 "removing connection %p for sensor[%d].handle=0x%08x",
520 c, i, handle);
521
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800522 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000523 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700524 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800525 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700526 delete rec;
527 size--;
528 } else {
529 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700530 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700531 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700532 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700533 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700534}
535
536status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700537 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700538{
Mathias Agopian50df2952010-07-19 19:09:10 -0700539 if (mInitCheck != NO_ERROR)
540 return mInitCheck;
541
Mathias Agopianf001c922010-11-11 17:58:51 -0800542 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700543 if (sensor == NULL) {
544 return BAD_VALUE;
545 }
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700546 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700547 SensorRecord* rec = mActiveSensors.valueFor(handle);
548 if (rec == 0) {
549 rec = new SensorRecord(connection);
550 mActiveSensors.add(handle, rec);
551 if (sensor->isVirtual()) {
552 mActiveVirtualSensors.add(handle, sensor);
553 }
554 } else {
555 if (rec->addConnection(connection)) {
556 // this sensor is already activated, but we are adding a
557 // connection that uses it. Immediately send down the last
558 // known value of the requested sensor if it's not a
559 // "continuous" sensor.
560 if (sensor->getSensor().getMinDelay() == 0) {
561 sensors_event_t scratch;
562 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
563 if (event.version == sizeof(sensors_event_t)) {
564 connection->sendEvents(&event, 1);
565 }
566 }
567 }
568 }
569
570 if (connection->addSensor(handle)) {
571 BatteryService::enableSensor(connection->getUid(), handle);
572 // the sensor was added (which means it wasn't already there)
573 // so, see if this connection becomes active
574 if (mActiveConnections.indexOf(connection) < 0) {
575 mActiveConnections.add(connection);
576 }
577 } else {
578 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
579 handle, connection.get());
580 }
581
Aravind Akella724d91d2013-06-27 12:04:23 -0700582 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
583 if (samplingPeriodNs < minDelayNs) {
584 samplingPeriodNs = minDelayNs;
585 }
586
587 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d rate=%lld timeout== %lld",
588 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
589
590 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
591 maxBatchReportLatencyNs);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700592 if (err == NO_ERROR) {
593 connection->setFirstFlushPending(handle, true);
594 status_t err_flush = sensor->flush(connection.get(), handle);
595 // Flush may return error if the sensor is not activated or the underlying h/w sensor does
596 // not support flush.
597 if (err_flush != NO_ERROR) {
598 connection->setFirstFlushPending(handle, false);
599 }
600 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700601
602 if (err == NO_ERROR) {
603 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
604 err = sensor->activate(connection.get(), true);
605 }
606
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700607 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700608 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700609 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700610 }
611 return err;
612}
613
614status_t SensorService::disable(const sp<SensorEventConnection>& connection,
615 int handle)
616{
Mathias Agopian50df2952010-07-19 19:09:10 -0700617 if (mInitCheck != NO_ERROR)
618 return mInitCheck;
619
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700620 Mutex::Autolock _l(mLock);
621 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700622 if (err == NO_ERROR) {
623 SensorInterface* sensor = mSensorMap.valueFor(handle);
624 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
625 }
626 return err;
627}
628
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700629status_t SensorService::cleanupWithoutDisable(
630 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700631 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700632 return cleanupWithoutDisableLocked(connection, handle);
633}
634
635status_t SensorService::cleanupWithoutDisableLocked(
636 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700637 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700638 if (rec) {
639 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700640 if (connection->removeSensor(handle)) {
641 BatteryService::disableSensor(connection->getUid(), handle);
642 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700643 if (connection->hasAnySensor() == false) {
644 mActiveConnections.remove(connection);
645 }
646 // see if this sensor becomes inactive
647 if (rec->removeConnection(connection)) {
648 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800649 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700650 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700651 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700652 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700653 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700654 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700655}
656
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700657status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700658 int handle, nsecs_t ns)
659{
Mathias Agopian50df2952010-07-19 19:09:10 -0700660 if (mInitCheck != NO_ERROR)
661 return mInitCheck;
662
Mathias Agopianae09d652011-11-01 17:37:49 -0700663 SensorInterface* sensor = mSensorMap.valueFor(handle);
664 if (!sensor)
665 return BAD_VALUE;
666
Mathias Agopian1cd70002010-07-21 15:59:50 -0700667 if (ns < 0)
668 return BAD_VALUE;
669
Mathias Agopian62569ec2011-11-07 21:21:47 -0800670 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
671 if (ns < minDelayNs) {
672 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700673 }
674
Mathias Agopianf001c922010-11-11 17:58:51 -0800675 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700676}
677
Aravind Akella724d91d2013-06-27 12:04:23 -0700678status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
679 int handle) {
680 if (mInitCheck != NO_ERROR) return mInitCheck;
681 SensorInterface* sensor = mSensorMap.valueFor(handle);
Aravind Akella701166d2013-10-08 14:59:26 -0700682 if (sensor == NULL) {
683 return BAD_VALUE;
684 }
685 if (sensor->getSensor().getType() == SENSOR_TYPE_SIGNIFICANT_MOTION) {
686 ALOGE("flush called on Significant Motion sensor");
687 return INVALID_OPERATION;
Aravind Akella724d91d2013-06-27 12:04:23 -0700688 }
689 return sensor->flush(connection.get(), handle);
690}
Mathias Agopianfc328812010-07-14 23:41:37 -0700691// ---------------------------------------------------------------------------
692
693SensorService::SensorRecord::SensorRecord(
694 const sp<SensorEventConnection>& connection)
695{
696 mConnections.add(connection);
697}
698
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700699bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700700 const sp<SensorEventConnection>& connection)
701{
702 if (mConnections.indexOf(connection) < 0) {
703 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700704 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700705 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700706 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700707}
708
709bool SensorService::SensorRecord::removeConnection(
710 const wp<SensorEventConnection>& connection)
711{
712 ssize_t index = mConnections.indexOf(connection);
713 if (index >= 0) {
714 mConnections.removeItemsAt(index, 1);
715 }
716 return mConnections.size() ? false : true;
717}
718
719// ---------------------------------------------------------------------------
720
721SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700722 const sp<SensorService>& service, uid_t uid)
Aravind Akella4c8b9512013-09-05 17:03:38 -0700723 : mService(service), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700724{
Aravind Akella4c8b9512013-09-05 17:03:38 -0700725 const SensorDevice& device(SensorDevice::getInstance());
726 if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) {
727 // Increase socket buffer size to 1MB for batching capabilities.
728 mChannel = new BitTube(service->mSocketBufferSize);
729 } else {
730 mChannel = new BitTube(SOCKET_BUFFER_SIZE_NON_BATCHED);
731 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700732}
733
734SensorService::SensorEventConnection::~SensorEventConnection()
735{
Steve Blocka5512372011-12-20 16:23:08 +0000736 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700737 mService->cleanupConnection(this);
738}
739
740void SensorService::SensorEventConnection::onFirstRef()
741{
742}
743
Aravind Akella4c8b9512013-09-05 17:03:38 -0700744void SensorService::SensorEventConnection::dump(String8& result) {
745 Mutex::Autolock _l(mConnectionLock);
746 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
747 const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
748 result.appendFormat("\t %s | status: %s | pending flush events %d\n",
749 mService->getSensorName(mSensorInfo.keyAt(i)).string(),
750 flushInfo.mFirstFlushPending ? "First flush pending" :
751 "active",
752 flushInfo.mPendingFlushEventsToSend);
753 }
754}
755
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700756bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800757 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700758 if (mSensorInfo.indexOfKey(handle) < 0) {
759 mSensorInfo.add(handle, FlushInfo());
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700760 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700761 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700762 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700763}
764
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700765bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800766 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700767 if (mSensorInfo.removeItem(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700768 return true;
769 }
770 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700771}
772
773bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800774 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700775 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700776}
777
778bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800779 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700780 return mSensorInfo.size() ? true : false;
781}
782
Aravind Akella4c8b9512013-09-05 17:03:38 -0700783void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
784 bool value) {
785 Mutex::Autolock _l(mConnectionLock);
786 ssize_t index = mSensorInfo.indexOfKey(handle);
787 if (index >= 0) {
788 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
789 flushInfo.mFirstFlushPending = value;
790 }
791}
792
Mathias Agopianfc328812010-07-14 23:41:37 -0700793status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700794 sensors_event_t const* buffer, size_t numEvents,
795 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700796{
Mathias Agopiancf510012010-07-22 16:18:10 -0700797 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700798 size_t count = 0;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700799
Mathias Agopian3560fb22010-07-22 21:24:39 -0700800 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800801 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700802 size_t i=0;
803 while (i<numEvents) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700804 int32_t curr = buffer[i].sensor;
805 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
806 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
807 buffer[i].meta_data.sensor);
808 // Setting curr to the correct sensor to ensure the sensor events per connection are
809 // filtered correctly. buffer[i].sensor is zero for meta_data events.
810 curr = buffer[i].meta_data.sensor;
811 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700812 ssize_t index = mSensorInfo.indexOfKey(curr);
813 if (index >= 0 && mSensorInfo[index].mFirstFlushPending == true &&
814 buffer[i].type == SENSOR_TYPE_META_DATA) {
815 // This is the first flush before activate is called. Events can now be sent for
816 // this sensor on this connection.
817 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
818 buffer[i].meta_data.sensor);
819 mSensorInfo.editValueAt(index).mFirstFlushPending = false;
820 }
821 if (index >= 0 && mSensorInfo[index].mFirstFlushPending == false) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700822 do {
Aravind Akella4c8b9512013-09-05 17:03:38 -0700823 scratch[count++] = buffer[i++];
Aravind Akella724d91d2013-06-27 12:04:23 -0700824 } while ((i<numEvents) && ((buffer[i].sensor == curr) ||
825 (buffer[i].type == SENSOR_TYPE_META_DATA &&
826 buffer[i].meta_data.sensor == curr)));
Mathias Agopian3560fb22010-07-22 21:24:39 -0700827 } else {
828 i++;
829 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700830 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700831 } else {
832 scratch = const_cast<sensors_event_t *>(buffer);
833 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700834 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700835
Aravind Akella4c8b9512013-09-05 17:03:38 -0700836 // Send pending flush events (if any) before sending events from the cache.
837 {
838 ASensorEvent flushCompleteEvent;
839 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
840 flushCompleteEvent.sensor = 0;
841 Mutex::Autolock _l(mConnectionLock);
842 // Loop through all the sensors for this connection and check if there are any pending
843 // flush complete events to be sent.
844 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
845 FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
846 while (flushInfo.mPendingFlushEventsToSend > 0) {
847 flushCompleteEvent.meta_data.sensor = mSensorInfo.keyAt(i);
848 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
849 if (size < 0) {
850 // ALOGW("dropping %d events on the floor", count);
Aravind Akellac551eac2013-10-14 17:04:42 -0700851 countFlushCompleteEventsLocked(scratch, count);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700852 return size;
853 }
854 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
855 flushCompleteEvent.meta_data.sensor);
856 flushInfo.mPendingFlushEventsToSend--;
857 }
858 }
859 }
860
Mathias Agopian907103b2012-04-02 18:38:02 -0700861 // NOTE: ASensorEvent and sensors_event_t are the same type
862 ssize_t size = SensorEventQueue::write(mChannel,
863 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700864 if (size == -EAGAIN) {
865 // the destination doesn't accept events anymore, it's probably
866 // full. For now, we just drop the events on the floor.
Aravind Akella4c8b9512013-09-05 17:03:38 -0700867 // ALOGW("dropping %d events on the floor", count);
Aravind Akellac551eac2013-10-14 17:04:42 -0700868 Mutex::Autolock _l(mConnectionLock);
869 countFlushCompleteEventsLocked(scratch, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700870 return size;
871 }
872
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700873 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700874}
875
Aravind Akellac551eac2013-10-14 17:04:42 -0700876void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
Aravind Akella4c8b9512013-09-05 17:03:38 -0700877 sensors_event_t* scratch, const int numEventsDropped) {
878 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700879 // Count flushComplete events in the events that are about to the dropped. These will be sent
880 // separately before the next batch of events.
881 for (int j = 0; j < numEventsDropped; ++j) {
882 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
883 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
884 flushInfo.mPendingFlushEventsToSend++;
885 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
886 flushInfo.mPendingFlushEventsToSend);
887 }
888 }
889 return;
890}
891
Mathias Agopianb3989272011-10-20 18:42:02 -0700892sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700893{
894 return mChannel;
895}
896
897status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -0700898 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
899 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700900{
901 status_t err;
902 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700903 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
904 reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700905 } else {
906 err = mService->disable(this, handle);
907 }
908 return err;
909}
910
911status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -0700912 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -0700913{
Aravind Akella724d91d2013-06-27 12:04:23 -0700914 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -0700915}
916
Aravind Akella701166d2013-10-08 14:59:26 -0700917status_t SensorService::SensorEventConnection::flush() {
Aravind Akellac551eac2013-10-14 17:04:42 -0700918 SensorDevice& dev(SensorDevice::getInstance());
919 const int halVersion = dev.getHalDeviceVersion();
Aravind Akella701166d2013-10-08 14:59:26 -0700920 Mutex::Autolock _l(mConnectionLock);
921 status_t err(NO_ERROR);
Aravind Akellac551eac2013-10-14 17:04:42 -0700922 // Loop through all sensors for this connection and call flush on each of them.
Aravind Akella701166d2013-10-08 14:59:26 -0700923 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
924 const int handle = mSensorInfo.keyAt(i);
Aravind Akellac551eac2013-10-14 17:04:42 -0700925 if (halVersion < SENSORS_DEVICE_API_VERSION_1_1) {
926 // For older devices just increment pending flush count which will send a trivial
927 // flush complete event.
928 FlushInfo& flushInfo = mSensorInfo.editValueFor(handle);
929 flushInfo.mPendingFlushEventsToSend++;
930 } else {
931 status_t err_flush = mService->flushSensor(this, handle);
932 if (err_flush != NO_ERROR) {
933 ALOGE("Flush error handle=%d %s", handle, strerror(-err_flush));
934 }
935 err = (err_flush != NO_ERROR) ? err_flush : err;
Aravind Akella701166d2013-10-08 14:59:26 -0700936 }
Aravind Akella701166d2013-10-08 14:59:26 -0700937 }
938 return err;
Aravind Akella724d91d2013-06-27 12:04:23 -0700939}
Aravind Akella4c8b9512013-09-05 17:03:38 -0700940
Mathias Agopianfc328812010-07-14 23:41:37 -0700941// ---------------------------------------------------------------------------
942}; // namespace android
943