common: dbus helper for fetching configuration

Since currently the EM schemas and PDI interfaces for configuration
parsing are not yet ready, we cannot use generated client bindings to
fetch the configuration.

Thus providing a helper function 'dbusGetRequiredProperty' for code
updaters to fetch configuration. Call sites to this helper function
should then later be replaced by using the generated client.

Change-Id: Icf3a34d58a8994a13f2a719ad13ca28a59cb77d0
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/common/include/dbus_helper.hpp b/common/include/dbus_helper.hpp
new file mode 100644
index 0000000..e2e620a
--- /dev/null
+++ b/common/include/dbus_helper.hpp
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/async.hpp>
+#include <sdbusplus/async/context.hpp>
+
+#include <optional>
+
+PHOSPHOR_LOG2_USING;
+
+template <typename T>
+sdbusplus::async::task<std::optional<T>> dbusGetRequiredProperty(
+    sdbusplus::async::context& ctx, const std::string& service,
+    const std::string& path, const std::string& intf,
+    const std::string& property)
+{
+    auto client =
+        sdbusplus::async::proxy().service(service).path(path).interface(
+            "org.freedesktop.DBus.Properties");
+
+    std::optional<T> opt = std::nullopt;
+    try
+    {
+        std::variant<T> result =
+            co_await client.call<std::variant<T>>(ctx, "Get", intf, property);
+
+        opt = std::get<T>(result);
+    }
+    catch (std::exception& e)
+    {
+        error("Missing property {PROPERTY} on path {PATH}, interface {INTF}",
+              "PROPERTY", property, "PATH", path, "INTF", intf);
+    }
+    co_return opt;
+}