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