1 #include "watchdog_service.hpp" 2 3 #include <exception> 4 #include <ipmid/api.hpp> 5 #include <phosphor-logging/elog-errors.hpp> 6 #include <phosphor-logging/elog.hpp> 7 #include <phosphor-logging/log.hpp> 8 #include <sdbusplus/bus.hpp> 9 #include <sdbusplus/message.hpp> 10 #include <stdexcept> 11 #include <string> 12 #include <xyz/openbmc_project/Common/error.hpp> 13 #include <xyz/openbmc_project/State/Watchdog/server.hpp> 14 15 using phosphor::logging::elog; 16 using phosphor::logging::entry; 17 using phosphor::logging::level; 18 using phosphor::logging::log; 19 using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 20 using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; 21 using sdbusplus::xyz::openbmc_project::State::server::Watchdog; 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 27 ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path); 28 29 WatchdogService::WatchdogService() : bus(ipmid_get_sd_bus_connection()) 30 { 31 } 32 33 void WatchdogService::resetTimeRemaining(bool enableWatchdog) 34 { 35 bool wasValid = wd_service.isValid(bus); 36 auto request = wd_service.newMethodCall(bus, wd_intf, "ResetTimeRemaining"); 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>( 48 "WatchdogService: Method error resetting time remaining", 49 entry("ENABLE_WATCHDOG=%d", !!enableWatchdog)); 50 elog<InternalFailure>(); 51 } 52 } 53 54 WatchdogService::Properties WatchdogService::getProperties() 55 { 56 bool wasValid = wd_service.isValid(bus); 57 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll"); 58 request.append(wd_intf); 59 auto response = bus.call(request); 60 if (response.is_method_error()) 61 { 62 wd_service.invalidate(); 63 if (wasValid) 64 { 65 // Retry the request once in case the cached service was stale 66 return getProperties(); 67 } 68 log<level::ERR>("WatchdogService: Method error getting properties"); 69 elog<InternalFailure>(); 70 } 71 try 72 { 73 std::map<std::string, std::variant<bool, uint64_t, std::string>> 74 properties; 75 response.read(properties); 76 Properties wd_prop; 77 wd_prop.initialized = std::get<bool>(properties.at("Initialized")); 78 wd_prop.enabled = std::get<bool>(properties.at("Enabled")); 79 wd_prop.expireAction = Watchdog::convertActionFromString( 80 std::get<std::string>(properties.at("ExpireAction"))); 81 wd_prop.timerUse = Watchdog::convertTimerUseFromString( 82 std::get<std::string>(properties.at("CurrentTimerUse"))); 83 84 wd_prop.interval = std::get<uint64_t>(properties.at("Interval")); 85 wd_prop.timeRemaining = 86 std::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 } 96 97 // Needed instead of elog<InternalFailure>() since the compiler can't 98 // deduce the that elog<>() always throws 99 throw std::runtime_error( 100 "WatchdogService: Should not reach end of getProperties"); 101 } 102 103 template <typename T> 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 } 122 try 123 { 124 std::variant<T> value; 125 response.read(value); 126 return std::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( 140 "WatchdogService: Should not reach end of getProperty"); 141 } 142 143 template <typename T> 144 void WatchdogService::setProperty(const std::string& key, const T& val) 145 { 146 bool wasValid = wd_service.isValid(bus); 147 auto request = wd_service.newMethodCall(bus, prop_intf, "Set"); 148 request.append(wd_intf, key, std::variant<T>(val)); 149 auto response = bus.call(request); 150 if (response.is_method_error()) 151 { 152 wd_service.invalidate(); 153 if (wasValid) 154 { 155 // Retry the request once in case the cached service was stale 156 setProperty(key, val); 157 return; 158 } 159 log<level::ERR>("WatchdogService: Method error setting property", 160 entry("PROPERTY=%s", key.c_str())); 161 elog<InternalFailure>(); 162 } 163 } 164 165 bool WatchdogService::getInitialized() 166 { 167 return getProperty<bool>("Initialized"); 168 } 169 170 void WatchdogService::setInitialized(bool initialized) 171 { 172 setProperty("Initialized", initialized); 173 } 174 175 void WatchdogService::setEnabled(bool enabled) 176 { 177 setProperty("Enabled", enabled); 178 } 179 180 void WatchdogService::setExpireAction(Action expireAction) 181 { 182 setProperty("ExpireAction", convertForMessage(expireAction)); 183 } 184 185 void WatchdogService::setTimerUse(TimerUse timerUse) 186 { 187 setProperty("CurrentTimerUse", convertForMessage(timerUse)); 188 } 189 190 void WatchdogService::setExpiredTimerUse(TimerUse timerUse) 191 { 192 setProperty("ExpiredTimerUse", convertForMessage(timerUse)); 193 } 194 195 void WatchdogService::setInterval(uint64_t interval) 196 { 197 setProperty("Interval", interval); 198 } 199 200 void WatchdogService::setTimeRemaining(uint64_t timeRemaining) 201 { 202 setProperty("TimeRemaining", timeRemaining); 203 } 204