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 "logging.hpp"
8
9 #include <nlohmann/json.hpp>
10 #include <sdbusplus/asio/property.hpp>
11 #include <sdbusplus/exception.hpp>
12 #include <sdbusplus/message.hpp>
13 #include <sdbusplus/message/native_types.hpp>
14
15 #include <memory>
16 #include <string>
17 #include <string_view>
18 #include <type_traits>
19
20 namespace redfish
21 {
22 namespace dbus_utils
23 {
24
25 struct UnpackErrorPrinter
26 {
operator ()redfish::dbus_utils::UnpackErrorPrinter27 void operator()(const sdbusplus::UnpackErrorReason reason,
28 const std::string& property) const noexcept
29 {
30 BMCWEB_LOG_ERROR(
31 "DBUS property error in property: {}, reason: {}", property,
32 static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
33 reason));
34 }
35 };
36
37 } // namespace dbus_utils
38
39 namespace details
40 {
41 void afterSetProperty(
42 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
43 const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
44 const boost::system::error_code& ec, const sdbusplus::message_t& msg);
45
46 void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
47 const std::string& redfishActionName,
48 const std::string& redfishActionParameterName,
49 const boost::system::error_code& ec,
50 const sdbusplus::message_t& msg);
51 } // namespace details
52
53 template <typename PropertyType>
setDbusProperty(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,std::string_view redfishPropertyName,std::string_view processName,const sdbusplus::message::object_path & path,std::string_view interface,std::string_view dbusProperty,const PropertyType & prop)54 void setDbusProperty(
55 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56 std::string_view redfishPropertyName, std::string_view processName,
57 const sdbusplus::message::object_path& path, std::string_view interface,
58 std::string_view dbusProperty, const PropertyType& prop)
59 {
60 std::string processNameStr(processName);
61 std::string interfaceStr(interface);
62 std::string dbusPropertyStr(dbusProperty);
63
64 sdbusplus::asio::setProperty(
65 *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
66 dbusPropertyStr, prop,
67 [asyncResp, redfishPropertyNameStr = std::string{redfishPropertyName},
68 jsonProp = nlohmann::json(prop)](const boost::system::error_code& ec,
69 const sdbusplus::message_t& msg) {
70 details::afterSetProperty(asyncResp, redfishPropertyNameStr,
71 jsonProp, ec, msg);
72 });
73 }
74
75 template <typename DbusPropertyType>
setDbusPropertyAction(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,std::string_view processName,const sdbusplus::message::object_path & path,std::string_view interface,std::string_view dbusProperty,std::string_view redfishActionParameterName,std::string_view redfishActionName,const DbusPropertyType & prop)76 void setDbusPropertyAction(
77 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
78 std::string_view processName, const sdbusplus::message::object_path& path,
79 std::string_view interface, std::string_view dbusProperty,
80 std::string_view redfishActionParameterName,
81 std::string_view redfishActionName, 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,
98 msg);
99 });
100 }
101
102 } // namespace redfish
103