1 #pragma once
2
3 #include <phosphor-logging/elog-errors.hpp>
4 #include <phosphor-logging/elog.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/exception.hpp>
8 #include <sdbusplus/message.hpp>
9 #include <xyz/openbmc_project/Common/error.hpp>
10
11 #include <string>
12
13 namespace util
14 {
15 namespace detail
16 {
17 namespace errors = sdbusplus::xyz::openbmc_project::Common::Error;
18 } // namespace detail
19
20 /** @brief Alias for PropertiesChanged signal callbacks. */
21 template <typename... T>
22 using Properties = std::map<std::string, std::variant<T...>>;
23
24 namespace sdbusplus
25 {
26
27 /** @brief Get the bus connection. */
28 static auto& getBus() __attribute__((pure));
getBus()29 static auto& getBus()
30 {
31 static auto bus = ::sdbusplus::bus::new_default();
32 return bus;
33 }
34
35 /** @brief Invoke a method. */
36 template <typename... Args>
callMethod(::sdbusplus::bus_t & bus,const std::string & busName,const std::string & path,const std::string & interface,const std::string & method,Args &&...args)37 static auto callMethod(::sdbusplus::bus_t& bus, const std::string& busName,
38 const std::string& path, const std::string& interface,
39 const std::string& method, Args&&... args)
40 {
41 auto reqMsg = bus.new_method_call(busName.c_str(), path.c_str(),
42 interface.c_str(), method.c_str());
43 reqMsg.append(std::forward<Args>(args)...);
44 try
45 {
46 return bus.call(reqMsg);
47 }
48 catch (const std::exception& e)
49 {
50 lg2::error("Failed to invoke DBus method: {PATH}, {INTF}, {METHOD}",
51 "PATH", path, "INTF", interface, "METHOD", method);
52 phosphor::logging::elog<detail::errors::InternalFailure>();
53 }
54 }
55
56 /** @brief Invoke a method. */
57 template <typename... Args>
callMethod(const std::string & busName,const std::string & path,const std::string & interface,const std::string & method,Args &&...args)58 static auto callMethod(const std::string& busName, const std::string& path,
59 const std::string& interface, const std::string& method,
60 Args&&... args)
61 {
62 return callMethod(getBus(), busName, path, interface, method,
63 std::forward<Args>(args)...);
64 }
65
66 /** @brief Invoke a method and read the response. */
67 template <typename Ret, typename... Args>
68 static auto
callMethodAndRead(::sdbusplus::bus_t & bus,const std::string & busName,const std::string & path,const std::string & interface,const std::string & method,Args &&...args)69 callMethodAndRead(::sdbusplus::bus_t& bus, const std::string& busName,
70 const std::string& path, const std::string& interface,
71 const std::string& method, Args&&... args)
72 {
73 ::sdbusplus::message_t respMsg = callMethod<Args...>(
74 bus, busName, path, interface, method, std::forward<Args>(args)...);
75 Ret resp;
76 respMsg.read(resp);
77 return resp;
78 }
79
80 /** @brief Invoke a method and read the response. */
81 template <typename Ret, typename... Args>
callMethodAndRead(const std::string & busName,const std::string & path,const std::string & interface,const std::string & method,Args &&...args)82 static auto callMethodAndRead(
83 const std::string& busName, const std::string& path,
84 const std::string& interface, const std::string& method, Args&&... args)
85 {
86 return callMethodAndRead<Ret>(getBus(), busName, path, interface, method,
87 std::forward<Args>(args)...);
88 }
89
90 /** @brief Get service from the mapper. */
getService(::sdbusplus::bus_t & bus,const std::string & path,const std::string & interface)91 static auto getService(::sdbusplus::bus_t& bus, const std::string& path,
92 const std::string& interface)
93 {
94 using namespace std::literals::string_literals;
95 using GetObject = std::map<std::string, std::vector<std::string>>;
96
97 auto mapperResp = callMethodAndRead<GetObject>(
98 bus, "xyz.openbmc_project.ObjectMapper"s,
99 "/xyz/openbmc_project/object_mapper"s,
100 "xyz.openbmc_project.ObjectMapper"s, "GetObject"s, path,
101 GetObject::mapped_type{interface});
102
103 if (mapperResp.empty())
104 {
105 lg2::error("Object not found. {PATH}, {INTF}", "PATH", path, "INTF",
106 interface);
107 phosphor::logging::elog<detail::errors::InternalFailure>();
108 }
109 return mapperResp.begin()->first;
110 }
111
112 /** @brief Get a property without mapper lookup. */
113 template <typename Property>
getProperty(::sdbusplus::bus_t & bus,const std::string & busName,const std::string & path,const std::string & interface,const std::string & property)114 static auto getProperty(::sdbusplus::bus_t& bus, const std::string& busName,
115 const std::string& path, const std::string& interface,
116 const std::string& property)
117 {
118 using namespace std::literals::string_literals;
119
120 auto msg =
121 callMethod(bus, busName, path, "org.freedesktop.DBus.Properties"s,
122 "Get"s, interface, property);
123 ::std::variant<Property> value;
124 msg.read(value);
125 return std::get<Property>(value);
126 }
127
128 /** @brief Get a property without mapper lookup. */
129 template <typename Property>
getProperty(const std::string & busName,const std::string & path,const std::string & interface,const std::string & property)130 static auto getProperty(const std::string& busName, const std::string& path,
131 const std::string& interface,
132 const std::string& property)
133 {
134 return getProperty<Property>(getBus(), busName, path, interface, property);
135 }
136
137 /** @brief Get a property with mapper lookup. */
138 template <typename Property>
getProperty(::sdbusplus::bus_t & bus,const std::string & path,const std::string & interface,const std::string & property)139 static auto getProperty(::sdbusplus::bus_t& bus, const std::string& path,
140 const std::string& interface,
141 const std::string& property)
142 {
143 return getProperty<Property>(bus, getService(bus, path, interface), path,
144 interface, property);
145 }
146
147 /** @brief Get a property with mapper lookup. */
148 template <typename Property>
getProperty(const std::string & path,const std::string & interface,const std::string & property)149 static auto getProperty(const std::string& path, const std::string& interface,
150 const std::string& property)
151 {
152 return getProperty<Property>(getBus(), path, interface, property);
153 }
154
155 } // namespace sdbusplus
156 } // namespace util
157