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