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