xref: /openbmc/bmcweb/redfish-core/include/utils/dbus_utils.hpp (revision 842c8f609be3957b844c85cb4b2d77c39d6ee9db)
1 #pragma once
2 
3 #include "async_resp.hpp"
4 #include "dbus_singleton.hpp"
5 #include "error_messages.hpp"
6 #include "logging.hpp"
7 
8 #include <nlohmann/json.hpp>
9 #include <sdbusplus/asio/property.hpp>
10 #include <sdbusplus/message.hpp>
11 #include <sdbusplus/unpack_properties.hpp>
12 
13 #include <memory>
14 #include <string_view>
15 
16 namespace redfish
17 {
18 namespace dbus_utils
19 {
20 
21 struct UnpackErrorPrinter
22 {
23     void operator()(const sdbusplus::UnpackErrorReason reason,
24                     const std::string& property) const noexcept
25     {
26         BMCWEB_LOG_ERROR(
27             "DBUS property error in property: {}, reason: {}", property,
28             static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
29                 reason));
30     }
31 };
32 
33 } // namespace dbus_utils
34 
35 namespace details
36 {
37 void afterSetProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38                       const std::string& redfishPropertyName,
39                       const nlohmann::json& propertyValue,
40                       const boost::system::error_code& ec,
41                       const sdbusplus::message_t& msg);
42 }
43 
44 template <typename PropertyType>
45 void setDbusProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
46                      std::string_view processName,
47                      const sdbusplus::message::object_path& path,
48                      std::string_view interface, std::string_view dbusProperty,
49                      std::string_view redfishPropertyName,
50                      const PropertyType& prop)
51 {
52     std::string processNameStr(processName);
53     std::string interfaceStr(interface);
54     std::string dbusPropertyStr(dbusProperty);
55 
56     sdbusplus::asio::setProperty(
57         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
58         dbusPropertyStr, prop,
59         [asyncResp, redfishPropertyNameStr = std::string{redfishPropertyName},
60          jsonProp = nlohmann::json(prop)](const boost::system::error_code& ec,
61                                           const sdbusplus::message_t& msg) {
62         details::afterSetProperty(asyncResp, redfishPropertyNameStr, jsonProp,
63                                   ec, msg);
64     });
65 }
66 
67 } // namespace redfish
68