From d27e71e23cbe02dad145419c46c064d212420836 Mon Sep 17 00:00:00 2001 From: Vishwanatha Subbanna Date: Wed, 1 Feb 2017 18:02:38 +0530 Subject: Add routines to start and stop the sd_event timer Change-Id: I738be7b70554125e544aa59fe1770e909d3dffb1 Signed-off-by: Vishwanatha Subbanna --- softoff/timer.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'softoff/timer.cpp') diff --git a/softoff/timer.cpp b/softoff/timer.cpp index 9e722f1..c626536 100644 --- a/softoff/timer.cpp +++ b/softoff/timer.cpp @@ -1,3 +1,4 @@ +#include #include #include "timer.hpp" namespace phosphor @@ -19,7 +20,7 @@ void Timer::initialize() // Add infinite expiration time auto r = sd_event_add_time(timeEvent, &eventSource, CLOCK_MONOTONIC, // Time base - UINT64_MAX, // Expire time - way long enough time + UINT64_MAX, // Expire time - way long time 0, // Use default event accuracy timeoutHandler, // Callback handler on timeout this); // User data @@ -32,13 +33,13 @@ void Timer::initialize() } // Disable the timer for now - r = sd_event_source_set_enabled(eventSource, SD_EVENT_OFF); + r = setTimer(SD_EVENT_OFF); if (r < 0) { log("Failure to disable timer", entry("ERROR=%s", strerror(-r))); - throw std::runtime_error("Setting initial timer value failed"); + throw std::runtime_error("Disabling the timer failed"); } return; } @@ -54,5 +55,48 @@ int Timer::timeoutHandler(sd_event_source* eventSource, return 0; } +// Gets the time from steady_clock +std::chrono::microseconds Timer::getTime() +{ + using namespace std::chrono; + auto usec = steady_clock::now().time_since_epoch(); + return duration_cast(usec); +} + +// Enables or disables the timer +int Timer::setTimer(int action) +{ + return sd_event_source_set_enabled(eventSource, action); +} + +// Sets the time and arms the timer +int Timer::startTimer(std::chrono::microseconds timeValue) +{ + // Disable the timer + setTimer(SD_EVENT_OFF); + + // Get the current MONOTONIC time and add the delta + auto expireTime = getTime() + timeValue; + + // Set the time + auto r = sd_event_source_set_time(eventSource, expireTime.count()); + if (r < 0) + { + log("Failure to set timer", + entry("ERROR=%s", strerror(-r))); + return r; + } + + // A ONESHOT timer means that when the timer goes off, + // its moves to disabled state. + r = setTimer(SD_EVENT_ONESHOT); + if (r < 0) + { + log("Failure to start timer", + entry("ERROR=%s", strerror(-r))); + } + return r; +} + } // namespace ipmi } // namespace phosphor -- cgit v1.2.1