Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 1 | #include <phosphor-logging/log.hpp> |
| 2 | #include "timer.hpp" |
| 3 | namespace phosphor |
| 4 | { |
| 5 | namespace ipmi |
| 6 | { |
| 7 | |
| 8 | using namespace phosphor::logging; |
| 9 | |
| 10 | // Initializes the timer object |
| 11 | void Timer::initialize() |
| 12 | { |
| 13 | // This can not be called more than once. |
| 14 | if (eventSource) |
| 15 | { |
| 16 | throw std::runtime_error("Timer already initialized"); |
| 17 | } |
| 18 | |
| 19 | // Add infinite expiration time |
| 20 | auto r = sd_event_add_time(timeEvent, &eventSource, |
| 21 | CLOCK_MONOTONIC, // Time base |
| 22 | UINT64_MAX, // Expire time - way long enough time |
| 23 | 0, // Use default event accuracy |
| 24 | timeoutHandler, // Callback handler on timeout |
| 25 | this); // User data |
| 26 | if (r < 0) |
| 27 | { |
| 28 | log<level::ERR>("Failure to set initial expiration time value", |
| 29 | entry("ERROR=%s", strerror(-r))); |
| 30 | |
| 31 | throw std::runtime_error("Timer initialization failed"); |
| 32 | } |
| 33 | |
| 34 | // Disable the timer for now |
| 35 | r = sd_event_source_set_enabled(eventSource, SD_EVENT_OFF); |
| 36 | if (r < 0) |
| 37 | { |
| 38 | log<level::ERR>("Failure to disable timer", |
| 39 | entry("ERROR=%s", strerror(-r))); |
| 40 | |
| 41 | throw std::runtime_error("Setting initial timer value failed"); |
| 42 | } |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | /** @brief callback handler on timeout */ |
| 47 | int Timer::timeoutHandler(sd_event_source* eventSource, |
| 48 | uint64_t usec, void* userData) |
| 49 | { |
| 50 | auto timer = static_cast<Timer*>(userData); |
| 51 | timer->expired = true; |
| 52 | |
| 53 | log<level::INFO>("Timer expired"); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | } // namespace ipmi |
| 58 | } // namespace phosphor |