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