Jayanth Othayoth | c483181 | 2021-06-08 01:33:40 -0500 | [diff] [blame] | 1 | #include "util.hpp" |
| 2 | |
| 3 | #include <fmt/format.h> |
| 4 | |
| 5 | #include <phosphor-logging/elog.hpp> |
| 6 | |
Jayanth Othayoth | 8744123 | 2022-09-08 23:47:11 -0500 | [diff] [blame^] | 7 | #include <sstream> |
| 8 | #include <variant> |
Jayanth Othayoth | c483181 | 2021-06-08 01:33:40 -0500 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | namespace openpower |
| 12 | { |
| 13 | namespace util |
| 14 | { |
| 15 | using namespace phosphor::logging; |
| 16 | |
Patrick Williams | aaea686 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 17 | std::string getService(sdbusplus::bus_t& bus, const std::string& objectPath, |
Jayanth Othayoth | c483181 | 2021-06-08 01:33:40 -0500 | [diff] [blame] | 18 | const std::string& interface) |
| 19 | { |
| 20 | constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper"; |
| 21 | constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper"; |
| 22 | constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper"; |
| 23 | std::vector<std::pair<std::string, std::vector<std::string>>> response; |
| 24 | auto method = bus.new_method_call(mapperBusBame, mapperObjectPath, |
| 25 | mapperInterface, "GetObject"); |
| 26 | method.append(objectPath, std::vector<std::string>({interface})); |
| 27 | try |
| 28 | { |
| 29 | auto reply = bus.call(method); |
| 30 | reply.read(response); |
| 31 | } |
Patrick Williams | aaea686 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 32 | catch (const sdbusplus::exception_t& e) |
Jayanth Othayoth | c483181 | 2021-06-08 01:33:40 -0500 | [diff] [blame] | 33 | { |
| 34 | log<level::ERR>(fmt::format("D-Bus call exception OBJPATH={}" |
| 35 | "INTERFACE={} EXCEPTION={}", |
Jayanth Othayoth | 4b06259 | 2022-06-30 01:43:40 -0500 | [diff] [blame] | 36 | objectPath, interface, e.what()) |
Jayanth Othayoth | c483181 | 2021-06-08 01:33:40 -0500 | [diff] [blame] | 37 | .c_str()); |
| 38 | |
| 39 | throw std::runtime_error("Service name is not found"); |
| 40 | } |
| 41 | |
| 42 | if (response.empty()) |
| 43 | { |
| 44 | throw std::runtime_error("Service name response is empty"); |
| 45 | } |
| 46 | return response.begin()->first; |
| 47 | } |
Marri Devender Rao | 4d5b5bf | 2022-05-23 09:23:31 -0500 | [diff] [blame] | 48 | |
| 49 | bool isHostPoweringOff() |
| 50 | { |
| 51 | try |
| 52 | { |
| 53 | constexpr auto object = "/xyz/openbmc_project/state/host0"; |
| 54 | constexpr auto service = "xyz.openbmc_project.State.Host"; |
| 55 | constexpr auto interface = "xyz.openbmc_project.State.Host"; |
| 56 | constexpr auto property = "CurrentHostState"; |
| 57 | auto bus = sdbusplus::bus::new_default(); |
| 58 | |
| 59 | std::variant<std::string> retval; |
| 60 | auto properties = bus.new_method_call( |
| 61 | service, object, "org.freedesktop.DBus.Properties", "Get"); |
| 62 | properties.append(interface); |
| 63 | properties.append(property); |
| 64 | auto result = bus.call(properties); |
| 65 | result.read(retval); |
| 66 | |
| 67 | const std::string* state = std::get_if<std::string>(&retval); |
| 68 | if (state == nullptr) |
| 69 | { |
| 70 | std::string err = fmt::format( |
| 71 | "CurrentHostState property is not set ({})", object); |
| 72 | log<level::ERR>(err.c_str()); |
| 73 | return false; |
| 74 | } |
| 75 | if ((*state == "xyz.openbmc_project.State.Host.HostState." |
| 76 | "TransitioningToOff") || |
| 77 | (*state == "xyz.openbmc_project.State.Host.HostState." |
| 78 | "Off") || |
| 79 | (*state == "xyz.openbmc_project.State.Host.HostState." |
| 80 | "Quiesced")) |
| 81 | { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | catch (const std::exception& ex) |
| 86 | { |
| 87 | log<level::ERR>( |
| 88 | fmt::format("Failed to read CurrentHostState property ({})", |
| 89 | ex.what()) |
| 90 | .c_str()); |
| 91 | } |
| 92 | return false; |
| 93 | } |
Jayanth Othayoth | 8744123 | 2022-09-08 23:47:11 -0500 | [diff] [blame^] | 94 | |
| 95 | std::string getChassisPowerState() |
| 96 | { |
| 97 | std::string powerState{}; |
| 98 | try |
| 99 | { |
| 100 | auto bus = sdbusplus::bus::new_default(); |
| 101 | auto properties = |
| 102 | bus.new_method_call("xyz.openbmc_project.State.Chassis", |
| 103 | "/xyz/openbmc_project/state/chassis0", |
| 104 | "org.freedesktop.DBus.Properties", "Get"); |
| 105 | properties.append("xyz.openbmc_project.State.Chassis"); |
| 106 | properties.append("CurrentPowerState"); |
| 107 | auto result = bus.call(properties); |
| 108 | std::variant<std::string> val; |
| 109 | result.read(val); |
| 110 | if (auto pVal = std::get_if<std::string>(&val)) |
| 111 | { |
| 112 | powerState = *pVal; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | catch (const std::exception& ex) |
| 117 | { |
| 118 | log<level::ERR>( |
| 119 | fmt::format("Failed to read CurrentPowerState property ({})", |
| 120 | ex.what()) |
| 121 | .c_str()); |
| 122 | } |
| 123 | |
| 124 | log<level::DEBUG>(fmt::format("Power state is: {} ", powerState).c_str()); |
| 125 | |
| 126 | return powerState; |
| 127 | } |
| 128 | |
Jayanth Othayoth | c483181 | 2021-06-08 01:33:40 -0500 | [diff] [blame] | 129 | } // namespace util |
| 130 | } // namespace openpower |