Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "timer.hpp" |
| 2 | |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 3 | #include <chrono> |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 5 | namespace phosphor |
| 6 | { |
| 7 | namespace ipmi |
| 8 | { |
| 9 | |
| 10 | using namespace phosphor::logging; |
| 11 | |
| 12 | // Initializes the timer object |
| 13 | void Timer::initialize() |
| 14 | { |
| 15 | // This can not be called more than once. |
| 16 | if (eventSource) |
| 17 | { |
| 18 | throw std::runtime_error("Timer already initialized"); |
| 19 | } |
| 20 | |
| 21 | // Add infinite expiration time |
| 22 | auto r = sd_event_add_time(timeEvent, &eventSource, |
| 23 | CLOCK_MONOTONIC, // Time base |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 24 | UINT64_MAX, // Expire time - way long time |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 25 | 0, // Use default event accuracy |
| 26 | timeoutHandler, // Callback handler on timeout |
| 27 | this); // User data |
| 28 | if (r < 0) |
| 29 | { |
| 30 | log<level::ERR>("Failure to set initial expiration time value", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 31 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 32 | |
| 33 | throw std::runtime_error("Timer initialization failed"); |
| 34 | } |
| 35 | |
| 36 | // Disable the timer for now |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 37 | r = setTimer(SD_EVENT_OFF); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 38 | if (r < 0) |
| 39 | { |
| 40 | log<level::ERR>("Failure to disable timer", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 41 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 42 | |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 43 | throw std::runtime_error("Disabling the timer failed"); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 44 | } |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | /** @brief callback handler on timeout */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 49 | int Timer::timeoutHandler(sd_event_source* eventSource, uint64_t usec, |
| 50 | void* userData) |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 51 | { |
| 52 | auto timer = static_cast<Timer*>(userData); |
| 53 | timer->expired = true; |
| 54 | |
Ratan Gupta | 32cbd65 | 2018-03-07 16:03:53 +0530 | [diff] [blame] | 55 | log<level::INFO>("Timer expired"); |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 56 | // Call optional user call back function if available |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 57 | if (timer->userCallBack) |
Andrew Geissler | 8622f69 | 2017-04-02 18:19:00 -0500 | [diff] [blame] | 58 | { |
| 59 | timer->userCallBack(); |
| 60 | } |
| 61 | |
Ratan Gupta | 32cbd65 | 2018-03-07 16:03:53 +0530 | [diff] [blame] | 62 | sd_event_source_set_enabled(eventSource, SD_EVENT_OFF); |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 66 | // Gets the time from steady_clock |
| 67 | std::chrono::microseconds Timer::getTime() |
| 68 | { |
| 69 | using namespace std::chrono; |
| 70 | auto usec = steady_clock::now().time_since_epoch(); |
| 71 | return duration_cast<microseconds>(usec); |
| 72 | } |
| 73 | |
| 74 | // Enables or disables the timer |
| 75 | int Timer::setTimer(int action) |
| 76 | { |
| 77 | return sd_event_source_set_enabled(eventSource, action); |
| 78 | } |
| 79 | |
| 80 | // Sets the time and arms the timer |
| 81 | int Timer::startTimer(std::chrono::microseconds timeValue) |
| 82 | { |
| 83 | // Disable the timer |
| 84 | setTimer(SD_EVENT_OFF); |
Ratan Gupta | 32cbd65 | 2018-03-07 16:03:53 +0530 | [diff] [blame] | 85 | expired = false; |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 86 | |
| 87 | // Get the current MONOTONIC time and add the delta |
| 88 | auto expireTime = getTime() + timeValue; |
| 89 | |
| 90 | // Set the time |
| 91 | auto r = sd_event_source_set_time(eventSource, expireTime.count()); |
| 92 | if (r < 0) |
| 93 | { |
| 94 | log<level::ERR>("Failure to set timer", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 95 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 96 | return r; |
| 97 | } |
| 98 | |
| 99 | // A ONESHOT timer means that when the timer goes off, |
| 100 | // its moves to disabled state. |
| 101 | r = setTimer(SD_EVENT_ONESHOT); |
| 102 | if (r < 0) |
| 103 | { |
| 104 | log<level::ERR>("Failure to start timer", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 105 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | d27e71e | 2017-02-01 18:02:38 +0530 | [diff] [blame] | 106 | } |
| 107 | return r; |
| 108 | } |
| 109 | |
Vishwanatha Subbanna | bcb7688 | 2017-01-25 16:29:43 +0530 | [diff] [blame] | 110 | } // namespace ipmi |
| 111 | } // namespace phosphor |