1 #include "watchdog_service.hpp" 2 3 #include <sdbusplus/bus.hpp> 4 #include <sdbusplus/message.hpp> 5 #include <string> 6 #include <xyz/openbmc_project/State/Watchdog/server.hpp> 7 8 #include "host-ipmid/ipmid-api.h" 9 10 using sdbusplus::message::variant_ns::get; 11 using sdbusplus::message::variant_ns::variant; 12 using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; 13 using sdbusplus::xyz::openbmc_project::State::server::Watchdog; 14 15 static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0"; 16 static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog"; 17 static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties"; 18 19 ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path); 20 21 WatchdogService::WatchdogService() 22 : bus(ipmid_get_sd_bus_connection()) 23 { 24 } 25 26 WatchdogService::Properties WatchdogService::getProperties() 27 { 28 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll"); 29 request.append(wd_intf); 30 auto response = bus.call(request); 31 if (response.is_method_error()) 32 { 33 wd_service.invalidate(); 34 throw std::runtime_error("Failed to get watchdog properties"); 35 } 36 37 std::map<std::string, variant<bool, uint64_t, std::string>> properties; 38 response.read(properties); 39 Properties wd_prop; 40 wd_prop.initialized = get<bool>(properties.at("Initialized")); 41 wd_prop.enabled = get<bool>(properties.at("Enabled")); 42 wd_prop.expireAction = Watchdog::convertActionFromString( 43 get<std::string>(properties.at("ExpireAction"))); 44 wd_prop.interval = get<uint64_t>(properties.at("Interval")); 45 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining")); 46 return wd_prop; 47 } 48 49 template <typename T> 50 void WatchdogService::setProperty(const std::string& key, const T& val) 51 { 52 auto request = wd_service.newMethodCall(bus, prop_intf, "Set"); 53 request.append(wd_intf, key, variant<T>(val)); 54 auto response = bus.call(request); 55 if (response.is_method_error()) 56 { 57 wd_service.invalidate(); 58 throw std::runtime_error(std::string("Failed to set property: ") + key); 59 } 60 } 61 62 void WatchdogService::setInitialized(bool initialized) 63 { 64 setProperty("Initialized", initialized); 65 } 66 67 void WatchdogService::setEnabled(bool enabled) 68 { 69 setProperty("Enabled", enabled); 70 } 71 72 void WatchdogService::setExpireAction(Action expireAction) 73 { 74 setProperty("ExpireAction", convertForMessage(expireAction)); 75 } 76 77 void WatchdogService::setInterval(uint64_t interval) 78 { 79 setProperty("Interval", interval); 80 } 81 82 void WatchdogService::setTimeRemaining(uint64_t timeRemaining) 83 { 84 setProperty("TimeRemaining", timeRemaining); 85 } 86