blob: 06ce58392b020195b8e0fa38f07210e65da13b23 [file] [log] [blame]
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Willy Tu13451e32023-05-24 16:08:18 -070018#include "bmcweb_config.h"
19
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080021#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070022#include "health.hpp"
James Feist1c8fba92019-12-20 15:12:07 -080023#include "led.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
Chau Ly7164bc62023-10-15 14:55:30 +000025#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "registries/privilege_registry.hpp"
27#include "utils/collection.hpp"
28#include "utils/dbus_utils.hpp"
Nan Zhoucf7eba02022-07-21 23:53:20 +000029#include "utils/json_utils.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070030
George Liue99073f2022-12-09 11:06:16 +080031#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070032#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070033#include <sdbusplus/asio/property.hpp>
Andrew Geisslerfc903b32023-05-31 14:15:42 -040034#include <sdbusplus/message.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020035#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050036
George Liu7a1dbc42022-12-07 16:03:22 +080037#include <array>
Ed Tanous3544d2a2023-08-06 18:12:20 -070038#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080039#include <string_view>
40
Ed Tanous1abe55e2018-09-05 08:30:59 -070041namespace redfish
42{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010043
44/**
Willy Tu5e577bc2022-07-26 00:41:55 +000045 * @brief Retrieves resources over dbus to link to the chassis
46 *
47 * @param[in] asyncResp - Shared pointer for completing asynchronous
48 * calls
49 * @param[in] path - Chassis dbus path to look for the storage.
50 *
51 * Calls the Association endpoints on the path + "/storage" and add the link of
52 * json["Links"]["Storage@odata.count"] =
53 * {"@odata.id", "/redfish/v1/Storage/" + resourceId}
54 *
55 * @return None.
56 */
57inline void getStorageLink(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
58 const sdbusplus::message::object_path& path)
59{
60 sdbusplus::asio::getProperty<std::vector<std::string>>(
61 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
62 (path / "storage").str, "xyz.openbmc_project.Association", "endpoints",
Willy Tud4b054c2023-06-12 15:18:45 -070063 [asyncResp](const boost::system::error_code& ec,
Willy Tu5e577bc2022-07-26 00:41:55 +000064 const std::vector<std::string>& storageList) {
65 if (ec)
66 {
Ed Tanous62598e32023-07-17 17:06:25 -070067 BMCWEB_LOG_DEBUG("getStorageLink got DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +000068 return;
69 }
70
71 nlohmann::json::array_t storages;
72 for (const std::string& storagePath : storageList)
73 {
74 std::string id =
75 sdbusplus::message::object_path(storagePath).filename();
76 if (id.empty())
77 {
78 continue;
79 }
80
81 nlohmann::json::object_t storage;
82 storage["@odata.id"] = boost::urls::format(
83 "/redfish/v1/Systems/system/Storage/{}", id);
84 storages.emplace_back(std::move(storage));
85 }
86 asyncResp->res.jsonValue["Links"]["Storage@odata.count"] =
87 storages.size();
88 asyncResp->res.jsonValue["Links"]["Storage"] = std::move(storages);
Patrick Williams5a39f772023-10-20 11:20:21 -050089 });
Willy Tu5e577bc2022-07-26 00:41:55 +000090}
91
92/**
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060093 * @brief Retrieves chassis state properties over dbus
94 *
Ed Tanousac106bf2023-06-07 09:24:59 -070095 * @param[in] asyncResp - Shared pointer for completing asynchronous calls.
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060096 *
97 * @return None.
98 */
Ed Tanousac106bf2023-06-07 09:24:59 -070099inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600100{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700101 // crow::connections::systemBus->async_method_call(
102 sdbusplus::asio::getProperty<std::string>(
103 *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
104 "/xyz/openbmc_project/state/chassis0",
105 "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
Ed Tanousac106bf2023-06-07 09:24:59 -0700106 [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
107 const std::string& chassisState) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700108 if (ec)
109 {
110 if (ec == boost::system::errc::host_unreachable)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600111 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700112 // Service not available, no error, just don't return
113 // chassis state info
Ed Tanous62598e32023-07-17 17:06:25 -0700114 BMCWEB_LOG_DEBUG("Service not available {}", ec);
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600115 return;
116 }
Ed Tanous62598e32023-07-17 17:06:25 -0700117 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700118 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700119 return;
120 }
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600121
Ed Tanous62598e32023-07-17 17:06:25 -0700122 BMCWEB_LOG_DEBUG("Chassis state: {}", chassisState);
Ed Tanous002d39b2022-05-31 08:59:27 -0700123 // Verify Chassis State
124 if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
125 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700126 asyncResp->res.jsonValue["PowerState"] = "On";
127 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Ed Tanous002d39b2022-05-31 08:59:27 -0700128 }
129 else if (chassisState ==
130 "xyz.openbmc_project.State.Chassis.PowerState.Off")
131 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700132 asyncResp->res.jsonValue["PowerState"] = "Off";
133 asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
Ed Tanous002d39b2022-05-31 08:59:27 -0700134 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500135 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600136}
137
Qiang XUc1819422019-02-27 13:51:32 +0800138/**
139 * Retrieves physical security properties over dbus
140 */
Chau Ly7164bc62023-10-15 14:55:30 +0000141inline void handlePhysicalSecurityGetSubTree(
142 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
143 const boost::system::error_code& ec,
144 const dbus::utility::MapperGetSubTreeResponse& subtree)
Qiang XUc1819422019-02-27 13:51:32 +0800145{
Chau Ly7164bc62023-10-15 14:55:30 +0000146 if (ec)
147 {
148 // do not add err msg in redfish response, because this is not
149 // mandatory property
150 BMCWEB_LOG_INFO("DBUS error: no matched iface {}", ec);
151 return;
152 }
153 // Iterate over all retrieved ObjectPaths.
154 for (const auto& object : subtree)
155 {
156 if (!object.second.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -0700157 {
Chau Ly7164bc62023-10-15 14:55:30 +0000158 const auto service = object.second.front();
159
160 BMCWEB_LOG_DEBUG("Get intrusion status by service ");
161
162 sdbusplus::asio::getProperty<std::string>(
163 *crow::connections::systemBus, service.first, object.first,
164 "xyz.openbmc_project.Chassis.Intrusion", "Status",
165 [asyncResp](const boost::system::error_code& ec1,
166 const std::string& value) {
167 if (ec1)
168 {
169 // do not add err msg in redfish response, because this is
170 // not
171 // mandatory property
172 BMCWEB_LOG_ERROR("DBUS response error {}", ec1);
173 return;
174 }
175 asyncResp->res
176 .jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] = 1;
177 asyncResp->res
178 .jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
179 });
180
Ed Tanous002d39b2022-05-31 08:59:27 -0700181 return;
182 }
Chau Ly7164bc62023-10-15 14:55:30 +0000183 }
Qiang XUc1819422019-02-27 13:51:32 +0800184}
185
Nan Zhoucf7eba02022-07-21 23:53:20 +0000186inline void handleChassisCollectionGet(
187 App& app, const crow::Request& req,
188 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
189{
190 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
191 {
192 return;
193 }
194 asyncResp->res.jsonValue["@odata.type"] =
195 "#ChassisCollection.ChassisCollection";
196 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
197 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
198
George Liu7a1dbc42022-12-07 16:03:22 +0800199 constexpr std::array<std::string_view, 2> interfaces{
200 "xyz.openbmc_project.Inventory.Item.Board",
201 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000202 collection_util::getCollectionMembers(
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500203 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces,
204 "/xyz/openbmc_project/inventory");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000205}
206
Jie Yanga5617492021-06-29 12:59:14 -0700207inline void getChassisContainedBy(
208 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
209 const std::string& chassisId, const boost::system::error_code& ec,
210 const dbus::utility::MapperEndPoints& upstreamChassisPaths)
211{
212 if (ec)
213 {
214 if (ec.value() != EBADR)
215 {
Ed Tanous62598e32023-07-17 17:06:25 -0700216 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Jie Yanga5617492021-06-29 12:59:14 -0700217 messages::internalError(asyncResp->res);
218 }
219 return;
220 }
221 if (upstreamChassisPaths.empty())
222 {
223 return;
224 }
225 if (upstreamChassisPaths.size() > 1)
226 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800227 BMCWEB_LOG_ERROR("{} is contained by multiple chassis", chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700228 messages::internalError(asyncResp->res);
229 return;
230 }
231
232 sdbusplus::message::object_path upstreamChassisPath(
233 upstreamChassisPaths[0]);
234 std::string upstreamChassis = upstreamChassisPath.filename();
235 if (upstreamChassis.empty())
236 {
Ed Tanous62598e32023-07-17 17:06:25 -0700237 BMCWEB_LOG_WARNING("Malformed upstream Chassis path {} on {}",
238 upstreamChassisPath.str, chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700239 return;
240 }
241
242 asyncResp->res.jsonValue["Links"]["ContainedBy"]["@odata.id"] =
243 boost::urls::format("/redfish/v1/Chassis/{}", upstreamChassis);
244}
245
246inline void getChassisContains(
247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
248 const std::string& chassisId, const boost::system::error_code& ec,
249 const dbus::utility::MapperEndPoints& downstreamChassisPaths)
250{
251 if (ec)
252 {
253 if (ec.value() != EBADR)
254 {
Ed Tanous62598e32023-07-17 17:06:25 -0700255 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Jie Yanga5617492021-06-29 12:59:14 -0700256 messages::internalError(asyncResp->res);
257 }
258 return;
259 }
260 if (downstreamChassisPaths.empty())
261 {
262 return;
263 }
264 nlohmann::json& jValue = asyncResp->res.jsonValue["Links"]["Contains"];
265 if (!jValue.is_array())
266 {
267 // Create the array if it was empty
268 jValue = nlohmann::json::array();
269 }
270 for (const auto& p : downstreamChassisPaths)
271 {
272 sdbusplus::message::object_path downstreamChassisPath(p);
273 std::string downstreamChassis = downstreamChassisPath.filename();
274 if (downstreamChassis.empty())
275 {
Ed Tanous62598e32023-07-17 17:06:25 -0700276 BMCWEB_LOG_WARNING("Malformed downstream Chassis path {} on {}",
277 downstreamChassisPath.str, chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700278 continue;
279 }
280 nlohmann::json link;
281 link["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
282 downstreamChassis);
283 jValue.push_back(std::move(link));
284 }
285 asyncResp->res.jsonValue["Links"]["Contains@odata.count"] = jValue.size();
286}
287
288inline void
289 getChassisConnectivity(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
290 const std::string& chassisId,
291 const std::string& chassisPath)
292{
Ed Tanous62598e32023-07-17 17:06:25 -0700293 BMCWEB_LOG_DEBUG("Get chassis connectivity");
Jie Yanga5617492021-06-29 12:59:14 -0700294
295 dbus::utility::getAssociationEndPoints(
296 chassisPath + "/contained_by",
297 std::bind_front(getChassisContainedBy, asyncResp, chassisId));
298
299 dbus::utility::getAssociationEndPoints(
300 chassisPath + "/containing",
301 std::bind_front(getChassisContains, asyncResp, chassisId));
302}
303
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100304/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100305 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700306 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100307 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700308inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700309{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700310 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700311 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700312 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000313 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700314}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100315
Willy Tu308f70c2021-09-28 20:24:52 -0700316inline void
317 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
318 const std::string& connectionName,
319 const std::string& path)
320{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700321 sdbusplus::asio::getProperty<std::string>(
322 *crow::connections::systemBus, connectionName, path,
323 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800324 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700325 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700326 if (ec)
327 {
Ed Tanous62598e32023-07-17 17:06:25 -0700328 BMCWEB_LOG_ERROR("DBUS response error for Location");
Ed Tanous002d39b2022-05-31 08:59:27 -0700329 messages::internalError(asyncResp->res);
330 return;
331 }
Willy Tu308f70c2021-09-28 20:24:52 -0700332
Ed Tanous002d39b2022-05-31 08:59:27 -0700333 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
334 property;
Patrick Williams5a39f772023-10-20 11:20:21 -0500335 });
Willy Tu308f70c2021-09-28 20:24:52 -0700336}
337
338inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
339 const std::string& connectionName,
340 const std::string& path)
341{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700342 sdbusplus::asio::getProperty<std::string>(
343 *crow::connections::systemBus, connectionName, path,
344 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800345 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700346 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 if (ec)
348 {
Ed Tanous62598e32023-07-17 17:06:25 -0700349 BMCWEB_LOG_ERROR("DBUS response error for UUID");
Ed Tanous002d39b2022-05-31 08:59:27 -0700350 messages::internalError(asyncResp->res);
351 return;
352 }
353 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Patrick Williams5a39f772023-10-20 11:20:21 -0500354 });
Willy Tu308f70c2021-09-28 20:24:52 -0700355}
356
Chau Ly7164bc62023-10-15 14:55:30 +0000357inline void handleDecoratorAssetProperties(
358 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
359 const std::string& chassisId, const std::string& path,
360 const dbus::utility::DBusPropertiesMap& propertiesList)
361{
362 const std::string* partNumber = nullptr;
363 const std::string* serialNumber = nullptr;
364 const std::string* manufacturer = nullptr;
365 const std::string* model = nullptr;
366 const std::string* sparePartNumber = nullptr;
367
368 const bool success = sdbusplus::unpackPropertiesNoThrow(
369 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
370 partNumber, "SerialNumber", serialNumber, "Manufacturer", manufacturer,
371 "Model", model, "SparePartNumber", sparePartNumber);
372
373 if (!success)
374 {
375 messages::internalError(asyncResp->res);
376 return;
377 }
378
379 if (partNumber != nullptr)
380 {
381 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
382 }
383
384 if (serialNumber != nullptr)
385 {
386 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
387 }
388
389 if (manufacturer != nullptr)
390 {
391 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
392 }
393
394 if (model != nullptr)
395 {
396 asyncResp->res.jsonValue["Model"] = *model;
397 }
398
399 // SparePartNumber is optional on D-Bus
400 // so skip if it is empty
401 if (sparePartNumber != nullptr && !sparePartNumber->empty())
402 {
403 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
404 }
405
406 asyncResp->res.jsonValue["Name"] = chassisId;
407 asyncResp->res.jsonValue["Id"] = chassisId;
408#ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
409 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
410 boost::urls::format("/redfish/v1/Chassis/{}/Thermal", chassisId);
411 // Power object
412 asyncResp->res.jsonValue["Power"]["@odata.id"] =
413 boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
414#endif
415#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
416 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
417 boost::urls::format("/redfish/v1/Chassis/{}/ThermalSubsystem",
418 chassisId);
419 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
420 boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem", chassisId);
421 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
422 boost::urls::format("/redfish/v1/Chassis/{}/EnvironmentMetrics",
423 chassisId);
424#endif
425 // SensorCollection
426 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
427 boost::urls::format("/redfish/v1/Chassis/{}/Sensors", chassisId);
428 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
429
430 nlohmann::json::array_t computerSystems;
431 nlohmann::json::object_t system;
432 system["@odata.id"] = "/redfish/v1/Systems/system";
433 computerSystems.emplace_back(std::move(system));
434 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
435 std::move(computerSystems);
436
437 nlohmann::json::array_t managedBy;
438 nlohmann::json::object_t manager;
439 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
440 managedBy.emplace_back(std::move(manager));
441 asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
442 getChassisState(asyncResp);
443 getStorageLink(asyncResp, path);
444}
445
446inline void handleChassisGetSubTree(
447 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
448 const std::string& chassisId, const boost::system::error_code& ec,
449 const dbus::utility::MapperGetSubTreeResponse& subtree)
450{
451 if (ec)
452 {
453 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
454 messages::internalError(asyncResp->res);
455 return;
456 }
457 // Iterate over all retrieved ObjectPaths.
458 for (const std::pair<
459 std::string,
460 std::vector<std::pair<std::string, std::vector<std::string>>>>&
461 object : subtree)
462 {
463 const std::string& path = object.first;
464 const std::vector<std::pair<std::string, std::vector<std::string>>>&
465 connectionNames = object.second;
466
467 sdbusplus::message::object_path objPath(path);
468 if (objPath.filename() != chassisId)
469 {
470 continue;
471 }
472
473 getChassisConnectivity(asyncResp, chassisId, path);
474
475 auto health = std::make_shared<HealthPopulate>(asyncResp);
476
477 if constexpr (bmcwebEnableHealthPopulate)
478 {
479 dbus::utility::getAssociationEndPoints(
480 path + "/all_sensors",
481 [health](const boost::system::error_code& ec2,
482 const dbus::utility::MapperEndPoints& resp) {
483 if (ec2)
484 {
485 return; // no sensors = no failures
486 }
487 health->inventory = resp;
488 });
489
490 health->populate();
491 }
492
493 if (connectionNames.empty())
494 {
495 BMCWEB_LOG_ERROR("Got 0 Connection names");
496 continue;
497 }
498
499 asyncResp->res.jsonValue["@odata.type"] = "#Chassis.v1_22_0.Chassis";
500 asyncResp->res.jsonValue["@odata.id"] =
501 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
502 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
503 asyncResp->res.jsonValue["ChassisType"] = "RackMount";
504 asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
505 boost::urls::format("/redfish/v1/Chassis/{}/Actions/Chassis.Reset",
506 chassisId);
507 asyncResp->res
508 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
509 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
510 chassisId);
511 asyncResp->res.jsonValue["PCIeDevices"]["@odata.id"] =
512 "/redfish/v1/Systems/system/PCIeDevices";
513
514 dbus::utility::getAssociationEndPoints(
515 path + "/drive",
516 [asyncResp, chassisId](const boost::system::error_code& ec3,
517 const dbus::utility::MapperEndPoints& resp) {
518 if (ec3 || resp.empty())
519 {
520 return; // no drives = no failures
521 }
522
523 nlohmann::json reference;
524 reference["@odata.id"] =
525 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
526 asyncResp->res.jsonValue["Drives"] = std::move(reference);
527 });
528
529 const std::string& connectionName = connectionNames[0].first;
530
531 const std::vector<std::string>& interfaces2 = connectionNames[0].second;
532 const std::array<const char*, 2> hasIndicatorLed = {
533 "xyz.openbmc_project.Inventory.Item.Panel",
534 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
535
536 const std::string assetTagInterface =
537 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
538 const std::string replaceableInterface =
539 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
540 for (const auto& interface : interfaces2)
541 {
542 if (interface == assetTagInterface)
543 {
544 sdbusplus::asio::getProperty<std::string>(
545 *crow::connections::systemBus, connectionName, path,
546 assetTagInterface, "AssetTag",
547 [asyncResp, chassisId](const boost::system::error_code& ec2,
548 const std::string& property) {
549 if (ec2)
550 {
551 BMCWEB_LOG_ERROR("DBus response error for AssetTag: {}",
552 ec2);
553 messages::internalError(asyncResp->res);
554 return;
555 }
556 asyncResp->res.jsonValue["AssetTag"] = property;
557 });
558 }
559 else if (interface == replaceableInterface)
560 {
561 sdbusplus::asio::getProperty<bool>(
562 *crow::connections::systemBus, connectionName, path,
563 replaceableInterface, "HotPluggable",
564 [asyncResp, chassisId](const boost::system::error_code& ec2,
565 const bool property) {
566 if (ec2)
567 {
568 BMCWEB_LOG_ERROR(
569 "DBus response error for HotPluggable: {}", ec2);
570 messages::internalError(asyncResp->res);
571 return;
572 }
573 asyncResp->res.jsonValue["HotPluggable"] = property;
574 });
575 }
576 }
577
578 for (const char* interface : hasIndicatorLed)
579 {
580 if (std::ranges::find(interfaces2, interface) != interfaces2.end())
581 {
582 getIndicatorLedState(asyncResp);
583 getSystemLocationIndicatorActive(asyncResp);
584 break;
585 }
586 }
587
588 sdbusplus::asio::getAllProperties(
589 *crow::connections::systemBus, connectionName, path,
590 "xyz.openbmc_project.Inventory.Decorator.Asset",
591 [asyncResp, chassisId,
592 path](const boost::system::error_code&,
593 const dbus::utility::DBusPropertiesMap& propertiesList) {
594 handleDecoratorAssetProperties(asyncResp, chassisId, path,
595 propertiesList);
596 });
597
598 for (const auto& interface : interfaces2)
599 {
600 if (interface == "xyz.openbmc_project.Common.UUID")
601 {
602 getChassisUUID(asyncResp, connectionName, path);
603 }
604 else if (interface ==
605 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
606 {
607 getChassisLocationCode(asyncResp, connectionName, path);
608 }
609 }
610
611 return;
612 }
613
614 // Couldn't find an object with that name. return an error
615 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
616}
617
Nan Zhoucf7eba02022-07-21 23:53:20 +0000618inline void
619 handleChassisGet(App& app, const crow::Request& req,
620 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
621 const std::string& chassisId)
622{
623 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
624 {
625 return;
626 }
George Liue99073f2022-12-09 11:06:16 +0800627 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000628 "xyz.openbmc_project.Inventory.Item.Board",
629 "xyz.openbmc_project.Inventory.Item.Chassis"};
630
George Liue99073f2022-12-09 11:06:16 +0800631 dbus::utility::getSubTree(
632 "/xyz/openbmc_project/inventory", 0, interfaces,
Chau Ly7164bc62023-10-15 14:55:30 +0000633 std::bind_front(handleChassisGetSubTree, asyncResp, chassisId));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000634
Chau Ly7164bc62023-10-15 14:55:30 +0000635 constexpr std::array<std::string_view, 1> interfaces2 = {
636 "xyz.openbmc_project.Chassis.Intrusion"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000637
Chau Ly7164bc62023-10-15 14:55:30 +0000638 dbus::utility::getSubTree(
639 "/xyz/openbmc_project", 0, interfaces2,
640 std::bind_front(handlePhysicalSecurityGetSubTree, asyncResp));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000641}
642
643inline void
644 handleChassisPatch(App& app, const crow::Request& req,
645 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
646 const std::string& param)
647{
648 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
649 {
650 return;
651 }
652 std::optional<bool> locationIndicatorActive;
653 std::optional<std::string> indicatorLed;
654
655 if (param.empty())
656 {
657 return;
658 }
659
660 if (!json_util::readJsonPatch(
661 req, asyncResp->res, "LocationIndicatorActive",
662 locationIndicatorActive, "IndicatorLED", indicatorLed))
663 {
664 return;
665 }
666
667 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
668 if (!locationIndicatorActive && !indicatorLed)
669 {
670 return; // delete this when we support more patch properties
671 }
672 if (indicatorLed)
673 {
674 asyncResp->res.addHeader(
675 boost::beast::http::field::warning,
676 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
677 }
678
George Liue99073f2022-12-09 11:06:16 +0800679 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000680 "xyz.openbmc_project.Inventory.Item.Board",
681 "xyz.openbmc_project.Inventory.Item.Chassis"};
682
683 const std::string& chassisId = param;
684
George Liue99073f2022-12-09 11:06:16 +0800685 dbus::utility::getSubTree(
686 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000687 [asyncResp, chassisId, locationIndicatorActive,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800688 indicatorLed](const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000689 const dbus::utility::MapperGetSubTreeResponse& subtree) {
690 if (ec)
691 {
Ed Tanous62598e32023-07-17 17:06:25 -0700692 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000693 messages::internalError(asyncResp->res);
694 return;
695 }
696
697 // Iterate over all retrieved ObjectPaths.
698 for (const std::pair<
699 std::string,
700 std::vector<std::pair<std::string, std::vector<std::string>>>>&
701 object : subtree)
702 {
703 const std::string& path = object.first;
704 const std::vector<std::pair<std::string, std::vector<std::string>>>&
705 connectionNames = object.second;
706
707 sdbusplus::message::object_path objPath(path);
708 if (objPath.filename() != chassisId)
709 {
710 continue;
711 }
712
713 if (connectionNames.empty())
714 {
Ed Tanous62598e32023-07-17 17:06:25 -0700715 BMCWEB_LOG_ERROR("Got 0 Connection names");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000716 continue;
717 }
718
719 const std::vector<std::string>& interfaces3 =
720 connectionNames[0].second;
721
722 const std::array<const char*, 2> hasIndicatorLed = {
723 "xyz.openbmc_project.Inventory.Item.Panel",
724 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
725 bool indicatorChassis = false;
726 for (const char* interface : hasIndicatorLed)
727 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700728 if (std::ranges::find(interfaces3, interface) !=
729 interfaces3.end())
Nan Zhoucf7eba02022-07-21 23:53:20 +0000730 {
731 indicatorChassis = true;
732 break;
733 }
734 }
735 if (locationIndicatorActive)
736 {
737 if (indicatorChassis)
738 {
George Liu59a17e42022-10-08 09:27:47 +0800739 setSystemLocationIndicatorActive(asyncResp,
740 *locationIndicatorActive);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000741 }
742 else
743 {
744 messages::propertyUnknown(asyncResp->res,
745 "LocationIndicatorActive");
746 }
747 }
748 if (indicatorLed)
749 {
750 if (indicatorChassis)
751 {
752 setIndicatorLedState(asyncResp, *indicatorLed);
753 }
754 else
755 {
756 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
757 }
758 }
759 return;
760 }
761
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800762 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
Patrick Williams5a39f772023-10-20 11:20:21 -0500763 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000764}
765
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100766/**
767 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700768 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100769 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700770inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700771{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700772 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700773 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700774 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000775 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700776
777 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700778 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700779 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000780 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700781}
P.K. Leedd99e042020-06-17 19:43:16 +0800782
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400783/**
784 * Handle error responses from d-bus for chassis power cycles
785 */
786inline void handleChassisPowerCycleError(const boost::system::error_code& ec,
787 const sdbusplus::message_t& eMsg,
788 crow::Response& res)
789{
790 if (eMsg.get_error() == nullptr)
791 {
Ed Tanous62598e32023-07-17 17:06:25 -0700792 BMCWEB_LOG_ERROR("D-Bus response error: {}", ec);
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400793 messages::internalError(res);
794 return;
795 }
796 std::string_view errorMessage = eMsg.get_error()->name;
797
798 // If operation failed due to BMC not being in Ready state, tell
799 // user to retry in a bit
800 if (errorMessage ==
801 std::string_view("xyz.openbmc_project.State.Chassis.Error.BMCNotReady"))
802 {
Ed Tanous62598e32023-07-17 17:06:25 -0700803 BMCWEB_LOG_DEBUG("BMC not ready, operation not allowed right now");
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400804 messages::serviceTemporarilyUnavailable(res, "10");
805 return;
806 }
807
Ed Tanous62598e32023-07-17 17:06:25 -0700808 BMCWEB_LOG_ERROR("Chassis Power Cycle fail {} sdbusplus:{}", ec,
809 errorMessage);
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400810 messages::internalError(res);
811}
812
zhanghch058d1b46d2021-04-01 11:18:24 +0800813inline void
814 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800815{
George Liu7a1dbc42022-12-07 16:03:22 +0800816 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700817 "xyz.openbmc_project.State.Chassis"};
818
819 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800820 dbus::utility::getSubTreePaths(
821 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800822 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800823 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800824 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700825 if (ec)
826 {
Ed Tanous62598e32023-07-17 17:06:25 -0700827 BMCWEB_LOG_ERROR("[mapper] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700828 messages::internalError(asyncResp->res);
829 return;
830 }
831
832 const char* processName = "xyz.openbmc_project.State.Chassis";
833 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
834 const char* destProperty = "RequestedPowerTransition";
835 const std::string propertyValue =
836 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
837 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
838
839 /* Look for system reset chassis path */
Ed Tanous3544d2a2023-08-06 18:12:20 -0700840 if ((std::ranges::find(chassisList, objectPath)) == chassisList.end())
Ed Tanous002d39b2022-05-31 08:59:27 -0700841 {
842 /* We prefer to reset the full chassis_system, but if it doesn't
843 * exist on some platforms, fall back to a host-only power reset
844 */
845 objectPath = "/xyz/openbmc_project/state/chassis0";
846 }
847
George Liu9ae226f2023-06-21 17:56:46 +0800848 sdbusplus::asio::setProperty(
849 *crow::connections::systemBus, processName, objectPath,
850 interfaceName, destProperty, propertyValue,
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400851 [asyncResp](const boost::system::error_code& ec2,
852 sdbusplus::message_t& sdbusErrMsg) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700853 // Use "Set" method to set the property value.
Ed Tanous8a592812022-06-04 09:06:59 -0700854 if (ec2)
P.K. Leedd99e042020-06-17 19:43:16 +0800855 {
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400856 handleChassisPowerCycleError(ec2, sdbusErrMsg, asyncResp->res);
857
P.K. Leedd99e042020-06-17 19:43:16 +0800858 return;
859 }
860
Ed Tanous002d39b2022-05-31 08:59:27 -0700861 messages::success(asyncResp->res);
George Liu7a1dbc42022-12-07 16:03:22 +0800862 });
Patrick Williams5a39f772023-10-20 11:20:21 -0500863 });
P.K. Leedd99e042020-06-17 19:43:16 +0800864}
865
Nan Zhoucf7eba02022-07-21 23:53:20 +0000866inline void handleChassisResetActionInfoPost(
867 App& app, const crow::Request& req,
868 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
869 const std::string& /*chassisId*/)
870{
871 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
872 {
873 return;
874 }
Ed Tanous62598e32023-07-17 17:06:25 -0700875 BMCWEB_LOG_DEBUG("Post Chassis Reset.");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000876
877 std::string resetType;
878
879 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
880 {
881 return;
882 }
883
884 if (resetType != "PowerCycle")
885 {
Ed Tanous62598e32023-07-17 17:06:25 -0700886 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000887 messages::actionParameterNotSupported(asyncResp->res, resetType,
888 "ResetType");
889
890 return;
891 }
892 doChassisPowerCycle(asyncResp);
893}
894
P.K. Leedd99e042020-06-17 19:43:16 +0800895/**
896 * ChassisResetAction class supports the POST method for the Reset
897 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700898 * Function handles POST method request.
899 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800900 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700901
902inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800903{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700904 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700905 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700906 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000907 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
908}
P.K. Leedd99e042020-06-17 19:43:16 +0800909
Nan Zhoucf7eba02022-07-21 23:53:20 +0000910inline void handleChassisResetActionInfoGet(
911 App& app, const crow::Request& req,
912 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
913 const std::string& chassisId)
914{
915 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
916 {
917 return;
918 }
919 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700920 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
921 "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000922 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800923
Nan Zhoucf7eba02022-07-21 23:53:20 +0000924 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
925 nlohmann::json::array_t parameters;
926 nlohmann::json::object_t parameter;
927 parameter["Name"] = "ResetType";
928 parameter["Required"] = true;
929 parameter["DataType"] = "String";
930 nlohmann::json::array_t allowed;
Patrick Williamsad539542023-05-12 10:10:08 -0500931 allowed.emplace_back("PowerCycle");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000932 parameter["AllowableValues"] = std::move(allowed);
Patrick Williamsad539542023-05-12 10:10:08 -0500933 parameters.emplace_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800934
Nan Zhoucf7eba02022-07-21 23:53:20 +0000935 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700936}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530937
938/**
939 * ChassisResetActionInfo derived class for delivering Chassis
940 * ResetType AllowableValues using ResetInfo schema.
941 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700942inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530943{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700944 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700945 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700946 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000947 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700948}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530949
Ed Tanous1abe55e2018-09-05 08:30:59 -0700950} // namespace redfish