blob: 41b78b91fa4a2205ef018facab2388ca47f4ca3f [file] [log] [blame]
George Liu1c737af2020-10-16 09:07:02 +08001#include "utils.hpp"
2
3#include <phosphor-logging/log.hpp>
4
5namespace phosphor
6{
7namespace led
8{
9namespace utils
10{
11
12using namespace phosphor::logging;
13
14// Get service name
15const std::string DBusHandler::getService(const std::string& path,
16 const std::string& interface) const
17{
18
19 using InterfaceList = std::vector<std::string>;
20 std::map<std::string, std::vector<std::string>> mapperResponse;
21
22 auto& bus = DBusHandler::getBus();
23
24 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
25 MAPPER_IFACE, "GetObject");
26 mapper.append(path, InterfaceList({interface}));
27
28 auto mapperResponseMsg = bus.call(mapper);
29 if (mapperResponseMsg.is_method_error())
30 {
31 log<level::ERR>("Failed to invoke ObjectMapper method",
32 entry("OBJECT_PATH=%s", path.c_str()),
33 entry("INTERFACE=%s", interface.c_str()));
34 return "";
35 }
36
37 mapperResponseMsg.read(mapperResponse);
38 if (mapperResponse.empty())
39 {
40 log<level::ERR>("Failed to read getService mapper response",
41 entry("OBJECT_PATH=%s", path.c_str()),
42 entry("INTERFACE=%s", interface.c_str()));
43 return "";
44 }
45
46 // the value here will be the service name
47 return mapperResponse.cbegin()->first;
48}
49
George Liub6151622020-11-23 18:16:18 +080050// Get all properties
51const PropertyMap
52 DBusHandler::getAllProperties(const std::string& objectPath,
53 const std::string& interface) const
54{
55 PropertyMap properties;
56
57 auto& bus = DBusHandler::getBus();
58 auto service = getService(objectPath, interface);
59 if (service.empty())
60 {
61 return properties;
62 }
63
64 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
65 DBUS_PROPERTY_IFACE, "GetAll");
66 method.append(interface);
67
68 auto reply = bus.call(method);
69 reply.read(properties);
70
71 return properties;
72}
73
George Liu4c5f5332020-10-10 17:04:28 +080074// Get the property name
75const PropertyValue
76 DBusHandler::getProperty(const std::string& objectPath,
77 const std::string& interface,
78 const std::string& propertyName) const
79{
80 PropertyValue value{};
81
82 auto& bus = DBusHandler::getBus();
83 auto service = getService(objectPath, interface);
84 if (service.empty())
85 {
86 return value;
87 }
88
89 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
90 DBUS_PROPERTY_IFACE, "Get");
91 method.append(interface, propertyName);
92
93 auto reply = bus.call(method);
94 reply.read(value);
95
96 return value;
97}
98
George Liu1c737af2020-10-16 09:07:02 +080099// Set property
100void DBusHandler::setProperty(const std::string& objectPath,
101 const std::string& interface,
102 const std::string& propertyName,
103 const PropertyValue& value) const
104{
105 auto& bus = DBusHandler::getBus();
106 auto service = getService(objectPath, interface);
107 if (service.empty())
108 {
109 return;
110 }
111
112 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
113 DBUS_PROPERTY_IFACE, "Set");
114 method.append(interface.c_str(), propertyName.c_str(), value);
115
116 bus.call_noreply(method);
117}
118
George Liu616a0712021-02-18 10:50:24 +0800119const std::vector<std::string>
120 DBusHandler::getSubTreePaths(const std::string& objectPath,
121 const std::string& interface)
122{
123 std::vector<std::string> paths;
124
125 auto& bus = DBusHandler::getBus();
126
127 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
128 MAPPER_IFACE, "GetSubTreePaths");
129 method.append(objectPath.c_str());
130 method.append(0); // Depth 0 to search all
131 method.append(std::vector<std::string>({interface.c_str()}));
132 auto reply = bus.call(method);
133
134 reply.read(paths);
135
136 return paths;
137}
138
George Liu1c737af2020-10-16 09:07:02 +0800139} // namespace utils
140} // namespace led
George Liu87fd11c2020-11-23 16:40:14 +0800141} // namespace phosphor