xref: /openbmc/bmcweb/features/redfish/include/utils/dbus_utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3351053f2SKrzysztof Grobelny #pragma once
4351053f2SKrzysztof Grobelny 
5d02aad39SEd Tanous #include "async_resp.hpp"
6d02aad39SEd Tanous #include "dbus_singleton.hpp"
7351053f2SKrzysztof Grobelny #include "logging.hpp"
8351053f2SKrzysztof Grobelny 
9d02aad39SEd Tanous #include <nlohmann/json.hpp>
10d02aad39SEd Tanous #include <sdbusplus/asio/property.hpp>
11*d7857201SEd Tanous #include <sdbusplus/exception.hpp>
12d02aad39SEd Tanous #include <sdbusplus/message.hpp>
13*d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
14351053f2SKrzysztof Grobelny 
15d02aad39SEd Tanous #include <memory>
16*d7857201SEd Tanous #include <string>
17d02aad39SEd Tanous #include <string_view>
18*d7857201SEd Tanous #include <type_traits>
19d02aad39SEd Tanous 
20351053f2SKrzysztof Grobelny namespace redfish
21351053f2SKrzysztof Grobelny {
22351053f2SKrzysztof Grobelny namespace dbus_utils
23351053f2SKrzysztof Grobelny {
24351053f2SKrzysztof Grobelny 
25351053f2SKrzysztof Grobelny struct UnpackErrorPrinter
26351053f2SKrzysztof Grobelny {
27351053f2SKrzysztof Grobelny     void operator()(const sdbusplus::UnpackErrorReason reason,
28351053f2SKrzysztof Grobelny                     const std::string& property) const noexcept
29351053f2SKrzysztof Grobelny     {
3062598e31SEd Tanous         BMCWEB_LOG_ERROR(
3162598e31SEd Tanous             "DBUS property error in property: {}, reason: {}", property,
3262598e31SEd Tanous             static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
3362598e31SEd Tanous                 reason));
34351053f2SKrzysztof Grobelny     }
35351053f2SKrzysztof Grobelny };
36351053f2SKrzysztof Grobelny 
37351053f2SKrzysztof Grobelny } // namespace dbus_utils
38d02aad39SEd Tanous 
39d02aad39SEd Tanous namespace details
40d02aad39SEd Tanous {
41bd79bce8SPatrick Williams void afterSetProperty(
42bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
43bd79bce8SPatrick Williams     const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
44bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg);
451827b4f1SAsmitha Karunanithi 
461827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
471827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
481827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
491827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
501827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& msg);
511827b4f1SAsmitha Karunanithi } // namespace details
52d02aad39SEd Tanous 
53d02aad39SEd Tanous template <typename PropertyType>
54bd79bce8SPatrick Williams void setDbusProperty(
55bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
56bd79bce8SPatrick Williams     std::string_view redfishPropertyName, std::string_view processName,
57bd79bce8SPatrick Williams     const sdbusplus::message::object_path& path, std::string_view interface,
58bd79bce8SPatrick Williams     std::string_view dbusProperty, const PropertyType& prop)
59d02aad39SEd Tanous {
60d02aad39SEd Tanous     std::string processNameStr(processName);
61d02aad39SEd Tanous     std::string interfaceStr(interface);
62d02aad39SEd Tanous     std::string dbusPropertyStr(dbusProperty);
63d02aad39SEd Tanous 
64d02aad39SEd Tanous     sdbusplus::asio::setProperty(
65d02aad39SEd Tanous         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
66d02aad39SEd Tanous         dbusPropertyStr, prop,
67d02aad39SEd Tanous         [asyncResp, redfishPropertyNameStr = std::string{redfishPropertyName},
68d02aad39SEd Tanous          jsonProp = nlohmann::json(prop)](const boost::system::error_code& ec,
69d02aad39SEd Tanous                                           const sdbusplus::message_t& msg) {
70bd79bce8SPatrick Williams             details::afterSetProperty(asyncResp, redfishPropertyNameStr,
71bd79bce8SPatrick Williams                                       jsonProp, ec, msg);
72d02aad39SEd Tanous         });
73d02aad39SEd Tanous }
74d02aad39SEd Tanous 
751827b4f1SAsmitha Karunanithi template <typename DbusPropertyType>
76bd79bce8SPatrick Williams void setDbusPropertyAction(
77bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
78bd79bce8SPatrick Williams     std::string_view processName, const sdbusplus::message::object_path& path,
79bd79bce8SPatrick Williams     std::string_view interface, std::string_view dbusProperty,
801827b4f1SAsmitha Karunanithi     std::string_view redfishActionParameterName,
81bd79bce8SPatrick Williams     std::string_view redfishActionName, const DbusPropertyType& prop)
821827b4f1SAsmitha Karunanithi {
831827b4f1SAsmitha Karunanithi     std::string processNameStr(processName);
841827b4f1SAsmitha Karunanithi     std::string interfaceStr(interface);
851827b4f1SAsmitha Karunanithi     std::string dbusPropertyStr(dbusProperty);
861827b4f1SAsmitha Karunanithi 
871827b4f1SAsmitha Karunanithi     sdbusplus::asio::setProperty(
881827b4f1SAsmitha Karunanithi         *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
891827b4f1SAsmitha Karunanithi         dbusPropertyStr, prop,
901827b4f1SAsmitha Karunanithi         [asyncResp,
911827b4f1SAsmitha Karunanithi          redfishActionParameterName = std::string{redfishActionParameterName},
921827b4f1SAsmitha Karunanithi          jsonProp = nlohmann::json(prop),
931827b4f1SAsmitha Karunanithi          redfishActionNameStr = std::string{redfishActionName}](
941827b4f1SAsmitha Karunanithi             const boost::system::error_code& ec,
951827b4f1SAsmitha Karunanithi             const sdbusplus::message_t& msg) {
961827b4f1SAsmitha Karunanithi             details::afterSetPropertyAction(asyncResp, redfishActionNameStr,
97bd79bce8SPatrick Williams                                             redfishActionParameterName, ec,
98bd79bce8SPatrick Williams                                             msg);
991827b4f1SAsmitha Karunanithi         });
1001827b4f1SAsmitha Karunanithi }
1011827b4f1SAsmitha Karunanithi 
102351053f2SKrzysztof Grobelny } // namespace redfish
103