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> |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 6 | #include <xyz/openbmc_project/State/Watchdog/server.hpp> |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 7 | |
| 8 | #include "host-ipmid/ipmid-api.h" |
| 9 | #include "utils.hpp" |
| 10 | |
| 11 | using sdbusplus::message::variant_ns::get; |
| 12 | using sdbusplus::message::variant_ns::variant; |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 13 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
| 14 | using sdbusplus::xyz::openbmc_project::State::server::Watchdog; |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 15 | |
| 16 | static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0"; |
| 17 | static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog"; |
| 18 | static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties"; |
| 19 | |
| 20 | WatchdogService::WatchdogService() |
| 21 | : bus(ipmid_get_sd_bus_connection()), |
| 22 | wd_service(ipmi::getService(bus, wd_intf, wd_path)) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | WatchdogService::Properties WatchdogService::getProperties() |
| 27 | { |
| 28 | auto request = bus.new_method_call(wd_service.c_str(), wd_path, |
| 29 | prop_intf, "GetAll"); |
| 30 | request.append(wd_intf); |
| 31 | auto response = bus.call(request); |
| 32 | if (response.is_method_error()) |
| 33 | { |
| 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; |
William A. Kennington III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 40 | wd_prop.initialized = get<bool>(properties.at("Initialized")); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 41 | wd_prop.enabled = get<bool>(properties.at("Enabled")); |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 42 | wd_prop.expireAction = Watchdog::convertActionFromString( |
| 43 | get<std::string>(properties.at("ExpireAction"))); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 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 = bus.new_method_call(wd_service.c_str(), wd_path, |
| 53 | prop_intf, "Set"); |
| 54 | request.append(wd_intf, key, variant<T>(val)); |
| 55 | auto response = bus.call(request); |
| 56 | if (response.is_method_error()) |
| 57 | { |
| 58 | throw std::runtime_error(std::string("Failed to set property: ") + key); |
| 59 | } |
| 60 | } |
| 61 | |
William A. Kennington III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 62 | void WatchdogService::setInitialized(bool initialized) |
| 63 | { |
| 64 | setProperty("Initialized", initialized); |
| 65 | } |
| 66 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 67 | void WatchdogService::setEnabled(bool enabled) |
| 68 | { |
| 69 | setProperty("Enabled", enabled); |
| 70 | } |
| 71 | |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 72 | void WatchdogService::setExpireAction(Action expireAction) |
| 73 | { |
| 74 | setProperty("ExpireAction", convertForMessage(expireAction)); |
| 75 | } |
| 76 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 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 | } |