-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutils.cpp
311 lines (261 loc) · 9.34 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "config.h"
#include "utils.hpp"
#include <gpiod.h>
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Dump/Create/client.hpp>
#include <xyz/openbmc_project/Logging/Create/client.hpp>
#include <xyz/openbmc_project/ObjectMapper/client.hpp>
#include <xyz/openbmc_project/Software/ActivationBlocksTransition/client.hpp>
#include <xyz/openbmc_project/State/BMC/client.hpp>
#include <chrono>
#include <filesystem>
#include <format>
namespace phosphor
{
namespace state
{
namespace manager
{
namespace utils
{
using namespace std::literals::chrono_literals;
PHOSPHOR_LOG2_USING;
constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
using ActBlockTrans = sdbusplus::client::xyz::openbmc_project::software::
ActivationBlocksTransition<>;
void subscribeToSystemdSignals(sdbusplus::bus_t& bus)
{
auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "Subscribe");
try
{
// On OpenBMC based systems, systemd has had a few situations where it
// has been unable to respond to this call within the default d-bus
// timeout of 25 seconds. This is due to the large amount of work being
// done by systemd during OpenBMC startup. Set the timeout for this call
// to 60 seconds (worst case seen was around 30s so double it).
bus.call(method, 60s);
}
catch (const sdbusplus::exception_t& e)
{
error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
throw std::runtime_error("Unable to subscribe to systemd signals");
}
return;
}
std::string getService(sdbusplus::bus_t& bus, std::string path,
std::string interface)
{
auto mapper = bus.new_method_call(ObjectMapper::default_service,
ObjectMapper::instance_path,
ObjectMapper::interface, "GetObject");
mapper.append(path, std::vector<std::string>({interface}));
std::vector<std::pair<std::string, std::vector<std::string>>>
mapperResponse;
try
{
auto mapperResponseMsg = bus.call(mapper);
mapperResponseMsg.read(mapperResponse);
if (mapperResponse.empty())
{
error(
"Error no matching service with path {PATH} and interface {INTERFACE}",
"PATH", path, "INTERFACE", interface);
throw std::runtime_error("Error no matching service");
}
}
catch (const sdbusplus::exception_t& e)
{
error("Error in mapper call with path {PATH}, interface "
"{INTERFACE}, and exception {ERROR}",
"PATH", path, "INTERFACE", interface, "ERROR", e);
throw;
}
return mapperResponse.begin()->first;
}
std::string getProperty(sdbusplus::bus_t& bus, const std::string& path,
const std::string& interface,
const std::string& propertyName)
{
std::variant<std::string> property;
std::string service = getService(bus, path, interface);
auto method = bus.new_method_call(service.c_str(), path.c_str(),
PROPERTY_INTERFACE, "Get");
method.append(interface, propertyName);
try
{
auto reply = bus.call(method);
reply.read(property);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in property Get, error {ERROR}, property {PROPERTY}",
"ERROR", e, "PROPERTY", propertyName);
throw;
}
if (std::get<std::string>(property).empty())
{
error("Error reading property response for {PROPERTY}", "PROPERTY",
propertyName);
throw std::runtime_error("Error reading property response");
}
return std::get<std::string>(property);
}
void setProperty(sdbusplus::bus_t& bus, const std::string& path,
const std::string& interface, const std::string& property,
const std::string& value)
{
std::variant<std::string> variantValue = value;
std::string service = getService(bus, path, interface);
auto method = bus.new_method_call(service.c_str(), path.c_str(),
PROPERTY_INTERFACE, "Set");
method.append(interface, property, variantValue);
bus.call_noreply(method);
return;
}
int getGpioValue(const std::string& gpioName)
{
int gpioval = -1;
gpiod_line* line = gpiod_line_find(gpioName.c_str());
if (nullptr != line)
{
// take ownership of gpio
if (0 != gpiod_line_request_input(line, "state-manager"))
{
error("Failed request for {GPIO_NAME} GPIO", "GPIO_NAME", gpioName);
}
else
{
// get gpio value
gpioval = gpiod_line_get_value(line);
// release ownership of gpio
gpiod_line_close_chip(line);
}
}
return gpioval;
}
void createError(
sdbusplus::bus_t& bus, const std::string& errorMsg,
sdbusplus::server::xyz::openbmc_project::logging::Entry::Level errLevel,
std::map<std::string, std::string> additionalData)
{
try
{
// Always add the _PID on for some extra logging debug
additionalData.emplace("_PID", std::to_string(getpid()));
using LoggingCreate =
sdbusplus::client::xyz::openbmc_project::logging::Create<>;
auto method = bus.new_method_call(LoggingCreate::default_service,
LoggingCreate::instance_path,
LoggingCreate::interface, "Create");
method.append(errorMsg, errLevel, additionalData);
auto resp = bus.call(method);
}
catch (const sdbusplus::exception_t& e)
{
error("sdbusplus D-Bus call exception, error {ERROR} trying to create "
"an error with {ERROR_MSG}",
"ERROR", e, "ERROR_MSG", errorMsg);
throw std::runtime_error(
"Error in invoking D-Bus logging create interface");
}
catch (const std::exception& e)
{
error("D-bus call exception: {ERROR}", "ERROR", e);
throw e;
}
}
void createBmcDump(sdbusplus::bus_t& bus)
{
using DumpCreate = sdbusplus::client::xyz::openbmc_project::dump::Create<>;
auto dumpPath =
sdbusplus::message::object_path(DumpCreate::namespace_path::value) /
DumpCreate::namespace_path::bmc;
auto method =
bus.new_method_call(DumpCreate::default_service, dumpPath.str.c_str(),
DumpCreate::interface, "CreateDump");
method.append(
std::vector<
std::pair<std::string, std::variant<std::string, uint64_t>>>());
try
{
bus.call_noreply(method);
}
catch (const sdbusplus::exception_t& e)
{
error("Failed to create BMC dump, exception:{ERROR}", "ERROR", e);
// just continue, this is error path anyway so we're just collecting
// what we can
}
}
bool checkACLoss(size_t& chassisId)
{
std::string chassisLostPowerFileFmt =
std::format(CHASSIS_LOST_POWER_FILE, chassisId);
std::filesystem::path chassisPowerLossFile{chassisLostPowerFileFmt};
return std::filesystem::exists(chassisPowerLossFile);
}
bool isBmcReady(sdbusplus::bus_t& bus)
{
using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) /
BMC::namespace_path::bmc;
auto bmcState =
getProperty(bus, bmcPath.str, BMC::interface, "CurrentBMCState");
if (sdbusplus::message::convert_from_string<BMC::BMCState>(bmcState) !=
BMC::BMCState::Ready)
{
debug("BMC State is {BMC_STATE}", "BMC_STATE", bmcState);
return false;
}
return true;
}
bool waitBmcReady(sdbusplus::bus_t& bus, std::chrono::seconds timeout)
{
while (timeout.count() != 0)
{
timeout--;
if (isBmcReady(bus))
{
return true;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return false;
}
#ifdef CHECK_FWUPDATE_BEFORE_DO_TRANSITION
bool isFirmwareUpdating(sdbusplus::bus_t& bus)
{
/*
* This method looks for ActivationBlocksTransition interface, if any object
* path is including this interface, the Transition action should be
* prevented.
*/
auto mapper = bus.new_method_call(
ObjectMapper::default_service, ObjectMapper::instance_path,
ObjectMapper::interface, "GetSubTreePaths");
mapper.append("/", 0, std::vector<std::string>({ActBlockTrans::interface}));
std::vector<std::string> mapperResponse;
try
{
auto mapperResponseMsg = bus.call(mapper);
mapperResponseMsg.read(mapperResponse);
}
catch (const sdbusplus::exception_t& e)
{
error("Error in mapper call with root path, interface "
"ActivationBlocksTransition, and exception {ERROR}",
"ERROR", e);
return false;
}
return !mapperResponse.empty();
}
#endif // CHECK_FWUPDATE_BEFORE_DO_TRANSITION
} // namespace utils
} // namespace manager
} // namespace state
} // namespace phosphor