Enable sensor data injection mode through adb.

Change-Id: I415cf8ff0871fa74babaf9b879c68f210298b472
This commit is contained in:
Aravind Akella 2015-06-29 12:37:48 -07:00
parent 3643c88f7b
commit 841a5926fc
7 changed files with 80 additions and 67 deletions

View File

@ -40,7 +40,7 @@ public:
virtual Vector<Sensor> getSensorList(const String16& opPackageName) = 0; virtual Vector<Sensor> getSensorList(const String16& opPackageName) = 0;
virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName, virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
int mode, const String16& opPackageName) = 0; int mode, const String16& opPackageName) = 0;
virtual status_t enableDataInjection(int enable) = 0; virtual int32_t isDataInjectionEnabled() = 0;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -107,7 +107,7 @@ public:
ssize_t getSensorList(Sensor const* const** list) const; ssize_t getSensorList(Sensor const* const** list) const;
Sensor const* getDefaultSensor(int type); Sensor const* getDefaultSensor(int type);
sp<SensorEventQueue> createEventQueue(String8 packageName = String8(""), int mode = 0); sp<SensorEventQueue> createEventQueue(String8 packageName = String8(""), int mode = 0);
status_t enableDataInjection(bool enable); bool isDataInjectionEnabled();
private: private:
// DeathRecipient interface // DeathRecipient interface

View File

@ -77,10 +77,9 @@ public:
return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
} }
virtual status_t enableDataInjection(int enable) { virtual int isDataInjectionEnabled() {
Parcel data, reply; Parcel data, reply;
data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
data.writeInt32(enable);
remote()->transact(ENABLE_DATA_INJECTION, data, &reply); remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
return reply.readInt32(); return reply.readInt32();
} }
@ -121,8 +120,7 @@ status_t BnSensorServer::onTransact(
} }
case ENABLE_DATA_INJECTION: { case ENABLE_DATA_INJECTION: {
CHECK_INTERFACE(ISensorServer, data, reply); CHECK_INTERFACE(ISensorServer, data, reply);
int32_t enable = data.readInt32(); int32_t ret = isDataInjectionEnabled();
status_t ret = enableDataInjection(enable);
reply->writeInt32(static_cast<int32_t>(ret)); reply->writeInt32(static_cast<int32_t>(ret));
return NO_ERROR; return NO_ERROR;
} }

View File

@ -20,6 +20,7 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <linux/errno.h>
#include <utils/Errors.h> #include <utils/Errors.h>
#include <utils/RefBase.h> #include <utils/RefBase.h>
@ -150,13 +151,20 @@ status_t SensorEventQueue::setEventRate(Sensor const* sensor, nsecs_t ns) const
} }
status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) { status_t SensorEventQueue::injectSensorEvent(const ASensorEvent& event) {
do {
// Blocking call. // Blocking call.
ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL); ssize_t size = ::send(mSensorChannel->getFd(), &event, sizeof(event), MSG_NOSIGNAL);
if (size < 0) { if (size >= 0) {
ALOGE("injectSensorEvent failure %zd %d", size, mSensorChannel->getFd()); return NO_ERROR;
} else if (size < 0 && errno == EAGAIN) {
// If send is returning a "Try again" error, sleep for 100ms and try again. In all
// other cases log a failure and exit.
usleep(100000);
} else {
ALOGE("injectSensorEvent failure %s %zd", strerror(errno), size);
return INVALID_OPERATION; return INVALID_OPERATION;
} }
return NO_ERROR; } while (true);
} }
void SensorEventQueue::sendAck(const ASensorEvent* events, int count) { void SensorEventQueue::sendAck(const ASensorEvent* events, int count) {

View File

@ -153,12 +153,12 @@ sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mo
return queue; return queue;
} }
status_t SensorManager::enableDataInjection(bool enable) { bool SensorManager::isDataInjectionEnabled() {
Mutex::Autolock _l(mLock); Mutex::Autolock _l(mLock);
if (assertStateLocked() == NO_ERROR) { if (assertStateLocked() == NO_ERROR) {
return mSensorServer->enableDataInjection(enable); return mSensorServer->isDataInjectionEnabled();
} }
return INVALID_OPERATION; return false;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -66,7 +66,6 @@ namespace android {
const char* SensorService::WAKE_LOCK_NAME = "SensorService"; const char* SensorService::WAKE_LOCK_NAME = "SensorService";
// Permissions. // Permissions.
static const String16 sDataInjectionPermission("android.permission.LOCATION_HARDWARE");
static const String16 sDump("android.permission.DUMP"); static const String16 sDump("android.permission.DUMP");
SensorService::SensorService() SensorService::SensorService()
@ -246,12 +245,12 @@ status_t SensorService::dump(int fd, const Vector<String16>& args)
IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingPid(),
IPCThreadState::self()->getCallingUid()); IPCThreadState::self()->getCallingUid());
} else { } else {
if (args.size() > 1) { if (args.size() > 2) {
return INVALID_OPERATION; return INVALID_OPERATION;
} }
Mutex::Autolock _l(mLock); Mutex::Autolock _l(mLock);
SensorDevice& dev(SensorDevice::getInstance()); SensorDevice& dev(SensorDevice::getInstance());
if (args.size() == 1 && args[0] == String16("restrict")) { if (args.size() == 2 && args[0] == String16("restrict")) {
// If already in restricted mode. Ignore. // If already in restricted mode. Ignore.
if (mCurrentOperatingMode == RESTRICTED) { if (mCurrentOperatingMode == RESTRICTED) {
return status_t(NO_ERROR); return status_t(NO_ERROR);
@ -268,6 +267,7 @@ status_t SensorService::dump(int fd, const Vector<String16>& args)
for (size_t i=0 ; i< mActiveSensors.size(); ++i) { for (size_t i=0 ; i< mActiveSensors.size(); ++i) {
mActiveSensors.valueAt(i)->clearAllPendingFlushConnections(); mActiveSensors.valueAt(i)->clearAllPendingFlushConnections();
} }
mWhiteListedPackage.setTo(String8(args[1]));
return status_t(NO_ERROR); return status_t(NO_ERROR);
} else if (args.size() == 1 && args[0] == String16("enable")) { } else if (args.size() == 1 && args[0] == String16("enable")) {
// If currently in restricted mode, reset back to NORMAL mode else ignore. // If currently in restricted mode, reset back to NORMAL mode else ignore.
@ -275,7 +275,30 @@ status_t SensorService::dump(int fd, const Vector<String16>& args)
mCurrentOperatingMode = NORMAL; mCurrentOperatingMode = NORMAL;
dev.enableAllSensors(); dev.enableAllSensors();
} }
if (mCurrentOperatingMode == DATA_INJECTION) {
resetToNormalModeLocked();
}
mWhiteListedPackage.clear();
return status_t(NO_ERROR); return status_t(NO_ERROR);
} 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;
}
} else if (mSensorList.size() == 0) { } else if (mSensorList.size() == 0) {
result.append("No Sensors on the device\n"); result.append("No Sensors on the device\n");
} else { } else {
@ -362,10 +385,10 @@ status_t SensorService::dump(int fd, const Vector<String16>& args)
result.appendFormat(" NORMAL\n"); result.appendFormat(" NORMAL\n");
break; break;
case RESTRICTED: case RESTRICTED:
result.appendFormat(" RESTRICTED\n"); result.appendFormat(" RESTRICTED : %s\n", mWhiteListedPackage.string());
break; break;
case DATA_INJECTION: case DATA_INJECTION:
result.appendFormat(" DATA_INJECTION\n"); result.appendFormat(" DATA_INJECTION : %s\n", mWhiteListedPackage.string());
} }
result.appendFormat("%zd active connections\n", mActiveConnections.size()); result.appendFormat("%zd active connections\n", mActiveConnections.size());
@ -712,12 +735,15 @@ sp<ISensorEventConnection> SensorService::createSensorEventConnection(const Stri
if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) {
return NULL; return NULL;
} }
// DATA_INJECTION mode needs to have the required permissions set.
if (requestedMode == DATA_INJECTION && !hasDataInjectionPermissions()) {
return NULL;
}
Mutex::Autolock _l(mLock); Mutex::Autolock _l(mLock);
// 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;
}
uid_t uid = IPCThreadState::self()->getCallingUid(); uid_t uid = IPCThreadState::self()->getCallingUid();
sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName, sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName,
requestedMode == DATA_INJECTION, opPackageName)); requestedMode == DATA_INJECTION, opPackageName));
@ -732,35 +758,9 @@ sp<ISensorEventConnection> SensorService::createSensorEventConnection(const Stri
return result; return result;
} }
status_t SensorService::enableDataInjection(int requestedMode) { int SensorService::isDataInjectionEnabled() {
if (!hasDataInjectionPermissions()) {
return INVALID_OPERATION;
}
Mutex::Autolock _l(mLock); Mutex::Autolock _l(mLock);
ALOGD_IF(DEBUG_CONNECTIONS, "SensorService::enableDataInjection %d", requestedMode); return (mCurrentOperatingMode == DATA_INJECTION);
SensorDevice& dev(SensorDevice::getInstance());
status_t err(NO_ERROR);
if (requestedMode == DATA_INJECTION) {
if (mCurrentOperatingMode == NORMAL) {
dev.disableAllSensors();
err = dev.setMode(requestedMode);
if (err == NO_ERROR) {
mCurrentOperatingMode = DATA_INJECTION;
} else {
// Re-enable sensors.
dev.enableAllSensors();
}
} 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;
}
} else if (requestedMode == NORMAL && mCurrentOperatingMode != NORMAL) {
err = resetToNormalModeLocked();
}
return err;
} }
status_t SensorService::resetToNormalMode() { status_t SensorService::resetToNormalMode() {
@ -838,7 +838,8 @@ status_t SensorService::enable(const sp<SensorEventConnection>& connection,
} }
Mutex::Autolock _l(mLock); Mutex::Autolock _l(mLock);
if (mCurrentOperatingMode == RESTRICTED && !isWhiteListedPackage(connection->getPackageName())) { if ((mCurrentOperatingMode == RESTRICTED || mCurrentOperatingMode == DATA_INJECTION)
&& !isWhiteListedPackage(connection->getPackageName())) {
return INVALID_OPERATION; return INVALID_OPERATION;
} }
@ -1106,15 +1107,6 @@ bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,
return true; return true;
} }
bool SensorService::hasDataInjectionPermissions() {
if (!PermissionCache::checkCallingPermission(sDataInjectionPermission)) {
ALOGE("Permission Denial trying to activate data injection without"
" the required permission");
return false;
}
return true;
}
void SensorService::checkWakeLockState() { void SensorService::checkWakeLockState() {
Mutex::Autolock _l(mLock); Mutex::Autolock _l(mLock);
checkWakeLockStateLocked(); checkWakeLockStateLocked();
@ -1159,8 +1151,7 @@ void SensorService::populateActiveConnections(
} }
bool SensorService::isWhiteListedPackage(const String8& packageName) { bool SensorService::isWhiteListedPackage(const String8& packageName) {
// TODO: Come up with a list of packages. return (packageName.contains(mWhiteListedPackage.string()));
return (packageName.find(".cts.") != -1);
} }
int SensorService::getNumEventsForSensorType(int sensor_event_type) { int SensorService::getNumEventsForSensorType(int sensor_event_type) {

View File

@ -100,6 +100,18 @@ class SensorService :
// State Transitions supported. // State Transitions supported.
// RESTRICTED <--- NORMAL ---> DATA_INJECTION // RESTRICTED <--- NORMAL ---> DATA_INJECTION
// ---> <--- // ---> <---
// Shell commands to switch modes in SensorService.
// 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in
// restricted mode it is treated as a NO_OP (and packageName is NOT changed).
// $ adb shell dumpsys sensorservice restrict .cts.
//
// 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in
// data_injection mode it is treated as a NO_OP (and packageName is NOT changed).
// $ adb shell dumpsys sensorservice data_injection .xts.
//
// 3) Reset sensorservice back to NORMAL mode.
// $ adb shell dumpsys sensorservice enable
}; };
static const char* WAKE_LOCK_NAME; static const char* WAKE_LOCK_NAME;
@ -117,7 +129,7 @@ class SensorService :
virtual Vector<Sensor> getSensorList(const String16& opPackageName); virtual Vector<Sensor> getSensorList(const String16& opPackageName);
virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName, virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
int requestedMode, const String16& opPackageName); int requestedMode, const String16& opPackageName);
virtual status_t enableDataInjection(int enable); virtual int isDataInjectionEnabled();
virtual status_t dump(int fd, const Vector<String16>& args); virtual status_t dump(int fd, const Vector<String16>& args);
class SensorEventConnection : public BnSensorEventConnection, public LooperCallback { class SensorEventConnection : public BnSensorEventConnection, public LooperCallback {
@ -334,7 +346,6 @@ class SensorService :
sensors_event_t const* buffer, const int count); sensors_event_t const* buffer, const int count);
static bool canAccessSensor(const Sensor& sensor, const char* operation, static bool canAccessSensor(const Sensor& sensor, const char* operation,
const String16& opPackageName); const String16& opPackageName);
static bool hasDataInjectionPermissions();
// SensorService acquires a partial wakelock for delivering events from wake up sensors. This // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
// method checks whether all the events from these wake up sensors have been delivered to the // method checks whether all the events from these wake up sensors have been delivered to the
// corresponding applications, if yes the wakelock is released. // corresponding applications, if yes the wakelock is released.
@ -394,6 +405,11 @@ class SensorService :
sensors_event_t *mSensorEventBuffer, *mSensorEventScratch; sensors_event_t *mSensorEventBuffer, *mSensorEventScratch;
SensorEventConnection const **mMapFlushEventsToConnections; SensorEventConnection const **mMapFlushEventsToConnections;
Mode mCurrentOperatingMode; Mode mCurrentOperatingMode;
// This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only
// applications with this packageName are allowed to activate/deactivate or call flush on
// sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to
// sensors.
String8 mWhiteListedPackage;
// The size of this vector is constant, only the items are mutable // The size of this vector is constant, only the items are mutable
KeyedVector<int32_t, CircularBuffer *> mLastEventSeen; KeyedVector<int32_t, CircularBuffer *> mLastEventSeen;