-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.cpp
126 lines (100 loc) · 3.43 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
#include "utils.hpp"
#include <phosphor-logging/lg2.hpp>
namespace phosphor
{
namespace led
{
namespace utils
{
// Get service name
std::string DBusHandler::getService(const std::string& path,
const std::string& interface)
{
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(mapperBusName, mapperObjPath, mapperIntf,
"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
PropertyMap DBusHandler::getAllProperties(const std::string& objectPath,
const std::string& interface)
{
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(),
proIntf, "GetAll");
method.append(interface);
auto reply = bus.call(method);
reply.read(properties);
return properties;
}
// Get the property name
PropertyValue DBusHandler::getProperty(const std::string& objectPath,
const std::string& interface,
const std::string& propertyName)
{
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(),
proIntf, "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)
{
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(),
proIntf, "Set");
method.append(interface.c_str(), propertyName.c_str(), value);
bus.call_noreply(method);
}
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(mapperBusName, mapperObjPath, mapperIntf,
"GetSubTreePaths");
method.append(objectPath.c_str());
method.append(0); // Depth 0 to search all
method.append(std::vector<std::string>({interface}));
auto reply = bus.call(method);
reply.read(paths);
return paths;
}
} // namespace utils
} // namespace led
} // namespace phosphor