diff options
Diffstat (limited to 'watchdog.cpp')
-rw-r--r-- | watchdog.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/watchdog.cpp b/watchdog.cpp new file mode 100644 index 0000000..c9e9919 --- /dev/null +++ b/watchdog.cpp @@ -0,0 +1,96 @@ +#include <chrono> +#include <phosphor-logging/log.hpp> +#include "watchdog.hpp" + +namespace phosphor +{ +namespace watchdog +{ +using namespace std::chrono; +using namespace std::chrono_literals; +using namespace phosphor::logging; + +// Enable or disable watchdog +bool Watchdog::enabled(bool value) +{ + if (WatchdogInherits::enabled() != value) + { + if (value) + { + // Start ONESHOT timer. Timer handles all in usec + auto usec = duration_cast<microseconds>( + milliseconds(this->interval())); + // Update new expiration + timer.start(usec); + + // Enable timer + timer.setEnabled<std::true_type>(); + + log<level::INFO>("watchdog: enabled and started", + entry("INTERVAL=%llu", this->interval())); + } + else + { + timer.setEnabled<std::false_type>(); + log<level::INFO>("watchdog: disabled"); + } + } + return WatchdogInherits::enabled(value); +} + +// Get the remaining time before timer expires. +// If the timer is disabled, returns 0 +uint64_t Watchdog::timeRemaining() const +{ + uint64_t timeRemain = 0; + + if (WatchdogInherits::enabled()) + { + // timer may have already expired and disabled + if (timer.getEnabled() != SD_EVENT_OFF) + { + // the one-shot timer does not expire yet + auto expiry = duration_cast<milliseconds>( + timer.getRemaining()); + + // convert to msec per interface expectation. + auto timeNow = duration_cast<milliseconds>( + Timer::getCurrentTime()); + + // Its possible that timer may have expired by now. + // So need to cross verify. + timeRemain = (expiry > timeNow) ? + (expiry - timeNow).count() : 0; + } + } + return timeRemain; +} + +// Reset the timer to a new expiration value +uint64_t Watchdog::timeRemaining(uint64_t value) +{ + if (WatchdogInherits::enabled()) + { + // Disable the timer + timer.setEnabled<std::false_type>(); + + // Timer handles all in microseconds and hence converting + auto usec = duration_cast<microseconds>( + milliseconds(value)); + // Update new expiration + timer.start(usec); + + // Enable the timer. + timer.setEnabled<std::true_type>(); + + log<level::INFO>("watchdog: reset timer", + entry("VALUE=%llu", value)); + + // Update Base class data. + return WatchdogInherits::timeRemaining(value); + } + return 0; +} + +} // namespace watchdog +} // namepsace phosphor |