xref: /openbmc/phosphor-led-manager/utils.cpp (revision 9c2877ae)
1 #include "utils.hpp"
2 
3 #include <phosphor-logging/log.hpp>
4 
5 namespace phosphor
6 {
7 namespace led
8 {
9 namespace utils
10 {
11 
12 using namespace phosphor::logging;
13 
14 // Get service name
15 const 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 
50 // Get all properties
51 const 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 
74 // Get the property name
75 const 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 
99 // Set property
100 void 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 
119 const 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 
139 } // namespace utils
140 } // namespace led
141 } // namespace phosphor
142