Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <systemd/sd-event.h> |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | #include "timer.hpp" |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace watchdog |
| 8 | { |
| 9 | |
| 10 | // Initializes the timer object |
| 11 | void Timer::initialize() |
| 12 | { |
| 13 | // This can not be called more than once. |
| 14 | if (eventSource.get()) |
| 15 | { |
| 16 | // TODO: Need to throw elog exception stating its already added. |
| 17 | throw std::runtime_error("Timer already initialized"); |
| 18 | } |
| 19 | |
| 20 | // Add infinite expiration time |
| 21 | decltype(eventSource.get()) sourcePtr = nullptr; |
| 22 | auto r = sd_event_add_time(event.get(), |
| 23 | &sourcePtr, |
| 24 | CLOCK_MONOTONIC, // Time base |
| 25 | UINT64_MAX, // Expire time - way long time |
| 26 | 0, // Use default event accuracy |
| 27 | timeoutHandler, // Callback handler on timeout |
| 28 | this); // User data |
| 29 | eventSource.reset(sourcePtr); |
| 30 | |
| 31 | if (r < 0) |
| 32 | { |
| 33 | // TODO: throw elog exception |
| 34 | throw std::runtime_error("Timer initialization failed"); |
| 35 | } |
| 36 | |
| 37 | // Disable the timer for now |
| 38 | setEnabled<std::false_type>(); |
| 39 | } |
| 40 | |
| 41 | // callback handler on timeout |
| 42 | int Timer::timeoutHandler(sd_event_source* eventSource, |
| 43 | uint64_t usec, void* userData) |
| 44 | { |
| 45 | using namespace phosphor::logging; |
| 46 | |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 47 | log<level::INFO>("Timer Expired"); |
| 48 | |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 49 | auto timer = static_cast<Timer*>(userData); |
| 50 | timer->expire = true; |
| 51 | |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 52 | // Call an optional callback function |
| 53 | if(timer->userCallBack) |
| 54 | { |
| 55 | timer->userCallBack(); |
| 56 | } |
Vishwanatha Subbanna | 7e14655 | 2017-05-29 17:03:33 +0530 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | // Gets the time from steady_clock |
| 61 | std::chrono::microseconds Timer::getCurrentTime() |
| 62 | { |
| 63 | using namespace std::chrono; |
| 64 | auto usec = steady_clock::now().time_since_epoch(); |
| 65 | return duration_cast<microseconds>(usec); |
| 66 | } |
| 67 | |
| 68 | // Sets the expiration time and arms the timer |
| 69 | void Timer::start(std::chrono::microseconds usec) |
| 70 | { |
| 71 | // Get the current MONOTONIC time and add the delta |
| 72 | auto expireTime = getCurrentTime() + usec; |
| 73 | |
| 74 | // Set the time |
| 75 | auto r = sd_event_source_set_time(eventSource.get(), |
| 76 | expireTime.count()); |
| 77 | if (r < 0) |
| 78 | { |
| 79 | // TODO throw elog exception |
| 80 | throw std::runtime_error("Error setting the expiration time"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Returns current timer enablement type |
| 85 | int Timer::getEnabled() const |
| 86 | { |
| 87 | int enabled{}; |
| 88 | auto r = sd_event_source_get_enabled(eventSource.get(), &enabled); |
| 89 | if (r < 0) |
| 90 | { |
| 91 | // TODO: Need to throw elog exception |
| 92 | throw std::runtime_error("Error geting current time enablement state"); |
| 93 | } |
| 94 | return enabled; |
| 95 | } |
| 96 | |
| 97 | // Enables / disables the timer |
| 98 | void Timer::setEnabled(int type) |
| 99 | { |
| 100 | auto r = sd_event_source_set_enabled(eventSource.get(), type); |
| 101 | if (r < 0) |
| 102 | { |
| 103 | // TODO: Need to throw elog exception |
| 104 | throw std::runtime_error("Error altering enabled property"); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Returns time remaining before expiration |
| 109 | std::chrono::microseconds Timer::getRemaining() const |
| 110 | { |
| 111 | uint64_t next = 0; |
| 112 | auto r = sd_event_source_get_time(eventSource.get(), &next); |
| 113 | if (r < 0) |
| 114 | { |
| 115 | // TODO: Need to throw elog exception |
| 116 | throw std::runtime_error("Error altering enabled property"); |
| 117 | } |
| 118 | return std::chrono::microseconds(next); |
| 119 | } |
| 120 | |
| 121 | } // namespace watchdog |
| 122 | } // namespace phosphor |