2010-07-15 06:41:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-03-12 20:12:44 +00:00
|
|
|
#include <inttypes.h>
|
2010-11-12 01:58:51 +00:00
|
|
|
#include <math.h>
|
2014-03-12 20:12:44 +00:00
|
|
|
#include <stdint.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
#include <sys/types.h>
|
2014-07-10 23:01:10 +00:00
|
|
|
#include <sys/socket.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2011-05-28 01:18:13 +00:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
#include <utils/SortedVector.h>
|
|
|
|
#include <utils/KeyedVector.h>
|
|
|
|
#include <utils/threads.h>
|
|
|
|
#include <utils/Atomic.h>
|
|
|
|
#include <utils/Errors.h>
|
|
|
|
#include <utils/RefBase.h>
|
2010-07-19 22:03:55 +00:00
|
|
|
#include <utils/Singleton.h>
|
2010-07-23 05:19:43 +00:00
|
|
|
#include <utils/String16.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
#include <binder/AppOpsManager.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
#include <binder/BinderService.h>
|
2010-07-19 22:03:55 +00:00
|
|
|
#include <binder/IServiceManager.h>
|
2011-06-27 23:05:52 +00:00
|
|
|
#include <binder/PermissionCache.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
#include <gui/ISensorServer.h>
|
|
|
|
#include <gui/ISensorEventConnection.h>
|
2012-04-03 01:38:02 +00:00
|
|
|
#include <gui/SensorEventQueue.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
#include <hardware/sensors.h>
|
2013-04-08 23:43:12 +00:00
|
|
|
#include <hardware_legacy/power.h>
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2012-09-19 01:49:18 +00:00
|
|
|
#include "BatteryService.h"
|
2011-05-18 05:54:42 +00:00
|
|
|
#include "CorrectedGyroSensor.h"
|
2010-11-12 01:58:51 +00:00
|
|
|
#include "GravitySensor.h"
|
|
|
|
#include "LinearAccelerationSensor.h"
|
2011-05-18 05:54:42 +00:00
|
|
|
#include "OrientationSensor.h"
|
2010-11-12 01:58:51 +00:00
|
|
|
#include "RotationVectorSensor.h"
|
2011-05-18 05:54:42 +00:00
|
|
|
#include "SensorFusion.h"
|
|
|
|
#include "SensorService.h"
|
2010-07-15 06:41:37 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2011-05-28 01:18:13 +00:00
|
|
|
/*
|
|
|
|
* Notes:
|
|
|
|
*
|
|
|
|
* - what about a gyro-corrected magnetic-field sensor?
|
|
|
|
* - run mag sensor from time to time to force calibration
|
|
|
|
* - gravity sensor length is wrong (=> drift in linear-acc sensor)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-07-10 18:24:28 +00:00
|
|
|
const char* SensorService::WAKE_LOCK_NAME = "SensorService_wakelock";
|
2015-04-17 01:57:31 +00:00
|
|
|
// Permissions.
|
|
|
|
static const String16 sDump("android.permission.DUMP");
|
2013-04-08 23:43:12 +00:00
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
SensorService::SensorService()
|
2014-09-29 00:52:41 +00:00
|
|
|
: mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED),
|
|
|
|
mWakeLockAcquired(false)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::onFirstRef()
|
|
|
|
{
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("nuSensorService starting...");
|
2010-11-12 01:58:51 +00:00
|
|
|
SensorDevice& dev(SensorDevice::getInstance());
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
if (dev.initCheck() == NO_ERROR) {
|
|
|
|
sensor_t const* list;
|
2011-07-14 23:39:46 +00:00
|
|
|
ssize_t count = dev.getSensorList(&list);
|
|
|
|
if (count > 0) {
|
|
|
|
ssize_t orientationIndex = -1;
|
2015-07-21 00:29:33 +00:00
|
|
|
bool hasGyro = false, hasAccel = false, hasMag = false;
|
2011-07-14 23:39:46 +00:00
|
|
|
uint32_t virtualSensorsNeeds =
|
|
|
|
(1<<SENSOR_TYPE_GRAVITY) |
|
|
|
|
(1<<SENSOR_TYPE_LINEAR_ACCELERATION) |
|
|
|
|
(1<<SENSOR_TYPE_ROTATION_VECTOR);
|
|
|
|
|
|
|
|
mLastEventSeen.setCapacity(count);
|
|
|
|
for (ssize_t i=0 ; i<count ; i++) {
|
|
|
|
registerSensor( new HardwareSensor(list[i]) );
|
|
|
|
switch (list[i].type) {
|
2015-07-21 00:29:33 +00:00
|
|
|
case SENSOR_TYPE_ACCELEROMETER:
|
|
|
|
hasAccel = true;
|
|
|
|
break;
|
|
|
|
case SENSOR_TYPE_MAGNETIC_FIELD:
|
|
|
|
hasMag = true;
|
|
|
|
break;
|
2011-07-14 23:39:46 +00:00
|
|
|
case SENSOR_TYPE_ORIENTATION:
|
|
|
|
orientationIndex = i;
|
|
|
|
break;
|
|
|
|
case SENSOR_TYPE_GYROSCOPE:
|
2013-05-11 02:32:39 +00:00
|
|
|
case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
|
2011-07-14 23:39:46 +00:00
|
|
|
hasGyro = true;
|
|
|
|
break;
|
|
|
|
case SENSOR_TYPE_GRAVITY:
|
|
|
|
case SENSOR_TYPE_LINEAR_ACCELERATION:
|
|
|
|
case SENSOR_TYPE_ROTATION_VECTOR:
|
|
|
|
virtualSensorsNeeds &= ~(1<<list[i].type);
|
|
|
|
break;
|
|
|
|
}
|
2010-07-20 02:09:10 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2011-07-14 23:39:46 +00:00
|
|
|
// it's safe to instantiate the SensorFusion object here
|
|
|
|
// (it wants to be instantiated after h/w sensors have been
|
|
|
|
// registered)
|
|
|
|
const SensorFusion& fusion(SensorFusion::getInstance());
|
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
// build the sensor list returned to users
|
|
|
|
mUserSensorList = mSensorList;
|
|
|
|
|
2015-07-21 00:29:33 +00:00
|
|
|
if (hasGyro && hasAccel && hasMag) {
|
2013-05-11 02:32:39 +00:00
|
|
|
Sensor aSensor;
|
2011-07-14 23:39:46 +00:00
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
// Add Android virtual sensors if they're not already
|
|
|
|
// available in the HAL
|
2011-07-14 23:39:46 +00:00
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
aSensor = registerVirtualSensor( new RotationVectorSensor() );
|
|
|
|
if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
|
|
|
|
mUserSensorList.add(aSensor);
|
|
|
|
}
|
2010-11-12 01:58:51 +00:00
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
aSensor = registerVirtualSensor( new GravitySensor(list, count) );
|
|
|
|
if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
|
|
|
|
mUserSensorList.add(aSensor);
|
|
|
|
}
|
2012-06-29 02:46:54 +00:00
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) );
|
|
|
|
if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
|
|
|
|
mUserSensorList.add(aSensor);
|
|
|
|
}
|
2012-06-29 02:46:54 +00:00
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
aSensor = registerVirtualSensor( new OrientationSensor() );
|
|
|
|
if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
|
2014-11-05 04:17:05 +00:00
|
|
|
if (orientationIndex == -1) {
|
|
|
|
// some sensor HALs don't provide an orientation sensor.
|
|
|
|
mUserSensorList.add(aSensor);
|
|
|
|
}
|
2011-07-14 23:39:46 +00:00
|
|
|
}
|
2013-05-11 02:32:39 +00:00
|
|
|
|
|
|
|
// virtual debugging sensors are not added to mUserSensorList
|
|
|
|
registerVirtualSensor( new CorrectedGyroSensor(list, count) );
|
|
|
|
registerVirtualSensor( new GyroDriftSensor() );
|
2011-06-09 03:06:50 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 02:46:54 +00:00
|
|
|
// debugging sensor list
|
2013-05-11 02:32:39 +00:00
|
|
|
mUserSensorListDebug = mSensorList;
|
2012-06-29 02:46:54 +00:00
|
|
|
|
2014-08-22 23:11:10 +00:00
|
|
|
// Check if the device really supports batching by looking at the FIFO event
|
|
|
|
// counts for each sensor.
|
|
|
|
bool batchingSupported = false;
|
2015-04-29 23:50:41 +00:00
|
|
|
for (size_t i = 0; i < mSensorList.size(); ++i) {
|
2014-08-22 23:11:10 +00:00
|
|
|
if (mSensorList[i].getFifoMaxEventCount() > 0) {
|
|
|
|
batchingSupported = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (batchingSupported) {
|
|
|
|
// Increase socket buffer size to a max of 100 KB for batching capabilities.
|
|
|
|
mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED;
|
|
|
|
} else {
|
|
|
|
mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare the socketBufferSize value against the system limits and limit
|
|
|
|
// it to maxSystemSocketBufferSize if necessary.
|
2013-09-06 00:03:38 +00:00
|
|
|
FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r");
|
|
|
|
char line[128];
|
|
|
|
if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) {
|
|
|
|
line[sizeof(line) - 1] = '\0';
|
2014-08-22 23:11:10 +00:00
|
|
|
size_t maxSystemSocketBufferSize;
|
|
|
|
sscanf(line, "%zu", &maxSystemSocketBufferSize);
|
|
|
|
if (mSocketBufferSize > maxSystemSocketBufferSize) {
|
|
|
|
mSocketBufferSize = maxSystemSocketBufferSize;
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
mWakeLockAcquired = false;
|
2014-07-10 23:01:10 +00:00
|
|
|
mLooper = new Looper(false);
|
2014-09-08 22:45:47 +00:00
|
|
|
const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
|
|
|
|
mSensorEventBuffer = new sensors_event_t[minBufferSize];
|
|
|
|
mSensorEventScratch = new sensors_event_t[minBufferSize];
|
|
|
|
mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize];
|
2015-04-17 01:57:31 +00:00
|
|
|
mCurrentOperatingMode = NORMAL;
|
2014-10-07 21:13:12 +00:00
|
|
|
|
2015-06-18 21:18:28 +00:00
|
|
|
mNextSensorRegIndex = 0;
|
|
|
|
for (int i = 0; i < SENSOR_REGISTRATIONS_BUF_SIZE; ++i) {
|
|
|
|
mLastNSensorRegistrations.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
mInitCheck = NO_ERROR;
|
2014-10-30 00:55:20 +00:00
|
|
|
mAckReceiver = new SensorEventAckReceiver(this);
|
|
|
|
mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY);
|
2014-10-07 21:13:12 +00:00
|
|
|
run("SensorService", PRIORITY_URGENT_DISPLAY);
|
2011-07-14 23:39:46 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
Sensor SensorService::registerSensor(SensorInterface* s)
|
2010-11-12 01:58:51 +00:00
|
|
|
{
|
|
|
|
sensors_event_t event;
|
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
|
|
|
|
const Sensor sensor(s->getSensor());
|
|
|
|
// add to the sensor list (returned to clients)
|
|
|
|
mSensorList.add(sensor);
|
|
|
|
// add to our handle->SensorInterface mapping
|
|
|
|
mSensorMap.add(sensor.getHandle(), s);
|
|
|
|
// create an entry in the mLastEventSeen array
|
2015-05-07 19:40:52 +00:00
|
|
|
mLastEventSeen.add(sensor.getHandle(), NULL);
|
2013-05-11 02:32:39 +00:00
|
|
|
|
|
|
|
return sensor;
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:32:39 +00:00
|
|
|
Sensor SensorService::registerVirtualSensor(SensorInterface* s)
|
2010-11-12 01:58:51 +00:00
|
|
|
{
|
2013-05-11 02:32:39 +00:00
|
|
|
Sensor sensor = registerSensor(s);
|
2010-11-12 01:58:51 +00:00
|
|
|
mVirtualSensorList.add( s );
|
2013-05-11 02:32:39 +00:00
|
|
|
return sensor;
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
SensorService::~SensorService()
|
|
|
|
{
|
2010-11-12 01:58:51 +00:00
|
|
|
for (size_t i=0 ; i<mSensorMap.size() ; i++)
|
|
|
|
delete mSensorMap.valueAt(i);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 23:54:35 +00:00
|
|
|
status_t SensorService::dump(int fd, const Vector<String16>& args)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
|
|
|
String8 result;
|
2011-06-27 23:05:52 +00:00
|
|
|
if (!PermissionCache::checkCallingPermission(sDump)) {
|
2013-07-03 23:20:57 +00:00
|
|
|
result.appendFormat("Permission Denial: "
|
2014-04-07 22:52:37 +00:00
|
|
|
"can't dump SensorService from pid=%d, uid=%d\n",
|
2010-07-15 06:41:37 +00:00
|
|
|
IPCThreadState::self()->getCallingPid(),
|
|
|
|
IPCThreadState::self()->getCallingUid());
|
2015-05-07 19:40:52 +00:00
|
|
|
} else {
|
2015-06-29 19:37:48 +00:00
|
|
|
if (args.size() > 2) {
|
2015-02-11 23:54:35 +00:00
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
SensorDevice& dev(SensorDevice::getInstance());
|
2015-06-29 19:37:48 +00:00
|
|
|
if (args.size() == 2 && args[0] == String16("restrict")) {
|
2015-05-07 19:40:52 +00:00
|
|
|
// If already in restricted mode. Ignore.
|
|
|
|
if (mCurrentOperatingMode == RESTRICTED) {
|
|
|
|
return status_t(NO_ERROR);
|
|
|
|
}
|
|
|
|
// If in any mode other than normal, ignore.
|
|
|
|
if (mCurrentOperatingMode != NORMAL) {
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
2015-04-17 01:57:31 +00:00
|
|
|
mCurrentOperatingMode = RESTRICTED;
|
2015-02-11 23:54:35 +00:00
|
|
|
dev.disableAllSensors();
|
|
|
|
// Clear all pending flush connections for all active sensors. If one of the active
|
|
|
|
// connections has called flush() and the underlying sensor has been disabled before a
|
|
|
|
// flush complete event is returned, we need to remove the connection from this queue.
|
|
|
|
for (size_t i=0 ; i< mActiveSensors.size(); ++i) {
|
|
|
|
mActiveSensors.valueAt(i)->clearAllPendingFlushConnections();
|
|
|
|
}
|
2015-06-29 19:37:48 +00:00
|
|
|
mWhiteListedPackage.setTo(String8(args[1]));
|
2015-05-07 19:40:52 +00:00
|
|
|
return status_t(NO_ERROR);
|
|
|
|
} else if (args.size() == 1 && args[0] == String16("enable")) {
|
|
|
|
// If currently in restricted mode, reset back to NORMAL mode else ignore.
|
|
|
|
if (mCurrentOperatingMode == RESTRICTED) {
|
|
|
|
mCurrentOperatingMode = NORMAL;
|
|
|
|
dev.enableAllSensors();
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
2015-06-29 19:37:48 +00:00
|
|
|
if (mCurrentOperatingMode == DATA_INJECTION) {
|
|
|
|
resetToNormalModeLocked();
|
|
|
|
}
|
|
|
|
mWhiteListedPackage.clear();
|
2015-05-07 19:40:52 +00:00
|
|
|
return status_t(NO_ERROR);
|
2015-06-29 19:37:48 +00:00
|
|
|
} else if (args.size() == 2 && args[0] == String16("data_injection")) {
|
|
|
|
if (mCurrentOperatingMode == NORMAL) {
|
|
|
|
dev.disableAllSensors();
|
|
|
|
status_t err = dev.setMode(DATA_INJECTION);
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
mCurrentOperatingMode = DATA_INJECTION;
|
|
|
|
} else {
|
|
|
|
// Re-enable sensors.
|
|
|
|
dev.enableAllSensors();
|
|
|
|
}
|
|
|
|
mWhiteListedPackage.setTo(String8(args[1]));
|
|
|
|
return NO_ERROR;
|
|
|
|
} else if (mCurrentOperatingMode == DATA_INJECTION) {
|
|
|
|
// Already in DATA_INJECTION mode. Treat this as a no_op.
|
|
|
|
return NO_ERROR;
|
|
|
|
} else {
|
|
|
|
// Transition to data injection mode supported only from NORMAL mode.
|
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
2015-06-24 15:31:32 +00:00
|
|
|
} else if (mSensorList.size() == 0) {
|
|
|
|
result.append("No Sensors on the device\n");
|
2015-05-07 19:40:52 +00:00
|
|
|
} else {
|
|
|
|
// Default dump the sensor list and debugging information.
|
|
|
|
result.append("Sensor List:\n");
|
|
|
|
for (size_t i=0 ; i<mSensorList.size() ; i++) {
|
|
|
|
const Sensor& s(mSensorList[i]);
|
|
|
|
result.appendFormat(
|
|
|
|
"%-15s| %-10s| version=%d |%-20s| 0x%08x | \"%s\" | type=%d |",
|
|
|
|
s.getName().string(),
|
|
|
|
s.getVendor().string(),
|
|
|
|
s.getVersion(),
|
|
|
|
s.getStringType().string(),
|
|
|
|
s.getHandle(),
|
|
|
|
s.getRequiredPermission().string(),
|
|
|
|
s.getType());
|
|
|
|
|
|
|
|
const int reportingMode = s.getReportingMode();
|
|
|
|
if (reportingMode == AREPORTING_MODE_CONTINUOUS) {
|
|
|
|
result.append(" continuous | ");
|
|
|
|
} else if (reportingMode == AREPORTING_MODE_ON_CHANGE) {
|
|
|
|
result.append(" on-change | ");
|
|
|
|
} else if (reportingMode == AREPORTING_MODE_ONE_SHOT) {
|
|
|
|
result.append(" one-shot | ");
|
|
|
|
} else {
|
|
|
|
result.append(" special-trigger | ");
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
if (s.getMaxDelay() > 0) {
|
|
|
|
result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay());
|
|
|
|
} else {
|
|
|
|
result.appendFormat("maxDelay=%dus |", s.getMaxDelay());
|
|
|
|
}
|
2014-06-04 02:19:57 +00:00
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
if (s.getMinDelay() > 0) {
|
|
|
|
result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay());
|
|
|
|
} else {
|
|
|
|
result.appendFormat("minDelay=%dus |", s.getMinDelay());
|
|
|
|
}
|
2014-06-04 02:19:57 +00:00
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
if (s.getFifoMaxEventCount() > 0) {
|
|
|
|
result.appendFormat("FifoMax=%d events | ",
|
|
|
|
s.getFifoMaxEventCount());
|
|
|
|
} else {
|
|
|
|
result.append("no batching | ");
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
if (s.isWakeUpSensor()) {
|
|
|
|
result.appendFormat("wakeUp | ");
|
|
|
|
} else {
|
|
|
|
result.appendFormat("non-wakeUp | ");
|
|
|
|
}
|
2013-07-03 23:20:57 +00:00
|
|
|
|
2015-06-18 21:18:28 +00:00
|
|
|
int bufIndex = mLastEventSeen.indexOfKey(s.getHandle());
|
|
|
|
if (bufIndex >= 0) {
|
|
|
|
const CircularBuffer* buf = mLastEventSeen.valueAt(bufIndex);
|
|
|
|
if (buf != NULL && s.getRequiredPermission().isEmpty()) {
|
|
|
|
buf->printBuffer(result);
|
|
|
|
} else {
|
|
|
|
result.append("last=<> \n");
|
|
|
|
}
|
2015-05-07 19:40:52 +00:00
|
|
|
}
|
|
|
|
result.append("\n");
|
|
|
|
}
|
|
|
|
SensorFusion::getInstance().dump(result);
|
|
|
|
SensorDevice::getInstance().dump(result);
|
|
|
|
|
|
|
|
result.append("Active sensors:\n");
|
|
|
|
for (size_t i=0 ; i<mActiveSensors.size() ; i++) {
|
|
|
|
int handle = mActiveSensors.keyAt(i);
|
|
|
|
result.appendFormat("%s (handle=0x%08x, connections=%zu)\n",
|
|
|
|
getSensorName(handle).string(),
|
|
|
|
handle,
|
|
|
|
mActiveSensors.valueAt(i)->getNumConnections());
|
2013-07-03 23:20:57 +00:00
|
|
|
}
|
2013-09-06 00:03:38 +00:00
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
result.appendFormat("Socket Buffer size = %d events\n",
|
|
|
|
mSocketBufferSize/sizeof(sensors_event_t));
|
2015-06-18 21:18:28 +00:00
|
|
|
result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" :
|
|
|
|
"not held");
|
2015-05-07 19:40:52 +00:00
|
|
|
result.appendFormat("Mode :");
|
|
|
|
switch(mCurrentOperatingMode) {
|
|
|
|
case NORMAL:
|
|
|
|
result.appendFormat(" NORMAL\n");
|
|
|
|
break;
|
|
|
|
case RESTRICTED:
|
2015-06-29 19:37:48 +00:00
|
|
|
result.appendFormat(" RESTRICTED : %s\n", mWhiteListedPackage.string());
|
2015-05-07 19:40:52 +00:00
|
|
|
break;
|
|
|
|
case DATA_INJECTION:
|
2015-06-29 19:37:48 +00:00
|
|
|
result.appendFormat(" DATA_INJECTION : %s\n", mWhiteListedPackage.string());
|
2015-05-07 19:40:52 +00:00
|
|
|
}
|
|
|
|
result.appendFormat("%zd active connections\n", mActiveConnections.size());
|
2013-09-06 00:03:38 +00:00
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
for (size_t i=0 ; i < mActiveConnections.size() ; i++) {
|
|
|
|
sp<SensorEventConnection> connection(mActiveConnections[i].promote());
|
|
|
|
if (connection != 0) {
|
|
|
|
result.appendFormat("Connection Number: %zu \n", i);
|
|
|
|
connection->dump(result);
|
|
|
|
}
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
2015-06-18 21:18:28 +00:00
|
|
|
|
|
|
|
result.appendFormat("Previous Registrations:\n");
|
|
|
|
// Log in the reverse chronological order.
|
|
|
|
int currentIndex = (mNextSensorRegIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) %
|
|
|
|
SENSOR_REGISTRATIONS_BUF_SIZE;
|
|
|
|
const int startIndex = currentIndex;
|
|
|
|
do {
|
|
|
|
const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[currentIndex];
|
|
|
|
if (SensorRegistrationInfo::isSentinel(reg_info)) {
|
|
|
|
// Ignore sentinel, proceed to next item.
|
|
|
|
currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) %
|
|
|
|
SENSOR_REGISTRATIONS_BUF_SIZE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (reg_info.mActivated) {
|
|
|
|
result.appendFormat("%02d:%02d:%02d activated package=%s handle=0x%08x "
|
|
|
|
"samplingRate=%dus maxReportLatency=%dus\n",
|
|
|
|
reg_info.mHour, reg_info.mMin, reg_info.mSec,
|
|
|
|
reg_info.mPackageName.string(), reg_info.mSensorHandle,
|
|
|
|
reg_info.mSamplingRateUs, reg_info.mMaxReportLatencyUs);
|
|
|
|
} else {
|
|
|
|
result.appendFormat("%02d:%02d:%02d de-activated package=%s handle=0x%08x\n",
|
|
|
|
reg_info.mHour, reg_info.mMin, reg_info.mSec,
|
|
|
|
reg_info.mPackageName.string(), reg_info.mSensorHandle);
|
|
|
|
}
|
|
|
|
currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) %
|
|
|
|
SENSOR_REGISTRATIONS_BUF_SIZE;
|
|
|
|
} while(startIndex != currentIndex);
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
write(fd, result.string(), result.size());
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
|
2013-04-08 23:43:12 +00:00
|
|
|
sensors_event_t const* buffer, const int count) {
|
|
|
|
for (int i=0 ; i<count ; i++) {
|
|
|
|
int handle = buffer[i].sensor;
|
2014-09-08 22:45:47 +00:00
|
|
|
if (buffer[i].type == SENSOR_TYPE_META_DATA) {
|
|
|
|
handle = buffer[i].meta_data.sensor;
|
|
|
|
}
|
2014-06-04 02:19:57 +00:00
|
|
|
if (connection->hasSensor(handle)) {
|
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
|
|
|
// If this buffer has an event from a one_shot sensor and this connection is registered
|
|
|
|
// for this particular one_shot sensor, try cleaning up the connection.
|
|
|
|
if (sensor != NULL &&
|
|
|
|
sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
|
|
|
|
sensor->autoDisable(connection.get(), handle);
|
2014-02-12 02:58:52 +00:00
|
|
|
cleanupWithoutDisableLocked(connection, handle);
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
2015-04-17 01:57:31 +00:00
|
|
|
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
2015-04-17 01:57:31 +00:00
|
|
|
}
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
bool SensorService::threadLoop()
|
|
|
|
{
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("nuSensorService thread starting...");
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2013-09-10 06:36:25 +00:00
|
|
|
// each virtual sensor could generate an event per "real" event, that's why we need
|
|
|
|
// to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT.
|
|
|
|
// in practice, this is too aggressive, but guaranteed to be enough.
|
|
|
|
const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT;
|
|
|
|
const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size());
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
SensorDevice& device(SensorDevice::getInstance());
|
|
|
|
const size_t vcount = mVirtualSensorList.size();
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2013-04-08 23:43:12 +00:00
|
|
|
const int halVersion = device.getHalDeviceVersion();
|
2010-07-15 06:41:37 +00:00
|
|
|
do {
|
2014-09-08 22:45:47 +00:00
|
|
|
ssize_t count = device.poll(mSensorEventBuffer, numEventMax);
|
|
|
|
if (count < 0) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("sensor poll failed (%s)", strerror(-count));
|
2010-07-15 06:41:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
// Reset sensors_event_t.flags to zero for all events in the buffer.
|
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-08 22:45:47 +00:00
|
|
|
mSensorEventBuffer[i].flags = 0;
|
2014-07-10 23:01:10 +00:00
|
|
|
}
|
2014-09-25 05:12:58 +00:00
|
|
|
|
|
|
|
// Make a copy of the connection vector as some connections may be removed during the
|
|
|
|
// course of this loop (especially when one-shot sensor events are present in the
|
|
|
|
// sensor_event buffer). Promote all connections to StrongPointers before the lock is
|
|
|
|
// acquired. If the destructor of the sp gets called when the lock is acquired, it may
|
|
|
|
// result in a deadlock as ~SensorEventConnection() needs to acquire mLock again for
|
|
|
|
// cleanup. So copy all the strongPointers to a vector before the lock is acquired.
|
|
|
|
SortedVector< sp<SensorEventConnection> > activeConnections;
|
2014-10-30 00:55:20 +00:00
|
|
|
populateActiveConnections(&activeConnections);
|
2014-02-12 02:58:52 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
// Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The
|
|
|
|
// rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock,
|
|
|
|
// sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should
|
|
|
|
// not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and
|
|
|
|
// releasing the wakelock.
|
|
|
|
bool bufferHasWakeUpEvent = false;
|
2013-04-08 23:43:12 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-08 22:45:47 +00:00
|
|
|
if (isWakeUpSensorEvent(mSensorEventBuffer[i])) {
|
2014-02-12 02:58:52 +00:00
|
|
|
bufferHasWakeUpEvent = true;
|
|
|
|
break;
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
if (bufferHasWakeUpEvent && !mWakeLockAcquired) {
|
2014-10-30 00:55:20 +00:00
|
|
|
setWakeLockAcquiredLocked(true);
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
2014-09-08 22:45:47 +00:00
|
|
|
recordLastValueLocked(mSensorEventBuffer, count);
|
2010-11-11 01:50:28 +00:00
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
// handle virtual sensors
|
|
|
|
if (count && vcount) {
|
2014-09-08 22:45:47 +00:00
|
|
|
sensors_event_t const * const event = mSensorEventBuffer;
|
2014-02-12 02:58:52 +00:00
|
|
|
const size_t activeVirtualSensorCount = mActiveVirtualSensors.size();
|
2010-11-12 01:58:51 +00:00
|
|
|
if (activeVirtualSensorCount) {
|
|
|
|
size_t k = 0;
|
2011-05-18 05:54:42 +00:00
|
|
|
SensorFusion& fusion(SensorFusion::getInstance());
|
|
|
|
if (fusion.isEnabled()) {
|
|
|
|
for (size_t i=0 ; i<size_t(count) ; i++) {
|
|
|
|
fusion.process(event[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-05-30 02:46:14 +00:00
|
|
|
for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) {
|
2010-11-12 01:58:51 +00:00
|
|
|
for (size_t j=0 ; j<activeVirtualSensorCount ; j++) {
|
2012-05-30 02:46:14 +00:00
|
|
|
if (count + k >= minBufferSize) {
|
|
|
|
ALOGE("buffer too small to hold all events: "
|
2014-06-10 21:50:02 +00:00
|
|
|
"count=%zd, k=%zu, size=%zu",
|
2012-05-30 02:46:14 +00:00
|
|
|
count, k, minBufferSize);
|
|
|
|
break;
|
|
|
|
}
|
2010-11-12 01:58:51 +00:00
|
|
|
sensors_event_t out;
|
2014-02-12 02:58:52 +00:00
|
|
|
SensorInterface* si = mActiveVirtualSensors.valueAt(j);
|
2012-05-30 02:46:14 +00:00
|
|
|
if (si->process(&out, event[i])) {
|
2014-09-08 22:45:47 +00:00
|
|
|
mSensorEventBuffer[count + k] = out;
|
2010-11-12 01:58:51 +00:00
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (k) {
|
|
|
|
// record the last synthesized values
|
2014-09-08 22:45:47 +00:00
|
|
|
recordLastValueLocked(&mSensorEventBuffer[count], k);
|
2010-11-12 01:58:51 +00:00
|
|
|
count += k;
|
|
|
|
// sort the buffer by time-stamps
|
2014-09-08 22:45:47 +00:00
|
|
|
sortEventBuffer(mSensorEventBuffer, count);
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:43:12 +00:00
|
|
|
// handle backward compatibility for RotationVector sensor
|
|
|
|
if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) {
|
|
|
|
for (int i = 0; i < count; i++) {
|
2014-09-08 22:45:47 +00:00
|
|
|
if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) {
|
2013-04-08 23:43:12 +00:00
|
|
|
// All the 4 components of the quaternion should be available
|
|
|
|
// No heading accuracy. Set it to -1
|
2014-09-08 22:45:47 +00:00
|
|
|
mSensorEventBuffer[i].data[4] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map flush_complete_events in the buffer to SensorEventConnections which called
|
|
|
|
// flush on the hardware sensor. mapFlushEventsToConnections[i] will be the
|
|
|
|
// SensorEventConnection mapped to the corresponding flush_complete_event in
|
|
|
|
// mSensorEventBuffer[i] if such a mapping exists (NULL otherwise).
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
mMapFlushEventsToConnections[i] = NULL;
|
|
|
|
if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) {
|
|
|
|
const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor;
|
|
|
|
SensorRecord* rec = mActiveSensors.valueFor(sensor_handle);
|
|
|
|
if (rec != NULL) {
|
|
|
|
mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection();
|
|
|
|
rec->removeFirstPendingFlushConnection();
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
// Send our events to clients. Check the state of wake lock for each client and release the
|
|
|
|
// lock if none of the clients need it.
|
|
|
|
bool needsWakeLock = false;
|
2014-09-08 22:45:47 +00:00
|
|
|
size_t numConnections = activeConnections.size();
|
|
|
|
for (size_t i=0 ; i < numConnections; ++i) {
|
2014-09-25 05:12:58 +00:00
|
|
|
if (activeConnections[i] != 0) {
|
|
|
|
activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch,
|
2014-09-08 22:45:47 +00:00
|
|
|
mMapFlushEventsToConnections);
|
2014-09-25 05:12:58 +00:00
|
|
|
needsWakeLock |= activeConnections[i]->needsWakeLock();
|
2014-09-08 22:45:47 +00:00
|
|
|
// If the connection has one-shot sensors, it may be cleaned up after first trigger.
|
|
|
|
// Early check for one-shot sensors.
|
2014-09-25 05:12:58 +00:00
|
|
|
if (activeConnections[i]->hasOneShotSensors()) {
|
|
|
|
cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer,
|
|
|
|
count);
|
2014-09-08 22:45:47 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-08 23:43:12 +00:00
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
if (mWakeLockAcquired && !needsWakeLock) {
|
2014-10-30 00:55:20 +00:00
|
|
|
setWakeLockAcquiredLocked(false);
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
2014-09-08 22:45:47 +00:00
|
|
|
} while (!Thread::exitPending());
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2012-01-05 23:22:43 +00:00
|
|
|
ALOGW("Exiting SensorService::threadLoop => aborting...");
|
2011-11-10 01:50:15 +00:00
|
|
|
abort();
|
2010-07-15 06:41:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
sp<Looper> SensorService::getLooper() const {
|
|
|
|
return mLooper;
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:55:20 +00:00
|
|
|
void SensorService::resetAllWakeLockRefCounts() {
|
|
|
|
SortedVector< sp<SensorEventConnection> > activeConnections;
|
|
|
|
populateActiveConnections(&activeConnections);
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
for (size_t i=0 ; i < activeConnections.size(); ++i) {
|
|
|
|
if (activeConnections[i] != 0) {
|
|
|
|
activeConnections[i]->resetWakeLockRefCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setWakeLockAcquiredLocked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::setWakeLockAcquiredLocked(bool acquire) {
|
|
|
|
if (acquire) {
|
|
|
|
if (!mWakeLockAcquired) {
|
|
|
|
acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME);
|
|
|
|
mWakeLockAcquired = true;
|
|
|
|
}
|
|
|
|
mLooper->wake();
|
|
|
|
} else {
|
|
|
|
if (mWakeLockAcquired) {
|
|
|
|
release_wake_lock(WAKE_LOCK_NAME);
|
|
|
|
mWakeLockAcquired = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SensorService::isWakeLockAcquired() {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
return mWakeLockAcquired;
|
|
|
|
}
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
bool SensorService::SensorEventAckReceiver::threadLoop() {
|
|
|
|
ALOGD("new thread SensorEventAckReceiver");
|
2014-10-30 00:55:20 +00:00
|
|
|
sp<Looper> looper = mService->getLooper();
|
2014-07-10 23:01:10 +00:00
|
|
|
do {
|
2014-10-30 00:55:20 +00:00
|
|
|
bool wakeLockAcquired = mService->isWakeLockAcquired();
|
|
|
|
int timeout = -1;
|
|
|
|
if (wakeLockAcquired) timeout = 5000;
|
|
|
|
int ret = looper->pollOnce(timeout);
|
|
|
|
if (ret == ALOOPER_POLL_TIMEOUT) {
|
|
|
|
mService->resetAllWakeLockRefCounts();
|
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
} while(!Thread::exitPending());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
void SensorService::recordLastValueLocked(
|
2014-03-04 03:02:46 +00:00
|
|
|
const sensors_event_t* buffer, size_t count) {
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
2015-05-07 19:40:52 +00:00
|
|
|
if (buffer[i].type != SENSOR_TYPE_META_DATA) {
|
|
|
|
CircularBuffer* &circular_buf = mLastEventSeen.editValueFor(buffer[i].sensor);
|
|
|
|
if (circular_buf == NULL) {
|
|
|
|
circular_buf = new CircularBuffer(buffer[i].type);
|
2014-03-04 03:02:46 +00:00
|
|
|
}
|
2015-05-07 19:40:52 +00:00
|
|
|
circular_buf->addEvent(buffer[i]);
|
2010-11-11 01:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count)
|
|
|
|
{
|
|
|
|
struct compar {
|
|
|
|
static int cmp(void const* lhs, void const* rhs) {
|
|
|
|
sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs);
|
|
|
|
sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs);
|
2012-04-20 01:18:24 +00:00
|
|
|
return l->timestamp - r->timestamp;
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
qsort(buffer, count, sizeof(sensors_event_t), compar::cmp);
|
|
|
|
}
|
|
|
|
|
2010-07-19 22:20:39 +00:00
|
|
|
String8 SensorService::getSensorName(int handle) const {
|
2011-06-09 03:06:50 +00:00
|
|
|
size_t count = mUserSensorList.size();
|
2010-07-19 22:20:39 +00:00
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
2011-06-09 03:06:50 +00:00
|
|
|
const Sensor& sensor(mUserSensorList[i]);
|
2010-07-19 22:20:39 +00:00
|
|
|
if (sensor.getHandle() == handle) {
|
|
|
|
return sensor.getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String8 result("unknown");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:43:10 +00:00
|
|
|
bool SensorService::isVirtualSensor(int handle) const {
|
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
|
|
|
return sensor->isVirtual();
|
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const {
|
2014-07-15 00:07:33 +00:00
|
|
|
int handle = event.sensor;
|
|
|
|
if (event.type == SENSOR_TYPE_META_DATA) {
|
|
|
|
handle = event.meta_data.sensor;
|
|
|
|
}
|
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
|
|
|
return sensor != NULL && sensor->getSensor().isWakeUpSensor();
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
SensorService::SensorRecord * SensorService::getSensorRecord(int handle) {
|
|
|
|
return mActiveSensors.valueFor(handle);
|
|
|
|
}
|
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
Vector<Sensor> SensorService::getSensorList(const String16& opPackageName)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
2012-06-29 02:46:54 +00:00
|
|
|
char value[PROPERTY_VALUE_MAX];
|
|
|
|
property_get("debug.sensors", value, "0");
|
2014-04-07 22:52:37 +00:00
|
|
|
const Vector<Sensor>& initialSensorList = (atoi(value)) ?
|
|
|
|
mUserSensorListDebug : mUserSensorList;
|
|
|
|
Vector<Sensor> accessibleSensorList;
|
|
|
|
for (size_t i = 0; i < initialSensorList.size(); i++) {
|
|
|
|
Sensor sensor = initialSensorList[i];
|
2015-04-29 23:50:41 +00:00
|
|
|
if (canAccessSensor(sensor, "getSensorList", opPackageName)) {
|
2014-04-07 22:52:37 +00:00
|
|
|
accessibleSensorList.add(sensor);
|
|
|
|
} else {
|
2015-04-29 23:50:41 +00:00
|
|
|
ALOGI("Skipped sensor %s because it requires permission %s and app op %d",
|
2014-11-17 20:06:20 +00:00
|
|
|
sensor.getName().string(),
|
2015-04-29 23:50:41 +00:00
|
|
|
sensor.getRequiredPermission().string(),
|
|
|
|
sensor.getRequiredAppOp());
|
2014-04-07 22:52:37 +00:00
|
|
|
}
|
2012-06-29 02:46:54 +00:00
|
|
|
}
|
2014-04-07 22:52:37 +00:00
|
|
|
return accessibleSensorList;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 01:57:31 +00:00
|
|
|
sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName,
|
2015-04-29 23:50:41 +00:00
|
|
|
int requestedMode, const String16& opPackageName) {
|
2015-04-17 01:57:31 +00:00
|
|
|
// Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION.
|
|
|
|
if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::Autolock _l(mLock);
|
2015-06-29 19:37:48 +00:00
|
|
|
// To create a client in DATA_INJECTION mode to inject data, SensorService should already be
|
|
|
|
// operating in DI mode.
|
|
|
|
if (requestedMode == DATA_INJECTION) {
|
|
|
|
if (mCurrentOperatingMode != DATA_INJECTION) return NULL;
|
|
|
|
if (!isWhiteListedPackage(packageName)) return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-19 00:02:43 +00:00
|
|
|
uid_t uid = IPCThreadState::self()->getCallingUid();
|
2015-04-17 01:57:31 +00:00
|
|
|
sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName,
|
2015-04-29 23:50:41 +00:00
|
|
|
requestedMode == DATA_INJECTION, opPackageName));
|
2015-04-17 01:57:31 +00:00
|
|
|
if (requestedMode == DATA_INJECTION) {
|
|
|
|
if (mActiveConnections.indexOf(result) < 0) {
|
|
|
|
mActiveConnections.add(result);
|
|
|
|
}
|
|
|
|
// Add the associated file descriptor to the Looper for polling whenever there is data to
|
|
|
|
// be injected.
|
|
|
|
result->updateLooperRegistration(mLooper);
|
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-29 19:37:48 +00:00
|
|
|
int SensorService::isDataInjectionEnabled() {
|
2015-04-17 01:57:31 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2015-06-29 19:37:48 +00:00
|
|
|
return (mCurrentOperatingMode == DATA_INJECTION);
|
2015-04-17 01:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorService::resetToNormalMode() {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
return resetToNormalModeLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorService::resetToNormalModeLocked() {
|
|
|
|
SensorDevice& dev(SensorDevice::getInstance());
|
|
|
|
dev.enableAllSensors();
|
|
|
|
status_t err = dev.setMode(NORMAL);
|
|
|
|
mCurrentOperatingMode = NORMAL;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-02-03 22:52:47 +00:00
|
|
|
void SensorService::cleanupConnection(SensorEventConnection* c)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2011-02-03 22:52:47 +00:00
|
|
|
const wp<SensorEventConnection> connection(c);
|
2010-07-21 22:59:50 +00:00
|
|
|
size_t size = mActiveSensors.size();
|
2014-06-10 21:50:02 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size);
|
2010-07-21 22:59:50 +00:00
|
|
|
for (size_t i=0 ; i<size ; ) {
|
2011-02-03 22:52:47 +00:00
|
|
|
int handle = mActiveSensors.keyAt(i);
|
|
|
|
if (c->hasSensor(handle)) {
|
2014-06-10 21:50:02 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle);
|
2010-11-12 01:58:51 +00:00
|
|
|
SensorInterface* sensor = mSensorMap.valueFor( handle );
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle);
|
2010-11-12 01:58:51 +00:00
|
|
|
if (sensor) {
|
2011-02-03 22:52:47 +00:00
|
|
|
sensor->activate(c, false);
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
2014-09-29 00:52:41 +00:00
|
|
|
c->removeSensor(handle);
|
2011-02-03 22:52:47 +00:00
|
|
|
}
|
|
|
|
SensorRecord* rec = mActiveSensors.valueAt(i);
|
2014-06-10 21:50:02 +00:00
|
|
|
ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS,
|
2014-06-10 21:50:02 +00:00
|
|
|
"removing connection %p for sensor[%zu].handle=0x%08x",
|
2011-05-27 23:23:58 +00:00
|
|
|
c, i, handle);
|
|
|
|
|
2011-02-03 22:52:47 +00:00
|
|
|
if (rec && rec->removeConnection(connection)) {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection");
|
2010-07-21 22:59:50 +00:00
|
|
|
mActiveSensors.removeItemsAt(i, 1);
|
2010-11-12 01:58:51 +00:00
|
|
|
mActiveVirtualSensors.removeItem(handle);
|
2010-07-21 22:59:50 +00:00
|
|
|
delete rec;
|
|
|
|
size--;
|
|
|
|
} else {
|
|
|
|
i++;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-29 00:52:41 +00:00
|
|
|
c->updateLooperRegistration(mLooper);
|
2010-07-21 22:59:50 +00:00
|
|
|
mActiveConnections.remove(connection);
|
2012-09-19 01:49:18 +00:00
|
|
|
BatteryService::cleanup(c->getUid());
|
2014-02-12 02:58:52 +00:00
|
|
|
if (c->needsWakeLock()) {
|
|
|
|
checkWakeLockStateLocked();
|
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 22:52:37 +00:00
|
|
|
Sensor SensorService::getSensorFromHandle(int handle) const {
|
|
|
|
return mSensorMap.valueFor(handle)->getSensor();
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
status_t SensorService::enable(const sp<SensorEventConnection>& connection,
|
2015-04-29 23:50:41 +00:00
|
|
|
int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags,
|
|
|
|
const String16& opPackageName)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
2010-07-20 02:09:10 +00:00
|
|
|
if (mInitCheck != NO_ERROR)
|
|
|
|
return mInitCheck;
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
2013-07-12 09:01:16 +00:00
|
|
|
if (sensor == NULL) {
|
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
2014-04-07 22:52:37 +00:00
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
if (!canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) {
|
2014-04-07 22:52:37 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2013-07-12 09:01:16 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2015-06-29 19:37:48 +00:00
|
|
|
if ((mCurrentOperatingMode == RESTRICTED || mCurrentOperatingMode == DATA_INJECTION)
|
|
|
|
&& !isWhiteListedPackage(connection->getPackageName())) {
|
2015-02-11 23:54:35 +00:00
|
|
|
return INVALID_OPERATION;
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:43:12 +00:00
|
|
|
SensorRecord* rec = mActiveSensors.valueFor(handle);
|
|
|
|
if (rec == 0) {
|
|
|
|
rec = new SensorRecord(connection);
|
|
|
|
mActiveSensors.add(handle, rec);
|
|
|
|
if (sensor->isVirtual()) {
|
|
|
|
mActiveVirtualSensors.add(handle, sensor);
|
2010-07-23 04:24:39 +00:00
|
|
|
}
|
2013-04-08 23:43:12 +00:00
|
|
|
} else {
|
|
|
|
if (rec->addConnection(connection)) {
|
2014-02-12 02:58:52 +00:00
|
|
|
// this sensor is already activated, but we are adding a connection that uses it.
|
|
|
|
// Immediately send down the last known value of the requested sensor if it's not a
|
2013-04-08 23:43:12 +00:00
|
|
|
// "continuous" sensor.
|
2014-06-04 02:19:57 +00:00
|
|
|
if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) {
|
2014-02-12 02:58:52 +00:00
|
|
|
// NOTE: The wake_up flag of this event may get set to
|
|
|
|
// WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event.
|
2015-05-07 19:40:52 +00:00
|
|
|
CircularBuffer *circular_buf = mLastEventSeen.valueFor(handle);
|
|
|
|
if (circular_buf) {
|
|
|
|
sensors_event_t event;
|
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
// It is unlikely that this buffer is empty as the sensor is already active.
|
|
|
|
// One possible corner case may be two applications activating an on-change
|
|
|
|
// sensor at the same time.
|
|
|
|
if(circular_buf->populateLastEvent(&event)) {
|
|
|
|
event.sensor = handle;
|
|
|
|
if (event.version == sizeof(sensors_event_t)) {
|
|
|
|
if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) {
|
|
|
|
setWakeLockAcquiredLocked(true);
|
|
|
|
}
|
|
|
|
connection->sendEvents(&event, 1, NULL);
|
|
|
|
if (!connection->needsWakeLock() && mWakeLockAcquired) {
|
|
|
|
checkWakeLockStateLocked();
|
|
|
|
}
|
|
|
|
}
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
2010-11-12 01:58:51 +00:00
|
|
|
}
|
2010-07-21 22:59:50 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-08 23:43:12 +00:00
|
|
|
|
|
|
|
if (connection->addSensor(handle)) {
|
|
|
|
BatteryService::enableSensor(connection->getUid(), handle);
|
|
|
|
// the sensor was added (which means it wasn't already there)
|
|
|
|
// so, see if this connection becomes active
|
|
|
|
if (mActiveConnections.indexOf(connection) < 0) {
|
|
|
|
mActiveConnections.add(connection);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ALOGW("sensor %08x already enabled in connection %p (ignoring)",
|
|
|
|
handle, connection.get());
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:04:23 +00:00
|
|
|
nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
|
|
|
|
if (samplingPeriodNs < minDelayNs) {
|
|
|
|
samplingPeriodNs = minDelayNs;
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d"
|
|
|
|
"rate=%" PRId64 " timeout== %" PRId64"",
|
2013-06-27 19:04:23 +00:00
|
|
|
handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs);
|
|
|
|
|
2015-02-11 23:54:35 +00:00
|
|
|
status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs,
|
2013-06-27 19:04:23 +00:00
|
|
|
maxBatchReportLatencyNs);
|
2014-08-13 19:24:50 +00:00
|
|
|
|
2014-08-22 23:11:10 +00:00
|
|
|
// Call flush() before calling activate() on the sensor. Wait for a first flush complete
|
|
|
|
// event before sending events on this connection. Ignore one-shot sensors which don't
|
|
|
|
// support flush(). Also if this sensor isn't already active, don't call flush().
|
2013-12-15 18:12:17 +00:00
|
|
|
const SensorDevice& device(SensorDevice::getInstance());
|
2014-08-22 23:11:10 +00:00
|
|
|
if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT &&
|
|
|
|
rec->getNumConnections() > 1) {
|
2013-12-15 18:12:17 +00:00
|
|
|
if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) {
|
|
|
|
connection->setFirstFlushPending(handle, true);
|
|
|
|
status_t err_flush = sensor->flush(connection.get(), handle);
|
|
|
|
// Flush may return error if the underlying h/w sensor uses an older HAL.
|
|
|
|
if (err_flush == NO_ERROR) {
|
|
|
|
rec->addPendingFlushConnection(connection.get());
|
|
|
|
} else {
|
|
|
|
connection->setFirstFlushPending(handle, false);
|
|
|
|
}
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-27 19:04:23 +00:00
|
|
|
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle);
|
|
|
|
err = sensor->activate(connection.get(), true);
|
|
|
|
}
|
|
|
|
|
2014-09-29 00:52:41 +00:00
|
|
|
if (err == NO_ERROR) {
|
|
|
|
connection->updateLooperRegistration(mLooper);
|
2015-06-18 21:18:28 +00:00
|
|
|
SensorRegistrationInfo ®_info =
|
|
|
|
mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex);
|
|
|
|
reg_info.mSensorHandle = handle;
|
|
|
|
reg_info.mSamplingRateUs = samplingPeriodNs/1000;
|
|
|
|
reg_info.mMaxReportLatencyUs = maxBatchReportLatencyNs/1000;
|
|
|
|
reg_info.mActivated = true;
|
|
|
|
reg_info.mPackageName = connection->getPackageName();
|
|
|
|
time_t rawtime = time(NULL);
|
|
|
|
struct tm * timeinfo = localtime(&rawtime);
|
|
|
|
reg_info.mHour = timeinfo->tm_hour;
|
|
|
|
reg_info.mMin = timeinfo->tm_min;
|
|
|
|
reg_info.mSec = timeinfo->tm_sec;
|
|
|
|
mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE;
|
2014-07-10 23:01:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-15 18:12:17 +00:00
|
|
|
if (device.getHalDeviceVersion() < SENSORS_DEVICE_API_VERSION_1_1) {
|
|
|
|
// Pre-1.1 sensor HALs had no flush method, and relied on setDelay at init
|
|
|
|
sensor->setDelay(connection.get(), handle, samplingPeriodNs);
|
|
|
|
}
|
|
|
|
|
2013-04-08 23:43:12 +00:00
|
|
|
if (err != NO_ERROR) {
|
2013-06-27 19:04:23 +00:00
|
|
|
// batch/activate has failed, reset our state.
|
2013-07-12 09:01:16 +00:00
|
|
|
cleanupWithoutDisableLocked(connection, handle);
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorService::disable(const sp<SensorEventConnection>& connection,
|
|
|
|
int handle)
|
|
|
|
{
|
2010-07-20 02:09:10 +00:00
|
|
|
if (mInitCheck != NO_ERROR)
|
|
|
|
return mInitCheck;
|
|
|
|
|
2013-07-12 09:01:16 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
status_t err = cleanupWithoutDisableLocked(connection, handle);
|
2013-04-08 23:43:12 +00:00
|
|
|
if (err == NO_ERROR) {
|
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
|
|
|
err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE);
|
2015-06-18 21:18:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
SensorRegistrationInfo ®_info =
|
|
|
|
mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex);
|
|
|
|
reg_info.mActivated = false;
|
|
|
|
reg_info.mPackageName= connection->getPackageName();
|
|
|
|
reg_info.mSensorHandle = handle;
|
|
|
|
time_t rawtime = time(NULL);
|
|
|
|
struct tm * timeinfo = localtime(&rawtime);
|
|
|
|
reg_info.mHour = timeinfo->tm_hour;
|
|
|
|
reg_info.mMin = timeinfo->tm_min;
|
|
|
|
reg_info.mSec = timeinfo->tm_sec;
|
|
|
|
mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE;
|
2013-04-08 23:43:12 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-07-12 09:01:16 +00:00
|
|
|
status_t SensorService::cleanupWithoutDisable(
|
|
|
|
const sp<SensorEventConnection>& connection, int handle) {
|
2010-07-15 06:41:37 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2013-07-12 09:01:16 +00:00
|
|
|
return cleanupWithoutDisableLocked(connection, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorService::cleanupWithoutDisableLocked(
|
|
|
|
const sp<SensorEventConnection>& connection, int handle) {
|
2010-07-15 06:41:37 +00:00
|
|
|
SensorRecord* rec = mActiveSensors.valueFor(handle);
|
|
|
|
if (rec) {
|
|
|
|
// see if this connection becomes inactive
|
2012-09-19 01:49:18 +00:00
|
|
|
if (connection->removeSensor(handle)) {
|
|
|
|
BatteryService::disableSensor(connection->getUid(), handle);
|
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
if (connection->hasAnySensor() == false) {
|
2014-09-29 00:52:41 +00:00
|
|
|
connection->updateLooperRegistration(mLooper);
|
2010-07-15 06:41:37 +00:00
|
|
|
mActiveConnections.remove(connection);
|
|
|
|
}
|
|
|
|
// see if this sensor becomes inactive
|
|
|
|
if (rec->removeConnection(connection)) {
|
|
|
|
mActiveSensors.removeItem(handle);
|
2010-11-12 01:58:51 +00:00
|
|
|
mActiveVirtualSensors.removeItem(handle);
|
2010-07-15 06:41:37 +00:00
|
|
|
delete rec;
|
|
|
|
}
|
2013-04-08 23:43:12 +00:00
|
|
|
return NO_ERROR;
|
2010-07-21 22:59:50 +00:00
|
|
|
}
|
2013-04-08 23:43:12 +00:00
|
|
|
return BAD_VALUE;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:59:50 +00:00
|
|
|
status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection,
|
2015-04-29 23:50:41 +00:00
|
|
|
int handle, nsecs_t ns, const String16& opPackageName)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
2010-07-20 02:09:10 +00:00
|
|
|
if (mInitCheck != NO_ERROR)
|
|
|
|
return mInitCheck;
|
|
|
|
|
2011-11-02 00:37:49 +00:00
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
|
|
|
if (!sensor)
|
|
|
|
return BAD_VALUE;
|
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
if (!canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) {
|
2014-04-07 22:52:37 +00:00
|
|
|
return BAD_VALUE;
|
|
|
|
}
|
|
|
|
|
2010-07-21 22:59:50 +00:00
|
|
|
if (ns < 0)
|
|
|
|
return BAD_VALUE;
|
|
|
|
|
2011-11-08 05:21:47 +00:00
|
|
|
nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs();
|
|
|
|
if (ns < minDelayNs) {
|
|
|
|
ns = minDelayNs;
|
2011-11-02 00:37:49 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:58:51 +00:00
|
|
|
return sensor->setDelay(connection.get(), handle, ns);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection,
|
|
|
|
const String16& opPackageName) {
|
2014-04-07 22:52:37 +00:00
|
|
|
if (mInitCheck != NO_ERROR) return mInitCheck;
|
2014-09-03 22:48:05 +00:00
|
|
|
SensorDevice& dev(SensorDevice::getInstance());
|
|
|
|
const int halVersion = dev.getHalDeviceVersion();
|
|
|
|
status_t err(NO_ERROR);
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
// Loop through all sensors for this connection and call flush on each of them.
|
|
|
|
for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) {
|
|
|
|
const int handle = connection->mSensorInfo.keyAt(i);
|
|
|
|
SensorInterface* sensor = mSensorMap.valueFor(handle);
|
|
|
|
if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
|
|
|
|
ALOGE("flush called on a one-shot sensor");
|
|
|
|
err = INVALID_OPERATION;
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-08 22:45:47 +00:00
|
|
|
if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) {
|
2014-09-03 22:48:05 +00:00
|
|
|
// For older devices just increment pending flush count which will send a trivial
|
|
|
|
// flush complete event.
|
2014-09-29 00:52:41 +00:00
|
|
|
connection->incrementPendingFlushCount(handle);
|
2014-09-03 22:48:05 +00:00
|
|
|
} else {
|
2015-04-29 23:50:41 +00:00
|
|
|
if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) {
|
|
|
|
err = INVALID_OPERATION;
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-03 22:48:05 +00:00
|
|
|
status_t err_flush = sensor->flush(connection.get(), handle);
|
|
|
|
if (err_flush == NO_ERROR) {
|
|
|
|
SensorRecord* rec = mActiveSensors.valueFor(handle);
|
|
|
|
if (rec != NULL) rec->addPendingFlushConnection(connection);
|
|
|
|
}
|
|
|
|
err = (err_flush != NO_ERROR) ? err_flush : err;
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
2014-09-03 22:48:05 +00:00
|
|
|
return err;
|
2014-04-07 22:52:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,
|
|
|
|
const String16& opPackageName) {
|
|
|
|
const String8& requiredPermission = sensor.getRequiredPermission();
|
2014-04-07 22:52:37 +00:00
|
|
|
|
2015-04-29 23:50:41 +00:00
|
|
|
if (requiredPermission.length() <= 0) {
|
2014-04-07 22:52:37 +00:00
|
|
|
return true;
|
2015-04-29 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasPermission = false;
|
|
|
|
|
|
|
|
// Runtime permissions can't use the cache as they may change.
|
|
|
|
if (sensor.isRequiredPermissionRuntime()) {
|
|
|
|
hasPermission = checkPermission(String16(requiredPermission),
|
|
|
|
IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid());
|
2014-04-07 22:52:37 +00:00
|
|
|
} else {
|
2015-04-29 23:50:41 +00:00
|
|
|
hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
|
ALOGE("%s a sensor (%s) without holding its required permission: %s",
|
|
|
|
operation, sensor.getName().string(), sensor.getRequiredPermission().string());
|
2014-04-07 22:52:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-29 23:50:41 +00:00
|
|
|
|
|
|
|
const int32_t opCode = sensor.getRequiredAppOp();
|
|
|
|
if (opCode >= 0) {
|
|
|
|
AppOpsManager appOps;
|
|
|
|
if (appOps.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName)
|
|
|
|
!= AppOpsManager::MODE_ALLOWED) {
|
|
|
|
ALOGE("%s a sensor (%s) without enabled required app op: %D",
|
|
|
|
operation, sensor.getName().string(), opCode);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-04-07 22:52:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
void SensorService::checkWakeLockState() {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
checkWakeLockStateLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::checkWakeLockStateLocked() {
|
|
|
|
if (!mWakeLockAcquired) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool releaseLock = true;
|
|
|
|
for (size_t i=0 ; i<mActiveConnections.size() ; i++) {
|
|
|
|
sp<SensorEventConnection> connection(mActiveConnections[i].promote());
|
|
|
|
if (connection != 0) {
|
|
|
|
if (connection->needsWakeLock()) {
|
|
|
|
releaseLock = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (releaseLock) {
|
2014-10-30 00:55:20 +00:00
|
|
|
setWakeLockAcquiredLocked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
connection->writeToSocketFromCache();
|
|
|
|
if (connection->needsWakeLock()) {
|
|
|
|
setWakeLockAcquiredLocked(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::populateActiveConnections(
|
|
|
|
SortedVector< sp<SensorEventConnection> >* activeConnections) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
for (size_t i=0 ; i < mActiveConnections.size(); ++i) {
|
|
|
|
sp<SensorEventConnection> connection(mActiveConnections[i].promote());
|
|
|
|
if (connection != 0) {
|
|
|
|
activeConnections->add(connection);
|
|
|
|
}
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
|
2015-02-11 23:54:35 +00:00
|
|
|
bool SensorService::isWhiteListedPackage(const String8& packageName) {
|
2015-06-29 19:37:48 +00:00
|
|
|
return (packageName.contains(mWhiteListedPackage.string()));
|
2015-02-11 23:54:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 19:40:52 +00:00
|
|
|
int SensorService::getNumEventsForSensorType(int sensor_event_type) {
|
|
|
|
switch (sensor_event_type) {
|
|
|
|
case SENSOR_TYPE_ROTATION_VECTOR:
|
|
|
|
case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
|
|
|
|
return 5;
|
|
|
|
|
|
|
|
case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
|
|
|
|
case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
case SENSOR_TYPE_GAME_ROTATION_VECTOR:
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
case SENSOR_TYPE_SIGNIFICANT_MOTION:
|
|
|
|
case SENSOR_TYPE_STEP_DETECTOR:
|
|
|
|
case SENSOR_TYPE_STEP_COUNTER:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
SensorService::SensorRecord::SensorRecord(
|
|
|
|
const sp<SensorEventConnection>& connection)
|
|
|
|
{
|
|
|
|
mConnections.add(connection);
|
|
|
|
}
|
|
|
|
|
2010-07-21 22:59:50 +00:00
|
|
|
bool SensorService::SensorRecord::addConnection(
|
2010-07-15 06:41:37 +00:00
|
|
|
const sp<SensorEventConnection>& connection)
|
|
|
|
{
|
|
|
|
if (mConnections.indexOf(connection) < 0) {
|
|
|
|
mConnections.add(connection);
|
2010-07-21 22:59:50 +00:00
|
|
|
return true;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
2010-07-21 22:59:50 +00:00
|
|
|
return false;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SensorService::SensorRecord::removeConnection(
|
|
|
|
const wp<SensorEventConnection>& connection)
|
|
|
|
{
|
|
|
|
ssize_t index = mConnections.indexOf(connection);
|
|
|
|
if (index >= 0) {
|
|
|
|
mConnections.removeItemsAt(index, 1);
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
// Remove this connections from the queue of flush() calls made on this sensor.
|
|
|
|
for (Vector< wp<SensorEventConnection> >::iterator it =
|
|
|
|
mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) {
|
2014-09-29 00:52:41 +00:00
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
if (it->unsafe_get() == connection.unsafe_get()) {
|
|
|
|
it = mPendingFlushConnections.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
return mConnections.size() ? false : true;
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
void SensorService::SensorRecord::addPendingFlushConnection(
|
|
|
|
const sp<SensorEventConnection>& connection) {
|
|
|
|
mPendingFlushConnections.add(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::SensorRecord::removeFirstPendingFlushConnection() {
|
|
|
|
if (mPendingFlushConnections.size() > 0) {
|
|
|
|
mPendingFlushConnections.removeAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SensorService::SensorEventConnection *
|
|
|
|
SensorService::SensorRecord::getFirstPendingFlushConnection() {
|
|
|
|
if (mPendingFlushConnections.size() > 0) {
|
|
|
|
return mPendingFlushConnections[0].unsafe_get();
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:54:35 +00:00
|
|
|
void SensorService::SensorRecord::clearAllPendingFlushConnections() {
|
|
|
|
mPendingFlushConnections.clear();
|
|
|
|
}
|
|
|
|
|
2015-06-18 21:18:28 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
SensorService::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) {
|
|
|
|
mTimestamp = -1;
|
|
|
|
const int numData = SensorService::getNumEventsForSensorType(sensorType);
|
|
|
|
if (sensorType == SENSOR_TYPE_STEP_COUNTER) {
|
|
|
|
mStepCounter = 0;
|
|
|
|
} else {
|
|
|
|
mData = new float[numData];
|
|
|
|
for (int i = 0; i < numData; ++i) {
|
|
|
|
mData[i] = -1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mHour = mMin = mSec = INT32_MIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SensorService::TrimmedSensorEvent::isSentinel(const TrimmedSensorEvent& event) {
|
|
|
|
return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN);
|
|
|
|
}
|
2015-05-07 19:40:52 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
SensorService::CircularBuffer::CircularBuffer(int sensor_event_type) {
|
|
|
|
mNextInd = 0;
|
2015-06-18 21:18:28 +00:00
|
|
|
mBufSize = CIRCULAR_BUF_SIZE;
|
|
|
|
if (sensor_event_type == SENSOR_TYPE_STEP_COUNTER ||
|
|
|
|
sensor_event_type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
|
|
|
|
sensor_event_type == SENSOR_TYPE_ACCELEROMETER) {
|
|
|
|
mBufSize = CIRCULAR_BUF_SIZE * 5;
|
|
|
|
}
|
|
|
|
mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize];
|
2015-05-07 19:40:52 +00:00
|
|
|
mSensorType = sensor_event_type;
|
2015-06-18 21:18:28 +00:00
|
|
|
for (int i = 0; i < mBufSize; ++i) {
|
|
|
|
mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType);
|
2015-05-07 19:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::CircularBuffer::addEvent(const sensors_event_t& sensor_event) {
|
|
|
|
TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd];
|
|
|
|
curr_event->mTimestamp = sensor_event.timestamp;
|
|
|
|
if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
|
|
|
|
curr_event->mStepCounter = sensor_event.u64.step_counter;
|
|
|
|
} else {
|
|
|
|
memcpy(curr_event->mData, sensor_event.data,
|
|
|
|
sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType));
|
|
|
|
}
|
|
|
|
time_t rawtime = time(NULL);
|
|
|
|
struct tm * timeinfo = localtime(&rawtime);
|
|
|
|
curr_event->mHour = timeinfo->tm_hour;
|
|
|
|
curr_event->mMin = timeinfo->tm_min;
|
|
|
|
curr_event->mSec = timeinfo->tm_sec;
|
2015-06-18 21:18:28 +00:00
|
|
|
mNextInd = (mNextInd + 1) % mBufSize;
|
2015-05-07 19:40:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::CircularBuffer::printBuffer(String8& result) const {
|
|
|
|
const int numData = SensorService::getNumEventsForSensorType(mSensorType);
|
|
|
|
int i = mNextInd, eventNum = 1;
|
2015-06-18 21:18:28 +00:00
|
|
|
result.appendFormat("last %d events = < ", mBufSize);
|
2015-05-07 19:40:52 +00:00
|
|
|
do {
|
2015-06-18 21:18:28 +00:00
|
|
|
if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) {
|
2015-05-07 19:40:52 +00:00
|
|
|
// Sentinel, ignore.
|
2015-06-18 21:18:28 +00:00
|
|
|
i = (i + 1) % mBufSize;
|
2015-05-07 19:40:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.appendFormat("%d) ", eventNum++);
|
|
|
|
if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
|
|
|
|
result.appendFormat("%llu,", mTrimmedSensorEventArr[i]->mStepCounter);
|
|
|
|
} else {
|
|
|
|
for (int j = 0; j < numData; ++j) {
|
|
|
|
result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.appendFormat("%lld %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp,
|
|
|
|
mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin,
|
|
|
|
mTrimmedSensorEventArr[i]->mSec);
|
2015-06-18 21:18:28 +00:00
|
|
|
i = (i + 1) % mBufSize;
|
2015-05-07 19:40:52 +00:00
|
|
|
} while (i != mNextInd);
|
|
|
|
result.appendFormat(">\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SensorService::CircularBuffer::populateLastEvent(sensors_event_t *event) {
|
2015-06-18 21:18:28 +00:00
|
|
|
int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize;
|
2015-05-07 19:40:52 +00:00
|
|
|
// Check if the buffer is empty.
|
2015-06-18 21:18:28 +00:00
|
|
|
if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) {
|
2015-05-07 19:40:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
event->version = sizeof(sensors_event_t);
|
|
|
|
event->type = mSensorType;
|
|
|
|
event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp;
|
|
|
|
if (mSensorType == SENSOR_TYPE_STEP_COUNTER) {
|
|
|
|
event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter;
|
|
|
|
} else {
|
|
|
|
memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData,
|
|
|
|
sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SensorService::CircularBuffer::~CircularBuffer() {
|
2015-06-18 21:18:28 +00:00
|
|
|
for (int i = 0; i < mBufSize; ++i) {
|
2015-05-07 19:40:52 +00:00
|
|
|
delete mTrimmedSensorEventArr[i];
|
|
|
|
}
|
|
|
|
delete [] mTrimmedSensorEventArr;
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SensorService::SensorEventConnection::SensorEventConnection(
|
2015-04-29 23:50:41 +00:00
|
|
|
const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode,
|
|
|
|
const String16& opPackageName)
|
2014-09-29 00:52:41 +00:00
|
|
|
: mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
|
2015-04-29 23:50:41 +00:00
|
|
|
mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL),
|
|
|
|
mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) {
|
2014-08-22 23:11:10 +00:00
|
|
|
mChannel = new BitTube(mService->mSocketBufferSize);
|
2014-07-10 23:01:10 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
|
2014-08-21 19:28:35 +00:00
|
|
|
mTotalAcksNeeded = mTotalAcksReceived = 0;
|
2014-07-10 23:01:10 +00:00
|
|
|
#endif
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
SensorService::SensorEventConnection::~SensorEventConnection() {
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
|
2014-09-25 05:12:58 +00:00
|
|
|
mService->cleanupConnection(this);
|
2014-07-10 23:01:10 +00:00
|
|
|
if (mEventCache != NULL) {
|
|
|
|
delete mEventCache;
|
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 23:01:10 +00:00
|
|
|
void SensorService::SensorEventConnection::onFirstRef() {
|
|
|
|
LooperCallback::onFirstRef();
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 02:58:52 +00:00
|
|
|
bool SensorService::SensorEventConnection::needsWakeLock() {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2014-09-29 00:52:41 +00:00
|
|
|
return !mDead && mWakeLockRefCount > 0;
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:55:20 +00:00
|
|
|
void SensorService::SensorEventConnection::resetWakeLockRefCount() {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
|
|
|
mWakeLockRefCount = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:03:38 +00:00
|
|
|
void SensorService::SensorEventConnection::dump(String8& result) {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2015-07-10 18:24:28 +00:00
|
|
|
result.appendFormat("\tOperating Mode: %s\n",mDataInjectionMode ? "DATA_INJECTION" : "NORMAL");
|
2015-06-18 21:18:28 +00:00
|
|
|
result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | "
|
|
|
|
"max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
|
|
|
|
mMaxCacheSize);
|
2013-09-06 00:03:38 +00:00
|
|
|
for (size_t i = 0; i < mSensorInfo.size(); ++i) {
|
|
|
|
const FlushInfo& flushInfo = mSensorInfo.valueAt(i);
|
2014-09-15 00:29:48 +00:00
|
|
|
result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
|
2013-09-06 00:03:38 +00:00
|
|
|
mService->getSensorName(mSensorInfo.keyAt(i)).string(),
|
2014-08-13 19:24:50 +00:00
|
|
|
mSensorInfo.keyAt(i),
|
2013-09-06 00:03:38 +00:00
|
|
|
flushInfo.mFirstFlushPending ? "First flush pending" :
|
|
|
|
"active",
|
2014-09-15 00:29:48 +00:00
|
|
|
flushInfo.mPendingFlushEventsToSend);
|
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
2014-09-15 00:29:48 +00:00
|
|
|
result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |"
|
|
|
|
" total_acks_needed %d | total_acks_recvd %d\n",
|
|
|
|
mEventsReceived,
|
|
|
|
mEventsSent,
|
|
|
|
mEventsSentFromCache,
|
|
|
|
mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize),
|
|
|
|
mTotalAcksNeeded,
|
|
|
|
mTotalAcksReceived);
|
2014-07-10 23:01:10 +00:00
|
|
|
#endif
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:59:50 +00:00
|
|
|
bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
|
2010-11-15 04:55:25 +00:00
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2015-04-29 23:50:41 +00:00
|
|
|
if (!canAccessSensor(mService->getSensorFromHandle(handle),
|
|
|
|
"Tried adding", mOpPackageName)) {
|
2014-04-07 22:52:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-06 00:03:38 +00:00
|
|
|
if (mSensorInfo.indexOfKey(handle) < 0) {
|
|
|
|
mSensorInfo.add(handle, FlushInfo());
|
2010-07-21 22:59:50 +00:00
|
|
|
return true;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
2010-07-21 22:59:50 +00:00
|
|
|
return false;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2010-07-21 22:59:50 +00:00
|
|
|
bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
|
2010-11-15 04:55:25 +00:00
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2013-09-06 00:03:38 +00:00
|
|
|
if (mSensorInfo.removeItem(handle) >= 0) {
|
2010-07-21 22:59:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
|
2010-11-15 04:55:25 +00:00
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2013-09-06 00:03:38 +00:00
|
|
|
return mSensorInfo.indexOfKey(handle) >= 0;
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SensorService::SensorEventConnection::hasAnySensor() const {
|
2010-11-15 04:55:25 +00:00
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2010-07-21 22:59:50 +00:00
|
|
|
return mSensorInfo.size() ? true : false;
|
|
|
|
}
|
|
|
|
|
2014-09-08 22:45:47 +00:00
|
|
|
bool SensorService::SensorEventConnection::hasOneShotSensors() const {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
|
|
|
for (size_t i = 0; i < mSensorInfo.size(); ++i) {
|
|
|
|
const int handle = mSensorInfo.keyAt(i);
|
|
|
|
if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:54:35 +00:00
|
|
|
String8 SensorService::SensorEventConnection::getPackageName() const {
|
|
|
|
return mPackageName;
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:03:38 +00:00
|
|
|
void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
|
|
|
|
bool value) {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
|
|
|
ssize_t index = mSensorInfo.indexOfKey(handle);
|
|
|
|
if (index >= 0) {
|
|
|
|
FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
|
|
|
|
flushInfo.mFirstFlushPending = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 00:52:41 +00:00
|
|
|
void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
|
|
|
updateLooperRegistrationLocked(looper);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::SensorEventConnection::updateLooperRegistrationLocked(
|
|
|
|
const sp<Looper>& looper) {
|
2015-04-17 01:57:31 +00:00
|
|
|
bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) ||
|
|
|
|
mDataInjectionMode;
|
2014-09-29 00:52:41 +00:00
|
|
|
// If all sensors are unregistered OR Looper has encountered an error, we
|
|
|
|
// can remove the Fd from the Looper if it has been previously added.
|
|
|
|
if (!isConnectionActive || mDead) {
|
|
|
|
if (mHasLooperCallbacks) {
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd());
|
|
|
|
looper->removeFd(mChannel->getSendFd());
|
|
|
|
mHasLooperCallbacks = false;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int looper_flags = 0;
|
|
|
|
if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT;
|
2015-04-17 01:57:31 +00:00
|
|
|
if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT;
|
2014-09-29 00:52:41 +00:00
|
|
|
for (size_t i = 0; i < mSensorInfo.size(); ++i) {
|
|
|
|
const int handle = mSensorInfo.keyAt(i);
|
|
|
|
if (mService->getSensorFromHandle(handle).isWakeUpSensor()) {
|
|
|
|
looper_flags |= ALOOPER_EVENT_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If flags is still set to zero, we don't need to add this fd to the Looper, if
|
|
|
|
// the fd has already been added, remove it. This is likely to happen when ALL the
|
|
|
|
// events stored in the cache have been sent to the corresponding app.
|
|
|
|
if (looper_flags == 0) {
|
|
|
|
if (mHasLooperCallbacks) {
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd());
|
|
|
|
looper->removeFd(mChannel->getSendFd());
|
|
|
|
mHasLooperCallbacks = false;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add the file descriptor to the Looper for receiving acknowledegments if the app has
|
|
|
|
// registered for wake-up sensors OR for sending events in the cache.
|
|
|
|
int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL);
|
|
|
|
if (ret == 1) {
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd());
|
|
|
|
mHasLooperCallbacks = true;
|
|
|
|
} else {
|
|
|
|
ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
|
|
|
ssize_t index = mSensorInfo.indexOfKey(handle);
|
|
|
|
if (index >= 0) {
|
|
|
|
FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
|
|
|
|
flushInfo.mPendingFlushEventsToSend++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
status_t SensorService::SensorEventConnection::sendEvents(
|
2014-09-15 00:29:48 +00:00
|
|
|
sensors_event_t const* buffer, size_t numEvents,
|
2014-09-08 22:45:47 +00:00
|
|
|
sensors_event_t* scratch,
|
|
|
|
SensorEventConnection const * const * mapFlushEventsToConnections) {
|
2010-07-22 23:18:10 +00:00
|
|
|
// filter out events not for this connection
|
2015-04-29 23:50:41 +00:00
|
|
|
int count = 0;
|
2014-02-12 02:58:52 +00:00
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2010-07-23 04:24:39 +00:00
|
|
|
if (scratch) {
|
|
|
|
size_t i=0;
|
|
|
|
while (i<numEvents) {
|
2014-08-13 19:24:50 +00:00
|
|
|
int32_t sensor_handle = buffer[i].sensor;
|
2013-06-27 19:04:23 +00:00
|
|
|
if (buffer[i].type == SENSOR_TYPE_META_DATA) {
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ",
|
2014-09-03 22:48:05 +00:00
|
|
|
buffer[i].meta_data.sensor);
|
2014-09-29 00:52:41 +00:00
|
|
|
// Setting sensor_handle to the correct sensor to ensure the sensor events per
|
|
|
|
// connection are filtered correctly. buffer[i].sensor is zero for meta_data
|
|
|
|
// events.
|
2014-08-13 19:24:50 +00:00
|
|
|
sensor_handle = buffer[i].meta_data.sensor;
|
2013-06-27 19:04:23 +00:00
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
ssize_t index = mSensorInfo.indexOfKey(sensor_handle);
|
2014-07-10 23:01:10 +00:00
|
|
|
// Check if this connection has registered for this sensor. If not continue to the
|
|
|
|
// next sensor_event.
|
|
|
|
if (index < 0) {
|
|
|
|
++i;
|
|
|
|
continue;
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
FlushInfo& flushInfo = mSensorInfo.editValueAt(index);
|
2014-08-13 19:24:50 +00:00
|
|
|
// Check if there is a pending flush_complete event for this sensor on this connection.
|
2014-09-08 22:45:47 +00:00
|
|
|
if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
|
|
|
|
this == mapFlushEventsToConnections[i]) {
|
|
|
|
flushInfo.mFirstFlushPending = false;
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ",
|
|
|
|
buffer[i].meta_data.sensor);
|
|
|
|
++i;
|
|
|
|
continue;
|
2014-07-10 23:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there is a pending flush complete event for this sensor on this connection,
|
|
|
|
// ignore the event and proceed to the next.
|
|
|
|
if (flushInfo.mFirstFlushPending) {
|
|
|
|
++i;
|
|
|
|
continue;
|
2010-07-23 04:24:39 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
do {
|
2014-09-08 22:45:47 +00:00
|
|
|
// Keep copying events into the scratch buffer as long as they are regular
|
|
|
|
// sensor_events are from the same sensor_handle OR they are flush_complete_events
|
|
|
|
// from the same sensor_handle AND the current connection is mapped to the
|
|
|
|
// corresponding flush_complete_event.
|
2014-07-10 23:01:10 +00:00
|
|
|
if (buffer[i].type == SENSOR_TYPE_META_DATA) {
|
2014-09-08 22:45:47 +00:00
|
|
|
if (this == mapFlushEventsToConnections[i]) {
|
2014-07-10 23:01:10 +00:00
|
|
|
scratch[count++] = buffer[i];
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
// Regular sensor event, just copy it to the scratch buffer.
|
|
|
|
scratch[count++] = buffer[i++];
|
|
|
|
}
|
2014-09-08 22:45:47 +00:00
|
|
|
} while ((i<numEvents) && ((buffer[i].sensor == sensor_handle &&
|
|
|
|
buffer[i].type != SENSOR_TYPE_META_DATA) ||
|
2014-07-10 23:01:10 +00:00
|
|
|
(buffer[i].type == SENSOR_TYPE_META_DATA &&
|
2014-08-13 19:24:50 +00:00
|
|
|
buffer[i].meta_data.sensor == sensor_handle)));
|
2010-07-22 23:18:10 +00:00
|
|
|
}
|
2010-07-23 04:24:39 +00:00
|
|
|
} else {
|
|
|
|
scratch = const_cast<sensors_event_t *>(buffer);
|
|
|
|
count = numEvents;
|
2010-07-22 23:18:10 +00:00
|
|
|
}
|
2010-07-15 06:41:37 +00:00
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
sendPendingFlushEventsLocked();
|
2014-07-10 23:01:10 +00:00
|
|
|
// Early return if there are no events for this connection.
|
|
|
|
if (count == 0) {
|
|
|
|
return status_t(NO_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
mEventsReceived += count;
|
|
|
|
#endif
|
|
|
|
if (mCacheSize != 0) {
|
|
|
|
// There are some events in the cache which need to be sent first. Copy this buffer to
|
|
|
|
// the end of cache.
|
|
|
|
if (mCacheSize + count <= mMaxCacheSize) {
|
|
|
|
memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
|
|
|
|
mCacheSize += count;
|
|
|
|
} else {
|
2014-08-13 19:24:50 +00:00
|
|
|
// Check if any new sensors have registered on this connection which may have increased
|
|
|
|
// the max cache size that is desired.
|
|
|
|
if (mCacheSize + count < computeMaxCacheSizeLocked()) {
|
|
|
|
reAllocateCacheLocked(scratch, count);
|
|
|
|
return status_t(NO_ERROR);
|
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
// Some events need to be dropped.
|
|
|
|
int remaningCacheSize = mMaxCacheSize - mCacheSize;
|
|
|
|
if (remaningCacheSize != 0) {
|
|
|
|
memcpy(&mEventCache[mCacheSize], scratch,
|
|
|
|
remaningCacheSize * sizeof(sensors_event_t));
|
|
|
|
}
|
|
|
|
int numEventsDropped = count - remaningCacheSize;
|
|
|
|
countFlushCompleteEventsLocked(mEventCache, numEventsDropped);
|
|
|
|
// Drop the first "numEventsDropped" in the cache.
|
|
|
|
memmove(mEventCache, &mEventCache[numEventsDropped],
|
|
|
|
(mCacheSize - numEventsDropped) * sizeof(sensors_event_t));
|
|
|
|
|
|
|
|
// Copy the remainingEvents in scratch buffer to the end of cache.
|
|
|
|
memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize,
|
|
|
|
numEventsDropped * sizeof(sensors_event_t));
|
|
|
|
}
|
|
|
|
return status_t(NO_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count);
|
|
|
|
if (index_wake_up_event >= 0) {
|
|
|
|
scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
|
|
|
|
++mWakeLockRefCount;
|
2014-08-21 19:28:35 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
++mTotalAcksNeeded;
|
|
|
|
#endif
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
// NOTE: ASensorEvent and sensors_event_t are the same type.
|
|
|
|
ssize_t size = SensorEventQueue::write(mChannel,
|
|
|
|
reinterpret_cast<ASensorEvent const*>(scratch), count);
|
|
|
|
if (size < 0) {
|
|
|
|
// Write error, copy events to local cache.
|
2014-08-13 19:24:50 +00:00
|
|
|
if (index_wake_up_event >= 0) {
|
|
|
|
// If there was a wake_up sensor_event, reset the flag.
|
|
|
|
scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
|
2014-09-15 00:29:48 +00:00
|
|
|
if (mWakeLockRefCount > 0) {
|
|
|
|
--mWakeLockRefCount;
|
|
|
|
}
|
2014-08-21 19:28:35 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
--mTotalAcksNeeded;
|
|
|
|
#endif
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
if (mEventCache == NULL) {
|
|
|
|
mMaxCacheSize = computeMaxCacheSizeLocked();
|
|
|
|
mEventCache = new sensors_event_t[mMaxCacheSize];
|
|
|
|
mCacheSize = 0;
|
|
|
|
}
|
|
|
|
memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t));
|
|
|
|
mCacheSize += count;
|
|
|
|
|
|
|
|
// Add this file descriptor to the looper to get a callback when this fd is available for
|
|
|
|
// writing.
|
2014-09-29 00:52:41 +00:00
|
|
|
updateLooperRegistrationLocked(mService->getLooper());
|
2014-07-10 23:01:10 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
if (size > 0) {
|
|
|
|
mEventsSent += count;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return size < 0 ? status_t(size) : status_t(NO_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch,
|
|
|
|
int count) {
|
|
|
|
sensors_event_t *eventCache_new;
|
|
|
|
const int new_cache_size = computeMaxCacheSizeLocked();
|
|
|
|
// Allocate new cache, copy over events from the old cache & scratch, free up memory.
|
|
|
|
eventCache_new = new sensors_event_t[new_cache_size];
|
|
|
|
memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t));
|
|
|
|
memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t));
|
|
|
|
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize,
|
|
|
|
new_cache_size);
|
|
|
|
|
|
|
|
delete mEventCache;
|
|
|
|
mEventCache = eventCache_new;
|
|
|
|
mCacheSize += count;
|
|
|
|
mMaxCacheSize = new_cache_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() {
|
|
|
|
ASensorEvent flushCompleteEvent;
|
2014-09-15 00:29:48 +00:00
|
|
|
memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent));
|
2014-08-13 19:24:50 +00:00
|
|
|
flushCompleteEvent.type = SENSOR_TYPE_META_DATA;
|
|
|
|
// Loop through all the sensors for this connection and check if there are any pending
|
|
|
|
// flush complete events to be sent.
|
|
|
|
for (size_t i = 0; i < mSensorInfo.size(); ++i) {
|
|
|
|
FlushInfo& flushInfo = mSensorInfo.editValueAt(i);
|
|
|
|
while (flushInfo.mPendingFlushEventsToSend > 0) {
|
2014-09-15 00:29:48 +00:00
|
|
|
const int sensor_handle = mSensorInfo.keyAt(i);
|
|
|
|
flushCompleteEvent.meta_data.sensor = sensor_handle;
|
2014-10-30 00:55:20 +00:00
|
|
|
bool wakeUpSensor = mService->getSensorFromHandle(sensor_handle).isWakeUpSensor();
|
|
|
|
if (wakeUpSensor) {
|
|
|
|
++mWakeLockRefCount;
|
2014-09-15 00:29:48 +00:00
|
|
|
flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1);
|
|
|
|
if (size < 0) {
|
2014-10-30 00:55:20 +00:00
|
|
|
if (wakeUpSensor) --mWakeLockRefCount;
|
2014-08-13 19:24:50 +00:00
|
|
|
return;
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ",
|
|
|
|
flushCompleteEvent.meta_data.sensor);
|
|
|
|
flushInfo.mPendingFlushEventsToSend--;
|
2013-09-06 00:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 00:55:20 +00:00
|
|
|
void SensorService::SensorEventConnection::writeToSocketFromCache() {
|
2014-08-22 23:11:10 +00:00
|
|
|
// At a time write at most half the size of the receiver buffer in SensorEventQueue OR
|
|
|
|
// half the size of the socket buffer allocated in BitTube whichever is smaller.
|
|
|
|
const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
|
|
|
|
int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
|
2014-10-30 00:55:20 +00:00
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2014-08-22 23:11:10 +00:00
|
|
|
// Send pending flush complete events (if any)
|
2014-08-13 19:24:50 +00:00
|
|
|
sendPendingFlushEventsLocked();
|
2014-07-10 23:01:10 +00:00
|
|
|
for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
|
2014-08-22 23:11:10 +00:00
|
|
|
const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize);
|
2014-08-13 19:24:50 +00:00
|
|
|
int index_wake_up_event =
|
|
|
|
findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite);
|
|
|
|
if (index_wake_up_event >= 0) {
|
|
|
|
mEventCache[index_wake_up_event + numEventsSent].flags |=
|
|
|
|
WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
|
|
|
|
++mWakeLockRefCount;
|
2014-08-21 19:28:35 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
++mTotalAcksNeeded;
|
|
|
|
#endif
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
ssize_t size = SensorEventQueue::write(mChannel,
|
|
|
|
reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent),
|
2014-08-13 19:24:50 +00:00
|
|
|
numEventsToWrite);
|
2014-07-10 23:01:10 +00:00
|
|
|
if (size < 0) {
|
2014-08-13 19:24:50 +00:00
|
|
|
if (index_wake_up_event >= 0) {
|
|
|
|
// If there was a wake_up sensor_event, reset the flag.
|
|
|
|
mEventCache[index_wake_up_event + numEventsSent].flags &=
|
|
|
|
~WAKE_UP_SENSOR_EVENT_NEEDS_ACK;
|
2014-09-15 00:29:48 +00:00
|
|
|
if (mWakeLockRefCount > 0) {
|
|
|
|
--mWakeLockRefCount;
|
|
|
|
}
|
2014-08-21 19:28:35 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
--mTotalAcksNeeded;
|
|
|
|
#endif
|
2014-08-13 19:24:50 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
memmove(mEventCache, &mEventCache[numEventsSent],
|
|
|
|
(mCacheSize - numEventsSent) * sizeof(sensors_event_t));
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ",
|
2014-08-13 19:24:50 +00:00
|
|
|
numEventsSent, mCacheSize);
|
2014-07-10 23:01:10 +00:00
|
|
|
mCacheSize -= numEventsSent;
|
|
|
|
return;
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
numEventsSent += numEventsToWrite;
|
2014-07-10 23:01:10 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
2014-08-13 19:24:50 +00:00
|
|
|
mEventsSentFromCache += numEventsToWrite;
|
2014-07-10 23:01:10 +00:00
|
|
|
#endif
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize);
|
|
|
|
// All events from the cache have been sent. Reset cache size to zero.
|
|
|
|
mCacheSize = 0;
|
2014-09-29 00:52:41 +00:00
|
|
|
// There are no more events in the cache. We don't need to poll for write on the fd.
|
|
|
|
// Update Looper registration.
|
|
|
|
updateLooperRegistrationLocked(mService->getLooper());
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2013-10-15 00:04:42 +00:00
|
|
|
void SensorService::SensorEventConnection::countFlushCompleteEventsLocked(
|
2014-09-15 00:29:48 +00:00
|
|
|
sensors_event_t const* scratch, const int numEventsDropped) {
|
2013-09-06 00:03:38 +00:00
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped);
|
|
|
|
// Count flushComplete events in the events that are about to the dropped. These will be sent
|
|
|
|
// separately before the next batch of events.
|
|
|
|
for (int j = 0; j < numEventsDropped; ++j) {
|
|
|
|
if (scratch[j].type == SENSOR_TYPE_META_DATA) {
|
|
|
|
FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor);
|
|
|
|
flushInfo.mPendingFlushEventsToSend++;
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
|
|
|
|
flushInfo.mPendingFlushEventsToSend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:24:50 +00:00
|
|
|
int SensorService::SensorEventConnection::findWakeUpSensorEventLocked(
|
|
|
|
sensors_event_t const* scratch, const int count) {
|
2014-02-12 02:58:52 +00:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (mService->isWakeUpSensorEvent(scratch[i])) {
|
2014-08-13 19:24:50 +00:00
|
|
|
return i;
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-13 19:24:50 +00:00
|
|
|
return -1;
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
|
2011-10-21 01:42:02 +00:00
|
|
|
sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
|
|
|
return mChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorService::SensorEventConnection::enableDisable(
|
2013-06-27 19:04:23 +00:00
|
|
|
int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
|
|
|
|
int reservedFlags)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
|
|
|
status_t err;
|
|
|
|
if (enabled) {
|
2013-06-27 19:04:23 +00:00
|
|
|
err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
|
2015-04-29 23:50:41 +00:00
|
|
|
reservedFlags, mOpPackageName);
|
2014-07-10 23:01:10 +00:00
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
} else {
|
|
|
|
err = mService->disable(this, handle);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SensorService::SensorEventConnection::setEventRate(
|
2013-06-27 19:04:23 +00:00
|
|
|
int handle, nsecs_t samplingPeriodNs)
|
2010-07-15 06:41:37 +00:00
|
|
|
{
|
2015-04-29 23:50:41 +00:00
|
|
|
return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
|
2010-07-15 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 21:59:26 +00:00
|
|
|
status_t SensorService::SensorEventConnection::flush() {
|
2015-04-29 23:50:41 +00:00
|
|
|
return mService->flushSensor(this, mOpPackageName);
|
2013-06-27 19:04:23 +00:00
|
|
|
}
|
2013-09-06 00:03:38 +00:00
|
|
|
|
2014-09-29 00:52:41 +00:00
|
|
|
int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) {
|
2014-07-10 23:01:10 +00:00
|
|
|
if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) {
|
2014-09-29 00:52:41 +00:00
|
|
|
{
|
|
|
|
// If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount,
|
|
|
|
// and remove the fd from Looper. Call checkWakeLockState to know if SensorService
|
|
|
|
// can release the wake-lock.
|
|
|
|
ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
|
|
|
mDead = true;
|
|
|
|
mWakeLockRefCount = 0;
|
|
|
|
updateLooperRegistrationLocked(mService->getLooper());
|
|
|
|
}
|
|
|
|
mService->checkWakeLockState();
|
2015-04-17 01:57:31 +00:00
|
|
|
if (mDataInjectionMode) {
|
|
|
|
// If the Looper has encountered some error in data injection mode, reset SensorService
|
|
|
|
// back to normal mode.
|
|
|
|
mService->resetToNormalMode();
|
|
|
|
mDataInjectionMode = false;
|
|
|
|
}
|
2014-09-29 00:52:41 +00:00
|
|
|
return 1;
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
|
|
|
|
if (events & ALOOPER_EVENT_INPUT) {
|
2015-04-17 01:57:31 +00:00
|
|
|
unsigned char buf[sizeof(sensors_event_t)];
|
|
|
|
ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
|
2014-07-10 23:01:10 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mConnectionLock);
|
2015-04-17 01:57:31 +00:00
|
|
|
if (numBytesRead == sizeof(sensors_event_t)) {
|
|
|
|
if (!mDataInjectionMode) {
|
|
|
|
ALOGE("Data injected in normal mode, dropping event"
|
|
|
|
"package=%s uid=%d", mPackageName.string(), mUid);
|
|
|
|
// Unregister call backs.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SensorDevice& dev(SensorDevice::getInstance());
|
|
|
|
sensors_event_t sensor_event;
|
|
|
|
memset(&sensor_event, 0, sizeof(sensor_event));
|
|
|
|
memcpy(&sensor_event, buf, sizeof(sensors_event_t));
|
|
|
|
Sensor sensor = mService->getSensorFromHandle(sensor_event.sensor);
|
|
|
|
sensor_event.type = sensor.getType();
|
2015-04-29 23:50:41 +00:00
|
|
|
dev.injectSensorData(&sensor_event);
|
2014-08-21 19:28:35 +00:00
|
|
|
#if DEBUG_CONNECTIONS
|
2015-04-17 01:57:31 +00:00
|
|
|
++mEventsReceived;
|
2014-08-21 19:28:35 +00:00
|
|
|
#endif
|
2015-04-17 01:57:31 +00:00
|
|
|
} else if (numBytesRead == sizeof(uint32_t)) {
|
|
|
|
uint32_t numAcks = 0;
|
2015-05-08 22:59:23 +00:00
|
|
|
memcpy(&numAcks, buf, numBytesRead);
|
2015-04-17 01:57:31 +00:00
|
|
|
// Sanity check to ensure there are no read errors in recv, numAcks is always
|
|
|
|
// within the range and not zero. If any of the above don't hold reset
|
|
|
|
// mWakeLockRefCount to zero.
|
|
|
|
if (numAcks > 0 && numAcks < mWakeLockRefCount) {
|
|
|
|
mWakeLockRefCount -= numAcks;
|
|
|
|
} else {
|
|
|
|
mWakeLockRefCount = 0;
|
|
|
|
}
|
|
|
|
#if DEBUG_CONNECTIONS
|
|
|
|
mTotalAcksReceived += numAcks;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
// Read error, reset wakelock refcount.
|
|
|
|
mWakeLockRefCount = 0;
|
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
}
|
|
|
|
// Check if wakelock can be released by sensorservice. mConnectionLock needs to be released
|
|
|
|
// here as checkWakeLockState() will need it.
|
|
|
|
if (mWakeLockRefCount == 0) {
|
|
|
|
mService->checkWakeLockState();
|
|
|
|
}
|
|
|
|
// continue getting callbacks.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (events & ALOOPER_EVENT_OUTPUT) {
|
2014-10-30 00:55:20 +00:00
|
|
|
// send sensor data that is stored in mEventCache for this connection.
|
|
|
|
mService->sendEventsFromCache(this);
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
2014-07-10 23:01:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const {
|
2015-04-29 23:50:41 +00:00
|
|
|
size_t fifoWakeUpSensors = 0;
|
|
|
|
size_t fifoNonWakeUpSensors = 0;
|
2014-07-10 23:01:10 +00:00
|
|
|
for (size_t i = 0; i < mSensorInfo.size(); ++i) {
|
|
|
|
const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i));
|
|
|
|
if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) {
|
|
|
|
// Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and
|
|
|
|
// non wake_up sensors.
|
|
|
|
if (sensor.isWakeUpSensor()) {
|
|
|
|
fifoWakeUpSensors += sensor.getFifoReservedEventCount();
|
|
|
|
} else {
|
|
|
|
fifoNonWakeUpSensors += sensor.getFifoReservedEventCount();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors.
|
|
|
|
if (sensor.isWakeUpSensor()) {
|
|
|
|
fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ?
|
|
|
|
fifoWakeUpSensors : sensor.getFifoMaxEventCount();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ?
|
|
|
|
fifoNonWakeUpSensors : sensor.getFifoMaxEventCount();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) {
|
|
|
|
// It is extremely unlikely that there is a write failure in non batch mode. Return a cache
|
2014-08-13 19:24:50 +00:00
|
|
|
// size that is equal to that of the batch mode.
|
2014-08-21 19:28:35 +00:00
|
|
|
// ALOGW("Write failure in non-batch mode");
|
2014-08-13 19:24:50 +00:00
|
|
|
return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t);
|
2014-07-10 23:01:10 +00:00
|
|
|
}
|
|
|
|
return fifoWakeUpSensors + fifoNonWakeUpSensors;
|
2014-02-12 02:58:52 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 06:41:37 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
|