Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame] | 3 | #include <functional> |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame] | 4 | #include <optional> |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/server/object.hpp> |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 7 | #include <sdeventplus/event.hpp> |
| 8 | #include <sdeventplus/utility/timer.hpp> |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 9 | #include <unordered_map> |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame] | 10 | #include <utility> |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 11 | #include <xyz/openbmc_project/State/Watchdog/server.hpp> |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame] | 12 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace watchdog |
| 16 | { |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 17 | |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 18 | constexpr auto DEFAULT_MIN_INTERVAL_MS = 0; |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 19 | namespace Base = sdbusplus::xyz::openbmc_project::State::server; |
| 20 | using WatchdogInherits = sdbusplus::server::object::object<Base::Watchdog>; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 21 | |
| 22 | /** @class Watchdog |
| 23 | * @brief OpenBMC watchdog implementation. |
| 24 | * @details A concrete implementation for the |
| 25 | * xyz.openbmc_project.State.Watchdog DBus API. |
| 26 | */ |
| 27 | class Watchdog : public WatchdogInherits |
| 28 | { |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 29 | public: |
| 30 | Watchdog() = delete; |
| 31 | ~Watchdog() = default; |
| 32 | Watchdog(const Watchdog&) = delete; |
| 33 | Watchdog& operator=(const Watchdog&) = delete; |
| 34 | Watchdog(Watchdog&&) = delete; |
| 35 | Watchdog& operator=(Watchdog&&) = delete; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 36 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 37 | /** @brief Type used to hold the name of a systemd target. |
| 38 | */ |
| 39 | using TargetName = std::string; |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 40 | |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 41 | /** @brief Type used to store the mapping of a Watchdog timeout |
| 42 | * action to a systemd target. |
| 43 | */ |
| 44 | using ActionTargetMap = std::unordered_map<Action, TargetName>; |
| 45 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 46 | /** @brief Type used to specify the parameters of a fallback watchdog |
| 47 | */ |
| 48 | struct Fallback |
| 49 | { |
| 50 | Action action; |
| 51 | uint64_t interval; |
| 52 | bool always; |
| 53 | }; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 54 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 55 | /** @brief Constructs the Watchdog object |
| 56 | * |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 57 | * @param[in] bus - DBus bus to attach to. |
| 58 | * @param[in] objPath - Object path to attach to. |
| 59 | * @param[in] event - reference to sdeventplus::Event loop |
| 60 | * @param[in] actionTargets - map of systemd targets called on timeout |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 61 | * @param[in] fallback |
| 62 | */ |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 63 | Watchdog(sdbusplus::bus::bus& bus, const char* objPath, |
| 64 | const sdeventplus::Event& event, |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 65 | ActionTargetMap&& actionTargetMap = {}, |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 66 | std::optional<Fallback>&& fallback = std::nullopt, |
| 67 | uint64_t minInterval = DEFAULT_MIN_INTERVAL_MS) : |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 68 | WatchdogInherits(bus, objPath), |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 69 | bus(bus), actionTargetMap(std::move(actionTargetMap)), |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 70 | fallback(std::move(fallback)), minInterval(minInterval), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 71 | timer(event, std::bind(&Watchdog::timeOutHandler, this)) |
| 72 | { |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 73 | // We set the watchdog interval with the default value. |
| 74 | interval(interval()); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 75 | // We need to poke the enable mechanism to make sure that the timer |
| 76 | // enters the fallback state if the fallback is always enabled. |
| 77 | tryFallbackOrDisable(); |
| 78 | } |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 79 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 80 | /** @brief Resets the TimeRemaining to the configured Interval |
| 81 | * Optionally enables the watchdog. |
| 82 | * |
| 83 | * @param[in] enableWatchdog - Should the call enable the watchdog |
| 84 | */ |
| 85 | void resetTimeRemaining(bool enableWatchdog) override; |
William A. Kennington III | 1726df6 | 2018-04-23 10:28:28 -0700 | [diff] [blame] | 86 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 87 | /** @brief Since we are overriding the setter-enabled but not the |
| 88 | * getter-enabled, we need to have this using in order to |
| 89 | * allow passthrough usage of the getter-enabled. |
| 90 | */ |
| 91 | using Base::Watchdog::enabled; |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 92 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 93 | /** @brief Enable or disable watchdog |
| 94 | * If a watchdog state is changed from disable to enable, |
| 95 | * the watchdog timer is set with the default expiration |
| 96 | * interval and it starts counting down. |
| 97 | * If a watchdog is already enabled, setting @value to true |
| 98 | * has no effect. |
| 99 | * |
| 100 | * @param[in] value - 'true' to enable. 'false' to disable |
| 101 | * |
| 102 | * @return : applied value if success, previous value otherwise |
| 103 | */ |
| 104 | bool enabled(bool value) override; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 105 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 106 | /** @brief Gets the remaining time before watchdog expires. |
| 107 | * |
| 108 | * @return 0 if watchdog is expired. |
| 109 | * Remaining time in milliseconds otherwise. |
| 110 | */ |
| 111 | uint64_t timeRemaining() const override; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 112 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 113 | /** @brief Reset timer to expire after new timeout in milliseconds. |
| 114 | * |
| 115 | * @param[in] value - the time in milliseconds after which |
| 116 | * the watchdog will expire |
| 117 | * |
| 118 | * @return: updated timeout value if watchdog is enabled. |
| 119 | * 0 otherwise. |
| 120 | */ |
| 121 | uint64_t timeRemaining(uint64_t value) override; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 122 | |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 123 | /** @brief Get value of Interval |
| 124 | * |
| 125 | * |
| 126 | * @return: current interval |
| 127 | * |
| 128 | */ |
| 129 | using WatchdogInherits::interval; |
| 130 | |
| 131 | /** @brief Set value of Interval |
| 132 | * |
| 133 | * @param[in] value - interval time to set |
| 134 | * |
| 135 | * @return: interval that was set |
| 136 | * |
| 137 | */ |
| 138 | uint64_t interval(uint64_t value); |
| 139 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 140 | /** @brief Tells if the referenced timer is expired or not */ |
| 141 | inline auto timerExpired() const |
| 142 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 143 | return timer.hasExpired(); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 144 | } |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 145 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 146 | /** @brief Tells if the timer is running or not */ |
| 147 | inline bool timerEnabled() const |
| 148 | { |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 149 | return timer.isEnabled(); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 150 | } |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 151 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 152 | private: |
| 153 | /** @brief sdbusplus handle */ |
| 154 | sdbusplus::bus::bus& bus; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 155 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 156 | /** @brief Map of systemd units to be started when the timer expires */ |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 157 | ActionTargetMap actionTargetMap; |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 158 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 159 | /** @brief Fallback timer options */ |
William A. Kennington III | 73c2cfb | 2018-09-12 18:01:37 -0700 | [diff] [blame] | 160 | std::optional<Fallback> fallback; |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 161 | |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 162 | /** @brief Minimum watchdog interval value */ |
| 163 | uint64_t minInterval; |
| 164 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 165 | /** @brief Contained timer object */ |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 166 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer; |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 167 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 168 | /** @brief Optional Callback handler on timer expirartion */ |
| 169 | void timeOutHandler(); |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 170 | |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 171 | /** @brief Attempt to enter the fallback watchdog or disables it */ |
| 172 | void tryFallbackOrDisable(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace watchdog |
| 176 | } // namespace phosphor |