summaryrefslogtreecommitdiff
path: root/redfish-core/include/utils/asset_utils.hpp
blob: 079b462bc0c9352f43cb2d54bba1c3b1603c4fe4 (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
103
104
105
106
107
108
109
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once

#include "async_resp.hpp"
#include "dbus_utility.hpp"
#include "error_messages.hpp"
#include "logging.hpp"
#include "utils/dbus_utils.hpp"

#include <asm-generic/errno.h>

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

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

namespace redfish
{
namespace asset_utils
{

inline void extractAssetInfo(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const nlohmann::json::json_pointer& jsonKeyName,
    const dbus::utility::DBusPropertiesMap& assetList,
    bool includeSparePartNumber = false, bool includeManufacturer = true)
{
    const std::string* manufacturer = nullptr;
    const std::string* model = nullptr;
    const std::string* partNumber = nullptr;
    const std::string* serialNumber = nullptr;
    const std::string* sparePartNumber = nullptr;

    const bool success = sdbusplus::unpackPropertiesNoThrow(
        dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
        manufacturer, "Model", model, "PartNumber", partNumber, "SerialNumber",
        serialNumber, "SparePartNumber", sparePartNumber);
    if (!success)
    {
        messages::internalError(asyncResp->res);
        return;
    }

    nlohmann::json& assetData = asyncResp->res.jsonValue[jsonKeyName];

    if (includeManufacturer && manufacturer != nullptr)
    {
        assetData["Manufacturer"] = *manufacturer;
    }
    if (model != nullptr)
    {
        assetData["Model"] = *model;
    }
    if (partNumber != nullptr)
    {
        assetData["PartNumber"] = *partNumber;
    }
    if (serialNumber != nullptr)
    {
        assetData["SerialNumber"] = *serialNumber;
    }
    // SparePartNumber is optional on D-Bus so skip if it is empty
    if (includeSparePartNumber && sparePartNumber != nullptr &&
        !sparePartNumber->empty())
    {
        assetData["SparePartNumber"] = *sparePartNumber;
    }
}

inline void afterGetAssetInfo(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const nlohmann::json::json_pointer& jsonKeyName,
    bool includeSparePartNumber, bool includeManufacturer,
    const boost::system::error_code& ec,
    const dbus::utility::DBusPropertiesMap& assetList)
{
    if (ec)
    {
        if (ec.value() != EBADR)
        {
            BMCWEB_LOG_ERROR("DBUS response error for Properties {}",
                             ec.value());
            messages::internalError(asyncResp->res);
        }
        return;
    }
    extractAssetInfo(asyncResp, jsonKeyName, assetList, includeSparePartNumber,
                     includeManufacturer);
}

inline void getAssetInfo(
    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
    const std::string& serviceName, const std::string& dbusPath,
    const nlohmann::json::json_pointer& jsonKeyName,
    bool includeSparePartNumber = false, bool includeManufacturer = true)
{
    dbus::utility::getAllProperties(
        serviceName, dbusPath, "xyz.openbmc_project.Inventory.Decorator.Asset",
        std::bind_front(afterGetAssetInfo, asyncResp, jsonKeyName,
                        includeSparePartNumber, includeManufacturer));
}

} // namespace asset_utils
} // namespace redfish