blob: 09b700fc1799e1724d0c384ba68d4646b5fd8099 [file] [log] [blame]
William A. Kennington III52575252018-02-09 15:54:56 -08001#include "watchdog_service.hpp"
2
Vernon Mauerye08fbff2019-04-03 09:19:34 -07003#include <ipmid/api.hpp>
William A. Kennington IIId5410272018-05-10 11:17:58 -07004#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -07005#include <phosphor-logging/elog.hpp>
William A. Kennington IIId5410272018-05-10 11:17:58 -07006#include <phosphor-logging/log.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -08007#include <sdbusplus/bus.hpp>
8#include <sdbusplus/message.hpp>
William A. Kennington IIId5410272018-05-10 11:17:58 -07009#include <xyz/openbmc_project/Common/error.hpp>
William A. Kennington IIIb638de22018-02-09 16:12:53 -080010#include <xyz/openbmc_project/State/Watchdog/server.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -080011
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050012#include <exception>
13#include <stdexcept>
14#include <string>
15
William A. Kennington IIId5410272018-05-10 11:17:58 -070016using phosphor::logging::elog;
Patrick Venture0b02be92018-08-31 11:55:55 -070017using phosphor::logging::entry;
William A. Kennington IIId5410272018-05-10 11:17:58 -070018using phosphor::logging::level;
19using phosphor::logging::log;
William A. Kennington IIId5410272018-05-10 11:17:58 -070020using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
William A. Kennington IIIb638de22018-02-09 16:12:53 -080021using sdbusplus::xyz::openbmc_project::State::server::convertForMessage;
22using sdbusplus::xyz::openbmc_project::State::server::Watchdog;
William A. Kennington III52575252018-02-09 15:54:56 -080023
24static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0";
25static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog";
26static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties";
27
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070028ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path);
29
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050030WatchdogService::WatchdogService() : bus(ipmid_get_sd_bus_connection()) {}
William A. Kennington III52575252018-02-09 15:54:56 -080031
William A. Kennington III4b017a92018-04-27 14:31:08 -070032void WatchdogService::resetTimeRemaining(bool enableWatchdog)
33{
34 bool wasValid = wd_service.isValid(bus);
Patrick Venture0b02be92018-08-31 11:55:55 -070035 auto request = wd_service.newMethodCall(bus, wd_intf, "ResetTimeRemaining");
William A. Kennington III4b017a92018-04-27 14:31:08 -070036 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 Venture0b02be92018-08-31 11:55:55 -070047 "WatchdogService: Method error resetting time remaining",
48 entry("ENABLE_WATCHDOG=%d", !!enableWatchdog));
William A. Kennington III4b017a92018-04-27 14:31:08 -070049 elog<InternalFailure>();
50 }
51}
52
William A. Kennington III52575252018-02-09 15:54:56 -080053WatchdogService::Properties WatchdogService::getProperties()
54{
William A. Kennington IIIe1628492018-05-11 16:17:28 -070055 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070056 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll");
William A. Kennington III52575252018-02-09 15:54:56 -080057 request.append(wd_intf);
58 auto response = bus.call(request);
59 if (response.is_method_error())
60 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070061 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070062 if (wasValid)
63 {
64 // Retry the request once in case the cached service was stale
65 return getProperties();
66 }
William A. Kennington IIId5410272018-05-10 11:17:58 -070067 log<level::ERR>("WatchdogService: Method error getting properties");
68 elog<InternalFailure>();
William A. Kennington III52575252018-02-09 15:54:56 -080069 }
William A. Kennington III1469f8a2018-05-15 14:40:59 -070070 try
71 {
Vernon Maueryf442e112019-04-09 11:44:36 -070072 std::map<std::string, std::variant<bool, uint64_t, std::string>>
73 properties;
William A. Kennington III1469f8a2018-05-15 14:40:59 -070074 response.read(properties);
75 Properties wd_prop;
Vernon Maueryf442e112019-04-09 11:44:36 -070076 wd_prop.initialized = std::get<bool>(properties.at("Initialized"));
77 wd_prop.enabled = std::get<bool>(properties.at("Enabled"));
William A. Kennington III1469f8a2018-05-15 14:40:59 -070078 wd_prop.expireAction = Watchdog::convertActionFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -070079 std::get<std::string>(properties.at("ExpireAction")));
Yong Li118907e2019-01-11 17:36:17 +080080 wd_prop.timerUse = Watchdog::convertTimerUseFromString(
Vernon Maueryf442e112019-04-09 11:44:36 -070081 std::get<std::string>(properties.at("CurrentTimerUse")));
Yong Li4dd71af2019-09-29 14:18:07 +080082 wd_prop.expiredTimerUse = Watchdog::convertTimerUseFromString(
83 std::get<std::string>(properties.at("ExpiredTimerUse")));
Yong Li118907e2019-01-11 17:36:17 +080084
Vernon Maueryf442e112019-04-09 11:44:36 -070085 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 III1469f8a2018-05-15 14:40:59 -070088 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 III52575252018-02-09 15:54:56 -080097
William A. Kennington III1469f8a2018-05-15 14:40:59 -070098 // Needed instead of elog<InternalFailure>() since the compiler can't
99 // deduce the that elog<>() always throws
100 throw std::runtime_error(
Patrick Venture0b02be92018-08-31 11:55:55 -0700101 "WatchdogService: Should not reach end of getProperties");
William A. Kennington III52575252018-02-09 15:54:56 -0800102}
103
104template <typename T>
William A. Kennington III2ecf5122018-04-27 14:31:51 -0700105T 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 III1469f8a2018-05-15 14:40:59 -0700123 try
124 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700125 std::variant<T> value;
William A. Kennington III1469f8a2018-05-15 14:40:59 -0700126 response.read(value);
Vernon Maueryf442e112019-04-09 11:44:36 -0700127 return std::get<T>(value);
William A. Kennington III1469f8a2018-05-15 14:40:59 -0700128 }
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 Venture0b02be92018-08-31 11:55:55 -0700141 "WatchdogService: Should not reach end of getProperty");
William A. Kennington III2ecf5122018-04-27 14:31:51 -0700142}
143
144template <typename T>
William A. Kennington III52575252018-02-09 15:54:56 -0800145void WatchdogService::setProperty(const std::string& key, const T& val)
146{
William A. Kennington IIIe1628492018-05-11 16:17:28 -0700147 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -0700148 auto request = wd_service.newMethodCall(bus, prop_intf, "Set");
Vernon Maueryf442e112019-04-09 11:44:36 -0700149 request.append(wd_intf, key, std::variant<T>(val));
William A. Kennington III52575252018-02-09 15:54:56 -0800150 auto response = bus.call(request);
151 if (response.is_method_error())
152 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -0700153 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -0700154 if (wasValid)
155 {
156 // Retry the request once in case the cached service was stale
Chen,Yugang0e862fa2019-09-06 11:03:05 +0800157 setProperty(key, val);
158 return;
William A. Kennington IIIe1628492018-05-11 16:17:28 -0700159 }
William A. Kennington IIId5410272018-05-10 11:17:58 -0700160 log<level::ERR>("WatchdogService: Method error setting property",
161 entry("PROPERTY=%s", key.c_str()));
162 elog<InternalFailure>();
William A. Kennington III52575252018-02-09 15:54:56 -0800163 }
164}
165
William A. Kennington III2ecf5122018-04-27 14:31:51 -0700166bool WatchdogService::getInitialized()
167{
168 return getProperty<bool>("Initialized");
169}
170
William A. Kennington IIIde14a022018-02-09 16:11:18 -0800171void WatchdogService::setInitialized(bool initialized)
172{
173 setProperty("Initialized", initialized);
174}
175
William A. Kennington III52575252018-02-09 15:54:56 -0800176void WatchdogService::setEnabled(bool enabled)
177{
178 setProperty("Enabled", enabled);
179}
180
William A. Kennington IIIb638de22018-02-09 16:12:53 -0800181void WatchdogService::setExpireAction(Action expireAction)
182{
183 setProperty("ExpireAction", convertForMessage(expireAction));
184}
185
Yong Li118907e2019-01-11 17:36:17 +0800186void WatchdogService::setTimerUse(TimerUse timerUse)
187{
188 setProperty("CurrentTimerUse", convertForMessage(timerUse));
189}
190
Deepak Kumar Sahucfae9482019-05-20 14:58:58 +0000191void WatchdogService::setExpiredTimerUse(TimerUse timerUse)
192{
193 setProperty("ExpiredTimerUse", convertForMessage(timerUse));
194}
195
William A. Kennington III52575252018-02-09 15:54:56 -0800196void WatchdogService::setInterval(uint64_t interval)
197{
198 setProperty("Interval", interval);
199}