blob: efb7abd6ab8f45d2c14a49b909df59e40ea6343c [file] [log] [blame]
George Liuf3b75142021-06-10 11:22:50 +08001#include "utils.hpp"
2
Chris Cainbae4d072022-02-28 09:46:50 -06003#include <systemd/sd-event.h>
4#include <unistd.h>
Chris Cain36f9cde2021-11-22 11:18:21 -06005
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05306#include <phosphor-logging/elog-errors.hpp>
Chris Cain37abe9b2024-10-31 17:20:31 -05007#include <phosphor-logging/lg2.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -05008#include <sdbusplus/bus.hpp>
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05309#include <xyz/openbmc_project/Common/error.hpp>
Chris Cainbae4d072022-02-28 09:46:50 -060010#include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
11#include <xyz/openbmc_project/State/Host/server.hpp>
George Liub5ca1012021-09-10 12:53:11 +080012
13#include <string>
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053014namespace open_power
15{
16namespace occ
17{
George Liuf3b75142021-06-10 11:22:50 +080018namespace utils
19{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053020// For throwing exceptions
21using namespace phosphor::logging;
Gunnar Mills94df8c92018-09-14 14:50:03 -050022using InternalFailure =
23 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053024
Chris Cainbae4d072022-02-28 09:46:50 -060025using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
26 Progress::ProgressStages;
27constexpr auto HOST_STATE_OBJ_PATH = "/xyz/openbmc_project/state/host0";
28
George Liuf3b75142021-06-10 11:22:50 +080029const std::string getService(const std::string& path,
30 const std::string& interface)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053031{
George Liuf3b75142021-06-10 11:22:50 +080032 using InterfaceList = std::vector<std::string>;
33 std::map<std::string, std::vector<std::string>> mapperResponse;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053034
George Liuf3b75142021-06-10 11:22:50 +080035 auto& bus = getBus();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053036
George Liuf3b75142021-06-10 11:22:50 +080037 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
38 MAPPER_IFACE, "GetObject");
39 mapper.append(path, InterfaceList({interface}));
40
41 auto mapperResponseMsg = bus.call(mapper);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053042 mapperResponseMsg.read(mapperResponse);
George Liuf3b75142021-06-10 11:22:50 +080043 if (mapperResponse.empty())
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053044 {
Chris Cain37abe9b2024-10-31 17:20:31 -050045 lg2::error("ERROR reading mapper response: path={PATH}, I/F={INTF}",
46 "PATH", path, "INTF", interface);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053047
48 elog<InternalFailure>();
49 }
George Liuf3b75142021-06-10 11:22:50 +080050
51 // the value here will be the service name
52 return mapperResponse.cbegin()->first;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053053}
54
George Liuf3b75142021-06-10 11:22:50 +080055const PropertyValue getProperty(const std::string& objectPath,
56 const std::string& interface,
57 const std::string& propertyName)
58{
59 PropertyValue value{};
60
61 auto& bus = getBus();
62 auto service = getService(objectPath, interface);
63 if (service.empty())
64 {
65 return value;
66 }
67
68 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
69 DBUS_PROPERTY_IFACE, "Get");
70 method.append(interface, propertyName);
71
72 auto reply = bus.call(method);
73 reply.read(value);
74
75 return value;
76}
77
Chris Cain36f9cde2021-11-22 11:18:21 -060078/**
79 * @brief Sets a given object's property value
80 *
Chris Cain1be43372021-12-09 19:29:37 -060081 * @param[in] objectPath - Name of the object containing the property
Chris Cain36f9cde2021-11-22 11:18:21 -060082 * @param[in] interface - Interface name containing the property
Chris Cain1be43372021-12-09 19:29:37 -060083 * @param[in] propertyName - Property name
Chris Cain36f9cde2021-11-22 11:18:21 -060084 * @param[in] value - Property value
85 */
86void setProperty(const std::string& objectPath, const std::string& interface,
Chris Cain5d66a0a2022-02-09 08:52:10 -060087 const std::string& propertyName, PropertyValue&& value)
Chris Cain36f9cde2021-11-22 11:18:21 -060088{
89 using namespace std::literals::string_literals;
Chris Cain5d66a0a2022-02-09 08:52:10 -060090 PropertyValue varValue(std::forward<PropertyValue>(value));
Chris Cain36f9cde2021-11-22 11:18:21 -060091
Chris Cain1be43372021-12-09 19:29:37 -060092 try
Chris Cain36f9cde2021-11-22 11:18:21 -060093 {
Chris Cain1be43372021-12-09 19:29:37 -060094 auto& bus = getBus();
95 auto service = getService(objectPath, interface);
96 if (service.empty())
97 {
98 return;
99 }
100
101 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
102 DBUS_PROPERTY_IFACE, "Set");
103 method.append(interface, propertyName, varValue);
104
105 auto reply = bus.call(method);
106 if (reply.is_method_error())
107 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500108 lg2::error("util::setProperty: Failed to set property {PROP}",
109 "PROP", propertyName);
Chris Cain1be43372021-12-09 19:29:37 -0600110 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600111 }
Chris Cain1be43372021-12-09 19:29:37 -0600112 catch (const std::exception& e)
Chris Cain36f9cde2021-11-22 11:18:21 -0600113 {
Chris Cain1be43372021-12-09 19:29:37 -0600114 auto error = errno;
Chris Cain37abe9b2024-10-31 17:20:31 -0500115 lg2::error("setProperty: failed to Set {PROP}, errno={ERR}, what={MSG}",
116 "PROP", propertyName, "ERR", error, "PROP", e.what());
Chris Cain36f9cde2021-11-22 11:18:21 -0600117 }
118}
119
Patrick Williamsd7542c82024-08-16 15:20:28 -0400120std::vector<std::string> getSubtreePaths(
121 const std::vector<std::string>& interfaces, const std::string& path)
Matt Spinler5901abd2021-09-23 13:50:03 -0500122{
123 std::vector<std::string> paths;
124
125 auto& bus = getBus();
126 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
127 MAPPER_IFACE, "GetSubTreePaths");
128 method.append(path, 0, interfaces);
129
130 auto reply = bus.call(method);
131 reply.read(paths);
132
133 return paths;
134}
Chris Cain1be43372021-12-09 19:29:37 -0600135
136// Get the service and object path for an interface
137std::string getServiceUsingSubTree(const std::string& interface,
138 std::string& path)
139{
140 using Path = std::string;
141 using Intf = std::string;
142 using Serv = std::string;
143 using Intfs = std::vector<Intf>;
144 using Objects = std::map<Path, std::map<Serv, Intfs>>;
145 Serv service;
146 Objects rspObjects;
147
148 auto& bus = getBus();
149 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
150 MAPPER_IFACE, "GetSubTree");
151 method.append(path, 0, std::vector{interface});
152
153 auto mapperResponseMsg = bus.call(method);
154 mapperResponseMsg.read(rspObjects);
155 if (rspObjects.empty())
156 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500157 lg2::error(
158 "util::getServiceUsingSubTree: Failed getSubTree({PATH},0,{INTF})",
159 "PATH", path, "INTF", interface);
Chris Cain1be43372021-12-09 19:29:37 -0600160 }
161 else
162 {
163 path = rspObjects.begin()->first;
164 if (!rspObjects.begin()->second.empty())
165 {
166 service = rspObjects.begin()->second.begin()->first;
167 }
168 else
169 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500170 lg2::error(
171 "getServiceUsingSubTree: service not found for interface {INTF} (path={PATH})",
172 "INTF", interface, "PATH", path);
Chris Cain1be43372021-12-09 19:29:37 -0600173 }
174 }
175
176 return service;
177}
178
Chris Cainbae4d072022-02-28 09:46:50 -0600179std::string getStateValue(const std::string& intf, const std::string& objPath,
180 const std::string& state)
181{
182 std::string stateVal;
183 try
184 {
185 auto& bus = getBus();
186 auto service = getService(objPath, intf);
187 if (service.empty())
188 {
189 throw std::runtime_error("getStateValue: Failed to get service");
190 }
191
Patrick Williamsd7542c82024-08-16 15:20:28 -0400192 auto method =
193 bus.new_method_call(service.c_str(), objPath.c_str(),
194 "org.freedesktop.DBus.Properties", "Get");
Chris Cainbae4d072022-02-28 09:46:50 -0600195
196 method.append(intf, state);
197
198 auto reply = bus.call(method);
199
200 std::variant<std::string> propertyVal;
201
202 reply.read(propertyVal);
203
204 stateVal = std::get<std::string>(propertyVal);
205 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500206 catch (const sdbusplus::exception_t& e)
Chris Cainbae4d072022-02-28 09:46:50 -0600207 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500208 lg2::error("D-Bus call exception, OBJPATH({PATH}), "
209 "INTERFACE({INTF}), PROPERTY({PROP}) EXCEPTION({ERR})",
210 "PATH", objPath, "INTF", intf, "PROP", state, "ERR",
211 e.what());
Chris Cainbae4d072022-02-28 09:46:50 -0600212 throw std::runtime_error("Failed to get host state property");
213 }
214 catch (const std::bad_variant_access& e)
215 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500216 lg2::error(
217 "Exception raised while read host state({STATE}) property "
218 "value, OBJPATH({PATH}), INTERFACE({INTF}), EXCEPTION({ERR})",
219 "STATE", state, "PATH", objPath, "INTF", intf, "ERR", e.what());
Chris Cainbae4d072022-02-28 09:46:50 -0600220 throw std::runtime_error("Failed to get host state property");
221 }
222
223 return stateVal;
224}
225
226BootProgress getBootProgress()
227{
228 BootProgress bootProgessStage;
229 constexpr auto bootProgressInterface =
230 "xyz.openbmc_project.State.Boot.Progress";
231 std::string value = getStateValue(bootProgressInterface,
232 HOST_STATE_OBJ_PATH, "BootProgress");
233 bootProgessStage = sdbusplus::xyz::openbmc_project::State::Boot::server::
234 Progress::convertProgressStagesFromString(value);
235 return bootProgessStage;
236}
237
238bool isHostRunning()
239{
240 BootProgress bootProgressStatus = getBootProgress();
241 if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
242 (bootProgressStatus == BootProgress::SystemSetup) ||
Chris Cainbae4d072022-02-28 09:46:50 -0600243 (bootProgressStatus == BootProgress::OSRunning))
244 {
245 return true;
246 }
247 return false;
248}
249
George Liuf3b75142021-06-10 11:22:50 +0800250} // namespace utils
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530251} // namespace occ
252} // namespace open_power