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