blob: 550107c4c5deafd9f92d1a959b90c41e95a6897d [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
Svetoslavb412f6e2015-04-29 16:50:41 -070034#include <binder/AppOpsManager.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070035#include <binder/BinderService.h>
Mathias Agopian451beee2010-07-19 15:03:55 -070036#include <binder/IServiceManager.h>
Mathias Agopian1cb13462011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070038
39#include <gui/ISensorServer.h>
40#include <gui/ISensorEventConnection.h>
Mathias Agopian907103b2012-04-02 18:38:02 -070041#include <gui/SensorEventQueue.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070042
43#include <hardware/sensors.h>
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070044#include <hardware_legacy/power.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070045
Mathias Agopian787ac1b2012-09-18 18:49:18 -070046#include "BatteryService.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070047#include "CorrectedGyroSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080048#include "GravitySensor.h"
49#include "LinearAccelerationSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070050#include "OrientationSensor.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080051#include "RotationVectorSensor.h"
Mathias Agopian984826c2011-05-17 22:54:42 -070052#include "SensorFusion.h"
53#include "SensorService.h"
Mathias Agopianfc328812010-07-14 23:41:37 -070054
55namespace android {
56// ---------------------------------------------------------------------------
57
Mathias Agopian33015422011-05-27 18:18:13 -070058/*
59 * Notes:
60 *
61 * - what about a gyro-corrected magnetic-field sensor?
Mathias Agopian33015422011-05-27 18:18:13 -070062 * - run mag sensor from time to time to force calibration
63 * - gravity sensor length is wrong (=> drift in linear-acc sensor)
64 *
65 */
66
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070067const char* SensorService::WAKE_LOCK_NAME = "SensorService";
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070068// Permissions.
Aravind Akellaa8814ce2015-06-10 15:15:57 -070069static const String16 sDataInjectionPermission("android.permission.LOCATION_HARDWARE");
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070070static const String16 sDump("android.permission.DUMP");
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -070071
Mathias Agopianfc328812010-07-14 23:41:37 -070072SensorService::SensorService()
Aravind Akella8a969552014-09-28 17:52:41 -070073 : mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED),
74 mWakeLockAcquired(false)
Mathias Agopianfc328812010-07-14 23:41:37 -070075{
76}
77
78void SensorService::onFirstRef()
79{
Steve Blocka5512372011-12-20 16:23:08 +000080 ALOGD("nuSensorService starting...");
Mathias Agopianf001c922010-11-11 17:58:51 -080081 SensorDevice& dev(SensorDevice::getInstance());
Mathias Agopianfc328812010-07-14 23:41:37 -070082
Mathias Agopianf001c922010-11-11 17:58:51 -080083 if (dev.initCheck() == NO_ERROR) {
Mathias Agopianf001c922010-11-11 17:58:51 -080084 sensor_t const* list;
Mathias Agopian7b2b32f2011-07-14 16:39:46 -070085 ssize_t count = dev.getSensorList(&list);
86 if (count > 0) {
87 ssize_t orientationIndex = -1;
88 bool hasGyro = false;
89 uint32_t virtualSensorsNeeds =
90 (1<<SENSOR_TYPE_GRAVITY) |
91 (1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
92 (1<<SENSOR_TYPE_ROTATION_VECTOR);
93
94 mLastEventSeen.setCapacity(count);
95 for (ssize_t i=0 ; i<count ; i++) {
96 registerSensor( new HardwareSensor(list[i]) );
97 switch (list[i].type) {
98 case SENSOR_TYPE_ORIENTATION:
99 orientationIndex = i;
100 break;
101 case SENSOR_TYPE_GYROSCOPE:
Mathias Agopian03193062013-05-10 19:32:39 -0700102 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700103 hasGyro = true;
104 break;
105 case SENSOR_TYPE_GRAVITY:
106 case SENSOR_TYPE_LINEAR_ACCELERATION:
107 case SENSOR_TYPE_ROTATION_VECTOR:
108 virtualSensorsNeeds &= ~(1<<list[i].type);
109 break;
110 }
Mathias Agopian50df2952010-07-19 19:09:10 -0700111 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700112
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700113 // it's safe to instantiate the SensorFusion object here
114 // (it wants to be instantiated after h/w sensors have been
115 // registered)
116 const SensorFusion& fusion(SensorFusion::getInstance());
Mathias Agopian984826c2011-05-17 22:54:42 -0700117
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700118 // build the sensor list returned to users
119 mUserSensorList = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700120
121 if (hasGyro) {
Mathias Agopian03193062013-05-10 19:32:39 -0700122 Sensor aSensor;
123
124 // Add Android virtual sensors if they're not already
125 // available in the HAL
126
127 aSensor = registerVirtualSensor( new RotationVectorSensor() );
128 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
129 mUserSensorList.add(aSensor);
130 }
131
132 aSensor = registerVirtualSensor( new GravitySensor(list, count) );
133 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
134 mUserSensorList.add(aSensor);
135 }
136
137 aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
138 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
139 mUserSensorList.add(aSensor);
140 }
141
142 aSensor = registerVirtualSensor( new OrientationSensor() );
143 if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
144 // if we are doing our own rotation-vector, also add
145 // the orientation sensor and remove the HAL provided one.
146 mUserSensorList.replaceAt(aSensor, orientationIndex);
147 }
148
Mathias Agopian33264862012-06-28 19:46:54 -0700149 // virtual debugging sensors are not added to mUserSensorList
Mathias Agopian03193062013-05-10 19:32:39 -0700150 registerVirtualSensor( new CorrectedGyroSensor(list, count) );
Mathias Agopian33264862012-06-28 19:46:54 -0700151 registerVirtualSensor( new GyroDriftSensor() );
152 }
153
Mathias Agopian33264862012-06-28 19:46:54 -0700154 // debugging sensor list
Mathias Agopian03193062013-05-10 19:32:39 -0700155 mUserSensorListDebug = mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700156
Aravind Akella5466c3d2014-08-22 16:11:10 -0700157 // Check if the device really supports batching by looking at the FIFO event
158 // counts for each sensor.
159 bool batchingSupported = false;
Svetoslavb412f6e2015-04-29 16:50:41 -0700160 for (size_t i = 0; i < mSensorList.size(); ++i) {
Aravind Akella5466c3d2014-08-22 16:11:10 -0700161 if (mSensorList[i].getFifoMaxEventCount() > 0) {
162 batchingSupported = true;
163 break;
164 }
165 }
166
167 if (batchingSupported) {
168 // Increase socket buffer size to a max of 100 KB for batching capabilities.
169 mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
170 } else {
171 mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
172 }
173
174 // Compare the socketBufferSize value against the system limits and limit
175 // it to maxSystemSocketBufferSize if necessary.
Aravind Akella4c8b9512013-09-05 17:03:38 -0700176 FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r");
177 char line[128];
178 if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
179 line[sizeof(line) - 1] = '\0';
Aravind Akella5466c3d2014-08-22 16:11:10 -0700180 size_t maxSystemSocketBufferSize;
181 sscanf(line, "%zu", &maxSystemSocketBufferSize);
182 if (mSocketBufferSize > maxSystemSocketBufferSize) {
183 mSocketBufferSize = maxSystemSocketBufferSize;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700184 }
185 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700186 if (fp) {
187 fclose(fp);
188 }
189
Aravind Akella9a844cf2014-02-11 18:58:52 -0800190 mWakeLockAcquired = false;
Aravind Akella56ae4262014-07-10 16:01:10 -0700191 mLooper = new Looper(false);
Aravind Akella8493b792014-09-08 15:45:47 -0700192 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
193 mSensorEventBuffer = new sensors_event_t[minBufferSize];
194 mSensorEventScratch = new sensors_event_t[minBufferSize];
195 mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize];
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700196 mCurrentOperatingMode = NORMAL;
Aravind Akella7830ef32014-10-07 14:13:12 -0700197
Aravind Akella18d6d512015-06-18 14:18:28 -0700198 mNextSensorRegIndex = 0;
199 for (int i = 0; i < SENSOR_REGISTRATIONS_BUF_SIZE; ++i) {
200 mLastNSensorRegistrations.push();
201 }
202
203 mInitCheck = NO_ERROR;
Aravind Akellab4373ac2014-10-29 17:55:20 -0700204 mAckReceiver = new SensorEventAckReceiver(this);
205 mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY);
Aravind Akella7830ef32014-10-07 14:13:12 -0700206 run("SensorService", PRIORITY_URGENT_DISPLAY);
Mathias Agopian7b2b32f2011-07-14 16:39:46 -0700207 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700208 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700209}
210
Mathias Agopian03193062013-05-10 19:32:39 -0700211Sensor SensorService::registerSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800212{
213 sensors_event_t event;
214 memset(&event, 0, sizeof(event));
215
216 const Sensor sensor(s->getSensor());
217 // add to the sensor list (returned to clients)
218 mSensorList.add(sensor);
219 // add to our handle->SensorInterface mapping
220 mSensorMap.add(sensor.getHandle(), s);
221 // create an entry in the mLastEventSeen array
Aravind Akella444f2672015-05-07 12:40:52 -0700222 mLastEventSeen.add(sensor.getHandle(), NULL);
Mathias Agopian03193062013-05-10 19:32:39 -0700223
224 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800225}
226
Mathias Agopian03193062013-05-10 19:32:39 -0700227Sensor SensorService::registerVirtualSensor(SensorInterface* s)
Mathias Agopianf001c922010-11-11 17:58:51 -0800228{
Mathias Agopian03193062013-05-10 19:32:39 -0700229 Sensor sensor = registerSensor(s);
Mathias Agopianf001c922010-11-11 17:58:51 -0800230 mVirtualSensorList.add( s );
Mathias Agopian03193062013-05-10 19:32:39 -0700231 return sensor;
Mathias Agopianf001c922010-11-11 17:58:51 -0800232}
233
Mathias Agopianfc328812010-07-14 23:41:37 -0700234SensorService::~SensorService()
235{
Mathias Agopianf001c922010-11-11 17:58:51 -0800236 for (size_t i=0 ; i<mSensorMap.size() ; i++)
237 delete mSensorMap.valueAt(i);
Mathias Agopianfc328812010-07-14 23:41:37 -0700238}
239
Aravind Akella4949c502015-02-11 15:54:35 -0800240status_t SensorService::dump(int fd, const Vector<String16>& args)
Mathias Agopianfc328812010-07-14 23:41:37 -0700241{
Mathias Agopianfc328812010-07-14 23:41:37 -0700242 String8 result;
Mathias Agopian1cb13462011-06-27 16:05:52 -0700243 if (!PermissionCache::checkCallingPermission(sDump)) {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700244 result.appendFormat("Permission Denial: "
Aravind Akella70018042014-04-07 22:52:37 +0000245 "can't dump SensorService from pid=%d, uid=%d\n",
Mathias Agopianfc328812010-07-14 23:41:37 -0700246 IPCThreadState::self()->getCallingPid(),
247 IPCThreadState::self()->getCallingUid());
Aravind Akella444f2672015-05-07 12:40:52 -0700248 } else {
Aravind Akella4949c502015-02-11 15:54:35 -0800249 if (args.size() > 1) {
250 return INVALID_OPERATION;
251 }
252 Mutex::Autolock _l(mLock);
253 SensorDevice& dev(SensorDevice::getInstance());
Aravind Akella444f2672015-05-07 12:40:52 -0700254 if (args.size() == 1 && args[0] == String16("restrict")) {
255 // If already in restricted mode. Ignore.
256 if (mCurrentOperatingMode == RESTRICTED) {
257 return status_t(NO_ERROR);
258 }
259 // If in any mode other than normal, ignore.
260 if (mCurrentOperatingMode != NORMAL) {
261 return INVALID_OPERATION;
262 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700263 mCurrentOperatingMode = RESTRICTED;
Aravind Akella4949c502015-02-11 15:54:35 -0800264 dev.disableAllSensors();
265 // Clear all pending flush connections for all active sensors. If one of the active
266 // connections has called flush() and the underlying sensor has been disabled before a
267 // flush complete event is returned, we need to remove the connection from this queue.
268 for (size_t i=0 ; i< mActiveSensors.size(); ++i) {
269 mActiveSensors.valueAt(i)->clearAllPendingFlushConnections();
270 }
Aravind Akella444f2672015-05-07 12:40:52 -0700271 return status_t(NO_ERROR);
272 } else if (args.size() == 1 && args[0] == String16("enable")) {
273 // If currently in restricted mode, reset back to NORMAL mode else ignore.
274 if (mCurrentOperatingMode == RESTRICTED) {
275 mCurrentOperatingMode = NORMAL;
276 dev.enableAllSensors();
277 }
278 return status_t(NO_ERROR);
Aravind Akellaee155ca2015-06-24 08:31:32 -0700279 } else if (mSensorList.size() == 0) {
280 result.append("No Sensors on the device\n");
Aravind Akella444f2672015-05-07 12:40:52 -0700281 } else {
282 // Default dump the sensor list and debugging information.
283 result.append("Sensor List:\n");
284 for (size_t i=0 ; i<mSensorList.size() ; i++) {
285 const Sensor& s(mSensorList[i]);
286 result.appendFormat(
287 "%-15s| %-10s| version=%d |%-20s| 0x%08x | \"%s\" | type=%d |",
288 s.getName().string(),
289 s.getVendor().string(),
290 s.getVersion(),
291 s.getStringType().string(),
292 s.getHandle(),
293 s.getRequiredPermission().string(),
294 s.getType());
Mathias Agopian3560fb22010-07-22 21:24:39 -0700295
Aravind Akella444f2672015-05-07 12:40:52 -0700296 const int reportingMode = s.getReportingMode();
297 if (reportingMode == AREPORTING_MODE_CONTINUOUS) {
298 result.append(" continuous | ");
299 } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) {
300 result.append(" on-change | ");
301 } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) {
302 result.append(" one-shot | ");
303 } else {
304 result.append(" special-trigger | ");
305 }
306
307 if (s.getMaxDelay() > 0) {
308 result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay());
309 } else {
310 result.appendFormat("maxDelay=%dus |", s.getMaxDelay());
311 }
312
313 if (s.getMinDelay() > 0) {
314 result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay());
315 } else {
316 result.appendFormat("minDelay=%dus |", s.getMinDelay());
317 }
318
319 if (s.getFifoMaxEventCount() > 0) {
320 result.appendFormat("FifoMax=%d events | ",
321 s.getFifoMaxEventCount());
322 } else {
323 result.append("no batching | ");
324 }
325
326 if (s.isWakeUpSensor()) {
327 result.appendFormat("wakeUp | ");
328 } else {
329 result.appendFormat("non-wakeUp | ");
330 }
331
Aravind Akella18d6d512015-06-18 14:18:28 -0700332 int bufIndex = mLastEventSeen.indexOfKey(s.getHandle());
333 if (bufIndex >= 0) {
334 const CircularBuffer* buf = mLastEventSeen.valueAt(bufIndex);
335 if (buf != NULL && s.getRequiredPermission().isEmpty()) {
336 buf->printBuffer(result);
337 } else {
338 result.append("last=<> \n");
339 }
Aravind Akella444f2672015-05-07 12:40:52 -0700340 }
341 result.append("\n");
342 }
343 SensorFusion::getInstance().dump(result);
344 SensorDevice::getInstance().dump(result);
345
346 result.append("Active sensors:\n");
347 for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
348 int handle = mActiveSensors.keyAt(i);
349 result.appendFormat("%s (handle=0x%08x, connections=%zu)\n",
350 getSensorName(handle).string(),
351 handle,
352 mActiveSensors.valueAt(i)->getNumConnections());
Aravind Akella6c2664a2014-08-13 12:24:50 -0700353 }
354
Aravind Akella444f2672015-05-07 12:40:52 -0700355 result.appendFormat("Socket Buffer size = %d events\n",
356 mSocketBufferSize/sizeof(sensors_event_t));
Aravind Akella18d6d512015-06-18 14:18:28 -0700357 result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" :
358 "not held");
Aravind Akella444f2672015-05-07 12:40:52 -0700359 result.appendFormat("Mode :");
360 switch(mCurrentOperatingMode) {
361 case NORMAL:
362 result.appendFormat(" NORMAL\n");
363 break;
364 case RESTRICTED:
365 result.appendFormat(" RESTRICTED\n");
366 break;
367 case DATA_INJECTION:
368 result.appendFormat(" DATA_INJECTION\n");
Mathias Agopianba02cd22013-07-03 16:20:57 -0700369 }
Aravind Akella444f2672015-05-07 12:40:52 -0700370 result.appendFormat("%zd active connections\n", mActiveConnections.size());
Aravind Akella0e025c52014-06-03 19:19:57 -0700371
Aravind Akella444f2672015-05-07 12:40:52 -0700372 for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
373 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
374 if (connection != 0) {
375 result.appendFormat("Connection Number: %zu \n", i);
376 connection->dump(result);
377 }
Aravind Akella4c8b9512013-09-05 17:03:38 -0700378 }
Aravind Akella18d6d512015-06-18 14:18:28 -0700379
380 result.appendFormat("Previous Registrations:\n");
381 // Log in the reverse chronological order.
382 int currentIndex = (mNextSensorRegIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) %
383 SENSOR_REGISTRATIONS_BUF_SIZE;
384 const int startIndex = currentIndex;
385 do {
386 const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[currentIndex];
387 if (SensorRegistrationInfo::isSentinel(reg_info)) {
388 // Ignore sentinel, proceed to next item.
389 currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) %
390 SENSOR_REGISTRATIONS_BUF_SIZE;
391 continue;
392 }
393 if (reg_info.mActivated) {
394 result.appendFormat("%02d:%02d:%02d activated package=%s handle=0x%08x "
395 "samplingRate=%dus maxReportLatency=%dus\n",
396 reg_info.mHour, reg_info.mMin, reg_info.mSec,
397 reg_info.mPackageName.string(), reg_info.mSensorHandle,
398 reg_info.mSamplingRateUs, reg_info.mMaxReportLatencyUs);
399 } else {
400 result.appendFormat("%02d:%02d:%02d de-activated package=%s handle=0x%08x\n",
401 reg_info.mHour, reg_info.mMin, reg_info.mSec,
402 reg_info.mPackageName.string(), reg_info.mSensorHandle);
403 }
404 currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) %
405 SENSOR_REGISTRATIONS_BUF_SIZE;
406 } while(startIndex != currentIndex);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700407 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700408 }
409 write(fd, result.string(), result.size());
410 return NO_ERROR;
411}
412
Aravind Akella9a844cf2014-02-11 18:58:52 -0800413void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700414 sensors_event_t const* buffer, const int count) {
415 for (int i=0 ; i<count ; i++) {
416 int handle = buffer[i].sensor;
Aravind Akella8493b792014-09-08 15:45:47 -0700417 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
418 handle = buffer[i].meta_data.sensor;
419 }
Aravind Akella0e025c52014-06-03 19:19:57 -0700420 if (connection->hasSensor(handle)) {
421 SensorInterface* sensor = mSensorMap.valueFor(handle);
422 // If this buffer has an event from a one_shot sensor and this connection is registered
423 // for this particular one_shot sensor, try cleaning up the connection.
424 if (sensor != NULL &&
425 sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
426 sensor->autoDisable(connection.get(), handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800427 cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700428 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700429
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700430 }
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700431 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700432}
433
Mathias Agopianfc328812010-07-14 23:41:37 -0700434bool SensorService::threadLoop()
435{
Steve Blocka5512372011-12-20 16:23:08 +0000436 ALOGD("nuSensorService thread starting...");
Mathias Agopianfc328812010-07-14 23:41:37 -0700437
Mathias Agopian90ed3e82013-09-09 23:36:25 -0700438 // each virtual sensor could generate an event per "real" event, that's why we need
439 // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
440 // in practice, this is too aggressive, but guaranteed to be enough.
441 const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
442 const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
443
Mathias Agopianf001c922010-11-11 17:58:51 -0800444 SensorDevice& device(SensorDevice::getInstance());
445 const size_t vcount = mVirtualSensorList.size();
Mathias Agopianfc328812010-07-14 23:41:37 -0700446
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700447 const int halVersion = device.getHalDeviceVersion();
Mathias Agopianfc328812010-07-14 23:41:37 -0700448 do {
Aravind Akella8493b792014-09-08 15:45:47 -0700449 ssize_t count = device.poll(mSensorEventBuffer, numEventMax);
450 if (count < 0) {
Steve Blockf5a12302012-01-06 19:20:56 +0000451 ALOGE("sensor poll failed (%s)", strerror(-count));
Mathias Agopianfc328812010-07-14 23:41:37 -0700452 break;
453 }
Aravind Akella56ae4262014-07-10 16:01:10 -0700454
455 // Reset sensors_event_t.flags to zero for all events in the buffer.
456 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700457 mSensorEventBuffer[i].flags = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -0700458 }
Aravind Akellae148bc22014-09-24 22:12:58 -0700459
460 // Make a copy of the connection vector as some connections may be removed during the
461 // course of this loop (especially when one-shot sensor events are present in the
462 // sensor_event buffer). Promote all connections to StrongPointers before the lock is
463 // acquired. If the destructor of the sp gets called when the lock is acquired, it may
464 // result in a deadlock as ~SensorEventConnection() needs to acquire mLock again for
465 // cleanup. So copy all the strongPointers to a vector before the lock is acquired.
466 SortedVector< sp<SensorEventConnection> > activeConnections;
Aravind Akellab4373ac2014-10-29 17:55:20 -0700467 populateActiveConnections(&activeConnections);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800468 Mutex::Autolock _l(mLock);
469 // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The
470 // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock,
471 // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should
472 // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and
473 // releasing the wakelock.
474 bool bufferHasWakeUpEvent = false;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700475 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700476 if (isWakeUpSensorEvent(mSensorEventBuffer[i])) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800477 bufferHasWakeUpEvent = true;
478 break;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700479 }
480 }
481
Aravind Akella9a844cf2014-02-11 18:58:52 -0800482 if (bufferHasWakeUpEvent && !mWakeLockAcquired) {
Aravind Akellab4373ac2014-10-29 17:55:20 -0700483 setWakeLockAcquiredLocked(true);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800484 }
Aravind Akella8493b792014-09-08 15:45:47 -0700485 recordLastValueLocked(mSensorEventBuffer, count);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800486
Mathias Agopianf001c922010-11-11 17:58:51 -0800487 // handle virtual sensors
488 if (count && vcount) {
Aravind Akella8493b792014-09-08 15:45:47 -0700489 sensors_event_t const * const event = mSensorEventBuffer;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800490 const size_t activeVirtualSensorCount = mActiveVirtualSensors.size();
Mathias Agopianf001c922010-11-11 17:58:51 -0800491 if (activeVirtualSensorCount) {
492 size_t k = 0;
Mathias Agopian984826c2011-05-17 22:54:42 -0700493 SensorFusion& fusion(SensorFusion::getInstance());
494 if (fusion.isEnabled()) {
495 for (size_t i=0 ; i<size_t(count) ; i++) {
496 fusion.process(event[i]);
497 }
498 }
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700499 for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
Mathias Agopianf001c922010-11-11 17:58:51 -0800500 for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700501 if (count + k >= minBufferSize) {
502 ALOGE("buffer too small to hold all events: "
Mark Salyzyndb458612014-06-10 14:50:02 -0700503 "count=%zd, k=%zu, size=%zu",
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700504 count, k, minBufferSize);
505 break;
506 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800507 sensors_event_t out;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800508 SensorInterface* si = mActiveVirtualSensors.valueAt(j);
Mathias Agopiand1920ff2012-05-29 19:46:14 -0700509 if (si->process(&out, event[i])) {
Aravind Akella8493b792014-09-08 15:45:47 -0700510 mSensorEventBuffer[count + k] = out;
Mathias Agopianf001c922010-11-11 17:58:51 -0800511 k++;
512 }
513 }
514 }
515 if (k) {
516 // record the last synthesized values
Aravind Akella8493b792014-09-08 15:45:47 -0700517 recordLastValueLocked(&mSensorEventBuffer[count], k);
Mathias Agopianf001c922010-11-11 17:58:51 -0800518 count += k;
519 // sort the buffer by time-stamps
Aravind Akella8493b792014-09-08 15:45:47 -0700520 sortEventBuffer(mSensorEventBuffer, count);
Mathias Agopianfc328812010-07-14 23:41:37 -0700521 }
522 }
523 }
524
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700525 // handle backward compatibility for RotationVector sensor
526 if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
527 for (int i = 0; i < count; i++) {
Aravind Akella8493b792014-09-08 15:45:47 -0700528 if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700529 // All the 4 components of the quaternion should be available
530 // No heading accuracy. Set it to -1
Aravind Akella8493b792014-09-08 15:45:47 -0700531 mSensorEventBuffer[i].data[4] = -1;
532 }
533 }
534 }
535
536 // Map flush_complete_events in the buffer to SensorEventConnections which called
537 // flush on the hardware sensor. mapFlushEventsToConnections[i] will be the
538 // SensorEventConnection mapped to the corresponding flush_complete_event in
539 // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise).
540 for (int i = 0; i < count; ++i) {
541 mMapFlushEventsToConnections[i] = NULL;
542 if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) {
543 const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor;
544 SensorRecord* rec = mActiveSensors.valueFor(sensor_handle);
545 if (rec != NULL) {
546 mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection();
547 rec->removeFirstPendingFlushConnection();
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700548 }
549 }
550 }
551
Aravind Akella9a844cf2014-02-11 18:58:52 -0800552 // Send our events to clients. Check the state of wake lock for each client and release the
553 // lock if none of the clients need it.
554 bool needsWakeLock = false;
Aravind Akella8493b792014-09-08 15:45:47 -0700555 size_t numConnections = activeConnections.size();
556 for (size_t i=0 ; i < numConnections; ++i) {
Aravind Akellae148bc22014-09-24 22:12:58 -0700557 if (activeConnections[i] != 0) {
558 activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch,
Aravind Akella8493b792014-09-08 15:45:47 -0700559 mMapFlushEventsToConnections);
Aravind Akellae148bc22014-09-24 22:12:58 -0700560 needsWakeLock |= activeConnections[i]->needsWakeLock();
Aravind Akella8493b792014-09-08 15:45:47 -0700561 // If the connection has one-shot sensors, it may be cleaned up after first trigger.
562 // Early check for one-shot sensors.
Aravind Akellae148bc22014-09-24 22:12:58 -0700563 if (activeConnections[i]->hasOneShotSensors()) {
564 cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer,
565 count);
Aravind Akella8493b792014-09-08 15:45:47 -0700566 }
Mathias Agopianf001c922010-11-11 17:58:51 -0800567 }
568 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700569
Aravind Akella9a844cf2014-02-11 18:58:52 -0800570 if (mWakeLockAcquired && !needsWakeLock) {
Aravind Akellab4373ac2014-10-29 17:55:20 -0700571 setWakeLockAcquiredLocked(false);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800572 }
Aravind Akella8493b792014-09-08 15:45:47 -0700573 } while (!Thread::exitPending());
Mathias Agopianfc328812010-07-14 23:41:37 -0700574
Steve Block3c20fbe2012-01-05 23:22:43 +0000575 ALOGW("Exiting SensorService::threadLoop => aborting...");
Mathias Agopian1a623012011-11-09 17:50:15 -0800576 abort();
Mathias Agopianfc328812010-07-14 23:41:37 -0700577 return false;
578}
579
Aravind Akella56ae4262014-07-10 16:01:10 -0700580sp<Looper> SensorService::getLooper() const {
581 return mLooper;
582}
583
Aravind Akellab4373ac2014-10-29 17:55:20 -0700584void SensorService::resetAllWakeLockRefCounts() {
585 SortedVector< sp<SensorEventConnection> > activeConnections;
586 populateActiveConnections(&activeConnections);
587 {
588 Mutex::Autolock _l(mLock);
589 for (size_t i=0 ; i < activeConnections.size(); ++i) {
590 if (activeConnections[i] != 0) {
591 activeConnections[i]->resetWakeLockRefCount();
592 }
593 }
594 setWakeLockAcquiredLocked(false);
595 }
596}
597
598void SensorService::setWakeLockAcquiredLocked(bool acquire) {
599 if (acquire) {
600 if (!mWakeLockAcquired) {
601 acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
602 mWakeLockAcquired = true;
603 }
604 mLooper->wake();
605 } else {
606 if (mWakeLockAcquired) {
607 release_wake_lock(WAKE_LOCK_NAME);
608 mWakeLockAcquired = false;
609 }
610 }
611}
612
Aravind Akellab4373ac2014-10-29 17:55:20 -0700613bool SensorService::isWakeLockAcquired() {
614 Mutex::Autolock _l(mLock);
615 return mWakeLockAcquired;
616}
617
Aravind Akella56ae4262014-07-10 16:01:10 -0700618bool SensorService::SensorEventAckReceiver::threadLoop() {
619 ALOGD("new thread SensorEventAckReceiver");
Aravind Akellab4373ac2014-10-29 17:55:20 -0700620 sp<Looper> looper = mService->getLooper();
Aravind Akella56ae4262014-07-10 16:01:10 -0700621 do {
Aravind Akellab4373ac2014-10-29 17:55:20 -0700622 bool wakeLockAcquired = mService->isWakeLockAcquired();
623 int timeout = -1;
624 if (wakeLockAcquired) timeout = 5000;
625 int ret = looper->pollOnce(timeout);
626 if (ret == ALOOPER_POLL_TIMEOUT) {
627 mService->resetAllWakeLockRefCounts();
628 }
Aravind Akella56ae4262014-07-10 16:01:10 -0700629 } while(!Thread::exitPending());
630 return false;
631}
632
Aravind Akella9a844cf2014-02-11 18:58:52 -0800633void SensorService::recordLastValueLocked(
Aravind Akella4b847042014-03-03 19:02:46 -0800634 const sensors_event_t* buffer, size_t count) {
Aravind Akella4b847042014-03-03 19:02:46 -0800635 for (size_t i = 0; i < count; i++) {
Aravind Akella444f2672015-05-07 12:40:52 -0700636 if (buffer[i].type != SENSOR_TYPE_META_DATA) {
637 CircularBuffer* &circular_buf = mLastEventSeen.editValueFor(buffer[i].sensor);
638 if (circular_buf == NULL) {
639 circular_buf = new CircularBuffer(buffer[i].type);
Aravind Akella4b847042014-03-03 19:02:46 -0800640 }
Aravind Akella444f2672015-05-07 12:40:52 -0700641 circular_buf->addEvent(buffer[i]);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800642 }
643 }
Mathias Agopian94e8f682010-11-10 17:50:28 -0800644}
645
Mathias Agopianf001c922010-11-11 17:58:51 -0800646void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
647{
648 struct compar {
649 static int cmp(void const* lhs, void const* rhs) {
650 sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
651 sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
Mathias Agopiana5c106a2012-04-19 18:18:24 -0700652 return l->timestamp - r->timestamp;
Mathias Agopianf001c922010-11-11 17:58:51 -0800653 }
654 };
655 qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
656}
657
Mathias Agopian5d270722010-07-19 15:20:39 -0700658String8 SensorService::getSensorName(int handle) const {
Mathias Agopian010e4222011-06-08 20:06:50 -0700659 size_t count = mUserSensorList.size();
Mathias Agopian5d270722010-07-19 15:20:39 -0700660 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian010e4222011-06-08 20:06:50 -0700661 const Sensor& sensor(mUserSensorList[i]);
Mathias Agopian5d270722010-07-19 15:20:39 -0700662 if (sensor.getHandle() == handle) {
663 return sensor.getName();
664 }
665 }
666 String8 result("unknown");
667 return result;
668}
669
Aravind Akellab4099e72013-10-15 15:43:10 -0700670bool SensorService::isVirtualSensor(int handle) const {
671 SensorInterface* sensor = mSensorMap.valueFor(handle);
672 return sensor->isVirtual();
673}
674
Aravind Akella9a844cf2014-02-11 18:58:52 -0800675bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const {
Sean Wan7869e222014-07-14 17:07:33 -0700676 int handle = event.sensor;
677 if (event.type == SENSOR_TYPE_META_DATA) {
678 handle = event.meta_data.sensor;
679 }
680 SensorInterface* sensor = mSensorMap.valueFor(handle);
681 return sensor != NULL && sensor->getSensor().isWakeUpSensor();
Aravind Akella9a844cf2014-02-11 18:58:52 -0800682}
683
Aravind Akella6c2664a2014-08-13 12:24:50 -0700684SensorService::SensorRecord * SensorService::getSensorRecord(int handle) {
685 return mActiveSensors.valueFor(handle);
686}
687
Svetoslavb412f6e2015-04-29 16:50:41 -0700688Vector<Sensor> SensorService::getSensorList(const String16& opPackageName)
Mathias Agopianfc328812010-07-14 23:41:37 -0700689{
Mathias Agopian33264862012-06-28 19:46:54 -0700690 char value[PROPERTY_VALUE_MAX];
691 property_get("debug.sensors", value, "0");
Aravind Akella70018042014-04-07 22:52:37 +0000692 const Vector<Sensor>& initialSensorList = (atoi(value)) ?
693 mUserSensorListDebug : mUserSensorList;
694 Vector<Sensor> accessibleSensorList;
695 for (size_t i = 0; i < initialSensorList.size(); i++) {
696 Sensor sensor = initialSensorList[i];
Svetoslavb412f6e2015-04-29 16:50:41 -0700697 if (canAccessSensor(sensor, "getSensorList", opPackageName)) {
Aravind Akella70018042014-04-07 22:52:37 +0000698 accessibleSensorList.add(sensor);
699 } else {
Svetoslavb412f6e2015-04-29 16:50:41 -0700700 ALOGI("Skipped sensor %s because it requires permission %s and app op %d",
Bernhard Rosenkränzer5f619932014-11-17 21:06:20 +0100701 sensor.getName().string(),
Svetoslavb412f6e2015-04-29 16:50:41 -0700702 sensor.getRequiredPermission().string(),
703 sensor.getRequiredAppOp());
Aravind Akella70018042014-04-07 22:52:37 +0000704 }
Mathias Agopian33264862012-06-28 19:46:54 -0700705 }
Aravind Akella70018042014-04-07 22:52:37 +0000706 return accessibleSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700707}
708
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700709sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -0700710 int requestedMode, const String16& opPackageName) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700711 // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION.
712 if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) {
713 return NULL;
714 }
715 // DATA_INJECTION mode needs to have the required permissions set.
716 if (requestedMode == DATA_INJECTION && !hasDataInjectionPermissions()) {
717 return NULL;
718 }
719
720 Mutex::Autolock _l(mLock);
Mathias Agopian5307d172012-09-18 17:02:43 -0700721 uid_t uid = IPCThreadState::self()->getCallingUid();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700722 sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -0700723 requestedMode == DATA_INJECTION, opPackageName));
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700724 if (requestedMode == DATA_INJECTION) {
725 if (mActiveConnections.indexOf(result) < 0) {
726 mActiveConnections.add(result);
727 }
728 // Add the associated file descriptor to the Looper for polling whenever there is data to
729 // be injected.
730 result->updateLooperRegistration(mLooper);
731 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700732 return result;
733}
734
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700735status_t SensorService::enableDataInjection(int requestedMode) {
736 if (!hasDataInjectionPermissions()) {
737 return INVALID_OPERATION;
738 }
739 Mutex::Autolock _l(mLock);
740 ALOGD_IF(DEBUG_CONNECTIONS, "SensorService::enableDataInjection %d", requestedMode);
741 SensorDevice& dev(SensorDevice::getInstance());
742 status_t err(NO_ERROR);
743 if (requestedMode == DATA_INJECTION) {
744 if (mCurrentOperatingMode == NORMAL) {
745 dev.disableAllSensors();
746 err = dev.setMode(requestedMode);
747 if (err == NO_ERROR) {
748 mCurrentOperatingMode = DATA_INJECTION;
749 } else {
750 // Re-enable sensors.
751 dev.enableAllSensors();
752 }
753 } else if (mCurrentOperatingMode == DATA_INJECTION) {
754 // Already in DATA_INJECTION mode. Treat this as a no_op.
755 return NO_ERROR;
756 } else {
757 // Transition to data injection mode supported only from NORMAL mode.
758 return INVALID_OPERATION;
759 }
760 } else if (requestedMode == NORMAL && mCurrentOperatingMode != NORMAL) {
761 err = resetToNormalModeLocked();
762 }
763 return err;
764}
765
766status_t SensorService::resetToNormalMode() {
767 Mutex::Autolock _l(mLock);
768 return resetToNormalModeLocked();
769}
770
771status_t SensorService::resetToNormalModeLocked() {
772 SensorDevice& dev(SensorDevice::getInstance());
773 dev.enableAllSensors();
774 status_t err = dev.setMode(NORMAL);
775 mCurrentOperatingMode = NORMAL;
776 return err;
777}
778
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800779void SensorService::cleanupConnection(SensorEventConnection* c)
Mathias Agopianfc328812010-07-14 23:41:37 -0700780{
781 Mutex::Autolock _l(mLock);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800782 const wp<SensorEventConnection> connection(c);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700783 size_t size = mActiveSensors.size();
Mark Salyzyndb458612014-06-10 14:50:02 -0700784 ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700785 for (size_t i=0 ; i<size ; ) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800786 int handle = mActiveSensors.keyAt(i);
787 if (c->hasSensor(handle)) {
Mark Salyzyndb458612014-06-10 14:50:02 -0700788 ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800789 SensorInterface* sensor = mSensorMap.valueFor( handle );
Steve Blockf5a12302012-01-06 19:20:56 +0000790 ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
Mathias Agopianf001c922010-11-11 17:58:51 -0800791 if (sensor) {
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800792 sensor->activate(c, false);
Mathias Agopianf001c922010-11-11 17:58:51 -0800793 }
Aravind Akella8a969552014-09-28 17:52:41 -0700794 c->removeSensor(handle);
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800795 }
796 SensorRecord* rec = mActiveSensors.valueAt(i);
Mark Salyzyndb458612014-06-10 14:50:02 -0700797 ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle);
Steve Blocka5512372011-12-20 16:23:08 +0000798 ALOGD_IF(DEBUG_CONNECTIONS,
Mark Salyzyndb458612014-06-10 14:50:02 -0700799 "removing connection %p for sensor[%zu].handle=0x%08x",
Mathias Agopiana1b7db92011-05-27 16:23:58 -0700800 c, i, handle);
801
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800802 if (rec && rec->removeConnection(connection)) {
Steve Blocka5512372011-12-20 16:23:08 +0000803 ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700804 mActiveSensors.removeItemsAt(i, 1);
Mathias Agopianf001c922010-11-11 17:58:51 -0800805 mActiveVirtualSensors.removeItem(handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700806 delete rec;
807 size--;
808 } else {
809 i++;
Mathias Agopianfc328812010-07-14 23:41:37 -0700810 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700811 }
Aravind Akella8a969552014-09-28 17:52:41 -0700812 c->updateLooperRegistration(mLooper);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700813 mActiveConnections.remove(connection);
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700814 BatteryService::cleanup(c->getUid());
Aravind Akella9a844cf2014-02-11 18:58:52 -0800815 if (c->needsWakeLock()) {
816 checkWakeLockStateLocked();
817 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700818}
819
Aravind Akella70018042014-04-07 22:52:37 +0000820Sensor SensorService::getSensorFromHandle(int handle) const {
821 return mSensorMap.valueFor(handle)->getSensor();
822}
823
Mathias Agopianfc328812010-07-14 23:41:37 -0700824status_t SensorService::enable(const sp<SensorEventConnection>& connection,
Svetoslavb412f6e2015-04-29 16:50:41 -0700825 int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags,
826 const String16& opPackageName)
Mathias Agopianfc328812010-07-14 23:41:37 -0700827{
Mathias Agopian50df2952010-07-19 19:09:10 -0700828 if (mInitCheck != NO_ERROR)
829 return mInitCheck;
830
Mathias Agopianf001c922010-11-11 17:58:51 -0800831 SensorInterface* sensor = mSensorMap.valueFor(handle);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700832 if (sensor == NULL) {
833 return BAD_VALUE;
834 }
Aravind Akella70018042014-04-07 22:52:37 +0000835
Svetoslavb412f6e2015-04-29 16:50:41 -0700836 if (!canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) {
Aravind Akella70018042014-04-07 22:52:37 +0000837 return BAD_VALUE;
838 }
839
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700840 Mutex::Autolock _l(mLock);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700841 if (mCurrentOperatingMode == RESTRICTED && !isWhiteListedPackage(connection->getPackageName())) {
Aravind Akella4949c502015-02-11 15:54:35 -0800842 return INVALID_OPERATION;
843 }
844
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700845 SensorRecord* rec = mActiveSensors.valueFor(handle);
846 if (rec == 0) {
847 rec = new SensorRecord(connection);
848 mActiveSensors.add(handle, rec);
849 if (sensor->isVirtual()) {
850 mActiveVirtualSensors.add(handle, sensor);
851 }
852 } else {
853 if (rec->addConnection(connection)) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800854 // this sensor is already activated, but we are adding a connection that uses it.
855 // Immediately send down the last known value of the requested sensor if it's not a
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700856 // "continuous" sensor.
Aravind Akella0e025c52014-06-03 19:19:57 -0700857 if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) {
Aravind Akella9a844cf2014-02-11 18:58:52 -0800858 // NOTE: The wake_up flag of this event may get set to
859 // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event.
Aravind Akella444f2672015-05-07 12:40:52 -0700860 CircularBuffer *circular_buf = mLastEventSeen.valueFor(handle);
861 if (circular_buf) {
862 sensors_event_t event;
863 memset(&event, 0, sizeof(event));
864 // It is unlikely that this buffer is empty as the sensor is already active.
865 // One possible corner case may be two applications activating an on-change
866 // sensor at the same time.
867 if(circular_buf->populateLastEvent(&event)) {
868 event.sensor = handle;
869 if (event.version == sizeof(sensors_event_t)) {
870 if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) {
871 setWakeLockAcquiredLocked(true);
872 }
873 connection->sendEvents(&event, 1, NULL);
874 if (!connection->needsWakeLock() && mWakeLockAcquired) {
875 checkWakeLockStateLocked();
876 }
877 }
Aravind Akella9a844cf2014-02-11 18:58:52 -0800878 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700879 }
880 }
881 }
882 }
883
884 if (connection->addSensor(handle)) {
885 BatteryService::enableSensor(connection->getUid(), handle);
886 // the sensor was added (which means it wasn't already there)
887 // so, see if this connection becomes active
888 if (mActiveConnections.indexOf(connection) < 0) {
889 mActiveConnections.add(connection);
890 }
891 } else {
892 ALOGW("sensor %08x already enabled in connection %p (ignoring)",
893 handle, connection.get());
894 }
895
Aravind Akella724d91d2013-06-27 12:04:23 -0700896 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
897 if (samplingPeriodNs < minDelayNs) {
898 samplingPeriodNs = minDelayNs;
899 }
900
Aravind Akella6c2664a2014-08-13 12:24:50 -0700901 ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d"
902 "rate=%" PRId64 " timeout== %" PRId64"",
Aravind Akella724d91d2013-06-27 12:04:23 -0700903 handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
904
Aravind Akella4949c502015-02-11 15:54:35 -0800905 status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs,
Aravind Akella724d91d2013-06-27 12:04:23 -0700906 maxBatchReportLatencyNs);
Aravind Akella6c2664a2014-08-13 12:24:50 -0700907
Aravind Akella5466c3d2014-08-22 16:11:10 -0700908 // Call flush() before calling activate() on the sensor. Wait for a first flush complete
909 // event before sending events on this connection. Ignore one-shot sensors which don't
910 // support flush(). Also if this sensor isn't already active, don't call flush().
911 if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT &&
912 rec->getNumConnections() > 1) {
913 connection->setFirstFlushPending(handle, true);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700914 status_t err_flush = sensor->flush(connection.get(), handle);
Aravind Akella5466c3d2014-08-22 16:11:10 -0700915 // Flush may return error if the underlying h/w sensor uses an older HAL.
Aravind Akella6c2664a2014-08-13 12:24:50 -0700916 if (err_flush == NO_ERROR) {
Aravind Akella6c2664a2014-08-13 12:24:50 -0700917 rec->addPendingFlushConnection(connection.get());
Aravind Akella5466c3d2014-08-22 16:11:10 -0700918 } else {
919 connection->setFirstFlushPending(handle, false);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700920 }
921 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700922
923 if (err == NO_ERROR) {
924 ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
925 err = sensor->activate(connection.get(), true);
926 }
927
Aravind Akella8a969552014-09-28 17:52:41 -0700928 if (err == NO_ERROR) {
929 connection->updateLooperRegistration(mLooper);
Aravind Akella18d6d512015-06-18 14:18:28 -0700930 SensorRegistrationInfo &reg_info =
931 mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex);
932 reg_info.mSensorHandle = handle;
933 reg_info.mSamplingRateUs = samplingPeriodNs/1000;
934 reg_info.mMaxReportLatencyUs = maxBatchReportLatencyNs/1000;
935 reg_info.mActivated = true;
936 reg_info.mPackageName = connection->getPackageName();
937 time_t rawtime = time(NULL);
938 struct tm * timeinfo = localtime(&rawtime);
939 reg_info.mHour = timeinfo->tm_hour;
940 reg_info.mMin = timeinfo->tm_min;
941 reg_info.mSec = timeinfo->tm_sec;
942 mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE;
Aravind Akella56ae4262014-07-10 16:01:10 -0700943 }
944
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700945 if (err != NO_ERROR) {
Aravind Akella724d91d2013-06-27 12:04:23 -0700946 // batch/activate has failed, reset our state.
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700947 cleanupWithoutDisableLocked(connection, handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700948 }
949 return err;
950}
951
952status_t SensorService::disable(const sp<SensorEventConnection>& connection,
953 int handle)
954{
Mathias Agopian50df2952010-07-19 19:09:10 -0700955 if (mInitCheck != NO_ERROR)
956 return mInitCheck;
957
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700958 Mutex::Autolock _l(mLock);
959 status_t err = cleanupWithoutDisableLocked(connection, handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700960 if (err == NO_ERROR) {
961 SensorInterface* sensor = mSensorMap.valueFor(handle);
962 err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
Aravind Akella18d6d512015-06-18 14:18:28 -0700963
964 }
965 if (err == NO_ERROR) {
966 SensorRegistrationInfo &reg_info =
967 mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex);
968 reg_info.mActivated = false;
969 reg_info.mPackageName= connection->getPackageName();
970 reg_info.mSensorHandle = handle;
971 time_t rawtime = time(NULL);
972 struct tm * timeinfo = localtime(&rawtime);
973 reg_info.mHour = timeinfo->tm_hour;
974 reg_info.mMin = timeinfo->tm_min;
975 reg_info.mSec = timeinfo->tm_sec;
976 mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE;
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700977 }
978 return err;
979}
980
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700981status_t SensorService::cleanupWithoutDisable(
982 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700983 Mutex::Autolock _l(mLock);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700984 return cleanupWithoutDisableLocked(connection, handle);
985}
986
987status_t SensorService::cleanupWithoutDisableLocked(
988 const sp<SensorEventConnection>& connection, int handle) {
Mathias Agopianfc328812010-07-14 23:41:37 -0700989 SensorRecord* rec = mActiveSensors.valueFor(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700990 if (rec) {
991 // see if this connection becomes inactive
Mathias Agopian787ac1b2012-09-18 18:49:18 -0700992 if (connection->removeSensor(handle)) {
993 BatteryService::disableSensor(connection->getUid(), handle);
994 }
Mathias Agopianfc328812010-07-14 23:41:37 -0700995 if (connection->hasAnySensor() == false) {
Aravind Akella8a969552014-09-28 17:52:41 -0700996 connection->updateLooperRegistration(mLooper);
Mathias Agopianfc328812010-07-14 23:41:37 -0700997 mActiveConnections.remove(connection);
998 }
999 // see if this sensor becomes inactive
1000 if (rec->removeConnection(connection)) {
1001 mActiveSensors.removeItem(handle);
Mathias Agopianf001c922010-11-11 17:58:51 -08001002 mActiveVirtualSensors.removeItem(handle);
Mathias Agopianfc328812010-07-14 23:41:37 -07001003 delete rec;
Mathias Agopianfc328812010-07-14 23:41:37 -07001004 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -07001005 return NO_ERROR;
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001006 }
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -07001007 return BAD_VALUE;
Mathias Agopianfc328812010-07-14 23:41:37 -07001008}
1009
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001010status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
Svetoslavb412f6e2015-04-29 16:50:41 -07001011 int handle, nsecs_t ns, const String16& opPackageName)
Mathias Agopianfc328812010-07-14 23:41:37 -07001012{
Mathias Agopian50df2952010-07-19 19:09:10 -07001013 if (mInitCheck != NO_ERROR)
1014 return mInitCheck;
1015
Mathias Agopianae09d652011-11-01 17:37:49 -07001016 SensorInterface* sensor = mSensorMap.valueFor(handle);
1017 if (!sensor)
1018 return BAD_VALUE;
1019
Svetoslavb412f6e2015-04-29 16:50:41 -07001020 if (!canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) {
Aravind Akella70018042014-04-07 22:52:37 +00001021 return BAD_VALUE;
1022 }
1023
Mathias Agopian1cd70002010-07-21 15:59:50 -07001024 if (ns < 0)
1025 return BAD_VALUE;
1026
Mathias Agopian62569ec2011-11-07 21:21:47 -08001027 nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
1028 if (ns < minDelayNs) {
1029 ns = minDelayNs;
Mathias Agopianae09d652011-11-01 17:37:49 -07001030 }
1031
Mathias Agopianf001c922010-11-11 17:58:51 -08001032 return sensor->setDelay(connection.get(), handle, ns);
Mathias Agopianfc328812010-07-14 23:41:37 -07001033}
1034
Svetoslavb412f6e2015-04-29 16:50:41 -07001035status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
1036 const String16& opPackageName) {
Aravind Akella70018042014-04-07 22:52:37 +00001037 if (mInitCheck != NO_ERROR) return mInitCheck;
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001038 SensorDevice& dev(SensorDevice::getInstance());
1039 const int halVersion = dev.getHalDeviceVersion();
1040 status_t err(NO_ERROR);
1041 Mutex::Autolock _l(mLock);
1042 // Loop through all sensors for this connection and call flush on each of them.
1043 for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) {
1044 const int handle = connection->mSensorInfo.keyAt(i);
1045 SensorInterface* sensor = mSensorMap.valueFor(handle);
1046 if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
1047 ALOGE("flush called on a one-shot sensor");
1048 err = INVALID_OPERATION;
1049 continue;
1050 }
Aravind Akella8493b792014-09-08 15:45:47 -07001051 if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) {
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001052 // For older devices just increment pending flush count which will send a trivial
1053 // flush complete event.
Aravind Akella8a969552014-09-28 17:52:41 -07001054 connection->incrementPendingFlushCount(handle);
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001055 } else {
Svetoslavb412f6e2015-04-29 16:50:41 -07001056 if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) {
1057 err = INVALID_OPERATION;
1058 continue;
1059 }
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001060 status_t err_flush = sensor->flush(connection.get(), handle);
1061 if (err_flush == NO_ERROR) {
1062 SensorRecord* rec = mActiveSensors.valueFor(handle);
1063 if (rec != NULL) rec->addPendingFlushConnection(connection);
1064 }
1065 err = (err_flush != NO_ERROR) ? err_flush : err;
1066 }
Aravind Akella70018042014-04-07 22:52:37 +00001067 }
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001068 return err;
Aravind Akella724d91d2013-06-27 12:04:23 -07001069}
Aravind Akella70018042014-04-07 22:52:37 +00001070
Svetoslavb412f6e2015-04-29 16:50:41 -07001071bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,
1072 const String16& opPackageName) {
1073 const String8& requiredPermission = sensor.getRequiredPermission();
Aravind Akella70018042014-04-07 22:52:37 +00001074
Svetoslavb412f6e2015-04-29 16:50:41 -07001075 if (requiredPermission.length() <= 0) {
Aravind Akella70018042014-04-07 22:52:37 +00001076 return true;
Svetoslavb412f6e2015-04-29 16:50:41 -07001077 }
1078
1079 bool hasPermission = false;
1080
1081 // Runtime permissions can't use the cache as they may change.
1082 if (sensor.isRequiredPermissionRuntime()) {
1083 hasPermission = checkPermission(String16(requiredPermission),
1084 IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
Aravind Akella70018042014-04-07 22:52:37 +00001085 } else {
Svetoslavb412f6e2015-04-29 16:50:41 -07001086 hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission));
1087 }
1088
1089 if (!hasPermission) {
1090 ALOGE("%s a sensor (%s) without holding its required permission: %s",
1091 operation, sensor.getName().string(), sensor.getRequiredPermission().string());
Aravind Akella70018042014-04-07 22:52:37 +00001092 return false;
1093 }
Svetoslavb412f6e2015-04-29 16:50:41 -07001094
1095 const int32_t opCode = sensor.getRequiredAppOp();
1096 if (opCode >= 0) {
1097 AppOpsManager appOps;
1098 if (appOps.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName)
1099 != AppOpsManager::MODE_ALLOWED) {
1100 ALOGE("%s a sensor (%s) without enabled required app op: %D",
1101 operation, sensor.getName().string(), opCode);
1102 return false;
1103 }
1104 }
1105
1106 return true;
Aravind Akella70018042014-04-07 22:52:37 +00001107}
1108
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001109bool SensorService::hasDataInjectionPermissions() {
1110 if (!PermissionCache::checkCallingPermission(sDataInjectionPermission)) {
1111 ALOGE("Permission Denial trying to activate data injection without"
1112 " the required permission");
1113 return false;
1114 }
1115 return true;
1116}
1117
Aravind Akella9a844cf2014-02-11 18:58:52 -08001118void SensorService::checkWakeLockState() {
1119 Mutex::Autolock _l(mLock);
1120 checkWakeLockStateLocked();
1121}
1122
1123void SensorService::checkWakeLockStateLocked() {
1124 if (!mWakeLockAcquired) {
1125 return;
1126 }
1127 bool releaseLock = true;
1128 for (size_t i=0 ; i<mActiveConnections.size() ; i++) {
1129 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
1130 if (connection != 0) {
1131 if (connection->needsWakeLock()) {
1132 releaseLock = false;
1133 break;
1134 }
1135 }
1136 }
1137 if (releaseLock) {
Aravind Akellab4373ac2014-10-29 17:55:20 -07001138 setWakeLockAcquiredLocked(false);
1139 }
1140}
1141
1142void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) {
1143 Mutex::Autolock _l(mLock);
1144 connection->writeToSocketFromCache();
1145 if (connection->needsWakeLock()) {
1146 setWakeLockAcquiredLocked(true);
1147 }
1148}
1149
1150void SensorService::populateActiveConnections(
1151 SortedVector< sp<SensorEventConnection> >* activeConnections) {
1152 Mutex::Autolock _l(mLock);
1153 for (size_t i=0 ; i < mActiveConnections.size(); ++i) {
1154 sp<SensorEventConnection> connection(mActiveConnections[i].promote());
1155 if (connection != 0) {
1156 activeConnections->add(connection);
1157 }
Aravind Akella9a844cf2014-02-11 18:58:52 -08001158 }
1159}
Aravind Akella6c2664a2014-08-13 12:24:50 -07001160
Aravind Akella4949c502015-02-11 15:54:35 -08001161bool SensorService::isWhiteListedPackage(const String8& packageName) {
1162 // TODO: Come up with a list of packages.
1163 return (packageName.find(".cts.") != -1);
1164}
1165
Aravind Akella444f2672015-05-07 12:40:52 -07001166int SensorService::getNumEventsForSensorType(int sensor_event_type) {
1167 switch (sensor_event_type) {
1168 case SENSOR_TYPE_ROTATION_VECTOR:
1169 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
1170 return 5;
1171
1172 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
1173 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
1174 return 6;
1175
1176 case SENSOR_TYPE_GAME_ROTATION_VECTOR:
1177 return 4;
1178
1179 case SENSOR_TYPE_SIGNIFICANT_MOTION:
1180 case SENSOR_TYPE_STEP_DETECTOR:
1181 case SENSOR_TYPE_STEP_COUNTER:
1182 return 1;
1183
1184 default:
1185 return 3;
1186 }
1187}
1188
Mathias Agopianfc328812010-07-14 23:41:37 -07001189// ---------------------------------------------------------------------------
Mathias Agopianfc328812010-07-14 23:41:37 -07001190SensorService::SensorRecord::SensorRecord(
1191 const sp<SensorEventConnection>& connection)
1192{
1193 mConnections.add(connection);
1194}
1195
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001196bool SensorService::SensorRecord::addConnection(
Mathias Agopianfc328812010-07-14 23:41:37 -07001197 const sp<SensorEventConnection>& connection)
1198{
1199 if (mConnections.indexOf(connection) < 0) {
1200 mConnections.add(connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001201 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -07001202 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001203 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001204}
1205
1206bool SensorService::SensorRecord::removeConnection(
1207 const wp<SensorEventConnection>& connection)
1208{
1209 ssize_t index = mConnections.indexOf(connection);
1210 if (index >= 0) {
1211 mConnections.removeItemsAt(index, 1);
1212 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001213 // Remove this connections from the queue of flush() calls made on this sensor.
1214 for (Vector< wp<SensorEventConnection> >::iterator it =
1215 mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) {
Aravind Akella8a969552014-09-28 17:52:41 -07001216
Aravind Akella6c2664a2014-08-13 12:24:50 -07001217 if (it->unsafe_get() == connection.unsafe_get()) {
1218 it = mPendingFlushConnections.erase(it);
1219 } else {
1220 ++it;
1221 }
1222 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001223 return mConnections.size() ? false : true;
1224}
1225
Aravind Akella6c2664a2014-08-13 12:24:50 -07001226void SensorService::SensorRecord::addPendingFlushConnection(
1227 const sp<SensorEventConnection>& connection) {
1228 mPendingFlushConnections.add(connection);
1229}
1230
1231void SensorService::SensorRecord::removeFirstPendingFlushConnection() {
1232 if (mPendingFlushConnections.size() > 0) {
1233 mPendingFlushConnections.removeAt(0);
1234 }
1235}
1236
1237SensorService::SensorEventConnection *
1238SensorService::SensorRecord::getFirstPendingFlushConnection() {
1239 if (mPendingFlushConnections.size() > 0) {
1240 return mPendingFlushConnections[0].unsafe_get();
1241 }
1242 return NULL;
1243}
1244
Aravind Akella4949c502015-02-11 15:54:35 -08001245void SensorService::SensorRecord::clearAllPendingFlushConnections() {
1246 mPendingFlushConnections.clear();
1247}
1248
Aravind Akella18d6d512015-06-18 14:18:28 -07001249
1250// ---------------------------------------------------------------------------
1251SensorService::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) {
1252 mTimestamp = -1;
1253 const int numData = SensorService::getNumEventsForSensorType(sensorType);
1254 if (sensorType == SENSOR_TYPE_STEP_COUNTER) {
1255 mStepCounter = 0;
1256 } else {
1257 mData = new float[numData];
1258 for (int i = 0; i < numData; ++i) {
1259 mData[i] = -1.0;
1260 }
1261 }
1262 mHour = mMin = mSec = INT32_MIN;
1263}
1264
1265bool SensorService::TrimmedSensorEvent::isSentinel(const TrimmedSensorEvent& event) {
1266 return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN);
1267}
Aravind Akella444f2672015-05-07 12:40:52 -07001268// --------------------------------------------------------------------------
1269SensorService::CircularBuffer::CircularBuffer(int sensor_event_type) {
1270 mNextInd = 0;
Aravind Akella18d6d512015-06-18 14:18:28 -07001271 mBufSize = CIRCULAR_BUF_SIZE;
1272 if (sensor_event_type == SENSOR_TYPE_STEP_COUNTER ||
1273 sensor_event_type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
1274 sensor_event_type == SENSOR_TYPE_ACCELEROMETER) {
1275 mBufSize = CIRCULAR_BUF_SIZE * 5;
1276 }
1277 mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize];
Aravind Akella444f2672015-05-07 12:40:52 -07001278 mSensorType = sensor_event_type;
Aravind Akella18d6d512015-06-18 14:18:28 -07001279 for (int i = 0; i < mBufSize; ++i) {
1280 mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType);
Aravind Akella444f2672015-05-07 12:40:52 -07001281 }
1282}
1283
1284void SensorService::CircularBuffer::addEvent(const sensors_event_t& sensor_event) {
1285 TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd];
1286 curr_event->mTimestamp = sensor_event.timestamp;
1287 if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
1288 curr_event->mStepCounter = sensor_event.u64.step_counter;
1289 } else {
1290 memcpy(curr_event->mData, sensor_event.data,
1291 sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType));
1292 }
1293 time_t rawtime = time(NULL);
1294 struct tm * timeinfo = localtime(&rawtime);
1295 curr_event->mHour = timeinfo->tm_hour;
1296 curr_event->mMin = timeinfo->tm_min;
1297 curr_event->mSec = timeinfo->tm_sec;
Aravind Akella18d6d512015-06-18 14:18:28 -07001298 mNextInd = (mNextInd + 1) % mBufSize;
Aravind Akella444f2672015-05-07 12:40:52 -07001299}
1300
1301void SensorService::CircularBuffer::printBuffer(String8& result) const {
1302 const int numData = SensorService::getNumEventsForSensorType(mSensorType);
1303 int i = mNextInd, eventNum = 1;
Aravind Akella18d6d512015-06-18 14:18:28 -07001304 result.appendFormat("last %d events = < ", mBufSize);
Aravind Akella444f2672015-05-07 12:40:52 -07001305 do {
Aravind Akella18d6d512015-06-18 14:18:28 -07001306 if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) {
Aravind Akella444f2672015-05-07 12:40:52 -07001307 // Sentinel, ignore.
Aravind Akella18d6d512015-06-18 14:18:28 -07001308 i = (i + 1) % mBufSize;
Aravind Akella444f2672015-05-07 12:40:52 -07001309 continue;
1310 }
1311 result.appendFormat("%d) ", eventNum++);
1312 if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
1313 result.appendFormat("%llu,", mTrimmedSensorEventArr[i]->mStepCounter);
1314 } else {
1315 for (int j = 0; j < numData; ++j) {
1316 result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]);
1317 }
1318 }
1319 result.appendFormat("%lld %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp,
1320 mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin,
1321 mTrimmedSensorEventArr[i]->mSec);
Aravind Akella18d6d512015-06-18 14:18:28 -07001322 i = (i + 1) % mBufSize;
Aravind Akella444f2672015-05-07 12:40:52 -07001323 } while (i != mNextInd);
1324 result.appendFormat(">\n");
1325}
1326
1327bool SensorService::CircularBuffer::populateLastEvent(sensors_event_t *event) {
Aravind Akella18d6d512015-06-18 14:18:28 -07001328 int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize;
Aravind Akella444f2672015-05-07 12:40:52 -07001329 // Check if the buffer is empty.
Aravind Akella18d6d512015-06-18 14:18:28 -07001330 if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) {
Aravind Akella444f2672015-05-07 12:40:52 -07001331 return false;
1332 }
1333 event->version = sizeof(sensors_event_t);
1334 event->type = mSensorType;
1335 event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp;
1336 if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
1337 event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter;
1338 } else {
1339 memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData,
1340 sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType));
1341 }
1342 return true;
1343}
1344
1345SensorService::CircularBuffer::~CircularBuffer() {
Aravind Akella18d6d512015-06-18 14:18:28 -07001346 for (int i = 0; i < mBufSize; ++i) {
Aravind Akella444f2672015-05-07 12:40:52 -07001347 delete mTrimmedSensorEventArr[i];
1348 }
1349 delete [] mTrimmedSensorEventArr;
1350}
1351
Mathias Agopianfc328812010-07-14 23:41:37 -07001352// ---------------------------------------------------------------------------
1353
1354SensorService::SensorEventConnection::SensorEventConnection(
Svetoslavb412f6e2015-04-29 16:50:41 -07001355 const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode,
1356 const String16& opPackageName)
Aravind Akella8a969552014-09-28 17:52:41 -07001357 : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
Svetoslavb412f6e2015-04-29 16:50:41 -07001358 mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL),
1359 mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001360 mChannel = new BitTube(mService->mSocketBufferSize);
Aravind Akella56ae4262014-07-10 16:01:10 -07001361#if DEBUG_CONNECTIONS
1362 mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
Aravind Akellae74baf62014-08-21 12:28:35 -07001363 mTotalAcksNeeded = mTotalAcksReceived = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -07001364#endif
Mathias Agopianfc328812010-07-14 23:41:37 -07001365}
1366
Aravind Akella56ae4262014-07-10 16:01:10 -07001367SensorService::SensorEventConnection::~SensorEventConnection() {
Steve Blocka5512372011-12-20 16:23:08 +00001368 ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
Aravind Akellae148bc22014-09-24 22:12:58 -07001369 mService->cleanupConnection(this);
Aravind Akella56ae4262014-07-10 16:01:10 -07001370 if (mEventCache != NULL) {
1371 delete mEventCache;
1372 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001373}
1374
Aravind Akella56ae4262014-07-10 16:01:10 -07001375void SensorService::SensorEventConnection::onFirstRef() {
1376 LooperCallback::onFirstRef();
Mathias Agopianfc328812010-07-14 23:41:37 -07001377}
1378
Aravind Akella9a844cf2014-02-11 18:58:52 -08001379bool SensorService::SensorEventConnection::needsWakeLock() {
1380 Mutex::Autolock _l(mConnectionLock);
Aravind Akella8a969552014-09-28 17:52:41 -07001381 return !mDead && mWakeLockRefCount > 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001382}
1383
Aravind Akellab4373ac2014-10-29 17:55:20 -07001384void SensorService::SensorEventConnection::resetWakeLockRefCount() {
1385 Mutex::Autolock _l(mConnectionLock);
1386 mWakeLockRefCount = 0;
1387}
1388
Aravind Akella4c8b9512013-09-05 17:03:38 -07001389void SensorService::SensorEventConnection::dump(String8& result) {
1390 Mutex::Autolock _l(mConnectionLock);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001391 result.appendFormat("Operating Mode: %s\n", mDataInjectionMode ? "DATA_INJECTION" : "NORMAL");
Aravind Akella18d6d512015-06-18 14:18:28 -07001392 result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
1393 "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
1394 mMaxCacheSize);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001395 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1396 const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
Aravind Akella0ec20662014-09-14 17:29:48 -07001397 result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
Aravind Akella4c8b9512013-09-05 17:03:38 -07001398 mService->getSensorName(mSensorInfo.keyAt(i)).string(),
Aravind Akella6c2664a2014-08-13 12:24:50 -07001399 mSensorInfo.keyAt(i),
Aravind Akella4c8b9512013-09-05 17:03:38 -07001400 flushInfo.mFirstFlushPending ? "First flush pending" :
1401 "active",
Aravind Akella0ec20662014-09-14 17:29:48 -07001402 flushInfo.mPendingFlushEventsToSend);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001403 }
Aravind Akella0ec20662014-09-14 17:29:48 -07001404#if DEBUG_CONNECTIONS
1405 result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
1406 " total_acks_needed %d | total_acks_recvd %d\n",
1407 mEventsReceived,
1408 mEventsSent,
1409 mEventsSentFromCache,
1410 mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
1411 mTotalAcksNeeded,
1412 mTotalAcksReceived);
1413#endif
Aravind Akella4c8b9512013-09-05 17:03:38 -07001414}
1415
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001416bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001417 Mutex::Autolock _l(mConnectionLock);
Svetoslavb412f6e2015-04-29 16:50:41 -07001418 if (!canAccessSensor(mService->getSensorFromHandle(handle),
1419 "Tried adding", mOpPackageName)) {
Aravind Akella70018042014-04-07 22:52:37 +00001420 return false;
1421 }
Aravind Akella4c8b9512013-09-05 17:03:38 -07001422 if (mSensorInfo.indexOfKey(handle) < 0) {
1423 mSensorInfo.add(handle, FlushInfo());
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001424 return true;
Mathias Agopianfc328812010-07-14 23:41:37 -07001425 }
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001426 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001427}
1428
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001429bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001430 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001431 if (mSensorInfo.removeItem(handle) >= 0) {
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001432 return true;
1433 }
1434 return false;
Mathias Agopianfc328812010-07-14 23:41:37 -07001435}
1436
1437bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001438 Mutex::Autolock _l(mConnectionLock);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001439 return mSensorInfo.indexOfKey(handle) >= 0;
Mathias Agopianfc328812010-07-14 23:41:37 -07001440}
1441
1442bool SensorService::SensorEventConnection::hasAnySensor() const {
Mathias Agopian71d7a5c2010-11-14 20:55:25 -08001443 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian7c1c5312010-07-21 15:59:50 -07001444 return mSensorInfo.size() ? true : false;
1445}
1446
Aravind Akella8493b792014-09-08 15:45:47 -07001447bool SensorService::SensorEventConnection::hasOneShotSensors() const {
1448 Mutex::Autolock _l(mConnectionLock);
1449 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1450 const int handle = mSensorInfo.keyAt(i);
1451 if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
1452 return true;
1453 }
1454 }
1455 return false;
1456}
1457
Aravind Akella4949c502015-02-11 15:54:35 -08001458String8 SensorService::SensorEventConnection::getPackageName() const {
1459 return mPackageName;
1460}
1461
Aravind Akella4c8b9512013-09-05 17:03:38 -07001462void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
1463 bool value) {
1464 Mutex::Autolock _l(mConnectionLock);
1465 ssize_t index = mSensorInfo.indexOfKey(handle);
1466 if (index >= 0) {
1467 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
1468 flushInfo.mFirstFlushPending = value;
1469 }
1470}
1471
Aravind Akella8a969552014-09-28 17:52:41 -07001472void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
1473 Mutex::Autolock _l(mConnectionLock);
1474 updateLooperRegistrationLocked(looper);
1475}
1476
1477void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
1478 const sp<Looper>& looper) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001479 bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) ||
1480 mDataInjectionMode;
Aravind Akella8a969552014-09-28 17:52:41 -07001481 // If all sensors are unregistered OR Looper has encountered an error, we
1482 // can remove the Fd from the Looper if it has been previously added.
1483 if (!isConnectionActive || mDead) {
1484 if (mHasLooperCallbacks) {
1485 ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd());
1486 looper->removeFd(mChannel->getSendFd());
1487 mHasLooperCallbacks = false;
1488 }
1489 return;
1490 }
1491
1492 int looper_flags = 0;
1493 if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001494 if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
Aravind Akella8a969552014-09-28 17:52:41 -07001495 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1496 const int handle = mSensorInfo.keyAt(i);
1497 if (mService->getSensorFromHandle(handle).isWakeUpSensor()) {
1498 looper_flags |= ALOOPER_EVENT_INPUT;
1499 break;
1500 }
1501 }
1502 // If flags is still set to zero, we don't need to add this fd to the Looper, if
1503 // the fd has already been added, remove it. This is likely to happen when ALL the
1504 // events stored in the cache have been sent to the corresponding app.
1505 if (looper_flags == 0) {
1506 if (mHasLooperCallbacks) {
1507 ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd());
1508 looper->removeFd(mChannel->getSendFd());
1509 mHasLooperCallbacks = false;
1510 }
1511 return;
1512 }
1513 // Add the file descriptor to the Looper for receiving acknowledegments if the app has
1514 // registered for wake-up sensors OR for sending events in the cache.
1515 int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL);
1516 if (ret == 1) {
1517 ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd());
1518 mHasLooperCallbacks = true;
1519 } else {
1520 ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd());
1521 }
1522}
1523
1524void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
1525 Mutex::Autolock _l(mConnectionLock);
1526 ssize_t index = mSensorInfo.indexOfKey(handle);
1527 if (index >= 0) {
1528 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
1529 flushInfo.mPendingFlushEventsToSend++;
1530 }
1531}
1532
Mathias Agopianfc328812010-07-14 23:41:37 -07001533status_t SensorService::SensorEventConnection::sendEvents(
Aravind Akella0ec20662014-09-14 17:29:48 -07001534 sensors_event_t const* buffer, size_t numEvents,
Aravind Akella8493b792014-09-08 15:45:47 -07001535 sensors_event_t* scratch,
1536 SensorEventConnection const * const * mapFlushEventsToConnections) {
Mathias Agopiancf510012010-07-22 16:18:10 -07001537 // filter out events not for this connection
Svetoslavb412f6e2015-04-29 16:50:41 -07001538 int count = 0;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001539 Mutex::Autolock _l(mConnectionLock);
Mathias Agopian3560fb22010-07-22 21:24:39 -07001540 if (scratch) {
1541 size_t i=0;
1542 while (i<numEvents) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001543 int32_t sensor_handle = buffer[i].sensor;
Aravind Akella724d91d2013-06-27 12:04:23 -07001544 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
1545 ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
Aravind Akella9e3adfc2014-09-03 15:48:05 -07001546 buffer[i].meta_data.sensor);
Aravind Akella8a969552014-09-28 17:52:41 -07001547 // Setting sensor_handle to the correct sensor to ensure the sensor events per
1548 // connection are filtered correctly. buffer[i].sensor is zero for meta_data
1549 // events.
Aravind Akella6c2664a2014-08-13 12:24:50 -07001550 sensor_handle = buffer[i].meta_data.sensor;
Aravind Akella724d91d2013-06-27 12:04:23 -07001551 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001552 ssize_t index = mSensorInfo.indexOfKey(sensor_handle);
Aravind Akella56ae4262014-07-10 16:01:10 -07001553 // Check if this connection has registered for this sensor. If not continue to the
1554 // next sensor_event.
1555 if (index < 0) {
1556 ++i;
1557 continue;
Aravind Akella4c8b9512013-09-05 17:03:38 -07001558 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001559
Aravind Akella56ae4262014-07-10 16:01:10 -07001560 FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
Aravind Akella6c2664a2014-08-13 12:24:50 -07001561 // Check if there is a pending flush_complete event for this sensor on this connection.
Aravind Akella8493b792014-09-08 15:45:47 -07001562 if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
1563 this == mapFlushEventsToConnections[i]) {
1564 flushInfo.mFirstFlushPending = false;
1565 ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
1566 buffer[i].meta_data.sensor);
1567 ++i;
1568 continue;
Aravind Akella56ae4262014-07-10 16:01:10 -07001569 }
1570
1571 // If there is a pending flush complete event for this sensor on this connection,
1572 // ignore the event and proceed to the next.
1573 if (flushInfo.mFirstFlushPending) {
1574 ++i;
1575 continue;
1576 }
1577
1578 do {
Aravind Akella8493b792014-09-08 15:45:47 -07001579 // Keep copying events into the scratch buffer as long as they are regular
1580 // sensor_events are from the same sensor_handle OR they are flush_complete_events
1581 // from the same sensor_handle AND the current connection is mapped to the
1582 // corresponding flush_complete_event.
Aravind Akella56ae4262014-07-10 16:01:10 -07001583 if (buffer[i].type == SENSOR_TYPE_META_DATA) {
Aravind Akella8493b792014-09-08 15:45:47 -07001584 if (this == mapFlushEventsToConnections[i]) {
Aravind Akella56ae4262014-07-10 16:01:10 -07001585 scratch[count++] = buffer[i];
Aravind Akella56ae4262014-07-10 16:01:10 -07001586 }
1587 ++i;
1588 } else {
1589 // Regular sensor event, just copy it to the scratch buffer.
Aravind Akella4c8b9512013-09-05 17:03:38 -07001590 scratch[count++] = buffer[i++];
Aravind Akella56ae4262014-07-10 16:01:10 -07001591 }
Aravind Akella8493b792014-09-08 15:45:47 -07001592 } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle &&
1593 buffer[i].type != SENSOR_TYPE_META_DATA) ||
Aravind Akella56ae4262014-07-10 16:01:10 -07001594 (buffer[i].type == SENSOR_TYPE_META_DATA &&
Aravind Akella6c2664a2014-08-13 12:24:50 -07001595 buffer[i].meta_data.sensor == sensor_handle)));
Mathias Agopiancf510012010-07-22 16:18:10 -07001596 }
Mathias Agopian3560fb22010-07-22 21:24:39 -07001597 } else {
1598 scratch = const_cast<sensors_event_t *>(buffer);
1599 count = numEvents;
Mathias Agopiancf510012010-07-22 16:18:10 -07001600 }
Mathias Agopianfc328812010-07-14 23:41:37 -07001601
Aravind Akella6c2664a2014-08-13 12:24:50 -07001602 sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -07001603 // Early return if there are no events for this connection.
1604 if (count == 0) {
1605 return status_t(NO_ERROR);
1606 }
1607
1608#if DEBUG_CONNECTIONS
1609 mEventsReceived += count;
1610#endif
1611 if (mCacheSize != 0) {
1612 // There are some events in the cache which need to be sent first. Copy this buffer to
1613 // the end of cache.
1614 if (mCacheSize + count <= mMaxCacheSize) {
1615 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
1616 mCacheSize += count;
1617 } else {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001618 // Check if any new sensors have registered on this connection which may have increased
1619 // the max cache size that is desired.
1620 if (mCacheSize + count < computeMaxCacheSizeLocked()) {
1621 reAllocateCacheLocked(scratch, count);
1622 return status_t(NO_ERROR);
1623 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001624 // Some events need to be dropped.
1625 int remaningCacheSize = mMaxCacheSize - mCacheSize;
1626 if (remaningCacheSize != 0) {
1627 memcpy(&mEventCache[mCacheSize], scratch,
1628 remaningCacheSize * sizeof(sensors_event_t));
1629 }
1630 int numEventsDropped = count - remaningCacheSize;
1631 countFlushCompleteEventsLocked(mEventCache, numEventsDropped);
1632 // Drop the first "numEventsDropped" in the cache.
1633 memmove(mEventCache, &mEventCache[numEventsDropped],
1634 (mCacheSize - numEventsDropped) * sizeof(sensors_event_t));
1635
1636 // Copy the remainingEvents in scratch buffer to the end of cache.
1637 memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize,
1638 numEventsDropped * sizeof(sensors_event_t));
1639 }
1640 return status_t(NO_ERROR);
1641 }
1642
Aravind Akella6c2664a2014-08-13 12:24:50 -07001643 int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count);
1644 if (index_wake_up_event >= 0) {
1645 scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1646 ++mWakeLockRefCount;
Aravind Akellae74baf62014-08-21 12:28:35 -07001647#if DEBUG_CONNECTIONS
1648 ++mTotalAcksNeeded;
1649#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001650 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001651
1652 // NOTE: ASensorEvent and sensors_event_t are the same type.
1653 ssize_t size = SensorEventQueue::write(mChannel,
1654 reinterpret_cast<ASensorEvent const*>(scratch), count);
1655 if (size < 0) {
1656 // Write error, copy events to local cache.
Aravind Akella6c2664a2014-08-13 12:24:50 -07001657 if (index_wake_up_event >= 0) {
1658 // If there was a wake_up sensor_event, reset the flag.
1659 scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
Aravind Akella0ec20662014-09-14 17:29:48 -07001660 if (mWakeLockRefCount > 0) {
1661 --mWakeLockRefCount;
1662 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001663#if DEBUG_CONNECTIONS
1664 --mTotalAcksNeeded;
1665#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001666 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001667 if (mEventCache == NULL) {
1668 mMaxCacheSize = computeMaxCacheSizeLocked();
1669 mEventCache = new sensors_event_t[mMaxCacheSize];
1670 mCacheSize = 0;
1671 }
1672 memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
1673 mCacheSize += count;
1674
1675 // Add this file descriptor to the looper to get a callback when this fd is available for
1676 // writing.
Aravind Akella8a969552014-09-28 17:52:41 -07001677 updateLooperRegistrationLocked(mService->getLooper());
Aravind Akella56ae4262014-07-10 16:01:10 -07001678 return size;
1679 }
1680
1681#if DEBUG_CONNECTIONS
1682 if (size > 0) {
1683 mEventsSent += count;
1684 }
1685#endif
1686
1687 return size < 0 ? status_t(size) : status_t(NO_ERROR);
1688}
1689
Aravind Akella6c2664a2014-08-13 12:24:50 -07001690void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch,
1691 int count) {
1692 sensors_event_t *eventCache_new;
1693 const int new_cache_size = computeMaxCacheSizeLocked();
1694 // Allocate new cache, copy over events from the old cache & scratch, free up memory.
1695 eventCache_new = new sensors_event_t[new_cache_size];
1696 memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t));
1697 memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t));
1698
1699 ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize,
1700 new_cache_size);
1701
1702 delete mEventCache;
1703 mEventCache = eventCache_new;
1704 mCacheSize += count;
1705 mMaxCacheSize = new_cache_size;
1706}
1707
1708void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
1709 ASensorEvent flushCompleteEvent;
Aravind Akella0ec20662014-09-14 17:29:48 -07001710 memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
Aravind Akella6c2664a2014-08-13 12:24:50 -07001711 flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
Aravind Akella6c2664a2014-08-13 12:24:50 -07001712 // Loop through all the sensors for this connection and check if there are any pending
1713 // flush complete events to be sent.
1714 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1715 FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
1716 while (flushInfo.mPendingFlushEventsToSend > 0) {
Aravind Akella0ec20662014-09-14 17:29:48 -07001717 const int sensor_handle = mSensorInfo.keyAt(i);
1718 flushCompleteEvent.meta_data.sensor = sensor_handle;
Aravind Akellab4373ac2014-10-29 17:55:20 -07001719 bool wakeUpSensor = mService->getSensorFromHandle(sensor_handle).isWakeUpSensor();
1720 if (wakeUpSensor) {
1721 ++mWakeLockRefCount;
Aravind Akella0ec20662014-09-14 17:29:48 -07001722 flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1723 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001724 ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
1725 if (size < 0) {
Aravind Akellab4373ac2014-10-29 17:55:20 -07001726 if (wakeUpSensor) --mWakeLockRefCount;
Aravind Akella6c2664a2014-08-13 12:24:50 -07001727 return;
1728 }
1729 ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
1730 flushCompleteEvent.meta_data.sensor);
1731 flushInfo.mPendingFlushEventsToSend--;
1732 }
1733 }
1734}
1735
Aravind Akellab4373ac2014-10-29 17:55:20 -07001736void SensorService::SensorEventConnection::writeToSocketFromCache() {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001737 // At a time write at most half the size of the receiver buffer in SensorEventQueue OR
1738 // half the size of the socket buffer allocated in BitTube whichever is smaller.
1739 const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
1740 int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
Aravind Akellab4373ac2014-10-29 17:55:20 -07001741 Mutex::Autolock _l(mConnectionLock);
Aravind Akella5466c3d2014-08-22 16:11:10 -07001742 // Send pending flush complete events (if any)
Aravind Akella6c2664a2014-08-13 12:24:50 -07001743 sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -07001744 for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
Aravind Akella5466c3d2014-08-22 16:11:10 -07001745 const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
Aravind Akella6c2664a2014-08-13 12:24:50 -07001746 int index_wake_up_event =
1747 findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
1748 if (index_wake_up_event >= 0) {
1749 mEventCache[index_wake_up_event + numEventsSent].flags |=
1750 WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
1751 ++mWakeLockRefCount;
Aravind Akellae74baf62014-08-21 12:28:35 -07001752#if DEBUG_CONNECTIONS
1753 ++mTotalAcksNeeded;
1754#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001755 }
Aravind Akella4c8b9512013-09-05 17:03:38 -07001756
Aravind Akella56ae4262014-07-10 16:01:10 -07001757 ssize_t size = SensorEventQueue::write(mChannel,
1758 reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
Aravind Akella6c2664a2014-08-13 12:24:50 -07001759 numEventsToWrite);
Aravind Akella56ae4262014-07-10 16:01:10 -07001760 if (size < 0) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001761 if (index_wake_up_event >= 0) {
1762 // If there was a wake_up sensor_event, reset the flag.
1763 mEventCache[index_wake_up_event + numEventsSent].flags &=
1764 ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
Aravind Akella0ec20662014-09-14 17:29:48 -07001765 if (mWakeLockRefCount > 0) {
1766 --mWakeLockRefCount;
1767 }
Aravind Akellae74baf62014-08-21 12:28:35 -07001768#if DEBUG_CONNECTIONS
1769 --mTotalAcksNeeded;
1770#endif
Aravind Akella6c2664a2014-08-13 12:24:50 -07001771 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001772 memmove(mEventCache, &mEventCache[numEventsSent],
1773 (mCacheSize - numEventsSent) * sizeof(sensors_event_t));
1774 ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ",
Aravind Akella6c2664a2014-08-13 12:24:50 -07001775 numEventsSent, mCacheSize);
Aravind Akella56ae4262014-07-10 16:01:10 -07001776 mCacheSize -= numEventsSent;
Aravind Akella56ae4262014-07-10 16:01:10 -07001777 return;
1778 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001779 numEventsSent += numEventsToWrite;
Aravind Akella56ae4262014-07-10 16:01:10 -07001780#if DEBUG_CONNECTIONS
Aravind Akella6c2664a2014-08-13 12:24:50 -07001781 mEventsSentFromCache += numEventsToWrite;
Aravind Akella56ae4262014-07-10 16:01:10 -07001782#endif
Aravind Akellab4099e72013-10-15 15:43:10 -07001783 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001784 ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize);
1785 // All events from the cache have been sent. Reset cache size to zero.
1786 mCacheSize = 0;
Aravind Akella8a969552014-09-28 17:52:41 -07001787 // There are no more events in the cache. We don't need to poll for write on the fd.
1788 // Update Looper registration.
1789 updateLooperRegistrationLocked(mService->getLooper());
Mathias Agopianfc328812010-07-14 23:41:37 -07001790}
1791
Aravind Akellac551eac2013-10-14 17:04:42 -07001792void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
Aravind Akella0ec20662014-09-14 17:29:48 -07001793 sensors_event_t const* scratch, const int numEventsDropped) {
Aravind Akella4c8b9512013-09-05 17:03:38 -07001794 ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
Aravind Akella4c8b9512013-09-05 17:03:38 -07001795 // Count flushComplete events in the events that are about to the dropped. These will be sent
1796 // separately before the next batch of events.
1797 for (int j = 0; j < numEventsDropped; ++j) {
1798 if (scratch[j].type == SENSOR_TYPE_META_DATA) {
1799 FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
1800 flushInfo.mPendingFlushEventsToSend++;
1801 ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
1802 flushInfo.mPendingFlushEventsToSend);
1803 }
1804 }
1805 return;
1806}
1807
Aravind Akella6c2664a2014-08-13 12:24:50 -07001808int SensorService::SensorEventConnection::findWakeUpSensorEventLocked(
1809 sensors_event_t const* scratch, const int count) {
Aravind Akella9a844cf2014-02-11 18:58:52 -08001810 for (int i = 0; i < count; ++i) {
1811 if (mService->isWakeUpSensorEvent(scratch[i])) {
Aravind Akella6c2664a2014-08-13 12:24:50 -07001812 return i;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001813 }
1814 }
Aravind Akella6c2664a2014-08-13 12:24:50 -07001815 return -1;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001816}
1817
Mathias Agopianb3989272011-10-20 18:42:02 -07001818sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
Mathias Agopianfc328812010-07-14 23:41:37 -07001819{
1820 return mChannel;
1821}
1822
1823status_t SensorService::SensorEventConnection::enableDisable(
Aravind Akella724d91d2013-06-27 12:04:23 -07001824 int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
1825 int reservedFlags)
Mathias Agopianfc328812010-07-14 23:41:37 -07001826{
1827 status_t err;
1828 if (enabled) {
Aravind Akella724d91d2013-06-27 12:04:23 -07001829 err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
Svetoslavb412f6e2015-04-29 16:50:41 -07001830 reservedFlags, mOpPackageName);
Aravind Akella56ae4262014-07-10 16:01:10 -07001831
Mathias Agopianfc328812010-07-14 23:41:37 -07001832 } else {
1833 err = mService->disable(this, handle);
1834 }
1835 return err;
1836}
1837
1838status_t SensorService::SensorEventConnection::setEventRate(
Aravind Akella724d91d2013-06-27 12:04:23 -07001839 int handle, nsecs_t samplingPeriodNs)
Mathias Agopianfc328812010-07-14 23:41:37 -07001840{
Svetoslavb412f6e2015-04-29 16:50:41 -07001841 return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
Mathias Agopianfc328812010-07-14 23:41:37 -07001842}
1843
Aravind Akella701166d2013-10-08 14:59:26 -07001844status_t SensorService::SensorEventConnection::flush() {
Svetoslavb412f6e2015-04-29 16:50:41 -07001845 return mService->flushSensor(this, mOpPackageName);
Aravind Akella724d91d2013-06-27 12:04:23 -07001846}
Aravind Akella4c8b9512013-09-05 17:03:38 -07001847
Aravind Akella8a969552014-09-28 17:52:41 -07001848int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) {
Aravind Akella56ae4262014-07-10 16:01:10 -07001849 if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) {
Aravind Akella8a969552014-09-28 17:52:41 -07001850 {
1851 // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount,
1852 // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
1853 // can release the wake-lock.
1854 ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
1855 Mutex::Autolock _l(mConnectionLock);
1856 mDead = true;
1857 mWakeLockRefCount = 0;
1858 updateLooperRegistrationLocked(mService->getLooper());
1859 }
1860 mService->checkWakeLockState();
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001861 if (mDataInjectionMode) {
1862 // If the Looper has encountered some error in data injection mode, reset SensorService
1863 // back to normal mode.
1864 mService->resetToNormalMode();
1865 mDataInjectionMode = false;
1866 }
Aravind Akella8a969552014-09-28 17:52:41 -07001867 return 1;
Aravind Akella56ae4262014-07-10 16:01:10 -07001868 }
1869
1870 if (events & ALOOPER_EVENT_INPUT) {
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001871 unsigned char buf[sizeof(sensors_event_t)];
1872 ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
Aravind Akella56ae4262014-07-10 16:01:10 -07001873 {
1874 Mutex::Autolock _l(mConnectionLock);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001875 if (numBytesRead == sizeof(sensors_event_t)) {
1876 if (!mDataInjectionMode) {
1877 ALOGE("Data injected in normal mode, dropping event"
1878 "package=%s uid=%d", mPackageName.string(), mUid);
1879 // Unregister call backs.
1880 return 0;
1881 }
1882 SensorDevice& dev(SensorDevice::getInstance());
1883 sensors_event_t sensor_event;
1884 memset(&sensor_event, 0, sizeof(sensor_event));
1885 memcpy(&sensor_event, buf, sizeof(sensors_event_t));
1886 Sensor sensor = mService->getSensorFromHandle(sensor_event.sensor);
1887 sensor_event.type = sensor.getType();
Svetoslavb412f6e2015-04-29 16:50:41 -07001888 dev.injectSensorData(&sensor_event);
Aravind Akellae74baf62014-08-21 12:28:35 -07001889#if DEBUG_CONNECTIONS
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001890 ++mEventsReceived;
Aravind Akellae74baf62014-08-21 12:28:35 -07001891#endif
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001892 } else if (numBytesRead == sizeof(uint32_t)) {
1893 uint32_t numAcks = 0;
Aravind Akella08f04bf2015-05-08 15:59:23 -07001894 memcpy(&numAcks, buf, numBytesRead);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -07001895 // Sanity check to ensure there are no read errors in recv, numAcks is always
1896 // within the range and not zero. If any of the above don't hold reset
1897 // mWakeLockRefCount to zero.
1898 if (numAcks > 0 && numAcks < mWakeLockRefCount) {
1899 mWakeLockRefCount -= numAcks;
1900 } else {
1901 mWakeLockRefCount = 0;
1902 }
1903#if DEBUG_CONNECTIONS
1904 mTotalAcksReceived += numAcks;
1905#endif
1906 } else {
1907 // Read error, reset wakelock refcount.
1908 mWakeLockRefCount = 0;
1909 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001910 }
1911 // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released
1912 // here as checkWakeLockState() will need it.
1913 if (mWakeLockRefCount == 0) {
1914 mService->checkWakeLockState();
1915 }
1916 // continue getting callbacks.
1917 return 1;
1918 }
1919
1920 if (events & ALOOPER_EVENT_OUTPUT) {
Aravind Akellab4373ac2014-10-29 17:55:20 -07001921 // send sensor data that is stored in mEventCache for this connection.
1922 mService->sendEventsFromCache(this);
Aravind Akella9a844cf2014-02-11 18:58:52 -08001923 }
Aravind Akella56ae4262014-07-10 16:01:10 -07001924 return 1;
1925}
1926
1927int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
Svetoslavb412f6e2015-04-29 16:50:41 -07001928 size_t fifoWakeUpSensors = 0;
1929 size_t fifoNonWakeUpSensors = 0;
Aravind Akella56ae4262014-07-10 16:01:10 -07001930 for (size_t i = 0; i < mSensorInfo.size(); ++i) {
1931 const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i));
1932 if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) {
1933 // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and
1934 // non wake_up sensors.
1935 if (sensor.isWakeUpSensor()) {
1936 fifoWakeUpSensors += sensor.getFifoReservedEventCount();
1937 } else {
1938 fifoNonWakeUpSensors += sensor.getFifoReservedEventCount();
1939 }
1940 } else {
1941 // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors.
1942 if (sensor.isWakeUpSensor()) {
1943 fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ?
1944 fifoWakeUpSensors : sensor.getFifoMaxEventCount();
1945
1946 } else {
1947 fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ?
1948 fifoNonWakeUpSensors : sensor.getFifoMaxEventCount();
1949
1950 }
1951 }
1952 }
1953 if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) {
1954 // It is extremely unlikely that there is a write failure in non batch mode. Return a cache
Aravind Akella6c2664a2014-08-13 12:24:50 -07001955 // size that is equal to that of the batch mode.
Aravind Akellae74baf62014-08-21 12:28:35 -07001956 // ALOGW("Write failure in non-batch mode");
Aravind Akella6c2664a2014-08-13 12:24:50 -07001957 return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t);
Aravind Akella56ae4262014-07-10 16:01:10 -07001958 }
1959 return fifoWakeUpSensors + fifoNonWakeUpSensors;
Aravind Akella9a844cf2014-02-11 18:58:52 -08001960}
1961
Mathias Agopianfc328812010-07-14 23:41:37 -07001962// ---------------------------------------------------------------------------
1963}; // namespace android
1964