summaryrefslogtreecommitdiff
path: root/redfish-core/include/utils/dbus_utils.hpp
blob: 008fef2c957210888015645a5dbf5ed36c2b4ede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once

#include "async_resp.hpp"
#include "dbus_singleton.hpp"
#include "logging.hpp"

#include <nlohmann/json.hpp>
#include <sdbusplus/asio/property.hpp>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/message.hpp>
#include <sdbusplus/message/native_types.hpp>

#include <memory>
#include <string>
#include <string_view>
#include <type_traits>

namespace redfish
{
namespace dbus_utils
{

struct UnpackErrorPrinter
{
    void operator()(const sdbusplus::UnpackErrorReason reason,
                    const std::string& property) const noexcept
    {
        BMCWEB_LOG_ERROR(
            "DBUS property error in property: {}, reason: {}", property,
            static_cast<std::underlying_type_t<sdbusplus::UnpackErrorReason>>(
                reason));
    }
};

} // namespace dbus_utils

namespace details
{
void afterSetProperty(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
    const boost::system::error_code& ec, const sdbusplus::message_t& msg);

void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
                            const std::string& redfishActionName,
                            const std::string& redfishActionParameterName,
                            const boost::system::error_code& ec,
                            const sdbusplus::message_t& msg);
} // namespace details

template <typename PropertyType>
void 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)
{
    std::string processNameStr(processName);
    std::string interfaceStr(interface);
    std::string dbusPropertyStr(dbusProperty);

    sdbusplus::asio::setProperty(
        *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
        dbusPropertyStr, prop,
        [asyncResp, redfishPropertyNameStr = std::string{redfishPropertyName},
         jsonProp = nlohmann::json(prop)](const boost::system::error_code& ec,
                                          const sdbusplus::message_t& msg) {
            details::afterSetProperty(asyncResp, redfishPropertyNameStr,
                                      jsonProp, ec, msg);
        });
}

template <typename DbusPropertyType>
void 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)
{
    std::string processNameStr(processName);
    std::string interfaceStr(interface);
    std::string dbusPropertyStr(dbusProperty);

    sdbusplus::asio::setProperty(
        *crow::connections::systemBus, processNameStr, path.str, interfaceStr,
        dbusPropertyStr, prop,
        [asyncResp,
         redfishActionParameterName = std::string{redfishActionParameterName},
         jsonProp = nlohmann::json(prop),
         redfishActionNameStr = std::string{redfishActionName}](
            const boost::system::error_code& ec,
            const sdbusplus::message_t& msg) {
            details::afterSetPropertyAction(asyncResp, redfishActionNameStr,
                                            redfishActionParameterName, ec,
                                            msg);
        });
}

} // namespace redfish