blob: aac2a00b04774d2b98692f29a935ead4e598975f [file] [log] [blame]
Jayanth Othayothc4831812021-06-08 01:33:40 -05001#include "util.hpp"
2
3#include <fmt/format.h>
4
5#include <phosphor-logging/elog.hpp>
6
Jayanth Othayoth87441232022-09-08 23:47:11 -05007#include <sstream>
8#include <variant>
Jayanth Othayothc4831812021-06-08 01:33:40 -05009#include <vector>
10
11namespace openpower
12{
13namespace util
14{
15using namespace phosphor::logging;
16
Patrick Williamsaaea6862022-07-22 19:26:54 -050017std::string getService(sdbusplus::bus_t& bus, const std::string& objectPath,
Jayanth Othayothc4831812021-06-08 01:33:40 -050018 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 Williamsaaea6862022-07-22 19:26:54 -050032 catch (const sdbusplus::exception_t& e)
Jayanth Othayothc4831812021-06-08 01:33:40 -050033 {
34 log<level::ERR>(fmt::format("D-Bus call exception OBJPATH={}"
35 "INTERFACE={} EXCEPTION={}",
Jayanth Othayoth4b062592022-06-30 01:43:40 -050036 objectPath, interface, e.what())
Jayanth Othayothc4831812021-06-08 01:33:40 -050037 .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 Rao4d5b5bf2022-05-23 09:23:31 -050048
49bool 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 Othayoth87441232022-09-08 23:47:11 -050094
95std::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 Othayothc4831812021-06-08 01:33:40 -0500129} // namespace util
130} // namespace openpower