Add utility for D-Bus

Putting all D-Bus related operation to the utils file.

Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I934e519bca43b73da01ed09fc73f6c7cdc795e1f
diff --git a/manager.cpp b/manager.cpp
index 7741933..d19eeae 100644
--- a/manager.cpp
+++ b/manager.cpp
@@ -12,6 +12,11 @@
 namespace led
 {
 
+using namespace phosphor::logging;
+
+static constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
+static constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
+
 // Assert -or- De-assert
 bool Manager::setGroupState(const std::string& path, bool assert,
                             group& ledsAssert, group& ledsDeAssert)
@@ -101,16 +106,6 @@
 /** @brief Run through the map and apply action on the LEDs */
 void Manager::driveLEDs(group& ledsAssert, group& ledsDeAssert)
 {
-    using namespace phosphor::logging;
-    // Map of physical LED dbus paths to their Service providers
-    populateObjectMap();
-
-    if (phyLeds.empty())
-    {
-        // Error message is inside the map construction logic.
-        return;
-    }
-
     // This order of LED operation is important.
     if (ledsDeAssert.size())
     {
@@ -142,24 +137,30 @@
                                Layout::Action action, uint8_t dutyOn,
                                const uint16_t period)
 {
-    using namespace phosphor::logging;
-
-    auto service = phyLeds.find(objPath);
-    if (service == phyLeds.end() || service->second.empty())
+    try
     {
-        log<level::ERR>("No service providers for physical LED",
-                        entry("PATH=%s", objPath.c_str()));
-        return;
+        // If Blink, set its property
+        if (action == Layout::Action::Blink)
+        {
+            PropertyValue dutyOnValue{dutyOn};
+            PropertyValue periodValue{period};
+
+            dBusHandler.setProperty(objPath, PHY_LED_IFACE, "DutyOn",
+                                    dutyOnValue);
+            dBusHandler.setProperty(objPath, PHY_LED_IFACE, "Period",
+                                    periodValue);
+        }
+
+        PropertyValue actionValue{getPhysicalAction(action)};
+        dBusHandler.setProperty(objPath, PHY_LED_IFACE, "State", actionValue);
+    }
+    catch (const std::exception& e)
+    {
+        log<level::ERR>("Error setting property for physical LED",
+                        entry("ERROR=%s", e.what()),
+                        entry("OBJECT_PATH=%s", objPath.c_str()));
     }
 
-    // If Blink, set its property
-    if (action == Layout::Action::Blink)
-    {
-        drivePhysicalLED(service->second, objPath, "DutyOn", dutyOn);
-        drivePhysicalLED(service->second, objPath, "Period", period);
-    }
-    drivePhysicalLED(service->second, objPath, "State",
-                     getPhysicalAction(action));
     return;
 }
 
@@ -185,69 +186,5 @@
     }
 }
 
-/** Populates a map with physical LED paths to its service providers */
-void Manager::populateObjectMap()
-{
-    using namespace phosphor::logging;
-
-    // Mapper dbus constructs
-    constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
-    constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper";
-    constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper";
-
-    // Needed to be passed to get the SubTree level
-    auto depth = 0;
-
-    // Clean start
-    phyLeds.clear();
-
-    // Make a mapper call
-    auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH,
-                                          MAPPER_IFACE, "GetSubTree");
-    // Cook rest of the things.
-    mapperCall.append(PHY_LED_PATH);
-    mapperCall.append(depth);
-    mapperCall.append(std::vector<std::string>({PHY_LED_IFACE}));
-
-    auto reply = bus.call(mapperCall);
-    if (reply.is_method_error())
-    {
-        // Its okay if we do not see a corresponding physical LED.
-        log<level::INFO>("Error looking up Physical LED services",
-                         entry("PATH=%s", PHY_LED_PATH));
-        return;
-    }
-
-    // Response by mapper in the case of success
-    std::map<std::string, std::map<std::string, std::vector<std::string>>>
-        objectTree;
-
-    // This is the dict of object paths - service names - interfaces
-    try
-    {
-        reply.read(objectTree);
-    }
-    catch (const sdbusplus::exception::SdBusError& e)
-    {
-        log<level::ERR>("Failed to parse Physical LED service lookup",
-                        entry("ERROR=%s", e.what()),
-                        entry("REPLY_SIG=%s", reply.get_signature()));
-        return;
-    }
-    if (objectTree.empty())
-    {
-        log<level::INFO>("Physical LED lookup did not return any services",
-                         entry("PATH=%s", PHY_LED_PATH));
-        return;
-    }
-
-    // Now construct our path -> Service name map.
-    for (const auto& iter : objectTree)
-    {
-        phyLeds.emplace(iter.first, iter.second.begin()->first);
-    }
-    return;
-}
-
 } // namespace led
 } // namespace phosphor