xref: /openbmc/bmcweb/redfish-core/include/utils/chassis_utils.hpp (revision f8fe53e7114ab10c9059377541277739ace5c1ff)
1 #pragma once
2 #include <async_resp.hpp>
3 
4 namespace redfish
5 {
6 
7 namespace chassis_utils
8 {
9 /**
10  * @brief Retrieves valid chassis path
11  * @param asyncResp   Pointer to object holding response data
12  * @param callback  Callback for next step to get valid chassis path
13  */
14 template <typename Callback>
15 void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16                          const std::string& chassisId, Callback&& callback)
17 {
18     BMCWEB_LOG_DEBUG << "checkChassisId enter";
19     const std::array<const char*, 2> interfaces = {
20         "xyz.openbmc_project.Inventory.Item.Board",
21         "xyz.openbmc_project.Inventory.Item.Chassis"};
22 
23     auto respHandler =
24         [callback{std::forward<Callback>(callback)}, asyncResp,
25          chassisId](const boost::system::error_code ec,
26                     const dbus::utility::MapperGetSubTreePathsResponse&
27                         chassisPaths) mutable {
28         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
29         if (ec)
30         {
31             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
32                              << ec;
33             messages::internalError(asyncResp->res);
34             return;
35         }
36 
37         std::optional<std::string> chassisPath;
38         for (const std::string& chassis : chassisPaths)
39         {
40             sdbusplus::message::object_path path(chassis);
41             std::string chassisName = path.filename();
42             if (chassisName.empty())
43             {
44                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
45                 continue;
46             }
47             if (chassisName == chassisId)
48             {
49                 chassisPath = chassis;
50                 break;
51             }
52         }
53         callback(chassisPath);
54     };
55 
56     // Get the Chassis Collection
57     crow::connections::systemBus->async_method_call(
58         respHandler, "xyz.openbmc_project.ObjectMapper",
59         "/xyz/openbmc_project/object_mapper",
60         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
61         "/xyz/openbmc_project/inventory", 0, interfaces);
62     BMCWEB_LOG_DEBUG << "checkChassisId exit";
63 }
64 
65 } // namespace chassis_utils
66 } // namespace redfish
67