blob: 8831893b04d78d818b7e0193f6cddb6cd2595a3b [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080018#include <math.h>
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070019#include <stdint.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070020#include <sys/types.h>
Aravind Akella56ae4262014-07-10 16:01:10 -070021#include <sys/socket.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070022
Mathias Agopian33015422011-05-27 18:18:13 -070023#include <cutils/properties.h>
24
Mathias Agopianfc328812010-07-14 23:41:37 -070025#include <utils/SortedVector.h>
26#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Atomic.h>
29#include <utils/Errors.h>
30#include <utils/RefBase.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070031#include <utils/Singleton.h>
Mathias Agopianc4a930d2010-07-22 22:19:43 -070032#include <utils/String16.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070033
34#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070035#include <binder/IServiceManager.h>
Mathias Agopian1cb13462011-06-27 16:05:52 -070036#include <binder/PermissionCache.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070037
38#include <gui/ISensorServer.h>
39#include <gui/ISensorEventConnection.h>
Mathias Agopian907103b2012-04-02 18:38:02 -070040#include <gui/SensorEventQueue.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070041
42#include <hardware/sensors.h>
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070043#include <hardware_legacy/power.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070044
Mathias Agopian787ac1b2012-09-18 18:49:18 -070045#include "BatteryService.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070046#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080047#include "GravitySensor.h"
48#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070049#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080050#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070051#include "SensorFusion.h"
52#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070053
54namespace android {
55// ---------------------------------------------------------------------------
56
Mathias Agopian33015422011-05-27 18:18:13 -070057/*
58 * Notes:
59 *
60 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070061 * - run mag sensor from time to time to force calibration
62 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
63 *
64 */
65
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070066const char* SensorService::WAKE_LOCK_NAME = "SensorService";
67
Mathias Agopianfc328812010-07-14 23:41:37 -070068SensorService::SensorService()
Aravind Akella8a969552014-09-28 17:52:41 -070069 : mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED),
70 mWakeLockAcquired(false)
Mathias Agopianfc328812010-07-14 23:41:37 -070071{
72}
73
74void SensorService::onFirstRef()
75{
Steve Blocka5512372011-12-20 16:23:08 +000076 ALOGD("nuSensorService starting...");
Mathias Agopian50df2952010-07-19 19:09:10 -070077
Mathias Agopianf001c922010-11-11 17:58:51 -080078 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070079
Mathias Agopianf001c922010-11-11 17:58:51 -080080 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080081 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070082 ssize_t count = dev.getSensorList(&list);
83 if (count > 0) {
84 ssize_t orientationIndex = -1;
85 bool hasGyro = false;
86 uint32_t virtualSensorsNeeds =
87 (1<<SENSOR_TYPE_GRAVITY) |
88 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
89 (1<<SENSOR_TYPE_ROTATION_VECTOR);
90
91 mLastEventSeen.setCapacity(count);
92 for (ssize_t i=0 ; i<count ; i++) {
93 registerSensor( new HardwareSensor(list[i]) );
94 switch (list[i].type) {
95 case SENSOR_TYPE_ORIENTATION:
96 orientationIndex = i;
97 break;
98 case SENSOR_TYPE_GYROSCOPE:
Mathias Agopian03193062013-05-10 19:32:39 -070099 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700100 hasGyro = true;
101 break;
102 case SENSOR_TYPE_GRAVITY:
103 case SENSOR_TYPE_LINEAR_ACCELERATION:
104 case SENSOR_TYPE_ROTATION_VECTOR:
105 virtualSensorsNeeds &= ~(1<<list[i].type);
106 break;
107 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700108 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700109
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700110 // it's safe to instantiate the SensorFusion object here
111 // (it wants to be instantiated after h/w sensors have been
112 // registered)
113 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700114
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700115 // build the sensor list returned to users
116 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700117
118 if (hasGyro) {
Mathias Agopian03193062013-05-10 19:32:39 -0700119 Sensor aSensor;
120
121 // Add Android virtual sensors if they're not already
122 // available in the HAL
123
124 aSensor = registerVirtualSensor( new RotationVectorSensor() );
125 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
126 mUserSensorList.add(aSensor);
127 }
128
129 aSensor = registerVirtualSensor( new GravitySensor(list, count) );
130 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
131 mUserSensorList.add(aSensor);
132 }
133
134 aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
135 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
136 mUserSensorList.add(aSensor);
137 }
138
139 aSensor = registerVirtualSensor( new OrientationSensor() );
140 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
141 // if we are doing our own rotation-vector, also add
142 // the orientation sensor and remove the HAL provided one.
143 mUserSensorList.replaceAt(aSensor, orientationIndex);
144 }
145
Mathias Agopian33264862012-06-28 19:46:54 -0700146 // virtual debugging sensors are not added to mUserSensorList
Mathias Agopian03193062013-05-10 19:32:39 -0700147 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33264862012-06-28 19:46:54 -0700148 registerVirtualSensor( new GyroDriftSensor() );
149 }
150
Mathias Agopian33264862012-06-28 19:46:54 -0700151 // debugging sensor list
Mathias Agopian03193062013-05-10 19:32:39 -0700152 mUserSensorListDebug = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700153
Aravind Akella5466c3d2014-08-22 16:11:10 -0700154 // Check if the device really supports batching by looking at the FIFO event
155 // counts for each sensor.
156 bool batchingSupported = false;
157 for (int i = 0; i < mSensorList.size(); ++i) {
158 if (mSensorList[i].getFifoMaxEventCount() > 0) {
159 batchingSupported = true;
160 break;
161 }
162 }
163
164 if (batchingSupported) {
165 // Increase socket buffer size to a max of 100 KB for batching capabilities.
166 mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
167 } else {
168 mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
169 }
170
171 // Compare the socketBufferSize value against the system limits and limit
172 // it to maxSystemSocketBufferSize if necessary.
Aravind Akella4c8b9512013-09-05 17:03:38 -0700173 FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r");
174 char line[128];
175 if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
176 line[sizeof(line) - 1] = '\0';
Aravind Akella5466c3d2014-08-22 16:11:10 -0700177 size_t maxSystemSocketBufferSize;
178 sscanf(line, "%zu", &maxSystemSocketBufferSize);
179 if (mSocketBufferSize > maxSystemSocketBufferSize) {
180 mSocketBufferSize = maxSystemSocketBufferSize;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700181 }
182 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700183 if (fp) {
184 fclose(fp);
185 }
186
Aravind Akella9a844cf2014-02-11 18:58:52 -0800187 mWakeLockAcquired = false;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700188 run("SensorService", PRIORITY_URGENT_DISPLAY);
Aravind Akella56ae4262014-07-10 16:01:10 -0700189 mLooper = new Looper(false);
Aravind Akella8493b792014-09-08 15:45:47 -0700190
191 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
192 mSensorEventBuffer = new sensors_event_t[minBufferSize];
193 mSensorEventScratch = new sensors_event_t[minBufferSize];
194 mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize];
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700195 mInitCheck = NO_ERROR;
196 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700197 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700198}
199
Mathias Agopian03193062013-05-10 19:32:39 -0700200Sensor SensorService::registerSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800201{
202 sensors_event_t event;
203 memset(&event, 0, sizeof(event));
204
205 const Sensor sensor(s->getSensor());
206 // add to the sensor list (returned to clients)
207 mSensorList.add(sensor);
208 // add to our handle->SensorInterface mapping
209 mSensorMap.add(sensor.getHandle(), s);
210 // create an entry in the mLastEventSeen array
211 mLastEventSeen.add(sensor.getHandle(), event);
Mathias Agopian03193062013-05-10 19:32:39 -0700212
213 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800214}
215
Mathias Agopian03193062013-05-10 19:32:39 -0700216Sensor SensorService::registerVirtualSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800217{
Mathias Agopian03193062013-05-10 19:32:39 -0700218 Sensor sensor = registerSensor(s);
Mathias Agopianf001c922010-11-11 17:58:51 -0800219 mVirtualSensorList.add( s );
Mathias Agopian03193062013-05-10 19:32:39 -0700220 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800221}
222
Mathias Agopianfc328812010-07-14 23:41:37 -0700223SensorService::~SensorService()
224{
Mathias Agopianf001c922010-11-11 17:58:51 -0800225 for (size_t i=0 ; i<mSensorMap.size() ; i++)
226 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700227}
228
Mathias Agopian1cb13462011-06-27 16:05:52 -0700229static const String16 sDump("android.permission.DUMP");
230
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700231status_t SensorService::dump(int fd, const Vector<String16>& /*args*/)
Mathias Agopianfc328812010-07-14 23:41:37 -0700232{
Mathias Agopianfc328812010-07-14 23:41:37 -0700233 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700234 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700235 result.appendFormat("Permission Denial: "
Aravind Akella70018042014-04-07 22:52:37 +0000236 "can't dump SensorService from pid=%d, uid=%d\n",
Mathias Agopianfc328812010-07-14 23:41:37 -0700237 IPCThreadState::self()->getCallingPid(),
238 IPCThreadState::self()->getCallingUid());
Mathias Agopianfc328812010-07-14 23:41:37 -0700239 } else {
240 Mutex::Autolock _l(mLock);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700241 result.append("Sensor List:\n");
Mathias Agopian3560fb22010-07-22 21:24:39 -0700242 for (size_t i=0 ; i<mSensorList.size() ; i++) {
243 const Sensor& s(mSensorList[i]);
244 const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle()));
Mathias Agopianba02cd22013-07-03 16:20:57 -0700245 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700246 "%-15s| %-10s| %-20s| 0x%08x | \"%s\" | type=%d |",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700247 s.getName().string(),
248 s.getVendor().string(),
Aravind Akella70018042014-04-07 22:52:37 +0000249 s.getStringType().string(),
250 s.getHandle(),
Aravind Akella6c2664a2014-08-13 12:24:50 -0700251 s.getRequiredPermission().string(),
252 s.getType());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700253
Aravind Akella0e025c52014-06-03 19:19:57 -0700254 const int reportingMode = s.getReportingMode();
255 if (reportingMode == AREPORTING_MODE_CONTINUOUS) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700256 result.append(" continuous | ");
Aravind Akella0e025c52014-06-03 19:19:57 -0700257 } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700258 result.append(" on-change | ");
Aravind Akella0e025c52014-06-03 19:19:57 -0700259 } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700260 result.append(" one-shot | ");
Mathias Agopianba02cd22013-07-03 16:20:57 -0700261 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700262 result.append(" special-trigger | ");
263 }
264
265 if (s.getMaxDelay() > 0) {
266 result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay());
267 } else {
268 result.appendFormat("maxDelay=%dus |", s.getMaxDelay());
Mathias Agopianba02cd22013-07-03 16:20:57 -0700269 }
Aravind Akella0e025c52014-06-03 19:19:57 -0700270
271 if (s.getMinDelay() > 0) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700272 result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay());
Aravind Akella0e025c52014-06-03 19:19:57 -0700273 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700274 result.appendFormat("minDelay=%dus |", s.getMinDelay());
Aravind Akella0e025c52014-06-03 19:19:57 -0700275 }
276
Aravind Akella724d91d2013-06-27 12:04:23 -0700277 if (s.getFifoMaxEventCount() > 0) {
Aravind Akella70018042014-04-07 22:52:37 +0000278 result.appendFormat("FifoMax=%d events | ",
279 s.getFifoMaxEventCount());
Aravind Akella724d91d2013-06-27 12:04:23 -0700280 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700281 result.append("no batching | ");
282 }
283
284 if (s.isWakeUpSensor()) {
285 result.appendFormat("wakeUp | ");
286 } else {
287 result.appendFormat("non-wakeUp | ");
Aravind Akella724d91d2013-06-27 12:04:23 -0700288 }
Mathias Agopianba02cd22013-07-03 16:20:57 -0700289
290 switch (s.getType()) {
291 case SENSOR_TYPE_ROTATION_VECTOR:
292 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
293 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700294 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
295 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700296 break;
297 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
298 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
299 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700300 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
301 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5],
302 e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700303 break;
304 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
305 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700306 "last=<%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
307 e.data[0], e.data[1], e.data[2], e.data[3], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700308 break;
309 case SENSOR_TYPE_SIGNIFICANT_MOTION:
310 case SENSOR_TYPE_STEP_DETECTOR:
Aravind Akella6c2664a2014-08-13 12:24:50 -0700311 result.appendFormat( "last=<%f %" PRId64 ">\n", e.data[0], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700312 break;
313 case SENSOR_TYPE_STEP_COUNTER:
Aravind Akella6c2664a2014-08-13 12:24:50 -0700314 result.appendFormat( "last=<%" PRIu64 ", %" PRId64 ">\n", e.u64.step_counter,
315 e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700316 break;
317 default:
318 // default to 3 values
319 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700320 "last=<%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
321 e.data[0], e.data[1], e.data[2], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700322 break;
323 }
Aravind Akella6c2664a2014-08-13 12:24:50 -0700324 result.append("\n");
Mathias Agopianba02cd22013-07-03 16:20:57 -0700325 }
326 SensorFusion::getInstance().dump(result);
327 SensorDevice::getInstance().dump(result);
328
Mathias Agopianba02cd22013-07-03 16:20:57 -0700329 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700330 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700331 int handle = mActiveSensors.keyAt(i);
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700332 result.appendFormat("%s (handle=0x%08x, connections=%zu)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700333 getSensorName(handle).string(),
334 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700335 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700336 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700337
Aravind Akella5466c3d2014-08-22 16:11:10 -0700338 result.appendFormat("Socket Buffer size = %d events\n",
Aravind Akella6c2664a2014-08-13 12:24:50 -0700339 mSocketBufferSize/sizeof(sensors_event_t));
Aravind Akella9a844cf2014-02-11 18:58:52 -0800340 result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : "not held");
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700341 result.appendFormat("%zd active connections\n", mActiveConnections.size());
Aravind Akella4c8b9512013-09-05 17:03:38 -0700342
343 for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
344 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
345 if (connection != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700346 result.appendFormat("Connection Number: %zu \n", i);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700347 connection->dump(result);
348 }
349 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700350 }
351 write(fd, result.string(), result.size());
352 return NO_ERROR;
353}
354
Aravind Akella9a844cf2014-02-11 18:58:52 -0800355void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700356 sensors_event_t const* buffer, const int count) {
357 for (int i=0 ; i<count ; i++) {
358 int handle = buffer[i].sensor;
Aravind Akella8493b792014-09-08 15:45:47 -0700359 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
360 handle = buffer[i].meta_data.sensor;
361 }
Aravind Akella0e025c52014-06-03 19:19:57 -0700362 if (connection->hasSensor(handle)) {
363 SensorInterface* sensor = mSensorMap.valueFor(handle);
364 // If this buffer has an event from a one_shot sensor and this connection is registered
365 // for this particular one_shot sensor, try cleaning up the connection.
366 if (sensor != NULL &&
367 sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
368 sensor->autoDisable(connection.get(), handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800369 cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700370 }
371 }
372 }
373}
374
Mathias Agopianfc328812010-07-14 23:41:37 -0700375bool SensorService::threadLoop()
376{
Steve Blocka5512372011-12-20 16:23:08 +0000377 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700378
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700379 // each virtual sensor could generate an event per "real" event, that's why we need
380 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
381 // in practice, this is too aggressive, but guaranteed to be enough.
382 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
383 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
384
Mathias Agopianf001c922010-11-11 17:58:51 -0800385 SensorDevice& device(SensorDevice::getInstance());
386 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700387
Aravind Akella56ae4262014-07-10 16:01:10 -0700388 SensorEventAckReceiver sender(this);
389 sender.run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700390 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700391 do {
Aravind Akella8493b792014-09-08 15:45:47 -0700392 ssize_t count = device.poll(mSensorEventBuffer, numEventMax);
393 if (count < 0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000394 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700395 break;
396 }
Aravind Akella56ae4262014-07-10 16:01:10 -0700397
398 // Reset sensors_event_t.flags to zero for all events in the buffer.
399 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700400 mSensorEventBuffer[i].flags = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -0700401 }
Aravind Akellae148bc22014-09-24 22:12:58 -0700402
403 // Make a copy of the connection vector as some connections may be removed during the
404 // course of this loop (especially when one-shot sensor events are present in the
405 // sensor_event buffer). Promote all connections to StrongPointers before the lock is
406 // acquired. If the destructor of the sp gets called when the lock is acquired, it may
407 // result in a deadlock as ~SensorEventConnection() needs to acquire mLock again for
408 // cleanup. So copy all the strongPointers to a vector before the lock is acquired.
409 SortedVector< sp<SensorEventConnection> > activeConnections;
410 {
411 Mutex::Autolock _l(mLock);
412 for (size_t i=0 ; i < mActiveConnections.size(); ++i) {
413 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
414 if (connection != 0) {
415 activeConnections.add(connection);
416 }
417 }
418 }
419
Aravind Akella9a844cf2014-02-11 18:58:52 -0800420 Mutex::Autolock _l(mLock);
421 // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The
422 // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock,
423 // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should
424 // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and
425 // releasing the wakelock.
426 bool bufferHasWakeUpEvent = false;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700427 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700428 if (isWakeUpSensorEvent(mSensorEventBuffer[i])) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800429 bufferHasWakeUpEvent = true;
430 break;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700431 }
432 }
433
Aravind Akella9a844cf2014-02-11 18:58:52 -0800434 if (bufferHasWakeUpEvent && !mWakeLockAcquired) {
435 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
436 mWakeLockAcquired = true;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800437 }
Aravind Akella8493b792014-09-08 15:45:47 -0700438 recordLastValueLocked(mSensorEventBuffer, count);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800439
Mathias Agopianf001c922010-11-11 17:58:51 -0800440 // handle virtual sensors
441 if (count && vcount) {
Aravind Akella8493b792014-09-08 15:45:47 -0700442 sensors_event_t const * const event = mSensorEventBuffer;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800443 const size_t activeVirtualSensorCount = mActiveVirtualSensors.size();
Mathias Agopianf001c922010-11-11 17:58:51 -0800444 if (activeVirtualSensorCount) {
445 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700446 SensorFusion& fusion(SensorFusion::getInstance());
447 if (fusion.isEnabled()) {
448 for (size_t i=0 ; i<size_t(count) ; i++) {
449 fusion.process(event[i]);
450 }
451 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700452 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800453 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700454 if (count + k >= minBufferSize) {
455 ALOGE("buffer too small to hold all events: "
Mark Salyzyndb458612014-06-10 14:50:02 -0700456 "count=%zd, k=%zu, size=%zu",
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700457 count, k, minBufferSize);
458 break;
459 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800460 sensors_event_t out;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800461 SensorInterface* si = mActiveVirtualSensors.valueAt(j);
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700462 if (si->process(&out, event[i])) {
Aravind Akella8493b792014-09-08 15:45:47 -0700463 mSensorEventBuffer[count + k] = out;
Mathias Agopianf001c922010-11-11 17:58:51 -0800464 k++;
465 }
466 }
467 }
468 if (k) {
469 // record the last synthesized values
Aravind Akella8493b792014-09-08 15:45:47 -0700470 recordLastValueLocked(&mSensorEventBuffer[count], k);
Mathias Agopianf001c922010-11-11 17:58:51 -0800471 count += k;
472 // sort the buffer by time-stamps
Aravind Akella8493b792014-09-08 15:45:47 -0700473 sortEventBuffer(mSensorEventBuffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700474 }
475 }
476 }
477
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700478 // handle backward compatibility for RotationVector sensor
479 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
480 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700481 if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700482 // All the 4 components of the quaternion should be available
483 // No heading accuracy. Set it to -1
Aravind Akella8493b792014-09-08 15:45:47 -0700484 mSensorEventBuffer[i].data[4] = -1;
485 }
486 }
487 }
488
489 // Map flush_complete_events in the buffer to SensorEventConnections which called
490 // flush on the hardware sensor. mapFlushEventsToConnections[i] will be the
491 // SensorEventConnection mapped to the corresponding flush_complete_event in
492 // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise).
493 for (int i = 0; i < count; ++i) {
494 mMapFlushEventsToConnections[i] = NULL;
495 if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) {
496 const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor;
497 SensorRecord* rec = mActiveSensors.valueFor(sensor_handle);
498 if (rec != NULL) {
499 mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection();
500 rec->removeFirstPendingFlushConnection();
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700501 }
502 }
503 }
504
Aravind Akella9a844cf2014-02-11 18:58:52 -0800505 // Send our events to clients. Check the state of wake lock for each client and release the
506 // lock if none of the clients need it.
507 bool needsWakeLock = false;
Aravind Akella8493b792014-09-08 15:45:47 -0700508 size_t numConnections = activeConnections.size();
509 for (size_t i=0 ; i < numConnections; ++i) {
Aravind Akellae148bc22014-09-24 22:12:58 -0700510 if (activeConnections[i] != 0) {
511 activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch,
Aravind Akella8493b792014-09-08 15:45:47 -0700512 mMapFlushEventsToConnections);
Aravind Akellae148bc22014-09-24 22:12:58 -0700513 needsWakeLock |= activeConnections[i]->needsWakeLock();
Aravind Akella8493b792014-09-08 15:45:47 -0700514 // If the connection has one-shot sensors, it may be cleaned up after first trigger.
515 // Early check for one-shot sensors.
Aravind Akellae148bc22014-09-24 22:12:58 -0700516 if (activeConnections[i]->hasOneShotSensors()) {
517 cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer,
518 count);
Aravind Akella8493b792014-09-08 15:45:47 -0700519 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800520 }
521 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700522
Aravind Akella9a844cf2014-02-11 18:58:52 -0800523 if (mWakeLockAcquired && !needsWakeLock) {
524 release_wake_lock(WAKE_LOCK_NAME);
525 mWakeLockAcquired = false;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800526 }
Aravind Akella8493b792014-09-08 15:45:47 -0700527 } while (!Thread::exitPending());
Mathias Agopianfc328812010-07-14 23:41:37 -0700528
Steve Block3c20fbe2012-01-05 23:22:43 +0000529 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800530 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700531 return false;
532}
533
Aravind Akella56ae4262014-07-10 16:01:10 -0700534sp<Looper> SensorService::getLooper() const {
535 return mLooper;
536}
537
538bool SensorService::SensorEventAckReceiver::threadLoop() {
539 ALOGD("new thread SensorEventAckReceiver");
540 do {
541 sp<Looper> looper = mService->getLooper();
542 looper->pollOnce(-1);
543 } while(!Thread::exitPending());
544 return false;
545}
546
Aravind Akella9a844cf2014-02-11 18:58:52 -0800547void SensorService::recordLastValueLocked(
Aravind Akella4b847042014-03-03 19:02:46 -0800548 const sensors_event_t* buffer, size_t count) {
Aravind Akella4b847042014-03-03 19:02:46 -0800549 const sensors_event_t* last = NULL;
550 for (size_t i = 0; i < count; i++) {
551 const sensors_event_t* event = &buffer[i];
552 if (event->type != SENSOR_TYPE_META_DATA) {
553 if (last && event->sensor != last->sensor) {
554 mLastEventSeen.editValueFor(last->sensor) = *last;
555 }
556 last = event;
Mathias Agopian94e8f682010-11-10 17:50:28 -0800557 }
558 }
Aravind Akella4b847042014-03-03 19:02:46 -0800559 if (last) {
560 mLastEventSeen.editValueFor(last->sensor) = *last;
561 }
Mathias Agopian94e8f682010-11-10 17:50:28 -0800562}
563
Mathias Agopianf001c922010-11-11 17:58:51 -0800564void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
565{
566 struct compar {
567 static int cmp(void const* lhs, void const* rhs) {
568 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
569 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700570 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800571 }
572 };
573 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
574}
575
Mathias Agopian5d270722010-07-19 15:20:39 -0700576String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700577 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700578 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700579 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700580 if (sensor.getHandle() == handle) {
581 return sensor.getName();
582 }
583 }
584 String8 result("unknown");
585 return result;
586}
587
Aravind Akellab4099e72013-10-15 15:43:10 -0700588bool SensorService::isVirtualSensor(int handle) const {
589 SensorInterface* sensor = mSensorMap.valueFor(handle);
590 return sensor->isVirtual();
591}
592
Aravind Akella9a844cf2014-02-11 18:58:52 -0800593bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const {
Sean Wan7869e222014-07-14 17:07:33 -0700594 int handle = event.sensor;
595 if (event.type == SENSOR_TYPE_META_DATA) {
596 handle = event.meta_data.sensor;
597 }
598 SensorInterface* sensor = mSensorMap.valueFor(handle);
599 return sensor != NULL && sensor->getSensor().isWakeUpSensor();
Aravind Akella9a844cf2014-02-11 18:58:52 -0800600}
601
Aravind Akella6c2664a2014-08-13 12:24:50 -0700602
603SensorService::SensorRecord * SensorService::getSensorRecord(int handle) {
604 return mActiveSensors.valueFor(handle);
605}
606
Mathias Agopianfc328812010-07-14 23:41:37 -0700607Vector<Sensor> SensorService::getSensorList()
608{
Mathias Agopian33264862012-06-28 19:46:54 -0700609 char value[PROPERTY_VALUE_MAX];
610 property_get("debug.sensors", value, "0");
Aravind Akella70018042014-04-07 22:52:37 +0000611 const Vector<Sensor>& initialSensorList = (atoi(value)) ?
612 mUserSensorListDebug : mUserSensorList;
613 Vector<Sensor> accessibleSensorList;
614 for (size_t i = 0; i < initialSensorList.size(); i++) {
615 Sensor sensor = initialSensorList[i];
616 if (canAccessSensor(sensor)) {
617 accessibleSensorList.add(sensor);
618 } else {
619 String8 infoMessage;
620 infoMessage.appendFormat(
621 "Skipped sensor %s because it requires permission %s",
622 sensor.getName().string(),
623 sensor.getRequiredPermission().string());
624 ALOGI(infoMessage.string());
625 }
Mathias Agopian33264862012-06-28 19:46:54 -0700626 }
Aravind Akella70018042014-04-07 22:52:37 +0000627 return accessibleSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700628}
629
630sp<ISensorEventConnection> SensorService::createSensorEventConnection()
631{
Mathias Agopian5307d172012-09-18 17:02:43 -0700632 uid_t uid = IPCThreadState::self()->getCallingUid();
633 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700634 return result;
635}
636
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800637void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700638{
639 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800640 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700641 size_t size = mActiveSensors.size();
Mark Salyzyndb458612014-06-10 14:50:02 -0700642 ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700643 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800644 int handle = mActiveSensors.keyAt(i);
645 if (c->hasSensor(handle)) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700646 ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800647 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000648 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800649 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800650 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800651 }
Aravind Akella8a969552014-09-28 17:52:41 -0700652 c->removeSensor(handle);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800653 }
654 SensorRecord* rec = mActiveSensors.valueAt(i);
Mark Salyzyndb458612014-06-10 14:50:02 -0700655 ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000656 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700657 "removing connection %p for sensor[%zu].handle=0x%08x",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700658 c, i, handle);
659
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800660 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000661 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700662 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800663 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700664 delete rec;
665 size--;
666 } else {
667 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700668 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700669 }
Aravind Akella8a969552014-09-28 17:52:41 -0700670 c->updateLooperRegistration(mLooper);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700671 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700672 BatteryService::cleanup(c->getUid());
Aravind Akella9a844cf2014-02-11 18:58:52 -0800673 if (c->needsWakeLock()) {
674 checkWakeLockStateLocked();
675 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700676}
677
Aravind Akella70018042014-04-07 22:52:37 +0000678Sensor SensorService::getSensorFromHandle(int handle) const {
679 return mSensorMap.valueFor(handle)->getSensor();
680}
681
Mathias Agopianfc328812010-07-14 23:41:37 -0700682status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700683 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700684{
Mathias Agopian50df2952010-07-19 19:09:10 -0700685 if (mInitCheck != NO_ERROR)
686 return mInitCheck;
687
Mathias Agopianf001c922010-11-11 17:58:51 -0800688 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700689 if (sensor == NULL) {
690 return BAD_VALUE;
691 }
Aravind Akella70018042014-04-07 22:52:37 +0000692
693 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried enabling")) {
694 return BAD_VALUE;
695 }
696
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700697 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700698 SensorRecord* rec = mActiveSensors.valueFor(handle);
699 if (rec == 0) {
700 rec = new SensorRecord(connection);
701 mActiveSensors.add(handle, rec);
702 if (sensor->isVirtual()) {
703 mActiveVirtualSensors.add(handle, sensor);
704 }
705 } else {
706 if (rec->addConnection(connection)) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800707 // this sensor is already activated, but we are adding a connection that uses it.
708 // Immediately send down the last known value of the requested sensor if it's not a
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700709 // "continuous" sensor.
Aravind Akella0e025c52014-06-03 19:19:57 -0700710 if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800711 // NOTE: The wake_up flag of this event may get set to
712 // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event.
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700713 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
714 if (event.version == sizeof(sensors_event_t)) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800715 if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) {
716 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
717 mWakeLockAcquired = true;
718 ALOGD_IF(DEBUG_CONNECTIONS, "acquired wakelock for on_change sensor %s",
719 WAKE_LOCK_NAME);
720 }
721 connection->sendEvents(&event, 1, NULL);
722 if (!connection->needsWakeLock() && mWakeLockAcquired) {
723 checkWakeLockStateLocked();
724 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700725 }
726 }
727 }
728 }
729
730 if (connection->addSensor(handle)) {
731 BatteryService::enableSensor(connection->getUid(), handle);
732 // the sensor was added (which means it wasn't already there)
733 // so, see if this connection becomes active
734 if (mActiveConnections.indexOf(connection) < 0) {
735 mActiveConnections.add(connection);
736 }
737 } else {
738 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
739 handle, connection.get());
740 }
741
Aravind Akella724d91d2013-06-27 12:04:23 -0700742 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
743 if (samplingPeriodNs < minDelayNs) {
744 samplingPeriodNs = minDelayNs;
745 }
746
Aravind Akella6c2664a2014-08-13 12:24:50 -0700747 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d"
748 "rate=%" PRId64 " timeout== %" PRId64"",
Aravind Akella724d91d2013-06-27 12:04:23 -0700749 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
750
751 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
752 maxBatchReportLatencyNs);
Aravind Akella6c2664a2014-08-13 12:24:50 -0700753
Aravind Akella5466c3d2014-08-22 16:11:10 -0700754 // Call flush() before calling activate() on the sensor. Wait for a first flush complete
755 // event before sending events on this connection. Ignore one-shot sensors which don't
756 // support flush(). Also if this sensor isn't already active, don't call flush().
757 if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT &&
758 rec->getNumConnections() > 1) {
759 connection->setFirstFlushPending(handle, true);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700760 status_t err_flush = sensor->flush(connection.get(), handle);
Aravind Akella5466c3d2014-08-22 16:11:10 -0700761 // Flush may return error if the underlying h/w sensor uses an older HAL.
Aravind Akella6c2664a2014-08-13 12:24:50 -0700762 if (err_flush == NO_ERROR) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700763 rec->addPendingFlushConnection(connection.get());
Aravind Akella5466c3d2014-08-22 16:11:10 -0700764 } else {
765 connection->setFirstFlushPending(handle, false);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700766 }
767 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700768
769 if (err == NO_ERROR) {
770 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
771 err = sensor->activate(connection.get(), true);
772 }
773
Aravind Akella8a969552014-09-28 17:52:41 -0700774 if (err == NO_ERROR) {
775 connection->updateLooperRegistration(mLooper);
Aravind Akella56ae4262014-07-10 16:01:10 -0700776 }
777
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700778 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700779 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700780 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700781 }
782 return err;
783}
784
785status_t SensorService::disable(const sp<SensorEventConnection>& connection,
786 int handle)
787{
Mathias Agopian50df2952010-07-19 19:09:10 -0700788 if (mInitCheck != NO_ERROR)
789 return mInitCheck;
790
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700791 Mutex::Autolock _l(mLock);
792 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700793 if (err == NO_ERROR) {
794 SensorInterface* sensor = mSensorMap.valueFor(handle);
795 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
796 }
797 return err;
798}
799
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700800status_t SensorService::cleanupWithoutDisable(
801 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700802 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700803 return cleanupWithoutDisableLocked(connection, handle);
804}
805
806status_t SensorService::cleanupWithoutDisableLocked(
807 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700808 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700809 if (rec) {
810 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700811 if (connection->removeSensor(handle)) {
812 BatteryService::disableSensor(connection->getUid(), handle);
813 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700814 if (connection->hasAnySensor() == false) {
Aravind Akella8a969552014-09-28 17:52:41 -0700815 connection->updateLooperRegistration(mLooper);
Mathias Agopianfc328812010-07-14 23:41:37 -0700816 mActiveConnections.remove(connection);
817 }
818 // see if this sensor becomes inactive
819 if (rec->removeConnection(connection)) {
820 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800821 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700822 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700823 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700824 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700825 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700826 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700827}
828
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700829status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700830 int handle, nsecs_t ns)
831{
Mathias Agopian50df2952010-07-19 19:09:10 -0700832 if (mInitCheck != NO_ERROR)
833 return mInitCheck;
834
Mathias Agopianae09d652011-11-01 17:37:49 -0700835 SensorInterface* sensor = mSensorMap.valueFor(handle);
836 if (!sensor)
837 return BAD_VALUE;
838
Aravind Akella70018042014-04-07 22:52:37 +0000839 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried configuring")) {
840 return BAD_VALUE;
841 }
842
Mathias Agopian1cd70002010-07-21 15:59:50 -0700843 if (ns < 0)
844 return BAD_VALUE;
845
Mathias Agopian62569ec2011-11-07 21:21:47 -0800846 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
847 if (ns < minDelayNs) {
848 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700849 }
850
Mathias Agopianf001c922010-11-11 17:58:51 -0800851 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700852}
853
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700854status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection) {
Aravind Akella70018042014-04-07 22:52:37 +0000855 if (mInitCheck != NO_ERROR) return mInitCheck;
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700856 SensorDevice& dev(SensorDevice::getInstance());
857 const int halVersion = dev.getHalDeviceVersion();
858 status_t err(NO_ERROR);
859 Mutex::Autolock _l(mLock);
860 // Loop through all sensors for this connection and call flush on each of them.
861 for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) {
862 const int handle = connection->mSensorInfo.keyAt(i);
863 SensorInterface* sensor = mSensorMap.valueFor(handle);
864 if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
865 ALOGE("flush called on a one-shot sensor");
866 err = INVALID_OPERATION;
867 continue;
868 }
Aravind Akella8493b792014-09-08 15:45:47 -0700869 if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) {
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700870 // For older devices just increment pending flush count which will send a trivial
871 // flush complete event.
Aravind Akella8a969552014-09-28 17:52:41 -0700872 connection->incrementPendingFlushCount(handle);
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700873 } else {
874 status_t err_flush = sensor->flush(connection.get(), handle);
875 if (err_flush == NO_ERROR) {
876 SensorRecord* rec = mActiveSensors.valueFor(handle);
877 if (rec != NULL) rec->addPendingFlushConnection(connection);
878 }
879 err = (err_flush != NO_ERROR) ? err_flush : err;
880 }
Aravind Akella70018042014-04-07 22:52:37 +0000881 }
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700882 return err;
Aravind Akella724d91d2013-06-27 12:04:23 -0700883}
Aravind Akella70018042014-04-07 22:52:37 +0000884
Aravind Akella70018042014-04-07 22:52:37 +0000885bool SensorService::canAccessSensor(const Sensor& sensor) {
Aravind Akella56ae4262014-07-10 16:01:10 -0700886 return (sensor.getRequiredPermission().isEmpty()) ||
887 PermissionCache::checkCallingPermission(String16(sensor.getRequiredPermission()));
Aravind Akella70018042014-04-07 22:52:37 +0000888}
889
890bool SensorService::verifyCanAccessSensor(const Sensor& sensor, const char* operation) {
891 if (canAccessSensor(sensor)) {
892 return true;
893 } else {
894 String8 errorMessage;
895 errorMessage.appendFormat(
896 "%s a sensor (%s) without holding its required permission: %s",
897 operation,
898 sensor.getName().string(),
899 sensor.getRequiredPermission().string());
900 return false;
901 }
902}
903
Aravind Akella9a844cf2014-02-11 18:58:52 -0800904void SensorService::checkWakeLockState() {
905 Mutex::Autolock _l(mLock);
906 checkWakeLockStateLocked();
907}
908
909void SensorService::checkWakeLockStateLocked() {
910 if (!mWakeLockAcquired) {
911 return;
912 }
913 bool releaseLock = true;
914 for (size_t i=0 ; i<mActiveConnections.size() ; i++) {
915 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
916 if (connection != 0) {
917 if (connection->needsWakeLock()) {
918 releaseLock = false;
919 break;
920 }
921 }
922 }
923 if (releaseLock) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800924 release_wake_lock(WAKE_LOCK_NAME);
925 mWakeLockAcquired = false;
926 }
927}
Aravind Akella6c2664a2014-08-13 12:24:50 -0700928
Mathias Agopianfc328812010-07-14 23:41:37 -0700929// ---------------------------------------------------------------------------
Mathias Agopianfc328812010-07-14 23:41:37 -0700930SensorService::SensorRecord::SensorRecord(
931 const sp<SensorEventConnection>& connection)
932{
933 mConnections.add(connection);
934}
935
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700936bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700937 const sp<SensorEventConnection>& connection)
938{
939 if (mConnections.indexOf(connection) < 0) {
940 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700941 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700942 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700943 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700944}
945
946bool SensorService::SensorRecord::removeConnection(
947 const wp<SensorEventConnection>& connection)
948{
949 ssize_t index = mConnections.indexOf(connection);
950 if (index >= 0) {
951 mConnections.removeItemsAt(index, 1);
952 }
Aravind Akella6c2664a2014-08-13 12:24:50 -0700953 // Remove this connections from the queue of flush() calls made on this sensor.
954 for (Vector< wp<SensorEventConnection> >::iterator it =
955 mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) {
Aravind Akella8a969552014-09-28 17:52:41 -0700956
Aravind Akella6c2664a2014-08-13 12:24:50 -0700957 if (it->unsafe_get() == connection.unsafe_get()) {
958 it = mPendingFlushConnections.erase(it);
959 } else {
960 ++it;
961 }
962 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700963 return mConnections.size() ? false : true;
964}
965
Aravind Akella6c2664a2014-08-13 12:24:50 -0700966void SensorService::SensorRecord::addPendingFlushConnection(
967 const sp<SensorEventConnection>& connection) {
968 mPendingFlushConnections.add(connection);
969}
970
971void SensorService::SensorRecord::removeFirstPendingFlushConnection() {
972 if (mPendingFlushConnections.size() > 0) {
973 mPendingFlushConnections.removeAt(0);
974 }
975}
976
977SensorService::SensorEventConnection *
978SensorService::SensorRecord::getFirstPendingFlushConnection() {
979 if (mPendingFlushConnections.size() > 0) {
980 return mPendingFlushConnections[0].unsafe_get();
981 }
982 return NULL;
983}
984
Mathias Agopianfc328812010-07-14 23:41:37 -0700985// ---------------------------------------------------------------------------
986
987SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700988 const sp<SensorService>& service, uid_t uid)
Aravind Akella8a969552014-09-28 17:52:41 -0700989 : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
990 mDead(false), mEventCache(NULL), mCacheSize(0), mMaxCacheSize(0) {
Aravind Akella5466c3d2014-08-22 16:11:10 -0700991 mChannel = new BitTube(mService->mSocketBufferSize);
Aravind Akella56ae4262014-07-10 16:01:10 -0700992#if DEBUG_CONNECTIONS
993 mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
Aravind Akellae74baf62014-08-21 12:28:35 -0700994 mTotalAcksNeeded = mTotalAcksReceived = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -0700995#endif
Mathias Agopianfc328812010-07-14 23:41:37 -0700996}
997
Aravind Akella56ae4262014-07-10 16:01:10 -0700998SensorService::SensorEventConnection::~SensorEventConnection() {
Steve Blocka5512372011-12-20 16:23:08 +0000999 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Aravind Akellae148bc22014-09-24 22:12:58 -07001000 mService->cleanupConnection(this);
Aravind Akella56ae4262014-07-10 16:01:10 -07001001 if (mEventCache != NULL) {
1002 delete mEventCache;
1003 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001004}
1005
Aravind Akella56ae4262014-07-10 16:01:10 -07001006void SensorService::SensorEventConnection::onFirstRef() {
1007 LooperCallback::onFirstRef();
Mathias Agopianfc328812010-07-14 23:41:37 -07001008}
1009
Aravind Akella9a844cf2014-02-11 18:58:52 -08001010bool SensorService::SensorEventConnection::needsWakeLock() {
1011 Mutex::Autolock _l(mConnectionLock);
Aravind Akella8a969552014-09-28 17:52:41 -07001012 return !mDead && mWakeLockRefCount > 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001013}
1014
Aravind Akella4c8b9512013-09-05 17:03:38 -07001015void SensorService::SensorEventConnection::dump(String8& result) {
1016 Mutex::Autolock _l(mConnectionLock);
Aravind Akella0ec20662014-09-14 17:29:48 -07001017 result.appendFormat("\t WakeLockRefCount %d | uid %d | cache size %d | max cache size %d\n",
1018 mWakeLockRefCount, mUid, mCacheSize, mMaxCacheSize);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001019 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1020 const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
Aravind Akella0ec20662014-09-14 17:29:48 -07001021 result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
Aravind Akella4c8b9512013-09-05 17:03:38 -07001022 mService->getSensorName(mSensorInfo.keyAt(i)).string(),
Aravind Akella6c2664a2014-08-13 12:24:50 -07001023 mSensorInfo.keyAt(i),
Aravind Akella4c8b9512013-09-05 17:03:38 -07001024 flushInfo.mFirstFlushPending ? "First flush pending" :
1025 "active",
Aravind Akella0ec20662014-09-14 17:29:48 -07001026 flushInfo.mPendingFlushEventsToSend);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001027 }
Aravind Akella0ec20662014-09-14 17:29:48 -07001028#if DEBUG_CONNECTIONS
1029 result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
1030 " total_acks_needed %d | total_acks_recvd %d\n",
1031 mEventsReceived,
1032 mEventsSent,
1033 mEventsSentFromCache,
1034 mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
1035 mTotalAcksNeeded,
1036 mTotalAcksReceived);
1037#endif
Aravind Akella4c8b9512013-09-05 17:03:38 -07001038}
1039
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001040bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001041 Mutex::Autolock _l(mConnectionLock);
Aravind Akella70018042014-04-07 22:52:37 +00001042 if (!verifyCanAccessSensor(mService->getSensorFromHandle(handle), "Tried adding")) {
1043 return false;
1044 }
Aravind Akella4c8b9512013-09-05 17:03:38 -07001045 if (mSensorInfo.indexOfKey(handle) < 0) {
1046 mSensorInfo.add(handle, FlushInfo());
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001047 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -07001048 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001049 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001050}
1051
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001052bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001053 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001054 if (mSensorInfo.removeItem(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001055 return true;
1056 }
1057 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001058}
1059
1060bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001061 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001062 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -07001063}
1064
1065bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001066 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001067 return mSensorInfo.size() ? true : false;
1068}
1069
Aravind Akella8493b792014-09-08 15:45:47 -07001070bool SensorService::SensorEventConnection::hasOneShotSensors() const {
1071 Mutex::Autolock _l(mConnectionLock);
1072 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1073 const int handle = mSensorInfo.keyAt(i);
1074 if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
1075 return true;
1076 }
1077 }
1078 return false;
1079}
1080
Aravind Akella4c8b9512013-09-05 17:03:38 -07001081void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
1082 bool value) {
1083 Mutex::Autolock _l(mConnectionLock);
1084 ssize_t index = mSensorInfo.indexOfKey(handle);
1085 if (index >= 0) {
1086 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
1087 flushInfo.mFirstFlushPending = value;
1088 }
1089}
1090
Aravind Akella8a969552014-09-28 17:52:41 -07001091void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
1092 Mutex::Autolock _l(mConnectionLock);
1093 updateLooperRegistrationLocked(looper);
1094}
1095
1096void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
1097 const sp<Looper>& looper) {
1098 bool isConnectionActive = mSensorInfo.size() > 0;
1099 // If all sensors are unregistered OR Looper has encountered an error, we
1100 // can remove the Fd from the Looper if it has been previously added.
1101 if (!isConnectionActive || mDead) {
1102 if (mHasLooperCallbacks) {
1103 ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd());
1104 looper->removeFd(mChannel->getSendFd());
1105 mHasLooperCallbacks = false;
1106 }
1107 return;
1108 }
1109
1110 int looper_flags = 0;
1111 if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
1112 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1113 const int handle = mSensorInfo.keyAt(i);
1114 if (mService->getSensorFromHandle(handle).isWakeUpSensor()) {
1115 looper_flags |= ALOOPER_EVENT_INPUT;
1116 break;
1117 }
1118 }
1119 // If flags is still set to zero, we don't need to add this fd to the Looper, if
1120 // the fd has already been added, remove it. This is likely to happen when ALL the
1121 // events stored in the cache have been sent to the corresponding app.
1122 if (looper_flags == 0) {
1123 if (mHasLooperCallbacks) {
1124 ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd());
1125 looper->removeFd(mChannel->getSendFd());
1126 mHasLooperCallbacks = false;
1127 }
1128 return;
1129 }
1130 // Add the file descriptor to the Looper for receiving acknowledegments if the app has
1131 // registered for wake-up sensors OR for sending events in the cache.
1132 int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL);
1133 if (ret == 1) {
1134 ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd());
1135 mHasLooperCallbacks = true;
1136 } else {
1137 ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd());
1138 }
1139}
1140
1141void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
1142 Mutex::Autolock _l(mConnectionLock);
1143 ssize_t index = mSensorInfo.indexOfKey(handle);
1144 if (index >= 0) {
1145 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
1146 flushInfo.mPendingFlushEventsToSend++;
1147 }
1148}
1149
Mathias Agopianfc328812010-07-14 23:41:37 -07001150status_t SensorService::SensorEventConnection::sendEvents(
Aravind Akella0ec20662014-09-14 17:29:48 -07001151 sensors_event_t const* buffer, size_t numEvents,
Aravind Akella8493b792014-09-08 15:45:47 -07001152 sensors_event_t* scratch,
1153 SensorEventConnection const * const * mapFlushEventsToConnections) {
Mathias Agopiancf510012010-07-22 16:18:10 -07001154 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -07001155 size_t count = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001156 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -07001157 if (scratch) {
1158 size_t i=0;
1159 while (i<numEvents) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001160 int32_t sensor_handle = buffer[i].sensor;
Aravind Akella724d91d2013-06-27 12:04:23 -07001161 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
1162 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001163 buffer[i].meta_data.sensor);
Aravind Akella8a969552014-09-28 17:52:41 -07001164 // Setting sensor_handle to the correct sensor to ensure the sensor events per
1165 // connection are filtered correctly. buffer[i].sensor is zero for meta_data
1166 // events.
Aravind Akella6c2664a2014-08-13 12:24:50 -07001167 sensor_handle = buffer[i].meta_data.sensor;
Aravind Akella724d91d2013-06-27 12:04:23 -07001168 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001169 ssize_t index = mSensorInfo.indexOfKey(sensor_handle);
Aravind Akella56ae4262014-07-10 16:01:10 -07001170 // Check if this connection has registered for this sensor. If not continue to the
1171 // next sensor_event.
1172 if (index < 0) {
1173 ++i;
1174 continue;
Aravind Akella4c8b9512013-09-05 17:03:38 -07001175 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001176
Aravind Akella56ae4262014-07-10 16:01:10 -07001177 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
Aravind Akella6c2664a2014-08-13 12:24:50 -07001178 // Check if there is a pending flush_complete event for this sensor on this connection.
Aravind Akella8493b792014-09-08 15:45:47 -07001179 if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
1180 this == mapFlushEventsToConnections[i]) {
1181 flushInfo.mFirstFlushPending = false;
1182 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
1183 buffer[i].meta_data.sensor);
1184 ++i;
1185 continue;
Aravind Akella56ae4262014-07-10 16:01:10 -07001186 }
1187
1188 // If there is a pending flush complete event for this sensor on this connection,
1189 // ignore the event and proceed to the next.
1190 if (flushInfo.mFirstFlushPending) {
1191 ++i;
1192 continue;
1193 }
1194
1195 do {
Aravind Akella8493b792014-09-08 15:45:47 -07001196 // Keep copying events into the scratch buffer as long as they are regular
1197 // sensor_events are from the same sensor_handle OR they are flush_complete_events
1198 // from the same sensor_handle AND the current connection is mapped to the
1199 // corresponding flush_complete_event.
Aravind Akella56ae4262014-07-10 16:01:10 -07001200 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
Aravind Akella8493b792014-09-08 15:45:47 -07001201 if (this == mapFlushEventsToConnections[i]) {
Aravind Akella56ae4262014-07-10 16:01:10 -07001202 scratch[count++] = buffer[i];
Aravind Akella56ae4262014-07-10 16:01:10 -07001203 }
1204 ++i;
1205 } else {
1206 // Regular sensor event, just copy it to the scratch buffer.
Aravind Akella4c8b9512013-09-05 17:03:38 -07001207 scratch[count++] = buffer[i++];
Aravind Akella56ae4262014-07-10 16:01:10 -07001208 }
Aravind Akella8493b792014-09-08 15:45:47 -07001209 } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle &&
1210 buffer[i].type != SENSOR_TYPE_META_DATA) ||
Aravind Akella56ae4262014-07-10 16:01:10 -07001211 (buffer[i].type == SENSOR_TYPE_META_DATA &&
Aravind Akella6c2664a2014-08-13 12:24:50 -07001212 buffer[i].meta_data.sensor == sensor_handle)));
Mathias Agopiancf510012010-07-22 16:18:10 -07001213 }
Mathias Agopian3560fb22010-07-22 21:24:39 -07001214 } else {
1215 scratch = const_cast<sensors_event_t *>(buffer);
1216 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -07001217 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001218
Aravind Akella6c2664a2014-08-13 12:24:50 -07001219 sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -07001220 // Early return if there are no events for this connection.
1221 if (count == 0) {
1222 return status_t(NO_ERROR);
1223 }
1224
1225#if DEBUG_CONNECTIONS
1226 mEventsReceived += count;
1227#endif
1228 if (mCacheSize != 0) {
1229 // There are some events in the cache which need to be sent first. Copy this buffer to
1230 // the end of cache.
1231 if (mCacheSize + count <= mMaxCacheSize) {
1232 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
1233 mCacheSize += count;
1234 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001235 // Check if any new sensors have registered on this connection which may have increased
1236 // the max cache size that is desired.
1237 if (mCacheSize + count < computeMaxCacheSizeLocked()) {
1238 reAllocateCacheLocked(scratch, count);
1239 return status_t(NO_ERROR);
1240 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001241 // Some events need to be dropped.
1242 int remaningCacheSize = mMaxCacheSize - mCacheSize;
1243 if (remaningCacheSize != 0) {
1244 memcpy(&mEventCache[mCacheSize], scratch,
1245 remaningCacheSize * sizeof(sensors_event_t));
1246 }
1247 int numEventsDropped = count - remaningCacheSize;
1248 countFlushCompleteEventsLocked(mEventCache, numEventsDropped);
1249 // Drop the first "numEventsDropped" in the cache.
1250 memmove(mEventCache, &mEventCache[numEventsDropped],
1251 (mCacheSize - numEventsDropped) * sizeof(sensors_event_t));
1252
1253 // Copy the remainingEvents in scratch buffer to the end of cache.
1254 memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize,
1255 numEventsDropped * sizeof(sensors_event_t));
1256 }
1257 return status_t(NO_ERROR);
1258 }
1259
Aravind Akella6c2664a2014-08-13 12:24:50 -07001260 int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count);
1261 if (index_wake_up_event >= 0) {
1262 scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1263 ++mWakeLockRefCount;
Aravind Akellae74baf62014-08-21 12:28:35 -07001264#if DEBUG_CONNECTIONS
1265 ++mTotalAcksNeeded;
1266#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001267 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001268
1269 // NOTE: ASensorEvent and sensors_event_t are the same type.
1270 ssize_t size = SensorEventQueue::write(mChannel,
1271 reinterpret_cast<ASensorEvent const*>(scratch), count);
1272 if (size < 0) {
1273 // Write error, copy events to local cache.
Aravind Akella6c2664a2014-08-13 12:24:50 -07001274 if (index_wake_up_event >= 0) {
1275 // If there was a wake_up sensor_event, reset the flag.
1276 scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
Aravind Akella0ec20662014-09-14 17:29:48 -07001277 if (mWakeLockRefCount > 0) {
1278 --mWakeLockRefCount;
1279 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001280#if DEBUG_CONNECTIONS
1281 --mTotalAcksNeeded;
1282#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001283 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001284 if (mEventCache == NULL) {
1285 mMaxCacheSize = computeMaxCacheSizeLocked();
1286 mEventCache = new sensors_event_t[mMaxCacheSize];
1287 mCacheSize = 0;
1288 }
1289 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
1290 mCacheSize += count;
1291
1292 // Add this file descriptor to the looper to get a callback when this fd is available for
1293 // writing.
Aravind Akella8a969552014-09-28 17:52:41 -07001294 updateLooperRegistrationLocked(mService->getLooper());
Aravind Akella56ae4262014-07-10 16:01:10 -07001295 return size;
1296 }
1297
1298#if DEBUG_CONNECTIONS
1299 if (size > 0) {
1300 mEventsSent += count;
1301 }
1302#endif
1303
1304 return size < 0 ? status_t(size) : status_t(NO_ERROR);
1305}
1306
Aravind Akella6c2664a2014-08-13 12:24:50 -07001307void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch,
1308 int count) {
1309 sensors_event_t *eventCache_new;
1310 const int new_cache_size = computeMaxCacheSizeLocked();
1311 // Allocate new cache, copy over events from the old cache & scratch, free up memory.
1312 eventCache_new = new sensors_event_t[new_cache_size];
1313 memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t));
1314 memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t));
1315
1316 ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize,
1317 new_cache_size);
1318
1319 delete mEventCache;
1320 mEventCache = eventCache_new;
1321 mCacheSize += count;
1322 mMaxCacheSize = new_cache_size;
1323}
1324
1325void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
1326 ASensorEvent flushCompleteEvent;
Aravind Akella0ec20662014-09-14 17:29:48 -07001327 memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
Aravind Akella6c2664a2014-08-13 12:24:50 -07001328 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
Aravind Akella6c2664a2014-08-13 12:24:50 -07001329 // Loop through all the sensors for this connection and check if there are any pending
1330 // flush complete events to be sent.
1331 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1332 FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
1333 while (flushInfo.mPendingFlushEventsToSend > 0) {
Aravind Akella0ec20662014-09-14 17:29:48 -07001334 const int sensor_handle = mSensorInfo.keyAt(i);
1335 flushCompleteEvent.meta_data.sensor = sensor_handle;
1336 if (mService->getSensorFromHandle(sensor_handle).isWakeUpSensor()) {
1337 flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1338 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001339 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
1340 if (size < 0) {
1341 return;
1342 }
1343 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
1344 flushCompleteEvent.meta_data.sensor);
1345 flushInfo.mPendingFlushEventsToSend--;
1346 }
1347 }
1348}
1349
Aravind Akella56ae4262014-07-10 16:01:10 -07001350void SensorService::SensorEventConnection::writeToSocketFromCacheLocked() {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001351 // At a time write at most half the size of the receiver buffer in SensorEventQueue OR
1352 // half the size of the socket buffer allocated in BitTube whichever is smaller.
1353 const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
1354 int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
1355 // Send pending flush complete events (if any)
Aravind Akella6c2664a2014-08-13 12:24:50 -07001356 sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -07001357 for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001358 const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
Aravind Akella6c2664a2014-08-13 12:24:50 -07001359 int index_wake_up_event =
1360 findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
1361 if (index_wake_up_event >= 0) {
1362 mEventCache[index_wake_up_event + numEventsSent].flags |=
1363 WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1364 ++mWakeLockRefCount;
Aravind Akellae74baf62014-08-21 12:28:35 -07001365#if DEBUG_CONNECTIONS
1366 ++mTotalAcksNeeded;
1367#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001368 }
Aravind Akella4c8b9512013-09-05 17:03:38 -07001369
Aravind Akella56ae4262014-07-10 16:01:10 -07001370 ssize_t size = SensorEventQueue::write(mChannel,
1371 reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
Aravind Akella6c2664a2014-08-13 12:24:50 -07001372 numEventsToWrite);
Aravind Akella56ae4262014-07-10 16:01:10 -07001373 if (size < 0) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001374 if (index_wake_up_event >= 0) {
1375 // If there was a wake_up sensor_event, reset the flag.
1376 mEventCache[index_wake_up_event + numEventsSent].flags &=
1377 ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
Aravind Akella0ec20662014-09-14 17:29:48 -07001378 if (mWakeLockRefCount > 0) {
1379 --mWakeLockRefCount;
1380 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001381#if DEBUG_CONNECTIONS
1382 --mTotalAcksNeeded;
1383#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001384 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001385 memmove(mEventCache, &mEventCache[numEventsSent],
1386 (mCacheSize - numEventsSent) * sizeof(sensors_event_t));
1387 ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ",
Aravind Akella6c2664a2014-08-13 12:24:50 -07001388 numEventsSent, mCacheSize);
Aravind Akella56ae4262014-07-10 16:01:10 -07001389 mCacheSize -= numEventsSent;
Aravind Akella56ae4262014-07-10 16:01:10 -07001390 return;
1391 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001392 numEventsSent += numEventsToWrite;
Aravind Akella56ae4262014-07-10 16:01:10 -07001393#if DEBUG_CONNECTIONS
Aravind Akella6c2664a2014-08-13 12:24:50 -07001394 mEventsSentFromCache += numEventsToWrite;
Aravind Akella56ae4262014-07-10 16:01:10 -07001395#endif
Aravind Akellab4099e72013-10-15 15:43:10 -07001396 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001397 ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize);
1398 // All events from the cache have been sent. Reset cache size to zero.
1399 mCacheSize = 0;
Aravind Akella8a969552014-09-28 17:52:41 -07001400 // There are no more events in the cache. We don't need to poll for write on the fd.
1401 // Update Looper registration.
1402 updateLooperRegistrationLocked(mService->getLooper());
Mathias Agopianfc328812010-07-14 23:41:37 -07001403}
1404
Aravind Akellac551eac2013-10-14 17:04:42 -07001405void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
Aravind Akella0ec20662014-09-14 17:29:48 -07001406 sensors_event_t const* scratch, const int numEventsDropped) {
Aravind Akella4c8b9512013-09-05 17:03:38 -07001407 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001408 // Count flushComplete events in the events that are about to the dropped. These will be sent
1409 // separately before the next batch of events.
1410 for (int j = 0; j < numEventsDropped; ++j) {
1411 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
1412 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
1413 flushInfo.mPendingFlushEventsToSend++;
1414 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
1415 flushInfo.mPendingFlushEventsToSend);
1416 }
1417 }
1418 return;
1419}
1420
Aravind Akella6c2664a2014-08-13 12:24:50 -07001421int SensorService::SensorEventConnection::findWakeUpSensorEventLocked(
1422 sensors_event_t const* scratch, const int count) {
Aravind Akella9a844cf2014-02-11 18:58:52 -08001423 for (int i = 0; i < count; ++i) {
1424 if (mService->isWakeUpSensorEvent(scratch[i])) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001425 return i;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001426 }
1427 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001428 return -1;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001429}
1430
Mathias Agopianb3989272011-10-20 18:42:02 -07001431sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -07001432{
1433 return mChannel;
1434}
1435
1436status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -07001437 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
1438 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -07001439{
1440 status_t err;
1441 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -07001442 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
1443 reservedFlags);
Aravind Akella56ae4262014-07-10 16:01:10 -07001444
Mathias Agopianfc328812010-07-14 23:41:37 -07001445 } else {
1446 err = mService->disable(this, handle);
1447 }
1448 return err;
1449}
1450
1451status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -07001452 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -07001453{
Aravind Akella724d91d2013-06-27 12:04:23 -07001454 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -07001455}
1456
Aravind Akella701166d2013-10-08 14:59:26 -07001457status_t SensorService::SensorEventConnection::flush() {
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001458 return mService->flushSensor(this);
Aravind Akella724d91d2013-06-27 12:04:23 -07001459}
Aravind Akella4c8b9512013-09-05 17:03:38 -07001460
Aravind Akella8a969552014-09-28 17:52:41 -07001461int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) {
Aravind Akella56ae4262014-07-10 16:01:10 -07001462 if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) {
Aravind Akella8a969552014-09-28 17:52:41 -07001463 {
1464 // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount,
1465 // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
1466 // can release the wake-lock.
1467 ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
1468 Mutex::Autolock _l(mConnectionLock);
1469 mDead = true;
1470 mWakeLockRefCount = 0;
1471 updateLooperRegistrationLocked(mService->getLooper());
1472 }
1473 mService->checkWakeLockState();
1474 return 1;
Aravind Akella56ae4262014-07-10 16:01:10 -07001475 }
1476
1477 if (events & ALOOPER_EVENT_INPUT) {
Aravind Akella8a969552014-09-28 17:52:41 -07001478 uint32_t numAcks = 0;
1479 ssize_t ret = ::recv(fd, &numAcks, sizeof(numAcks), MSG_DONTWAIT);
Aravind Akella56ae4262014-07-10 16:01:10 -07001480 {
1481 Mutex::Autolock _l(mConnectionLock);
Aravind Akella8a969552014-09-28 17:52:41 -07001482 // Sanity check to ensure there are no read errors in recv, numAcks is always
1483 // within the range and not zero. If any of the above don't hold reset mWakeLockRefCount
1484 // to zero.
1485 if (ret != sizeof(numAcks) || numAcks > mWakeLockRefCount || numAcks == 0) {
1486 ALOGE("Looper read error ret=%d numAcks=%d", ret, numAcks);
1487 mWakeLockRefCount = 0;
1488 } else {
1489 mWakeLockRefCount -= numAcks;
Aravind Akella0ec20662014-09-14 17:29:48 -07001490 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001491#if DEBUG_CONNECTIONS
Aravind Akella8a969552014-09-28 17:52:41 -07001492 mTotalAcksReceived += numAcks;
Aravind Akellae74baf62014-08-21 12:28:35 -07001493#endif
Aravind Akella56ae4262014-07-10 16:01:10 -07001494 }
1495 // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released
1496 // here as checkWakeLockState() will need it.
1497 if (mWakeLockRefCount == 0) {
1498 mService->checkWakeLockState();
1499 }
1500 // continue getting callbacks.
1501 return 1;
1502 }
1503
1504 if (events & ALOOPER_EVENT_OUTPUT) {
1505 // send sensor data that is stored in mEventCache.
Aravind Akella9a844cf2014-02-11 18:58:52 -08001506 Mutex::Autolock _l(mConnectionLock);
Aravind Akella56ae4262014-07-10 16:01:10 -07001507 writeToSocketFromCacheLocked();
Aravind Akella9a844cf2014-02-11 18:58:52 -08001508 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001509 return 1;
1510}
1511
1512int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
1513 int fifoWakeUpSensors = 0;
1514 int fifoNonWakeUpSensors = 0;
1515 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1516 const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i));
1517 if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) {
1518 // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and
1519 // non wake_up sensors.
1520 if (sensor.isWakeUpSensor()) {
1521 fifoWakeUpSensors += sensor.getFifoReservedEventCount();
1522 } else {
1523 fifoNonWakeUpSensors += sensor.getFifoReservedEventCount();
1524 }
1525 } else {
1526 // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors.
1527 if (sensor.isWakeUpSensor()) {
1528 fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ?
1529 fifoWakeUpSensors : sensor.getFifoMaxEventCount();
1530
1531 } else {
1532 fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ?
1533 fifoNonWakeUpSensors : sensor.getFifoMaxEventCount();
1534
1535 }
1536 }
1537 }
1538 if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) {
1539 // It is extremely unlikely that there is a write failure in non batch mode. Return a cache
Aravind Akella6c2664a2014-08-13 12:24:50 -07001540 // size that is equal to that of the batch mode.
Aravind Akellae74baf62014-08-21 12:28:35 -07001541 // ALOGW("Write failure in non-batch mode");
Aravind Akella6c2664a2014-08-13 12:24:50 -07001542 return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t);
Aravind Akella56ae4262014-07-10 16:01:10 -07001543 }
1544 return fifoWakeUpSensors + fifoNonWakeUpSensors;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001545}
1546
Mathias Agopianfc328812010-07-14 23:41:37 -07001547// ---------------------------------------------------------------------------
1548}; // namespace android
1549