1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4
5 #include "async_resp.hpp"
6 #include "dbus_utility.hpp"
7 #include "error_messages.hpp"
8 #include "logging.hpp"
9 #include "utils/dbus_utils.hpp"
10
11 #include <asm-generic/errno.h>
12
13 #include <boost/system/error_code.hpp>
14 #include <nlohmann/json.hpp>
15 #include <sdbusplus/unpack_properties.hpp>
16
17 #include <functional>
18 #include <memory>
19 #include <ranges>
20 #include <string>
21
22 namespace redfish
23 {
24 namespace asset_utils
25 {
26
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)27 inline void extractAssetInfo(
28 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29 const nlohmann::json::json_pointer& jsonKeyName,
30 const dbus::utility::DBusPropertiesMap& assetList,
31 bool includeSparePartNumber = false, bool includeManufacturer = true)
32 {
33 const std::string* manufacturer = nullptr;
34 const std::string* model = nullptr;
35 const std::string* partNumber = nullptr;
36 const std::string* serialNumber = nullptr;
37 const std::string* sparePartNumber = nullptr;
38
39 const bool success = sdbusplus::unpackPropertiesNoThrow(
40 dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
41 manufacturer, "Model", model, "PartNumber", partNumber, "SerialNumber",
42 serialNumber, "SparePartNumber", sparePartNumber);
43 if (!success)
44 {
45 messages::internalError(asyncResp->res);
46 return;
47 }
48
49 nlohmann::json& assetData = asyncResp->res.jsonValue[jsonKeyName];
50
51 if (includeManufacturer && manufacturer != nullptr)
52 {
53 assetData["Manufacturer"] = *manufacturer;
54 }
55 if (model != nullptr)
56 {
57 assetData["Model"] = *model;
58 }
59 if (partNumber != nullptr)
60 {
61 assetData["PartNumber"] = *partNumber;
62 }
63 if (serialNumber != nullptr)
64 {
65 assetData["SerialNumber"] = *serialNumber;
66 }
67 // SparePartNumber is optional on D-Bus so skip if it is empty
68 if (includeSparePartNumber && sparePartNumber != nullptr &&
69 !sparePartNumber->empty())
70 {
71 assetData["SparePartNumber"] = *sparePartNumber;
72 }
73 }
74
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)75 inline void getAssetInfo(
76 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77 const std::string& serviceName, const std::string& dbusPath,
78 const nlohmann::json::json_pointer& jsonKeyName,
79 bool includeSparePartNumber = false, bool includeManufacturer = true)
80 {
81 dbus::utility::getAllProperties(
82 serviceName, dbusPath, "xyz.openbmc_project.Inventory.Decorator.Asset",
83 [asyncResp, jsonKeyName, includeSparePartNumber, includeManufacturer](
84 const boost::system::error_code& ec,
85 const dbus::utility::DBusPropertiesMap& assetList) {
86 if (ec)
87 {
88 if (ec.value() != EBADR)
89 {
90 BMCWEB_LOG_ERROR("DBUS response error for Properties {}",
91 ec.value());
92 messages::internalError(asyncResp->res);
93 }
94 return;
95 }
96 extractAssetInfo(asyncResp, jsonKeyName, assetList,
97 includeSparePartNumber, includeManufacturer);
98 });
99 }
100
101 } // namespace asset_utils
102 } // namespace redfish
103