diff options
author | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2017-05-30 15:34:23 +0530 |
---|---|---|
committer | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2017-06-02 16:37:51 +0530 |
commit | 8c5a2298627c6cbe37782f3b33e8d6c43ed9c4d8 (patch) | |
tree | 784fc6e0fa0a9c79ac4d14d43a2c71b5d1ad9ebc | |
parent | d7a3f13ef17e34a39bae3ff482c15181e40cc0d7 (diff) | |
download | phosphor-watchdog-8c5a2298627c6cbe37782f3b33e8d6c43ed9c4d8.tar.gz phosphor-watchdog-8c5a2298627c6cbe37782f3b33e8d6c43ed9c4d8.zip |
Invoke optional callback function on timer expiration
When the timer expires, it calls into it's own timeout
handler which matches with sd_event callback handler.
However, it is beneficial if the users of timer register
their own callback routine so that they can execute some
operations on timeout.
Change-Id: Ia88cb4e3c17f6dd8d4528fa193ec7927f083a92b
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
-rw-r--r-- | timer.cpp | 10 | ||||
-rw-r--r-- | timer.hpp | 15 | ||||
-rw-r--r-- | watchdog.cpp | 9 | ||||
-rw-r--r-- | watchdog.hpp | 6 |
4 files changed, 31 insertions, 9 deletions
@@ -44,12 +44,16 @@ int Timer::timeoutHandler(sd_event_source* eventSource, { using namespace phosphor::logging; + log<level::INFO>("Timer Expired"); + auto timer = static_cast<Timer*>(userData); timer->expire = true; - log<level::INFO>("Timer Expired"); - - //TODO: Need to call user callback function. + // Call an optional callback function + if(timer->userCallBack) + { + timer->userCallBack(); + } return 0; } @@ -43,10 +43,14 @@ class Timer /** @brief Constructs timer object * - * @param[in] event - sd_event unique pointer reference + * @param[in] event - sd_event unique pointer + * @param[in] userCallBack - Optional function callback + * for timer expiration */ - Timer(EventPtr& event) - : event(event) + Timer(EventPtr& event, + std::function<void()> userCallBack = nullptr) + : event(event), + userCallBack(userCallBack) { // Initialize the timer initialize(); @@ -100,6 +104,11 @@ class Timer /** @brief Set to true when the timeoutHandler is called into */ bool expire = false; + /** @brief Optional function to call on timer expiration + * This is called from timeout handler. + */ + std::function<void()> userCallBack; + /** @brief Initializes the timer object with infinite * expiration time and sets up the callback handler * diff --git a/watchdog.cpp b/watchdog.cpp index c9e9919..e5e8aff 100644 --- a/watchdog.cpp +++ b/watchdog.cpp @@ -1,7 +1,6 @@ #include <chrono> #include <phosphor-logging/log.hpp> #include "watchdog.hpp" - namespace phosphor { namespace watchdog @@ -92,5 +91,13 @@ uint64_t Watchdog::timeRemaining(uint64_t value) return 0; } +// Optional callback function on timer expiration +void Watchdog::timeOutHandler() +{ + log<level::INFO>("Optional callback called"); + // TODO: Need to call the user passed systemd + // target on this condition +} + } // namespace watchdog } // namepsace phosphor diff --git a/watchdog.hpp b/watchdog.hpp index 47484af..1a7a410 100644 --- a/watchdog.hpp +++ b/watchdog.hpp @@ -5,7 +5,6 @@ #include <sdbusplus/server/object.hpp> #include <xyz/openbmc_project/State/Watchdog/server.hpp> #include "timer.hpp" - namespace phosphor { namespace watchdog @@ -39,7 +38,7 @@ class Watchdog : public WatchdogInherits EventPtr& event) : WatchdogInherits(bus, objPath), bus(bus), - timer(event) + timer(event, std::bind(&Watchdog::timeOutHandler, this)) { // Nothing } @@ -86,6 +85,9 @@ class Watchdog : public WatchdogInherits /** @brief Contained timer object */ Timer timer; + + /** @brief Optional Callback handler on timer expirartion */ + void timeOutHandler(); }; } // namespace watchdog |