xref: /openbmc/phosphor-power/utility.hpp (revision 974c916e2db64738557f219a72ed9c32c35ec3a0)
1*974c916eSMatt Spinler #pragma once
2*974c916eSMatt Spinler 
3*974c916eSMatt Spinler #include <phosphor-logging/log.hpp>
4*974c916eSMatt Spinler #include <sdbusplus/bus.hpp>
5*974c916eSMatt Spinler #include <string>
6*974c916eSMatt Spinler 
7*974c916eSMatt Spinler namespace witherspoon
8*974c916eSMatt Spinler {
9*974c916eSMatt Spinler namespace power
10*974c916eSMatt Spinler {
11*974c916eSMatt Spinler namespace util
12*974c916eSMatt Spinler {
13*974c916eSMatt Spinler 
14*974c916eSMatt Spinler constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
15*974c916eSMatt Spinler 
16*974c916eSMatt Spinler /**
17*974c916eSMatt Spinler  * @brief Get the service name from the mapper for the
18*974c916eSMatt Spinler  *        interface and path passed in.
19*974c916eSMatt Spinler  *
20*974c916eSMatt Spinler  * @param[in] path - the D-Bus path name
21*974c916eSMatt Spinler  * @param[in] interface - the D-Bus interface name
22*974c916eSMatt Spinler  * @param[in] bus - the D-Bus object
23*974c916eSMatt Spinler  *
24*974c916eSMatt Spinler  * @return The service name
25*974c916eSMatt Spinler  */
26*974c916eSMatt Spinler std::string getService(const std::string& path,
27*974c916eSMatt Spinler                        const std::string& interface,
28*974c916eSMatt Spinler                        sdbusplus::bus::bus& bus);
29*974c916eSMatt Spinler 
30*974c916eSMatt Spinler /**
31*974c916eSMatt Spinler  * @brief Read a D-Bus property
32*974c916eSMatt Spinler  *
33*974c916eSMatt Spinler  * @param[in] interface - the interface the property is on
34*974c916eSMatt Spinler  * @param[in] propertName - the name of the property
35*974c916eSMatt Spinler  * @param[in] path - the D-Bus path
36*974c916eSMatt Spinler  * @param[in] service - the D-Bus service
37*974c916eSMatt Spinler  * @param[in] bus - the D-Bus object
38*974c916eSMatt Spinler  * @param[out] value - filled in with the property value
39*974c916eSMatt Spinler  */
40*974c916eSMatt Spinler template<typename T>
41*974c916eSMatt Spinler void getProperty(const std::string& interface,
42*974c916eSMatt Spinler                  const std::string& propertyName,
43*974c916eSMatt Spinler                  const std::string& path,
44*974c916eSMatt Spinler                  const std::string& service,
45*974c916eSMatt Spinler                  sdbusplus::bus::bus& bus,
46*974c916eSMatt Spinler                  T& value)
47*974c916eSMatt Spinler {
48*974c916eSMatt Spinler     sdbusplus::message::variant<T> property;
49*974c916eSMatt Spinler 
50*974c916eSMatt Spinler     auto method = bus.new_method_call(service.c_str(),
51*974c916eSMatt Spinler             path.c_str(),
52*974c916eSMatt Spinler             PROPERTY_INTF,
53*974c916eSMatt Spinler             "Get");
54*974c916eSMatt Spinler 
55*974c916eSMatt Spinler     method.append(interface, propertyName);
56*974c916eSMatt Spinler 
57*974c916eSMatt Spinler     auto reply = bus.call(method);
58*974c916eSMatt Spinler     if (reply.is_method_error())
59*974c916eSMatt Spinler     {
60*974c916eSMatt Spinler         using namespace phosphor::logging;
61*974c916eSMatt Spinler         log<level::ERR>("Error in property get call",
62*974c916eSMatt Spinler                 entry("PATH=%s", path.c_str()),
63*974c916eSMatt Spinler                 entry("PROPERTY=%s", propertyName.c_str()));
64*974c916eSMatt Spinler         //
65*974c916eSMatt Spinler         // TODO openbmc/openbmc#851 - Once available, throw returned error
66*974c916eSMatt Spinler         throw std::runtime_error("Error in property get call");
67*974c916eSMatt Spinler     }
68*974c916eSMatt Spinler 
69*974c916eSMatt Spinler     reply.read(property);
70*974c916eSMatt Spinler     value = sdbusplus::message::variant_ns::get<T>(property);
71*974c916eSMatt Spinler }
72*974c916eSMatt Spinler 
73*974c916eSMatt Spinler }
74*974c916eSMatt Spinler }
75*974c916eSMatt Spinler }
76