Data Injection Support for Default Sensors HAL 2.0
Implements data injection support for the default Sensors HAL 2.0. Allows events to be injected into the Sensors HAL and these events be written back to the Event FMQ. Bug: 111070257 Test: Passes data injection VTS tests Change-Id: If652417ec0df4148ffb5be3d5dd8bc6f0be65467
This commit is contained in:
parent
9c1867eec8
commit
d23f200603
3 changed files with 63 additions and 12 deletions
|
@ -29,14 +29,20 @@ using ::android::hardware::sensors::V1_0::SensorFlagBits;
|
|||
using ::android::hardware::sensors::V1_0::SensorStatus;
|
||||
|
||||
Sensor::Sensor(ISensorsEventCallback* callback)
|
||||
: mIsEnabled(false), mSamplingPeriodNs(0), mLastSampleTimeNs(0), mCallback(callback) {
|
||||
: mIsEnabled(false),
|
||||
mSamplingPeriodNs(0),
|
||||
mLastSampleTimeNs(0),
|
||||
mCallback(callback),
|
||||
mMode(OperationMode::NORMAL) {
|
||||
mRunThread = std::thread(startThread, this);
|
||||
}
|
||||
|
||||
Sensor::~Sensor() {
|
||||
std::unique_lock<std::mutex> lock(mRunMutex);
|
||||
mStopThread = true;
|
||||
mIsEnabled = false;
|
||||
mWaitCV.notify_all();
|
||||
lock.release();
|
||||
mRunThread.join();
|
||||
}
|
||||
|
||||
|
@ -60,6 +66,7 @@ void Sensor::batch(int32_t samplingPeriodNs) {
|
|||
|
||||
void Sensor::activate(bool enable) {
|
||||
if (mIsEnabled != enable) {
|
||||
std::unique_lock<std::mutex> lock(mRunMutex);
|
||||
mIsEnabled = enable;
|
||||
mWaitCV.notify_all();
|
||||
}
|
||||
|
@ -89,13 +96,14 @@ void Sensor::startThread(Sensor* sensor) {
|
|||
}
|
||||
|
||||
void Sensor::run() {
|
||||
std::mutex runMutex;
|
||||
std::unique_lock<std::mutex> runLock(runMutex);
|
||||
std::unique_lock<std::mutex> runLock(mRunMutex);
|
||||
constexpr int64_t kNanosecondsInSeconds = 1000 * 1000 * 1000;
|
||||
|
||||
while (!mStopThread) {
|
||||
if (!mIsEnabled) {
|
||||
mWaitCV.wait(runLock, [&] { return mIsEnabled || mStopThread; });
|
||||
if (!mIsEnabled || mMode == OperationMode::DATA_INJECTION) {
|
||||
mWaitCV.wait(runLock, [&] {
|
||||
return ((mIsEnabled && mMode == OperationMode::NORMAL) || mStopThread);
|
||||
});
|
||||
} else {
|
||||
timespec curTime;
|
||||
clock_gettime(CLOCK_REALTIME, &curTime);
|
||||
|
@ -127,6 +135,33 @@ std::vector<Event> Sensor::readEvents() {
|
|||
return events;
|
||||
}
|
||||
|
||||
void Sensor::setOperationMode(OperationMode mode) {
|
||||
if (mMode != mode) {
|
||||
std::unique_lock<std::mutex> lock(mRunMutex);
|
||||
mMode = mode;
|
||||
mWaitCV.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
bool Sensor::supportsDataInjection() const {
|
||||
return mSensorInfo.flags & static_cast<uint32_t>(SensorFlagBits::DATA_INJECTION);
|
||||
}
|
||||
|
||||
Result Sensor::injectEvent(const Event& event) {
|
||||
Result result = Result::OK;
|
||||
if (event.sensorType == SensorType::ADDITIONAL_INFO) {
|
||||
// When in OperationMode::NORMAL, SensorType::ADDITIONAL_INFO is used to push operation
|
||||
// environment data into the device.
|
||||
} else if (!supportsDataInjection()) {
|
||||
result = Result::INVALID_OPERATION;
|
||||
} else if (mMode == OperationMode::DATA_INJECTION) {
|
||||
mCallback->postEvents(std::vector<Event>{event});
|
||||
} else {
|
||||
result = Result::BAD_VALUE;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) {
|
||||
mSensorInfo.sensorHandle = sensorHandle;
|
||||
mSensorInfo.name = "Accel Sensor";
|
||||
|
@ -142,7 +177,8 @@ AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback)
|
|||
mSensorInfo.fifoReservedEventCount = 0;
|
||||
mSensorInfo.fifoMaxEventCount = 0;
|
||||
mSensorInfo.requiredPermission = "";
|
||||
mSensorInfo.flags = static_cast<uint32_t>(SensorFlagBits::WAKE_UP);
|
||||
mSensorInfo.flags =
|
||||
static_cast<uint32_t>(SensorFlagBits::WAKE_UP | SensorFlagBits::DATA_INJECTION);
|
||||
};
|
||||
|
||||
} // namespace implementation
|
||||
|
|
|
@ -21,10 +21,12 @@
|
|||
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using ::android::hardware::sensors::V1_0::Event;
|
||||
using ::android::hardware::sensors::V1_0::OperationMode;
|
||||
using ::android::hardware::sensors::V1_0::Result;
|
||||
using ::android::hardware::sensors::V1_0::SensorInfo;
|
||||
using ::android::hardware::sensors::V1_0::SensorType;
|
||||
|
@ -51,6 +53,10 @@ class Sensor {
|
|||
void activate(bool enable);
|
||||
Result flush();
|
||||
|
||||
void setOperationMode(OperationMode mode);
|
||||
bool supportsDataInjection() const;
|
||||
Result injectEvent(const Event& event);
|
||||
|
||||
protected:
|
||||
void run();
|
||||
virtual std::vector<Event> readEvents();
|
||||
|
@ -63,9 +69,12 @@ class Sensor {
|
|||
|
||||
std::atomic_bool mStopThread;
|
||||
std::condition_variable mWaitCV;
|
||||
std::mutex mRunMutex;
|
||||
std::thread mRunThread;
|
||||
|
||||
ISensorsEventCallback* mCallback;
|
||||
|
||||
OperationMode mMode;
|
||||
};
|
||||
|
||||
class AccelSensor : public Sensor {
|
||||
|
|
|
@ -54,9 +54,11 @@ Return<void> Sensors::getSensorsList(getSensorsList_cb _hidl_cb) {
|
|||
return Void();
|
||||
}
|
||||
|
||||
Return<Result> Sensors::setOperationMode(OperationMode /* mode */) {
|
||||
// TODO implement
|
||||
return Result{};
|
||||
Return<Result> Sensors::setOperationMode(OperationMode mode) {
|
||||
for (auto sensor : mSensors) {
|
||||
sensor.second->setOperationMode(mode);
|
||||
}
|
||||
return Result::OK;
|
||||
}
|
||||
|
||||
Return<Result> Sensors::activate(int32_t sensorHandle, bool enabled) {
|
||||
|
@ -120,9 +122,13 @@ Return<Result> Sensors::flush(int32_t sensorHandle) {
|
|||
return Result::BAD_VALUE;
|
||||
}
|
||||
|
||||
Return<Result> Sensors::injectSensorData(const Event& /* event */) {
|
||||
// TODO implement
|
||||
return Result{};
|
||||
Return<Result> Sensors::injectSensorData(const Event& event) {
|
||||
auto sensor = mSensors.find(event.sensorHandle);
|
||||
if (sensor != mSensors.end()) {
|
||||
return sensor->second->injectEvent(event);
|
||||
}
|
||||
|
||||
return Result::BAD_VALUE;
|
||||
}
|
||||
|
||||
Return<void> Sensors::registerDirectChannel(const SharedMemInfo& /* mem */,
|
||||
|
|
Loading…
Reference in a new issue