1 #include "utils.hpp"
2
3 #include <phosphor-logging/lg2.hpp>
4
5 namespace phosphor
6 {
7 namespace led
8 {
9 namespace utils
10 {
11
12 // Get service name
getService(const std::string & path,const std::string & interface) const13 const std::string DBusHandler::getService(const std::string& path,
14 const std::string& interface) const
15 {
16 using InterfaceList = std::vector<std::string>;
17 std::unordered_map<std::string, std::vector<std::string>> mapperResponse;
18
19 auto& bus = DBusHandler::getBus();
20
21 auto mapper = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
22 "GetObject");
23 mapper.append(path, InterfaceList({interface}));
24
25 auto mapperResponseMsg = bus.call(mapper);
26 mapperResponseMsg.read(mapperResponse);
27 if (mapperResponse.empty())
28 {
29 lg2::error(
30 "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}",
31 "PATH", path, "INTERFACE", interface);
32 return "";
33 }
34
35 // the value here will be the service name
36 return mapperResponse.cbegin()->first;
37 }
38
39 // Get all properties
40 const PropertyMap
getAllProperties(const std::string & objectPath,const std::string & interface) const41 DBusHandler::getAllProperties(const std::string& objectPath,
42 const std::string& interface) const
43 {
44 PropertyMap properties;
45
46 auto& bus = DBusHandler::getBus();
47 auto service = getService(objectPath, interface);
48 if (service.empty())
49 {
50 return properties;
51 }
52
53 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
54 proIntf, "GetAll");
55 method.append(interface);
56
57 auto reply = bus.call(method);
58 reply.read(properties);
59
60 return properties;
61 }
62
63 // Get the property name
64 const PropertyValue
getProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName) const65 DBusHandler::getProperty(const std::string& objectPath,
66 const std::string& interface,
67 const std::string& propertyName) const
68 {
69 PropertyValue value{};
70
71 auto& bus = DBusHandler::getBus();
72 auto service = getService(objectPath, interface);
73 if (service.empty())
74 {
75 return value;
76 }
77
78 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
79 proIntf, "Get");
80 method.append(interface, propertyName);
81
82 auto reply = bus.call(method);
83 reply.read(value);
84
85 return value;
86 }
87
88 // Set property
setProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName,const PropertyValue & value) const89 void DBusHandler::setProperty(const std::string& objectPath,
90 const std::string& interface,
91 const std::string& propertyName,
92 const PropertyValue& value) const
93 {
94 auto& bus = DBusHandler::getBus();
95 auto service = getService(objectPath, interface);
96 if (service.empty())
97 {
98 return;
99 }
100
101 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
102 proIntf, "Set");
103 method.append(interface.c_str(), propertyName.c_str(), value);
104
105 bus.call_noreply(method);
106 }
107
108 const std::vector<std::string>
getSubTreePaths(const std::string & objectPath,const std::string & interface)109 DBusHandler::getSubTreePaths(const std::string& objectPath,
110 const std::string& interface)
111 {
112 std::vector<std::string> paths;
113
114 auto& bus = DBusHandler::getBus();
115
116 auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
117 "GetSubTreePaths");
118 method.append(objectPath.c_str());
119 method.append(0); // Depth 0 to search all
120 method.append(std::vector<std::string>({interface.c_str()}));
121 auto reply = bus.call(method);
122
123 reply.read(paths);
124
125 return paths;
126 }
127
128 } // namespace utils
129 } // namespace led
130 } // namespace phosphor
131