xref: /openbmc/bmcweb/features/redfish/include/utils/chassis_utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
30d7702c0SZhenwei Chen #pragma once
47a1dbc48SGeorge Liu 
53ccb3adbSEd Tanous #include "async_resp.hpp"
67a1dbc48SGeorge Liu #include "dbus_utility.hpp"
73ccb3adbSEd Tanous #include "error_messages.hpp"
8*d7857201SEd Tanous #include "logging.hpp"
9*d7857201SEd Tanous 
10*d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
110d7702c0SZhenwei Chen 
127a1dbc48SGeorge Liu #include <array>
13*d7857201SEd Tanous #include <memory>
14*d7857201SEd Tanous #include <optional>
15*d7857201SEd Tanous #include <string>
167a1dbc48SGeorge Liu #include <string_view>
177a1dbc48SGeorge Liu 
180d7702c0SZhenwei Chen namespace redfish
190d7702c0SZhenwei Chen {
200d7702c0SZhenwei Chen 
210d7702c0SZhenwei Chen namespace chassis_utils
220d7702c0SZhenwei Chen {
230d7702c0SZhenwei Chen /**
240d7702c0SZhenwei Chen  * @brief Retrieves valid chassis path
250d7702c0SZhenwei Chen  * @param asyncResp   Pointer to object holding response data
260d7702c0SZhenwei Chen  * @param callback  Callback for next step to get valid chassis path
270d7702c0SZhenwei Chen  */
280d7702c0SZhenwei Chen template <typename Callback>
290d7702c0SZhenwei Chen void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
300d7702c0SZhenwei Chen                          const std::string& chassisId, Callback&& callback)
310d7702c0SZhenwei Chen {
3262598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId enter");
337a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
340d7702c0SZhenwei Chen         "xyz.openbmc_project.Inventory.Item.Board",
350d7702c0SZhenwei Chen         "xyz.openbmc_project.Inventory.Item.Chassis"};
360d7702c0SZhenwei Chen 
377a1dbc48SGeorge Liu     // Get the Chassis Collection
387a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
397a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
408cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
417a1dbc48SGeorge Liu          chassisId](const boost::system::error_code& ec,
420d7702c0SZhenwei Chen                     const dbus::utility::MapperGetSubTreePathsResponse&
430d7702c0SZhenwei Chen                         chassisPaths) mutable {
4462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
450d7702c0SZhenwei Chen             if (ec)
460d7702c0SZhenwei Chen             {
47bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
48bd79bce8SPatrick Williams                     "getValidChassisPath respHandler DBUS error: {}", ec);
490d7702c0SZhenwei Chen                 messages::internalError(asyncResp->res);
500d7702c0SZhenwei Chen                 return;
510d7702c0SZhenwei Chen             }
520d7702c0SZhenwei Chen 
530d7702c0SZhenwei Chen             std::optional<std::string> chassisPath;
540d7702c0SZhenwei Chen             for (const std::string& chassis : chassisPaths)
550d7702c0SZhenwei Chen             {
560d7702c0SZhenwei Chen                 sdbusplus::message::object_path path(chassis);
57f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
580d7702c0SZhenwei Chen                 if (chassisName.empty())
590d7702c0SZhenwei Chen                 {
6062598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
610d7702c0SZhenwei Chen                     continue;
620d7702c0SZhenwei Chen                 }
630d7702c0SZhenwei Chen                 if (chassisName == chassisId)
640d7702c0SZhenwei Chen                 {
650d7702c0SZhenwei Chen                     chassisPath = chassis;
660d7702c0SZhenwei Chen                     break;
670d7702c0SZhenwei Chen                 }
680d7702c0SZhenwei Chen             }
690d7702c0SZhenwei Chen             callback(chassisPath);
707a1dbc48SGeorge Liu         });
7162598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId exit");
720d7702c0SZhenwei Chen }
730d7702c0SZhenwei Chen 
740d7702c0SZhenwei Chen } // namespace chassis_utils
750d7702c0SZhenwei Chen } // namespace redfish
76