William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame^] | 1 | #include "watchdog_service.hpp" |
| 2 | |
| 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <sdbusplus/message.hpp> |
| 5 | #include <string> |
| 6 | |
| 7 | #include "host-ipmid/ipmid-api.h" |
| 8 | #include "utils.hpp" |
| 9 | |
| 10 | using sdbusplus::message::variant_ns::get; |
| 11 | using sdbusplus::message::variant_ns::variant; |
| 12 | |
| 13 | static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0"; |
| 14 | static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog"; |
| 15 | static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties"; |
| 16 | |
| 17 | WatchdogService::WatchdogService() |
| 18 | : bus(ipmid_get_sd_bus_connection()), |
| 19 | wd_service(ipmi::getService(bus, wd_intf, wd_path)) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | WatchdogService::Properties WatchdogService::getProperties() |
| 24 | { |
| 25 | auto request = bus.new_method_call(wd_service.c_str(), wd_path, |
| 26 | prop_intf, "GetAll"); |
| 27 | request.append(wd_intf); |
| 28 | auto response = bus.call(request); |
| 29 | if (response.is_method_error()) |
| 30 | { |
| 31 | throw std::runtime_error("Failed to get watchdog properties"); |
| 32 | } |
| 33 | |
| 34 | std::map<std::string, variant<bool, uint64_t, std::string>> properties; |
| 35 | response.read(properties); |
| 36 | Properties wd_prop; |
| 37 | wd_prop.enabled = get<bool>(properties.at("Enabled")); |
| 38 | wd_prop.interval = get<uint64_t>(properties.at("Interval")); |
| 39 | wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining")); |
| 40 | return wd_prop; |
| 41 | } |
| 42 | |
| 43 | template <typename T> |
| 44 | void WatchdogService::setProperty(const std::string& key, const T& val) |
| 45 | { |
| 46 | auto request = bus.new_method_call(wd_service.c_str(), wd_path, |
| 47 | prop_intf, "Set"); |
| 48 | request.append(wd_intf, key, variant<T>(val)); |
| 49 | auto response = bus.call(request); |
| 50 | if (response.is_method_error()) |
| 51 | { |
| 52 | throw std::runtime_error(std::string("Failed to set property: ") + key); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void WatchdogService::setEnabled(bool enabled) |
| 57 | { |
| 58 | setProperty("Enabled", enabled); |
| 59 | } |
| 60 | |
| 61 | void WatchdogService::setInterval(uint64_t interval) |
| 62 | { |
| 63 | setProperty("Interval", interval); |
| 64 | } |
| 65 | |
| 66 | void WatchdogService::setTimeRemaining(uint64_t timeRemaining) |
| 67 | { |
| 68 | setProperty("TimeRemaining", timeRemaining); |
| 69 | } |