xref: /openbmc/bmcweb/redfish-core/include/utils/chassis_utils.hpp (revision 177612aaa0633cf9d5aef0b763a43135cf552d9b)
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 "logging.hpp"
10 
11 #include <sdbusplus/message/native_types.hpp>
12 
13 #include <array>
14 #include <memory>
15 #include <optional>
16 #include <string>
17 #include <string_view>
18 
19 namespace redfish
20 {
21 
22 static constexpr std::array<std::string_view, 2> chassisInterfaces = {
23     "xyz.openbmc_project.Inventory.Item.Board",
24     "xyz.openbmc_project.Inventory.Item.Chassis"};
25 
26 namespace chassis_utils
27 {
28 /**
29  * @brief Retrieves valid chassis path
30  * @param asyncResp   Pointer to object holding response data
31  * @param callback  Callback for next step to get valid chassis path
32  */
33 template <typename Callback>
getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisId,Callback && callback)34 void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35                          const std::string& chassisId, Callback&& callback)
36 {
37     BMCWEB_LOG_DEBUG("checkChassisId enter");
38 
39     // Get the Chassis Collection
40     dbus::utility::getSubTreePaths(
41         "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
42         [callback = std::forward<Callback>(callback), asyncResp,
43          chassisId](const boost::system::error_code& ec,
44                     const dbus::utility::MapperGetSubTreePathsResponse&
45                         chassisPaths) mutable {
46             BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
47             if (ec)
48             {
49                 BMCWEB_LOG_ERROR(
50                     "getValidChassisPath respHandler DBUS error: {}", ec);
51                 messages::internalError(asyncResp->res);
52                 return;
53             }
54 
55             std::optional<std::string> chassisPath;
56             for (const std::string& chassis : chassisPaths)
57             {
58                 sdbusplus::message::object_path path(chassis);
59                 std::string chassisName = path.filename();
60                 if (chassisName.empty())
61                 {
62                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
63                     continue;
64                 }
65                 if (chassisName == chassisId)
66                 {
67                     chassisPath = chassis;
68                     break;
69                 }
70             }
71             callback(chassisPath);
72         });
73     BMCWEB_LOG_DEBUG("checkChassisId exit");
74 }
75 
76 } // namespace chassis_utils
77 } // namespace redfish
78