xref: /openbmc/phosphor-bmc-code-mgmt/common/include/dbus_helper.hpp (revision 449e793c823154561a44b7827592ce73d877c834)
1 #pragma once
2 
3 #include <phosphor-logging/lg2.hpp>
4 #include <sdbusplus/async.hpp>
5 #include <sdbusplus/async/context.hpp>
6 
7 #include <optional>
8 
9 PHOSPHOR_LOG2_USING;
10 
11 template <typename T>
dbusGetRequiredProperty(sdbusplus::async::context & ctx,const std::string & service,const std::string & path,const std::string & intf,const std::string & property)12 sdbusplus::async::task<std::optional<T>> dbusGetRequiredProperty(
13     sdbusplus::async::context& ctx, const std::string& service,
14     const std::string& path, const std::string& intf,
15     const std::string& property)
16 {
17     auto client =
18         sdbusplus::async::proxy().service(service).path(path).interface(
19             "org.freedesktop.DBus.Properties");
20 
21     std::optional<T> opt = std::nullopt;
22     try
23     {
24         std::variant<T> result =
25             co_await client.call<std::variant<T>>(ctx, "Get", intf, property);
26 
27         opt = std::get<T>(result);
28     }
29     catch (std::exception& e)
30     {
31         error("Missing property {PROPERTY} on path {PATH}, interface {INTF}",
32               "PROPERTY", property, "PATH", path, "INTF", intf);
33     }
34     co_return opt;
35 }
36