blob: 1f54c493c3b57f38cde3fdf821f111edc6d13e8f [file] [log] [blame]
Tony Lee89659212019-06-21 17:34:14 +08001#include <phosphor-logging/elog-errors.hpp>
2#include <phosphor-logging/elog.hpp>
3#include <phosphor-logging/log.hpp>
4#include <sdbusplus/bus.hpp>
5#include <sdbusplus/bus/match.hpp>
6#include <sdbusplus/message.hpp>
7#include <xyz/openbmc_project/Common/error.hpp>
8
Patrick Williams7da98582023-05-10 07:50:46 -05009#include <iostream>
10
Tony Lee89659212019-06-21 17:34:14 +080011namespace phosphor
12{
13namespace nvme
14{
15namespace util
16{
17
18using namespace phosphor::logging;
19
20class SDBusPlus
21{
22 public:
23 template <typename T>
Patrick Williams7ce68bc2022-07-22 19:26:55 -050024 static auto setProperty(sdbusplus::bus_t& bus, const std::string& busName,
25 const std::string& objPath,
26 const std::string& interface,
27 const std::string& property, const T& value)
Tony Lee89659212019-06-21 17:34:14 +080028 {
Patrick Williams05eedaa2020-05-13 17:58:42 -050029 std::variant<T> data = value;
Tony Lee89659212019-06-21 17:34:14 +080030
31 try
32 {
33 auto methodCall = bus.new_method_call(
34 busName.c_str(), objPath.c_str(), DBUS_PROPERTY_IFACE, "Set");
35
36 methodCall.append(interface.c_str());
37 methodCall.append(property);
38 methodCall.append(data);
39
40 auto reply = bus.call(methodCall);
41 }
42 catch (const std::exception& e)
43 {
Brandon Kim9b771e22020-10-05 12:02:01 -070044 log<level::ERR>("Set properties fail.", entry("ERROR=%s", e.what()),
45 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080046 return;
47 }
48 }
49
50 template <typename Property>
Patrick Williams7ce68bc2022-07-22 19:26:55 -050051 static auto getProperty(sdbusplus::bus_t& bus, const std::string& busName,
52 const std::string& objPath,
53 const std::string& interface,
54 const std::string& property)
Tony Lee89659212019-06-21 17:34:14 +080055 {
56 auto methodCall = bus.new_method_call(busName.c_str(), objPath.c_str(),
57 DBUS_PROPERTY_IFACE, "Get");
58
59 methodCall.append(interface.c_str());
60 methodCall.append(property);
61
Patrick Williams05eedaa2020-05-13 17:58:42 -050062 std::variant<Property> value;
Tony Lee89659212019-06-21 17:34:14 +080063
64 try
65 {
66 auto reply = bus.call(methodCall);
67 reply.read(value);
68 }
69 catch (const std::exception& e)
70 {
Brandon Kim9b771e22020-10-05 12:02:01 -070071 log<level::ERR>("Get properties fail.", entry("ERROR=%s", e.what()),
72 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080073 return false;
74 }
75
Patrick Williamsc80e6e22020-05-13 11:50:34 -050076 return std::get<Property>(value);
Tony Lee89659212019-06-21 17:34:14 +080077 }
78
79 template <typename... Args>
Patrick Williams7ce68bc2022-07-22 19:26:55 -050080 static auto CallMethod(sdbusplus::bus_t& bus, const std::string& busName,
Tony Lee89659212019-06-21 17:34:14 +080081 const std::string& objPath,
82 const std::string& interface,
83 const std::string& method, Args&&... args)
84 {
85 auto reqMsg = bus.new_method_call(busName.c_str(), objPath.c_str(),
86 interface.c_str(), method.c_str());
87 reqMsg.append(std::forward<Args>(args)...);
88 try
89 {
90 auto respMsg = bus.call(reqMsg);
91 }
92 catch (const std::exception& e)
93 {
Brandon Kim9b771e22020-10-05 12:02:01 -070094 log<level::ERR>("Call method fail.", entry("ERROR=%s", e.what()),
95 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080096 return;
97 }
98 }
99};
100
101} // namespace util
102} // namespace nvme
103} // namespace phosphor