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