xref: /openbmc/bmcweb/features/redfish/include/utils/chassis_utils.hpp (revision bd79bce8c3f1deb1fb2773868b9ece25233cf27b)
10d7702c0SZhenwei Chen #pragma once
27a1dbc48SGeorge Liu 
33ccb3adbSEd Tanous #include "async_resp.hpp"
47a1dbc48SGeorge Liu #include "dbus_utility.hpp"
53ccb3adbSEd Tanous #include "error_messages.hpp"
60d7702c0SZhenwei Chen 
77a1dbc48SGeorge Liu #include <array>
87a1dbc48SGeorge Liu #include <string_view>
97a1dbc48SGeorge Liu 
100d7702c0SZhenwei Chen namespace redfish
110d7702c0SZhenwei Chen {
120d7702c0SZhenwei Chen 
130d7702c0SZhenwei Chen namespace chassis_utils
140d7702c0SZhenwei Chen {
150d7702c0SZhenwei Chen /**
160d7702c0SZhenwei Chen  * @brief Retrieves valid chassis path
170d7702c0SZhenwei Chen  * @param asyncResp   Pointer to object holding response data
180d7702c0SZhenwei Chen  * @param callback  Callback for next step to get valid chassis path
190d7702c0SZhenwei Chen  */
200d7702c0SZhenwei Chen template <typename Callback>
210d7702c0SZhenwei Chen void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
220d7702c0SZhenwei Chen                          const std::string& chassisId, Callback&& callback)
230d7702c0SZhenwei Chen {
2462598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId enter");
257a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
260d7702c0SZhenwei Chen         "xyz.openbmc_project.Inventory.Item.Board",
270d7702c0SZhenwei Chen         "xyz.openbmc_project.Inventory.Item.Chassis"};
280d7702c0SZhenwei Chen 
297a1dbc48SGeorge Liu     // Get the Chassis Collection
307a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
317a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
328cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
337a1dbc48SGeorge Liu          chassisId](const boost::system::error_code& ec,
340d7702c0SZhenwei Chen                     const dbus::utility::MapperGetSubTreePathsResponse&
350d7702c0SZhenwei Chen                         chassisPaths) mutable {
3662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getValidChassisPath respHandler enter");
370d7702c0SZhenwei Chen             if (ec)
380d7702c0SZhenwei Chen             {
39*bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR(
40*bd79bce8SPatrick Williams                     "getValidChassisPath respHandler DBUS error: {}", ec);
410d7702c0SZhenwei Chen                 messages::internalError(asyncResp->res);
420d7702c0SZhenwei Chen                 return;
430d7702c0SZhenwei Chen             }
440d7702c0SZhenwei Chen 
450d7702c0SZhenwei Chen             std::optional<std::string> chassisPath;
460d7702c0SZhenwei Chen             for (const std::string& chassis : chassisPaths)
470d7702c0SZhenwei Chen             {
480d7702c0SZhenwei Chen                 sdbusplus::message::object_path path(chassis);
49f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
500d7702c0SZhenwei Chen                 if (chassisName.empty())
510d7702c0SZhenwei Chen                 {
5262598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
530d7702c0SZhenwei Chen                     continue;
540d7702c0SZhenwei Chen                 }
550d7702c0SZhenwei Chen                 if (chassisName == chassisId)
560d7702c0SZhenwei Chen                 {
570d7702c0SZhenwei Chen                     chassisPath = chassis;
580d7702c0SZhenwei Chen                     break;
590d7702c0SZhenwei Chen                 }
600d7702c0SZhenwei Chen             }
610d7702c0SZhenwei Chen             callback(chassisPath);
627a1dbc48SGeorge Liu         });
6362598e31SEd Tanous     BMCWEB_LOG_DEBUG("checkChassisId exit");
640d7702c0SZhenwei Chen }
650d7702c0SZhenwei Chen 
660d7702c0SZhenwei Chen } // namespace chassis_utils
670d7702c0SZhenwei Chen } // namespace redfish
68