sensor: Replace GpioLock with stdplus RAII helper
The old RAII helper was not move safe, although it was never moved in
the current code so it didn't have any effect on runtime safety.
Change-Id: Ica19ed7e60d699d86d0166b356cedb82e4a28b61
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/sensor.cpp b/sensor.cpp
index 6e5e841..145ba6c 100644
--- a/sensor.cpp
+++ b/sensor.cpp
@@ -9,6 +9,7 @@
#include "sysfs.hpp"
#include <cassert>
+#include <chrono>
#include <cmath>
#include <cstring>
#include <filesystem>
@@ -151,7 +152,7 @@
#endif
{
// RAII object for GPIO unlock / lock
- GpioLock gpioLock(getGpio());
+ auto locker = gpioUnlock(getGpio());
// Retry for up to a second if device is busy
// or has a transient error.
@@ -259,31 +260,22 @@
return iface;
}
-GpioLock::GpioLock(const gpioplus::HandleInterface* handle) : _handle(handle)
+void gpioLock(const gpioplus::HandleInterface*&& handle)
{
- unlockGpio();
+ handle->setValues({0});
}
-GpioLock::~GpioLock()
+std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle)
{
- lockGpio();
-}
-
-void GpioLock::unlockGpio()
-{
- if (_handle)
+ if (handle == nullptr)
{
- _handle->setValues({1});
- std::this_thread::sleep_for(_pause);
+ return std::nullopt;
}
-}
-void GpioLock::lockGpio()
-{
- if (_handle)
- {
- _handle->setValues({0});
- }
+ handle->setValues({1});
+ // Default pause needed to guarantee sensors are ready
+ std::this_thread::sleep_for(std::chrono::milliseconds(500));
+ return GpioLocker(std::move(handle));
}
} // namespace sensor