xref: /openbmc/phosphor-led-manager/utils.cpp (revision 3d68ed5fdf4d8282f15b3a3f593ed99580adf821)
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)13405ea286SGeorge Liu std::string DBusHandler::getService(const std::string& path,
14f0592559SGeorge Liu                                     const std::string& interface)
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 
211f0b715aSGeorge Liu     auto mapper = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
221f0b715aSGeorge 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
getAllProperties(const std::string & objectPath,const std::string & interface)40405ea286SGeorge Liu PropertyMap DBusHandler::getAllProperties(const std::string& objectPath,
41f0592559SGeorge Liu                                           const std::string& interface)
42b6151623SGeorge Liu {
43b6151623SGeorge Liu     PropertyMap properties;
44b6151623SGeorge Liu 
45b6151623SGeorge Liu     auto& bus = DBusHandler::getBus();
46b6151623SGeorge Liu     auto service = getService(objectPath, interface);
47b6151623SGeorge Liu     if (service.empty())
48b6151623SGeorge Liu     {
49b6151623SGeorge Liu         return properties;
50b6151623SGeorge Liu     }
51b6151623SGeorge Liu 
52b6151623SGeorge Liu     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
531f0b715aSGeorge Liu                                       proIntf, "GetAll");
54b6151623SGeorge Liu     method.append(interface);
55b6151623SGeorge Liu 
56b6151623SGeorge Liu     auto reply = bus.call(method);
57b6151623SGeorge Liu     reply.read(properties);
58b6151623SGeorge Liu 
59b6151623SGeorge Liu     return properties;
60b6151623SGeorge Liu }
61b6151623SGeorge Liu 
624c5f5337SGeorge Liu // Get the property name
getProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName)63405ea286SGeorge Liu PropertyValue DBusHandler::getProperty(const std::string& objectPath,
64405ea286SGeorge Liu                                        const std::string& interface,
65f0592559SGeorge Liu                                        const std::string& propertyName)
664c5f5337SGeorge Liu {
674c5f5337SGeorge Liu     PropertyValue value{};
684c5f5337SGeorge Liu 
694c5f5337SGeorge Liu     auto& bus = DBusHandler::getBus();
704c5f5337SGeorge Liu     auto service = getService(objectPath, interface);
714c5f5337SGeorge Liu     if (service.empty())
724c5f5337SGeorge Liu     {
734c5f5337SGeorge Liu         return value;
744c5f5337SGeorge Liu     }
754c5f5337SGeorge Liu 
764c5f5337SGeorge Liu     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
771f0b715aSGeorge Liu                                       proIntf, "Get");
784c5f5337SGeorge Liu     method.append(interface, propertyName);
794c5f5337SGeorge Liu 
804c5f5337SGeorge Liu     auto reply = bus.call(method);
814c5f5337SGeorge Liu     reply.read(value);
824c5f5337SGeorge Liu 
834c5f5337SGeorge Liu     return value;
844c5f5337SGeorge Liu }
854c5f5337SGeorge Liu 
861c737afcSGeorge Liu // Set property
setProperty(const std::string & objectPath,const std::string & interface,const std::string & propertyName,const PropertyValue & value)87543ac9f1SPatrick Williams void DBusHandler::setProperty(
88543ac9f1SPatrick Williams     const std::string& objectPath, const std::string& interface,
89f0592559SGeorge Liu     const std::string& propertyName, const PropertyValue& value)
901c737afcSGeorge Liu {
911c737afcSGeorge Liu     auto& bus = DBusHandler::getBus();
921c737afcSGeorge Liu     auto service = getService(objectPath, interface);
931c737afcSGeorge Liu     if (service.empty())
941c737afcSGeorge Liu     {
951c737afcSGeorge Liu         return;
961c737afcSGeorge Liu     }
971c737afcSGeorge Liu 
981c737afcSGeorge Liu     auto method = bus.new_method_call(service.c_str(), objectPath.c_str(),
991f0b715aSGeorge Liu                                       proIntf, "Set");
1001c737afcSGeorge Liu     method.append(interface.c_str(), propertyName.c_str(), value);
1011c737afcSGeorge Liu 
1021c737afcSGeorge Liu     bus.call_noreply(method);
1031c737afcSGeorge Liu }
1041c737afcSGeorge Liu 
getSubTreePaths(const std::string & objectPath,const std::string & interface)105405ea286SGeorge Liu std::vector<std::string> DBusHandler::getSubTreePaths(
106f0592559SGeorge Liu     const std::string& objectPath, const std::string& interface)
107616a0716SGeorge Liu {
108616a0716SGeorge Liu     std::vector<std::string> paths;
109616a0716SGeorge Liu 
110616a0716SGeorge Liu     auto& bus = DBusHandler::getBus();
111616a0716SGeorge Liu 
1121f0b715aSGeorge Liu     auto method = bus.new_method_call(mapperBusName, mapperObjPath, mapperIntf,
1131f0b715aSGeorge Liu                                       "GetSubTreePaths");
114616a0716SGeorge Liu     method.append(objectPath.c_str());
115616a0716SGeorge Liu     method.append(0); // Depth 0 to search all
116*3d68ed5fSGeorge Liu     method.append(std::vector<std::string>({interface}));
117616a0716SGeorge Liu     auto reply = bus.call(method);
118616a0716SGeorge Liu 
119616a0716SGeorge Liu     reply.read(paths);
120616a0716SGeorge Liu 
121616a0716SGeorge Liu     return paths;
122616a0716SGeorge Liu }
123616a0716SGeorge Liu 
1241c737afcSGeorge Liu } // namespace utils
1251c737afcSGeorge Liu } // namespace led
1261c737afcSGeorge Liu } // namespace phosphor
127