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