blob: c95c092c4b0a2de1b69bf37c14b01adbb3d1c649 [file] [log] [blame]
William A. Kennington III52575252018-02-09 15:54:56 -08001#include "watchdog_service.hpp"
2
William A. Kennington IIId5410272018-05-10 11:17:58 -07003#include <phosphor-logging/elog.hpp>
4#include <phosphor-logging/elog-errors.hpp>
5#include <phosphor-logging/log.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -08006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/message.hpp>
8#include <string>
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
12#include "host-ipmid/ipmid-api.h"
William A. Kennington III52575252018-02-09 15:54:56 -080013
William A. Kennington IIId5410272018-05-10 11:17:58 -070014using phosphor::logging::entry;
15using phosphor::logging::elog;
16using phosphor::logging::level;
17using phosphor::logging::log;
William A. Kennington III52575252018-02-09 15:54:56 -080018using sdbusplus::message::variant_ns::get;
19using sdbusplus::message::variant_ns::variant;
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
William A. Kennington III52575252018-02-09 15:54:56 -080030WatchdogService::WatchdogService()
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070031 : bus(ipmid_get_sd_bus_connection())
William A. Kennington III52575252018-02-09 15:54:56 -080032{
33}
34
35WatchdogService::Properties WatchdogService::getProperties()
36{
William A. Kennington IIIe1628492018-05-11 16:17:28 -070037 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070038 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll");
William A. Kennington III52575252018-02-09 15:54:56 -080039 request.append(wd_intf);
40 auto response = bus.call(request);
41 if (response.is_method_error())
42 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070043 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070044 if (wasValid)
45 {
46 // Retry the request once in case the cached service was stale
47 return getProperties();
48 }
William A. Kennington IIId5410272018-05-10 11:17:58 -070049 log<level::ERR>("WatchdogService: Method error getting properties");
50 elog<InternalFailure>();
William A. Kennington III52575252018-02-09 15:54:56 -080051 }
52
53 std::map<std::string, variant<bool, uint64_t, std::string>> properties;
54 response.read(properties);
55 Properties wd_prop;
William A. Kennington IIIde14a022018-02-09 16:11:18 -080056 wd_prop.initialized = get<bool>(properties.at("Initialized"));
William A. Kennington III52575252018-02-09 15:54:56 -080057 wd_prop.enabled = get<bool>(properties.at("Enabled"));
William A. Kennington IIIb638de22018-02-09 16:12:53 -080058 wd_prop.expireAction = Watchdog::convertActionFromString(
59 get<std::string>(properties.at("ExpireAction")));
William A. Kennington III52575252018-02-09 15:54:56 -080060 wd_prop.interval = get<uint64_t>(properties.at("Interval"));
61 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
62 return wd_prop;
63}
64
65template <typename T>
66void WatchdogService::setProperty(const std::string& key, const T& val)
67{
William A. Kennington IIIe1628492018-05-11 16:17:28 -070068 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070069 auto request = wd_service.newMethodCall(bus, prop_intf, "Set");
William A. Kennington III52575252018-02-09 15:54:56 -080070 request.append(wd_intf, key, variant<T>(val));
71 auto response = bus.call(request);
72 if (response.is_method_error())
73 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070074 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070075 if (wasValid)
76 {
77 // Retry the request once in case the cached service was stale
78 return setProperty(key, val);
79 }
William A. Kennington IIId5410272018-05-10 11:17:58 -070080 log<level::ERR>("WatchdogService: Method error setting property",
81 entry("PROPERTY=%s", key.c_str()));
82 elog<InternalFailure>();
William A. Kennington III52575252018-02-09 15:54:56 -080083 }
84}
85
William A. Kennington IIIde14a022018-02-09 16:11:18 -080086void WatchdogService::setInitialized(bool initialized)
87{
88 setProperty("Initialized", initialized);
89}
90
William A. Kennington III52575252018-02-09 15:54:56 -080091void WatchdogService::setEnabled(bool enabled)
92{
93 setProperty("Enabled", enabled);
94}
95
William A. Kennington IIIb638de22018-02-09 16:12:53 -080096void WatchdogService::setExpireAction(Action expireAction)
97{
98 setProperty("ExpireAction", convertForMessage(expireAction));
99}
100
William A. Kennington III52575252018-02-09 15:54:56 -0800101void WatchdogService::setInterval(uint64_t interval)
102{
103 setProperty("Interval", interval);
104}
105
106void WatchdogService::setTimeRemaining(uint64_t timeRemaining)
107{
108 setProperty("TimeRemaining", timeRemaining);
109}