xref: /openbmc/bmcweb/redfish-core/include/utils/dbus_utils.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "async_resp.hpp"
6 #include "dbus_singleton.hpp"
7 #include "error_messages.hpp"
8 #include "logging.hpp"
9 
10 #include <nlohmann/json.hpp>
11 #include <sdbusplus/asio/property.hpp>
12 #include <sdbusplus/message.hpp>
13 #include <sdbusplus/unpack_properties.hpp>
14 
15 #include <memory>
16 #include <string_view>
17 
18 namespace redfish
19 {
20 namespace dbus_utils
21 {
22 
23 struct UnpackErrorPrinter
24 {
25     void operator()(const sdbusplus::UnpackErrorReason reason,
26                     const std::string& property) const noexcept
27     {
28         BMCWEB_LOG_ERROR(
29             "DBUS property error in property: {}, reason: {}", property,
30             static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
31                 reason));
32     }
33 };
34 
35 } // namespace dbus_utils
36 
37 namespace details
38 {
39 void afterSetProperty(
40     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
41     const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
42     const boost::system::error_code& ec, const sdbusplus::message_t& msg);
43 
44 void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
45                             const std::string& redfishActionName,
46                             const std::string& redfishActionParameterName,
47                             const boost::system::error_code& ec,
48                             const sdbusplus::message_t& msg);
49 } // namespace details
50 
51 template <typename PropertyType>
52 void setDbusProperty(
53     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
54     std::string_view redfishPropertyName, std::string_view processName,
55     const sdbusplus::message::object_path& path, std::string_view interface,
56     std::string_view dbusProperty, 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,
69                                       jsonProp, ec, msg);
70         });
71 }
72 
73 template <typename DbusPropertyType>
74 void setDbusPropertyAction(
75     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
76     std::string_view processName, const sdbusplus::message::object_path& path,
77     std::string_view interface, std::string_view dbusProperty,
78     std::string_view redfishActionParameterName,
79     std::string_view redfishActionName, const DbusPropertyType& prop)
80 {
81     std::string processNameStr(processName);
82     std::string interfaceStr(interface);
83     std::string dbusPropertyStr(dbusProperty);
84 
85     sdbusplus::asio::setProperty(
86         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
87         dbusPropertyStr, prop,
88         [asyncResp,
89          redfishActionParameterName = std::string{redfishActionParameterName},
90          jsonProp = nlohmann::json(prop),
91          redfishActionNameStr = std::string{redfishActionName}](
92             const boost::system::error_code& ec,
93             const sdbusplus::message_t& msg) {
94             details::afterSetPropertyAction(asyncResp, redfishActionNameStr,
95                                             redfishActionParameterName, ec,
96                                             msg);
97         });
98 }
99 
100 } // namespace redfish
101