blob: 186a88d879170aaf0d813e437d25984fa2417cd4 [file] [log] [blame]
Tony Lee89659212019-06-21 17:34:14 +08001#include <iostream>
2#include <phosphor-logging/elog-errors.hpp>
3#include <phosphor-logging/elog.hpp>
4#include <phosphor-logging/log.hpp>
5#include <sdbusplus/bus.hpp>
6#include <sdbusplus/bus/match.hpp>
7#include <sdbusplus/message.hpp>
8#include <xyz/openbmc_project/Common/error.hpp>
9
10namespace phosphor
11{
12namespace nvme
13{
14namespace util
15{
16
17using namespace phosphor::logging;
18
19class SDBusPlus
20{
21 public:
22 template <typename T>
23 static auto
24 setProperty(sdbusplus::bus::bus& bus, const std::string& busName,
25 const std::string& objPath, const std::string& interface,
26 const std::string& property, const T& value)
27 {
Patrick Williams05eedaa2020-05-13 17:58:42 -050028 std::variant<T> data = value;
Tony Lee89659212019-06-21 17:34:14 +080029
30 try
31 {
32 auto methodCall = bus.new_method_call(
33 busName.c_str(), objPath.c_str(), DBUS_PROPERTY_IFACE, "Set");
34
35 methodCall.append(interface.c_str());
36 methodCall.append(property);
37 methodCall.append(data);
38
39 auto reply = bus.call(methodCall);
40 }
41 catch (const std::exception& e)
42 {
Brandon Kim9b771e22020-10-05 12:02:01 -070043 log<level::ERR>("Set properties fail.", entry("ERROR=%s", e.what()),
44 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080045 return;
46 }
47 }
48
49 template <typename Property>
50 static auto
51 getProperty(sdbusplus::bus::bus& bus, const std::string& busName,
52 const std::string& objPath, const std::string& interface,
53 const std::string& property)
54 {
55 auto methodCall = bus.new_method_call(busName.c_str(), objPath.c_str(),
56 DBUS_PROPERTY_IFACE, "Get");
57
58 methodCall.append(interface.c_str());
59 methodCall.append(property);
60
Patrick Williams05eedaa2020-05-13 17:58:42 -050061 std::variant<Property> value;
Tony Lee89659212019-06-21 17:34:14 +080062
63 try
64 {
65 auto reply = bus.call(methodCall);
66 reply.read(value);
67 }
68 catch (const std::exception& e)
69 {
Brandon Kim9b771e22020-10-05 12:02:01 -070070 log<level::ERR>("Get properties fail.", entry("ERROR=%s", e.what()),
71 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080072 return false;
73 }
74
Patrick Williamsc80e6e22020-05-13 11:50:34 -050075 return std::get<Property>(value);
Tony Lee89659212019-06-21 17:34:14 +080076 }
77
78 template <typename... Args>
79 static auto CallMethod(sdbusplus::bus::bus& bus, const std::string& busName,
80 const std::string& objPath,
81 const std::string& interface,
82 const std::string& method, Args&&... args)
83 {
84 auto reqMsg = bus.new_method_call(busName.c_str(), objPath.c_str(),
85 interface.c_str(), method.c_str());
86 reqMsg.append(std::forward<Args>(args)...);
87 try
88 {
89 auto respMsg = bus.call(reqMsg);
90 }
91 catch (const std::exception& e)
92 {
Brandon Kim9b771e22020-10-05 12:02:01 -070093 log<level::ERR>("Call method fail.", entry("ERROR=%s", e.what()),
94 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080095 return;
96 }
97 }
98};
99
100} // namespace util
101} // namespace nvme
102} // namespace phosphor