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