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