blob: 7a565a451fd2d11f8a413b3f67c9164c23b65381 [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);
George Liu1c737af2020-10-16 09:07:02 +080029 mapperResponseMsg.read(mapperResponse);
30 if (mapperResponse.empty())
31 {
32 log<level::ERR>("Failed to read getService mapper response",
33 entry("OBJECT_PATH=%s", path.c_str()),
34 entry("INTERFACE=%s", interface.c_str()));
35 return "";
36 }
37
38 // the value here will be the service name
39 return mapperResponse.cbegin()->first;
40}
41
George Liub6151622020-11-23 18:16:18 +080042// Get all properties
43const PropertyMap
44 DBusHandler::getAllProperties(const std::string& objectPath,
45 const std::string& interface) const
46{
47 PropertyMap properties;
48
49 auto& bus = DBusHandler::getBus();
50 auto service = getService(objectPath, interface);
51 if (service.empty())
52 {
53 return properties;
54 }
55
56 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
57 DBUS_PROPERTY_IFACE, "GetAll");
58 method.append(interface);
59
60 auto reply = bus.call(method);
61 reply.read(properties);
62
63 return properties;
64}
65
George Liu4c5f5332020-10-10 17:04:28 +080066// Get the property name
67const PropertyValue
68 DBusHandler::getProperty(const std::string& objectPath,
69 const std::string& interface,
70 const std::string& propertyName) const
71{
72 PropertyValue value{};
73
74 auto& bus = DBusHandler::getBus();
75 auto service = getService(objectPath, interface);
76 if (service.empty())
77 {
78 return value;
79 }
80
81 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
82 DBUS_PROPERTY_IFACE, "Get");
83 method.append(interface, propertyName);
84
85 auto reply = bus.call(method);
86 reply.read(value);
87
88 return value;
89}
90
George Liu1c737af2020-10-16 09:07:02 +080091// Set property
92void DBusHandler::setProperty(const std::string& objectPath,
93 const std::string& interface,
94 const std::string& propertyName,
95 const PropertyValue& value) const
96{
97 auto& bus = DBusHandler::getBus();
98 auto service = getService(objectPath, interface);
99 if (service.empty())
100 {
101 return;
102 }
103
104 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
105 DBUS_PROPERTY_IFACE, "Set");
106 method.append(interface.c_str(), propertyName.c_str(), value);
107
108 bus.call_noreply(method);
109}
110
George Liu616a0712021-02-18 10:50:24 +0800111const std::vector<std::string>
112 DBusHandler::getSubTreePaths(const std::string& objectPath,
113 const std::string& interface)
114{
115 std::vector<std::string> paths;
116
117 auto& bus = DBusHandler::getBus();
118
119 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
120 MAPPER_IFACE, "GetSubTreePaths");
121 method.append(objectPath.c_str());
122 method.append(0); // Depth 0 to search all
123 method.append(std::vector<std::string>({interface.c_str()}));
124 auto reply = bus.call(method);
125
126 reply.read(paths);
127
128 return paths;
129}
130
George Liu1c737af2020-10-16 09:07:02 +0800131} // namespace utils
132} // namespace led
George Liu87fd11c2020-11-23 16:40:14 +0800133} // namespace phosphor