blob: fdae252ccfce92da7ed6d740c0fc0f5363fd40db [file] [log] [blame]
Matt Spinler974c9162017-08-04 08:36:37 -05001#pragma once
2
3#include <phosphor-logging/log.hpp>
4#include <sdbusplus/bus.hpp>
5#include <string>
6
7namespace witherspoon
8{
9namespace power
10{
11namespace util
12{
13
14constexpr 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 */
26std::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 */
40template<typename T>
41void 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
Matt Spinler48b4a432017-08-04 11:57:37 -050073/**
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 */
79void powerOff(sdbusplus::bus::bus& bus);
80
Matt Spinler974c9162017-08-04 08:36:37 -050081}
82}
83}