xref: /openbmc/bmcweb/features/redfish/lib/chassis.hpp (revision 59a17e4f40c78c6ee04f68a369fb9c041f64122d)
1e37f8451SRapkiewicz, Pawel /*
2e37f8451SRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation
3e37f8451SRapkiewicz, Pawel //
4e37f8451SRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License");
5e37f8451SRapkiewicz, Pawel // you may not use this file except in compliance with the License.
6e37f8451SRapkiewicz, Pawel // You may obtain a copy of the License at
7e37f8451SRapkiewicz, Pawel //
8e37f8451SRapkiewicz, Pawel //      http://www.apache.org/licenses/LICENSE-2.0
9e37f8451SRapkiewicz, Pawel //
10e37f8451SRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software
11e37f8451SRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS,
12e37f8451SRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e37f8451SRapkiewicz, Pawel // See the License for the specific language governing permissions and
14e37f8451SRapkiewicz, Pawel // limitations under the License.
15e37f8451SRapkiewicz, Pawel */
16e37f8451SRapkiewicz, Pawel #pragma once
17e37f8451SRapkiewicz, Pawel 
1813451e39SWilly Tu #include "bmcweb_config.h"
1913451e39SWilly Tu 
203ccb3adbSEd Tanous #include "app.hpp"
217a1dbc48SGeorge Liu #include "dbus_utility.hpp"
22b49ac873SJames Feist #include "health.hpp"
231c8fba97SJames Feist #include "led.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/collection.hpp"
273ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
28cf7eba09SNan Zhou #include "utils/json_utils.hpp"
291abe55efSEd Tanous 
30e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
31ef4c65b7SEd Tanous #include <boost/url/format.hpp>
321e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
33fc903b3dSAndrew Geissler #include <sdbusplus/message.hpp>
3486d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
351214b7e7SGunnar Mills 
367a1dbc48SGeorge Liu #include <array>
373544d2a7SEd Tanous #include <ranges>
387a1dbc48SGeorge Liu #include <string_view>
397a1dbc48SGeorge Liu 
401abe55efSEd Tanous namespace redfish
411abe55efSEd Tanous {
42e37f8451SRapkiewicz, Pawel 
43e37f8451SRapkiewicz, Pawel /**
445e577bc1SWilly Tu  * @brief Retrieves resources over dbus to link to the chassis
455e577bc1SWilly Tu  *
465e577bc1SWilly Tu  * @param[in] asyncResp  - Shared pointer for completing asynchronous
475e577bc1SWilly Tu  * calls
485e577bc1SWilly Tu  * @param[in] path       - Chassis dbus path to look for the storage.
495e577bc1SWilly Tu  *
505e577bc1SWilly Tu  * Calls the Association endpoints on the path + "/storage" and add the link of
515e577bc1SWilly Tu  * json["Links"]["Storage@odata.count"] =
525e577bc1SWilly Tu  *    {"@odata.id", "/redfish/v1/Storage/" + resourceId}
535e577bc1SWilly Tu  *
545e577bc1SWilly Tu  * @return None.
555e577bc1SWilly Tu  */
565e577bc1SWilly Tu inline void getStorageLink(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
575e577bc1SWilly Tu                            const sdbusplus::message::object_path& path)
585e577bc1SWilly Tu {
595e577bc1SWilly Tu     sdbusplus::asio::getProperty<std::vector<std::string>>(
605e577bc1SWilly Tu         *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
615e577bc1SWilly Tu         (path / "storage").str, "xyz.openbmc_project.Association", "endpoints",
62d4b054c1SWilly Tu         [asyncResp](const boost::system::error_code& ec,
635e577bc1SWilly Tu                     const std::vector<std::string>& storageList) {
645e577bc1SWilly Tu         if (ec)
655e577bc1SWilly Tu         {
6662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getStorageLink got DBUS response error");
675e577bc1SWilly Tu             return;
685e577bc1SWilly Tu         }
695e577bc1SWilly Tu 
705e577bc1SWilly Tu         nlohmann::json::array_t storages;
715e577bc1SWilly Tu         for (const std::string& storagePath : storageList)
725e577bc1SWilly Tu         {
735e577bc1SWilly Tu             std::string id =
745e577bc1SWilly Tu                 sdbusplus::message::object_path(storagePath).filename();
755e577bc1SWilly Tu             if (id.empty())
765e577bc1SWilly Tu             {
775e577bc1SWilly Tu                 continue;
785e577bc1SWilly Tu             }
795e577bc1SWilly Tu 
805e577bc1SWilly Tu             nlohmann::json::object_t storage;
815e577bc1SWilly Tu             storage["@odata.id"] = boost::urls::format(
825e577bc1SWilly Tu                 "/redfish/v1/Systems/system/Storage/{}", id);
835e577bc1SWilly Tu             storages.emplace_back(std::move(storage));
845e577bc1SWilly Tu         }
855e577bc1SWilly Tu         asyncResp->res.jsonValue["Links"]["Storage@odata.count"] =
865e577bc1SWilly Tu             storages.size();
875e577bc1SWilly Tu         asyncResp->res.jsonValue["Links"]["Storage"] = std::move(storages);
885e577bc1SWilly Tu     });
895e577bc1SWilly Tu }
905e577bc1SWilly Tu 
915e577bc1SWilly Tu /**
92beeca0aeSGunnar Mills  * @brief Retrieves chassis state properties over dbus
93beeca0aeSGunnar Mills  *
94ac106bf6SEd Tanous  * @param[in] asyncResp - Shared pointer for completing asynchronous calls.
95beeca0aeSGunnar Mills  *
96beeca0aeSGunnar Mills  * @return None.
97beeca0aeSGunnar Mills  */
98ac106bf6SEd Tanous inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
99beeca0aeSGunnar Mills {
1001e1e598dSJonathan Doman     // crow::connections::systemBus->async_method_call(
1011e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
1021e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
1031e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
1041e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
105ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
1061e1e598dSJonathan Doman                                           const std::string& chassisState) {
107beeca0aeSGunnar Mills         if (ec)
108beeca0aeSGunnar Mills         {
109a6e5e0abSCarson Labrado             if (ec == boost::system::errc::host_unreachable)
110a6e5e0abSCarson Labrado             {
111a6e5e0abSCarson Labrado                 // Service not available, no error, just don't return
112a6e5e0abSCarson Labrado                 // chassis state info
11362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Service not available {}", ec);
114a6e5e0abSCarson Labrado                 return;
115a6e5e0abSCarson Labrado             }
11662598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
117ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
118beeca0aeSGunnar Mills             return;
119beeca0aeSGunnar Mills         }
120beeca0aeSGunnar Mills 
12162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Chassis state: {}", chassisState);
122beeca0aeSGunnar Mills         // Verify Chassis State
123002d39b4SEd Tanous         if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
124beeca0aeSGunnar Mills         {
125ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
126ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
127beeca0aeSGunnar Mills         }
1281e1e598dSJonathan Doman         else if (chassisState ==
129beeca0aeSGunnar Mills                  "xyz.openbmc_project.State.Chassis.PowerState.Off")
130beeca0aeSGunnar Mills         {
131ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "Off";
132ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
133beeca0aeSGunnar Mills         }
1341e1e598dSJonathan Doman     });
135beeca0aeSGunnar Mills }
136beeca0aeSGunnar Mills 
137ac106bf6SEd Tanous inline void getIntrusionByService(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
138c181942fSQiang XU                                   const std::string& service,
139c181942fSQiang XU                                   const std::string& objPath)
140c181942fSQiang XU {
14162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get intrusion status by service ");
142c181942fSQiang XU 
1431e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
1441e1e598dSJonathan Doman         *crow::connections::systemBus, service, objPath,
1451e1e598dSJonathan Doman         "xyz.openbmc_project.Chassis.Intrusion", "Status",
146ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
1471e1e598dSJonathan Doman                                           const std::string& value) {
148c181942fSQiang XU         if (ec)
149c181942fSQiang XU         {
1504e0453b1SGunnar Mills             // do not add err msg in redfish response, because this is not
151c181942fSQiang XU             //     mandatory property
15262598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
153c181942fSQiang XU             return;
154c181942fSQiang XU         }
155c181942fSQiang XU 
156ac106bf6SEd Tanous         asyncResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] =
157ac106bf6SEd Tanous             1;
158ac106bf6SEd Tanous         asyncResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
1591e1e598dSJonathan Doman     });
160c181942fSQiang XU }
161c181942fSQiang XU 
162c181942fSQiang XU /**
163c181942fSQiang XU  * Retrieves physical security properties over dbus
164c181942fSQiang XU  */
165ac106bf6SEd Tanous inline void
166ac106bf6SEd Tanous     getPhysicalSecurityData(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
167c181942fSQiang XU {
168e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
169e99073f5SGeorge Liu         "xyz.openbmc_project.Chassis.Intrusion"};
170e99073f5SGeorge Liu     dbus::utility::getSubTree(
171630adcdcSChau Ly         "/xyz/openbmc_project", 0, interfaces,
172ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)}](
173e99073f5SGeorge Liu             const boost::system::error_code& ec,
174b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
175c181942fSQiang XU         if (ec)
176c181942fSQiang XU         {
1774e0453b1SGunnar Mills             // do not add err msg in redfish response, because this is not
178c181942fSQiang XU             //     mandatory property
17962598e31SEd Tanous             BMCWEB_LOG_INFO("DBUS error: no matched iface {}", ec);
180c181942fSQiang XU             return;
181c181942fSQiang XU         }
182c181942fSQiang XU         // Iterate over all retrieved ObjectPaths.
183c181942fSQiang XU         for (const auto& object : subtree)
184c181942fSQiang XU         {
185840a9ffcSPatrick Williams             if (!object.second.empty())
186c181942fSQiang XU             {
187840a9ffcSPatrick Williams                 const auto service = object.second.front();
188ac106bf6SEd Tanous                 getIntrusionByService(asyncResp, service.first, object.first);
189c181942fSQiang XU                 return;
190c181942fSQiang XU             }
191c181942fSQiang XU         }
192e99073f5SGeorge Liu     });
193c181942fSQiang XU }
194c181942fSQiang XU 
195cf7eba09SNan Zhou inline void handleChassisCollectionGet(
196cf7eba09SNan Zhou     App& app, const crow::Request& req,
197cf7eba09SNan Zhou     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1981abe55efSEd Tanous {
1993ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20045ca1b86SEd Tanous     {
20145ca1b86SEd Tanous         return;
20245ca1b86SEd Tanous     }
2038d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
2048d1b46d7Szhanghch05         "#ChassisCollection.ChassisCollection";
2058d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
2068d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Chassis Collection";
207e37f8451SRapkiewicz, Pawel 
2087a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces{
2097a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
2107a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
21102f6ff19SGunnar Mills     collection_util::getCollectionMembers(
21236b5f1edSLakshmi Yadlapati         asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces,
21336b5f1edSLakshmi Yadlapati         "/xyz/openbmc_project/inventory");
214cf7eba09SNan Zhou }
215cf7eba09SNan Zhou 
216a5617496SJie Yang inline void getChassisContainedBy(
217a5617496SJie Yang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218a5617496SJie Yang     const std::string& chassisId, const boost::system::error_code& ec,
219a5617496SJie Yang     const dbus::utility::MapperEndPoints& upstreamChassisPaths)
220a5617496SJie Yang {
221a5617496SJie Yang     if (ec)
222a5617496SJie Yang     {
223a5617496SJie Yang         if (ec.value() != EBADR)
224a5617496SJie Yang         {
22562598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
226a5617496SJie Yang             messages::internalError(asyncResp->res);
227a5617496SJie Yang         }
228a5617496SJie Yang         return;
229a5617496SJie Yang     }
230a5617496SJie Yang     if (upstreamChassisPaths.empty())
231a5617496SJie Yang     {
232a5617496SJie Yang         return;
233a5617496SJie Yang     }
234a5617496SJie Yang     if (upstreamChassisPaths.size() > 1)
235a5617496SJie Yang     {
23662598e31SEd Tanous         BMCWEB_LOG_ERROR("{} is contained by mutliple chassis", chassisId);
237a5617496SJie Yang         messages::internalError(asyncResp->res);
238a5617496SJie Yang         return;
239a5617496SJie Yang     }
240a5617496SJie Yang 
241a5617496SJie Yang     sdbusplus::message::object_path upstreamChassisPath(
242a5617496SJie Yang         upstreamChassisPaths[0]);
243a5617496SJie Yang     std::string upstreamChassis = upstreamChassisPath.filename();
244a5617496SJie Yang     if (upstreamChassis.empty())
245a5617496SJie Yang     {
24662598e31SEd Tanous         BMCWEB_LOG_WARNING("Malformed upstream Chassis path {} on {}",
24762598e31SEd Tanous                            upstreamChassisPath.str, chassisId);
248a5617496SJie Yang         return;
249a5617496SJie Yang     }
250a5617496SJie Yang 
251a5617496SJie Yang     asyncResp->res.jsonValue["Links"]["ContainedBy"]["@odata.id"] =
252a5617496SJie Yang         boost::urls::format("/redfish/v1/Chassis/{}", upstreamChassis);
253a5617496SJie Yang }
254a5617496SJie Yang 
255a5617496SJie Yang inline void getChassisContains(
256a5617496SJie Yang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
257a5617496SJie Yang     const std::string& chassisId, const boost::system::error_code& ec,
258a5617496SJie Yang     const dbus::utility::MapperEndPoints& downstreamChassisPaths)
259a5617496SJie Yang {
260a5617496SJie Yang     if (ec)
261a5617496SJie Yang     {
262a5617496SJie Yang         if (ec.value() != EBADR)
263a5617496SJie Yang         {
26462598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
265a5617496SJie Yang             messages::internalError(asyncResp->res);
266a5617496SJie Yang         }
267a5617496SJie Yang         return;
268a5617496SJie Yang     }
269a5617496SJie Yang     if (downstreamChassisPaths.empty())
270a5617496SJie Yang     {
271a5617496SJie Yang         return;
272a5617496SJie Yang     }
273a5617496SJie Yang     nlohmann::json& jValue = asyncResp->res.jsonValue["Links"]["Contains"];
274a5617496SJie Yang     if (!jValue.is_array())
275a5617496SJie Yang     {
276a5617496SJie Yang         // Create the array if it was empty
277a5617496SJie Yang         jValue = nlohmann::json::array();
278a5617496SJie Yang     }
279a5617496SJie Yang     for (const auto& p : downstreamChassisPaths)
280a5617496SJie Yang     {
281a5617496SJie Yang         sdbusplus::message::object_path downstreamChassisPath(p);
282a5617496SJie Yang         std::string downstreamChassis = downstreamChassisPath.filename();
283a5617496SJie Yang         if (downstreamChassis.empty())
284a5617496SJie Yang         {
28562598e31SEd Tanous             BMCWEB_LOG_WARNING("Malformed downstream Chassis path {} on {}",
28662598e31SEd Tanous                                downstreamChassisPath.str, chassisId);
287a5617496SJie Yang             continue;
288a5617496SJie Yang         }
289a5617496SJie Yang         nlohmann::json link;
290a5617496SJie Yang         link["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
291a5617496SJie Yang                                                 downstreamChassis);
292a5617496SJie Yang         jValue.push_back(std::move(link));
293a5617496SJie Yang     }
294a5617496SJie Yang     asyncResp->res.jsonValue["Links"]["Contains@odata.count"] = jValue.size();
295a5617496SJie Yang }
296a5617496SJie Yang 
297a5617496SJie Yang inline void
298a5617496SJie Yang     getChassisConnectivity(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
299a5617496SJie Yang                            const std::string& chassisId,
300a5617496SJie Yang                            const std::string& chassisPath)
301a5617496SJie Yang {
30262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get chassis connectivity");
303a5617496SJie Yang 
304a5617496SJie Yang     dbus::utility::getAssociationEndPoints(
305a5617496SJie Yang         chassisPath + "/contained_by",
306a5617496SJie Yang         std::bind_front(getChassisContainedBy, asyncResp, chassisId));
307a5617496SJie Yang 
308a5617496SJie Yang     dbus::utility::getAssociationEndPoints(
309a5617496SJie Yang         chassisPath + "/containing",
310a5617496SJie Yang         std::bind_front(getChassisContains, asyncResp, chassisId));
311a5617496SJie Yang }
312a5617496SJie Yang 
313cf7eba09SNan Zhou /**
314cf7eba09SNan Zhou  * ChassisCollection derived class for delivering Chassis Collection Schema
315cf7eba09SNan Zhou  *  Functions triggers appropriate requests on DBus
316cf7eba09SNan Zhou  */
317cf7eba09SNan Zhou inline void requestRoutesChassisCollection(App& app)
318cf7eba09SNan Zhou {
319cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
320cf7eba09SNan Zhou         .privileges(redfish::privileges::getChassisCollection)
321cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
322cf7eba09SNan Zhou             std::bind_front(handleChassisCollectionGet, std::ref(app)));
32362d5e2e4SEd Tanous }
324e37f8451SRapkiewicz, Pawel 
325308f70c7SWilly Tu inline void
326308f70c7SWilly Tu     getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
327308f70c7SWilly Tu                            const std::string& connectionName,
328308f70c7SWilly Tu                            const std::string& path)
329308f70c7SWilly Tu {
3301e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
3311e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3321e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
3335e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
3341e1e598dSJonathan Doman                     const std::string& property) {
335308f70c7SWilly Tu         if (ec)
336308f70c7SWilly Tu         {
33762598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error for Location");
338308f70c7SWilly Tu             messages::internalError(asyncResp->res);
339308f70c7SWilly Tu             return;
340308f70c7SWilly Tu         }
341308f70c7SWilly Tu 
342002d39b4SEd Tanous         asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
3431e1e598dSJonathan Doman             property;
3441e1e598dSJonathan Doman     });
345308f70c7SWilly Tu }
346308f70c7SWilly Tu 
347308f70c7SWilly Tu inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
348308f70c7SWilly Tu                            const std::string& connectionName,
349308f70c7SWilly Tu                            const std::string& path)
350308f70c7SWilly Tu {
3511e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
3521e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3531e1e598dSJonathan Doman         "xyz.openbmc_project.Common.UUID", "UUID",
3545e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
3551e1e598dSJonathan Doman                     const std::string& chassisUUID) {
356308f70c7SWilly Tu         if (ec)
357308f70c7SWilly Tu         {
35862598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error for UUID");
359308f70c7SWilly Tu             messages::internalError(asyncResp->res);
360308f70c7SWilly Tu             return;
361308f70c7SWilly Tu         }
3621e1e598dSJonathan Doman         asyncResp->res.jsonValue["UUID"] = chassisUUID;
3631e1e598dSJonathan Doman     });
364308f70c7SWilly Tu }
365308f70c7SWilly Tu 
366cf7eba09SNan Zhou inline void
367cf7eba09SNan Zhou     handleChassisGet(App& app, const crow::Request& req,
36845ca1b86SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
369cf7eba09SNan Zhou                      const std::string& chassisId)
370cf7eba09SNan Zhou {
3713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37245ca1b86SEd Tanous     {
37345ca1b86SEd Tanous         return;
37445ca1b86SEd Tanous     }
375e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
376734bfe90SGunnar Mills         "xyz.openbmc_project.Inventory.Item.Board",
377adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
378734bfe90SGunnar Mills 
379e99073f5SGeorge Liu     dbus::utility::getSubTree(
380e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
38162d5e2e4SEd Tanous         [asyncResp, chassisId(std::string(chassisId))](
382e99073f5SGeorge Liu             const boost::system::error_code& ec,
383b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
38462d5e2e4SEd Tanous         if (ec)
3851abe55efSEd Tanous         {
38662598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
387f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
388daf36e2eSEd Tanous             return;
389daf36e2eSEd Tanous         }
390daf36e2eSEd Tanous         // Iterate over all retrieved ObjectPaths.
391cf7eba09SNan Zhou         for (const std::pair<
392cf7eba09SNan Zhou                  std::string,
393cf7eba09SNan Zhou                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3941214b7e7SGunnar Mills                  object : subtree)
3951abe55efSEd Tanous         {
396daf36e2eSEd Tanous             const std::string& path = object.first;
397cf7eba09SNan Zhou             const std::vector<std::pair<std::string, std::vector<std::string>>>&
3981214b7e7SGunnar Mills                 connectionNames = object.second;
3997e860f15SJohn Edward Broadbent 
400997093ebSGeorge Liu             sdbusplus::message::object_path objPath(path);
401997093ebSGeorge Liu             if (objPath.filename() != chassisId)
4021abe55efSEd Tanous             {
403daf36e2eSEd Tanous                 continue;
404daf36e2eSEd Tanous             }
40526f03899SShawn McCarney 
406a5617496SJie Yang             getChassisConnectivity(asyncResp, chassisId, path);
407a5617496SJie Yang 
408002d39b4SEd Tanous             auto health = std::make_shared<HealthPopulate>(asyncResp);
409b49ac873SJames Feist 
41013451e39SWilly Tu             if constexpr (bmcwebEnableHealthPopulate)
41113451e39SWilly Tu             {
4126c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
4136c3e9451SGeorge Liu                     path + "/all_sensors",
4145e7e2dc5SEd Tanous                     [health](const boost::system::error_code& ec2,
4156c3e9451SGeorge Liu                              const dbus::utility::MapperEndPoints& resp) {
41623a21a1cSEd Tanous                     if (ec2)
417b49ac873SJames Feist                     {
418b49ac873SJames Feist                         return; // no sensors = no failures
419b49ac873SJames Feist                     }
4201e1e598dSJonathan Doman                     health->inventory = resp;
4211e1e598dSJonathan Doman                 });
422b49ac873SJames Feist 
423b49ac873SJames Feist                 health->populate();
42413451e39SWilly Tu             }
425b49ac873SJames Feist 
42626f6976fSEd Tanous             if (connectionNames.empty())
4271abe55efSEd Tanous             {
42862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
429e0d918bcSEd Tanous                 continue;
430daf36e2eSEd Tanous             }
431e0d918bcSEd Tanous 
43249c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["@odata.type"] =
433523d4868SLogananth Sundararaj                 "#Chassis.v1_22_0.Chassis";
43449c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["@odata.id"] =
435ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
43649c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["Name"] = "Chassis Collection";
43749c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["ChassisType"] = "RackMount";
438cf7eba09SNan Zhou             asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
439ef4c65b7SEd Tanous                 boost::urls::format(
440ef4c65b7SEd Tanous                     "/redfish/v1/Chassis/{}/Actions/Chassis.Reset", chassisId);
4411476687dSEd Tanous             asyncResp->res
442cf7eba09SNan Zhou                 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
443ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
444ef4c65b7SEd Tanous                                     chassisId);
4451476687dSEd Tanous             asyncResp->res.jsonValue["PCIeDevices"]["@odata.id"] =
446ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices";
44749c53ac9SJohnathan Mantey 
4486c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4496c3e9451SGeorge Liu                 path + "/drive",
4506c3e9451SGeorge Liu                 [asyncResp,
4516c3e9451SGeorge Liu                  chassisId](const boost::system::error_code& ec3,
4526c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
45392903bd4SJohn Edward Broadbent                 if (ec3 || resp.empty())
45492903bd4SJohn Edward Broadbent                 {
45592903bd4SJohn Edward Broadbent                     return; // no drives = no failures
45692903bd4SJohn Edward Broadbent                 }
45792903bd4SJohn Edward Broadbent 
45892903bd4SJohn Edward Broadbent                 nlohmann::json reference;
459ef4c65b7SEd Tanous                 reference["@odata.id"] = boost::urls::format(
460ef4c65b7SEd Tanous                     "/redfish/v1/Chassis/{}/Drives", chassisId);
46192903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Drives"] = std::move(reference);
46292903bd4SJohn Edward Broadbent             });
46392903bd4SJohn Edward Broadbent 
464002d39b4SEd Tanous             const std::string& connectionName = connectionNames[0].first;
4651c8fba97SJames Feist 
46623a21a1cSEd Tanous             const std::vector<std::string>& interfaces2 =
4671c8fba97SJames Feist                 connectionNames[0].second;
4681c8fba97SJames Feist             const std::array<const char*, 2> hasIndicatorLed = {
4691c8fba97SJames Feist                 "xyz.openbmc_project.Inventory.Item.Panel",
4700fda0f12SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
4711c8fba97SJames Feist 
472476b9cc5STejas Patil             const std::string assetTagInterface =
4730fda0f12SGeorge Liu                 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
474523d4868SLogananth Sundararaj             const std::string replaceableInterface =
475523d4868SLogananth Sundararaj                 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
476523d4868SLogananth Sundararaj             for (const auto& interface : interfaces2)
477523d4868SLogananth Sundararaj             {
478523d4868SLogananth Sundararaj                 if (interface == assetTagInterface)
479476b9cc5STejas Patil                 {
4801e1e598dSJonathan Doman                     sdbusplus::asio::getProperty<std::string>(
481002d39b4SEd Tanous                         *crow::connections::systemBus, connectionName, path,
482002d39b4SEd Tanous                         assetTagInterface, "AssetTag",
483523d4868SLogananth Sundararaj                         [asyncResp,
484523d4868SLogananth Sundararaj                          chassisId](const boost::system::error_code& ec2,
4851e1e598dSJonathan Doman                                     const std::string& property) {
4868a592810SEd Tanous                         if (ec2)
487476b9cc5STejas Patil                         {
48862598e31SEd Tanous                             BMCWEB_LOG_ERROR(
48962598e31SEd Tanous                                 "DBus response error for AssetTag: {}", ec2);
490476b9cc5STejas Patil                             messages::internalError(asyncResp->res);
491476b9cc5STejas Patil                             return;
492476b9cc5STejas Patil                         }
493002d39b4SEd Tanous                         asyncResp->res.jsonValue["AssetTag"] = property;
4941e1e598dSJonathan Doman                     });
495476b9cc5STejas Patil                 }
496523d4868SLogananth Sundararaj                 else if (interface == replaceableInterface)
497523d4868SLogananth Sundararaj                 {
498523d4868SLogananth Sundararaj                     sdbusplus::asio::getProperty<bool>(
499523d4868SLogananth Sundararaj                         *crow::connections::systemBus, connectionName, path,
500523d4868SLogananth Sundararaj                         replaceableInterface, "HotPluggable",
501523d4868SLogananth Sundararaj                         [asyncResp,
502523d4868SLogananth Sundararaj                          chassisId](const boost::system::error_code& ec2,
503523d4868SLogananth Sundararaj                                     const bool property) {
504523d4868SLogananth Sundararaj                         if (ec2)
505523d4868SLogananth Sundararaj                         {
50662598e31SEd Tanous                             BMCWEB_LOG_ERROR(
50762598e31SEd Tanous                                 "DBus response error for HotPluggable: {}",
50862598e31SEd Tanous                                 ec2);
509523d4868SLogananth Sundararaj                             messages::internalError(asyncResp->res);
510523d4868SLogananth Sundararaj                             return;
511523d4868SLogananth Sundararaj                         }
512523d4868SLogananth Sundararaj                         asyncResp->res.jsonValue["HotPluggable"] = property;
513523d4868SLogananth Sundararaj                     });
514523d4868SLogananth Sundararaj                 }
515523d4868SLogananth Sundararaj             }
516476b9cc5STejas Patil 
5171c8fba97SJames Feist             for (const char* interface : hasIndicatorLed)
5181c8fba97SJames Feist             {
5193544d2a7SEd Tanous                 if (std::ranges::find(interfaces2, interface) !=
5203544d2a7SEd Tanous                     interfaces2.end())
5211c8fba97SJames Feist                 {
5221c8fba97SJames Feist                     getIndicatorLedState(asyncResp);
523*59a17e4fSGeorge Liu                     getSystemLocationIndicatorActive(asyncResp);
5241c8fba97SJames Feist                     break;
5251c8fba97SJames Feist                 }
5261c8fba97SJames Feist             }
5271c8fba97SJames Feist 
52886d89ed7SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
52986d89ed7SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
53086d89ed7SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
5315e577bc1SWilly Tu                 [asyncResp, chassisId(std::string(chassisId)),
5325e577bc1SWilly Tu                  path](const boost::system::error_code& /*ec2*/,
533cf7eba09SNan Zhou                        const dbus::utility::DBusPropertiesMap& propertiesList) {
53486d89ed7SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
53586d89ed7SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
53686d89ed7SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
53786d89ed7SKrzysztof Grobelny                 const std::string* model = nullptr;
53886d89ed7SKrzysztof Grobelny                 const std::string* sparePartNumber = nullptr;
53986d89ed7SKrzysztof Grobelny 
54086d89ed7SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
54186d89ed7SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
54286d89ed7SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
54386d89ed7SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model,
54486d89ed7SKrzysztof Grobelny                     "SparePartNumber", sparePartNumber);
54586d89ed7SKrzysztof Grobelny 
54686d89ed7SKrzysztof Grobelny                 if (!success)
5471abe55efSEd Tanous                 {
548002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
549caa11f7aSAlpana Kumari                     return;
550daf36e2eSEd Tanous                 }
55186d89ed7SKrzysztof Grobelny 
55286d89ed7SKrzysztof Grobelny                 if (partNumber != nullptr)
55386d89ed7SKrzysztof Grobelny                 {
55486d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["PartNumber"] = *partNumber;
55586d89ed7SKrzysztof Grobelny                 }
55686d89ed7SKrzysztof Grobelny 
55786d89ed7SKrzysztof Grobelny                 if (serialNumber != nullptr)
55886d89ed7SKrzysztof Grobelny                 {
55986d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
56086d89ed7SKrzysztof Grobelny                 }
56186d89ed7SKrzysztof Grobelny 
56286d89ed7SKrzysztof Grobelny                 if (manufacturer != nullptr)
56386d89ed7SKrzysztof Grobelny                 {
56486d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
56586d89ed7SKrzysztof Grobelny                 }
56686d89ed7SKrzysztof Grobelny 
56786d89ed7SKrzysztof Grobelny                 if (model != nullptr)
56886d89ed7SKrzysztof Grobelny                 {
56986d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["Model"] = *model;
57086d89ed7SKrzysztof Grobelny                 }
57186d89ed7SKrzysztof Grobelny 
572caa11f7aSAlpana Kumari                 // SparePartNumber is optional on D-Bus
573caa11f7aSAlpana Kumari                 // so skip if it is empty
57486d89ed7SKrzysztof Grobelny                 if (sparePartNumber != nullptr && !sparePartNumber->empty())
575caa11f7aSAlpana Kumari                 {
57686d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["SparePartNumber"] =
57786d89ed7SKrzysztof Grobelny                         *sparePartNumber;
578caa11f7aSAlpana Kumari                 }
57986d89ed7SKrzysztof Grobelny 
58062d5e2e4SEd Tanous                 asyncResp->res.jsonValue["Name"] = chassisId;
58162d5e2e4SEd Tanous                 asyncResp->res.jsonValue["Id"] = chassisId;
5820256b694Szhanghch05 #ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
583002d39b4SEd Tanous                 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
584ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/Thermal",
585ef4c65b7SEd Tanous                                         chassisId);
5862474adfaSEd Tanous                 // Power object
5871476687dSEd Tanous                 asyncResp->res.jsonValue["Power"]["@odata.id"] =
588ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/Power",
589ef4c65b7SEd Tanous                                         chassisId);
5900256b694Szhanghch05 #endif
5912973963eSXiaochao Ma #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
5922973963eSXiaochao Ma                 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
593ef4c65b7SEd Tanous                     boost::urls::format(
594ef4c65b7SEd Tanous                         "/redfish/v1/Chassis/{}/ThermalSubsystem", chassisId);
59577b36437SChicago Duan                 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
596ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem",
597ef4c65b7SEd Tanous                                         chassisId);
5984ca3ec3cSAlbert Zhang                 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
599ef4c65b7SEd Tanous                     boost::urls::format(
600ef4c65b7SEd Tanous                         "/redfish/v1/Chassis/{}/EnvironmentMetrics", chassisId);
6012973963eSXiaochao Ma #endif
60295a3ecadSAnthony Wilson                 // SensorCollection
603002d39b4SEd Tanous                 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
604ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/Sensors",
605ef4c65b7SEd Tanous                                         chassisId);
606002d39b4SEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
6071476687dSEd Tanous 
6081476687dSEd Tanous                 nlohmann::json::array_t computerSystems;
6091476687dSEd Tanous                 nlohmann::json::object_t system;
610002d39b4SEd Tanous                 system["@odata.id"] = "/redfish/v1/Systems/system";
611ad539545SPatrick Williams                 computerSystems.emplace_back(std::move(system));
612002d39b4SEd Tanous                 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
6131476687dSEd Tanous                     std::move(computerSystems);
6141476687dSEd Tanous 
6151476687dSEd Tanous                 nlohmann::json::array_t managedBy;
6161476687dSEd Tanous                 nlohmann::json::object_t manager;
617002d39b4SEd Tanous                 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
618ad539545SPatrick Williams                 managedBy.emplace_back(std::move(manager));
6197e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Links"]["ManagedBy"] =
6201476687dSEd Tanous                     std::move(managedBy);
621beeca0aeSGunnar Mills                 getChassisState(asyncResp);
6225e577bc1SWilly Tu                 getStorageLink(asyncResp, path);
62386d89ed7SKrzysztof Grobelny             });
6242c37b4b0SSharad Yadav 
625308f70c7SWilly Tu             for (const auto& interface : interfaces2)
6262c37b4b0SSharad Yadav             {
627308f70c7SWilly Tu                 if (interface == "xyz.openbmc_project.Common.UUID")
6282c37b4b0SSharad Yadav                 {
629308f70c7SWilly Tu                     getChassisUUID(asyncResp, connectionName, path);
6302c37b4b0SSharad Yadav                 }
631cf7eba09SNan Zhou                 else if (interface ==
6320fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
6332c37b4b0SSharad Yadav                 {
634002d39b4SEd Tanous                     getChassisLocationCode(asyncResp, connectionName, path);
6352c37b4b0SSharad Yadav                 }
6362c37b4b0SSharad Yadav             }
6372c37b4b0SSharad Yadav 
638daf36e2eSEd Tanous             return;
639daf36e2eSEd Tanous         }
640e0d918bcSEd Tanous 
641daf36e2eSEd Tanous         // Couldn't find an object with that name.  return an error
642d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
643e99073f5SGeorge Liu     });
644c181942fSQiang XU 
645c181942fSQiang XU     getPhysicalSecurityData(asyncResp);
646cf7eba09SNan Zhou }
6471c8fba97SJames Feist 
648cf7eba09SNan Zhou inline void
649cf7eba09SNan Zhou     handleChassisPatch(App& app, const crow::Request& req,
6507e860f15SJohn Edward Broadbent                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
651cf7eba09SNan Zhou                        const std::string& param)
652cf7eba09SNan Zhou {
6533ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65445ca1b86SEd Tanous     {
65545ca1b86SEd Tanous         return;
65645ca1b86SEd Tanous     }
6579f8bfa7cSGunnar Mills     std::optional<bool> locationIndicatorActive;
6581c8fba97SJames Feist     std::optional<std::string> indicatorLed;
6591c8fba97SJames Feist 
6607e860f15SJohn Edward Broadbent     if (param.empty())
6611c8fba97SJames Feist     {
6621c8fba97SJames Feist         return;
6631c8fba97SJames Feist     }
6641c8fba97SJames Feist 
66515ed6780SWilly Tu     if (!json_util::readJsonPatch(
6667e860f15SJohn Edward Broadbent             req, asyncResp->res, "LocationIndicatorActive",
6677e860f15SJohn Edward Broadbent             locationIndicatorActive, "IndicatorLED", indicatorLed))
6681c8fba97SJames Feist     {
6691c8fba97SJames Feist         return;
6701c8fba97SJames Feist     }
6711c8fba97SJames Feist 
6729f8bfa7cSGunnar Mills     // TODO (Gunnar): Remove IndicatorLED after enough time has passed
6739f8bfa7cSGunnar Mills     if (!locationIndicatorActive && !indicatorLed)
6741c8fba97SJames Feist     {
6751c8fba97SJames Feist         return; // delete this when we support more patch properties
6761c8fba97SJames Feist     }
677d6aa0093SGunnar Mills     if (indicatorLed)
678d6aa0093SGunnar Mills     {
6797e860f15SJohn Edward Broadbent         asyncResp->res.addHeader(
6807e860f15SJohn Edward Broadbent             boost::beast::http::field::warning,
6810fda0f12SGeorge Liu             "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
682d6aa0093SGunnar Mills     }
6831c8fba97SJames Feist 
684e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
6851c8fba97SJames Feist         "xyz.openbmc_project.Inventory.Item.Board",
6861c8fba97SJames Feist         "xyz.openbmc_project.Inventory.Item.Chassis"};
6871c8fba97SJames Feist 
6887e860f15SJohn Edward Broadbent     const std::string& chassisId = param;
6891c8fba97SJames Feist 
690e99073f5SGeorge Liu     dbus::utility::getSubTree(
691e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
692cf7eba09SNan Zhou         [asyncResp, chassisId, locationIndicatorActive,
6935e7e2dc5SEd Tanous          indicatorLed](const boost::system::error_code& ec,
694b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
6951c8fba97SJames Feist         if (ec)
6961c8fba97SJames Feist         {
69762598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
6981c8fba97SJames Feist             messages::internalError(asyncResp->res);
6991c8fba97SJames Feist             return;
7001c8fba97SJames Feist         }
7011c8fba97SJames Feist 
7021c8fba97SJames Feist         // Iterate over all retrieved ObjectPaths.
703cf7eba09SNan Zhou         for (const std::pair<
704cf7eba09SNan Zhou                  std::string,
705cf7eba09SNan Zhou                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
7061214b7e7SGunnar Mills                  object : subtree)
7071c8fba97SJames Feist         {
7081c8fba97SJames Feist             const std::string& path = object.first;
709cf7eba09SNan Zhou             const std::vector<std::pair<std::string, std::vector<std::string>>>&
7101214b7e7SGunnar Mills                 connectionNames = object.second;
7111c8fba97SJames Feist 
712997093ebSGeorge Liu             sdbusplus::message::object_path objPath(path);
713997093ebSGeorge Liu             if (objPath.filename() != chassisId)
7141c8fba97SJames Feist             {
7151c8fba97SJames Feist                 continue;
7161c8fba97SJames Feist             }
7171c8fba97SJames Feist 
71826f6976fSEd Tanous             if (connectionNames.empty())
7191c8fba97SJames Feist             {
72062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
7211c8fba97SJames Feist                 continue;
7221c8fba97SJames Feist             }
7231c8fba97SJames Feist 
72423a21a1cSEd Tanous             const std::vector<std::string>& interfaces3 =
7251c8fba97SJames Feist                 connectionNames[0].second;
7261c8fba97SJames Feist 
7271c8fba97SJames Feist             const std::array<const char*, 2> hasIndicatorLed = {
7281c8fba97SJames Feist                 "xyz.openbmc_project.Inventory.Item.Panel",
7290fda0f12SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
7301c8fba97SJames Feist             bool indicatorChassis = false;
7311c8fba97SJames Feist             for (const char* interface : hasIndicatorLed)
7321c8fba97SJames Feist             {
7333544d2a7SEd Tanous                 if (std::ranges::find(interfaces3, interface) !=
7343544d2a7SEd Tanous                     interfaces3.end())
7351c8fba97SJames Feist                 {
7361c8fba97SJames Feist                     indicatorChassis = true;
7371c8fba97SJames Feist                     break;
7381c8fba97SJames Feist                 }
7391c8fba97SJames Feist             }
7409f8bfa7cSGunnar Mills             if (locationIndicatorActive)
7419f8bfa7cSGunnar Mills             {
7429f8bfa7cSGunnar Mills                 if (indicatorChassis)
7439f8bfa7cSGunnar Mills                 {
744*59a17e4fSGeorge Liu                     setSystemLocationIndicatorActive(asyncResp,
745002d39b4SEd Tanous                                                      *locationIndicatorActive);
7469f8bfa7cSGunnar Mills                 }
7479f8bfa7cSGunnar Mills                 else
7489f8bfa7cSGunnar Mills                 {
749002d39b4SEd Tanous                     messages::propertyUnknown(asyncResp->res,
750002d39b4SEd Tanous                                               "LocationIndicatorActive");
7519f8bfa7cSGunnar Mills                 }
7529f8bfa7cSGunnar Mills             }
7539f8bfa7cSGunnar Mills             if (indicatorLed)
7549f8bfa7cSGunnar Mills             {
7551c8fba97SJames Feist                 if (indicatorChassis)
7561c8fba97SJames Feist                 {
757f23b7296SEd Tanous                     setIndicatorLedState(asyncResp, *indicatorLed);
7581c8fba97SJames Feist                 }
7591c8fba97SJames Feist                 else
7601c8fba97SJames Feist                 {
761cf7eba09SNan Zhou                     messages::propertyUnknown(asyncResp->res, "IndicatorLED");
7621c8fba97SJames Feist                 }
7631c8fba97SJames Feist             }
7641c8fba97SJames Feist             return;
7651c8fba97SJames Feist         }
7661c8fba97SJames Feist 
767d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
768e99073f5SGeorge Liu     });
769cf7eba09SNan Zhou }
770cf7eba09SNan Zhou 
771cf7eba09SNan Zhou /**
772cf7eba09SNan Zhou  * Chassis override class for delivering Chassis Schema
773cf7eba09SNan Zhou  * Functions triggers appropriate requests on DBus
774cf7eba09SNan Zhou  */
775cf7eba09SNan Zhou inline void requestRoutesChassis(App& app)
776cf7eba09SNan Zhou {
777cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
778cf7eba09SNan Zhou         .privileges(redfish::privileges::getChassis)
779cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
780cf7eba09SNan Zhou             std::bind_front(handleChassisGet, std::ref(app)));
781cf7eba09SNan Zhou 
782cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
783cf7eba09SNan Zhou         .privileges(redfish::privileges::patchChassis)
784cf7eba09SNan Zhou         .methods(boost::beast::http::verb::patch)(
785cf7eba09SNan Zhou             std::bind_front(handleChassisPatch, std::ref(app)));
7861c8fba97SJames Feist }
787dd99e04bSP.K. Lee 
788fc903b3dSAndrew Geissler /**
789fc903b3dSAndrew Geissler  * Handle error responses from d-bus for chassis power cycles
790fc903b3dSAndrew Geissler  */
791fc903b3dSAndrew Geissler inline void handleChassisPowerCycleError(const boost::system::error_code& ec,
792fc903b3dSAndrew Geissler                                          const sdbusplus::message_t& eMsg,
793fc903b3dSAndrew Geissler                                          crow::Response& res)
794fc903b3dSAndrew Geissler {
795fc903b3dSAndrew Geissler     if (eMsg.get_error() == nullptr)
796fc903b3dSAndrew Geissler     {
79762598e31SEd Tanous         BMCWEB_LOG_ERROR("D-Bus response error: {}", ec);
798fc903b3dSAndrew Geissler         messages::internalError(res);
799fc903b3dSAndrew Geissler         return;
800fc903b3dSAndrew Geissler     }
801fc903b3dSAndrew Geissler     std::string_view errorMessage = eMsg.get_error()->name;
802fc903b3dSAndrew Geissler 
803fc903b3dSAndrew Geissler     // If operation failed due to BMC not being in Ready state, tell
804fc903b3dSAndrew Geissler     // user to retry in a bit
805fc903b3dSAndrew Geissler     if (errorMessage ==
806fc903b3dSAndrew Geissler         std::string_view("xyz.openbmc_project.State.Chassis.Error.BMCNotReady"))
807fc903b3dSAndrew Geissler     {
80862598e31SEd Tanous         BMCWEB_LOG_DEBUG("BMC not ready, operation not allowed right now");
809fc903b3dSAndrew Geissler         messages::serviceTemporarilyUnavailable(res, "10");
810fc903b3dSAndrew Geissler         return;
811fc903b3dSAndrew Geissler     }
812fc903b3dSAndrew Geissler 
81362598e31SEd Tanous     BMCWEB_LOG_ERROR("Chassis Power Cycle fail {} sdbusplus:{}", ec,
81462598e31SEd Tanous                      errorMessage);
815fc903b3dSAndrew Geissler     messages::internalError(res);
816fc903b3dSAndrew Geissler }
817fc903b3dSAndrew Geissler 
8188d1b46d7Szhanghch05 inline void
8198d1b46d7Szhanghch05     doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
820dd99e04bSP.K. Lee {
8217a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
822c3b3c92aSVijay Khemka         "xyz.openbmc_project.State.Chassis"};
823c3b3c92aSVijay Khemka 
824c3b3c92aSVijay Khemka     // Use mapper to get subtree paths.
8257a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
8267a1dbc48SGeorge Liu         "/", 0, interfaces,
827b9d36b47SEd Tanous         [asyncResp](
8287a1dbc48SGeorge Liu             const boost::system::error_code& ec,
829b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
830c3b3c92aSVijay Khemka         if (ec)
831c3b3c92aSVijay Khemka         {
83262598e31SEd Tanous             BMCWEB_LOG_ERROR("[mapper] Bad D-Bus request error: {}", ec);
833c3b3c92aSVijay Khemka             messages::internalError(asyncResp->res);
834c3b3c92aSVijay Khemka             return;
835c3b3c92aSVijay Khemka         }
836c3b3c92aSVijay Khemka 
837dd99e04bSP.K. Lee         const char* processName = "xyz.openbmc_project.State.Chassis";
838dd99e04bSP.K. Lee         const char* interfaceName = "xyz.openbmc_project.State.Chassis";
839dd99e04bSP.K. Lee         const char* destProperty = "RequestedPowerTransition";
840dd99e04bSP.K. Lee         const std::string propertyValue =
841dd99e04bSP.K. Lee             "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
842002d39b4SEd Tanous         std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
843c3b3c92aSVijay Khemka 
844c3b3c92aSVijay Khemka         /* Look for system reset chassis path */
8453544d2a7SEd Tanous         if ((std::ranges::find(chassisList, objectPath)) == chassisList.end())
846c3b3c92aSVijay Khemka         {
847c3b3c92aSVijay Khemka             /* We prefer to reset the full chassis_system, but if it doesn't
848c3b3c92aSVijay Khemka              * exist on some platforms, fall back to a host-only power reset
849c3b3c92aSVijay Khemka              */
850c3b3c92aSVijay Khemka             objectPath = "/xyz/openbmc_project/state/chassis0";
851c3b3c92aSVijay Khemka         }
852dd99e04bSP.K. Lee 
8539ae226faSGeorge Liu         sdbusplus::asio::setProperty(
8549ae226faSGeorge Liu             *crow::connections::systemBus, processName, objectPath,
8559ae226faSGeorge Liu             interfaceName, destProperty, propertyValue,
856fc903b3dSAndrew Geissler             [asyncResp](const boost::system::error_code& ec2,
857fc903b3dSAndrew Geissler                         sdbusplus::message_t& sdbusErrMsg) {
858dd99e04bSP.K. Lee             // Use "Set" method to set the property value.
8598a592810SEd Tanous             if (ec2)
860dd99e04bSP.K. Lee             {
861fc903b3dSAndrew Geissler                 handleChassisPowerCycleError(ec2, sdbusErrMsg, asyncResp->res);
862fc903b3dSAndrew Geissler 
863dd99e04bSP.K. Lee                 return;
864dd99e04bSP.K. Lee             }
865dd99e04bSP.K. Lee 
866dd99e04bSP.K. Lee             messages::success(asyncResp->res);
8679ae226faSGeorge Liu         });
8687a1dbc48SGeorge Liu     });
869dd99e04bSP.K. Lee }
870dd99e04bSP.K. Lee 
871cf7eba09SNan Zhou inline void handleChassisResetActionInfoPost(
872cf7eba09SNan Zhou     App& app, const crow::Request& req,
8737e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
874cf7eba09SNan Zhou     const std::string& /*chassisId*/)
875cf7eba09SNan Zhou {
8763ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
87745ca1b86SEd Tanous     {
87845ca1b86SEd Tanous         return;
87945ca1b86SEd Tanous     }
88062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Post Chassis Reset.");
881dd99e04bSP.K. Lee 
882dd99e04bSP.K. Lee     std::string resetType;
883dd99e04bSP.K. Lee 
884cf7eba09SNan Zhou     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
885dd99e04bSP.K. Lee     {
886dd99e04bSP.K. Lee         return;
887dd99e04bSP.K. Lee     }
888dd99e04bSP.K. Lee 
889dd99e04bSP.K. Lee     if (resetType != "PowerCycle")
890dd99e04bSP.K. Lee     {
89162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
892002d39b4SEd Tanous         messages::actionParameterNotSupported(asyncResp->res, resetType,
893002d39b4SEd Tanous                                               "ResetType");
894dd99e04bSP.K. Lee 
895dd99e04bSP.K. Lee         return;
896dd99e04bSP.K. Lee     }
897dd99e04bSP.K. Lee     doChassisPowerCycle(asyncResp);
898dd99e04bSP.K. Lee }
8991cb1a9e6SAppaRao Puli 
9001cb1a9e6SAppaRao Puli /**
901cf7eba09SNan Zhou  * ChassisResetAction class supports the POST method for the Reset
902cf7eba09SNan Zhou  * action.
903cf7eba09SNan Zhou  * Function handles POST method request.
904cf7eba09SNan Zhou  * Analyzes POST body before sending Reset request data to D-Bus.
9051cb1a9e6SAppaRao Puli  */
906cf7eba09SNan Zhou 
907cf7eba09SNan Zhou inline void requestRoutesChassisResetAction(App& app)
9081cb1a9e6SAppaRao Puli {
909cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
910cf7eba09SNan Zhou         .privileges(redfish::privileges::postChassis)
911cf7eba09SNan Zhou         .methods(boost::beast::http::verb::post)(
912cf7eba09SNan Zhou             std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
913cf7eba09SNan Zhou }
914cf7eba09SNan Zhou 
915cf7eba09SNan Zhou inline void handleChassisResetActionInfoGet(
916cf7eba09SNan Zhou     App& app, const crow::Request& req,
9177e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
918cf7eba09SNan Zhou     const std::string& chassisId)
919cf7eba09SNan Zhou {
9203ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9211cb1a9e6SAppaRao Puli     {
92245ca1b86SEd Tanous         return;
92345ca1b86SEd Tanous     }
924cf7eba09SNan Zhou     asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
925ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
926ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
9271476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Reset Action Info";
9281476687dSEd Tanous 
9291476687dSEd Tanous     asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
9305b9e95a1SNan Zhou     nlohmann::json::array_t parameters;
9315b9e95a1SNan Zhou     nlohmann::json::object_t parameter;
9325b9e95a1SNan Zhou     parameter["Name"] = "ResetType";
9335b9e95a1SNan Zhou     parameter["Required"] = true;
9345b9e95a1SNan Zhou     parameter["DataType"] = "String";
9351476687dSEd Tanous     nlohmann::json::array_t allowed;
936ad539545SPatrick Williams     allowed.emplace_back("PowerCycle");
9375b9e95a1SNan Zhou     parameter["AllowableValues"] = std::move(allowed);
938ad539545SPatrick Williams     parameters.emplace_back(std::move(parameter));
9395b9e95a1SNan Zhou 
9401476687dSEd Tanous     asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
941cf7eba09SNan Zhou }
942cf7eba09SNan Zhou 
943cf7eba09SNan Zhou /**
944cf7eba09SNan Zhou  * ChassisResetActionInfo derived class for delivering Chassis
945cf7eba09SNan Zhou  * ResetType AllowableValues using ResetInfo schema.
946cf7eba09SNan Zhou  */
947cf7eba09SNan Zhou inline void requestRoutesChassisResetActionInfo(App& app)
948cf7eba09SNan Zhou {
949cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
950cf7eba09SNan Zhou         .privileges(redfish::privileges::getActionInfo)
951cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
952cf7eba09SNan Zhou             std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
9531cb1a9e6SAppaRao Puli }
9541cb1a9e6SAppaRao Puli 
955e37f8451SRapkiewicz, Pawel } // namespace redfish
956