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 | |
| 30 | /** @brief Constructs the Watchdog object |
| 31 | * |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 32 | * @param[in] bus - DBus bus to attach to. |
| 33 | * @param[in] objPath - Object path to attach to. |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 34 | * @param[in] event - reference to sd_event unique pointer |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 35 | * @param[in] target - systemd target to be called into on timeout |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 36 | */ |
| 37 | Watchdog(sdbusplus::bus::bus& bus, |
| 38 | const char* objPath, |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 39 | EventPtr& event, |
| 40 | std::string&& target = std::string()) : |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 41 | WatchdogInherits(bus, objPath), |
| 42 | bus(bus), |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 43 | target(std::move(target)), |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 44 | timer(event, std::bind(&Watchdog::timeOutHandler, this)) |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 45 | { |
| 46 | // Nothing |
| 47 | } |
| 48 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 49 | /** @brief Since we are overriding the setter-enabled but not the |
| 50 | * getter-enabled, we need to have this using in order to |
| 51 | * allow passthrough usage of the getter-enabled. |
| 52 | */ |
| 53 | using Base::Watchdog::enabled; |
| 54 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 55 | /** @brief Enable or disable watchdog |
| 56 | * If a watchdog state is changed from disable to enable, |
| 57 | * the watchdog timer is set with the default expiration |
| 58 | * interval and it starts counting down. |
| 59 | * If a watchdog is already enabled, setting @value to true |
| 60 | * has no effect. |
| 61 | * |
| 62 | * @param[in] value - 'true' to enable. 'false' to disable |
| 63 | * |
| 64 | * @return : applied value if success, previous value otherwise |
| 65 | */ |
| 66 | bool enabled(bool value) override; |
| 67 | |
| 68 | /** @brief Gets the remaining time before watchdog expires. |
| 69 | * |
| 70 | * @return 0 if watchdog is disabled or expired. |
| 71 | * Remaining time in milliseconds otherwise. |
| 72 | */ |
| 73 | uint64_t timeRemaining() const override; |
| 74 | |
| 75 | /** @brief Reset timer to expire after new timeout in milliseconds. |
| 76 | * |
Gunnar Mills | bfe5cb8 | 2017-10-25 20:48:50 -0500 | [diff] [blame] | 77 | * @param[in] value - the time in milliseconds after which |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 78 | * the watchdog will expire |
| 79 | * |
| 80 | * @return: updated timeout value if watchdog is enabled. |
| 81 | * 0 otherwise. |
| 82 | */ |
| 83 | uint64_t timeRemaining(uint64_t value) override; |
| 84 | |
| 85 | /** @brief Tells if the referenced timer is expired or not */ |
| 86 | inline auto timerExpired() const |
| 87 | { |
| 88 | return timer.expired(); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | /** @brief sdbusplus handle */ |
| 93 | sdbusplus::bus::bus& bus; |
| 94 | |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame] | 95 | /** @brief Systemd unit to be started when the timer expires */ |
| 96 | std::string target; |
| 97 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 98 | /** @brief Contained timer object */ |
| 99 | Timer timer; |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 100 | |
| 101 | /** @brief Optional Callback handler on timer expirartion */ |
| 102 | void timeOutHandler(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace watchdog |
| 106 | } // namespace phosphor |