blob: c654760f6573dd3fc66dd4176362a7c5d7850408 [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
James Feist1c8fba92019-12-20 15:12:07 -080020#include "led.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "query.hpp"
Chau Ly7164bc62023-10-15 14:55:30 +000022#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "registries/privilege_registry.hpp"
24#include "utils/collection.hpp"
25#include "utils/dbus_utils.hpp"
Nan Zhoucf7eba02022-07-21 23:53:20 +000026#include "utils/json_utils.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070027
George Liue99073f2022-12-09 11:06:16 +080028#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070029#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070030#include <sdbusplus/asio/property.hpp>
Andrew Geisslerfc903b32023-05-31 14:15:42 -040031#include <sdbusplus/message.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020032#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
George Liu7a1dbc42022-12-07 16:03:22 +080034#include <array>
Ed Tanous3544d2a2023-08-06 18:12:20 -070035#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080036#include <string_view>
37
Ed Tanous1abe55e2018-09-05 08:30:59 -070038namespace redfish
39{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010040
41/**
Willy Tu5e577bc2022-07-26 00:41:55 +000042 * @brief Retrieves resources over dbus to link to the chassis
43 *
44 * @param[in] asyncResp - Shared pointer for completing asynchronous
45 * calls
46 * @param[in] path - Chassis dbus path to look for the storage.
47 *
48 * Calls the Association endpoints on the path + "/storage" and add the link of
49 * json["Links"]["Storage@odata.count"] =
50 * {"@odata.id", "/redfish/v1/Storage/" + resourceId}
51 *
52 * @return None.
53 */
54inline void getStorageLink(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
55 const sdbusplus::message::object_path& path)
56{
57 sdbusplus::asio::getProperty<std::vector<std::string>>(
58 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
59 (path / "storage").str, "xyz.openbmc_project.Association", "endpoints",
Willy Tud4b054c2023-06-12 15:18:45 -070060 [asyncResp](const boost::system::error_code& ec,
Willy Tu5e577bc2022-07-26 00:41:55 +000061 const std::vector<std::string>& storageList) {
62 if (ec)
63 {
Ed Tanous62598e32023-07-17 17:06:25 -070064 BMCWEB_LOG_DEBUG("getStorageLink got DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +000065 return;
66 }
67
68 nlohmann::json::array_t storages;
69 for (const std::string& storagePath : storageList)
70 {
71 std::string id =
72 sdbusplus::message::object_path(storagePath).filename();
73 if (id.empty())
74 {
75 continue;
76 }
77
78 nlohmann::json::object_t storage;
Ed Tanous253f11b2024-05-16 09:38:31 -070079 storage["@odata.id"] =
80 boost::urls::format("/redfish/v1/Systems/{}/Storage/{}",
81 BMCWEB_REDFISH_SYSTEM_URI_NAME, id);
Willy Tu5e577bc2022-07-26 00:41:55 +000082 storages.emplace_back(std::move(storage));
83 }
84 asyncResp->res.jsonValue["Links"]["Storage@odata.count"] =
85 storages.size();
86 asyncResp->res.jsonValue["Links"]["Storage"] = std::move(storages);
Patrick Williams5a39f772023-10-20 11:20:21 -050087 });
Willy Tu5e577bc2022-07-26 00:41:55 +000088}
89
90/**
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060091 * @brief Retrieves chassis state properties over dbus
92 *
Ed Tanousac106bf2023-06-07 09:24:59 -070093 * @param[in] asyncResp - Shared pointer for completing asynchronous calls.
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060094 *
95 * @return None.
96 */
Ed Tanousac106bf2023-06-07 09:24:59 -070097inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060098{
Jonathan Doman1e1e5982021-06-11 09:36:17 -070099 // crow::connections::systemBus->async_method_call(
100 sdbusplus::asio::getProperty<std::string>(
101 *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
102 "/xyz/openbmc_project/state/chassis0",
103 "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
Ed Tanousac106bf2023-06-07 09:24:59 -0700104 [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
105 const std::string& chassisState) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700106 if (ec)
107 {
108 if (ec == boost::system::errc::host_unreachable)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600109 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700110 // Service not available, no error, just don't return
111 // chassis state info
Ed Tanous62598e32023-07-17 17:06:25 -0700112 BMCWEB_LOG_DEBUG("Service not available {}", ec);
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600113 return;
114 }
Ed Tanous62598e32023-07-17 17:06:25 -0700115 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700116 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700117 return;
118 }
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600119
Ed Tanous62598e32023-07-17 17:06:25 -0700120 BMCWEB_LOG_DEBUG("Chassis state: {}", chassisState);
Ed Tanous002d39b2022-05-31 08:59:27 -0700121 // Verify Chassis State
122 if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
123 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700124 asyncResp->res.jsonValue["PowerState"] = "On";
125 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 }
127 else if (chassisState ==
128 "xyz.openbmc_project.State.Chassis.PowerState.Off")
129 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700130 asyncResp->res.jsonValue["PowerState"] = "Off";
131 asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500133 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600134}
135
Qiang XUc1819422019-02-27 13:51:32 +0800136/**
137 * Retrieves physical security properties over dbus
138 */
Chau Ly7164bc62023-10-15 14:55:30 +0000139inline void handlePhysicalSecurityGetSubTree(
140 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
141 const boost::system::error_code& ec,
142 const dbus::utility::MapperGetSubTreeResponse& subtree)
Qiang XUc1819422019-02-27 13:51:32 +0800143{
Chau Ly7164bc62023-10-15 14:55:30 +0000144 if (ec)
145 {
146 // do not add err msg in redfish response, because this is not
147 // mandatory property
148 BMCWEB_LOG_INFO("DBUS error: no matched iface {}", ec);
149 return;
150 }
151 // Iterate over all retrieved ObjectPaths.
152 for (const auto& object : subtree)
153 {
154 if (!object.second.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -0700155 {
Ed Tanous89144a32024-04-08 17:27:04 -0700156 const auto& service = object.second.front();
Chau Ly7164bc62023-10-15 14:55:30 +0000157
158 BMCWEB_LOG_DEBUG("Get intrusion status by service ");
159
160 sdbusplus::asio::getProperty<std::string>(
161 *crow::connections::systemBus, service.first, object.first,
162 "xyz.openbmc_project.Chassis.Intrusion", "Status",
163 [asyncResp](const boost::system::error_code& ec1,
164 const std::string& value) {
165 if (ec1)
166 {
167 // do not add err msg in redfish response, because this is
168 // not
169 // mandatory property
170 BMCWEB_LOG_ERROR("DBUS response error {}", ec1);
171 return;
172 }
173 asyncResp->res
174 .jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] = 1;
175 asyncResp->res
176 .jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
177 });
178
Ed Tanous002d39b2022-05-31 08:59:27 -0700179 return;
180 }
Chau Ly7164bc62023-10-15 14:55:30 +0000181 }
Qiang XUc1819422019-02-27 13:51:32 +0800182}
183
Nan Zhoucf7eba02022-07-21 23:53:20 +0000184inline void handleChassisCollectionGet(
185 App& app, const crow::Request& req,
186 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
187{
188 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
189 {
190 return;
191 }
192 asyncResp->res.jsonValue["@odata.type"] =
193 "#ChassisCollection.ChassisCollection";
194 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
195 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
196
George Liu7a1dbc42022-12-07 16:03:22 +0800197 constexpr std::array<std::string_view, 2> interfaces{
198 "xyz.openbmc_project.Inventory.Item.Board",
199 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000200 collection_util::getCollectionMembers(
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500201 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces,
202 "/xyz/openbmc_project/inventory");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000203}
204
Jie Yanga5617492021-06-29 12:59:14 -0700205inline void getChassisContainedBy(
206 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207 const std::string& chassisId, const boost::system::error_code& ec,
208 const dbus::utility::MapperEndPoints& upstreamChassisPaths)
209{
210 if (ec)
211 {
212 if (ec.value() != EBADR)
213 {
Ed Tanous62598e32023-07-17 17:06:25 -0700214 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Jie Yanga5617492021-06-29 12:59:14 -0700215 messages::internalError(asyncResp->res);
216 }
217 return;
218 }
219 if (upstreamChassisPaths.empty())
220 {
221 return;
222 }
223 if (upstreamChassisPaths.size() > 1)
224 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800225 BMCWEB_LOG_ERROR("{} is contained by multiple chassis", chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700226 messages::internalError(asyncResp->res);
227 return;
228 }
229
230 sdbusplus::message::object_path upstreamChassisPath(
231 upstreamChassisPaths[0]);
232 std::string upstreamChassis = upstreamChassisPath.filename();
233 if (upstreamChassis.empty())
234 {
Ed Tanous62598e32023-07-17 17:06:25 -0700235 BMCWEB_LOG_WARNING("Malformed upstream Chassis path {} on {}",
236 upstreamChassisPath.str, chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700237 return;
238 }
239
240 asyncResp->res.jsonValue["Links"]["ContainedBy"]["@odata.id"] =
241 boost::urls::format("/redfish/v1/Chassis/{}", upstreamChassis);
242}
243
244inline void getChassisContains(
245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
246 const std::string& chassisId, const boost::system::error_code& ec,
247 const dbus::utility::MapperEndPoints& downstreamChassisPaths)
248{
249 if (ec)
250 {
251 if (ec.value() != EBADR)
252 {
Ed Tanous62598e32023-07-17 17:06:25 -0700253 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Jie Yanga5617492021-06-29 12:59:14 -0700254 messages::internalError(asyncResp->res);
255 }
256 return;
257 }
258 if (downstreamChassisPaths.empty())
259 {
260 return;
261 }
262 nlohmann::json& jValue = asyncResp->res.jsonValue["Links"]["Contains"];
263 if (!jValue.is_array())
264 {
265 // Create the array if it was empty
266 jValue = nlohmann::json::array();
267 }
268 for (const auto& p : downstreamChassisPaths)
269 {
270 sdbusplus::message::object_path downstreamChassisPath(p);
271 std::string downstreamChassis = downstreamChassisPath.filename();
272 if (downstreamChassis.empty())
273 {
Ed Tanous62598e32023-07-17 17:06:25 -0700274 BMCWEB_LOG_WARNING("Malformed downstream Chassis path {} on {}",
275 downstreamChassisPath.str, chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700276 continue;
277 }
278 nlohmann::json link;
279 link["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
280 downstreamChassis);
281 jValue.push_back(std::move(link));
282 }
283 asyncResp->res.jsonValue["Links"]["Contains@odata.count"] = jValue.size();
284}
285
286inline void
287 getChassisConnectivity(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
288 const std::string& chassisId,
289 const std::string& chassisPath)
290{
Ed Tanous62598e32023-07-17 17:06:25 -0700291 BMCWEB_LOG_DEBUG("Get chassis connectivity");
Jie Yanga5617492021-06-29 12:59:14 -0700292
293 dbus::utility::getAssociationEndPoints(
294 chassisPath + "/contained_by",
295 std::bind_front(getChassisContainedBy, asyncResp, chassisId));
296
297 dbus::utility::getAssociationEndPoints(
298 chassisPath + "/containing",
299 std::bind_front(getChassisContains, asyncResp, chassisId));
300}
301
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100302/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100303 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700304 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100305 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700306inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700307{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700308 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700309 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700310 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000311 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700312}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100313
Willy Tu308f70c2021-09-28 20:24:52 -0700314inline void
315 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
316 const std::string& connectionName,
317 const std::string& path)
318{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700319 sdbusplus::asio::getProperty<std::string>(
320 *crow::connections::systemBus, connectionName, path,
321 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800322 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700323 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700324 if (ec)
325 {
Ed Tanous62598e32023-07-17 17:06:25 -0700326 BMCWEB_LOG_ERROR("DBUS response error for Location");
Ed Tanous002d39b2022-05-31 08:59:27 -0700327 messages::internalError(asyncResp->res);
328 return;
329 }
Willy Tu308f70c2021-09-28 20:24:52 -0700330
Ed Tanous002d39b2022-05-31 08:59:27 -0700331 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
332 property;
Patrick Williams5a39f772023-10-20 11:20:21 -0500333 });
Willy Tu308f70c2021-09-28 20:24:52 -0700334}
335
336inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
337 const std::string& connectionName,
338 const std::string& path)
339{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700340 sdbusplus::asio::getProperty<std::string>(
341 *crow::connections::systemBus, connectionName, path,
342 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800343 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700344 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700345 if (ec)
346 {
Ed Tanous62598e32023-07-17 17:06:25 -0700347 BMCWEB_LOG_ERROR("DBUS response error for UUID");
Ed Tanous002d39b2022-05-31 08:59:27 -0700348 messages::internalError(asyncResp->res);
349 return;
350 }
351 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Patrick Williams5a39f772023-10-20 11:20:21 -0500352 });
Willy Tu308f70c2021-09-28 20:24:52 -0700353}
354
Chau Ly7164bc62023-10-15 14:55:30 +0000355inline void handleDecoratorAssetProperties(
356 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357 const std::string& chassisId, const std::string& path,
358 const dbus::utility::DBusPropertiesMap& propertiesList)
359{
360 const std::string* partNumber = nullptr;
361 const std::string* serialNumber = nullptr;
362 const std::string* manufacturer = nullptr;
363 const std::string* model = nullptr;
364 const std::string* sparePartNumber = nullptr;
365
366 const bool success = sdbusplus::unpackPropertiesNoThrow(
367 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
368 partNumber, "SerialNumber", serialNumber, "Manufacturer", manufacturer,
369 "Model", model, "SparePartNumber", sparePartNumber);
370
371 if (!success)
372 {
373 messages::internalError(asyncResp->res);
374 return;
375 }
376
377 if (partNumber != nullptr)
378 {
379 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
380 }
381
382 if (serialNumber != nullptr)
383 {
384 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
385 }
386
387 if (manufacturer != nullptr)
388 {
389 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
390 }
391
392 if (model != nullptr)
393 {
394 asyncResp->res.jsonValue["Model"] = *model;
395 }
396
397 // SparePartNumber is optional on D-Bus
398 // so skip if it is empty
399 if (sparePartNumber != nullptr && !sparePartNumber->empty())
400 {
401 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
402 }
403
404 asyncResp->res.jsonValue["Name"] = chassisId;
405 asyncResp->res.jsonValue["Id"] = chassisId;
Ed Tanous25b54db2024-04-17 15:40:31 -0700406
407 if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_POWER_THERMAL)
408 {
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 }
415
416 if constexpr (BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM)
417 {
418 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
419 boost::urls::format("/redfish/v1/Chassis/{}/ThermalSubsystem",
420 chassisId);
421 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
422 boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem",
423 chassisId);
424 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
425 boost::urls::format("/redfish/v1/Chassis/{}/EnvironmentMetrics",
426 chassisId);
427 }
Chau Ly7164bc62023-10-15 14:55:30 +0000428 // SensorCollection
429 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
430 boost::urls::format("/redfish/v1/Chassis/{}/Sensors", chassisId);
431 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
432
433 nlohmann::json::array_t computerSystems;
434 nlohmann::json::object_t system;
Ed Tanous253f11b2024-05-16 09:38:31 -0700435 system["@odata.id"] = std::format("/redfish/v1/Systems/{}",
436 BMCWEB_REDFISH_SYSTEM_URI_NAME);
Chau Ly7164bc62023-10-15 14:55:30 +0000437 computerSystems.emplace_back(std::move(system));
438 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
439 std::move(computerSystems);
440
441 nlohmann::json::array_t managedBy;
442 nlohmann::json::object_t manager;
Ed Tanous253f11b2024-05-16 09:38:31 -0700443 manager["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}",
444 BMCWEB_REDFISH_MANAGER_URI_NAME);
Chau Ly7164bc62023-10-15 14:55:30 +0000445 managedBy.emplace_back(std::move(manager));
446 asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
447 getChassisState(asyncResp);
448 getStorageLink(asyncResp, path);
449}
450
451inline void handleChassisGetSubTree(
452 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
453 const std::string& chassisId, const boost::system::error_code& ec,
454 const dbus::utility::MapperGetSubTreeResponse& subtree)
455{
456 if (ec)
457 {
458 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
459 messages::internalError(asyncResp->res);
460 return;
461 }
462 // Iterate over all retrieved ObjectPaths.
463 for (const std::pair<
464 std::string,
465 std::vector<std::pair<std::string, std::vector<std::string>>>>&
466 object : subtree)
467 {
468 const std::string& path = object.first;
469 const std::vector<std::pair<std::string, std::vector<std::string>>>&
470 connectionNames = object.second;
471
472 sdbusplus::message::object_path objPath(path);
473 if (objPath.filename() != chassisId)
474 {
475 continue;
476 }
477
478 getChassisConnectivity(asyncResp, chassisId, path);
479
Chau Ly7164bc62023-10-15 14:55:30 +0000480 if (connectionNames.empty())
481 {
482 BMCWEB_LOG_ERROR("Got 0 Connection names");
483 continue;
484 }
485
486 asyncResp->res.jsonValue["@odata.type"] = "#Chassis.v1_22_0.Chassis";
487 asyncResp->res.jsonValue["@odata.id"] =
488 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
489 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
490 asyncResp->res.jsonValue["ChassisType"] = "RackMount";
491 asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
492 boost::urls::format("/redfish/v1/Chassis/{}/Actions/Chassis.Reset",
493 chassisId);
494 asyncResp->res
495 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
496 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
497 chassisId);
Chau Ly7164bc62023-10-15 14:55:30 +0000498 dbus::utility::getAssociationEndPoints(
499 path + "/drive",
500 [asyncResp, chassisId](const boost::system::error_code& ec3,
501 const dbus::utility::MapperEndPoints& resp) {
502 if (ec3 || resp.empty())
503 {
504 return; // no drives = no failures
505 }
506
507 nlohmann::json reference;
508 reference["@odata.id"] =
509 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
510 asyncResp->res.jsonValue["Drives"] = std::move(reference);
511 });
512
513 const std::string& connectionName = connectionNames[0].first;
514
515 const std::vector<std::string>& interfaces2 = connectionNames[0].second;
George Liue5ae9c12021-11-16 10:31:23 +0800516 const std::array<const char*, 3> hasIndicatorLed = {
517 "xyz.openbmc_project.Inventory.Item.Chassis",
Chau Ly7164bc62023-10-15 14:55:30 +0000518 "xyz.openbmc_project.Inventory.Item.Panel",
519 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
520
521 const std::string assetTagInterface =
522 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
523 const std::string replaceableInterface =
524 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
Carson Labradob4d593f2024-02-16 22:34:32 +0000525 const std::string revisionInterface =
526 "xyz.openbmc_project.Inventory.Decorator.Revision";
Chau Ly7164bc62023-10-15 14:55:30 +0000527 for (const auto& interface : interfaces2)
528 {
529 if (interface == assetTagInterface)
530 {
531 sdbusplus::asio::getProperty<std::string>(
532 *crow::connections::systemBus, connectionName, path,
533 assetTagInterface, "AssetTag",
534 [asyncResp, chassisId](const boost::system::error_code& ec2,
535 const std::string& property) {
536 if (ec2)
537 {
538 BMCWEB_LOG_ERROR("DBus response error for AssetTag: {}",
539 ec2);
540 messages::internalError(asyncResp->res);
541 return;
542 }
543 asyncResp->res.jsonValue["AssetTag"] = property;
544 });
545 }
546 else if (interface == replaceableInterface)
547 {
548 sdbusplus::asio::getProperty<bool>(
549 *crow::connections::systemBus, connectionName, path,
550 replaceableInterface, "HotPluggable",
551 [asyncResp, chassisId](const boost::system::error_code& ec2,
552 const bool property) {
553 if (ec2)
554 {
555 BMCWEB_LOG_ERROR(
556 "DBus response error for HotPluggable: {}", ec2);
557 messages::internalError(asyncResp->res);
558 return;
559 }
560 asyncResp->res.jsonValue["HotPluggable"] = property;
561 });
562 }
Carson Labradob4d593f2024-02-16 22:34:32 +0000563 else if (interface == revisionInterface)
564 {
565 sdbusplus::asio::getProperty<std::string>(
566 *crow::connections::systemBus, connectionName, path,
567 revisionInterface, "Version",
568 [asyncResp, chassisId](const boost::system::error_code& ec2,
569 const std::string& property) {
570 if (ec2)
571 {
572 BMCWEB_LOG_ERROR("DBus response error for Version: {}",
573 ec2);
574 messages::internalError(asyncResp->res);
575 return;
576 }
577 asyncResp->res.jsonValue["Version"] = property;
578 });
579 }
Chau Ly7164bc62023-10-15 14:55:30 +0000580 }
581
582 for (const char* interface : hasIndicatorLed)
583 {
584 if (std::ranges::find(interfaces2, interface) != interfaces2.end())
585 {
586 getIndicatorLedState(asyncResp);
587 getSystemLocationIndicatorActive(asyncResp);
588 break;
589 }
590 }
591
592 sdbusplus::asio::getAllProperties(
593 *crow::connections::systemBus, connectionName, path,
594 "xyz.openbmc_project.Inventory.Decorator.Asset",
595 [asyncResp, chassisId,
596 path](const boost::system::error_code&,
597 const dbus::utility::DBusPropertiesMap& propertiesList) {
598 handleDecoratorAssetProperties(asyncResp, chassisId, path,
599 propertiesList);
600 });
601
602 for (const auto& interface : interfaces2)
603 {
604 if (interface == "xyz.openbmc_project.Common.UUID")
605 {
606 getChassisUUID(asyncResp, connectionName, path);
607 }
608 else if (interface ==
609 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
610 {
611 getChassisLocationCode(asyncResp, connectionName, path);
612 }
613 }
614
615 return;
616 }
617
618 // Couldn't find an object with that name. return an error
619 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
620}
621
Nan Zhoucf7eba02022-07-21 23:53:20 +0000622inline void
623 handleChassisGet(App& app, const crow::Request& req,
624 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
625 const std::string& chassisId)
626{
627 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
628 {
629 return;
630 }
George Liue99073f2022-12-09 11:06:16 +0800631 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000632 "xyz.openbmc_project.Inventory.Item.Board",
633 "xyz.openbmc_project.Inventory.Item.Chassis"};
634
George Liue99073f2022-12-09 11:06:16 +0800635 dbus::utility::getSubTree(
636 "/xyz/openbmc_project/inventory", 0, interfaces,
Chau Ly7164bc62023-10-15 14:55:30 +0000637 std::bind_front(handleChassisGetSubTree, asyncResp, chassisId));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000638
Chau Ly7164bc62023-10-15 14:55:30 +0000639 constexpr std::array<std::string_view, 1> interfaces2 = {
640 "xyz.openbmc_project.Chassis.Intrusion"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000641
Chau Ly7164bc62023-10-15 14:55:30 +0000642 dbus::utility::getSubTree(
643 "/xyz/openbmc_project", 0, interfaces2,
644 std::bind_front(handlePhysicalSecurityGetSubTree, asyncResp));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000645}
646
647inline void
648 handleChassisPatch(App& app, const crow::Request& req,
649 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
650 const std::string& param)
651{
652 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
653 {
654 return;
655 }
656 std::optional<bool> locationIndicatorActive;
657 std::optional<std::string> indicatorLed;
658
659 if (param.empty())
660 {
661 return;
662 }
663
664 if (!json_util::readJsonPatch(
665 req, asyncResp->res, "LocationIndicatorActive",
666 locationIndicatorActive, "IndicatorLED", indicatorLed))
667 {
668 return;
669 }
670
671 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
672 if (!locationIndicatorActive && !indicatorLed)
673 {
674 return; // delete this when we support more patch properties
675 }
676 if (indicatorLed)
677 {
678 asyncResp->res.addHeader(
679 boost::beast::http::field::warning,
680 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
681 }
682
George Liue99073f2022-12-09 11:06:16 +0800683 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000684 "xyz.openbmc_project.Inventory.Item.Board",
685 "xyz.openbmc_project.Inventory.Item.Chassis"};
686
687 const std::string& chassisId = param;
688
George Liue99073f2022-12-09 11:06:16 +0800689 dbus::utility::getSubTree(
690 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000691 [asyncResp, chassisId, locationIndicatorActive,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800692 indicatorLed](const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000693 const dbus::utility::MapperGetSubTreeResponse& subtree) {
694 if (ec)
695 {
Ed Tanous62598e32023-07-17 17:06:25 -0700696 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000697 messages::internalError(asyncResp->res);
698 return;
699 }
700
701 // Iterate over all retrieved ObjectPaths.
702 for (const std::pair<
703 std::string,
704 std::vector<std::pair<std::string, std::vector<std::string>>>>&
705 object : subtree)
706 {
707 const std::string& path = object.first;
708 const std::vector<std::pair<std::string, std::vector<std::string>>>&
709 connectionNames = object.second;
710
711 sdbusplus::message::object_path objPath(path);
712 if (objPath.filename() != chassisId)
713 {
714 continue;
715 }
716
717 if (connectionNames.empty())
718 {
Ed Tanous62598e32023-07-17 17:06:25 -0700719 BMCWEB_LOG_ERROR("Got 0 Connection names");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000720 continue;
721 }
722
723 const std::vector<std::string>& interfaces3 =
724 connectionNames[0].second;
725
George Liue5ae9c12021-11-16 10:31:23 +0800726 const std::array<const char*, 3> hasIndicatorLed = {
727 "xyz.openbmc_project.Inventory.Item.Chassis",
Nan Zhoucf7eba02022-07-21 23:53:20 +0000728 "xyz.openbmc_project.Inventory.Item.Panel",
729 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
730 bool indicatorChassis = false;
731 for (const char* interface : hasIndicatorLed)
732 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700733 if (std::ranges::find(interfaces3, interface) !=
734 interfaces3.end())
Nan Zhoucf7eba02022-07-21 23:53:20 +0000735 {
736 indicatorChassis = true;
737 break;
738 }
739 }
740 if (locationIndicatorActive)
741 {
742 if (indicatorChassis)
743 {
George Liu59a17e42022-10-08 09:27:47 +0800744 setSystemLocationIndicatorActive(asyncResp,
745 *locationIndicatorActive);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000746 }
747 else
748 {
749 messages::propertyUnknown(asyncResp->res,
750 "LocationIndicatorActive");
751 }
752 }
753 if (indicatorLed)
754 {
755 if (indicatorChassis)
756 {
757 setIndicatorLedState(asyncResp, *indicatorLed);
758 }
759 else
760 {
761 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
762 }
763 }
764 return;
765 }
766
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800767 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
Patrick Williams5a39f772023-10-20 11:20:21 -0500768 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000769}
770
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100771/**
772 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700773 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100774 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700775inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700776{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700777 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700778 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700779 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000780 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700781
782 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700783 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700784 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000785 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700786}
P.K. Leedd99e042020-06-17 19:43:16 +0800787
zhanghch058d1b46d2021-04-01 11:18:24 +0800788inline void
789 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800790{
George Liu7a1dbc42022-12-07 16:03:22 +0800791 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700792 "xyz.openbmc_project.State.Chassis"};
793
794 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800795 dbus::utility::getSubTreePaths(
796 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800797 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800798 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800799 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700800 if (ec)
801 {
Ed Tanous62598e32023-07-17 17:06:25 -0700802 BMCWEB_LOG_ERROR("[mapper] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700803 messages::internalError(asyncResp->res);
804 return;
805 }
806
807 const char* processName = "xyz.openbmc_project.State.Chassis";
808 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
809 const char* destProperty = "RequestedPowerTransition";
810 const std::string propertyValue =
811 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
812 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
813
814 /* Look for system reset chassis path */
Ed Tanous3544d2a2023-08-06 18:12:20 -0700815 if ((std::ranges::find(chassisList, objectPath)) == chassisList.end())
Ed Tanous002d39b2022-05-31 08:59:27 -0700816 {
817 /* We prefer to reset the full chassis_system, but if it doesn't
818 * exist on some platforms, fall back to a host-only power reset
819 */
820 objectPath = "/xyz/openbmc_project/state/chassis0";
821 }
822
Ed Tanousd02aad32024-02-13 14:43:34 -0800823 setDbusProperty(asyncResp, processName, objectPath, interfaceName,
824 destProperty, "ResetType", propertyValue);
Patrick Williams5a39f772023-10-20 11:20:21 -0500825 });
P.K. Leedd99e042020-06-17 19:43:16 +0800826}
827
Nan Zhoucf7eba02022-07-21 23:53:20 +0000828inline void handleChassisResetActionInfoPost(
829 App& app, const crow::Request& req,
830 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
831 const std::string& /*chassisId*/)
832{
833 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
834 {
835 return;
836 }
Ed Tanous62598e32023-07-17 17:06:25 -0700837 BMCWEB_LOG_DEBUG("Post Chassis Reset.");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000838
839 std::string resetType;
840
841 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
842 {
843 return;
844 }
845
846 if (resetType != "PowerCycle")
847 {
Ed Tanous62598e32023-07-17 17:06:25 -0700848 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000849 messages::actionParameterNotSupported(asyncResp->res, resetType,
850 "ResetType");
851
852 return;
853 }
854 doChassisPowerCycle(asyncResp);
855}
856
P.K. Leedd99e042020-06-17 19:43:16 +0800857/**
858 * ChassisResetAction class supports the POST method for the Reset
859 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700860 * Function handles POST method request.
861 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800862 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700863
864inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800865{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700866 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700867 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700868 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000869 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
870}
P.K. Leedd99e042020-06-17 19:43:16 +0800871
Nan Zhoucf7eba02022-07-21 23:53:20 +0000872inline void handleChassisResetActionInfoGet(
873 App& app, const crow::Request& req,
874 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
875 const std::string& chassisId)
876{
877 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
878 {
879 return;
880 }
881 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700882 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
883 "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000884 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800885
Nan Zhoucf7eba02022-07-21 23:53:20 +0000886 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
887 nlohmann::json::array_t parameters;
888 nlohmann::json::object_t parameter;
889 parameter["Name"] = "ResetType";
890 parameter["Required"] = true;
891 parameter["DataType"] = "String";
892 nlohmann::json::array_t allowed;
Patrick Williamsad539542023-05-12 10:10:08 -0500893 allowed.emplace_back("PowerCycle");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000894 parameter["AllowableValues"] = std::move(allowed);
Patrick Williamsad539542023-05-12 10:10:08 -0500895 parameters.emplace_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800896
Nan Zhoucf7eba02022-07-21 23:53:20 +0000897 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700898}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530899
900/**
901 * ChassisResetActionInfo derived class for delivering Chassis
902 * ResetType AllowableValues using ResetInfo schema.
903 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700904inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530905{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700906 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700907 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700908 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000909 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700910}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530911
Ed Tanous1abe55e2018-09-05 08:30:59 -0700912} // namespace redfish