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 | { |
| 12 | using WatchdogInherits = sdbusplus::server::object::object< |
| 13 | sdbusplus::xyz::openbmc_project::State::server::Watchdog>; |
| 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 | |
| 49 | /** @brief Enable or disable watchdog |
| 50 | * If a watchdog state is changed from disable to enable, |
| 51 | * the watchdog timer is set with the default expiration |
| 52 | * interval and it starts counting down. |
| 53 | * If a watchdog is already enabled, setting @value to true |
| 54 | * has no effect. |
| 55 | * |
| 56 | * @param[in] value - 'true' to enable. 'false' to disable |
| 57 | * |
| 58 | * @return : applied value if success, previous value otherwise |
| 59 | */ |
| 60 | bool enabled(bool value) override; |
| 61 | |
| 62 | /** @brief Gets the remaining time before watchdog expires. |
| 63 | * |
| 64 | * @return 0 if watchdog is disabled or expired. |
| 65 | * Remaining time in milliseconds otherwise. |
| 66 | */ |
| 67 | uint64_t timeRemaining() const override; |
| 68 | |
| 69 | /** @brief Reset timer to expire after new timeout in milliseconds. |
| 70 | * |
| 71 | * @param[in] value - the time in miliseconds after which |
| 72 | * the watchdog will expire |
| 73 | * |
| 74 | * @return: updated timeout value if watchdog is enabled. |
| 75 | * 0 otherwise. |
| 76 | */ |
| 77 | uint64_t timeRemaining(uint64_t value) override; |
| 78 | |
| 79 | /** @brief Tells if the referenced timer is expired or not */ |
| 80 | inline auto timerExpired() const |
| 81 | { |
| 82 | return timer.expired(); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | /** @brief sdbusplus handle */ |
| 87 | sdbusplus::bus::bus& bus; |
| 88 | |
Vishwanatha Subbanna | 3473d70 | 2017-05-30 16:38:50 +0530 | [diff] [blame^] | 89 | /** @brief Systemd unit to be started when the timer expires */ |
| 90 | std::string target; |
| 91 | |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 92 | /** @brief Contained timer object */ |
| 93 | Timer timer; |
Vishwanatha Subbanna | 8c5a229 | 2017-05-30 15:34:23 +0530 | [diff] [blame] | 94 | |
| 95 | /** @brief Optional Callback handler on timer expirartion */ |
| 96 | void timeOutHandler(); |
Vishwanatha Subbanna | d7a3f13 | 2017-05-29 19:39:08 +0530 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace watchdog |
| 100 | } // namespace phosphor |