xref: /openbmc/bmcweb/features/redfish/lib/chassis.hpp (revision 3544d2a719c8bb7b07f9a39f61a3770ec84b909f)
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>
37*3544d2a7SEd 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(
2127a1dbc48SGeorge Liu         asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
213cf7eba09SNan Zhou }
214cf7eba09SNan Zhou 
215a5617496SJie Yang inline void getChassisContainedBy(
216a5617496SJie Yang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
217a5617496SJie Yang     const std::string& chassisId, const boost::system::error_code& ec,
218a5617496SJie Yang     const dbus::utility::MapperEndPoints& upstreamChassisPaths)
219a5617496SJie Yang {
220a5617496SJie Yang     if (ec)
221a5617496SJie Yang     {
222a5617496SJie Yang         if (ec.value() != EBADR)
223a5617496SJie Yang         {
22462598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
225a5617496SJie Yang             messages::internalError(asyncResp->res);
226a5617496SJie Yang         }
227a5617496SJie Yang         return;
228a5617496SJie Yang     }
229a5617496SJie Yang     if (upstreamChassisPaths.empty())
230a5617496SJie Yang     {
231a5617496SJie Yang         return;
232a5617496SJie Yang     }
233a5617496SJie Yang     if (upstreamChassisPaths.size() > 1)
234a5617496SJie Yang     {
23562598e31SEd Tanous         BMCWEB_LOG_ERROR("{} is contained by mutliple chassis", chassisId);
236a5617496SJie Yang         messages::internalError(asyncResp->res);
237a5617496SJie Yang         return;
238a5617496SJie Yang     }
239a5617496SJie Yang 
240a5617496SJie Yang     sdbusplus::message::object_path upstreamChassisPath(
241a5617496SJie Yang         upstreamChassisPaths[0]);
242a5617496SJie Yang     std::string upstreamChassis = upstreamChassisPath.filename();
243a5617496SJie Yang     if (upstreamChassis.empty())
244a5617496SJie Yang     {
24562598e31SEd Tanous         BMCWEB_LOG_WARNING("Malformed upstream Chassis path {} on {}",
24662598e31SEd Tanous                            upstreamChassisPath.str, chassisId);
247a5617496SJie Yang         return;
248a5617496SJie Yang     }
249a5617496SJie Yang 
250a5617496SJie Yang     asyncResp->res.jsonValue["Links"]["ContainedBy"]["@odata.id"] =
251a5617496SJie Yang         boost::urls::format("/redfish/v1/Chassis/{}", upstreamChassis);
252a5617496SJie Yang }
253a5617496SJie Yang 
254a5617496SJie Yang inline void getChassisContains(
255a5617496SJie Yang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
256a5617496SJie Yang     const std::string& chassisId, const boost::system::error_code& ec,
257a5617496SJie Yang     const dbus::utility::MapperEndPoints& downstreamChassisPaths)
258a5617496SJie Yang {
259a5617496SJie Yang     if (ec)
260a5617496SJie Yang     {
261a5617496SJie Yang         if (ec.value() != EBADR)
262a5617496SJie Yang         {
26362598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
264a5617496SJie Yang             messages::internalError(asyncResp->res);
265a5617496SJie Yang         }
266a5617496SJie Yang         return;
267a5617496SJie Yang     }
268a5617496SJie Yang     if (downstreamChassisPaths.empty())
269a5617496SJie Yang     {
270a5617496SJie Yang         return;
271a5617496SJie Yang     }
272a5617496SJie Yang     nlohmann::json& jValue = asyncResp->res.jsonValue["Links"]["Contains"];
273a5617496SJie Yang     if (!jValue.is_array())
274a5617496SJie Yang     {
275a5617496SJie Yang         // Create the array if it was empty
276a5617496SJie Yang         jValue = nlohmann::json::array();
277a5617496SJie Yang     }
278a5617496SJie Yang     for (const auto& p : downstreamChassisPaths)
279a5617496SJie Yang     {
280a5617496SJie Yang         sdbusplus::message::object_path downstreamChassisPath(p);
281a5617496SJie Yang         std::string downstreamChassis = downstreamChassisPath.filename();
282a5617496SJie Yang         if (downstreamChassis.empty())
283a5617496SJie Yang         {
28462598e31SEd Tanous             BMCWEB_LOG_WARNING("Malformed downstream Chassis path {} on {}",
28562598e31SEd Tanous                                downstreamChassisPath.str, chassisId);
286a5617496SJie Yang             continue;
287a5617496SJie Yang         }
288a5617496SJie Yang         nlohmann::json link;
289a5617496SJie Yang         link["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
290a5617496SJie Yang                                                 downstreamChassis);
291a5617496SJie Yang         jValue.push_back(std::move(link));
292a5617496SJie Yang     }
293a5617496SJie Yang     asyncResp->res.jsonValue["Links"]["Contains@odata.count"] = jValue.size();
294a5617496SJie Yang }
295a5617496SJie Yang 
296a5617496SJie Yang inline void
297a5617496SJie Yang     getChassisConnectivity(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
298a5617496SJie Yang                            const std::string& chassisId,
299a5617496SJie Yang                            const std::string& chassisPath)
300a5617496SJie Yang {
30162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get chassis connectivity");
302a5617496SJie Yang 
303a5617496SJie Yang     dbus::utility::getAssociationEndPoints(
304a5617496SJie Yang         chassisPath + "/contained_by",
305a5617496SJie Yang         std::bind_front(getChassisContainedBy, asyncResp, chassisId));
306a5617496SJie Yang 
307a5617496SJie Yang     dbus::utility::getAssociationEndPoints(
308a5617496SJie Yang         chassisPath + "/containing",
309a5617496SJie Yang         std::bind_front(getChassisContains, asyncResp, chassisId));
310a5617496SJie Yang }
311a5617496SJie Yang 
312cf7eba09SNan Zhou /**
313cf7eba09SNan Zhou  * ChassisCollection derived class for delivering Chassis Collection Schema
314cf7eba09SNan Zhou  *  Functions triggers appropriate requests on DBus
315cf7eba09SNan Zhou  */
316cf7eba09SNan Zhou inline void requestRoutesChassisCollection(App& app)
317cf7eba09SNan Zhou {
318cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
319cf7eba09SNan Zhou         .privileges(redfish::privileges::getChassisCollection)
320cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
321cf7eba09SNan Zhou             std::bind_front(handleChassisCollectionGet, std::ref(app)));
32262d5e2e4SEd Tanous }
323e37f8451SRapkiewicz, Pawel 
324308f70c7SWilly Tu inline void
325308f70c7SWilly Tu     getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
326308f70c7SWilly Tu                            const std::string& connectionName,
327308f70c7SWilly Tu                            const std::string& path)
328308f70c7SWilly Tu {
3291e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
3301e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3311e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
3325e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
3331e1e598dSJonathan Doman                     const std::string& property) {
334308f70c7SWilly Tu         if (ec)
335308f70c7SWilly Tu         {
33662598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error for Location");
337308f70c7SWilly Tu             messages::internalError(asyncResp->res);
338308f70c7SWilly Tu             return;
339308f70c7SWilly Tu         }
340308f70c7SWilly Tu 
341002d39b4SEd Tanous         asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
3421e1e598dSJonathan Doman             property;
3431e1e598dSJonathan Doman         });
344308f70c7SWilly Tu }
345308f70c7SWilly Tu 
346308f70c7SWilly Tu inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
347308f70c7SWilly Tu                            const std::string& connectionName,
348308f70c7SWilly Tu                            const std::string& path)
349308f70c7SWilly Tu {
3501e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
3511e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3521e1e598dSJonathan Doman         "xyz.openbmc_project.Common.UUID", "UUID",
3535e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec,
3541e1e598dSJonathan Doman                     const std::string& chassisUUID) {
355308f70c7SWilly Tu         if (ec)
356308f70c7SWilly Tu         {
35762598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error for UUID");
358308f70c7SWilly Tu             messages::internalError(asyncResp->res);
359308f70c7SWilly Tu             return;
360308f70c7SWilly Tu         }
3611e1e598dSJonathan Doman         asyncResp->res.jsonValue["UUID"] = chassisUUID;
3621e1e598dSJonathan Doman         });
363308f70c7SWilly Tu }
364308f70c7SWilly Tu 
365cf7eba09SNan Zhou inline void
366cf7eba09SNan Zhou     handleChassisGet(App& app, const crow::Request& req,
36745ca1b86SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
368cf7eba09SNan Zhou                      const std::string& chassisId)
369cf7eba09SNan Zhou {
3703ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37145ca1b86SEd Tanous     {
37245ca1b86SEd Tanous         return;
37345ca1b86SEd Tanous     }
374e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
375734bfe90SGunnar Mills         "xyz.openbmc_project.Inventory.Item.Board",
376adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
377734bfe90SGunnar Mills 
378e99073f5SGeorge Liu     dbus::utility::getSubTree(
379e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
38062d5e2e4SEd Tanous         [asyncResp, chassisId(std::string(chassisId))](
381e99073f5SGeorge Liu             const boost::system::error_code& ec,
382b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
38362d5e2e4SEd Tanous         if (ec)
3841abe55efSEd Tanous         {
38562598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
386f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
387daf36e2eSEd Tanous             return;
388daf36e2eSEd Tanous         }
389daf36e2eSEd Tanous         // Iterate over all retrieved ObjectPaths.
390cf7eba09SNan Zhou         for (const std::pair<
391cf7eba09SNan Zhou                  std::string,
392cf7eba09SNan Zhou                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3931214b7e7SGunnar Mills                  object : subtree)
3941abe55efSEd Tanous         {
395daf36e2eSEd Tanous             const std::string& path = object.first;
396cf7eba09SNan Zhou             const std::vector<std::pair<std::string, std::vector<std::string>>>&
3971214b7e7SGunnar Mills                 connectionNames = object.second;
3987e860f15SJohn Edward Broadbent 
399997093ebSGeorge Liu             sdbusplus::message::object_path objPath(path);
400997093ebSGeorge Liu             if (objPath.filename() != chassisId)
4011abe55efSEd Tanous             {
402daf36e2eSEd Tanous                 continue;
403daf36e2eSEd Tanous             }
40426f03899SShawn McCarney 
405a5617496SJie Yang             getChassisConnectivity(asyncResp, chassisId, path);
406a5617496SJie Yang 
407002d39b4SEd Tanous             auto health = std::make_shared<HealthPopulate>(asyncResp);
408b49ac873SJames Feist 
40913451e39SWilly Tu             if constexpr (bmcwebEnableHealthPopulate)
41013451e39SWilly Tu             {
4116c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
4126c3e9451SGeorge Liu                     path + "/all_sensors",
4135e7e2dc5SEd Tanous                     [health](const boost::system::error_code& ec2,
4146c3e9451SGeorge Liu                              const dbus::utility::MapperEndPoints& resp) {
41523a21a1cSEd Tanous                     if (ec2)
416b49ac873SJames Feist                     {
417b49ac873SJames Feist                         return; // no sensors = no failures
418b49ac873SJames Feist                     }
4191e1e598dSJonathan Doman                     health->inventory = resp;
4201e1e598dSJonathan Doman                     });
421b49ac873SJames Feist 
422b49ac873SJames Feist                 health->populate();
42313451e39SWilly Tu             }
424b49ac873SJames Feist 
42526f6976fSEd Tanous             if (connectionNames.empty())
4261abe55efSEd Tanous             {
42762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
428e0d918bcSEd Tanous                 continue;
429daf36e2eSEd Tanous             }
430e0d918bcSEd Tanous 
43149c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["@odata.type"] =
432523d4868SLogananth Sundararaj                 "#Chassis.v1_22_0.Chassis";
43349c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["@odata.id"] =
434ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
43549c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["Name"] = "Chassis Collection";
43649c53ac9SJohnathan Mantey             asyncResp->res.jsonValue["ChassisType"] = "RackMount";
437cf7eba09SNan Zhou             asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
438ef4c65b7SEd Tanous                 boost::urls::format(
439ef4c65b7SEd Tanous                     "/redfish/v1/Chassis/{}/Actions/Chassis.Reset", chassisId);
4401476687dSEd Tanous             asyncResp->res
441cf7eba09SNan Zhou                 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
442ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
443ef4c65b7SEd Tanous                                     chassisId);
4441476687dSEd Tanous             asyncResp->res.jsonValue["PCIeDevices"]["@odata.id"] =
445ef4c65b7SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices";
44649c53ac9SJohnathan Mantey 
4476c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4486c3e9451SGeorge Liu                 path + "/drive",
4496c3e9451SGeorge Liu                 [asyncResp,
4506c3e9451SGeorge Liu                  chassisId](const boost::system::error_code& ec3,
4516c3e9451SGeorge Liu                             const dbus::utility::MapperEndPoints& resp) {
45292903bd4SJohn Edward Broadbent                 if (ec3 || resp.empty())
45392903bd4SJohn Edward Broadbent                 {
45492903bd4SJohn Edward Broadbent                     return; // no drives = no failures
45592903bd4SJohn Edward Broadbent                 }
45692903bd4SJohn Edward Broadbent 
45792903bd4SJohn Edward Broadbent                 nlohmann::json reference;
458ef4c65b7SEd Tanous                 reference["@odata.id"] = boost::urls::format(
459ef4c65b7SEd Tanous                     "/redfish/v1/Chassis/{}/Drives", chassisId);
46092903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Drives"] = std::move(reference);
46192903bd4SJohn Edward Broadbent                 });
46292903bd4SJohn Edward Broadbent 
463002d39b4SEd Tanous             const std::string& connectionName = connectionNames[0].first;
4641c8fba97SJames Feist 
46523a21a1cSEd Tanous             const std::vector<std::string>& interfaces2 =
4661c8fba97SJames Feist                 connectionNames[0].second;
4671c8fba97SJames Feist             const std::array<const char*, 2> hasIndicatorLed = {
4681c8fba97SJames Feist                 "xyz.openbmc_project.Inventory.Item.Panel",
4690fda0f12SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
4701c8fba97SJames Feist 
471476b9cc5STejas Patil             const std::string assetTagInterface =
4720fda0f12SGeorge Liu                 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
473523d4868SLogananth Sundararaj             const std::string replaceableInterface =
474523d4868SLogananth Sundararaj                 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
475523d4868SLogananth Sundararaj             for (const auto& interface : interfaces2)
476523d4868SLogananth Sundararaj             {
477523d4868SLogananth Sundararaj                 if (interface == assetTagInterface)
478476b9cc5STejas Patil                 {
4791e1e598dSJonathan Doman                     sdbusplus::asio::getProperty<std::string>(
480002d39b4SEd Tanous                         *crow::connections::systemBus, connectionName, path,
481002d39b4SEd Tanous                         assetTagInterface, "AssetTag",
482523d4868SLogananth Sundararaj                         [asyncResp,
483523d4868SLogananth Sundararaj                          chassisId](const boost::system::error_code& ec2,
4841e1e598dSJonathan Doman                                     const std::string& property) {
4858a592810SEd Tanous                         if (ec2)
486476b9cc5STejas Patil                         {
48762598e31SEd Tanous                             BMCWEB_LOG_ERROR(
48862598e31SEd Tanous                                 "DBus response error for AssetTag: {}", ec2);
489476b9cc5STejas Patil                             messages::internalError(asyncResp->res);
490476b9cc5STejas Patil                             return;
491476b9cc5STejas Patil                         }
492002d39b4SEd Tanous                         asyncResp->res.jsonValue["AssetTag"] = property;
4931e1e598dSJonathan Doman                         });
494476b9cc5STejas Patil                 }
495523d4868SLogananth Sundararaj                 else if (interface == replaceableInterface)
496523d4868SLogananth Sundararaj                 {
497523d4868SLogananth Sundararaj                     sdbusplus::asio::getProperty<bool>(
498523d4868SLogananth Sundararaj                         *crow::connections::systemBus, connectionName, path,
499523d4868SLogananth Sundararaj                         replaceableInterface, "HotPluggable",
500523d4868SLogananth Sundararaj                         [asyncResp,
501523d4868SLogananth Sundararaj                          chassisId](const boost::system::error_code& ec2,
502523d4868SLogananth Sundararaj                                     const bool property) {
503523d4868SLogananth Sundararaj                         if (ec2)
504523d4868SLogananth Sundararaj                         {
50562598e31SEd Tanous                             BMCWEB_LOG_ERROR(
50662598e31SEd Tanous                                 "DBus response error for HotPluggable: {}",
50762598e31SEd Tanous                                 ec2);
508523d4868SLogananth Sundararaj                             messages::internalError(asyncResp->res);
509523d4868SLogananth Sundararaj                             return;
510523d4868SLogananth Sundararaj                         }
511523d4868SLogananth Sundararaj                         asyncResp->res.jsonValue["HotPluggable"] = property;
512523d4868SLogananth Sundararaj                         });
513523d4868SLogananth Sundararaj                 }
514523d4868SLogananth Sundararaj             }
515476b9cc5STejas Patil 
5161c8fba97SJames Feist             for (const char* interface : hasIndicatorLed)
5171c8fba97SJames Feist             {
518*3544d2a7SEd Tanous                 if (std::ranges::find(interfaces2, interface) !=
519*3544d2a7SEd Tanous                     interfaces2.end())
5201c8fba97SJames Feist                 {
5211c8fba97SJames Feist                     getIndicatorLedState(asyncResp);
5229f8bfa7cSGunnar Mills                     getLocationIndicatorActive(asyncResp);
5231c8fba97SJames Feist                     break;
5241c8fba97SJames Feist                 }
5251c8fba97SJames Feist             }
5261c8fba97SJames Feist 
52786d89ed7SKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
52886d89ed7SKrzysztof Grobelny                 *crow::connections::systemBus, connectionName, path,
52986d89ed7SKrzysztof Grobelny                 "xyz.openbmc_project.Inventory.Decorator.Asset",
5305e577bc1SWilly Tu                 [asyncResp, chassisId(std::string(chassisId)),
5315e577bc1SWilly Tu                  path](const boost::system::error_code& /*ec2*/,
532cf7eba09SNan Zhou                        const dbus::utility::DBusPropertiesMap& propertiesList) {
53386d89ed7SKrzysztof Grobelny                 const std::string* partNumber = nullptr;
53486d89ed7SKrzysztof Grobelny                 const std::string* serialNumber = nullptr;
53586d89ed7SKrzysztof Grobelny                 const std::string* manufacturer = nullptr;
53686d89ed7SKrzysztof Grobelny                 const std::string* model = nullptr;
53786d89ed7SKrzysztof Grobelny                 const std::string* sparePartNumber = nullptr;
53886d89ed7SKrzysztof Grobelny 
53986d89ed7SKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
54086d89ed7SKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), propertiesList,
54186d89ed7SKrzysztof Grobelny                     "PartNumber", partNumber, "SerialNumber", serialNumber,
54286d89ed7SKrzysztof Grobelny                     "Manufacturer", manufacturer, "Model", model,
54386d89ed7SKrzysztof Grobelny                     "SparePartNumber", sparePartNumber);
54486d89ed7SKrzysztof Grobelny 
54586d89ed7SKrzysztof Grobelny                 if (!success)
5461abe55efSEd Tanous                 {
547002d39b4SEd Tanous                     messages::internalError(asyncResp->res);
548caa11f7aSAlpana Kumari                     return;
549daf36e2eSEd Tanous                 }
55086d89ed7SKrzysztof Grobelny 
55186d89ed7SKrzysztof Grobelny                 if (partNumber != nullptr)
55286d89ed7SKrzysztof Grobelny                 {
55386d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["PartNumber"] = *partNumber;
55486d89ed7SKrzysztof Grobelny                 }
55586d89ed7SKrzysztof Grobelny 
55686d89ed7SKrzysztof Grobelny                 if (serialNumber != nullptr)
55786d89ed7SKrzysztof Grobelny                 {
55886d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
55986d89ed7SKrzysztof Grobelny                 }
56086d89ed7SKrzysztof Grobelny 
56186d89ed7SKrzysztof Grobelny                 if (manufacturer != nullptr)
56286d89ed7SKrzysztof Grobelny                 {
56386d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
56486d89ed7SKrzysztof Grobelny                 }
56586d89ed7SKrzysztof Grobelny 
56686d89ed7SKrzysztof Grobelny                 if (model != nullptr)
56786d89ed7SKrzysztof Grobelny                 {
56886d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["Model"] = *model;
56986d89ed7SKrzysztof Grobelny                 }
57086d89ed7SKrzysztof Grobelny 
571caa11f7aSAlpana Kumari                 // SparePartNumber is optional on D-Bus
572caa11f7aSAlpana Kumari                 // so skip if it is empty
57386d89ed7SKrzysztof Grobelny                 if (sparePartNumber != nullptr && !sparePartNumber->empty())
574caa11f7aSAlpana Kumari                 {
57586d89ed7SKrzysztof Grobelny                     asyncResp->res.jsonValue["SparePartNumber"] =
57686d89ed7SKrzysztof Grobelny                         *sparePartNumber;
577caa11f7aSAlpana Kumari                 }
57886d89ed7SKrzysztof Grobelny 
57962d5e2e4SEd Tanous                 asyncResp->res.jsonValue["Name"] = chassisId;
58062d5e2e4SEd Tanous                 asyncResp->res.jsonValue["Id"] = chassisId;
5810256b694Szhanghch05 #ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
582002d39b4SEd Tanous                 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
583ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/Thermal",
584ef4c65b7SEd Tanous                                         chassisId);
5852474adfaSEd Tanous                 // Power object
5861476687dSEd Tanous                 asyncResp->res.jsonValue["Power"]["@odata.id"] =
587ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/Power",
588ef4c65b7SEd Tanous                                         chassisId);
5890256b694Szhanghch05 #endif
5902973963eSXiaochao Ma #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
5912973963eSXiaochao Ma                 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
592ef4c65b7SEd Tanous                     boost::urls::format(
593ef4c65b7SEd Tanous                         "/redfish/v1/Chassis/{}/ThermalSubsystem", chassisId);
59477b36437SChicago Duan                 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
595ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem",
596ef4c65b7SEd Tanous                                         chassisId);
5974ca3ec3cSAlbert Zhang                 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
598ef4c65b7SEd Tanous                     boost::urls::format(
599ef4c65b7SEd Tanous                         "/redfish/v1/Chassis/{}/EnvironmentMetrics", chassisId);
6002973963eSXiaochao Ma #endif
60195a3ecadSAnthony Wilson                 // SensorCollection
602002d39b4SEd Tanous                 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
603ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}/Sensors",
604ef4c65b7SEd Tanous                                         chassisId);
605002d39b4SEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
6061476687dSEd Tanous 
6071476687dSEd Tanous                 nlohmann::json::array_t computerSystems;
6081476687dSEd Tanous                 nlohmann::json::object_t system;
609002d39b4SEd Tanous                 system["@odata.id"] = "/redfish/v1/Systems/system";
610ad539545SPatrick Williams                 computerSystems.emplace_back(std::move(system));
611002d39b4SEd Tanous                 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
6121476687dSEd Tanous                     std::move(computerSystems);
6131476687dSEd Tanous 
6141476687dSEd Tanous                 nlohmann::json::array_t managedBy;
6151476687dSEd Tanous                 nlohmann::json::object_t manager;
616002d39b4SEd Tanous                 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
617ad539545SPatrick Williams                 managedBy.emplace_back(std::move(manager));
6187e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Links"]["ManagedBy"] =
6191476687dSEd Tanous                     std::move(managedBy);
620beeca0aeSGunnar Mills                 getChassisState(asyncResp);
6215e577bc1SWilly Tu                 getStorageLink(asyncResp, path);
62286d89ed7SKrzysztof Grobelny                 });
6232c37b4b0SSharad Yadav 
624308f70c7SWilly Tu             for (const auto& interface : interfaces2)
6252c37b4b0SSharad Yadav             {
626308f70c7SWilly Tu                 if (interface == "xyz.openbmc_project.Common.UUID")
6272c37b4b0SSharad Yadav                 {
628308f70c7SWilly Tu                     getChassisUUID(asyncResp, connectionName, path);
6292c37b4b0SSharad Yadav                 }
630cf7eba09SNan Zhou                 else if (interface ==
6310fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
6322c37b4b0SSharad Yadav                 {
633002d39b4SEd Tanous                     getChassisLocationCode(asyncResp, connectionName, path);
6342c37b4b0SSharad Yadav                 }
6352c37b4b0SSharad Yadav             }
6362c37b4b0SSharad Yadav 
637daf36e2eSEd Tanous             return;
638daf36e2eSEd Tanous         }
639e0d918bcSEd Tanous 
640daf36e2eSEd Tanous         // Couldn't find an object with that name.  return an error
641d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
642e99073f5SGeorge Liu         });
643c181942fSQiang XU 
644c181942fSQiang XU     getPhysicalSecurityData(asyncResp);
645cf7eba09SNan Zhou }
6461c8fba97SJames Feist 
647cf7eba09SNan Zhou inline void
648cf7eba09SNan Zhou     handleChassisPatch(App& app, const crow::Request& req,
6497e860f15SJohn Edward Broadbent                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
650cf7eba09SNan Zhou                        const std::string& param)
651cf7eba09SNan Zhou {
6523ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
65345ca1b86SEd Tanous     {
65445ca1b86SEd Tanous         return;
65545ca1b86SEd Tanous     }
6569f8bfa7cSGunnar Mills     std::optional<bool> locationIndicatorActive;
6571c8fba97SJames Feist     std::optional<std::string> indicatorLed;
6581c8fba97SJames Feist 
6597e860f15SJohn Edward Broadbent     if (param.empty())
6601c8fba97SJames Feist     {
6611c8fba97SJames Feist         return;
6621c8fba97SJames Feist     }
6631c8fba97SJames Feist 
66415ed6780SWilly Tu     if (!json_util::readJsonPatch(
6657e860f15SJohn Edward Broadbent             req, asyncResp->res, "LocationIndicatorActive",
6667e860f15SJohn Edward Broadbent             locationIndicatorActive, "IndicatorLED", indicatorLed))
6671c8fba97SJames Feist     {
6681c8fba97SJames Feist         return;
6691c8fba97SJames Feist     }
6701c8fba97SJames Feist 
6719f8bfa7cSGunnar Mills     // TODO (Gunnar): Remove IndicatorLED after enough time has passed
6729f8bfa7cSGunnar Mills     if (!locationIndicatorActive && !indicatorLed)
6731c8fba97SJames Feist     {
6741c8fba97SJames Feist         return; // delete this when we support more patch properties
6751c8fba97SJames Feist     }
676d6aa0093SGunnar Mills     if (indicatorLed)
677d6aa0093SGunnar Mills     {
6787e860f15SJohn Edward Broadbent         asyncResp->res.addHeader(
6797e860f15SJohn Edward Broadbent             boost::beast::http::field::warning,
6800fda0f12SGeorge Liu             "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
681d6aa0093SGunnar Mills     }
6821c8fba97SJames Feist 
683e99073f5SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
6841c8fba97SJames Feist         "xyz.openbmc_project.Inventory.Item.Board",
6851c8fba97SJames Feist         "xyz.openbmc_project.Inventory.Item.Chassis"};
6861c8fba97SJames Feist 
6877e860f15SJohn Edward Broadbent     const std::string& chassisId = param;
6881c8fba97SJames Feist 
689e99073f5SGeorge Liu     dbus::utility::getSubTree(
690e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
691cf7eba09SNan Zhou         [asyncResp, chassisId, locationIndicatorActive,
6925e7e2dc5SEd Tanous          indicatorLed](const boost::system::error_code& ec,
693b9d36b47SEd Tanous                        const dbus::utility::MapperGetSubTreeResponse& subtree) {
6941c8fba97SJames Feist         if (ec)
6951c8fba97SJames Feist         {
69662598e31SEd Tanous             BMCWEB_LOG_ERROR("DBUS response error {}", ec);
6971c8fba97SJames Feist             messages::internalError(asyncResp->res);
6981c8fba97SJames Feist             return;
6991c8fba97SJames Feist         }
7001c8fba97SJames Feist 
7011c8fba97SJames Feist         // Iterate over all retrieved ObjectPaths.
702cf7eba09SNan Zhou         for (const std::pair<
703cf7eba09SNan Zhou                  std::string,
704cf7eba09SNan Zhou                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
7051214b7e7SGunnar Mills                  object : subtree)
7061c8fba97SJames Feist         {
7071c8fba97SJames Feist             const std::string& path = object.first;
708cf7eba09SNan Zhou             const std::vector<std::pair<std::string, std::vector<std::string>>>&
7091214b7e7SGunnar Mills                 connectionNames = object.second;
7101c8fba97SJames Feist 
711997093ebSGeorge Liu             sdbusplus::message::object_path objPath(path);
712997093ebSGeorge Liu             if (objPath.filename() != chassisId)
7131c8fba97SJames Feist             {
7141c8fba97SJames Feist                 continue;
7151c8fba97SJames Feist             }
7161c8fba97SJames Feist 
71726f6976fSEd Tanous             if (connectionNames.empty())
7181c8fba97SJames Feist             {
71962598e31SEd Tanous                 BMCWEB_LOG_ERROR("Got 0 Connection names");
7201c8fba97SJames Feist                 continue;
7211c8fba97SJames Feist             }
7221c8fba97SJames Feist 
72323a21a1cSEd Tanous             const std::vector<std::string>& interfaces3 =
7241c8fba97SJames Feist                 connectionNames[0].second;
7251c8fba97SJames Feist 
7261c8fba97SJames Feist             const std::array<const char*, 2> hasIndicatorLed = {
7271c8fba97SJames Feist                 "xyz.openbmc_project.Inventory.Item.Panel",
7280fda0f12SGeorge Liu                 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
7291c8fba97SJames Feist             bool indicatorChassis = false;
7301c8fba97SJames Feist             for (const char* interface : hasIndicatorLed)
7311c8fba97SJames Feist             {
732*3544d2a7SEd Tanous                 if (std::ranges::find(interfaces3, interface) !=
733*3544d2a7SEd Tanous                     interfaces3.end())
7341c8fba97SJames Feist                 {
7351c8fba97SJames Feist                     indicatorChassis = true;
7361c8fba97SJames Feist                     break;
7371c8fba97SJames Feist                 }
7381c8fba97SJames Feist             }
7399f8bfa7cSGunnar Mills             if (locationIndicatorActive)
7409f8bfa7cSGunnar Mills             {
7419f8bfa7cSGunnar Mills                 if (indicatorChassis)
7429f8bfa7cSGunnar Mills                 {
743002d39b4SEd Tanous                     setLocationIndicatorActive(asyncResp,
744002d39b4SEd Tanous                                                *locationIndicatorActive);
7459f8bfa7cSGunnar Mills                 }
7469f8bfa7cSGunnar Mills                 else
7479f8bfa7cSGunnar Mills                 {
748002d39b4SEd Tanous                     messages::propertyUnknown(asyncResp->res,
749002d39b4SEd Tanous                                               "LocationIndicatorActive");
7509f8bfa7cSGunnar Mills                 }
7519f8bfa7cSGunnar Mills             }
7529f8bfa7cSGunnar Mills             if (indicatorLed)
7539f8bfa7cSGunnar Mills             {
7541c8fba97SJames Feist                 if (indicatorChassis)
7551c8fba97SJames Feist                 {
756f23b7296SEd Tanous                     setIndicatorLedState(asyncResp, *indicatorLed);
7571c8fba97SJames Feist                 }
7581c8fba97SJames Feist                 else
7591c8fba97SJames Feist                 {
760cf7eba09SNan Zhou                     messages::propertyUnknown(asyncResp->res, "IndicatorLED");
7611c8fba97SJames Feist                 }
7621c8fba97SJames Feist             }
7631c8fba97SJames Feist             return;
7641c8fba97SJames Feist         }
7651c8fba97SJames Feist 
766d8a5d5d8SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
767e99073f5SGeorge Liu         });
768cf7eba09SNan Zhou }
769cf7eba09SNan Zhou 
770cf7eba09SNan Zhou /**
771cf7eba09SNan Zhou  * Chassis override class for delivering Chassis Schema
772cf7eba09SNan Zhou  * Functions triggers appropriate requests on DBus
773cf7eba09SNan Zhou  */
774cf7eba09SNan Zhou inline void requestRoutesChassis(App& app)
775cf7eba09SNan Zhou {
776cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
777cf7eba09SNan Zhou         .privileges(redfish::privileges::getChassis)
778cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
779cf7eba09SNan Zhou             std::bind_front(handleChassisGet, std::ref(app)));
780cf7eba09SNan Zhou 
781cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
782cf7eba09SNan Zhou         .privileges(redfish::privileges::patchChassis)
783cf7eba09SNan Zhou         .methods(boost::beast::http::verb::patch)(
784cf7eba09SNan Zhou             std::bind_front(handleChassisPatch, std::ref(app)));
7851c8fba97SJames Feist }
786dd99e04bSP.K. Lee 
787fc903b3dSAndrew Geissler /**
788fc903b3dSAndrew Geissler  * Handle error responses from d-bus for chassis power cycles
789fc903b3dSAndrew Geissler  */
790fc903b3dSAndrew Geissler inline void handleChassisPowerCycleError(const boost::system::error_code& ec,
791fc903b3dSAndrew Geissler                                          const sdbusplus::message_t& eMsg,
792fc903b3dSAndrew Geissler                                          crow::Response& res)
793fc903b3dSAndrew Geissler {
794fc903b3dSAndrew Geissler     if (eMsg.get_error() == nullptr)
795fc903b3dSAndrew Geissler     {
79662598e31SEd Tanous         BMCWEB_LOG_ERROR("D-Bus response error: {}", ec);
797fc903b3dSAndrew Geissler         messages::internalError(res);
798fc903b3dSAndrew Geissler         return;
799fc903b3dSAndrew Geissler     }
800fc903b3dSAndrew Geissler     std::string_view errorMessage = eMsg.get_error()->name;
801fc903b3dSAndrew Geissler 
802fc903b3dSAndrew Geissler     // If operation failed due to BMC not being in Ready state, tell
803fc903b3dSAndrew Geissler     // user to retry in a bit
804fc903b3dSAndrew Geissler     if (errorMessage ==
805fc903b3dSAndrew Geissler         std::string_view("xyz.openbmc_project.State.Chassis.Error.BMCNotReady"))
806fc903b3dSAndrew Geissler     {
80762598e31SEd Tanous         BMCWEB_LOG_DEBUG("BMC not ready, operation not allowed right now");
808fc903b3dSAndrew Geissler         messages::serviceTemporarilyUnavailable(res, "10");
809fc903b3dSAndrew Geissler         return;
810fc903b3dSAndrew Geissler     }
811fc903b3dSAndrew Geissler 
81262598e31SEd Tanous     BMCWEB_LOG_ERROR("Chassis Power Cycle fail {} sdbusplus:{}", ec,
81362598e31SEd Tanous                      errorMessage);
814fc903b3dSAndrew Geissler     messages::internalError(res);
815fc903b3dSAndrew Geissler }
816fc903b3dSAndrew Geissler 
8178d1b46d7Szhanghch05 inline void
8188d1b46d7Szhanghch05     doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
819dd99e04bSP.K. Lee {
8207a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
821c3b3c92aSVijay Khemka         "xyz.openbmc_project.State.Chassis"};
822c3b3c92aSVijay Khemka 
823c3b3c92aSVijay Khemka     // Use mapper to get subtree paths.
8247a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
8257a1dbc48SGeorge Liu         "/", 0, interfaces,
826b9d36b47SEd Tanous         [asyncResp](
8277a1dbc48SGeorge Liu             const boost::system::error_code& ec,
828b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
829c3b3c92aSVijay Khemka         if (ec)
830c3b3c92aSVijay Khemka         {
83162598e31SEd Tanous             BMCWEB_LOG_ERROR("[mapper] Bad D-Bus request error: {}", ec);
832c3b3c92aSVijay Khemka             messages::internalError(asyncResp->res);
833c3b3c92aSVijay Khemka             return;
834c3b3c92aSVijay Khemka         }
835c3b3c92aSVijay Khemka 
836dd99e04bSP.K. Lee         const char* processName = "xyz.openbmc_project.State.Chassis";
837dd99e04bSP.K. Lee         const char* interfaceName = "xyz.openbmc_project.State.Chassis";
838dd99e04bSP.K. Lee         const char* destProperty = "RequestedPowerTransition";
839dd99e04bSP.K. Lee         const std::string propertyValue =
840dd99e04bSP.K. Lee             "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
841002d39b4SEd Tanous         std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
842c3b3c92aSVijay Khemka 
843c3b3c92aSVijay Khemka         /* Look for system reset chassis path */
844*3544d2a7SEd Tanous         if ((std::ranges::find(chassisList, objectPath)) == chassisList.end())
845c3b3c92aSVijay Khemka         {
846c3b3c92aSVijay Khemka             /* We prefer to reset the full chassis_system, but if it doesn't
847c3b3c92aSVijay Khemka              * exist on some platforms, fall back to a host-only power reset
848c3b3c92aSVijay Khemka              */
849c3b3c92aSVijay Khemka             objectPath = "/xyz/openbmc_project/state/chassis0";
850c3b3c92aSVijay Khemka         }
851dd99e04bSP.K. Lee 
8529ae226faSGeorge Liu         sdbusplus::asio::setProperty(
8539ae226faSGeorge Liu             *crow::connections::systemBus, processName, objectPath,
8549ae226faSGeorge Liu             interfaceName, destProperty, propertyValue,
855fc903b3dSAndrew Geissler             [asyncResp](const boost::system::error_code& ec2,
856fc903b3dSAndrew Geissler                         sdbusplus::message_t& sdbusErrMsg) {
857dd99e04bSP.K. Lee             // Use "Set" method to set the property value.
8588a592810SEd Tanous             if (ec2)
859dd99e04bSP.K. Lee             {
860fc903b3dSAndrew Geissler                 handleChassisPowerCycleError(ec2, sdbusErrMsg, asyncResp->res);
861fc903b3dSAndrew Geissler 
862dd99e04bSP.K. Lee                 return;
863dd99e04bSP.K. Lee             }
864dd99e04bSP.K. Lee 
865dd99e04bSP.K. Lee             messages::success(asyncResp->res);
8669ae226faSGeorge Liu             });
8677a1dbc48SGeorge Liu         });
868dd99e04bSP.K. Lee }
869dd99e04bSP.K. Lee 
870cf7eba09SNan Zhou inline void handleChassisResetActionInfoPost(
871cf7eba09SNan Zhou     App& app, const crow::Request& req,
8727e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
873cf7eba09SNan Zhou     const std::string& /*chassisId*/)
874cf7eba09SNan Zhou {
8753ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
87645ca1b86SEd Tanous     {
87745ca1b86SEd Tanous         return;
87845ca1b86SEd Tanous     }
87962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Post Chassis Reset.");
880dd99e04bSP.K. Lee 
881dd99e04bSP.K. Lee     std::string resetType;
882dd99e04bSP.K. Lee 
883cf7eba09SNan Zhou     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
884dd99e04bSP.K. Lee     {
885dd99e04bSP.K. Lee         return;
886dd99e04bSP.K. Lee     }
887dd99e04bSP.K. Lee 
888dd99e04bSP.K. Lee     if (resetType != "PowerCycle")
889dd99e04bSP.K. Lee     {
89062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
891002d39b4SEd Tanous         messages::actionParameterNotSupported(asyncResp->res, resetType,
892002d39b4SEd Tanous                                               "ResetType");
893dd99e04bSP.K. Lee 
894dd99e04bSP.K. Lee         return;
895dd99e04bSP.K. Lee     }
896dd99e04bSP.K. Lee     doChassisPowerCycle(asyncResp);
897dd99e04bSP.K. Lee }
8981cb1a9e6SAppaRao Puli 
8991cb1a9e6SAppaRao Puli /**
900cf7eba09SNan Zhou  * ChassisResetAction class supports the POST method for the Reset
901cf7eba09SNan Zhou  * action.
902cf7eba09SNan Zhou  * Function handles POST method request.
903cf7eba09SNan Zhou  * Analyzes POST body before sending Reset request data to D-Bus.
9041cb1a9e6SAppaRao Puli  */
905cf7eba09SNan Zhou 
906cf7eba09SNan Zhou inline void requestRoutesChassisResetAction(App& app)
9071cb1a9e6SAppaRao Puli {
908cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
909cf7eba09SNan Zhou         .privileges(redfish::privileges::postChassis)
910cf7eba09SNan Zhou         .methods(boost::beast::http::verb::post)(
911cf7eba09SNan Zhou             std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
912cf7eba09SNan Zhou }
913cf7eba09SNan Zhou 
914cf7eba09SNan Zhou inline void handleChassisResetActionInfoGet(
915cf7eba09SNan Zhou     App& app, const crow::Request& req,
9167e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
917cf7eba09SNan Zhou     const std::string& chassisId)
918cf7eba09SNan Zhou {
9193ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9201cb1a9e6SAppaRao Puli     {
92145ca1b86SEd Tanous         return;
92245ca1b86SEd Tanous     }
923cf7eba09SNan Zhou     asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
924ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
925ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
9261476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Reset Action Info";
9271476687dSEd Tanous 
9281476687dSEd Tanous     asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
9295b9e95a1SNan Zhou     nlohmann::json::array_t parameters;
9305b9e95a1SNan Zhou     nlohmann::json::object_t parameter;
9315b9e95a1SNan Zhou     parameter["Name"] = "ResetType";
9325b9e95a1SNan Zhou     parameter["Required"] = true;
9335b9e95a1SNan Zhou     parameter["DataType"] = "String";
9341476687dSEd Tanous     nlohmann::json::array_t allowed;
935ad539545SPatrick Williams     allowed.emplace_back("PowerCycle");
9365b9e95a1SNan Zhou     parameter["AllowableValues"] = std::move(allowed);
937ad539545SPatrick Williams     parameters.emplace_back(std::move(parameter));
9385b9e95a1SNan Zhou 
9391476687dSEd Tanous     asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
940cf7eba09SNan Zhou }
941cf7eba09SNan Zhou 
942cf7eba09SNan Zhou /**
943cf7eba09SNan Zhou  * ChassisResetActionInfo derived class for delivering Chassis
944cf7eba09SNan Zhou  * ResetType AllowableValues using ResetInfo schema.
945cf7eba09SNan Zhou  */
946cf7eba09SNan Zhou inline void requestRoutesChassisResetActionInfo(App& app)
947cf7eba09SNan Zhou {
948cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
949cf7eba09SNan Zhou         .privileges(redfish::privileges::getActionInfo)
950cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
951cf7eba09SNan Zhou             std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
9521cb1a9e6SAppaRao Puli }
9531cb1a9e6SAppaRao Puli 
954e37f8451SRapkiewicz, Pawel } // namespace redfish
955