summaryrefslogtreecommitdiff
path: root/redfish-core/include/utils/name_utils.hpp
blob: 6c7063e3dbc3da8b6dbafb56ac7b2ca9731eb384 (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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors

#pragma once

#include "async_resp.hpp"
#include "dbus_utility.hpp"

#include <boost/system/error_code.hpp>
#include <nlohmann/json.hpp>

#include <functional>
#include <memory>
#include <string>

namespace redfish
{

namespace name_utils
{

inline void afterGetPrettyName(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const nlohmann::json::json_pointer& jsonPtr,
    const boost::system::error_code& ec, const std::string& prettyName)
{
    if (ec)
    {
        BMCWEB_LOG_DEBUG("DBus response error {}", ec);
        return;
    }
    if (!prettyName.empty())
    {
        asyncResp->res.jsonValue[jsonPtr]["Name"] = prettyName;
    }
}
/**
 * @brief Get PrettyName from D-Bus Inventory.Item interface and set it
 *        at the specified JSON pointer location.
 *
 * @param asyncResp AsyncResp object to update
 * @param service D-Bus service name
 * @param path D-Bus object path
 * @param defaultName Fallback name if PrettyName is not available
 * @param jsonPtr JSON pointer where to the parent JSON object
 */
inline void getPrettyName(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const std::string& service, const std::string& path,
    const std::string& defaultName,
    const nlohmann::json::json_pointer& jsonPtr = ""_json_pointer)
{
    asyncResp->res.jsonValue[jsonPtr]["Name"] = defaultName;

    dbus::utility::getProperty<std::string>(
        service, path, "xyz.openbmc_project.Inventory.Item", "PrettyName",
        std::bind_front(afterGetPrettyName, asyncResp, jsonPtr));
}

/**
 * @brief Get PrettyName from the first service that implements Inventory.Item
 * otherwise defaults to defaultName
 *
 * @param asyncResp AsyncResp object to update
 * @param services Map of services to interfaces
 * @param path D-Bus object path
 * @param defaultName Fallback name if no service implements Inventory.Item
 * @param jsonPtr JSON pointer where to the parent JSON object
 */
inline void getPrettyName(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const dbus::utility::MapperServiceMap& services, const std::string& path,
    const std::string& defaultName,
    const nlohmann::json::json_pointer& jsonPtr = ""_json_pointer)
{
    for (const auto& [serviceName, interfaces] : services)
    {
        for (const auto& interface : interfaces)
        {
            if (interface == "xyz.openbmc_project.Inventory.Item")
            {
                getPrettyName(asyncResp, serviceName, path, defaultName,
                              jsonPtr);
                return;
            }
        }
    }
    // If the Inventory.Item interface is missing, set to default
    asyncResp->res.jsonValue[jsonPtr]["Name"] = defaultName;
}

} // namespace name_utils
} // namespace redfish