-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdbusplus.hpp
104 lines (91 loc) · 3.15 KB
/
sdbusplus.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdbusplus/message.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
namespace phosphor
{
namespace nvme
{
namespace util
{
using namespace phosphor::logging;
class SDBusPlus
{
public:
template <typename T>
static auto
setProperty(sdbusplus::bus::bus& bus, const std::string& busName,
const std::string& objPath, const std::string& interface,
const std::string& property, const T& value)
{
sdbusplus::message::variant<T> data = value;
try
{
auto methodCall = bus.new_method_call(
busName.c_str(), objPath.c_str(), DBUS_PROPERTY_IFACE, "Set");
methodCall.append(interface.c_str());
methodCall.append(property);
methodCall.append(data);
auto reply = bus.call(methodCall);
}
catch (const std::exception& e)
{
log<level::ERR>("Set properties fail.",
entry("ERROR = %s", e.what()),
entry("Object path = %s", objPath.c_str()));
return;
}
}
template <typename Property>
static auto
getProperty(sdbusplus::bus::bus& bus, const std::string& busName,
const std::string& objPath, const std::string& interface,
const std::string& property)
{
auto methodCall = bus.new_method_call(busName.c_str(), objPath.c_str(),
DBUS_PROPERTY_IFACE, "Get");
methodCall.append(interface.c_str());
methodCall.append(property);
sdbusplus::message::variant<Property> value;
try
{
auto reply = bus.call(methodCall);
reply.read(value);
}
catch (const std::exception& e)
{
log<level::ERR>("Get properties fail.",
entry("ERROR = %s", e.what()),
entry("Object path = %s", objPath.c_str()));
return false;
}
return sdbusplus::message::variant_ns::get<Property>(value);
}
template <typename... Args>
static auto CallMethod(sdbusplus::bus::bus& bus, const std::string& busName,
const std::string& objPath,
const std::string& interface,
const std::string& method, Args&&... args)
{
auto reqMsg = bus.new_method_call(busName.c_str(), objPath.c_str(),
interface.c_str(), method.c_str());
reqMsg.append(std::forward<Args>(args)...);
try
{
auto respMsg = bus.call(reqMsg);
}
catch (const std::exception& e)
{
log<level::ERR>("Call method fail.", entry("ERROR = %s", e.what()),
entry("Object path = %s", objPath.c_str()));
return;
}
}
};
} // namespace util
} // namespace nvme
} // namespace phosphor