William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 1 | #include "watchdog_service.hpp" |
| 2 | |
Vernon Mauery | e08fbff | 2019-04-03 09:19:34 -0700 | [diff] [blame] | 3 | #include <ipmid/api.hpp> |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame] | 4 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 5 | #include <phosphor-logging/elog.hpp> |
George Liu | 05e9387 | 2024-07-17 14:48:14 +0800 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/message.hpp> |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame] | 9 | #include <xyz/openbmc_project/Common/error.hpp> |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 10 | #include <xyz/openbmc_project/State/Watchdog/server.hpp> |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 11 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 12 | #include <exception> |
| 13 | #include <stdexcept> |
| 14 | #include <string> |
| 15 | |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame] | 16 | using phosphor::logging::elog; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 17 | using phosphor::logging::entry; |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame] | 18 | using phosphor::logging::level; |
| 19 | using phosphor::logging::log; |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 20 | using sdbusplus::common::xyz::openbmc_project::state::convertForMessage; |
| 21 | using sdbusplus::error::xyz::openbmc_project::common::InternalFailure; |
| 22 | using sdbusplus::server::xyz::openbmc_project::state::Watchdog; |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 23 | |
| 24 | static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0"; |
| 25 | static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog"; |
| 26 | static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties"; |
| 27 | |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 28 | ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path); |
| 29 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 30 | WatchdogService::WatchdogService() : bus(ipmid_get_sd_bus_connection()) {} |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 31 | |
William A. Kennington III | 4b017a9 | 2018-04-27 14:31:08 -0700 | [diff] [blame] | 32 | void WatchdogService::resetTimeRemaining(bool enableWatchdog) |
| 33 | { |
| 34 | bool wasValid = wd_service.isValid(bus); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 35 | auto request = wd_service.newMethodCall(bus, wd_intf, "ResetTimeRemaining"); |
William A. Kennington III | 4b017a9 | 2018-04-27 14:31:08 -0700 | [diff] [blame] | 36 | request.append(enableWatchdog); |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 37 | try |
| 38 | { |
| 39 | auto response = bus.call(request); |
| 40 | } |
| 41 | catch (const std::exception& e) |
William A. Kennington III | 4b017a9 | 2018-04-27 14:31:08 -0700 | [diff] [blame] | 42 | { |
| 43 | wd_service.invalidate(); |
| 44 | if (wasValid) |
| 45 | { |
| 46 | // Retry the request once in case the cached service was stale |
| 47 | return resetTimeRemaining(enableWatchdog); |
| 48 | } |
George Liu | 05e9387 | 2024-07-17 14:48:14 +0800 | [diff] [blame] | 49 | lg2::error("WatchdogService: Method error resetting time remaining, " |
| 50 | "ENABLE_WATCHDOG: {ENABLE_WATCHDOG}, ERROR: {ERROR}", |
| 51 | "ENABLE_WATCHDOG", enableWatchdog, "ERROR", e); |
William A. Kennington III | 4b017a9 | 2018-04-27 14:31:08 -0700 | [diff] [blame] | 52 | elog<InternalFailure>(); |
| 53 | } |
| 54 | } |
| 55 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 56 | WatchdogService::Properties WatchdogService::getProperties() |
| 57 | { |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 58 | bool wasValid = wd_service.isValid(bus); |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 59 | auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll"); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 60 | request.append(wd_intf); |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 61 | |
| 62 | std::map<std::string, std::variant<bool, uint64_t, std::string>> properties; |
| 63 | try |
| 64 | { |
| 65 | auto response = bus.call(request); |
| 66 | response.read(properties); |
| 67 | } |
| 68 | catch (const std::exception& e) |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 69 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 70 | wd_service.invalidate(); |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 71 | if (wasValid) |
| 72 | { |
| 73 | // Retry the request once in case the cached service was stale |
| 74 | return getProperties(); |
| 75 | } |
George Liu | 05e9387 | 2024-07-17 14:48:14 +0800 | [diff] [blame] | 76 | lg2::error("WatchdogService: Method error getting properties: {ERROR}", |
| 77 | "ERROR", e); |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame] | 78 | elog<InternalFailure>(); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 79 | } |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 80 | |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 81 | try |
| 82 | { |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 83 | Properties wd_prop; |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 84 | wd_prop.initialized = std::get<bool>(properties.at("Initialized")); |
| 85 | wd_prop.enabled = std::get<bool>(properties.at("Enabled")); |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 86 | wd_prop.expireAction = Watchdog::convertActionFromString( |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 87 | std::get<std::string>(properties.at("ExpireAction"))); |
Yong Li | 118907e | 2019-01-11 17:36:17 +0800 | [diff] [blame] | 88 | wd_prop.timerUse = Watchdog::convertTimerUseFromString( |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 89 | std::get<std::string>(properties.at("CurrentTimerUse"))); |
Yong Li | 4dd71af | 2019-09-29 14:18:07 +0800 | [diff] [blame] | 90 | wd_prop.expiredTimerUse = Watchdog::convertTimerUseFromString( |
| 91 | std::get<std::string>(properties.at("ExpiredTimerUse"))); |
Yong Li | 118907e | 2019-01-11 17:36:17 +0800 | [diff] [blame] | 92 | |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 93 | wd_prop.interval = std::get<uint64_t>(properties.at("Interval")); |
| 94 | wd_prop.timeRemaining = |
| 95 | std::get<uint64_t>(properties.at("TimeRemaining")); |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 96 | return wd_prop; |
| 97 | } |
| 98 | catch (const std::exception& e) |
| 99 | { |
George Liu | 05e9387 | 2024-07-17 14:48:14 +0800 | [diff] [blame] | 100 | lg2::error("WatchdogService: Decode error in get properties: {ERROR}", |
| 101 | "ERROR", e); |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 102 | elog<InternalFailure>(); |
| 103 | } |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 104 | |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 105 | // Needed instead of elog<InternalFailure>() since the compiler can't |
| 106 | // deduce the that elog<>() always throws |
| 107 | throw std::runtime_error( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 108 | "WatchdogService: Should not reach end of getProperties"); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | template <typename T> |
William A. Kennington III | 2ecf512 | 2018-04-27 14:31:51 -0700 | [diff] [blame] | 112 | T WatchdogService::getProperty(const std::string& key) |
| 113 | { |
| 114 | bool wasValid = wd_service.isValid(bus); |
| 115 | auto request = wd_service.newMethodCall(bus, prop_intf, "Get"); |
| 116 | request.append(wd_intf, key); |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 117 | try |
| 118 | { |
| 119 | auto response = bus.call(request); |
| 120 | std::variant<T> value; |
| 121 | response.read(value); |
| 122 | return std::get<T>(value); |
| 123 | } |
| 124 | catch (const std::exception& e) |
William A. Kennington III | 2ecf512 | 2018-04-27 14:31:51 -0700 | [diff] [blame] | 125 | { |
| 126 | wd_service.invalidate(); |
| 127 | if (wasValid) |
| 128 | { |
| 129 | // Retry the request once in case the cached service was stale |
| 130 | return getProperty<T>(key); |
| 131 | } |
George Liu | 05e9387 | 2024-07-17 14:48:14 +0800 | [diff] [blame] | 132 | lg2::error("WatchdogService: Method error getting {PROPERTY}: {ERROR}", |
| 133 | "PROPERTY", key, "ERROR", e); |
William A. Kennington III | 1469f8a | 2018-05-15 14:40:59 -0700 | [diff] [blame] | 134 | elog<InternalFailure>(); |
| 135 | } |
| 136 | |
| 137 | // Needed instead of elog<InternalFailure>() since the compiler can't |
| 138 | // deduce the that elog<>() always throws |
| 139 | throw std::runtime_error( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 140 | "WatchdogService: Should not reach end of getProperty"); |
William A. Kennington III | 2ecf512 | 2018-04-27 14:31:51 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | template <typename T> |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 144 | void WatchdogService::setProperty(const std::string& key, const T& val) |
| 145 | { |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 146 | bool wasValid = wd_service.isValid(bus); |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 147 | auto request = wd_service.newMethodCall(bus, prop_intf, "Set"); |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 148 | request.append(wd_intf, key, std::variant<T>(val)); |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 149 | try |
| 150 | { |
| 151 | auto response = bus.call(request); |
| 152 | } |
| 153 | catch (const std::exception& e) |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 154 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 155 | wd_service.invalidate(); |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 156 | if (wasValid) |
| 157 | { |
| 158 | // Retry the request once in case the cached service was stale |
Chen,Yugang | 0e862fa | 2019-09-06 11:03:05 +0800 | [diff] [blame] | 159 | setProperty(key, val); |
| 160 | return; |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 161 | } |
George Liu | 05e9387 | 2024-07-17 14:48:14 +0800 | [diff] [blame] | 162 | lg2::error("WatchdogService: Method error setting {PROPERTY}: {ERROR}", |
| 163 | "PROPERTY", key, "ERROR", e); |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame] | 164 | elog<InternalFailure>(); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
William A. Kennington III | 2ecf512 | 2018-04-27 14:31:51 -0700 | [diff] [blame] | 168 | bool WatchdogService::getInitialized() |
| 169 | { |
| 170 | return getProperty<bool>("Initialized"); |
| 171 | } |
| 172 | |
William A. Kennington III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 173 | void WatchdogService::setInitialized(bool initialized) |
| 174 | { |
| 175 | setProperty("Initialized", initialized); |
| 176 | } |
| 177 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 178 | void WatchdogService::setEnabled(bool enabled) |
| 179 | { |
| 180 | setProperty("Enabled", enabled); |
| 181 | } |
| 182 | |
Tim Chao | 65362f4 | 2023-11-14 14:47:25 +0800 | [diff] [blame] | 183 | void WatchdogService::setLogTimeout(bool LogTimeout) |
| 184 | { |
| 185 | setProperty("LogTimeout", LogTimeout); |
| 186 | } |
| 187 | |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 188 | void WatchdogService::setExpireAction(Action expireAction) |
| 189 | { |
| 190 | setProperty("ExpireAction", convertForMessage(expireAction)); |
| 191 | } |
| 192 | |
Yong Li | 118907e | 2019-01-11 17:36:17 +0800 | [diff] [blame] | 193 | void WatchdogService::setTimerUse(TimerUse timerUse) |
| 194 | { |
| 195 | setProperty("CurrentTimerUse", convertForMessage(timerUse)); |
| 196 | } |
| 197 | |
Deepak Kumar Sahu | cfae948 | 2019-05-20 14:58:58 +0000 | [diff] [blame] | 198 | void WatchdogService::setExpiredTimerUse(TimerUse timerUse) |
| 199 | { |
| 200 | setProperty("ExpiredTimerUse", convertForMessage(timerUse)); |
| 201 | } |
| 202 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 203 | void WatchdogService::setInterval(uint64_t interval) |
| 204 | { |
| 205 | setProperty("Interval", interval); |
| 206 | } |