blob: 1cc6de0ea75d85f85be71ac9365c302b2e7138da [file] [log] [blame]
Patrick Williams029c23e2025-03-03 14:12:40 -05001#include "config.h"
2
Tony Lee89659212019-06-21 17:34:14 +08003#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/elog.hpp>
5#include <phosphor-logging/log.hpp>
6#include <sdbusplus/bus.hpp>
7#include <sdbusplus/bus/match.hpp>
8#include <sdbusplus/message.hpp>
9#include <xyz/openbmc_project/Common/error.hpp>
10
Patrick Williams7da98582023-05-10 07:50:46 -050011#include <iostream>
12
Tony Lee89659212019-06-21 17:34:14 +080013namespace phosphor
14{
15namespace nvme
16{
17namespace util
18{
19
20using namespace phosphor::logging;
21
22class SDBusPlus
23{
24 public:
25 template <typename T>
Patrick Williams7ce68bc2022-07-22 19:26:55 -050026 static auto setProperty(sdbusplus::bus_t& bus, const std::string& busName,
27 const std::string& objPath,
28 const std::string& interface,
29 const std::string& property, const T& value)
Tony Lee89659212019-06-21 17:34:14 +080030 {
Patrick Williams05eedaa2020-05-13 17:58:42 -050031 std::variant<T> data = value;
Tony Lee89659212019-06-21 17:34:14 +080032
33 try
34 {
35 auto methodCall = bus.new_method_call(
36 busName.c_str(), objPath.c_str(), DBUS_PROPERTY_IFACE, "Set");
37
38 methodCall.append(interface.c_str());
39 methodCall.append(property);
40 methodCall.append(data);
41
42 auto reply = bus.call(methodCall);
43 }
44 catch (const std::exception& e)
45 {
Brandon Kim9b771e22020-10-05 12:02:01 -070046 log<level::ERR>("Set properties fail.", entry("ERROR=%s", e.what()),
47 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080048 return;
49 }
50 }
51
52 template <typename Property>
Patrick Williams7ce68bc2022-07-22 19:26:55 -050053 static auto getProperty(sdbusplus::bus_t& bus, const std::string& busName,
54 const std::string& objPath,
55 const std::string& interface,
56 const std::string& property)
Tony Lee89659212019-06-21 17:34:14 +080057 {
58 auto methodCall = bus.new_method_call(busName.c_str(), objPath.c_str(),
59 DBUS_PROPERTY_IFACE, "Get");
60
61 methodCall.append(interface.c_str());
62 methodCall.append(property);
63
Patrick Williams05eedaa2020-05-13 17:58:42 -050064 std::variant<Property> value;
Tony Lee89659212019-06-21 17:34:14 +080065
66 try
67 {
68 auto reply = bus.call(methodCall);
69 reply.read(value);
70 }
71 catch (const std::exception& e)
72 {
Brandon Kim9b771e22020-10-05 12:02:01 -070073 log<level::ERR>("Get properties fail.", entry("ERROR=%s", e.what()),
74 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080075 return false;
76 }
77
Patrick Williamsc80e6e22020-05-13 11:50:34 -050078 return std::get<Property>(value);
Tony Lee89659212019-06-21 17:34:14 +080079 }
80
81 template <typename... Args>
Patrick Williams7ce68bc2022-07-22 19:26:55 -050082 static auto CallMethod(sdbusplus::bus_t& bus, const std::string& busName,
Tony Lee89659212019-06-21 17:34:14 +080083 const std::string& objPath,
84 const std::string& interface,
85 const std::string& method, Args&&... args)
86 {
87 auto reqMsg = bus.new_method_call(busName.c_str(), objPath.c_str(),
88 interface.c_str(), method.c_str());
89 reqMsg.append(std::forward<Args>(args)...);
90 try
91 {
92 auto respMsg = bus.call(reqMsg);
93 }
94 catch (const std::exception& e)
95 {
Brandon Kim9b771e22020-10-05 12:02:01 -070096 log<level::ERR>("Call method fail.", entry("ERROR=%s", e.what()),
97 entry("OBJ_PATH=%s", objPath.c_str()));
Tony Lee89659212019-06-21 17:34:14 +080098 return;
99 }
100 }
101};
102
103} // namespace util
104} // namespace nvme
105} // namespace phosphor