-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.cpp
252 lines (214 loc) · 7.57 KB
/
utils.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "utils.hpp"
#include <systemd/sd-event.h>
#include <unistd.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
#include <xyz/openbmc_project/State/Host/server.hpp>
#include <string>
namespace open_power
{
namespace occ
{
namespace utils
{
// For throwing exceptions
using namespace phosphor::logging;
using InternalFailure =
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
Progress::ProgressStages;
constexpr auto HOST_STATE_OBJ_PATH = "/xyz/openbmc_project/state/host0";
const std::string getService(const std::string& path,
const std::string& interface)
{
using InterfaceList = std::vector<std::string>;
std::map<std::string, std::vector<std::string>> mapperResponse;
auto& bus = getBus();
auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
MAPPER_IFACE, "GetObject");
mapper.append(path, InterfaceList({interface}));
auto mapperResponseMsg = bus.call(mapper);
mapperResponseMsg.read(mapperResponse);
if (mapperResponse.empty())
{
lg2::error("ERROR reading mapper response: path={PATH}, I/F={INTF}",
"PATH", path, "INTF", interface);
elog<InternalFailure>();
}
// the value here will be the service name
return mapperResponse.cbegin()->first;
}
const PropertyValue getProperty(const std::string& objectPath,
const std::string& interface,
const std::string& propertyName)
{
PropertyValue value{};
auto& bus = getBus();
auto service = getService(objectPath, interface);
if (service.empty())
{
return value;
}
auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
DBUS_PROPERTY_IFACE, "Get");
method.append(interface, propertyName);
auto reply = bus.call(method);
reply.read(value);
return value;
}
/**
* @brief Sets a given object's property value
*
* @param[in] objectPath - Name of the object containing the property
* @param[in] interface - Interface name containing the property
* @param[in] propertyName - Property name
* @param[in] value - Property value
*/
void setProperty(const std::string& objectPath, const std::string& interface,
const std::string& propertyName, PropertyValue&& value)
{
using namespace std::literals::string_literals;
PropertyValue varValue(std::forward<PropertyValue>(value));
try
{
auto& bus = getBus();
auto service = getService(objectPath, interface);
if (service.empty())
{
return;
}
auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
DBUS_PROPERTY_IFACE, "Set");
method.append(interface, propertyName, varValue);
auto reply = bus.call(method);
if (reply.is_method_error())
{
lg2::error("util::setProperty: Failed to set property {PROP}",
"PROP", propertyName);
}
}
catch (const std::exception& e)
{
auto error = errno;
lg2::error("setProperty: failed to Set {PROP}, errno={ERR}, what={MSG}",
"PROP", propertyName, "ERR", error, "PROP", e.what());
}
}
std::vector<std::string> getSubtreePaths(
const std::vector<std::string>& interfaces, const std::string& path)
{
std::vector<std::string> paths;
auto& bus = getBus();
auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
MAPPER_IFACE, "GetSubTreePaths");
method.append(path, 0, interfaces);
auto reply = bus.call(method);
reply.read(paths);
return paths;
}
// Get the service and object path for an interface
std::string getServiceUsingSubTree(const std::string& interface,
std::string& path)
{
using Path = std::string;
using Intf = std::string;
using Serv = std::string;
using Intfs = std::vector<Intf>;
using Objects = std::map<Path, std::map<Serv, Intfs>>;
Serv service;
Objects rspObjects;
auto& bus = getBus();
auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
MAPPER_IFACE, "GetSubTree");
method.append(path, 0, std::vector{interface});
auto mapperResponseMsg = bus.call(method);
mapperResponseMsg.read(rspObjects);
if (rspObjects.empty())
{
lg2::error(
"util::getServiceUsingSubTree: Failed getSubTree({PATH},0,{INTF})",
"PATH", path, "INTF", interface);
}
else
{
path = rspObjects.begin()->first;
if (!rspObjects.begin()->second.empty())
{
service = rspObjects.begin()->second.begin()->first;
}
else
{
lg2::error(
"getServiceUsingSubTree: service not found for interface {INTF} (path={PATH})",
"INTF", interface, "PATH", path);
}
}
return service;
}
std::string getStateValue(const std::string& intf, const std::string& objPath,
const std::string& state)
{
std::string stateVal;
try
{
auto& bus = getBus();
auto service = getService(objPath, intf);
if (service.empty())
{
throw std::runtime_error("getStateValue: Failed to get service");
}
auto method =
bus.new_method_call(service.c_str(), objPath.c_str(),
"org.freedesktop.DBus.Properties", "Get");
method.append(intf, state);
auto reply = bus.call(method);
std::variant<std::string> propertyVal;
reply.read(propertyVal);
stateVal = std::get<std::string>(propertyVal);
}
catch (const sdbusplus::exception_t& e)
{
lg2::error("D-Bus call exception, OBJPATH({PATH}), "
"INTERFACE({INTF}), PROPERTY({PROP}) EXCEPTION({ERR})",
"PATH", objPath, "INTF", intf, "PROP", state, "ERR",
e.what());
throw std::runtime_error("Failed to get host state property");
}
catch (const std::bad_variant_access& e)
{
lg2::error(
"Exception raised while read host state({STATE}) property "
"value, OBJPATH({PATH}), INTERFACE({INTF}), EXCEPTION({ERR})",
"STATE", state, "PATH", objPath, "INTF", intf, "ERR", e.what());
throw std::runtime_error("Failed to get host state property");
}
return stateVal;
}
BootProgress getBootProgress()
{
BootProgress bootProgessStage;
constexpr auto bootProgressInterface =
"xyz.openbmc_project.State.Boot.Progress";
std::string value = getStateValue(bootProgressInterface,
HOST_STATE_OBJ_PATH, "BootProgress");
bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::server::
Progress::convertProgressStagesFromString(value);
return bootProgessStage;
}
bool isHostRunning()
{
BootProgress bootProgressStatus = getBootProgress();
if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
(bootProgressStatus == BootProgress::SystemSetup) ||
(bootProgressStatus == BootProgress::OSRunning))
{
return true;
}
return false;
}
} // namespace utils
} // namespace occ
} // namespace open_power