xref: /openbmc/phosphor-led-manager/utils.cpp (revision e9fb5c6a)
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
13 const 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);
27     mapperResponseMsg.read(mapperResponse);
28     if (mapperResponse.empty())
29     {
30         lg2::error(
31             "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}",
32             "PATH", path, "INTERFACE", interface);
33         return "";
34     }
35 
36     // the value here will be the service name
37     return mapperResponse.cbegin()->first;
38 }
39 
40 // Get all properties
41 const 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 
64 // Get the property name
65 const 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 
89 // Set property
90 void 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 
109 const 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 
129 } // namespace utils
130 } // namespace led
131 } // namespace phosphor
132