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