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 void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44                             const std::string& redfishActionName,
45                             const std::string& redfishActionParameterName,
46                             const boost::system::error_code& ec,
47                             const sdbusplus::message_t& msg);
48 } // namespace details
49 
50 template <typename PropertyType>
51 void setDbusProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
52                      std::string_view processName,
53                      const sdbusplus::message::object_path& path,
54                      std::string_view interface, std::string_view dbusProperty,
55                      std::string_view redfishPropertyName,
56                      const PropertyType& prop)
57 {
58     std::string processNameStr(processName);
59     std::string interfaceStr(interface);
60     std::string dbusPropertyStr(dbusProperty);
61 
62     sdbusplus::asio::setProperty(
63         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
64         dbusPropertyStr, prop,
65         [asyncResp, redfishPropertyNameStr = std::string{redfishPropertyName},
66          jsonProp = nlohmann::json(prop)](const boost::system::error_code& ec,
67                                           const sdbusplus::message_t& msg) {
68         details::afterSetProperty(asyncResp, redfishPropertyNameStr, jsonProp,
69                                   ec, msg);
70     });
71 }
72 
73 template <typename DbusPropertyType>
74 void setDbusPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
75                            std::string_view processName,
76                            const sdbusplus::message::object_path& path,
77                            std::string_view interface,
78                            std::string_view dbusProperty,
79                            std::string_view redfishActionParameterName,
80                            std::string_view redfishActionName,
81                            const DbusPropertyType& prop)
82 {
83     std::string processNameStr(processName);
84     std::string interfaceStr(interface);
85     std::string dbusPropertyStr(dbusProperty);
86 
87     sdbusplus::asio::setProperty(
88         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
89         dbusPropertyStr, prop,
90         [asyncResp,
91          redfishActionParameterName = std::string{redfishActionParameterName},
92          jsonProp = nlohmann::json(prop),
93          redfishActionNameStr = std::string{redfishActionName}](
94             const boost::system::error_code& ec,
95             const sdbusplus::message_t& msg) {
96         details::afterSetPropertyAction(asyncResp, redfishActionNameStr,
97                                         redfishActionParameterName, ec, msg);
98     });
99 }
100 
101 } // namespace redfish
102