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)13 std::string DBusHandler::getService(const std::string& path,
14 const std::string& interface)
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
getAllProperties(const std::string & objectPath,const std::string & interface)40 PropertyMap DBusHandler::getAllProperties(const std::string& objectPath,
41 const std::string& interface)
42 {
43 PropertyMap properties;
44
45 auto& bus = DBusHandler::getBus();
46 auto service = getService(objectPath, interface);
47 if (service.empty())
48 {
49 return properties;
50 }
51
52 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
53 proIntf, "GetAll");
54 method.append(interface);
55
56 auto reply = bus.call(method);
57 reply.read(properties);
58
59 return properties;
60 }
61
62 // Get the property name
getProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName)63 PropertyValue DBusHandler::getProperty(const std::string& objectPath,
64 const std::string& interface,
65 const std::string& propertyName)
66 {
67 PropertyValue value{};
68
69 auto& bus = DBusHandler::getBus();
70 auto service = getService(objectPath, interface);
71 if (service.empty())
72 {
73 return value;
74 }
75
76 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
77 proIntf, "Get");
78 method.append(interface, propertyName);
79
80 auto reply = bus.call(method);
81 reply.read(value);
82
83 return value;
84 }
85
86 // Set property
setProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName,const PropertyValue & value)87 void DBusHandler::setProperty(
88 const std::string& objectPath, const std::string& interface,
89 const std::string& propertyName, const PropertyValue& value)
90 {
91 auto& bus = DBusHandler::getBus();
92 auto service = getService(objectPath, interface);
93 if (service.empty())
94 {
95 return;
96 }
97
98 auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
99 proIntf, "Set");
100 method.append(interface.c_str(), propertyName.c_str(), value);
101
102 bus.call_noreply(method);
103 }
104
getSubTreePaths(const std::string & objectPath,const std::string & interface)105 std::vector<std::string> DBusHandler::getSubTreePaths(
106 const std::string& objectPath, const std::string& interface)
107 {
108 std::vector<std::string> paths;
109
110 auto& bus = DBusHandler::getBus();
111
112 auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
113 "GetSubTreePaths");
114 method.append(objectPath.c_str());
115 method.append(0); // Depth 0 to search all
116 method.append(std::vector<std::string>({interface}));
117 auto reply = bus.call(method);
118
119 reply.read(paths);
120
121 return paths;
122 }
123
124 } // namespace utils
125 } // namespace led
126 } // namespace phosphor
127