xref: /openbmc/bmcweb/features/redfish/lib/chassis.hpp (revision a56174963e1d8bd9fc90cdeb3aa7f1b7f2e0dca4)
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>
377a1dbc48SGeorge Liu #include <string_view>
387a1dbc48SGeorge Liu 
391abe55efSEd Tanous namespace redfish
401abe55efSEd Tanous {
41e37f8451SRapkiewicz, Pawel 
42e37f8451SRapkiewicz, Pawel /**
435e577bc1SWilly Tu  * @brief Retrieves resources over dbus to link to the chassis
445e577bc1SWilly Tu  *
455e577bc1SWilly Tu  * @param[in] asyncResp  - Shared pointer for completing asynchronous
465e577bc1SWilly Tu  * calls
475e577bc1SWilly Tu  * @param[in] path       - Chassis dbus path to look for the storage.
485e577bc1SWilly Tu  *
495e577bc1SWilly Tu  * Calls the Association endpoints on the path + "/storage" and add the link of
505e577bc1SWilly Tu  * json["Links"]["Storage@odata.count"] =
515e577bc1SWilly Tu  *    {"@odata.id", "/redfish/v1/Storage/" + resourceId}
525e577bc1SWilly Tu  *
535e577bc1SWilly Tu  * @return None.
545e577bc1SWilly Tu  */
555e577bc1SWilly Tu inline void getStorageLink(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
565e577bc1SWilly Tu                            const sdbusplus::message::object_path& path)
575e577bc1SWilly Tu {
585e577bc1SWilly Tu     sdbusplus::asio::getProperty<std::vector<std::string>>(
595e577bc1SWilly Tu         *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
605e577bc1SWilly Tu         (path / "storage").str, "xyz.openbmc_project.Association", "endpoints",
61d4b054c1SWilly Tu         [asyncResp](const boost::system::error_code& ec,
625e577bc1SWilly Tu                     const std::vector<std::string>& storageList) {
635e577bc1SWilly Tu         if (ec)
645e577bc1SWilly Tu         {
655e577bc1SWilly Tu             BMCWEB_LOG_DEBUG << "getStorageLink got DBUS response error";
665e577bc1SWilly Tu             return;
675e577bc1SWilly Tu         }
685e577bc1SWilly Tu 
695e577bc1SWilly Tu         nlohmann::json::array_t storages;
705e577bc1SWilly Tu         for (const std::string& storagePath : storageList)
715e577bc1SWilly Tu         {
725e577bc1SWilly Tu             std::string id =
735e577bc1SWilly Tu                 sdbusplus::message::object_path(storagePath).filename();
745e577bc1SWilly Tu             if (id.empty())
755e577bc1SWilly Tu             {
765e577bc1SWilly Tu                 continue;
775e577bc1SWilly Tu             }
785e577bc1SWilly Tu 
795e577bc1SWilly Tu             nlohmann::json::object_t storage;
805e577bc1SWilly Tu             storage["@odata.id"] = boost::urls::format(
815e577bc1SWilly Tu                 "/redfish/v1/Systems/system/Storage/{}", id);
825e577bc1SWilly Tu             storages.emplace_back(std::move(storage));
835e577bc1SWilly Tu         }
845e577bc1SWilly Tu         asyncResp->res.jsonValue["Links"]["Storage@odata.count"] =
855e577bc1SWilly Tu             storages.size();
865e577bc1SWilly Tu         asyncResp->res.jsonValue["Links"]["Storage"] = std::move(storages);
875e577bc1SWilly Tu         });
885e577bc1SWilly Tu }
895e577bc1SWilly Tu 
905e577bc1SWilly Tu /**
91beeca0aeSGunnar Mills  * @brief Retrieves chassis state properties over dbus
92beeca0aeSGunnar Mills  *
93ac106bf6SEd Tanous  * @param[in] asyncResp - Shared pointer for completing asynchronous calls.
94beeca0aeSGunnar Mills  *
95beeca0aeSGunnar Mills  * @return None.
96beeca0aeSGunnar Mills  */
97ac106bf6SEd Tanous inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
98beeca0aeSGunnar Mills {
991e1e598dSJonathan Doman     // crow::connections::systemBus->async_method_call(
1001e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
1011e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
1021e1e598dSJonathan Doman         "/xyz/openbmc_project/state/chassis0",
1031e1e598dSJonathan Doman         "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
104ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
1051e1e598dSJonathan Doman                                           const std::string& chassisState) {
106beeca0aeSGunnar Mills         if (ec)
107beeca0aeSGunnar Mills         {
108a6e5e0abSCarson Labrado             if (ec == boost::system::errc::host_unreachable)
109a6e5e0abSCarson Labrado             {
110a6e5e0abSCarson Labrado                 // Service not available, no error, just don't return
111a6e5e0abSCarson Labrado                 // chassis state info
112a6e5e0abSCarson Labrado                 BMCWEB_LOG_DEBUG << "Service not available " << ec;
113a6e5e0abSCarson Labrado                 return;
114a6e5e0abSCarson Labrado             }
115ac106bf6SEd Tanous             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
116ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
117beeca0aeSGunnar Mills             return;
118beeca0aeSGunnar Mills         }
119beeca0aeSGunnar Mills 
1201e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "Chassis state: " << chassisState;
121beeca0aeSGunnar Mills         // Verify Chassis State
122002d39b4SEd Tanous         if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
123beeca0aeSGunnar Mills         {
124ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
125ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
126beeca0aeSGunnar Mills         }
1271e1e598dSJonathan Doman         else if (chassisState ==
128beeca0aeSGunnar Mills                  "xyz.openbmc_project.State.Chassis.PowerState.Off")
129beeca0aeSGunnar Mills         {
130ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "Off";
131ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
132beeca0aeSGunnar Mills         }
1331e1e598dSJonathan Doman         });
134beeca0aeSGunnar Mills }
135beeca0aeSGunnar Mills 
136ac106bf6SEd Tanous inline void getIntrusionByService(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
137c181942fSQiang XU                                   const std::string& service,
138c181942fSQiang XU                                   const std::string& objPath)
139c181942fSQiang XU {
140c181942fSQiang XU     BMCWEB_LOG_DEBUG << "Get intrusion status by service \n";
141c181942fSQiang XU 
1421e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
1431e1e598dSJonathan Doman         *crow::connections::systemBus, service, objPath,
1441e1e598dSJonathan Doman         "xyz.openbmc_project.Chassis.Intrusion", "Status",
145ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
1461e1e598dSJonathan Doman                                           const std::string& value) {
147c181942fSQiang XU         if (ec)
148c181942fSQiang XU         {
1494e0453b1SGunnar Mills             // do not add err msg in redfish response, because this is not
150c181942fSQiang XU             //     mandatory property
151c181942fSQiang XU             BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n";
152c181942fSQiang XU             return;
153c181942fSQiang XU         }
154c181942fSQiang XU 
155ac106bf6SEd Tanous         asyncResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] =
156ac106bf6SEd Tanous             1;
157ac106bf6SEd Tanous         asyncResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
1581e1e598dSJonathan Doman         });
159c181942fSQiang XU }
160c181942fSQiang XU 
161c181942fSQiang XU /**
162c181942fSQiang XU  * Retrieves physical security properties over dbus
163c181942fSQiang XU  */
164ac106bf6SEd Tanous inline void
165ac106bf6SEd Tanous     getPhysicalSecurityData(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
166c181942fSQiang XU {
167e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
168e99073f5SGeorge Liu         "xyz.openbmc_project.Chassis.Intrusion"};
169e99073f5SGeorge Liu     dbus::utility::getSubTree(
170e99073f5SGeorge Liu         "/xyz/openbmc_project/Intrusion", 1, interfaces,
171ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)}](
172e99073f5SGeorge Liu             const boost::system::error_code& ec,
173b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
174c181942fSQiang XU         if (ec)
175c181942fSQiang XU         {
1764e0453b1SGunnar Mills             // do not add err msg in redfish response, because this is not
177c181942fSQiang XU             //     mandatory property
178002d39b4SEd Tanous             BMCWEB_LOG_INFO << "DBUS error: no matched iface " << ec << "\n";
179c181942fSQiang XU             return;
180c181942fSQiang XU         }
181c181942fSQiang XU         // Iterate over all retrieved ObjectPaths.
182c181942fSQiang XU         for (const auto& object : subtree)
183c181942fSQiang XU         {
184840a9ffcSPatrick Williams             if (!object.second.empty())
185c181942fSQiang XU             {
186840a9ffcSPatrick Williams                 const auto service = object.second.front();
187ac106bf6SEd Tanous                 getIntrusionByService(asyncResp, service.first, object.first);
188c181942fSQiang XU                 return;
189c181942fSQiang XU             }
190c181942fSQiang XU         }
191e99073f5SGeorge Liu         });
192c181942fSQiang XU }
193c181942fSQiang XU 
194cf7eba09SNan Zhou inline void handleChassisCollectionGet(
195cf7eba09SNan Zhou     App& app, const crow::Request& req,
196cf7eba09SNan Zhou     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1971abe55efSEd Tanous {
1983ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19945ca1b86SEd Tanous     {
20045ca1b86SEd Tanous         return;
20145ca1b86SEd Tanous     }
2028d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
2038d1b46d7Szhanghch05         "#ChassisCollection.ChassisCollection";
2048d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
2058d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Chassis Collection";
206e37f8451SRapkiewicz, Pawel 
2077a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces{
2087a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Board",
2097a1dbc48SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Chassis"};
21002f6ff19SGunnar Mills     collection_util::getCollectionMembers(
2117a1dbc48SGeorge Liu         asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
212cf7eba09SNan Zhou }
213cf7eba09SNan Zhou 
214*a5617496SJie Yang inline void getChassisContainedBy(
215*a5617496SJie Yang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216*a5617496SJie Yang     const std::string& chassisId, const boost::system::error_code& ec,
217*a5617496SJie Yang     const dbus::utility::MapperEndPoints& upstreamChassisPaths)
218*a5617496SJie Yang {
219*a5617496SJie Yang     if (ec)
220*a5617496SJie Yang     {
221*a5617496SJie Yang         if (ec.value() != EBADR)
222*a5617496SJie Yang         {
223*a5617496SJie Yang             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
224*a5617496SJie Yang             messages::internalError(asyncResp->res);
225*a5617496SJie Yang         }
226*a5617496SJie Yang         return;
227*a5617496SJie Yang     }
228*a5617496SJie Yang     if (upstreamChassisPaths.empty())
229*a5617496SJie Yang     {
230*a5617496SJie Yang         return;
231*a5617496SJie Yang     }
232*a5617496SJie Yang     if (upstreamChassisPaths.size() > 1)
233*a5617496SJie Yang     {
234*a5617496SJie Yang         BMCWEB_LOG_ERROR << chassisId << " is contained by mutliple chassis";
235*a5617496SJie Yang         messages::internalError(asyncResp->res);
236*a5617496SJie Yang         return;
237*a5617496SJie Yang     }
238*a5617496SJie Yang 
239*a5617496SJie Yang     sdbusplus::message::object_path upstreamChassisPath(
240*a5617496SJie Yang         upstreamChassisPaths[0]);
241*a5617496SJie Yang     std::string upstreamChassis = upstreamChassisPath.filename();
242*a5617496SJie Yang     if (upstreamChassis.empty())
243*a5617496SJie Yang     {
244*a5617496SJie Yang         BMCWEB_LOG_WARNING << "Malformed upstream Chassis path "
245*a5617496SJie Yang                            << upstreamChassisPath.str << " on " << chassisId;
246*a5617496SJie Yang         return;
247*a5617496SJie Yang     }
248*a5617496SJie Yang 
249*a5617496SJie Yang     asyncResp->res.jsonValue["Links"]["ContainedBy"]["@odata.id"] =
250*a5617496SJie Yang         boost::urls::format("/redfish/v1/Chassis/{}", upstreamChassis);
251*a5617496SJie Yang }
252*a5617496SJie Yang 
253*a5617496SJie Yang inline void getChassisContains(
254*a5617496SJie Yang     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
255*a5617496SJie Yang     const std::string& chassisId, const boost::system::error_code& ec,
256*a5617496SJie Yang     const dbus::utility::MapperEndPoints& downstreamChassisPaths)
257*a5617496SJie Yang {
258*a5617496SJie Yang     if (ec)
259*a5617496SJie Yang     {
260*a5617496SJie Yang         if (ec.value() != EBADR)
261*a5617496SJie Yang         {
262*a5617496SJie Yang             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
263*a5617496SJie Yang             messages::internalError(asyncResp->res);
264*a5617496SJie Yang         }
265*a5617496SJie Yang         return;
266*a5617496SJie Yang     }
267*a5617496SJie Yang     if (downstreamChassisPaths.empty())
268*a5617496SJie Yang     {
269*a5617496SJie Yang         return;
270*a5617496SJie Yang     }
271*a5617496SJie Yang     nlohmann::json& jValue = asyncResp->res.jsonValue["Links"]["Contains"];
272*a5617496SJie Yang     if (!jValue.is_array())
273*a5617496SJie Yang     {
274*a5617496SJie Yang         // Create the array if it was empty
275*a5617496SJie Yang         jValue = nlohmann::json::array();
276*a5617496SJie Yang     }
277*a5617496SJie Yang     for (const auto& p : downstreamChassisPaths)
278*a5617496SJie Yang     {
279*a5617496SJie Yang         sdbusplus::message::object_path downstreamChassisPath(p);
280*a5617496SJie Yang         std::string downstreamChassis = downstreamChassisPath.filename();
281*a5617496SJie Yang         if (downstreamChassis.empty())
282*a5617496SJie Yang         {
283*a5617496SJie Yang             BMCWEB_LOG_WARNING << "Malformed downstream Chassis path "
284*a5617496SJie Yang                                << downstreamChassisPath.str << " on "
285*a5617496SJie Yang                                << chassisId;
286*a5617496SJie Yang             continue;
287*a5617496SJie Yang         }
288*a5617496SJie Yang         nlohmann::json link;
289*a5617496SJie Yang         link["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
290*a5617496SJie Yang                                                 downstreamChassis);
291*a5617496SJie Yang         jValue.push_back(std::move(link));
292*a5617496SJie Yang     }
293*a5617496SJie Yang     asyncResp->res.jsonValue["Links"]["Contains@odata.count"] = jValue.size();
294*a5617496SJie Yang }
295*a5617496SJie Yang 
296*a5617496SJie Yang inline void
297*a5617496SJie Yang     getChassisConnectivity(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
298*a5617496SJie Yang                            const std::string& chassisId,
299*a5617496SJie Yang                            const std::string& chassisPath)
300*a5617496SJie Yang {
301*a5617496SJie Yang     BMCWEB_LOG_DEBUG << "Get chassis connectivity";
302*a5617496SJie Yang 
303*a5617496SJie Yang     dbus::utility::getAssociationEndPoints(
304*a5617496SJie Yang         chassisPath + "/contained_by",
305*a5617496SJie Yang         std::bind_front(getChassisContainedBy, asyncResp, chassisId));
306*a5617496SJie Yang 
307*a5617496SJie Yang     dbus::utility::getAssociationEndPoints(
308*a5617496SJie Yang         chassisPath + "/containing",
309*a5617496SJie Yang         std::bind_front(getChassisContains, asyncResp, chassisId));
310*a5617496SJie Yang }
311*a5617496SJie 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         {
33651dab2a7SAndrew Geissler             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         {
35751dab2a7SAndrew Geissler             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         {
38551dab2a7SAndrew Geissler             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 
405*a5617496SJie Yang             getChassisConnectivity(asyncResp, chassisId, path);
406*a5617496SJie 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             {
4271c8fba97SJames Feist                 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                         {
487523d4868SLogananth Sundararaj                             BMCWEB_LOG_ERROR
488523d4868SLogananth Sundararaj                                 << "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                         {
505523d4868SLogananth Sundararaj                             BMCWEB_LOG_ERROR
506523d4868SLogananth Sundararaj                                 << "DBus response error for HotPluggable: "
507523d4868SLogananth Sundararaj                                 << 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             {
518002d39b4SEd Tanous                 if (std::find(interfaces2.begin(), interfaces2.end(),
51923a21a1cSEd Tanous                               interface) != 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         {
69651dab2a7SAndrew Geissler             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             {
7191c8fba97SJames Feist                 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             {
732002d39b4SEd Tanous                 if (std::find(interfaces3.begin(), interfaces3.end(),
73323a21a1cSEd Tanous                               interface) != 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     {
796fc903b3dSAndrew Geissler         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     {
807fc903b3dSAndrew Geissler         BMCWEB_LOG_DEBUG << "BMC not ready, operation not allowed right now";
808fc903b3dSAndrew Geissler         messages::serviceTemporarilyUnavailable(res, "10");
809fc903b3dSAndrew Geissler         return;
810fc903b3dSAndrew Geissler     }
811fc903b3dSAndrew Geissler 
812fc903b3dSAndrew Geissler     BMCWEB_LOG_ERROR << "Chassis Power Cycle fail " << ec
813fc903b3dSAndrew Geissler                      << " sdbusplus:" << 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         {
83151dab2a7SAndrew Geissler             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 */
844002d39b4SEd Tanous         if ((std::find(chassisList.begin(), chassisList.end(), objectPath)) ==
845002d39b4SEd Tanous             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     }
880dd99e04bSP.K. Lee     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     {
891dd99e04bSP.K. Lee         BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
892dd99e04bSP.K. Lee                          << resetType;
893002d39b4SEd Tanous         messages::actionParameterNotSupported(asyncResp->res, resetType,
894002d39b4SEd Tanous                                               "ResetType");
895dd99e04bSP.K. Lee 
896dd99e04bSP.K. Lee         return;
897dd99e04bSP.K. Lee     }
898dd99e04bSP.K. Lee     doChassisPowerCycle(asyncResp);
899dd99e04bSP.K. Lee }
9001cb1a9e6SAppaRao Puli 
9011cb1a9e6SAppaRao Puli /**
902cf7eba09SNan Zhou  * ChassisResetAction class supports the POST method for the Reset
903cf7eba09SNan Zhou  * action.
904cf7eba09SNan Zhou  * Function handles POST method request.
905cf7eba09SNan Zhou  * Analyzes POST body before sending Reset request data to D-Bus.
9061cb1a9e6SAppaRao Puli  */
907cf7eba09SNan Zhou 
908cf7eba09SNan Zhou inline void requestRoutesChassisResetAction(App& app)
9091cb1a9e6SAppaRao Puli {
910cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
911cf7eba09SNan Zhou         .privileges(redfish::privileges::postChassis)
912cf7eba09SNan Zhou         .methods(boost::beast::http::verb::post)(
913cf7eba09SNan Zhou             std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
914cf7eba09SNan Zhou }
915cf7eba09SNan Zhou 
916cf7eba09SNan Zhou inline void handleChassisResetActionInfoGet(
917cf7eba09SNan Zhou     App& app, const crow::Request& req,
9187e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
919cf7eba09SNan Zhou     const std::string& chassisId)
920cf7eba09SNan Zhou {
9213ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
9221cb1a9e6SAppaRao Puli     {
92345ca1b86SEd Tanous         return;
92445ca1b86SEd Tanous     }
925cf7eba09SNan Zhou     asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
926ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
927ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
9281476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "Reset Action Info";
9291476687dSEd Tanous 
9301476687dSEd Tanous     asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
9315b9e95a1SNan Zhou     nlohmann::json::array_t parameters;
9325b9e95a1SNan Zhou     nlohmann::json::object_t parameter;
9335b9e95a1SNan Zhou     parameter["Name"] = "ResetType";
9345b9e95a1SNan Zhou     parameter["Required"] = true;
9355b9e95a1SNan Zhou     parameter["DataType"] = "String";
9361476687dSEd Tanous     nlohmann::json::array_t allowed;
937ad539545SPatrick Williams     allowed.emplace_back("PowerCycle");
9385b9e95a1SNan Zhou     parameter["AllowableValues"] = std::move(allowed);
939ad539545SPatrick Williams     parameters.emplace_back(std::move(parameter));
9405b9e95a1SNan Zhou 
9411476687dSEd Tanous     asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
942cf7eba09SNan Zhou }
943cf7eba09SNan Zhou 
944cf7eba09SNan Zhou /**
945cf7eba09SNan Zhou  * ChassisResetActionInfo derived class for delivering Chassis
946cf7eba09SNan Zhou  * ResetType AllowableValues using ResetInfo schema.
947cf7eba09SNan Zhou  */
948cf7eba09SNan Zhou inline void requestRoutesChassisResetActionInfo(App& app)
949cf7eba09SNan Zhou {
950cf7eba09SNan Zhou     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
951cf7eba09SNan Zhou         .privileges(redfish::privileges::getActionInfo)
952cf7eba09SNan Zhou         .methods(boost::beast::http::verb::get)(
953cf7eba09SNan Zhou             std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
9541cb1a9e6SAppaRao Puli }
9551cb1a9e6SAppaRao Puli 
956e37f8451SRapkiewicz, Pawel } // namespace redfish
957