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