From 897528dd53b19da07e35959102defc7e5e1e546c Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Tue, 23 Oct 2018 10:38:03 -0700 Subject: Implement activate and batch functions Implements the activate and batch functions for the default Sensors 2.0 implementation. Bug: 111070257 Test: Builds Change-Id: I5987ab722cdd97c7cd7ff466d6d989794171b851 --- sensors/2.0/default/Sensor.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 sensors/2.0/default/Sensor.cpp (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp new file mode 100644 index 0000000000..f4c9b57520 --- /dev/null +++ b/sensors/2.0/default/Sensor.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2018 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. + */ + +#include "Sensor.h" + +namespace android { +namespace hardware { +namespace sensors { +namespace V2_0 { +namespace implementation { + +Sensor::Sensor() : mIsEnabled(false), mSamplingPeriodNs(0) {} + +const SensorInfo& Sensor::getSensorInfo() const { + return mSensorInfo; +} + +bool Sensor::batch(int32_t samplingPeriodNs) { + bool success = true; + if (samplingPeriodNs >= mSensorInfo.minDelay * 1000 && + samplingPeriodNs <= mSensorInfo.maxDelay * 1000) { + mSamplingPeriodNs = samplingPeriodNs; + } else { + success = false; + } + return success; +} + +void Sensor::activate(bool enable) { + mIsEnabled = enable; +} + +} // namespace implementation +} // namespace V2_0 +} // namespace sensors +} // namespace hardware +} // namespace android \ No newline at end of file -- cgit v1.2.3 From 237abc6cf82883a38897433a3bdc16903b9f14eb Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Tue, 23 Oct 2018 11:09:59 -0700 Subject: Generate events for default Sensors 2.0 Adds the ability for default Sensors 2.0 implementation to generate sensor events. Bug: 111070257 Test: Builds Change-Id: I98f04dbac5370cc6fc3be43468ba43b6476b4515 --- sensors/2.0/default/Sensor.cpp | 83 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 10 deletions(-) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index f4c9b57520..1691b3e7a2 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -16,35 +16,98 @@ #include "Sensor.h" +#include + namespace android { namespace hardware { namespace sensors { namespace V2_0 { namespace implementation { -Sensor::Sensor() : mIsEnabled(false), mSamplingPeriodNs(0) {} +using ::android::hardware::sensors::V1_0::SensorStatus; + +Sensor::Sensor(ISensorsEventCallback* callback) + : mIsEnabled(false), mSamplingPeriodNs(0), mLastSampleTimeNs(0), mCallback(callback) { + mRunThread = std::thread(startThread, this); +} + +Sensor::~Sensor() { + mStopThread = true; + mIsEnabled = false; + mWaitCV.notify_all(); + mRunThread.join(); +} const SensorInfo& Sensor::getSensorInfo() const { return mSensorInfo; } -bool Sensor::batch(int32_t samplingPeriodNs) { - bool success = true; - if (samplingPeriodNs >= mSensorInfo.minDelay * 1000 && - samplingPeriodNs <= mSensorInfo.maxDelay * 1000) { +void Sensor::batch(int32_t samplingPeriodNs) { + if (samplingPeriodNs < mSensorInfo.minDelay * 1000) { + samplingPeriodNs = mSensorInfo.minDelay * 1000; + } else if (samplingPeriodNs > mSensorInfo.maxDelay * 1000) { + samplingPeriodNs = mSensorInfo.maxDelay * 1000; + } + + if (mSamplingPeriodNs != samplingPeriodNs) { mSamplingPeriodNs = samplingPeriodNs; - } else { - success = false; + // Wake up the 'run' thread to check if a new event should be generated now + mWaitCV.notify_all(); } - return success; } void Sensor::activate(bool enable) { - mIsEnabled = enable; + if (mIsEnabled != enable) { + mIsEnabled = enable; + mWaitCV.notify_all(); + } +} + +void Sensor::startThread(Sensor* sensor) { + sensor->run(); +} + +void Sensor::run() { + std::mutex runMutex; + std::unique_lock runLock(runMutex); + constexpr int64_t kNanosecondsInSeconds = 1000 * 1000 * 1000; + + while (!mStopThread) { + if (!mIsEnabled) { + mWaitCV.wait(runLock, [&] { return mIsEnabled || mStopThread; }); + } else { + timespec curTime; + clock_gettime(CLOCK_REALTIME, &curTime); + int64_t now = (curTime.tv_sec * kNanosecondsInSeconds) + curTime.tv_nsec; + int64_t nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs; + + if (now >= nextSampleTime) { + mLastSampleTimeNs = now; + nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs; + mCallback->postEvents(readEvents()); + } + + mWaitCV.wait_for(runLock, std::chrono::nanoseconds(nextSampleTime - now)); + } + } +} + +std::vector Sensor::readEvents() { + std::vector events; + Event event; + event.sensorHandle = mSensorInfo.sensorHandle; + event.sensorType = mSensorInfo.type; + event.timestamp = ::android::elapsedRealtimeNano(); + event.u.vec3.x = 1; + event.u.vec3.y = 2; + event.u.vec3.z = 3; + event.u.vec3.status = SensorStatus::ACCURACY_HIGH; + events.push_back(event); + return events; } } // namespace implementation } // namespace V2_0 } // namespace sensors } // namespace hardware -} // namespace android \ No newline at end of file +} // namespace android -- cgit v1.2.3 From 2927ab7ed7deb4560f0686e29405d87e68940499 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Tue, 23 Oct 2018 11:22:55 -0700 Subject: Register default accelerometer sensor Registers a default accelerometer with the Sensors HAL. Bug: 111070257 Test: Builds, a_sns_test receives accel events Change-Id: I2f016eb74b9b729854e771264f4c2f1adf025c09 --- sensors/2.0/default/Sensor.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index 1691b3e7a2..21c1591d01 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -24,6 +24,7 @@ namespace sensors { namespace V2_0 { namespace implementation { +using ::android::hardware::sensors::V1_0::SensorFlagBits; using ::android::hardware::sensors::V1_0::SensorStatus; Sensor::Sensor(ISensorsEventCallback* callback) @@ -106,6 +107,24 @@ std::vector Sensor::readEvents() { return events; } +AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Accel Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::ACCELEROMETER; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 78.4f; // +/- 8g + mSensorInfo.resolution = 1.52e-5; + mSensorInfo.power = 0.001f; // mA + mSensorInfo.minDelay = 20 * 1000; // microseconds + mSensorInfo.maxDelay = 1000 * 1000; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = static_cast(SensorFlagBits::WAKE_UP); +}; + } // namespace implementation } // namespace V2_0 } // namespace sensors -- cgit v1.2.3 From e4f74c77b105b09e0c5aa10a3a31491636cfed38 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Mon, 29 Oct 2018 11:51:46 -0700 Subject: Default flush implementation for Sensors 2.0 Bug: 111070257 Test: Builds, passes corresponding VTS tests Change-Id: I5b46232ffb0dd2c85bccd9dad34dd04d29a5677d --- sensors/2.0/default/Sensor.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index 21c1591d01..d3e3f7e15a 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -24,6 +24,7 @@ namespace sensors { namespace V2_0 { namespace implementation { +using ::android::hardware::sensors::V1_0::MetaDataEventType; using ::android::hardware::sensors::V1_0::SensorFlagBits; using ::android::hardware::sensors::V1_0::SensorStatus; @@ -64,6 +65,25 @@ void Sensor::activate(bool enable) { } } +Result Sensor::flush() { + // Only generate a flush complete event if the sensor is enabled and if the sensor is not a + // one-shot sensor. + if (!mIsEnabled || (mSensorInfo.flags & static_cast(SensorFlagBits::ONE_SHOT_MODE))) { + return Result::BAD_VALUE; + } + + // Note: If a sensor supports batching, write all of the currently batched events for the sensor + // to the Event FMQ prior to writing the flush complete event. + Event ev; + ev.sensorHandle = mSensorInfo.sensorHandle; + ev.sensorType = SensorType::ADDITIONAL_INFO; + ev.u.meta.what = MetaDataEventType::META_DATA_FLUSH_COMPLETE; + std::vector evs{ev}; + mCallback->postEvents(evs); + + return Result::OK; +} + void Sensor::startThread(Sensor* sensor) { sensor->run(); } -- cgit v1.2.3 From d23f2006031a033a8edf4f07a73724340d40294a Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Mon, 5 Nov 2018 13:48:16 -0800 Subject: 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 --- sensors/2.0/default/Sensor.cpp | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index d3e3f7e15a..17337e21b0 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -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 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 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 runLock(runMutex); + std::unique_lock 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 Sensor::readEvents() { return events; } +void Sensor::setOperationMode(OperationMode mode) { + if (mMode != mode) { + std::unique_lock lock(mRunMutex); + mMode = mode; + mWaitCV.notify_all(); + } +} + +bool Sensor::supportsDataInjection() const { + return mSensorInfo.flags & static_cast(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}); + } 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(SensorFlagBits::WAKE_UP); + mSensorInfo.flags = + static_cast(SensorFlagBits::WAKE_UP | SensorFlagBits::DATA_INJECTION); }; } // namespace implementation -- cgit v1.2.3 From f2aca3b4873432a3ace1621a11c77a00f6090dac Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Mon, 5 Nov 2018 09:37:15 -0800 Subject: Acquire and Release Wake Lock Acquire a wake lock in the Sensors 2.0 Default implementation whenever there are outstanding WAKE_UP events. Release the wake lock whenever the number of oustanding WAKE_UP events is zero or at least SensorTimeout::WAKE_LOCK_SECONDS seconds have elapsed since the previous WAKE_UP event was written to the Event FMQ. Bug: 111070257 Test: Builds, wake lock is acquired and released as expected. Change-Id: I7c57724430144fd4022646d1fef1b1fa8bc4235d --- sensors/2.0/default/Sensor.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index 17337e21b0..168b40219c 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -86,7 +86,7 @@ Result Sensor::flush() { ev.sensorType = SensorType::ADDITIONAL_INFO; ev.u.meta.what = MetaDataEventType::META_DATA_FLUSH_COMPLETE; std::vector evs{ev}; - mCallback->postEvents(evs); + mCallback->postEvents(evs, isWakeUpSensor()); return Result::OK; } @@ -113,7 +113,7 @@ void Sensor::run() { if (now >= nextSampleTime) { mLastSampleTimeNs = now; nextSampleTime = mLastSampleTimeNs + mSamplingPeriodNs; - mCallback->postEvents(readEvents()); + mCallback->postEvents(readEvents(), isWakeUpSensor()); } mWaitCV.wait_for(runLock, std::chrono::nanoseconds(nextSampleTime - now)); @@ -121,6 +121,10 @@ void Sensor::run() { } } +bool Sensor::isWakeUpSensor() { + return mSensorInfo.flags & static_cast(SensorFlagBits::WAKE_UP); +} + std::vector Sensor::readEvents() { std::vector events; Event event; @@ -155,7 +159,7 @@ Result Sensor::injectEvent(const Event& event) { } else if (!supportsDataInjection()) { result = Result::INVALID_OPERATION; } else if (mMode == OperationMode::DATA_INJECTION) { - mCallback->postEvents(std::vector{event}); + mCallback->postEvents(std::vector{event}, isWakeUpSensor()); } else { result = Result::BAD_VALUE; } -- cgit v1.2.3 From a2d16bbc44adca41e4db2047b77e11b3e619cc87 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Fri, 25 Jan 2019 11:22:04 -0800 Subject: Add missing sensors to Sensors 2.0 Bug: 123380869 Test: atest com.android.devicehealthchecks.SensorsBootCheck#checkSensors passes on Cuttlefish Change-Id: I08a8406b3932469878da981add61f60b7774a377 --- sensors/2.0/default/Sensor.cpp | 185 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 181 insertions(+), 4 deletions(-) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index 168b40219c..373ab1280d 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -131,9 +131,9 @@ std::vector Sensor::readEvents() { event.sensorHandle = mSensorInfo.sensorHandle; event.sensorType = mSensorInfo.type; event.timestamp = ::android::elapsedRealtimeNano(); - event.u.vec3.x = 1; - event.u.vec3.y = 2; - event.u.vec3.z = 3; + event.u.vec3.x = 0; + event.u.vec3.y = 0; + event.u.vec3.z = 0; event.u.vec3.status = SensorStatus::ACCURACY_HIGH; events.push_back(event); return events; @@ -166,6 +166,31 @@ Result Sensor::injectEvent(const Event& event) { return result; } +OnChangeSensor::OnChangeSensor(ISensorsEventCallback* callback) + : Sensor(callback), mPreviousEventSet(false) {} + +void OnChangeSensor::activate(bool enable) { + Sensor::activate(enable); + if (!enable) { + mPreviousEventSet = false; + } +} + +std::vector OnChangeSensor::readEvents() { + std::vector events = Sensor::readEvents(); + std::vector outputEvents; + + for (auto iter = events.begin(); iter != events.end(); ++iter) { + Event ev = *iter; + if (ev.u.vec3 != mPreviousEvent.u.vec3 || !mPreviousEventSet) { + outputEvents.push_back(ev); + mPreviousEvent = ev; + mPreviousEventSet = true; + } + } + return outputEvents; +} + AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) { mSensorInfo.sensorHandle = sensorHandle; mSensorInfo.name = "Accel Sensor"; @@ -181,10 +206,162 @@ AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = static_cast(SensorFlagBits::DATA_INJECTION); +}; + +PressureSensor::PressureSensor(int32_t sensorHandle, ISensorsEventCallback* callback) + : Sensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Pressure Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::PRESSURE; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 1100.0f; // hPa + mSensorInfo.resolution = 1.0f; // hPa + mSensorInfo.power = 0.001f; // mA + mSensorInfo.minDelay = 28571.0f; // microseconds + mSensorInfo.maxDelay = 0.0f; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = 0; +}; + +MagnetometerSensor::MagnetometerSensor(int32_t sensorHandle, ISensorsEventCallback* callback) + : Sensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Magnetic Field Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::MAGNETIC_FIELD; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 4911.0f; + mSensorInfo.resolution = 1.00f; + mSensorInfo.power = 0.001f; // mA + mSensorInfo.minDelay = 14284.0f; // microseconds + mSensorInfo.maxDelay = 0.0f; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = 0; +}; + +LightSensor::LightSensor(int32_t sensorHandle, ISensorsEventCallback* callback) + : OnChangeSensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Light Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::LIGHT; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 10000.0f; + mSensorInfo.resolution = 10.0f; + mSensorInfo.power = 0.001f; // mA + mSensorInfo.minDelay = 20.0f * 1000; // microseconds + mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = static_cast(SensorFlagBits::ON_CHANGE_MODE); +}; + +ProximitySensor::ProximitySensor(int32_t sensorHandle, ISensorsEventCallback* callback) + : OnChangeSensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Proximity Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::PROXIMITY; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 5.0f; + mSensorInfo.resolution = 1.0f; + mSensorInfo.power = 0.012f; // mA + mSensorInfo.minDelay = 500; // microseconds + mSensorInfo.maxDelay = 2 * mSensorInfo.minDelay; + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; mSensorInfo.flags = - static_cast(SensorFlagBits::WAKE_UP | SensorFlagBits::DATA_INJECTION); + static_cast(SensorFlagBits::ON_CHANGE_MODE | SensorFlagBits::WAKE_UP); +}; + +GyroSensor::GyroSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : Sensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Gyro Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::GYROSCOPE; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 8.726639f; + mSensorInfo.resolution = 1.0f; + mSensorInfo.power = 0.001f; + mSensorInfo.minDelay = 4444; // microseonds + mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = 0; +}; + +AmbientTempSensor::AmbientTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback) + : OnChangeSensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Ambient Temp Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::AMBIENT_TEMPERATURE; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 80.0f; + mSensorInfo.resolution = 1.0f; + mSensorInfo.power = 0.001f; + mSensorInfo.minDelay = 4444; // microseonds + mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = static_cast(SensorFlagBits::ON_CHANGE_MODE); }; +DeviceTempSensor::DeviceTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback) + : OnChangeSensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Device Temp Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::TEMPERATURE; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 80.0f; + mSensorInfo.resolution = 1.0f; + mSensorInfo.power = 0.001f; + mSensorInfo.minDelay = 4444; // microseonds + mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = static_cast(SensorFlagBits::ON_CHANGE_MODE); +} + +RelativeHumiditySensor::RelativeHumiditySensor(int32_t sensorHandle, + ISensorsEventCallback* callback) + : OnChangeSensor(callback) { + mSensorInfo.sensorHandle = sensorHandle; + mSensorInfo.name = "Relative Humidity Sensor"; + mSensorInfo.vendor = "Vendor String"; + mSensorInfo.version = 1; + mSensorInfo.type = SensorType::RELATIVE_HUMIDITY; + mSensorInfo.typeAsString = ""; + mSensorInfo.maxRange = 100.0f; + mSensorInfo.resolution = 1.0f; + mSensorInfo.power = 0.001f; + mSensorInfo.minDelay = 4444; // microseonds + mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.fifoReservedEventCount = 0; + mSensorInfo.fifoMaxEventCount = 0; + mSensorInfo.requiredPermission = ""; + mSensorInfo.flags = static_cast(SensorFlagBits::ON_CHANGE_MODE); +} + } // namespace implementation } // namespace V2_0 } // namespace sensors -- cgit v1.2.3 From 913334e0368d63ae47748cb0ab9a17296cfff995 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Tue, 29 Jan 2019 15:39:13 -0800 Subject: Properly append META_DATA event on flush Bug: 123597380 Test: atest VtsHalSensorsV2_0TargetTest:SensorsHidlTest#FlushSensor Test: Framework properly receives and handles flush complete events Change-Id: I09d2b52555c27602e1fd2ee2282f8ad700a1be18 --- sensors/2.0/default/Sensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index 373ab1280d..e8067d00ba 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -83,7 +83,7 @@ Result Sensor::flush() { // to the Event FMQ prior to writing the flush complete event. Event ev; ev.sensorHandle = mSensorInfo.sensorHandle; - ev.sensorType = SensorType::ADDITIONAL_INFO; + ev.sensorType = SensorType::META_DATA; ev.u.meta.what = MetaDataEventType::META_DATA_FLUSH_COMPLETE; std::vector evs{ev}; mCallback->postEvents(evs, isWakeUpSensor()); -- cgit v1.2.3 From e388f14db602eaebda488c7b1c052e3f6c2f0a84 Mon Sep 17 00:00:00 2001 From: Brian Stack Date: Mon, 28 Jan 2019 13:24:20 -0800 Subject: Use more realistic values for sensors Bug: 123597515 Test: Builds, cuttlefish device health checks pass Change-Id: Ifddf16761a218788ac7174e16b2d9f7ebb9685a9 --- sensors/2.0/default/Sensor.cpp | 56 ++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) (limited to 'sensors/2.0/default/Sensor.cpp') diff --git a/sensors/2.0/default/Sensor.cpp b/sensors/2.0/default/Sensor.cpp index 373ab1280d..f6dd87e8c1 100644 --- a/sensors/2.0/default/Sensor.cpp +++ b/sensors/2.0/default/Sensor.cpp @@ -18,6 +18,8 @@ #include +#include + namespace android { namespace hardware { namespace sensors { @@ -28,6 +30,8 @@ using ::android::hardware::sensors::V1_0::MetaDataEventType; using ::android::hardware::sensors::V1_0::SensorFlagBits; using ::android::hardware::sensors::V1_0::SensorStatus; +static constexpr float kDefaultMaxDelayUs = 10 * 1000 * 1000; + Sensor::Sensor(ISensorsEventCallback* callback) : mIsEnabled(false), mSamplingPeriodNs(0), @@ -202,7 +206,7 @@ AccelSensor::AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback) mSensorInfo.resolution = 1.52e-5; mSensorInfo.power = 0.001f; // mA mSensorInfo.minDelay = 20 * 1000; // microseconds - mSensorInfo.maxDelay = 1000 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -218,10 +222,10 @@ PressureSensor::PressureSensor(int32_t sensorHandle, ISensorsEventCallback* call mSensorInfo.type = SensorType::PRESSURE; mSensorInfo.typeAsString = ""; mSensorInfo.maxRange = 1100.0f; // hPa - mSensorInfo.resolution = 1.0f; // hPa + mSensorInfo.resolution = 0.005f; // hPa mSensorInfo.power = 0.001f; // mA - mSensorInfo.minDelay = 28571.0f; // microseconds - mSensorInfo.maxDelay = 0.0f; // microseconds + mSensorInfo.minDelay = 100 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -236,11 +240,11 @@ MagnetometerSensor::MagnetometerSensor(int32_t sensorHandle, ISensorsEventCallba mSensorInfo.version = 1; mSensorInfo.type = SensorType::MAGNETIC_FIELD; mSensorInfo.typeAsString = ""; - mSensorInfo.maxRange = 4911.0f; - mSensorInfo.resolution = 1.00f; + mSensorInfo.maxRange = 1300.0f; + mSensorInfo.resolution = 0.01f; mSensorInfo.power = 0.001f; // mA - mSensorInfo.minDelay = 14284.0f; // microseconds - mSensorInfo.maxDelay = 0.0f; // microseconds + mSensorInfo.minDelay = 20 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -255,11 +259,11 @@ LightSensor::LightSensor(int32_t sensorHandle, ISensorsEventCallback* callback) mSensorInfo.version = 1; mSensorInfo.type = SensorType::LIGHT; mSensorInfo.typeAsString = ""; - mSensorInfo.maxRange = 10000.0f; + mSensorInfo.maxRange = 43000.0f; mSensorInfo.resolution = 10.0f; mSensorInfo.power = 0.001f; // mA - mSensorInfo.minDelay = 20.0f * 1000; // microseconds - mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.minDelay = 200 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -277,8 +281,8 @@ ProximitySensor::ProximitySensor(int32_t sensorHandle, ISensorsEventCallback* ca mSensorInfo.maxRange = 5.0f; mSensorInfo.resolution = 1.0f; mSensorInfo.power = 0.012f; // mA - mSensorInfo.minDelay = 500; // microseconds - mSensorInfo.maxDelay = 2 * mSensorInfo.minDelay; + mSensorInfo.minDelay = 200 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -293,11 +297,11 @@ GyroSensor::GyroSensor(int32_t sensorHandle, ISensorsEventCallback* callback) : mSensorInfo.version = 1; mSensorInfo.type = SensorType::GYROSCOPE; mSensorInfo.typeAsString = ""; - mSensorInfo.maxRange = 8.726639f; - mSensorInfo.resolution = 1.0f; + mSensorInfo.maxRange = 1000.0f * M_PI / 180.0f; + mSensorInfo.resolution = 1000.0f * M_PI / (180.0f * 32768.0f); mSensorInfo.power = 0.001f; - mSensorInfo.minDelay = 4444; // microseonds - mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.minDelay = 2.5f * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -313,10 +317,10 @@ AmbientTempSensor::AmbientTempSensor(int32_t sensorHandle, ISensorsEventCallback mSensorInfo.type = SensorType::AMBIENT_TEMPERATURE; mSensorInfo.typeAsString = ""; mSensorInfo.maxRange = 80.0f; - mSensorInfo.resolution = 1.0f; + mSensorInfo.resolution = 0.01f; mSensorInfo.power = 0.001f; - mSensorInfo.minDelay = 4444; // microseonds - mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.minDelay = 40 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -332,10 +336,10 @@ DeviceTempSensor::DeviceTempSensor(int32_t sensorHandle, ISensorsEventCallback* mSensorInfo.type = SensorType::TEMPERATURE; mSensorInfo.typeAsString = ""; mSensorInfo.maxRange = 80.0f; - mSensorInfo.resolution = 1.0f; + mSensorInfo.resolution = 0.01f; mSensorInfo.power = 0.001f; - mSensorInfo.minDelay = 4444; // microseonds - mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.minDelay = 40 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; @@ -352,10 +356,10 @@ RelativeHumiditySensor::RelativeHumiditySensor(int32_t sensorHandle, mSensorInfo.type = SensorType::RELATIVE_HUMIDITY; mSensorInfo.typeAsString = ""; mSensorInfo.maxRange = 100.0f; - mSensorInfo.resolution = 1.0f; + mSensorInfo.resolution = 0.1f; mSensorInfo.power = 0.001f; - mSensorInfo.minDelay = 4444; // microseonds - mSensorInfo.maxDelay = 0; // microseconds + mSensorInfo.minDelay = 40 * 1000; // microseconds + mSensorInfo.maxDelay = kDefaultMaxDelayUs; mSensorInfo.fifoReservedEventCount = 0; mSensorInfo.fifoMaxEventCount = 0; mSensorInfo.requiredPermission = ""; -- cgit v1.2.3