summaryrefslogtreecommitdiffstats
path: root/watchdog.cpp
diff options
context:
space:
mode:
authorVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-05-29 19:39:08 +0530
committerVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-06-02 16:37:48 +0530
commitd7a3f13ef17e34a39bae3ff482c15181e40cc0d7 (patch)
tree47c1d40b652828101541f216b0f9282e61c4519c /watchdog.cpp
parent7e146557a27508a07ce2abeaf22871daab94c6d8 (diff)
downloadphosphor-watchdog-d7a3f13ef17e34a39bae3ff482c15181e40cc0d7.tar.gz
phosphor-watchdog-d7a3f13ef17e34a39bae3ff482c15181e40cc0d7.zip
Implement Watchdog interface
This commit gives concrete implementation of the interface. Change-Id: I3951c5811c8e6cff87c87842a1e3c538463bfde7 Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
Diffstat (limited to 'watchdog.cpp')
-rw-r--r--watchdog.cpp96
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
OpenPOWER on IntegriCloud