forked from openbmc/phosphor-led-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
130 lines (104 loc) · 3.63 KB
/
utils.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "utils.hpp"
#include <phosphor-logging/lg2.hpp>
namespace phosphor
{
namespace led
{
namespace utils
{
// Get service name
const std::string DBusHandler::getService(const std::string& path,
const std::string& interface) const
{
using InterfaceList = std::vector<std::string>;
std::unordered_map<std::string, std::vector<std::string>> mapperResponse;
auto& bus = DBusHandler::getBus();
auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
MAPPER_IFACE, "GetObject");
mapper.append(path, InterfaceList({interface}));
auto mapperResponseMsg = bus.call(mapper);
mapperResponseMsg.read(mapperResponse);
if (mapperResponse.empty())
{
lg2::error(
"Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}",
"PATH", path, "INTERFACE", interface);
return "";
}
// the value here will be the service name
return mapperResponse.cbegin()->first;
}
// Get all properties
const PropertyMap
DBusHandler::getAllProperties(const std::string& objectPath,
const std::string& interface) const
{
PropertyMap properties;
auto& bus = DBusHandler::getBus();
auto service = getService(objectPath, interface);
if (service.empty())
{
return properties;
}
auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
DBUS_PROPERTY_IFACE, "GetAll");
method.append(interface);
auto reply = bus.call(method);
reply.read(properties);
return properties;
}
// Get the property name
const PropertyValue
DBusHandler::getProperty(const std::string& objectPath,
const std::string& interface,
const std::string& propertyName) const
{
PropertyValue value{};
auto& bus = DBusHandler::getBus();
auto service = getService(objectPath, interface);
if (service.empty())
{
return value;
}
auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
DBUS_PROPERTY_IFACE, "Get");
method.append(interface, propertyName);
auto reply = bus.call(method);
reply.read(value);
return value;
}
// Set property
void DBusHandler::setProperty(const std::string& objectPath,
const std::string& interface,
const std::string& propertyName,
const PropertyValue& value) const
{
auto& bus = DBusHandler::getBus();
auto service = getService(objectPath, interface);
if (service.empty())
{
return;
}
auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
DBUS_PROPERTY_IFACE, "Set");
method.append(interface.c_str(), propertyName.c_str(), value);
bus.call_noreply(method);
}
const std::vector<std::string>
DBusHandler::getSubTreePaths(const std::string& objectPath,
const std::string& interface)
{
std::vector<std::string> paths;
auto& bus = DBusHandler::getBus();
auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
MAPPER_IFACE, "GetSubTreePaths");
method.append(objectPath.c_str());
method.append(0); // Depth 0 to search all
method.append(std::vector<std::string>({interface.c_str()}));
auto reply = bus.call(method);
reply.read(paths);
return paths;
}
} // namespace utils
} // namespace led
} // namespace phosphor