xref: /openbmc/bmcweb/features/redfish/include/utils/dbus_utils.hpp (revision 1827b4f1de1e42bb3e2f53f584f07bb7119a3ed2)
1351053f2SKrzysztof Grobelny #pragma once
2351053f2SKrzysztof Grobelny 
3d02aad39SEd Tanous #include "async_resp.hpp"
4d02aad39SEd Tanous #include "dbus_singleton.hpp"
5d02aad39SEd Tanous #include "error_messages.hpp"
6351053f2SKrzysztof Grobelny #include "logging.hpp"
7351053f2SKrzysztof Grobelny 
8d02aad39SEd Tanous #include <nlohmann/json.hpp>
9d02aad39SEd Tanous #include <sdbusplus/asio/property.hpp>
10d02aad39SEd Tanous #include <sdbusplus/message.hpp>
11351053f2SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
12351053f2SKrzysztof Grobelny 
13d02aad39SEd Tanous #include <memory>
14d02aad39SEd Tanous #include <string_view>
15d02aad39SEd Tanous 
16351053f2SKrzysztof Grobelny namespace redfish
17351053f2SKrzysztof Grobelny {
18351053f2SKrzysztof Grobelny namespace dbus_utils
19351053f2SKrzysztof Grobelny {
20351053f2SKrzysztof Grobelny 
21351053f2SKrzysztof Grobelny struct UnpackErrorPrinter
22351053f2SKrzysztof Grobelny {
23351053f2SKrzysztof Grobelny     void operator()(const sdbusplus::UnpackErrorReason reason,
24351053f2SKrzysztof Grobelny                     const std::string& property) const noexcept
25351053f2SKrzysztof Grobelny     {
2662598e31SEd Tanous         BMCWEB_LOG_ERROR(
2762598e31SEd Tanous             "DBUS property error in property: {}, reason: {}", property,
2862598e31SEd Tanous             static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
2962598e31SEd Tanous                 reason));
30351053f2SKrzysztof Grobelny     }
31351053f2SKrzysztof Grobelny };
32351053f2SKrzysztof Grobelny 
33351053f2SKrzysztof Grobelny } // namespace dbus_utils
34d02aad39SEd Tanous 
35d02aad39SEd Tanous namespace details
36d02aad39SEd Tanous {
37d02aad39SEd Tanous void afterSetProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38d02aad39SEd Tanous                       const std::string& redfishPropertyName,
39d02aad39SEd Tanous                       const nlohmann::json& propertyValue,
40d02aad39SEd Tanous                       const boost::system::error_code& ec,
41d02aad39SEd Tanous                       const sdbusplus::message_t& msg);
42*1827b4f1SAsmitha Karunanithi 
43*1827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44*1827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
45*1827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
46*1827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
47*1827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& msg);
48*1827b4f1SAsmitha Karunanithi } // namespace details
49d02aad39SEd Tanous 
50d02aad39SEd Tanous template <typename PropertyType>
51d02aad39SEd Tanous void setDbusProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
52d02aad39SEd Tanous                      std::string_view processName,
53d02aad39SEd Tanous                      const sdbusplus::message::object_path& path,
54d02aad39SEd Tanous                      std::string_view interface, std::string_view dbusProperty,
55d02aad39SEd Tanous                      std::string_view redfishPropertyName,
56d02aad39SEd Tanous                      const PropertyType& prop)
57d02aad39SEd Tanous {
58d02aad39SEd Tanous     std::string processNameStr(processName);
59d02aad39SEd Tanous     std::string interfaceStr(interface);
60d02aad39SEd Tanous     std::string dbusPropertyStr(dbusProperty);
61d02aad39SEd Tanous 
62d02aad39SEd Tanous     sdbusplus::asio::setProperty(
63d02aad39SEd Tanous         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
64d02aad39SEd Tanous         dbusPropertyStr, prop,
65d02aad39SEd Tanous         [asyncResp, redfishPropertyNameStr = std::string{redfishPropertyName},
66d02aad39SEd Tanous          jsonProp = nlohmann::json(prop)](const boost::system::error_code& ec,
67d02aad39SEd Tanous                                           const sdbusplus::message_t& msg) {
68d02aad39SEd Tanous         details::afterSetProperty(asyncResp, redfishPropertyNameStr, jsonProp,
69d02aad39SEd Tanous                                   ec, msg);
70d02aad39SEd Tanous     });
71d02aad39SEd Tanous }
72d02aad39SEd Tanous 
73*1827b4f1SAsmitha Karunanithi template <typename DbusPropertyType>
74*1827b4f1SAsmitha Karunanithi void setDbusPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
75*1827b4f1SAsmitha Karunanithi                            std::string_view processName,
76*1827b4f1SAsmitha Karunanithi                            const sdbusplus::message::object_path& path,
77*1827b4f1SAsmitha Karunanithi                            std::string_view interface,
78*1827b4f1SAsmitha Karunanithi                            std::string_view dbusProperty,
79*1827b4f1SAsmitha Karunanithi                            std::string_view redfishActionParameterName,
80*1827b4f1SAsmitha Karunanithi                            std::string_view redfishActionName,
81*1827b4f1SAsmitha Karunanithi                            const DbusPropertyType& prop)
82*1827b4f1SAsmitha Karunanithi {
83*1827b4f1SAsmitha Karunanithi     std::string processNameStr(processName);
84*1827b4f1SAsmitha Karunanithi     std::string interfaceStr(interface);
85*1827b4f1SAsmitha Karunanithi     std::string dbusPropertyStr(dbusProperty);
86*1827b4f1SAsmitha Karunanithi 
87*1827b4f1SAsmitha Karunanithi     sdbusplus::asio::setProperty(
88*1827b4f1SAsmitha Karunanithi         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
89*1827b4f1SAsmitha Karunanithi         dbusPropertyStr, prop,
90*1827b4f1SAsmitha Karunanithi         [asyncResp,
91*1827b4f1SAsmitha Karunanithi          redfishActionParameterName = std::string{redfishActionParameterName},
92*1827b4f1SAsmitha Karunanithi          jsonProp = nlohmann::json(prop),
93*1827b4f1SAsmitha Karunanithi          redfishActionNameStr = std::string{redfishActionName}](
94*1827b4f1SAsmitha Karunanithi             const boost::system::error_code& ec,
95*1827b4f1SAsmitha Karunanithi             const sdbusplus::message_t& msg) {
96*1827b4f1SAsmitha Karunanithi         details::afterSetPropertyAction(asyncResp, redfishActionNameStr,
97*1827b4f1SAsmitha Karunanithi                                         redfishActionParameterName, ec, msg);
98*1827b4f1SAsmitha Karunanithi     });
99*1827b4f1SAsmitha Karunanithi }
100*1827b4f1SAsmitha Karunanithi 
101351053f2SKrzysztof Grobelny } // namespace redfish
102