xref: /openbmc/bmcweb/redfish-core/include/utils/assembly_utils.hpp (revision 72375723eeeabb039b0164ca95ab57be1c5235d3)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "async_resp.hpp"
6 #include "boost_formatters.hpp"
7 #include "dbus_utility.hpp"
8 #include "error_messages.hpp"
9 #include "human_sort.hpp"
10 #include "logging.hpp"
11 #include "utils/chassis_utils.hpp"
12 
13 #include <asm-generic/errno.h>
14 
15 #include <boost/system/error_code.hpp>
16 #include <sdbusplus/message/native_types.hpp>
17 
18 #include <algorithm>
19 #include <array>
20 #include <functional>
21 #include <memory>
22 #include <optional>
23 #include <ranges>
24 #include <string>
25 #include <string_view>
26 #include <utility>
27 #include <vector>
28 
29 namespace redfish
30 {
31 
32 static constexpr std::array<std::string_view, 1> assemblyInterfaces = {
33     "xyz.openbmc_project.Inventory.Item.Panel"};
34 
35 namespace assembly_utils
36 {
37 
afterGetChassisAssembly(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,std::function<void (const boost::system::error_code &,const std::vector<std::string> & sortedAssemblyList)> & callback,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreePathsResponse & subtreePaths)38 inline void afterGetChassisAssembly(
39     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40     std::function<void(const boost::system::error_code&,
41                        const std::vector<std::string>& sortedAssemblyList)>&
42         callback,
43     const boost::system::error_code& ec,
44     const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths)
45 {
46     if (ec)
47     {
48         if (ec.value() == boost::system::errc::io_error || ec.value() == EBADR)
49         {
50             // Not found
51             callback(ec, std::vector<std::string>());
52             return;
53         }
54 
55         BMCWEB_LOG_ERROR("DBUS response error {}", ec);
56         messages::internalError(asyncResp->res);
57         return;
58     }
59 
60     std::vector<std::string> sortedAssemblyList = subtreePaths;
61     std::ranges::sort(sortedAssemblyList, AlphanumLess<std::string>());
62 
63     callback(ec, sortedAssemblyList);
64 }
65 
66 /**
67  * @brief Get chassis path with given chassis ID
68  * @param[in] asyncResp - Shared pointer for asynchronous calls.
69  * @param[in] chassisId - Chassis to which the assemblies are
70  * associated.
71  * @param[in] callback
72  *
73  * @return None.
74  */
getChassisAssembly(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,std::function<void (const boost::system::error_code & ec,const std::vector<std::string> & sortedAssemblyList)> && callback)75 inline void getChassisAssembly(
76     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77     const std::string& chassisId,
78     std::function<void(const boost::system::error_code& ec,
79                        const std::vector<std::string>& sortedAssemblyList)>&&
80         callback)
81 {
82     BMCWEB_LOG_DEBUG("Get ChassisAssembly");
83 
84     dbus::utility::getAssociatedSubTreePathsById(
85         chassisId, "/xyz/openbmc_project/inventory", chassisInterfaces,
86         "containing", assemblyInterfaces,
87         std::bind_front(afterGetChassisAssembly, asyncResp,
88                         std::move(callback)));
89 }
90 
91 } // namespace assembly_utils
92 } // namespace redfish
93