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