Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <systemd/sd-event.h> |
| 4 | #include <sdbusplus/bus.hpp> |
| 5 | #include <sdbusplus/server/object.hpp> |
| 6 | #include <xyz/openbmc_project/State/Watchdog/server.hpp> |
| 7 | #include "timer.hpp" |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace watchdog |
| 11 | { |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 12 | namespace Base = sdbusplus::xyz::openbmc_project::State::server; |
| 13 | using WatchdogInherits = sdbusplus::server::object::object<Base::Watchdog>; |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 14 | |
| 15 | /** @class Watchdog |
| 16 | * @brief OpenBMC watchdog implementation. |
| 17 | * @details A concrete implementation for the |
| 18 | * xyz.openbmc_project.State.Watchdog DBus API. |
| 19 | */ |
| 20 | class Watchdog : public WatchdogInherits |
| 21 | { |
| 22 | public: |
| 23 | Watchdog() = delete; |
| 24 | ~Watchdog() = default; |
| 25 | Watchdog(const Watchdog&) = delete; |
| 26 | Watchdog& operator=(const Watchdog&) = delete; |
| 27 | Watchdog(Watchdog&&) = delete; |
| 28 | Watchdog& operator=(Watchdog &&) = delete; |
| 29 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 30 | /** @brief Type used to hold the name of a systemd target. |
| 31 | */ |
| 32 | using TargetName = std::string; |
| 33 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 34 | /** @brief Constructs the Watchdog object |
| 35 | * |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 36 | * @param[in] bus - DBus bus to attach to. |
| 37 | * @param[in] objPath - Object path to attach to. |
| 38 | * @param[in] event - reference to sd_event unique pointer |
| 39 | * @param[in] actionTargets - map of systemd targets called on timeout |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 40 | */ |
| 41 | Watchdog(sdbusplus::bus::bus& bus, |
| 42 | const char* objPath, |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 43 | EventPtr& event, |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 44 | std::map<Action, TargetName>&& actionTargets = |
| 45 | std::map<Action, TargetName>()) : |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 46 | WatchdogInherits(bus, objPath), |
| 47 | bus(bus), |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 48 | actionTargets(std::move(actionTargets)), |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 49 | timer(event, std::bind(&Watchdog::timeOutHandler, this)) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 50 | { |
| 51 | // Nothing |
| 52 | } |
| 53 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 54 | /** @brief Since we are overriding the setter-enabled but not the |
| 55 | * getter-enabled, we need to have this using in order to |
| 56 | * allow passthrough usage of the getter-enabled. |
| 57 | */ |
| 58 | using Base::Watchdog::enabled; |
| 59 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 60 | /** @brief Enable or disable watchdog |
| 61 | * If a watchdog state is changed from disable to enable, |
| 62 | * the watchdog timer is set with the default expiration |
| 63 | * interval and it starts counting down. |
| 64 | * If a watchdog is already enabled, setting @value to true |
| 65 | * has no effect. |
| 66 | * |
| 67 | * @param[in] value - 'true' to enable. 'false' to disable |
| 68 | * |
| 69 | * @return : applied value if success, previous value otherwise |
| 70 | */ |
| 71 | bool enabled(bool value) override; |
| 72 | |
| 73 | /** @brief Gets the remaining time before watchdog expires. |
| 74 | * |
| 75 | * @return 0 if watchdog is disabled or expired. |
| 76 | * Remaining time in milliseconds otherwise. |
| 77 | */ |
| 78 | uint64_t timeRemaining() const override; |
| 79 | |
| 80 | /** @brief Reset timer to expire after new timeout in milliseconds. |
| 81 | * |
Gunnar Mills | bfe5cb8 | 2017-10-25 20:48:50 -0500 | [diff] [blame] | 82 | * @param[in] value - the time in milliseconds after which |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 83 | * the watchdog will expire |
| 84 | * |
| 85 | * @return: updated timeout value if watchdog is enabled. |
| 86 | * 0 otherwise. |
| 87 | */ |
| 88 | uint64_t timeRemaining(uint64_t value) override; |
| 89 | |
| 90 | /** @brief Tells if the referenced timer is expired or not */ |
| 91 | inline auto timerExpired() const |
| 92 | { |
| 93 | return timer.expired(); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | /** @brief sdbusplus handle */ |
| 98 | sdbusplus::bus::bus& bus; |
| 99 | |
William A. Kennington III | 1232a15 | 2018-02-02 15:57:34 -0800 | [diff] [blame] | 100 | /** @brief Map of systemd units to be started when the timer expires */ |
| 101 | std::map<Action, TargetName> actionTargets; |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 102 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 103 | /** @brief Contained timer object */ |
| 104 | Timer timer; |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 105 | |
| 106 | /** @brief Optional Callback handler on timer expirartion */ |
| 107 | void timeOutHandler(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace watchdog |
| 111 | } // namespace phosphor |