blob: dc8fa649d8d8f88105af5fba5ab1d92270751c3b [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;
Aravind Akella56ae4262014-07-10 16:01:10 -0700188 mLooper = new Looper(false);
Aravind Akella8493b792014-09-08 15:45:47 -0700189 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
190 mSensorEventBuffer = new sensors_event_t[minBufferSize];
191 mSensorEventScratch = new sensors_event_t[minBufferSize];
192 mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize];
Aravind Akella7830ef32014-10-07 14:13:12 -0700193
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700194 mInitCheck = NO_ERROR;
Aravind Akella7830ef32014-10-07 14:13:12 -0700195 run("SensorService", PRIORITY_URGENT_DISPLAY);
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700196 }
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 Akella0b6acb22014-10-18 16:37:13 -0700246 "%-15s| %-10s| version=%d |%-20s| 0x%08x | \"%s\" | type=%d |",
Mathias Agopian3560fb22010-07-22 21:24:39 -0700247 s.getName().string(),
248 s.getVendor().string(),
Aravind Akella0b6acb22014-10-18 16:37:13 -0700249 s.getVersion(),
Aravind Akella70018042014-04-07 22:52:37 +0000250 s.getStringType().string(),
251 s.getHandle(),
Aravind Akella6c2664a2014-08-13 12:24:50 -0700252 s.getRequiredPermission().string(),
253 s.getType());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700254
Aravind Akella0e025c52014-06-03 19:19:57 -0700255 const int reportingMode = s.getReportingMode();
256 if (reportingMode == AREPORTING_MODE_CONTINUOUS) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700257 result.append(" continuous | ");
Aravind Akella0e025c52014-06-03 19:19:57 -0700258 } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700259 result.append(" on-change | ");
Aravind Akella0e025c52014-06-03 19:19:57 -0700260 } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700261 result.append(" one-shot | ");
Mathias Agopianba02cd22013-07-03 16:20:57 -0700262 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700263 result.append(" special-trigger | ");
264 }
265
266 if (s.getMaxDelay() > 0) {
267 result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay());
268 } else {
269 result.appendFormat("maxDelay=%dus |", s.getMaxDelay());
Mathias Agopianba02cd22013-07-03 16:20:57 -0700270 }
Aravind Akella0e025c52014-06-03 19:19:57 -0700271
272 if (s.getMinDelay() > 0) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700273 result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay());
Aravind Akella0e025c52014-06-03 19:19:57 -0700274 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700275 result.appendFormat("minDelay=%dus |", s.getMinDelay());
Aravind Akella0e025c52014-06-03 19:19:57 -0700276 }
277
Aravind Akella724d91d2013-06-27 12:04:23 -0700278 if (s.getFifoMaxEventCount() > 0) {
Aravind Akella70018042014-04-07 22:52:37 +0000279 result.appendFormat("FifoMax=%d events | ",
280 s.getFifoMaxEventCount());
Aravind Akella724d91d2013-06-27 12:04:23 -0700281 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700282 result.append("no batching | ");
283 }
284
285 if (s.isWakeUpSensor()) {
286 result.appendFormat("wakeUp | ");
287 } else {
288 result.appendFormat("non-wakeUp | ");
Aravind Akella724d91d2013-06-27 12:04:23 -0700289 }
Mathias Agopianba02cd22013-07-03 16:20:57 -0700290
291 switch (s.getType()) {
292 case SENSOR_TYPE_ROTATION_VECTOR:
293 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
294 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700295 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
296 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700297 break;
298 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
299 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
300 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700301 "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
302 e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5],
303 e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700304 break;
305 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
306 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700307 "last=<%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
308 e.data[0], e.data[1], e.data[2], e.data[3], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700309 break;
310 case SENSOR_TYPE_SIGNIFICANT_MOTION:
311 case SENSOR_TYPE_STEP_DETECTOR:
Aravind Akella6c2664a2014-08-13 12:24:50 -0700312 result.appendFormat( "last=<%f %" PRId64 ">\n", e.data[0], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700313 break;
314 case SENSOR_TYPE_STEP_COUNTER:
Aravind Akella6c2664a2014-08-13 12:24:50 -0700315 result.appendFormat( "last=<%" PRIu64 ", %" PRId64 ">\n", e.u64.step_counter,
316 e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700317 break;
318 default:
319 // default to 3 values
320 result.appendFormat(
Aravind Akella6c2664a2014-08-13 12:24:50 -0700321 "last=<%5.1f,%5.1f,%5.1f, %" PRId64 ">\n",
322 e.data[0], e.data[1], e.data[2], e.timestamp);
Mathias Agopianba02cd22013-07-03 16:20:57 -0700323 break;
324 }
Aravind Akella6c2664a2014-08-13 12:24:50 -0700325 result.append("\n");
Mathias Agopianba02cd22013-07-03 16:20:57 -0700326 }
327 SensorFusion::getInstance().dump(result);
328 SensorDevice::getInstance().dump(result);
329
Mathias Agopianba02cd22013-07-03 16:20:57 -0700330 result.append("Active sensors:\n");
Mathias Agopianfc328812010-07-14 23:41:37 -0700331 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
Mathias Agopian5d270722010-07-19 15:20:39 -0700332 int handle = mActiveSensors.keyAt(i);
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700333 result.appendFormat("%s (handle=0x%08x, connections=%zu)\n",
Mathias Agopian5d270722010-07-19 15:20:39 -0700334 getSensorName(handle).string(),
335 handle,
Mathias Agopianfc328812010-07-14 23:41:37 -0700336 mActiveSensors.valueAt(i)->getNumConnections());
Mathias Agopianfc328812010-07-14 23:41:37 -0700337 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700338
Aravind Akella5466c3d2014-08-22 16:11:10 -0700339 result.appendFormat("Socket Buffer size = %d events\n",
Aravind Akella6c2664a2014-08-13 12:24:50 -0700340 mSocketBufferSize/sizeof(sensors_event_t));
Aravind Akella9a844cf2014-02-11 18:58:52 -0800341 result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : "not held");
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700342 result.appendFormat("%zd active connections\n", mActiveConnections.size());
Aravind Akella4c8b9512013-09-05 17:03:38 -0700343
344 for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
345 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
346 if (connection != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700347 result.appendFormat("Connection Number: %zu \n", i);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700348 connection->dump(result);
349 }
350 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700351 }
352 write(fd, result.string(), result.size());
353 return NO_ERROR;
354}
355
Aravind Akella9a844cf2014-02-11 18:58:52 -0800356void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700357 sensors_event_t const* buffer, const int count) {
358 for (int i=0 ; i<count ; i++) {
359 int handle = buffer[i].sensor;
Aravind Akella8493b792014-09-08 15:45:47 -0700360 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
361 handle = buffer[i].meta_data.sensor;
362 }
Aravind Akella0e025c52014-06-03 19:19:57 -0700363 if (connection->hasSensor(handle)) {
364 SensorInterface* sensor = mSensorMap.valueFor(handle);
365 // If this buffer has an event from a one_shot sensor and this connection is registered
366 // for this particular one_shot sensor, try cleaning up the connection.
367 if (sensor != NULL &&
368 sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
369 sensor->autoDisable(connection.get(), handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800370 cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700371 }
372 }
373 }
374}
375
Mathias Agopianfc328812010-07-14 23:41:37 -0700376bool SensorService::threadLoop()
377{
Steve Blocka5512372011-12-20 16:23:08 +0000378 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700379
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700380 // each virtual sensor could generate an event per "real" event, that's why we need
381 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
382 // in practice, this is too aggressive, but guaranteed to be enough.
383 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
384 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
385
Mathias Agopianf001c922010-11-11 17:58:51 -0800386 SensorDevice& device(SensorDevice::getInstance());
387 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700388
Aravind Akella56ae4262014-07-10 16:01:10 -0700389 SensorEventAckReceiver sender(this);
390 sender.run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700391 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700392 do {
Aravind Akella8493b792014-09-08 15:45:47 -0700393 ssize_t count = device.poll(mSensorEventBuffer, numEventMax);
394 if (count < 0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000395 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700396 break;
397 }
Aravind Akella56ae4262014-07-10 16:01:10 -0700398
399 // Reset sensors_event_t.flags to zero for all events in the buffer.
400 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700401 mSensorEventBuffer[i].flags = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -0700402 }
Aravind Akellae148bc22014-09-24 22:12:58 -0700403
404 // Make a copy of the connection vector as some connections may be removed during the
405 // course of this loop (especially when one-shot sensor events are present in the
406 // sensor_event buffer). Promote all connections to StrongPointers before the lock is
407 // acquired. If the destructor of the sp gets called when the lock is acquired, it may
408 // result in a deadlock as ~SensorEventConnection() needs to acquire mLock again for
409 // cleanup. So copy all the strongPointers to a vector before the lock is acquired.
410 SortedVector< sp<SensorEventConnection> > activeConnections;
411 {
412 Mutex::Autolock _l(mLock);
413 for (size_t i=0 ; i < mActiveConnections.size(); ++i) {
414 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
415 if (connection != 0) {
416 activeConnections.add(connection);
417 }
418 }
419 }
420
Aravind Akella9a844cf2014-02-11 18:58:52 -0800421 Mutex::Autolock _l(mLock);
422 // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The
423 // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock,
424 // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should
425 // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and
426 // releasing the wakelock.
427 bool bufferHasWakeUpEvent = false;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700428 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700429 if (isWakeUpSensorEvent(mSensorEventBuffer[i])) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800430 bufferHasWakeUpEvent = true;
431 break;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700432 }
433 }
434
Aravind Akella9a844cf2014-02-11 18:58:52 -0800435 if (bufferHasWakeUpEvent && !mWakeLockAcquired) {
436 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
437 mWakeLockAcquired = true;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800438 }
Aravind Akella8493b792014-09-08 15:45:47 -0700439 recordLastValueLocked(mSensorEventBuffer, count);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800440
Mathias Agopianf001c922010-11-11 17:58:51 -0800441 // handle virtual sensors
442 if (count && vcount) {
Aravind Akella8493b792014-09-08 15:45:47 -0700443 sensors_event_t const * const event = mSensorEventBuffer;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800444 const size_t activeVirtualSensorCount = mActiveVirtualSensors.size();
Mathias Agopianf001c922010-11-11 17:58:51 -0800445 if (activeVirtualSensorCount) {
446 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700447 SensorFusion& fusion(SensorFusion::getInstance());
448 if (fusion.isEnabled()) {
449 for (size_t i=0 ; i<size_t(count) ; i++) {
450 fusion.process(event[i]);
451 }
452 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700453 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800454 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700455 if (count + k >= minBufferSize) {
456 ALOGE("buffer too small to hold all events: "
Mark Salyzyndb458612014-06-10 14:50:02 -0700457 "count=%zd, k=%zu, size=%zu",
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700458 count, k, minBufferSize);
459 break;
460 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800461 sensors_event_t out;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800462 SensorInterface* si = mActiveVirtualSensors.valueAt(j);
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700463 if (si->process(&out, event[i])) {
Aravind Akella8493b792014-09-08 15:45:47 -0700464 mSensorEventBuffer[count + k] = out;
Mathias Agopianf001c922010-11-11 17:58:51 -0800465 k++;
466 }
467 }
468 }
469 if (k) {
470 // record the last synthesized values
Aravind Akella8493b792014-09-08 15:45:47 -0700471 recordLastValueLocked(&mSensorEventBuffer[count], k);
Mathias Agopianf001c922010-11-11 17:58:51 -0800472 count += k;
473 // sort the buffer by time-stamps
Aravind Akella8493b792014-09-08 15:45:47 -0700474 sortEventBuffer(mSensorEventBuffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700475 }
476 }
477 }
478
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700479 // handle backward compatibility for RotationVector sensor
480 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
481 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700482 if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700483 // All the 4 components of the quaternion should be available
484 // No heading accuracy. Set it to -1
Aravind Akella8493b792014-09-08 15:45:47 -0700485 mSensorEventBuffer[i].data[4] = -1;
486 }
487 }
488 }
489
490 // Map flush_complete_events in the buffer to SensorEventConnections which called
491 // flush on the hardware sensor. mapFlushEventsToConnections[i] will be the
492 // SensorEventConnection mapped to the corresponding flush_complete_event in
493 // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise).
494 for (int i = 0; i < count; ++i) {
495 mMapFlushEventsToConnections[i] = NULL;
496 if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) {
497 const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor;
498 SensorRecord* rec = mActiveSensors.valueFor(sensor_handle);
499 if (rec != NULL) {
500 mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection();
501 rec->removeFirstPendingFlushConnection();
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700502 }
503 }
504 }
505
Aravind Akella9a844cf2014-02-11 18:58:52 -0800506 // Send our events to clients. Check the state of wake lock for each client and release the
507 // lock if none of the clients need it.
508 bool needsWakeLock = false;
Aravind Akella8493b792014-09-08 15:45:47 -0700509 size_t numConnections = activeConnections.size();
510 for (size_t i=0 ; i < numConnections; ++i) {
Aravind Akellae148bc22014-09-24 22:12:58 -0700511 if (activeConnections[i] != 0) {
512 activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch,
Aravind Akella8493b792014-09-08 15:45:47 -0700513 mMapFlushEventsToConnections);
Aravind Akellae148bc22014-09-24 22:12:58 -0700514 needsWakeLock |= activeConnections[i]->needsWakeLock();
Aravind Akella8493b792014-09-08 15:45:47 -0700515 // If the connection has one-shot sensors, it may be cleaned up after first trigger.
516 // Early check for one-shot sensors.
Aravind Akellae148bc22014-09-24 22:12:58 -0700517 if (activeConnections[i]->hasOneShotSensors()) {
518 cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer,
519 count);
Aravind Akella8493b792014-09-08 15:45:47 -0700520 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800521 }
522 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700523
Aravind Akella9a844cf2014-02-11 18:58:52 -0800524 if (mWakeLockAcquired && !needsWakeLock) {
525 release_wake_lock(WAKE_LOCK_NAME);
526 mWakeLockAcquired = false;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800527 }
Aravind Akella8493b792014-09-08 15:45:47 -0700528 } while (!Thread::exitPending());
Mathias Agopianfc328812010-07-14 23:41:37 -0700529
Steve Block3c20fbe2012-01-05 23:22:43 +0000530 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800531 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700532 return false;
533}
534
Aravind Akella56ae4262014-07-10 16:01:10 -0700535sp<Looper> SensorService::getLooper() const {
536 return mLooper;
537}
538
539bool SensorService::SensorEventAckReceiver::threadLoop() {
540 ALOGD("new thread SensorEventAckReceiver");
541 do {
542 sp<Looper> looper = mService->getLooper();
543 looper->pollOnce(-1);
544 } while(!Thread::exitPending());
545 return false;
546}
547
Aravind Akella9a844cf2014-02-11 18:58:52 -0800548void SensorService::recordLastValueLocked(
Aravind Akella4b847042014-03-03 19:02:46 -0800549 const sensors_event_t* buffer, size_t count) {
Aravind Akella4b847042014-03-03 19:02:46 -0800550 const sensors_event_t* last = NULL;
551 for (size_t i = 0; i < count; i++) {
552 const sensors_event_t* event = &buffer[i];
553 if (event->type != SENSOR_TYPE_META_DATA) {
554 if (last && event->sensor != last->sensor) {
555 mLastEventSeen.editValueFor(last->sensor) = *last;
556 }
557 last = event;
Mathias Agopian94e8f682010-11-10 17:50:28 -0800558 }
559 }
Aravind Akella4b847042014-03-03 19:02:46 -0800560 if (last) {
561 mLastEventSeen.editValueFor(last->sensor) = *last;
562 }
Mathias Agopian94e8f682010-11-10 17:50:28 -0800563}
564
Mathias Agopianf001c922010-11-11 17:58:51 -0800565void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
566{
567 struct compar {
568 static int cmp(void const* lhs, void const* rhs) {
569 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
570 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700571 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800572 }
573 };
574 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
575}
576
Mathias Agopian5d270722010-07-19 15:20:39 -0700577String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700578 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700579 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700580 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700581 if (sensor.getHandle() == handle) {
582 return sensor.getName();
583 }
584 }
585 String8 result("unknown");
586 return result;
587}
588
Aravind Akellab4099e72013-10-15 15:43:10 -0700589bool SensorService::isVirtualSensor(int handle) const {
590 SensorInterface* sensor = mSensorMap.valueFor(handle);
591 return sensor->isVirtual();
592}
593
Aravind Akella9a844cf2014-02-11 18:58:52 -0800594bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const {
Sean Wan7869e222014-07-14 17:07:33 -0700595 int handle = event.sensor;
596 if (event.type == SENSOR_TYPE_META_DATA) {
597 handle = event.meta_data.sensor;
598 }
599 SensorInterface* sensor = mSensorMap.valueFor(handle);
600 return sensor != NULL && sensor->getSensor().isWakeUpSensor();
Aravind Akella9a844cf2014-02-11 18:58:52 -0800601}
602
Aravind Akella6c2664a2014-08-13 12:24:50 -0700603
604SensorService::SensorRecord * SensorService::getSensorRecord(int handle) {
605 return mActiveSensors.valueFor(handle);
606}
607
Mathias Agopianfc328812010-07-14 23:41:37 -0700608Vector<Sensor> SensorService::getSensorList()
609{
Mathias Agopian33264862012-06-28 19:46:54 -0700610 char value[PROPERTY_VALUE_MAX];
611 property_get("debug.sensors", value, "0");
Aravind Akella70018042014-04-07 22:52:37 +0000612 const Vector<Sensor>& initialSensorList = (atoi(value)) ?
613 mUserSensorListDebug : mUserSensorList;
614 Vector<Sensor> accessibleSensorList;
615 for (size_t i = 0; i < initialSensorList.size(); i++) {
616 Sensor sensor = initialSensorList[i];
617 if (canAccessSensor(sensor)) {
618 accessibleSensorList.add(sensor);
619 } else {
620 String8 infoMessage;
621 infoMessage.appendFormat(
622 "Skipped sensor %s because it requires permission %s",
623 sensor.getName().string(),
624 sensor.getRequiredPermission().string());
625 ALOGI(infoMessage.string());
626 }
Mathias Agopian33264862012-06-28 19:46:54 -0700627 }
Aravind Akella70018042014-04-07 22:52:37 +0000628 return accessibleSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700629}
630
631sp<ISensorEventConnection> SensorService::createSensorEventConnection()
632{
Mathias Agopian5307d172012-09-18 17:02:43 -0700633 uid_t uid = IPCThreadState::self()->getCallingUid();
634 sp<SensorEventConnection> result(new SensorEventConnection(this, uid));
Mathias Agopianfc328812010-07-14 23:41:37 -0700635 return result;
636}
637
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800638void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700639{
640 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800641 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700642 size_t size = mActiveSensors.size();
Mark Salyzyndb458612014-06-10 14:50:02 -0700643 ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700644 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800645 int handle = mActiveSensors.keyAt(i);
646 if (c->hasSensor(handle)) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700647 ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800648 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000649 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800650 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800651 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800652 }
Aravind Akella8a969552014-09-28 17:52:41 -0700653 c->removeSensor(handle);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800654 }
655 SensorRecord* rec = mActiveSensors.valueAt(i);
Mark Salyzyndb458612014-06-10 14:50:02 -0700656 ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000657 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700658 "removing connection %p for sensor[%zu].handle=0x%08x",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700659 c, i, handle);
660
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800661 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000662 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700663 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800664 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700665 delete rec;
666 size--;
667 } else {
668 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700669 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700670 }
Aravind Akella8a969552014-09-28 17:52:41 -0700671 c->updateLooperRegistration(mLooper);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700672 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700673 BatteryService::cleanup(c->getUid());
Aravind Akella9a844cf2014-02-11 18:58:52 -0800674 if (c->needsWakeLock()) {
675 checkWakeLockStateLocked();
676 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700677}
678
Aravind Akella70018042014-04-07 22:52:37 +0000679Sensor SensorService::getSensorFromHandle(int handle) const {
680 return mSensorMap.valueFor(handle)->getSensor();
681}
682
Mathias Agopianfc328812010-07-14 23:41:37 -0700683status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Aravind Akella724d91d2013-06-27 12:04:23 -0700684 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -0700685{
Mathias Agopian50df2952010-07-19 19:09:10 -0700686 if (mInitCheck != NO_ERROR)
687 return mInitCheck;
688
Mathias Agopianf001c922010-11-11 17:58:51 -0800689 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700690 if (sensor == NULL) {
691 return BAD_VALUE;
692 }
Aravind Akella70018042014-04-07 22:52:37 +0000693
694 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried enabling")) {
695 return BAD_VALUE;
696 }
697
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700698 Mutex::Autolock _l(mLock);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700699 SensorRecord* rec = mActiveSensors.valueFor(handle);
700 if (rec == 0) {
701 rec = new SensorRecord(connection);
702 mActiveSensors.add(handle, rec);
703 if (sensor->isVirtual()) {
704 mActiveVirtualSensors.add(handle, sensor);
705 }
706 } else {
707 if (rec->addConnection(connection)) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800708 // this sensor is already activated, but we are adding a connection that uses it.
709 // Immediately send down the last known value of the requested sensor if it's not a
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700710 // "continuous" sensor.
Aravind Akella0e025c52014-06-03 19:19:57 -0700711 if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800712 // NOTE: The wake_up flag of this event may get set to
713 // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event.
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700714 sensors_event_t& event(mLastEventSeen.editValueFor(handle));
715 if (event.version == sizeof(sensors_event_t)) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800716 if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) {
717 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
718 mWakeLockAcquired = true;
719 ALOGD_IF(DEBUG_CONNECTIONS, "acquired wakelock for on_change sensor %s",
720 WAKE_LOCK_NAME);
721 }
722 connection->sendEvents(&event, 1, NULL);
723 if (!connection->needsWakeLock() && mWakeLockAcquired) {
724 checkWakeLockStateLocked();
725 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700726 }
727 }
728 }
729 }
730
731 if (connection->addSensor(handle)) {
732 BatteryService::enableSensor(connection->getUid(), handle);
733 // the sensor was added (which means it wasn't already there)
734 // so, see if this connection becomes active
735 if (mActiveConnections.indexOf(connection) < 0) {
736 mActiveConnections.add(connection);
737 }
738 } else {
739 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
740 handle, connection.get());
741 }
742
Aravind Akella724d91d2013-06-27 12:04:23 -0700743 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
744 if (samplingPeriodNs < minDelayNs) {
745 samplingPeriodNs = minDelayNs;
746 }
747
Aravind Akella6c2664a2014-08-13 12:24:50 -0700748 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d"
749 "rate=%" PRId64 " timeout== %" PRId64"",
Aravind Akella724d91d2013-06-27 12:04:23 -0700750 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
751
752 status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs,
753 maxBatchReportLatencyNs);
Aravind Akella6c2664a2014-08-13 12:24:50 -0700754
Aravind Akella5466c3d2014-08-22 16:11:10 -0700755 // Call flush() before calling activate() on the sensor. Wait for a first flush complete
756 // event before sending events on this connection. Ignore one-shot sensors which don't
757 // support flush(). Also if this sensor isn't already active, don't call flush().
758 if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT &&
759 rec->getNumConnections() > 1) {
760 connection->setFirstFlushPending(handle, true);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700761 status_t err_flush = sensor->flush(connection.get(), handle);
Aravind Akella5466c3d2014-08-22 16:11:10 -0700762 // Flush may return error if the underlying h/w sensor uses an older HAL.
Aravind Akella6c2664a2014-08-13 12:24:50 -0700763 if (err_flush == NO_ERROR) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700764 rec->addPendingFlushConnection(connection.get());
Aravind Akella5466c3d2014-08-22 16:11:10 -0700765 } else {
766 connection->setFirstFlushPending(handle, false);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700767 }
768 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700769
770 if (err == NO_ERROR) {
771 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
772 err = sensor->activate(connection.get(), true);
773 }
774
Aravind Akella8a969552014-09-28 17:52:41 -0700775 if (err == NO_ERROR) {
776 connection->updateLooperRegistration(mLooper);
Aravind Akella56ae4262014-07-10 16:01:10 -0700777 }
778
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700779 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700780 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700781 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700782 }
783 return err;
784}
785
786status_t SensorService::disable(const sp<SensorEventConnection>& connection,
787 int handle)
788{
Mathias Agopian50df2952010-07-19 19:09:10 -0700789 if (mInitCheck != NO_ERROR)
790 return mInitCheck;
791
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700792 Mutex::Autolock _l(mLock);
793 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700794 if (err == NO_ERROR) {
795 SensorInterface* sensor = mSensorMap.valueFor(handle);
796 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
797 }
798 return err;
799}
800
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700801status_t SensorService::cleanupWithoutDisable(
802 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700803 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700804 return cleanupWithoutDisableLocked(connection, handle);
805}
806
807status_t SensorService::cleanupWithoutDisableLocked(
808 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700809 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700810 if (rec) {
811 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700812 if (connection->removeSensor(handle)) {
813 BatteryService::disableSensor(connection->getUid(), handle);
814 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700815 if (connection->hasAnySensor() == false) {
Aravind Akella8a969552014-09-28 17:52:41 -0700816 connection->updateLooperRegistration(mLooper);
Mathias Agopianfc328812010-07-14 23:41:37 -0700817 mActiveConnections.remove(connection);
818 }
819 // see if this sensor becomes inactive
820 if (rec->removeConnection(connection)) {
821 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800822 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700823 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -0700824 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700825 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700826 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700827 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -0700828}
829
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700830status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Mathias Agopianfc328812010-07-14 23:41:37 -0700831 int handle, nsecs_t ns)
832{
Mathias Agopian50df2952010-07-19 19:09:10 -0700833 if (mInitCheck != NO_ERROR)
834 return mInitCheck;
835
Mathias Agopianae09d652011-11-01 17:37:49 -0700836 SensorInterface* sensor = mSensorMap.valueFor(handle);
837 if (!sensor)
838 return BAD_VALUE;
839
Aravind Akella70018042014-04-07 22:52:37 +0000840 if (!verifyCanAccessSensor(sensor->getSensor(), "Tried configuring")) {
841 return BAD_VALUE;
842 }
843
Mathias Agopian1cd70002010-07-21 15:59:50 -0700844 if (ns < 0)
845 return BAD_VALUE;
846
Mathias Agopian62569ec2011-11-07 21:21:47 -0800847 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
848 if (ns < minDelayNs) {
849 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -0700850 }
851
Mathias Agopianf001c922010-11-11 17:58:51 -0800852 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700853}
854
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700855status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection) {
Aravind Akella70018042014-04-07 22:52:37 +0000856 if (mInitCheck != NO_ERROR) return mInitCheck;
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700857 SensorDevice& dev(SensorDevice::getInstance());
858 const int halVersion = dev.getHalDeviceVersion();
859 status_t err(NO_ERROR);
860 Mutex::Autolock _l(mLock);
861 // Loop through all sensors for this connection and call flush on each of them.
862 for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) {
863 const int handle = connection->mSensorInfo.keyAt(i);
864 SensorInterface* sensor = mSensorMap.valueFor(handle);
865 if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
866 ALOGE("flush called on a one-shot sensor");
867 err = INVALID_OPERATION;
868 continue;
869 }
Aravind Akella8493b792014-09-08 15:45:47 -0700870 if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) {
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700871 // For older devices just increment pending flush count which will send a trivial
872 // flush complete event.
Aravind Akella8a969552014-09-28 17:52:41 -0700873 connection->incrementPendingFlushCount(handle);
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700874 } else {
875 status_t err_flush = sensor->flush(connection.get(), handle);
876 if (err_flush == NO_ERROR) {
877 SensorRecord* rec = mActiveSensors.valueFor(handle);
878 if (rec != NULL) rec->addPendingFlushConnection(connection);
879 }
880 err = (err_flush != NO_ERROR) ? err_flush : err;
881 }
Aravind Akella70018042014-04-07 22:52:37 +0000882 }
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700883 return err;
Aravind Akella724d91d2013-06-27 12:04:23 -0700884}
Aravind Akella70018042014-04-07 22:52:37 +0000885
Aravind Akella70018042014-04-07 22:52:37 +0000886bool SensorService::canAccessSensor(const Sensor& sensor) {
Aravind Akella56ae4262014-07-10 16:01:10 -0700887 return (sensor.getRequiredPermission().isEmpty()) ||
888 PermissionCache::checkCallingPermission(String16(sensor.getRequiredPermission()));
Aravind Akella70018042014-04-07 22:52:37 +0000889}
890
891bool SensorService::verifyCanAccessSensor(const Sensor& sensor, const char* operation) {
892 if (canAccessSensor(sensor)) {
893 return true;
894 } else {
895 String8 errorMessage;
896 errorMessage.appendFormat(
897 "%s a sensor (%s) without holding its required permission: %s",
898 operation,
899 sensor.getName().string(),
900 sensor.getRequiredPermission().string());
901 return false;
902 }
903}
904
Aravind Akella9a844cf2014-02-11 18:58:52 -0800905void SensorService::checkWakeLockState() {
906 Mutex::Autolock _l(mLock);
907 checkWakeLockStateLocked();
908}
909
910void SensorService::checkWakeLockStateLocked() {
911 if (!mWakeLockAcquired) {
912 return;
913 }
914 bool releaseLock = true;
915 for (size_t i=0 ; i<mActiveConnections.size() ; i++) {
916 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
917 if (connection != 0) {
918 if (connection->needsWakeLock()) {
919 releaseLock = false;
920 break;
921 }
922 }
923 }
924 if (releaseLock) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800925 release_wake_lock(WAKE_LOCK_NAME);
926 mWakeLockAcquired = false;
927 }
928}
Aravind Akella6c2664a2014-08-13 12:24:50 -0700929
Mathias Agopianfc328812010-07-14 23:41:37 -0700930// ---------------------------------------------------------------------------
Mathias Agopianfc328812010-07-14 23:41:37 -0700931SensorService::SensorRecord::SensorRecord(
932 const sp<SensorEventConnection>& connection)
933{
934 mConnections.add(connection);
935}
936
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700937bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -0700938 const sp<SensorEventConnection>& connection)
939{
940 if (mConnections.indexOf(connection) < 0) {
941 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700942 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -0700943 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700944 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -0700945}
946
947bool SensorService::SensorRecord::removeConnection(
948 const wp<SensorEventConnection>& connection)
949{
950 ssize_t index = mConnections.indexOf(connection);
951 if (index >= 0) {
952 mConnections.removeItemsAt(index, 1);
953 }
Aravind Akella6c2664a2014-08-13 12:24:50 -0700954 // Remove this connections from the queue of flush() calls made on this sensor.
955 for (Vector< wp<SensorEventConnection> >::iterator it =
956 mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) {
Aravind Akella8a969552014-09-28 17:52:41 -0700957
Aravind Akella6c2664a2014-08-13 12:24:50 -0700958 if (it->unsafe_get() == connection.unsafe_get()) {
959 it = mPendingFlushConnections.erase(it);
960 } else {
961 ++it;
962 }
963 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700964 return mConnections.size() ? false : true;
965}
966
Aravind Akella6c2664a2014-08-13 12:24:50 -0700967void SensorService::SensorRecord::addPendingFlushConnection(
968 const sp<SensorEventConnection>& connection) {
969 mPendingFlushConnections.add(connection);
970}
971
972void SensorService::SensorRecord::removeFirstPendingFlushConnection() {
973 if (mPendingFlushConnections.size() > 0) {
974 mPendingFlushConnections.removeAt(0);
975 }
976}
977
978SensorService::SensorEventConnection *
979SensorService::SensorRecord::getFirstPendingFlushConnection() {
980 if (mPendingFlushConnections.size() > 0) {
981 return mPendingFlushConnections[0].unsafe_get();
982 }
983 return NULL;
984}
985
Mathias Agopianfc328812010-07-14 23:41:37 -0700986// ---------------------------------------------------------------------------
987
988SensorService::SensorEventConnection::SensorEventConnection(
Mathias Agopian5307d172012-09-18 17:02:43 -0700989 const sp<SensorService>& service, uid_t uid)
Aravind Akella8a969552014-09-28 17:52:41 -0700990 : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
991 mDead(false), mEventCache(NULL), mCacheSize(0), mMaxCacheSize(0) {
Aravind Akella5466c3d2014-08-22 16:11:10 -0700992 mChannel = new BitTube(mService->mSocketBufferSize);
Aravind Akella56ae4262014-07-10 16:01:10 -0700993#if DEBUG_CONNECTIONS
994 mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
Aravind Akellae74baf62014-08-21 12:28:35 -0700995 mTotalAcksNeeded = mTotalAcksReceived = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -0700996#endif
Mathias Agopianfc328812010-07-14 23:41:37 -0700997}
998
Aravind Akella56ae4262014-07-10 16:01:10 -0700999SensorService::SensorEventConnection::~SensorEventConnection() {
Steve Blocka5512372011-12-20 16:23:08 +00001000 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Aravind Akellae148bc22014-09-24 22:12:58 -07001001 mService->cleanupConnection(this);
Aravind Akella56ae4262014-07-10 16:01:10 -07001002 if (mEventCache != NULL) {
1003 delete mEventCache;
1004 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001005}
1006
Aravind Akella56ae4262014-07-10 16:01:10 -07001007void SensorService::SensorEventConnection::onFirstRef() {
1008 LooperCallback::onFirstRef();
Mathias Agopianfc328812010-07-14 23:41:37 -07001009}
1010
Aravind Akella9a844cf2014-02-11 18:58:52 -08001011bool SensorService::SensorEventConnection::needsWakeLock() {
1012 Mutex::Autolock _l(mConnectionLock);
Aravind Akella8a969552014-09-28 17:52:41 -07001013 return !mDead && mWakeLockRefCount > 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001014}
1015
Aravind Akella4c8b9512013-09-05 17:03:38 -07001016void SensorService::SensorEventConnection::dump(String8& result) {
1017 Mutex::Autolock _l(mConnectionLock);
Aravind Akella0ec20662014-09-14 17:29:48 -07001018 result.appendFormat("\t WakeLockRefCount %d | uid %d | cache size %d | max cache size %d\n",
1019 mWakeLockRefCount, mUid, mCacheSize, mMaxCacheSize);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001020 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1021 const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
Aravind Akella0ec20662014-09-14 17:29:48 -07001022 result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
Aravind Akella4c8b9512013-09-05 17:03:38 -07001023 mService->getSensorName(mSensorInfo.keyAt(i)).string(),
Aravind Akella6c2664a2014-08-13 12:24:50 -07001024 mSensorInfo.keyAt(i),
Aravind Akella4c8b9512013-09-05 17:03:38 -07001025 flushInfo.mFirstFlushPending ? "First flush pending" :
1026 "active",
Aravind Akella0ec20662014-09-14 17:29:48 -07001027 flushInfo.mPendingFlushEventsToSend);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001028 }
Aravind Akella0ec20662014-09-14 17:29:48 -07001029#if DEBUG_CONNECTIONS
1030 result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
1031 " total_acks_needed %d | total_acks_recvd %d\n",
1032 mEventsReceived,
1033 mEventsSent,
1034 mEventsSentFromCache,
1035 mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
1036 mTotalAcksNeeded,
1037 mTotalAcksReceived);
1038#endif
Aravind Akella4c8b9512013-09-05 17:03:38 -07001039}
1040
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001041bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001042 Mutex::Autolock _l(mConnectionLock);
Aravind Akella70018042014-04-07 22:52:37 +00001043 if (!verifyCanAccessSensor(mService->getSensorFromHandle(handle), "Tried adding")) {
1044 return false;
1045 }
Aravind Akella4c8b9512013-09-05 17:03:38 -07001046 if (mSensorInfo.indexOfKey(handle) < 0) {
1047 mSensorInfo.add(handle, FlushInfo());
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001048 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -07001049 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001050 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001051}
1052
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001053bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001054 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001055 if (mSensorInfo.removeItem(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001056 return true;
1057 }
1058 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001059}
1060
1061bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001062 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001063 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -07001064}
1065
1066bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001067 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001068 return mSensorInfo.size() ? true : false;
1069}
1070
Aravind Akella8493b792014-09-08 15:45:47 -07001071bool SensorService::SensorEventConnection::hasOneShotSensors() const {
1072 Mutex::Autolock _l(mConnectionLock);
1073 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1074 const int handle = mSensorInfo.keyAt(i);
1075 if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
1076 return true;
1077 }
1078 }
1079 return false;
1080}
1081
Aravind Akella4c8b9512013-09-05 17:03:38 -07001082void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
1083 bool value) {
1084 Mutex::Autolock _l(mConnectionLock);
1085 ssize_t index = mSensorInfo.indexOfKey(handle);
1086 if (index >= 0) {
1087 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
1088 flushInfo.mFirstFlushPending = value;
1089 }
1090}
1091
Aravind Akella8a969552014-09-28 17:52:41 -07001092void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
1093 Mutex::Autolock _l(mConnectionLock);
1094 updateLooperRegistrationLocked(looper);
1095}
1096
1097void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
1098 const sp<Looper>& looper) {
1099 bool isConnectionActive = mSensorInfo.size() > 0;
1100 // If all sensors are unregistered OR Looper has encountered an error, we
1101 // can remove the Fd from the Looper if it has been previously added.
1102 if (!isConnectionActive || mDead) {
1103 if (mHasLooperCallbacks) {
1104 ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd());
1105 looper->removeFd(mChannel->getSendFd());
1106 mHasLooperCallbacks = false;
1107 }
1108 return;
1109 }
1110
1111 int looper_flags = 0;
1112 if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
1113 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1114 const int handle = mSensorInfo.keyAt(i);
1115 if (mService->getSensorFromHandle(handle).isWakeUpSensor()) {
1116 looper_flags |= ALOOPER_EVENT_INPUT;
1117 break;
1118 }
1119 }
1120 // If flags is still set to zero, we don't need to add this fd to the Looper, if
1121 // the fd has already been added, remove it. This is likely to happen when ALL the
1122 // events stored in the cache have been sent to the corresponding app.
1123 if (looper_flags == 0) {
1124 if (mHasLooperCallbacks) {
1125 ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd());
1126 looper->removeFd(mChannel->getSendFd());
1127 mHasLooperCallbacks = false;
1128 }
1129 return;
1130 }
1131 // Add the file descriptor to the Looper for receiving acknowledegments if the app has
1132 // registered for wake-up sensors OR for sending events in the cache.
1133 int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL);
1134 if (ret == 1) {
1135 ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd());
1136 mHasLooperCallbacks = true;
1137 } else {
1138 ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd());
1139 }
1140}
1141
1142void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
1143 Mutex::Autolock _l(mConnectionLock);
1144 ssize_t index = mSensorInfo.indexOfKey(handle);
1145 if (index >= 0) {
1146 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
1147 flushInfo.mPendingFlushEventsToSend++;
1148 }
1149}
1150
Mathias Agopianfc328812010-07-14 23:41:37 -07001151status_t SensorService::SensorEventConnection::sendEvents(
Aravind Akella0ec20662014-09-14 17:29:48 -07001152 sensors_event_t const* buffer, size_t numEvents,
Aravind Akella8493b792014-09-08 15:45:47 -07001153 sensors_event_t* scratch,
1154 SensorEventConnection const * const * mapFlushEventsToConnections) {
Mathias Agopiancf510012010-07-22 16:18:10 -07001155 // filter out events not for this connection
Mathias Agopian3560fb22010-07-22 21:24:39 -07001156 size_t count = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001157 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -07001158 if (scratch) {
1159 size_t i=0;
1160 while (i<numEvents) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001161 int32_t sensor_handle = buffer[i].sensor;
Aravind Akella724d91d2013-06-27 12:04:23 -07001162 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
1163 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001164 buffer[i].meta_data.sensor);
Aravind Akella8a969552014-09-28 17:52:41 -07001165 // Setting sensor_handle to the correct sensor to ensure the sensor events per
1166 // connection are filtered correctly. buffer[i].sensor is zero for meta_data
1167 // events.
Aravind Akella6c2664a2014-08-13 12:24:50 -07001168 sensor_handle = buffer[i].meta_data.sensor;
Aravind Akella724d91d2013-06-27 12:04:23 -07001169 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001170 ssize_t index = mSensorInfo.indexOfKey(sensor_handle);
Aravind Akella56ae4262014-07-10 16:01:10 -07001171 // Check if this connection has registered for this sensor. If not continue to the
1172 // next sensor_event.
1173 if (index < 0) {
1174 ++i;
1175 continue;
Aravind Akella4c8b9512013-09-05 17:03:38 -07001176 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001177
Aravind Akella56ae4262014-07-10 16:01:10 -07001178 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
Aravind Akella6c2664a2014-08-13 12:24:50 -07001179 // Check if there is a pending flush_complete event for this sensor on this connection.
Aravind Akella8493b792014-09-08 15:45:47 -07001180 if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
1181 this == mapFlushEventsToConnections[i]) {
1182 flushInfo.mFirstFlushPending = false;
1183 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
1184 buffer[i].meta_data.sensor);
1185 ++i;
1186 continue;
Aravind Akella56ae4262014-07-10 16:01:10 -07001187 }
1188
1189 // If there is a pending flush complete event for this sensor on this connection,
1190 // ignore the event and proceed to the next.
1191 if (flushInfo.mFirstFlushPending) {
1192 ++i;
1193 continue;
1194 }
1195
1196 do {
Aravind Akella8493b792014-09-08 15:45:47 -07001197 // Keep copying events into the scratch buffer as long as they are regular
1198 // sensor_events are from the same sensor_handle OR they are flush_complete_events
1199 // from the same sensor_handle AND the current connection is mapped to the
1200 // corresponding flush_complete_event.
Aravind Akella56ae4262014-07-10 16:01:10 -07001201 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
Aravind Akella8493b792014-09-08 15:45:47 -07001202 if (this == mapFlushEventsToConnections[i]) {
Aravind Akella56ae4262014-07-10 16:01:10 -07001203 scratch[count++] = buffer[i];
Aravind Akella56ae4262014-07-10 16:01:10 -07001204 }
1205 ++i;
1206 } else {
1207 // Regular sensor event, just copy it to the scratch buffer.
Aravind Akella4c8b9512013-09-05 17:03:38 -07001208 scratch[count++] = buffer[i++];
Aravind Akella56ae4262014-07-10 16:01:10 -07001209 }
Aravind Akella8493b792014-09-08 15:45:47 -07001210 } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle &&
1211 buffer[i].type != SENSOR_TYPE_META_DATA) ||
Aravind Akella56ae4262014-07-10 16:01:10 -07001212 (buffer[i].type == SENSOR_TYPE_META_DATA &&
Aravind Akella6c2664a2014-08-13 12:24:50 -07001213 buffer[i].meta_data.sensor == sensor_handle)));
Mathias Agopiancf510012010-07-22 16:18:10 -07001214 }
Mathias Agopian3560fb22010-07-22 21:24:39 -07001215 } else {
1216 scratch = const_cast<sensors_event_t *>(buffer);
1217 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -07001218 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001219
Aravind Akella6c2664a2014-08-13 12:24:50 -07001220 sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -07001221 // Early return if there are no events for this connection.
1222 if (count == 0) {
1223 return status_t(NO_ERROR);
1224 }
1225
1226#if DEBUG_CONNECTIONS
1227 mEventsReceived += count;
1228#endif
1229 if (mCacheSize != 0) {
1230 // There are some events in the cache which need to be sent first. Copy this buffer to
1231 // the end of cache.
1232 if (mCacheSize + count <= mMaxCacheSize) {
1233 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
1234 mCacheSize += count;
1235 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001236 // Check if any new sensors have registered on this connection which may have increased
1237 // the max cache size that is desired.
1238 if (mCacheSize + count < computeMaxCacheSizeLocked()) {
1239 reAllocateCacheLocked(scratch, count);
1240 return status_t(NO_ERROR);
1241 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001242 // Some events need to be dropped.
1243 int remaningCacheSize = mMaxCacheSize - mCacheSize;
1244 if (remaningCacheSize != 0) {
1245 memcpy(&mEventCache[mCacheSize], scratch,
1246 remaningCacheSize * sizeof(sensors_event_t));
1247 }
1248 int numEventsDropped = count - remaningCacheSize;
1249 countFlushCompleteEventsLocked(mEventCache, numEventsDropped);
1250 // Drop the first "numEventsDropped" in the cache.
1251 memmove(mEventCache, &mEventCache[numEventsDropped],
1252 (mCacheSize - numEventsDropped) * sizeof(sensors_event_t));
1253
1254 // Copy the remainingEvents in scratch buffer to the end of cache.
1255 memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize,
1256 numEventsDropped * sizeof(sensors_event_t));
1257 }
1258 return status_t(NO_ERROR);
1259 }
1260
Aravind Akella6c2664a2014-08-13 12:24:50 -07001261 int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count);
1262 if (index_wake_up_event >= 0) {
1263 scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1264 ++mWakeLockRefCount;
Aravind Akellae74baf62014-08-21 12:28:35 -07001265#if DEBUG_CONNECTIONS
1266 ++mTotalAcksNeeded;
1267#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001268 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001269
1270 // NOTE: ASensorEvent and sensors_event_t are the same type.
1271 ssize_t size = SensorEventQueue::write(mChannel,
1272 reinterpret_cast<ASensorEvent const*>(scratch), count);
1273 if (size < 0) {
1274 // Write error, copy events to local cache.
Aravind Akella6c2664a2014-08-13 12:24:50 -07001275 if (index_wake_up_event >= 0) {
1276 // If there was a wake_up sensor_event, reset the flag.
1277 scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
Aravind Akella0ec20662014-09-14 17:29:48 -07001278 if (mWakeLockRefCount > 0) {
1279 --mWakeLockRefCount;
1280 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001281#if DEBUG_CONNECTIONS
1282 --mTotalAcksNeeded;
1283#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001284 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001285 if (mEventCache == NULL) {
1286 mMaxCacheSize = computeMaxCacheSizeLocked();
1287 mEventCache = new sensors_event_t[mMaxCacheSize];
1288 mCacheSize = 0;
1289 }
1290 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
1291 mCacheSize += count;
1292
1293 // Add this file descriptor to the looper to get a callback when this fd is available for
1294 // writing.
Aravind Akella8a969552014-09-28 17:52:41 -07001295 updateLooperRegistrationLocked(mService->getLooper());
Aravind Akella56ae4262014-07-10 16:01:10 -07001296 return size;
1297 }
1298
1299#if DEBUG_CONNECTIONS
1300 if (size > 0) {
1301 mEventsSent += count;
1302 }
1303#endif
1304
1305 return size < 0 ? status_t(size) : status_t(NO_ERROR);
1306}
1307
Aravind Akella6c2664a2014-08-13 12:24:50 -07001308void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch,
1309 int count) {
1310 sensors_event_t *eventCache_new;
1311 const int new_cache_size = computeMaxCacheSizeLocked();
1312 // Allocate new cache, copy over events from the old cache & scratch, free up memory.
1313 eventCache_new = new sensors_event_t[new_cache_size];
1314 memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t));
1315 memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t));
1316
1317 ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize,
1318 new_cache_size);
1319
1320 delete mEventCache;
1321 mEventCache = eventCache_new;
1322 mCacheSize += count;
1323 mMaxCacheSize = new_cache_size;
1324}
1325
1326void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
1327 ASensorEvent flushCompleteEvent;
Aravind Akella0ec20662014-09-14 17:29:48 -07001328 memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
Aravind Akella6c2664a2014-08-13 12:24:50 -07001329 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
Aravind Akella6c2664a2014-08-13 12:24:50 -07001330 // Loop through all the sensors for this connection and check if there are any pending
1331 // flush complete events to be sent.
1332 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1333 FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
1334 while (flushInfo.mPendingFlushEventsToSend > 0) {
Aravind Akella0ec20662014-09-14 17:29:48 -07001335 const int sensor_handle = mSensorInfo.keyAt(i);
1336 flushCompleteEvent.meta_data.sensor = sensor_handle;
1337 if (mService->getSensorFromHandle(sensor_handle).isWakeUpSensor()) {
1338 flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1339 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001340 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
1341 if (size < 0) {
1342 return;
1343 }
1344 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
1345 flushCompleteEvent.meta_data.sensor);
1346 flushInfo.mPendingFlushEventsToSend--;
1347 }
1348 }
1349}
1350
Aravind Akella56ae4262014-07-10 16:01:10 -07001351void SensorService::SensorEventConnection::writeToSocketFromCacheLocked() {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001352 // At a time write at most half the size of the receiver buffer in SensorEventQueue OR
1353 // half the size of the socket buffer allocated in BitTube whichever is smaller.
1354 const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
1355 int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
1356 // Send pending flush complete events (if any)
Aravind Akella6c2664a2014-08-13 12:24:50 -07001357 sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -07001358 for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001359 const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
Aravind Akella6c2664a2014-08-13 12:24:50 -07001360 int index_wake_up_event =
1361 findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
1362 if (index_wake_up_event >= 0) {
1363 mEventCache[index_wake_up_event + numEventsSent].flags |=
1364 WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1365 ++mWakeLockRefCount;
Aravind Akellae74baf62014-08-21 12:28:35 -07001366#if DEBUG_CONNECTIONS
1367 ++mTotalAcksNeeded;
1368#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001369 }
Aravind Akella4c8b9512013-09-05 17:03:38 -07001370
Aravind Akella56ae4262014-07-10 16:01:10 -07001371 ssize_t size = SensorEventQueue::write(mChannel,
1372 reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
Aravind Akella6c2664a2014-08-13 12:24:50 -07001373 numEventsToWrite);
Aravind Akella56ae4262014-07-10 16:01:10 -07001374 if (size < 0) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001375 if (index_wake_up_event >= 0) {
1376 // If there was a wake_up sensor_event, reset the flag.
1377 mEventCache[index_wake_up_event + numEventsSent].flags &=
1378 ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
Aravind Akella0ec20662014-09-14 17:29:48 -07001379 if (mWakeLockRefCount > 0) {
1380 --mWakeLockRefCount;
1381 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001382#if DEBUG_CONNECTIONS
1383 --mTotalAcksNeeded;
1384#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001385 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001386 memmove(mEventCache, &mEventCache[numEventsSent],
1387 (mCacheSize - numEventsSent) * sizeof(sensors_event_t));
1388 ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ",
Aravind Akella6c2664a2014-08-13 12:24:50 -07001389 numEventsSent, mCacheSize);
Aravind Akella56ae4262014-07-10 16:01:10 -07001390 mCacheSize -= numEventsSent;
Aravind Akella56ae4262014-07-10 16:01:10 -07001391 return;
1392 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001393 numEventsSent += numEventsToWrite;
Aravind Akella56ae4262014-07-10 16:01:10 -07001394#if DEBUG_CONNECTIONS
Aravind Akella6c2664a2014-08-13 12:24:50 -07001395 mEventsSentFromCache += numEventsToWrite;
Aravind Akella56ae4262014-07-10 16:01:10 -07001396#endif
Aravind Akellab4099e72013-10-15 15:43:10 -07001397 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001398 ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize);
1399 // All events from the cache have been sent. Reset cache size to zero.
1400 mCacheSize = 0;
Aravind Akella8a969552014-09-28 17:52:41 -07001401 // There are no more events in the cache. We don't need to poll for write on the fd.
1402 // Update Looper registration.
1403 updateLooperRegistrationLocked(mService->getLooper());
Mathias Agopianfc328812010-07-14 23:41:37 -07001404}
1405
Aravind Akellac551eac2013-10-14 17:04:42 -07001406void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
Aravind Akella0ec20662014-09-14 17:29:48 -07001407 sensors_event_t const* scratch, const int numEventsDropped) {
Aravind Akella4c8b9512013-09-05 17:03:38 -07001408 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001409 // Count flushComplete events in the events that are about to the dropped. These will be sent
1410 // separately before the next batch of events.
1411 for (int j = 0; j < numEventsDropped; ++j) {
1412 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
1413 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
1414 flushInfo.mPendingFlushEventsToSend++;
1415 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
1416 flushInfo.mPendingFlushEventsToSend);
1417 }
1418 }
1419 return;
1420}
1421
Aravind Akella6c2664a2014-08-13 12:24:50 -07001422int SensorService::SensorEventConnection::findWakeUpSensorEventLocked(
1423 sensors_event_t const* scratch, const int count) {
Aravind Akella9a844cf2014-02-11 18:58:52 -08001424 for (int i = 0; i < count; ++i) {
1425 if (mService->isWakeUpSensorEvent(scratch[i])) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001426 return i;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001427 }
1428 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001429 return -1;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001430}
1431
Mathias Agopianb3989272011-10-20 18:42:02 -07001432sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -07001433{
1434 return mChannel;
1435}
1436
1437status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -07001438 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
1439 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -07001440{
1441 status_t err;
1442 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -07001443 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
1444 reservedFlags);
Aravind Akella56ae4262014-07-10 16:01:10 -07001445
Mathias Agopianfc328812010-07-14 23:41:37 -07001446 } else {
1447 err = mService->disable(this, handle);
1448 }
1449 return err;
1450}
1451
1452status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -07001453 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -07001454{
Aravind Akella724d91d2013-06-27 12:04:23 -07001455 return mService->setEventRate(this, handle, samplingPeriodNs);
Mathias Agopianfc328812010-07-14 23:41:37 -07001456}
1457
Aravind Akella701166d2013-10-08 14:59:26 -07001458status_t SensorService::SensorEventConnection::flush() {
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001459 return mService->flushSensor(this);
Aravind Akella724d91d2013-06-27 12:04:23 -07001460}
Aravind Akella4c8b9512013-09-05 17:03:38 -07001461
Aravind Akella8a969552014-09-28 17:52:41 -07001462int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) {
Aravind Akella56ae4262014-07-10 16:01:10 -07001463 if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) {
Aravind Akella8a969552014-09-28 17:52:41 -07001464 {
1465 // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount,
1466 // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
1467 // can release the wake-lock.
1468 ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
1469 Mutex::Autolock _l(mConnectionLock);
1470 mDead = true;
1471 mWakeLockRefCount = 0;
1472 updateLooperRegistrationLocked(mService->getLooper());
1473 }
1474 mService->checkWakeLockState();
1475 return 1;
Aravind Akella56ae4262014-07-10 16:01:10 -07001476 }
1477
1478 if (events & ALOOPER_EVENT_INPUT) {
Aravind Akella8a969552014-09-28 17:52:41 -07001479 uint32_t numAcks = 0;
1480 ssize_t ret = ::recv(fd, &numAcks, sizeof(numAcks), MSG_DONTWAIT);
Aravind Akella56ae4262014-07-10 16:01:10 -07001481 {
1482 Mutex::Autolock _l(mConnectionLock);
Aravind Akella8a969552014-09-28 17:52:41 -07001483 // Sanity check to ensure there are no read errors in recv, numAcks is always
1484 // within the range and not zero. If any of the above don't hold reset mWakeLockRefCount
1485 // to zero.
1486 if (ret != sizeof(numAcks) || numAcks > mWakeLockRefCount || numAcks == 0) {
1487 ALOGE("Looper read error ret=%d numAcks=%d", ret, numAcks);
1488 mWakeLockRefCount = 0;
1489 } else {
1490 mWakeLockRefCount -= numAcks;
Aravind Akella0ec20662014-09-14 17:29:48 -07001491 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001492#if DEBUG_CONNECTIONS
Aravind Akella8a969552014-09-28 17:52:41 -07001493 mTotalAcksReceived += numAcks;
Aravind Akellae74baf62014-08-21 12:28:35 -07001494#endif
Aravind Akella56ae4262014-07-10 16:01:10 -07001495 }
1496 // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released
1497 // here as checkWakeLockState() will need it.
1498 if (mWakeLockRefCount == 0) {
1499 mService->checkWakeLockState();
1500 }
1501 // continue getting callbacks.
1502 return 1;
1503 }
1504
1505 if (events & ALOOPER_EVENT_OUTPUT) {
1506 // send sensor data that is stored in mEventCache.
Aravind Akella9a844cf2014-02-11 18:58:52 -08001507 Mutex::Autolock _l(mConnectionLock);
Aravind Akella56ae4262014-07-10 16:01:10 -07001508 writeToSocketFromCacheLocked();
Aravind Akella9a844cf2014-02-11 18:58:52 -08001509 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001510 return 1;
1511}
1512
1513int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
1514 int fifoWakeUpSensors = 0;
1515 int fifoNonWakeUpSensors = 0;
1516 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1517 const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i));
1518 if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) {
1519 // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and
1520 // non wake_up sensors.
1521 if (sensor.isWakeUpSensor()) {
1522 fifoWakeUpSensors += sensor.getFifoReservedEventCount();
1523 } else {
1524 fifoNonWakeUpSensors += sensor.getFifoReservedEventCount();
1525 }
1526 } else {
1527 // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors.
1528 if (sensor.isWakeUpSensor()) {
1529 fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ?
1530 fifoWakeUpSensors : sensor.getFifoMaxEventCount();
1531
1532 } else {
1533 fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ?
1534 fifoNonWakeUpSensors : sensor.getFifoMaxEventCount();
1535
1536 }
1537 }
1538 }
1539 if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) {
1540 // It is extremely unlikely that there is a write failure in non batch mode. Return a cache
Aravind Akella6c2664a2014-08-13 12:24:50 -07001541 // size that is equal to that of the batch mode.
Aravind Akellae74baf62014-08-21 12:28:35 -07001542 // ALOGW("Write failure in non-batch mode");
Aravind Akella6c2664a2014-08-13 12:24:50 -07001543 return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t);
Aravind Akella56ae4262014-07-10 16:01:10 -07001544 }
1545 return fifoWakeUpSensors + fifoNonWakeUpSensors;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001546}
1547
Mathias Agopianfc328812010-07-14 23:41:37 -07001548// ---------------------------------------------------------------------------
1549}; // namespace android
1550