blob: e03f96583189e534d42bb220edeedddc7d185ae8 [file] [log] [blame]
Jayanth Othayothc4831812021-06-08 01:33:40 -05001#include "util.hpp"
2
Jayanth Othayothc4831812021-06-08 01:33:40 -05003#include <phosphor-logging/elog.hpp>
4
Jayanth Othayothe0dd7af2023-09-13 09:03:30 +00005#include <format>
Jayanth Othayoth87441232022-09-08 23:47:11 -05006#include <sstream>
7#include <variant>
Jayanth Othayothc4831812021-06-08 01:33:40 -05008#include <vector>
9
10namespace openpower
11{
12namespace util
13{
14using namespace phosphor::logging;
15
Patrick Williamsaaea6862022-07-22 19:26:54 -050016std::string getService(sdbusplus::bus_t& bus, const std::string& objectPath,
Jayanth Othayothc4831812021-06-08 01:33:40 -050017 const std::string& interface)
18{
19 constexpr auto mapperBusBame = "xyz.openbmc_project.ObjectMapper";
20 constexpr auto mapperObjectPath = "/xyz/openbmc_project/object_mapper";
21 constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
22 std::vector<std::pair<std::string, std::vector<std::string>>> response;
23 auto method = bus.new_method_call(mapperBusBame, mapperObjectPath,
24 mapperInterface, "GetObject");
25 method.append(objectPath, std::vector<std::string>({interface}));
26 try
27 {
28 auto reply = bus.call(method);
29 reply.read(response);
30 }
Patrick Williamsaaea6862022-07-22 19:26:54 -050031 catch (const sdbusplus::exception_t& e)
Jayanth Othayothc4831812021-06-08 01:33:40 -050032 {
Jayanth Othayothe0dd7af2023-09-13 09:03:30 +000033 log<level::ERR>(std::format("D-Bus call exception OBJPATH={}"
Jayanth Othayothc4831812021-06-08 01:33:40 -050034 "INTERFACE={} EXCEPTION={}",
Jayanth Othayoth4b062592022-06-30 01:43:40 -050035 objectPath, interface, e.what())
Jayanth Othayothc4831812021-06-08 01:33:40 -050036 .c_str());
37
38 throw std::runtime_error("Service name is not found");
39 }
40
41 if (response.empty())
42 {
43 throw std::runtime_error("Service name response is empty");
44 }
45 return response.begin()->first;
46}
Marri Devender Rao4d5b5bf2022-05-23 09:23:31 -050047
48bool isHostPoweringOff()
49{
50 try
51 {
52 constexpr auto object = "/xyz/openbmc_project/state/host0";
53 constexpr auto service = "xyz.openbmc_project.State.Host";
54 constexpr auto interface = "xyz.openbmc_project.State.Host";
55 constexpr auto property = "CurrentHostState";
56 auto bus = sdbusplus::bus::new_default();
57
58 std::variant<std::string> retval;
59 auto properties = bus.new_method_call(
60 service, object, "org.freedesktop.DBus.Properties", "Get");
61 properties.append(interface);
62 properties.append(property);
63 auto result = bus.call(properties);
64 result.read(retval);
65
66 const std::string* state = std::get_if<std::string>(&retval);
67 if (state == nullptr)
68 {
Jayanth Othayothe0dd7af2023-09-13 09:03:30 +000069 std::string err = std::format(
Marri Devender Rao4d5b5bf2022-05-23 09:23:31 -050070 "CurrentHostState property is not set ({})", object);
71 log<level::ERR>(err.c_str());
72 return false;
73 }
74 if ((*state == "xyz.openbmc_project.State.Host.HostState."
75 "TransitioningToOff") ||
76 (*state == "xyz.openbmc_project.State.Host.HostState."
77 "Off") ||
78 (*state == "xyz.openbmc_project.State.Host.HostState."
79 "Quiesced"))
80 {
81 return true;
82 }
83 }
84 catch (const std::exception& ex)
85 {
86 log<level::ERR>(
Jayanth Othayothe0dd7af2023-09-13 09:03:30 +000087 std::format("Failed to read CurrentHostState property ({})",
Marri Devender Rao4d5b5bf2022-05-23 09:23:31 -050088 ex.what())
89 .c_str());
90 }
91 return false;
92}
Jayanth Othayoth87441232022-09-08 23:47:11 -050093
94std::string getChassisPowerState()
95{
96 std::string powerState{};
97 try
98 {
99 auto bus = sdbusplus::bus::new_default();
100 auto properties =
101 bus.new_method_call("xyz.openbmc_project.State.Chassis",
102 "/xyz/openbmc_project/state/chassis0",
103 "org.freedesktop.DBus.Properties", "Get");
104 properties.append("xyz.openbmc_project.State.Chassis");
105 properties.append("CurrentPowerState");
106 auto result = bus.call(properties);
107 std::variant<std::string> val;
108 result.read(val);
109 if (auto pVal = std::get_if<std::string>(&val))
110 {
111 powerState = *pVal;
112 }
113 }
114
115 catch (const std::exception& ex)
116 {
117 log<level::ERR>(
Jayanth Othayothe0dd7af2023-09-13 09:03:30 +0000118 std::format("Failed to read CurrentPowerState property ({})",
Jayanth Othayoth87441232022-09-08 23:47:11 -0500119 ex.what())
120 .c_str());
121 }
122
Jayanth Othayothe0dd7af2023-09-13 09:03:30 +0000123 log<level::DEBUG>(std::format("Power state is: {} ", powerState).c_str());
Jayanth Othayoth87441232022-09-08 23:47:11 -0500124
125 return powerState;
126}
127
Jayanth Othayothc4831812021-06-08 01:33:40 -0500128} // namespace util
129} // namespace openpower