xref: /openbmc/bmcweb/features/redfish/include/utils/dbus_utils.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3351053f2SKrzysztof Grobelny #pragma once
4351053f2SKrzysztof Grobelny 
5d02aad39SEd Tanous #include "async_resp.hpp"
6d02aad39SEd Tanous #include "dbus_singleton.hpp"
7d02aad39SEd Tanous #include "error_messages.hpp"
8351053f2SKrzysztof Grobelny #include "logging.hpp"
9351053f2SKrzysztof Grobelny 
10d02aad39SEd Tanous #include <nlohmann/json.hpp>
11d02aad39SEd Tanous #include <sdbusplus/asio/property.hpp>
12d02aad39SEd Tanous #include <sdbusplus/message.hpp>
13351053f2SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
14351053f2SKrzysztof Grobelny 
15d02aad39SEd Tanous #include <memory>
16d02aad39SEd Tanous #include <string_view>
17d02aad39SEd Tanous 
18351053f2SKrzysztof Grobelny namespace redfish
19351053f2SKrzysztof Grobelny {
20351053f2SKrzysztof Grobelny namespace dbus_utils
21351053f2SKrzysztof Grobelny {
22351053f2SKrzysztof Grobelny 
23351053f2SKrzysztof Grobelny struct UnpackErrorPrinter
24351053f2SKrzysztof Grobelny {
25351053f2SKrzysztof Grobelny     void operator()(const sdbusplus::UnpackErrorReason reason,
26351053f2SKrzysztof Grobelny                     const std::string& property) const noexcept
27351053f2SKrzysztof Grobelny     {
2862598e31SEd Tanous         BMCWEB_LOG_ERROR(
2962598e31SEd Tanous             "DBUS property error in property: {}, reason: {}", property,
3062598e31SEd Tanous             static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
3162598e31SEd Tanous                 reason));
32351053f2SKrzysztof Grobelny     }
33351053f2SKrzysztof Grobelny };
34351053f2SKrzysztof Grobelny 
35351053f2SKrzysztof Grobelny } // namespace dbus_utils
36d02aad39SEd Tanous 
37d02aad39SEd Tanous namespace details
38d02aad39SEd Tanous {
39bd79bce8SPatrick Williams void afterSetProperty(
40bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
41bd79bce8SPatrick Williams     const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
42bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg);
431827b4f1SAsmitha Karunanithi 
441827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
451827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
461827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
471827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
481827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& msg);
491827b4f1SAsmitha Karunanithi } // namespace details
50d02aad39SEd Tanous 
51d02aad39SEd Tanous template <typename PropertyType>
52bd79bce8SPatrick Williams void setDbusProperty(
53bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
54bd79bce8SPatrick Williams     std::string_view redfishPropertyName, std::string_view processName,
55bd79bce8SPatrick Williams     const sdbusplus::message::object_path& path, std::string_view interface,
56bd79bce8SPatrick Williams     std::string_view dbusProperty, 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) {
68bd79bce8SPatrick Williams             details::afterSetProperty(asyncResp, redfishPropertyNameStr,
69bd79bce8SPatrick Williams                                       jsonProp, ec, msg);
70d02aad39SEd Tanous         });
71d02aad39SEd Tanous }
72d02aad39SEd Tanous 
731827b4f1SAsmitha Karunanithi template <typename DbusPropertyType>
74bd79bce8SPatrick Williams void setDbusPropertyAction(
75bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
76bd79bce8SPatrick Williams     std::string_view processName, const sdbusplus::message::object_path& path,
77bd79bce8SPatrick Williams     std::string_view interface, std::string_view dbusProperty,
781827b4f1SAsmitha Karunanithi     std::string_view redfishActionParameterName,
79bd79bce8SPatrick Williams     std::string_view redfishActionName, const DbusPropertyType& prop)
801827b4f1SAsmitha Karunanithi {
811827b4f1SAsmitha Karunanithi     std::string processNameStr(processName);
821827b4f1SAsmitha Karunanithi     std::string interfaceStr(interface);
831827b4f1SAsmitha Karunanithi     std::string dbusPropertyStr(dbusProperty);
841827b4f1SAsmitha Karunanithi 
851827b4f1SAsmitha Karunanithi     sdbusplus::asio::setProperty(
861827b4f1SAsmitha Karunanithi         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
871827b4f1SAsmitha Karunanithi         dbusPropertyStr, prop,
881827b4f1SAsmitha Karunanithi         [asyncResp,
891827b4f1SAsmitha Karunanithi          redfishActionParameterName = std::string{redfishActionParameterName},
901827b4f1SAsmitha Karunanithi          jsonProp = nlohmann::json(prop),
911827b4f1SAsmitha Karunanithi          redfishActionNameStr = std::string{redfishActionName}](
921827b4f1SAsmitha Karunanithi             const boost::system::error_code& ec,
931827b4f1SAsmitha Karunanithi             const sdbusplus::message_t& msg) {
941827b4f1SAsmitha Karunanithi             details::afterSetPropertyAction(asyncResp, redfishActionNameStr,
95bd79bce8SPatrick Williams                                             redfishActionParameterName, ec,
96bd79bce8SPatrick Williams                                             msg);
971827b4f1SAsmitha Karunanithi         });
981827b4f1SAsmitha Karunanithi }
991827b4f1SAsmitha Karunanithi 
100351053f2SKrzysztof Grobelny } // namespace redfish
101