xref: /openbmc/phosphor-led-manager/utils.cpp (revision 1f0b715a)
11c737afcSGeorge Liu #include "utils.hpp"
21c737afcSGeorge Liu 
3e9fb5c6aSGeorge Liu #include <phosphor-logging/lg2.hpp>
41c737afcSGeorge Liu 
51c737afcSGeorge Liu namespace phosphor
61c737afcSGeorge Liu {
71c737afcSGeorge Liu namespace led
81c737afcSGeorge Liu {
91c737afcSGeorge Liu namespace utils
101c737afcSGeorge Liu {
111c737afcSGeorge Liu 
121c737afcSGeorge Liu // Get service name
getService(const std::string & path,const std::string & interface) const131c737afcSGeorge Liu const std::string DBusHandler::getService(const std::string& path,
141c737afcSGeorge Liu                                           const std::string& interface) const
151c737afcSGeorge Liu {
161c737afcSGeorge Liu     using InterfaceList = std::vector<std::string>;
17f2044037SPatrick Williams     std::unordered_map<std::string, std::vector<std::string>> mapperResponse;
181c737afcSGeorge Liu 
191c737afcSGeorge Liu     auto& bus = DBusHandler::getBus();
201c737afcSGeorge Liu 
21*1f0b715aSGeorge Liu     auto mapper = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
22*1f0b715aSGeorge Liu                                       "GetObject");
231c737afcSGeorge Liu     mapper.append(path, InterfaceList({interface}));
241c737afcSGeorge Liu 
251c737afcSGeorge Liu     auto mapperResponseMsg = bus.call(mapper);
261c737afcSGeorge Liu     mapperResponseMsg.read(mapperResponse);
271c737afcSGeorge Liu     if (mapperResponse.empty())
281c737afcSGeorge Liu     {
29e9fb5c6aSGeorge Liu         lg2::error(
30e9fb5c6aSGeorge Liu             "Failed to read getService mapper response, OBJECT_PATH = {PATH}, INTERFACE = {INTERFACE}",
31e9fb5c6aSGeorge Liu             "PATH", path, "INTERFACE", interface);
321c737afcSGeorge Liu         return "";
331c737afcSGeorge Liu     }
341c737afcSGeorge Liu 
351c737afcSGeorge Liu     // the value here will be the service name
361c737afcSGeorge Liu     return mapperResponse.cbegin()->first;
371c737afcSGeorge Liu }
381c737afcSGeorge Liu 
39b6151623SGeorge Liu // Get all properties
40b6151623SGeorge Liu const PropertyMap
getAllProperties(const std::string & objectPath,const std::string & interface) const41b6151623SGeorge Liu     DBusHandler::getAllProperties(const std::string& objectPath,
42b6151623SGeorge Liu                                   const std::string& interface) const
43b6151623SGeorge Liu {
44b6151623SGeorge Liu     PropertyMap properties;
45b6151623SGeorge Liu 
46b6151623SGeorge Liu     auto& bus = DBusHandler::getBus();
47b6151623SGeorge Liu     auto service = getService(objectPath, interface);
48b6151623SGeorge Liu     if (service.empty())
49b6151623SGeorge Liu     {
50b6151623SGeorge Liu         return properties;
51b6151623SGeorge Liu     }
52b6151623SGeorge Liu 
53b6151623SGeorge Liu     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
54*1f0b715aSGeorge Liu                                       proIntf, "GetAll");
55b6151623SGeorge Liu     method.append(interface);
56b6151623SGeorge Liu 
57b6151623SGeorge Liu     auto reply = bus.call(method);
58b6151623SGeorge Liu     reply.read(properties);
59b6151623SGeorge Liu 
60b6151623SGeorge Liu     return properties;
61b6151623SGeorge Liu }
62b6151623SGeorge Liu 
634c5f5337SGeorge Liu // Get the property name
644c5f5337SGeorge Liu const PropertyValue
getProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName) const654c5f5337SGeorge Liu     DBusHandler::getProperty(const std::string& objectPath,
664c5f5337SGeorge Liu                              const std::string& interface,
674c5f5337SGeorge Liu                              const std::string& propertyName) const
684c5f5337SGeorge Liu {
694c5f5337SGeorge Liu     PropertyValue value{};
704c5f5337SGeorge Liu 
714c5f5337SGeorge Liu     auto& bus = DBusHandler::getBus();
724c5f5337SGeorge Liu     auto service = getService(objectPath, interface);
734c5f5337SGeorge Liu     if (service.empty())
744c5f5337SGeorge Liu     {
754c5f5337SGeorge Liu         return value;
764c5f5337SGeorge Liu     }
774c5f5337SGeorge Liu 
784c5f5337SGeorge Liu     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
79*1f0b715aSGeorge Liu                                       proIntf, "Get");
804c5f5337SGeorge Liu     method.append(interface, propertyName);
814c5f5337SGeorge Liu 
824c5f5337SGeorge Liu     auto reply = bus.call(method);
834c5f5337SGeorge Liu     reply.read(value);
844c5f5337SGeorge Liu 
854c5f5337SGeorge Liu     return value;
864c5f5337SGeorge Liu }
874c5f5337SGeorge Liu 
881c737afcSGeorge Liu // Set property
setProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName,const PropertyValue & value) const891c737afcSGeorge Liu void DBusHandler::setProperty(const std::string& objectPath,
901c737afcSGeorge Liu                               const std::string& interface,
911c737afcSGeorge Liu                               const std::string& propertyName,
921c737afcSGeorge Liu                               const PropertyValue& value) const
931c737afcSGeorge Liu {
941c737afcSGeorge Liu     auto& bus = DBusHandler::getBus();
951c737afcSGeorge Liu     auto service = getService(objectPath, interface);
961c737afcSGeorge Liu     if (service.empty())
971c737afcSGeorge Liu     {
981c737afcSGeorge Liu         return;
991c737afcSGeorge Liu     }
1001c737afcSGeorge Liu 
1011c737afcSGeorge Liu     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
102*1f0b715aSGeorge Liu                                       proIntf, "Set");
1031c737afcSGeorge Liu     method.append(interface.c_str(), propertyName.c_str(), value);
1041c737afcSGeorge Liu 
1051c737afcSGeorge Liu     bus.call_noreply(method);
1061c737afcSGeorge Liu }
1071c737afcSGeorge Liu 
108616a0716SGeorge Liu const std::vector<std::string>
getSubTreePaths(const std::string & objectPath,const std::string & interface)109616a0716SGeorge Liu     DBusHandler::getSubTreePaths(const std::string& objectPath,
110616a0716SGeorge Liu                                  const std::string& interface)
111616a0716SGeorge Liu {
112616a0716SGeorge Liu     std::vector<std::string> paths;
113616a0716SGeorge Liu 
114616a0716SGeorge Liu     auto& bus = DBusHandler::getBus();
115616a0716SGeorge Liu 
116*1f0b715aSGeorge Liu     auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
117*1f0b715aSGeorge Liu                                       "GetSubTreePaths");
118616a0716SGeorge Liu     method.append(objectPath.c_str());
119616a0716SGeorge Liu     method.append(0); // Depth 0 to search all
120616a0716SGeorge Liu     method.append(std::vector<std::string>({interface.c_str()}));
121616a0716SGeorge Liu     auto reply = bus.call(method);
122616a0716SGeorge Liu 
123616a0716SGeorge Liu     reply.read(paths);
124616a0716SGeorge Liu 
125616a0716SGeorge Liu     return paths;
126616a0716SGeorge Liu }
127616a0716SGeorge Liu 
1281c737afcSGeorge Liu } // namespace utils
1291c737afcSGeorge Liu } // namespace led
1301c737afcSGeorge Liu } // namespace phosphor
131