Fix Sensors HAL 1.0 VTS tests
HAL 1.0 VTS tests need to detach the polling thread or it will never
exit on its own. Additionally, the poll() methods return status needs to
be checked or HIDL will assert and cause the program to crash.
Bug: 150475314
Test: atest VtsHalSensorsV1_0TargetTest VtsHalSensorsV2_0TargetTest
Change-Id: I626b7aa064a1f258c968d1787872b9c67786dede
diff --git a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp
index 1e5e886..aca6961 100644
--- a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp
+++ b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.cpp
@@ -25,6 +25,13 @@
using ::android::hardware::sensors::V1_0::Result;
using ::android::hardware::sensors::V1_0::SensorInfo;
+void SensorsHidlEnvironmentV1_0::HidlTearDown() {
+ mStopThread = true;
+ if (mPollThread.joinable()) {
+ mPollThread.detach();
+ }
+}
+
bool SensorsHidlEnvironmentV1_0::resetHal() {
// wait upto 100ms * 10 = 1s for hidl service.
constexpr auto RETRY_DELAY = std::chrono::milliseconds(100);
@@ -103,18 +110,23 @@
ALOGD("polling thread start");
while (!stop) {
- env->sensors->poll(
- 64, [&](auto result, const auto& events, const auto& dynamicSensorsAdded) {
- if (result != Result::OK ||
- (events.size() == 0 && dynamicSensorsAdded.size() == 0) || stop) {
- stop = true;
- return;
- }
+ if (!env->sensors
+ ->poll(64,
+ [&](auto result, const auto& events, const auto& dynamicSensorsAdded) {
+ if (result != Result::OK ||
+ (events.size() == 0 && dynamicSensorsAdded.size() == 0) ||
+ stop) {
+ stop = true;
+ return;
+ }
- for (const auto& e : events) {
- env->addEvent(e);
- }
- });
+ for (const auto& e : events) {
+ env->addEvent(e);
+ }
+ })
+ .isOk()) {
+ break;
+ }
}
ALOGD("polling thread end");
}
\ No newline at end of file
diff --git a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h
index 485ed1e..168777d 100644
--- a/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h
+++ b/sensors/1.0/vts/functional/SensorsHidlEnvironmentV1_0.h
@@ -32,6 +32,8 @@
class SensorsHidlEnvironmentV1_0
: public SensorsHidlEnvironmentBase<::android::hardware::sensors::V1_0::Event> {
public:
+ void HidlTearDown() override;
+
using Event = ::android::hardware::sensors::V1_0::Event;
SensorsHidlEnvironmentV1_0(const std::string& service_name)
: SensorsHidlEnvironmentBase(service_name) {}
diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h
index 781427d..19dfbe5 100644
--- a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h
+++ b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsHidlEnvironmentBase.h
@@ -46,12 +46,7 @@
std::this_thread::sleep_for(std::chrono::seconds(3));
}
- virtual void HidlTearDown() {
- mStopThread = true;
- if (mPollThread.joinable()) {
- mPollThread.join();
- }
- }
+ virtual void HidlTearDown() = 0;
// Get and clear all events collected so far (like "cat" shell command).
// If output is nullptr, it clears all collected events.