blob: ca2fdf66d63e1d4d1e6a3548b9e0acf1786d3554 [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: "
Aravind Akella70018042014-04-07 22:52:37 +0000208 "can't dump SensorService from pid=%d, uid=%d\n",
Mathias Agopianfc328812010-07-14 23:41:37 -0700209 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(
Aravind Akella70018042014-04-07 22:52:37 +0000218 "%-48s| %-32s| %-48s| 0x%08x | \"%s\"\n\t",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700219 s.getName().string(),
220 s.getVendor().string(),
Aravind Akella70018042014-04-07 22:52:37 +0000221 s.getStringType().string(),
222 s.getHandle(),
223 s.getRequiredPermission().string());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700224
Mathias Agopianba02cd22013-07-03 16:20:57 -0700225 if (s.getMinDelay() > 0) {
226 result.appendFormat(
Aravind Akella70018042014-04-07 22:52:37 +0000227 "maxRate=%7.2fHz | ", 1e6f / s.getMinDelay());
Mathias Agopianba02cd22013-07-03 16:20:57 -0700228 } else {
229 result.append(s.getMinDelay() == 0
230 ? "on-demand | "
231 : "one-shot | ");
232 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700233 if (s.getFifoMaxEventCount() > 0) {
Aravind Akella70018042014-04-07 22:52:37 +0000234 result.appendFormat("FifoMax=%d events | ",
235 s.getFifoMaxEventCount());
Aravind Akella724d91d2013-06-27 12:04:23 -0700236 } else {
237 result.append("no batching support | ");
238 }
Mathias Agopianba02cd22013-07-03 16:20:57 -0700239
240 switch (s.getType()) {
241 case SENSOR_TYPE_ROTATION_VECTOR:
242 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
243 result.appendFormat(
244 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
245 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4]);
246 break;
247 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
248 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
249 result.appendFormat(
250 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f>\n",
251 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5]);
252 break;
253 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
254 result.appendFormat(
255 "last=<%5.1f,%5.1f,%5.1f,%5.1f>\n",
256 e.data[0], e.data[1], e.data[2], e.data[3]);
257 break;
258 case SENSOR_TYPE_SIGNIFICANT_MOTION:
259 case SENSOR_TYPE_STEP_DETECTOR:
260 result.appendFormat( "last=<%f>\n", e.data[0]);
261 break;
262 case SENSOR_TYPE_STEP_COUNTER:
263 result.appendFormat( "last=<%llu>\n", e.u64.step_counter);
264 break;
265 default:
266 // default to 3 values
267 result.appendFormat(
268 "last=<%5.1f,%5.1f,%5.1f>\n",
269 e.data[0], e.data[1], e.data[2]);
270 break;
271 }
272 }
273 SensorFusion::getInstance().dump(result);
274 SensorDevice::getInstance().dump(result);
275
Mathias Agopianba02cd22013-07-03 16:20:57 -0700276 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700277 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700278 int handle = mActiveSensors.keyAt(i);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700279 result.appendFormat("%s (handle=0x%08x, connections=%d)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700280 getSensorName(handle).string(),
281 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700282 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700283 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700284
285 result.appendFormat("%u Max Socket Buffer size\n", mSocketBufferSize);
286 result.appendFormat("%d active connections\n", mActiveConnections.size());
287
288 for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
289 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
290 if (connection != 0) {
291 result.appendFormat("Connection Number: %d \n", i);
292 connection->dump(result);
293 }
294 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700295 }
296 write(fd, result.string(), result.size());
297 return NO_ERROR;
298}
299
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700300void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
301 sensors_event_t const* buffer, const int count) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700302 SensorInterface* sensor;
303 status_t err = NO_ERROR;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700304 for (int i=0 ; i<count ; i++) {
305 int handle = buffer[i].sensor;
Mathias Agopian7438fd12013-07-08 12:50:39 -0700306 int type = buffer[i].type;
307 if (type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700308 if (connection->hasSensor(handle)) {
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700309 sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700310 if (sensor != NULL) {
311 sensor->autoDisable(connection.get(), handle);
Jaikumar Ganesh4c01b1a2013-04-16 15:52:23 -0700312 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700313 cleanupWithoutDisable(connection, handle);
314 }
315 }
316 }
317}
318
Mathias Agopianfc328812010-07-14 23:41:37 -0700319bool SensorService::threadLoop()
320{
Steve Blocka5512372011-12-20 16:23:08 +0000321 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700322
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700323 // each virtual sensor could generate an event per "real" event, that's why we need
324 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
325 // in practice, this is too aggressive, but guaranteed to be enough.
326 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
327 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
328
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700329 sensors_event_t buffer[minBufferSize];
330 sensors_event_t scratch[minBufferSize];
Mathias Agopianf001c922010-11-11 17:58:51 -0800331 SensorDevice& device(SensorDevice::getInstance());
332 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700333
Mathias Agopianf001c922010-11-11 17:58:51 -0800334 ssize_t count;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700335 bool wakeLockAcquired = false;
336 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700337 do {
Mathias Agopianf001c922010-11-11 17:58:51 -0800338 count = device.poll(buffer, numEventMax);
Mathias Agopianfc328812010-07-14 23:41:37 -0700339 if (count<0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000340 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700341 break;
342 }
343
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700344 // Poll has returned. Hold a wakelock.
345 // Todo(): add a flag to the sensors definitions to indicate
346 // the sensors which can wake up the AP
347 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700348 if (buffer[i].type == SENSOR_TYPE_SIGNIFICANT_MOTION) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700349 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
350 wakeLockAcquired = true;
351 break;
352 }
353 }
354
Mathias Agopian94e8f682010-11-10 17:50:28 -0800355 recordLastValue(buffer, count);
356
Mathias Agopianf001c922010-11-11 17:58:51 -0800357 // handle virtual sensors
358 if (count && vcount) {
Mathias Agopian984826c2011-05-17 22:54:42 -0700359 sensors_event_t const * const event = buffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800360 const DefaultKeyedVector<int, SensorInterface*> virtualSensors(
361 getActiveVirtualSensors());
362 const size_t activeVirtualSensorCount = virtualSensors.size();
363 if (activeVirtualSensorCount) {
364 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700365 SensorFusion& fusion(SensorFusion::getInstance());
366 if (fusion.isEnabled()) {
367 for (size_t i=0 ; i<size_t(count) ; i++) {
368 fusion.process(event[i]);
369 }
370 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700371 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800372 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700373 if (count + k >= minBufferSize) {
374 ALOGE("buffer too small to hold all events: "
375 "count=%u, k=%u, size=%u",
376 count, k, minBufferSize);
377 break;
378 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800379 sensors_event_t out;
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700380 SensorInterface* si = virtualSensors.valueAt(j);
381 if (si->process(&out, event[i])) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800382 buffer[count + k] = out;
383 k++;
384 }
385 }
386 }
387 if (k) {
388 // record the last synthesized values
389 recordLastValue(&buffer[count], k);
390 count += k;
391 // sort the buffer by time-stamps
392 sortEventBuffer(buffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700393 }
394 }
395 }
396
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700397 // handle backward compatibility for RotationVector sensor
398 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
399 for (int i = 0; i < count; i++) {
Mathias Agopian7438fd12013-07-08 12:50:39 -0700400 if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700401 // All the 4 components of the quaternion should be available
402 // No heading accuracy. Set it to -1
403 buffer[i].data[4] = -1;
404 }
405 }
406 }
407
Mathias Agopianf001c922010-11-11 17:58:51 -0800408 // send our events to clients...
409 const SortedVector< wp<SensorEventConnection> > activeConnections(
410 getActiveConnections());
411 size_t numConnections = activeConnections.size();
412 for (size_t i=0 ; i<numConnections ; i++) {
413 sp<SensorEventConnection> connection(
414 activeConnections[i].promote());
415 if (connection != 0) {
416 connection->sendEvents(buffer, count, scratch);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700417 // Some sensors need to be auto disabled after the trigger
418 cleanupAutoDisabledSensor(connection, buffer, count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800419 }
420 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700421
422 // We have read the data, upper layers should hold the wakelock.
423 if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME);
Mathias Agopianfc328812010-07-14 23:41:37 -0700424 } while (count >= 0 || Thread::exitPending());
425
Steve Block3c20fbe2012-01-05 23:22:43 +0000426 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800427 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700428 return false;
429}
430
Mathias Agopian94e8f682010-11-10 17:50:28 -0800431void SensorService::recordLastValue(
Aravind Akella4b847042014-03-03 19:02:46 -0800432 const sensors_event_t* buffer, size_t count) {
Mathias Agopian94e8f682010-11-10 17:50:28 -0800433 Mutex::Autolock _l(mLock);
Aravind Akella4b847042014-03-03 19:02:46 -0800434 const sensors_event_t* last = NULL;
435 for (size_t i = 0; i < count; i++) {
436 const sensors_event_t* event = &buffer[i];
437 if (event->type != SENSOR_TYPE_META_DATA) {
438 if (last && event->sensor != last->sensor) {
439 mLastEventSeen.editValueFor(last->sensor) = *last;
440 }
441 last = event;
Mathias Agopian94e8f682010-11-10 17:50:28 -0800442 }
443 }
Aravind Akella4b847042014-03-03 19:02:46 -0800444 if (last) {
445 mLastEventSeen.editValueFor(last->sensor) = *last;
446 }
Mathias Agopian94e8f682010-11-10 17:50:28 -0800447}
448
Mathias Agopianf001c922010-11-11 17:58:51 -0800449void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
450{
451 struct compar {
452 static int cmp(void const* lhs, void const* rhs) {
453 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
454 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700455 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800456 }
457 };
458 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
459}
460
Mathias Agopianfc328812010-07-14 23:41:37 -0700461SortedVector< wp<SensorService::SensorEventConnection> >
462SensorService::getActiveConnections() const
463{
464 Mutex::Autolock _l(mLock);
465 return mActiveConnections;
466}
467
Mathias Agopianf001c922010-11-11 17:58:51 -0800468DefaultKeyedVector<int, SensorInterface*>
469SensorService::getActiveVirtualSensors() const
470{
471 Mutex::Autolock _l(mLock);
472 return mActiveVirtualSensors;
473}
474
Mathias Agopian5d270722010-07-19 15:20:39 -0700475String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700476 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700477 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700478 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700479 if (sensor.getHandle() == handle) {
480 return sensor.getName();
481 }
482 }
483 String8 result("unknown");
484 return result;
485}
486
Aravind Akellab4099e72013-10-15 15:43:10 -0700487bool SensorService::isVirtualSensor(int handle) const {
488 SensorInterface* sensor = mSensorMap.valueFor(handle);
489 return sensor->isVirtual();
490}
491
Mathias Agopianfc328812010-07-14 23:41:37 -0700492Vector<Sensor> SensorService::getSensorList()
493{
Mathias Agopian33264862012-06-28 19:46:54 -0700494 char value[PROPERTY_VALUE_MAX];
495 property_get("debug.sensors", value, "0");
Aravind Akella70018042014-04-07 22:52:37 +0000496 const Vector<Sensor>& initialSensorList = (atoi(value)) ?
497 mUserSensorListDebug : mUserSensorList;
498 Vector<Sensor> accessibleSensorList;
499 for (size_t i = 0; i < initialSensorList.size(); i++) {
500 Sensor sensor = initialSensorList[i];
501 if (canAccessSensor(sensor)) {
502 accessibleSensorList.add(sensor);
503 } else {
504 String8 infoMessage;
505 infoMessage.appendFormat(
506 "Skipped sensor %s because it requires permission %s",
507 sensor.getName().string(),
508 sensor.getRequiredPermission().string());
509 ALOGI(infoMessage.string());
510 }
Mathias Agopian33264862012-06-28 19:46:54 -0700511 }
Aravind Akella70018042014-04-07 22:52:37 +0000512 return accessibleSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700513}
514
515sp<ISensorEventConnection> SensorService::createSensorEventConnection()
516{
Mathias Agopian5307d172012-09-18 17:02:43 -0700517 uid_t uid = IPCThreadState::self()->getCallingUid();
518 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700519 return result;
520}
521
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800522void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700523{
524 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800525 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700526 size_t size = mActiveSensors.size();
Steve Blocka5512372011-12-20 16:23:08 +0000527 ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700528 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800529 int handle = mActiveSensors.keyAt(i);
530 if (c->hasSensor(handle)) {
Steve Blocka5512372011-12-20 16:23:08 +0000531 ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800532 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000533 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800534 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800535 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800536 }
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800537 }
538 SensorRecord* rec = mActiveSensors.valueAt(i);
Steve Blockf5a12302012-01-06 19:20:56 +0000539 ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000540 ALOGD_IF(DEBUG_CONNECTIONS,
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700541 "removing connection %p for sensor[%d].handle=0x%08x",
542 c, i, handle);
543
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800544 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000545 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700546 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800547 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700548 delete rec;
549 size--;
550 } else {
551 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700552 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700553 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700554 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700555 BatteryService::cleanup(c->getUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700556}
557
Aravind Akella70018042014-04-07 22:52:37 +0000558Sensor SensorService::getSensorFromHandle(int handle) const {
559 return mSensorMap.valueFor(handle)->getSensor();
560}
561
Mathias Agopianfc328812010-07-14 23:41:37 -0700562status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700563 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700564{
Mathias Agopian50df2952010-07-19 19:09:10 -0700565 if (mInitCheck != NO_ERROR)
566 return mInitCheck;
567
Mathias Agopianf001c922010-11-11 17:58:51 -0800568 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700569 if (sensor == NULL) {
570 return BAD_VALUE;
571 }
Aravind Akella70018042014-04-07 22:52:37 +0000572
573 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried enabling")) {
574 return BAD_VALUE;
575 }
576
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700577 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700578 SensorRecord* rec = mActiveSensors.valueFor(handle);
579 if (rec == 0) {
580 rec = new SensorRecord(connection);
581 mActiveSensors.add(handle, rec);
582 if (sensor->isVirtual()) {
583 mActiveVirtualSensors.add(handle, sensor);
584 }
585 } else {
586 if (rec->addConnection(connection)) {
587 // this sensor is already activated, but we are adding a
588 // connection that uses it. Immediately send down the last
589 // known value of the requested sensor if it's not a
590 // "continuous" sensor.
591 if (sensor->getSensor().getMinDelay() == 0) {
592 sensors_event_t scratch;
593 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
594 if (event.version == sizeof(sensors_event_t)) {
595 connection->sendEvents(&event, 1);
596 }
597 }
598 }
599 }
600
601 if (connection->addSensor(handle)) {
602 BatteryService::enableSensor(connection->getUid(), handle);
603 // the sensor was added (which means it wasn't already there)
604 // so, see if this connection becomes active
605 if (mActiveConnections.indexOf(connection) < 0) {
606 mActiveConnections.add(connection);
607 }
608 } else {
609 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
610 handle, connection.get());
611 }
612
Aravind Akella724d91d2013-06-27 12:04:23 -0700613 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
614 if (samplingPeriodNs < minDelayNs) {
615 samplingPeriodNs = minDelayNs;
616 }
617
618 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d rate=%lld timeout== %lld",
619 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
620
621 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
622 maxBatchReportLatencyNs);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700623 if (err == NO_ERROR) {
624 connection->setFirstFlushPending(handle, true);
625 status_t err_flush = sensor->flush(connection.get(), handle);
626 // Flush may return error if the sensor is not activated or the underlying h/w sensor does
627 // not support flush.
628 if (err_flush != NO_ERROR) {
629 connection->setFirstFlushPending(handle, false);
630 }
631 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700632
633 if (err == NO_ERROR) {
634 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
635 err = sensor->activate(connection.get(), true);
636 }
637
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700638 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700639 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700640 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700641 }
642 return err;
643}
644
645status_t SensorService::disable(const sp<SensorEventConnection>& connection,
646 int handle)
647{
Mathias Agopian50df2952010-07-19 19:09:10 -0700648 if (mInitCheck != NO_ERROR)
649 return mInitCheck;
650
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700651 Mutex::Autolock _l(mLock);
652 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700653 if (err == NO_ERROR) {
654 SensorInterface* sensor = mSensorMap.valueFor(handle);
655 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
656 }
657 return err;
658}
659
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700660status_t SensorService::cleanupWithoutDisable(
661 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700662 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700663 return cleanupWithoutDisableLocked(connection, handle);
664}
665
666status_t SensorService::cleanupWithoutDisableLocked(
667 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700668 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700669 if (rec) {
670 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700671 if (connection->removeSensor(handle)) {
672 BatteryService::disableSensor(connection->getUid(), handle);
673 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700674 if (connection->hasAnySensor() == false) {
675 mActiveConnections.remove(connection);
676 }
677 // see if this sensor becomes inactive
678 if (rec->removeConnection(connection)) {
679 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800680 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700681 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700682 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700683 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700684 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700685 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700686}
687
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700688status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700689 int handle, nsecs_t ns)
690{
Mathias Agopian50df2952010-07-19 19:09:10 -0700691 if (mInitCheck != NO_ERROR)
692 return mInitCheck;
693
Mathias Agopianae09d652011-11-01 17:37:49 -0700694 SensorInterface* sensor = mSensorMap.valueFor(handle);
695 if (!sensor)
696 return BAD_VALUE;
697
Aravind Akella70018042014-04-07 22:52:37 +0000698 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried configuring")) {
699 return BAD_VALUE;
700 }
701
Mathias Agopian1cd70002010-07-21 15:59:50 -0700702 if (ns < 0)
703 return BAD_VALUE;
704
Mathias Agopian62569ec2011-11-07 21:21:47 -0800705 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
706 if (ns < minDelayNs) {
707 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700708 }
709
Mathias Agopianf001c922010-11-11 17:58:51 -0800710 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700711}
712
Aravind Akella724d91d2013-06-27 12:04:23 -0700713status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
714 int handle) {
Aravind Akella70018042014-04-07 22:52:37 +0000715 if (mInitCheck != NO_ERROR) return mInitCheck;
716 SensorInterface* sensor = mSensorMap.valueFor(handle);
717 if (sensor == NULL) {
718 return BAD_VALUE;
719 }
720
721 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried flushing")) {
722 return BAD_VALUE;
723 }
724
725 if (sensor->getSensor().getType() == SENSOR_TYPE_SIGNIFICANT_MOTION) {
726 ALOGE("flush called on Significant Motion sensor");
727 return INVALID_OPERATION;
728 }
729 return sensor->flush(connection.get(), handle);
Aravind Akella724d91d2013-06-27 12:04:23 -0700730}
Aravind Akella70018042014-04-07 22:52:37 +0000731
732
733bool SensorService::canAccessSensor(const Sensor& sensor) {
734 String16 permissionString(sensor.getRequiredPermission());
735 return permissionString.size() == 0 ||
736 PermissionCache::checkCallingPermission(permissionString);
737}
738
739bool SensorService::verifyCanAccessSensor(const Sensor& sensor, const char* operation) {
740 if (canAccessSensor(sensor)) {
741 return true;
742 } else {
743 String8 errorMessage;
744 errorMessage.appendFormat(
745 "%s a sensor (%s) without holding its required permission: %s",
746 operation,
747 sensor.getName().string(),
748 sensor.getRequiredPermission().string());
749 return false;
750 }
751}
752
Mathias Agopianfc328812010-07-14 23:41:37 -0700753// ---------------------------------------------------------------------------
754
755SensorService::SensorRecord::SensorRecord(
756 const sp<SensorEventConnection>& connection)
757{
758 mConnections.add(connection);
759}
760
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700761bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700762 const sp<SensorEventConnection>& connection)
763{
764 if (mConnections.indexOf(connection) < 0) {
765 mConnections.add(connection);
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
771bool SensorService::SensorRecord::removeConnection(
772 const wp<SensorEventConnection>& connection)
773{
774 ssize_t index = mConnections.indexOf(connection);
775 if (index >= 0) {
776 mConnections.removeItemsAt(index, 1);
777 }
778 return mConnections.size() ? false : true;
779}
780
781// ---------------------------------------------------------------------------
782
783SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700784 const sp<SensorService>& service, uid_t uid)
Aravind Akella4c8b9512013-09-05 17:03:38 -0700785 : mService(service), mUid(uid)
Mathias Agopianfc328812010-07-14 23:41:37 -0700786{
Aravind Akella4c8b9512013-09-05 17:03:38 -0700787 const SensorDevice& device(SensorDevice::getInstance());
788 if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) {
789 // Increase socket buffer size to 1MB for batching capabilities.
790 mChannel = new BitTube(service->mSocketBufferSize);
791 } else {
792 mChannel = new BitTube(SOCKET_BUFFER_SIZE_NON_BATCHED);
793 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700794}
795
796SensorService::SensorEventConnection::~SensorEventConnection()
797{
Steve Blocka5512372011-12-20 16:23:08 +0000798 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Mathias Agopianfc328812010-07-14 23:41:37 -0700799 mService->cleanupConnection(this);
800}
801
802void SensorService::SensorEventConnection::onFirstRef()
803{
804}
805
Aravind Akella4c8b9512013-09-05 17:03:38 -0700806void SensorService::SensorEventConnection::dump(String8& result) {
807 Mutex::Autolock _l(mConnectionLock);
808 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
809 const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
810 result.appendFormat("\t %s | status: %s | pending flush events %d\n",
811 mService->getSensorName(mSensorInfo.keyAt(i)).string(),
812 flushInfo.mFirstFlushPending ? "First flush pending" :
813 "active",
814 flushInfo.mPendingFlushEventsToSend);
815 }
816}
817
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700818bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800819 Mutex::Autolock _l(mConnectionLock);
Aravind Akella70018042014-04-07 22:52:37 +0000820 if (!verifyCanAccessSensor(mService->getSensorFromHandle(handle), "Tried adding")) {
821 return false;
822 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700823 if (mSensorInfo.indexOfKey(handle) < 0) {
824 mSensorInfo.add(handle, FlushInfo());
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700825 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700826 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700827 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700828}
829
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700830bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800831 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700832 if (mSensorInfo.removeItem(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700833 return true;
834 }
835 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700836}
837
838bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800839 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700840 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -0700841}
842
843bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800844 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700845 return mSensorInfo.size() ? true : false;
846}
847
Aravind Akella4c8b9512013-09-05 17:03:38 -0700848void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
849 bool value) {
850 Mutex::Autolock _l(mConnectionLock);
851 ssize_t index = mSensorInfo.indexOfKey(handle);
852 if (index >= 0) {
853 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
854 flushInfo.mFirstFlushPending = value;
855 }
856}
857
Mathias Agopianfc328812010-07-14 23:41:37 -0700858status_t SensorService::SensorEventConnection::sendEvents(
Mathias Agopiancf510012010-07-22 16:18:10 -0700859 sensors_event_t const* buffer, size_t numEvents,
860 sensors_event_t* scratch)
Mathias Agopianfc328812010-07-14 23:41:37 -0700861{
Mathias Agopiancf510012010-07-22 16:18:10 -0700862 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -0700863 size_t count = 0;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700864
Mathias Agopian3560fb22010-07-22 21:24:39 -0700865 if (scratch) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800866 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -0700867 size_t i=0;
868 while (i<numEvents) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700869 int32_t curr = buffer[i].sensor;
870 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
871 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
872 buffer[i].meta_data.sensor);
873 // Setting curr to the correct sensor to ensure the sensor events per connection are
874 // filtered correctly. buffer[i].sensor is zero for meta_data events.
875 curr = buffer[i].meta_data.sensor;
876 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700877 ssize_t index = mSensorInfo.indexOfKey(curr);
878 if (index >= 0 && mSensorInfo[index].mFirstFlushPending == true &&
879 buffer[i].type == SENSOR_TYPE_META_DATA) {
880 // This is the first flush before activate is called. Events can now be sent for
881 // this sensor on this connection.
882 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
883 buffer[i].meta_data.sensor);
884 mSensorInfo.editValueAt(index).mFirstFlushPending = false;
885 }
886 if (index >= 0 && mSensorInfo[index].mFirstFlushPending == false) {
Mathias Agopian3560fb22010-07-22 21:24:39 -0700887 do {
Aravind Akella4c8b9512013-09-05 17:03:38 -0700888 scratch[count++] = buffer[i++];
Aravind Akella724d91d2013-06-27 12:04:23 -0700889 } while ((i<numEvents) && ((buffer[i].sensor == curr) ||
890 (buffer[i].type == SENSOR_TYPE_META_DATA &&
891 buffer[i].meta_data.sensor == curr)));
Mathias Agopian3560fb22010-07-22 21:24:39 -0700892 } else {
893 i++;
894 }
Mathias Agopiancf510012010-07-22 16:18:10 -0700895 }
Mathias Agopian3560fb22010-07-22 21:24:39 -0700896 } else {
897 scratch = const_cast<sensors_event_t *>(buffer);
898 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -0700899 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700900
Aravind Akella4c8b9512013-09-05 17:03:38 -0700901 // Send pending flush events (if any) before sending events from the cache.
902 {
903 ASensorEvent flushCompleteEvent;
904 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
905 flushCompleteEvent.sensor = 0;
906 Mutex::Autolock _l(mConnectionLock);
907 // Loop through all the sensors for this connection and check if there are any pending
908 // flush complete events to be sent.
909 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
910 FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
911 while (flushInfo.mPendingFlushEventsToSend > 0) {
912 flushCompleteEvent.meta_data.sensor = mSensorInfo.keyAt(i);
913 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
914 if (size < 0) {
915 // ALOGW("dropping %d events on the floor", count);
Aravind Akellac551eac2013-10-14 17:04:42 -0700916 countFlushCompleteEventsLocked(scratch, count);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700917 return size;
918 }
919 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
920 flushCompleteEvent.meta_data.sensor);
921 flushInfo.mPendingFlushEventsToSend--;
922 }
923 }
924 }
925
Aravind Akellab4099e72013-10-15 15:43:10 -0700926 // Early return if there are no events for this connection.
927 if (count == 0) {
928 return status_t(NO_ERROR);
929 }
930
Mathias Agopian907103b2012-04-02 18:38:02 -0700931 // NOTE: ASensorEvent and sensors_event_t are the same type
932 ssize_t size = SensorEventQueue::write(mChannel,
933 reinterpret_cast<ASensorEvent const*>(scratch), count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700934 if (size == -EAGAIN) {
935 // the destination doesn't accept events anymore, it's probably
936 // full. For now, we just drop the events on the floor.
Aravind Akella4c8b9512013-09-05 17:03:38 -0700937 // ALOGW("dropping %d events on the floor", count);
Aravind Akellac551eac2013-10-14 17:04:42 -0700938 Mutex::Autolock _l(mConnectionLock);
939 countFlushCompleteEventsLocked(scratch, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700940 return size;
941 }
942
Jeff Brown1e0b1e82010-09-13 23:17:30 -0700943 return size < 0 ? status_t(size) : status_t(NO_ERROR);
Mathias Agopianfc328812010-07-14 23:41:37 -0700944}
945
Aravind Akellac551eac2013-10-14 17:04:42 -0700946void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
Aravind Akella4c8b9512013-09-05 17:03:38 -0700947 sensors_event_t* scratch, const int numEventsDropped) {
948 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700949 // Count flushComplete events in the events that are about to the dropped. These will be sent
950 // separately before the next batch of events.
951 for (int j = 0; j < numEventsDropped; ++j) {
952 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
953 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
954 flushInfo.mPendingFlushEventsToSend++;
955 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
956 flushInfo.mPendingFlushEventsToSend);
957 }
958 }
959 return;
960}
961
Mathias Agopianb3989272011-10-20 18:42:02 -0700962sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -0700963{
964 return mChannel;
965}
966
967status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -0700968 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
969 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700970{
971 status_t err;
972 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700973 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
974 reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700975 } else {
976 err = mService->disable(this, handle);
977 }
978 return err;
979}
980
981status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -0700982 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -0700983{
Aravind Akella724d91d2013-06-27 12:04:23 -0700984 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -0700985}
986
Aravind Akella701166d2013-10-08 14:59:26 -0700987status_t SensorService::SensorEventConnection::flush() {
Aravind Akellac551eac2013-10-14 17:04:42 -0700988 SensorDevice& dev(SensorDevice::getInstance());
989 const int halVersion = dev.getHalDeviceVersion();
Aravind Akella701166d2013-10-08 14:59:26 -0700990 Mutex::Autolock _l(mConnectionLock);
991 status_t err(NO_ERROR);
Aravind Akellac551eac2013-10-14 17:04:42 -0700992 // Loop through all sensors for this connection and call flush on each of them.
Aravind Akella701166d2013-10-08 14:59:26 -0700993 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
994 const int handle = mSensorInfo.keyAt(i);
Aravind Akellab4099e72013-10-15 15:43:10 -0700995 if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 || mService->isVirtualSensor(handle)) {
Aravind Akellac551eac2013-10-14 17:04:42 -0700996 // For older devices just increment pending flush count which will send a trivial
997 // flush complete event.
998 FlushInfo& flushInfo = mSensorInfo.editValueFor(handle);
999 flushInfo.mPendingFlushEventsToSend++;
1000 } else {
1001 status_t err_flush = mService->flushSensor(this, handle);
1002 if (err_flush != NO_ERROR) {
1003 ALOGE("Flush error handle=%d %s", handle, strerror(-err_flush));
1004 }
1005 err = (err_flush != NO_ERROR) ? err_flush : err;
Aravind Akella701166d2013-10-08 14:59:26 -07001006 }
Aravind Akella701166d2013-10-08 14:59:26 -07001007 }
1008 return err;
Aravind Akella724d91d2013-06-27 12:04:23 -07001009}
Aravind Akella4c8b9512013-09-05 17:03:38 -07001010
Mathias Agopianfc328812010-07-14 23:41:37 -07001011// ---------------------------------------------------------------------------
1012}; // namespace android
1013