blob: b44d47713c1526a2fa4757c0c278612fd43aac48 [file] [log] [blame]
George Liuf3b75142021-06-10 11:22:50 +08001#include "utils.hpp"
2
Chris Cain36f9cde2021-11-22 11:18:21 -06003#include <fmt/core.h>
4
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05305#include <phosphor-logging/elog-errors.hpp>
Gunnar Mills94df8c92018-09-14 14:50:03 -05006#include <sdbusplus/bus.hpp>
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05307#include <xyz/openbmc_project/Common/error.hpp>
George Liub5ca1012021-09-10 12:53:11 +08008
9#include <string>
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053010namespace open_power
11{
12namespace occ
13{
George Liuf3b75142021-06-10 11:22:50 +080014namespace utils
15{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053016// For throwing exceptions
17using namespace phosphor::logging;
Gunnar Mills94df8c92018-09-14 14:50:03 -050018using InternalFailure =
19 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053020
George Liuf3b75142021-06-10 11:22:50 +080021const std::string getService(const std::string& path,
22 const std::string& interface)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053023{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053024
George Liuf3b75142021-06-10 11:22:50 +080025 using InterfaceList = std::vector<std::string>;
26 std::map<std::string, std::vector<std::string>> mapperResponse;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053027
George Liuf3b75142021-06-10 11:22:50 +080028 auto& bus = getBus();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053029
George Liuf3b75142021-06-10 11:22:50 +080030 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
31 MAPPER_IFACE, "GetObject");
32 mapper.append(path, InterfaceList({interface}));
33
34 auto mapperResponseMsg = bus.call(mapper);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053035 mapperResponseMsg.read(mapperResponse);
George Liuf3b75142021-06-10 11:22:50 +080036 if (mapperResponse.empty())
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053037 {
38 log<level::ERR>("ERROR reading mapper response",
Gunnar Mills94df8c92018-09-14 14:50:03 -050039 entry("PATH=%s", path.c_str()),
George Liuf3b75142021-06-10 11:22:50 +080040 entry("INTERFACE=%s", interface.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053041
42 elog<InternalFailure>();
43 }
George Liuf3b75142021-06-10 11:22:50 +080044
45 // the value here will be the service name
46 return mapperResponse.cbegin()->first;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053047}
48
George Liuf3b75142021-06-10 11:22:50 +080049const PropertyValue getProperty(const std::string& objectPath,
50 const std::string& interface,
51 const std::string& propertyName)
52{
53 PropertyValue value{};
54
55 auto& bus = getBus();
56 auto service = getService(objectPath, interface);
57 if (service.empty())
58 {
59 return value;
60 }
61
62 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
63 DBUS_PROPERTY_IFACE, "Get");
64 method.append(interface, propertyName);
65
66 auto reply = bus.call(method);
67 reply.read(value);
68
69 return value;
70}
71
Chris Cain36f9cde2021-11-22 11:18:21 -060072/**
73 * @brief Sets a given object's property value
74 *
75 * @param[in] object - Name of the object containing the property
76 * @param[in] interface - Interface name containing the property
77 * @param[in] property - Property name
78 * @param[in] value - Property value
79 */
80void setProperty(const std::string& objectPath, const std::string& interface,
81 const std::string& propertyName, std::string&& value)
82{
83 using namespace std::literals::string_literals;
84 std::variant<std::string> varValue(std::forward<std::string>(value));
85
86 auto& bus = getBus();
87 auto service = getService(objectPath, interface);
88 if (service.empty())
89 {
90 return;
91 }
92
93 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
94 DBUS_PROPERTY_IFACE, "Set");
95 method.append(interface, propertyName, varValue);
96
97 auto reply = bus.call(method);
98 if (reply.is_method_error())
99 {
100 log<level::ERR>(
101 fmt::format("util::setProperty: Failed to set property {}",
102 propertyName)
103 .c_str());
104 }
105}
106
Matt Spinler5901abd2021-09-23 13:50:03 -0500107std::vector<std::string>
108 getSubtreePaths(const std::vector<std::string>& interfaces,
109 const std::string& path)
110{
111 std::vector<std::string> paths;
112
113 auto& bus = getBus();
114 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
115 MAPPER_IFACE, "GetSubTreePaths");
116 method.append(path, 0, interfaces);
117
118 auto reply = bus.call(method);
119 reply.read(paths);
120
121 return paths;
122}
George Liuf3b75142021-06-10 11:22:50 +0800123} // namespace utils
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530124} // namespace occ
125} // namespace open_power