Refactor to call the getProperty method

Uniformly use the getProperty method of utils.hpp to obtain values

Signed-off-by: George Liu <liuxiwei@ieisystem.com>
Change-Id: I9eae6bba6806215b51090637a7e42c8c8d90be87
diff --git a/selutility.cpp b/selutility.cpp
index b3bfa3e..c43a660 100644
--- a/selutility.cpp
+++ b/selutility.cpp
@@ -317,22 +317,13 @@
         "xyz.openbmc_project.Association.Definitions";
     static constexpr auto assocProp = "Associations";
 
-    auto service = ipmi::getService(bus, assocIntf, objPath);
-
-    // Read the Associations interface.
-    auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
-                                          propIntf, "Get");
-    methodCall.append(assocIntf);
-    methodCall.append(assocProp);
-
-    using AssociationList =
-        std::vector<std::tuple<std::string, std::string, std::string>>;
-
-    std::variant<AssociationList> list;
+    std::vector<ipmi::Association> assocs;
     try
     {
-        auto reply = bus.call(methodCall);
-        reply.read(list);
+        auto service = ipmi::getService(bus, assocIntf, objPath);
+        auto propValue = ipmi::getDbusProperty(bus, service, objPath, assocIntf,
+                                               assocProp);
+        assocs = std::get<std::vector<ipmi::Association>>(propValue);
     }
     catch (const std::exception& e)
     {
@@ -341,8 +332,6 @@
         elog<InternalFailure>();
     }
 
-    auto& assocs = std::get<AssociationList>(list);
-
     /*
      * Check if the log entry has any callout associations, if there is a
      * callout association try to match the inventory path to the corresponding
@@ -377,21 +366,15 @@
 {
     sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
 
-    auto service = ipmi::getService(bus, logEntryIntf, objPath);
+    static constexpr auto propTimeStamp = "Timestamp";
 
-    using namespace std::string_literals;
-    static const auto propTimeStamp = "Timestamp"s;
-
-    auto methodCall = bus.new_method_call(service.c_str(), objPath.c_str(),
-                                          propIntf, "Get");
-    methodCall.append(logEntryIntf);
-    methodCall.append(propTimeStamp);
-
-    std::variant<uint64_t> timeStamp;
+    uint64_t timeStamp;
     try
     {
-        auto reply = bus.call(methodCall);
-        reply.read(timeStamp);
+        auto service = ipmi::getService(bus, logEntryIntf, objPath);
+        auto propValue = ipmi::getDbusProperty(bus, service, objPath,
+                                               logEntryIntf, propTimeStamp);
+        timeStamp = std::get<uint64_t>(propValue);
     }
     catch (const std::exception& e)
     {
@@ -400,7 +383,7 @@
         elog<InternalFailure>();
     }
 
-    std::chrono::milliseconds chronoTimeStamp(std::get<uint64_t>(timeStamp));
+    std::chrono::milliseconds chronoTimeStamp(timeStamp);
 
     return std::chrono::duration_cast<std::chrono::seconds>(chronoTimeStamp);
 }
diff --git a/storagehandler.cpp b/storagehandler.cpp
index d384693..581a3de 100644
--- a/storagehandler.cpp
+++ b/storagehandler.cpp
@@ -552,16 +552,9 @@
     {
         sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
         auto service = ipmi::getService(bus, TIME_INTERFACE, BMC_TIME_PATH);
-        std::variant<uint64_t> value;
-
-        // Get bmc time
-        auto method = bus.new_method_call(service.c_str(), BMC_TIME_PATH,
-                                          DBUS_PROPERTIES, "Get");
-
-        method.append(TIME_INTERFACE, PROPERTY_ELAPSED);
-        auto reply = bus.call(method);
-        reply.read(value);
-        bmc_time_usec = std::get<uint64_t>(value);
+        auto propValue = ipmi::getDbusProperty(
+            bus, service, BMC_TIME_PATH, TIME_INTERFACE, PROPERTY_ELAPSED);
+        bmc_time_usec = std::get<uint64_t>(propValue);
     }
     catch (const InternalFailure& e)
     {
diff --git a/user_channel/channel_mgmt.cpp b/user_channel/channel_mgmt.cpp
index 50505aa..ac907de 100644
--- a/user_channel/channel_mgmt.cpp
+++ b/user_channel/channel_mgmt.cpp
@@ -25,6 +25,7 @@
 #include <unistd.h>
 
 #include <boost/interprocess/sync/scoped_lock.hpp>
+#include <ipmid/utils.hpp>
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/bus/match.hpp>
 #include <sdbusplus/server/object.hpp>
@@ -1331,35 +1332,6 @@
     return 0;
 }
 
-int ChannelConfig::getDbusProperty(const std::string& service,
-                                   const std::string& objPath,
-                                   const std::string& interface,
-                                   const std::string& property,
-                                   DbusVariant& value)
-{
-    try
-    {
-        auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
-                                          "org.freedesktop.DBus.Properties",
-                                          "Get");
-
-        method.append(interface, property);
-
-        auto reply = bus.call(method);
-        reply.read(value);
-    }
-    catch (const sdbusplus::exception_t& e)
-    {
-        log<level::DEBUG>("get-property failed",
-                          entry("SERVICE=%s", service.c_str()),
-                          entry("OBJPATH=%s", objPath.c_str()),
-                          entry("INTERFACE=%s", interface.c_str()),
-                          entry("PROP=%s", property.c_str()));
-        return -EIO;
-    }
-    return 0;
-}
-
 int ChannelConfig::syncNetworkChannelConfig()
 {
     boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
@@ -1376,17 +1348,11 @@
                 std::string networkIntfObj =
                     std::string(networkIntfObjectBasePath) + "/" +
                     channelData[chNum].chName;
-                DbusVariant variant;
-                if (0 != getDbusProperty(networkIntfServiceName, networkIntfObj,
-                                         networkChConfigIntfName,
-                                         privilegePropertyString, variant))
-                {
-                    log<level::DEBUG>("Network interface does not exist",
-                                      entry("INTERFACE=%s",
-                                            channelData[chNum].chName.c_str()));
-                    continue;
-                }
-                intfPrivStr = std::get<std::string>(variant);
+                auto propValue = ipmi::getDbusProperty(
+                    bus, networkIntfServiceName, networkIntfObj,
+                    networkChConfigIntfName, privilegePropertyString);
+
+                intfPrivStr = std::get<std::string>(propValue);
                 intfPriv =
                     static_cast<uint8_t>(convertToPrivLimitIndex(intfPrivStr));
             }
diff --git a/user_channel/channel_mgmt.hpp b/user_channel/channel_mgmt.hpp
index a8de017..39aa116 100644
--- a/user_channel/channel_mgmt.hpp
+++ b/user_channel/channel_mgmt.hpp
@@ -322,20 +322,6 @@
                         const std::string& interface,
                         const std::string& property, const DbusVariant& value);
 
-    /** @brief function to get D-Bus property value
-     *
-     *  @param[in] service - service name
-     *  @param[in] objPath - object path
-     *  @param[in] interface - interface
-     *  @param[in] property - property name
-     *  @param[out] value - property value
-     *
-     *  @return 0 for success, -errno for failure.
-     */
-    int getDbusProperty(const std::string& service, const std::string& objPath,
-                        const std::string& interface,
-                        const std::string& property, DbusVariant& value);
-
     /** @brief function to read json config file
      *
      *  @param[in] configFile - configuration file name
diff --git a/whitelist-filter.cpp b/whitelist-filter.cpp
index 994925e..2c86464 100644
--- a/whitelist-filter.cpp
+++ b/whitelist-filter.cpp
@@ -94,31 +94,30 @@
             return;
         }
 
-        bus->async_method_call(
-            [this, index = std::distance(&*std::begin(devices), &dev)](
-                boost::system::error_code ec, ipmi::Value v) {
-            if (ec)
-            {
-                log<level::ERR>("Error in RestrictionMode Get");
-                // Fail-safe to true.
-                restrictedMode[index] = true;
-                return;
-            }
+        std::string mode;
+        try
+        {
+            auto propValue = ipmi::getDbusProperty(
+                *bus, restrictionModeService, restrictionModeSetting,
+                restrictionModeIntf, "RestrictionMode");
+            mode = std::get<std::string>(propValue);
+        }
+        catch (const std::exception& e)
+        {
+            log<level::ERR>("Error in RestrictionMode Get");
+            // Fail-safe to true.
+            size_t index = std::distance(&*std::begin(devices), &dev);
+            restrictedMode[index] = true;
+        }
 
-            auto mode = std::get<std::string>(v);
-            auto restrictionMode =
-                RestrictionMode::convertModesFromString(mode);
+        auto restrictionMode = RestrictionMode::convertModesFromString(mode);
 
-            bool restrictMode =
-                (restrictionMode == RestrictionMode::Modes::Allowlist);
-            restrictedMode.emplace_back(restrictMode);
+        bool restrictMode =
+            (restrictionMode == RestrictionMode::Modes::Allowlist);
+        restrictedMode.emplace_back(restrictMode);
 
-            log<level::INFO>((restrictMode ? "Set restrictedMode = true"
-                                           : "Set restrictedMode = false"));
-        },
-            restrictionModeService, restrictionModeSetting,
-            "org.freedesktop.DBus.Properties", "Get", restrictionModeIntf,
-            "RestrictionMode");
+        log<level::INFO>((restrictMode ? "Set restrictedMode = true"
+                                       : "Set restrictedMode = false"));
     }
 }