blob: 6cb877f84584dfce01b54bdf7058ef46879219ce [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"
25#include "registries/privilege_registry.hpp"
26#include "utils/collection.hpp"
27#include "utils/dbus_utils.hpp"
Nan Zhoucf7eba02022-07-21 23:53:20 +000028#include "utils/json_utils.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070029
George Liue99073f2022-12-09 11:06:16 +080030#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070031#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070032#include <sdbusplus/asio/property.hpp>
Andrew Geisslerfc903b32023-05-31 14:15:42 -040033#include <sdbusplus/message.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020034#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050035
George Liu7a1dbc42022-12-07 16:03:22 +080036#include <array>
37#include <string_view>
38
Ed Tanous1abe55e2018-09-05 08:30:59 -070039namespace redfish
40{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010041
42/**
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060043 * @brief Retrieves chassis state properties over dbus
44 *
Ed Tanousac106bf2023-06-07 09:24:59 -070045 * @param[in] asyncResp - Shared pointer for completing asynchronous calls.
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060046 *
47 * @return None.
48 */
Ed Tanousac106bf2023-06-07 09:24:59 -070049inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060050{
Jonathan Doman1e1e5982021-06-11 09:36:17 -070051 // crow::connections::systemBus->async_method_call(
52 sdbusplus::asio::getProperty<std::string>(
53 *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
54 "/xyz/openbmc_project/state/chassis0",
55 "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
Ed Tanousac106bf2023-06-07 09:24:59 -070056 [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
57 const std::string& chassisState) {
Ed Tanous002d39b2022-05-31 08:59:27 -070058 if (ec)
59 {
60 if (ec == boost::system::errc::host_unreachable)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060061 {
Ed Tanous002d39b2022-05-31 08:59:27 -070062 // Service not available, no error, just don't return
63 // chassis state info
64 BMCWEB_LOG_DEBUG << "Service not available " << ec;
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060065 return;
66 }
Ed Tanousac106bf2023-06-07 09:24:59 -070067 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
68 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -070069 return;
70 }
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060071
Ed Tanous002d39b2022-05-31 08:59:27 -070072 BMCWEB_LOG_DEBUG << "Chassis state: " << chassisState;
73 // Verify Chassis State
74 if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
75 {
Ed Tanousac106bf2023-06-07 09:24:59 -070076 asyncResp->res.jsonValue["PowerState"] = "On";
77 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Ed Tanous002d39b2022-05-31 08:59:27 -070078 }
79 else if (chassisState ==
80 "xyz.openbmc_project.State.Chassis.PowerState.Off")
81 {
Ed Tanousac106bf2023-06-07 09:24:59 -070082 asyncResp->res.jsonValue["PowerState"] = "Off";
83 asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
Ed Tanous002d39b2022-05-31 08:59:27 -070084 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070085 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060086}
87
Ed Tanousac106bf2023-06-07 09:24:59 -070088inline void getIntrusionByService(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Ed Tanous23a21a12020-07-25 04:45:05 +000089 const std::string& service,
90 const std::string& objPath)
Qiang XUc1819422019-02-27 13:51:32 +080091{
92 BMCWEB_LOG_DEBUG << "Get intrusion status by service \n";
93
Jonathan Doman1e1e5982021-06-11 09:36:17 -070094 sdbusplus::asio::getProperty<std::string>(
95 *crow::connections::systemBus, service, objPath,
96 "xyz.openbmc_project.Chassis.Intrusion", "Status",
Ed Tanousac106bf2023-06-07 09:24:59 -070097 [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
98 const std::string& value) {
Ed Tanous002d39b2022-05-31 08:59:27 -070099 if (ec)
100 {
101 // do not add err msg in redfish response, because this is not
102 // mandatory property
103 BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n";
104 return;
105 }
Qiang XUc1819422019-02-27 13:51:32 +0800106
Ed Tanousac106bf2023-06-07 09:24:59 -0700107 asyncResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] =
108 1;
109 asyncResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700110 });
Qiang XUc1819422019-02-27 13:51:32 +0800111}
112
113/**
114 * Retrieves physical security properties over dbus
115 */
Ed Tanousac106bf2023-06-07 09:24:59 -0700116inline void
117 getPhysicalSecurityData(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
Qiang XUc1819422019-02-27 13:51:32 +0800118{
George Liue99073f2022-12-09 11:06:16 +0800119 constexpr std::array<std::string_view, 1> interfaces = {
120 "xyz.openbmc_project.Chassis.Intrusion"};
121 dbus::utility::getSubTree(
122 "/xyz/openbmc_project/Intrusion", 1, interfaces,
Ed Tanousac106bf2023-06-07 09:24:59 -0700123 [asyncResp{std::move(asyncResp)}](
George Liue99073f2022-12-09 11:06:16 +0800124 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800125 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700126 if (ec)
127 {
128 // do not add err msg in redfish response, because this is not
129 // mandatory property
130 BMCWEB_LOG_INFO << "DBUS error: no matched iface " << ec << "\n";
131 return;
132 }
133 // Iterate over all retrieved ObjectPaths.
134 for (const auto& object : subtree)
135 {
Patrick Williams840a9ff2023-05-12 10:18:43 -0500136 if (!object.second.empty())
Qiang XUc1819422019-02-27 13:51:32 +0800137 {
Patrick Williams840a9ff2023-05-12 10:18:43 -0500138 const auto service = object.second.front();
Ed Tanousac106bf2023-06-07 09:24:59 -0700139 getIntrusionByService(asyncResp, service.first, object.first);
Qiang XUc1819422019-02-27 13:51:32 +0800140 return;
141 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700142 }
George Liue99073f2022-12-09 11:06:16 +0800143 });
Qiang XUc1819422019-02-27 13:51:32 +0800144}
145
Nan Zhoucf7eba02022-07-21 23:53:20 +0000146inline void handleChassisCollectionGet(
147 App& app, const crow::Request& req,
148 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
149{
150 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
151 {
152 return;
153 }
154 asyncResp->res.jsonValue["@odata.type"] =
155 "#ChassisCollection.ChassisCollection";
156 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
157 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
158
George Liu7a1dbc42022-12-07 16:03:22 +0800159 constexpr std::array<std::string_view, 2> interfaces{
160 "xyz.openbmc_project.Inventory.Item.Board",
161 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000162 collection_util::getCollectionMembers(
George Liu7a1dbc42022-12-07 16:03:22 +0800163 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000164}
165
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100166/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100167 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100169 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700171{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700172 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700173 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700174 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000175 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700176}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100177
Willy Tu308f70c2021-09-28 20:24:52 -0700178inline void
179 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
180 const std::string& connectionName,
181 const std::string& path)
182{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700183 sdbusplus::asio::getProperty<std::string>(
184 *crow::connections::systemBus, connectionName, path,
185 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800186 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700187 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700188 if (ec)
189 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400190 BMCWEB_LOG_ERROR << "DBUS response error for Location";
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 messages::internalError(asyncResp->res);
192 return;
193 }
Willy Tu308f70c2021-09-28 20:24:52 -0700194
Ed Tanous002d39b2022-05-31 08:59:27 -0700195 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
196 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700197 });
Willy Tu308f70c2021-09-28 20:24:52 -0700198}
199
200inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
201 const std::string& connectionName,
202 const std::string& path)
203{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700204 sdbusplus::asio::getProperty<std::string>(
205 *crow::connections::systemBus, connectionName, path,
206 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800207 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700208 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700209 if (ec)
210 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400211 BMCWEB_LOG_ERROR << "DBUS response error for UUID";
Ed Tanous002d39b2022-05-31 08:59:27 -0700212 messages::internalError(asyncResp->res);
213 return;
214 }
215 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700216 });
Willy Tu308f70c2021-09-28 20:24:52 -0700217}
218
Nan Zhoucf7eba02022-07-21 23:53:20 +0000219inline void
220 handleChassisGet(App& app, const crow::Request& req,
221 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
222 const std::string& chassisId)
223{
224 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
225 {
226 return;
227 }
George Liue99073f2022-12-09 11:06:16 +0800228 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000229 "xyz.openbmc_project.Inventory.Item.Board",
230 "xyz.openbmc_project.Inventory.Item.Chassis"};
231
George Liue99073f2022-12-09 11:06:16 +0800232 dbus::utility::getSubTree(
233 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000234 [asyncResp, chassisId(std::string(chassisId))](
George Liue99073f2022-12-09 11:06:16 +0800235 const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000236 const dbus::utility::MapperGetSubTreeResponse& subtree) {
237 if (ec)
238 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400239 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000240 messages::internalError(asyncResp->res);
241 return;
242 }
243 // Iterate over all retrieved ObjectPaths.
244 for (const std::pair<
245 std::string,
246 std::vector<std::pair<std::string, std::vector<std::string>>>>&
247 object : subtree)
248 {
249 const std::string& path = object.first;
250 const std::vector<std::pair<std::string, std::vector<std::string>>>&
251 connectionNames = object.second;
252
253 sdbusplus::message::object_path objPath(path);
254 if (objPath.filename() != chassisId)
255 {
256 continue;
257 }
258
259 auto health = std::make_shared<HealthPopulate>(asyncResp);
260
Willy Tu13451e32023-05-24 16:08:18 -0700261 if constexpr (bmcwebEnableHealthPopulate)
262 {
263 dbus::utility::getAssociationEndPoints(
264 path + "/all_sensors",
265 [health](const boost::system::error_code& ec2,
266 const dbus::utility::MapperEndPoints& resp) {
267 if (ec2)
268 {
269 return; // no sensors = no failures
270 }
271 health->inventory = resp;
272 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000273
Willy Tu13451e32023-05-24 16:08:18 -0700274 health->populate();
275 }
Nan Zhoucf7eba02022-07-21 23:53:20 +0000276
277 if (connectionNames.empty())
278 {
279 BMCWEB_LOG_ERROR << "Got 0 Connection names";
280 continue;
281 }
282
283 asyncResp->res.jsonValue["@odata.type"] =
Logananth Sundararaj523d4862023-01-24 11:56:28 +0530284 "#Chassis.v1_22_0.Chassis";
Nan Zhoucf7eba02022-07-21 23:53:20 +0000285 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700286 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000287 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
288 asyncResp->res.jsonValue["ChassisType"] = "RackMount";
289 asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700290 boost::urls::format(
291 "/redfish/v1/Chassis/{}/Actions/Chassis.Reset", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000292 asyncResp->res
293 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700294 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
295 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000296 asyncResp->res.jsonValue["PCIeDevices"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700297 "/redfish/v1/Systems/system/PCIeDevices";
Nan Zhoucf7eba02022-07-21 23:53:20 +0000298
George Liu6c3e9452023-03-03 13:55:29 +0800299 dbus::utility::getAssociationEndPoints(
300 path + "/drive",
301 [asyncResp,
302 chassisId](const boost::system::error_code& ec3,
303 const dbus::utility::MapperEndPoints& resp) {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000304 if (ec3 || resp.empty())
305 {
306 return; // no drives = no failures
307 }
308
309 nlohmann::json reference;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700310 reference["@odata.id"] = boost::urls::format(
311 "/redfish/v1/Chassis/{}/Drives", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000312 asyncResp->res.jsonValue["Drives"] = std::move(reference);
313 });
314
315 const std::string& connectionName = connectionNames[0].first;
316
317 const std::vector<std::string>& interfaces2 =
318 connectionNames[0].second;
319 const std::array<const char*, 2> hasIndicatorLed = {
320 "xyz.openbmc_project.Inventory.Item.Panel",
321 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
322
323 const std::string assetTagInterface =
324 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
Logananth Sundararaj523d4862023-01-24 11:56:28 +0530325 const std::string replaceableInterface =
326 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
327 for (const auto& interface : interfaces2)
Nan Zhoucf7eba02022-07-21 23:53:20 +0000328 {
Logananth Sundararaj523d4862023-01-24 11:56:28 +0530329 if (interface == assetTagInterface)
330 {
331 sdbusplus::asio::getProperty<std::string>(
332 *crow::connections::systemBus, connectionName, path,
333 assetTagInterface, "AssetTag",
334 [asyncResp,
335 chassisId](const boost::system::error_code& ec2,
336 const std::string& property) {
337 if (ec2)
338 {
339 BMCWEB_LOG_ERROR
340 << "DBus response error for AssetTag: " << ec2;
341 messages::internalError(asyncResp->res);
342 return;
343 }
344 asyncResp->res.jsonValue["AssetTag"] = property;
345 });
346 }
347 else if (interface == replaceableInterface)
348 {
349 sdbusplus::asio::getProperty<bool>(
350 *crow::connections::systemBus, connectionName, path,
351 replaceableInterface, "HotPluggable",
352 [asyncResp,
353 chassisId](const boost::system::error_code& ec2,
354 const bool property) {
355 if (ec2)
356 {
357 BMCWEB_LOG_ERROR
358 << "DBus response error for HotPluggable: "
359 << ec2;
360 messages::internalError(asyncResp->res);
361 return;
362 }
363 asyncResp->res.jsonValue["HotPluggable"] = property;
364 });
365 }
Nan Zhoucf7eba02022-07-21 23:53:20 +0000366 }
367
368 for (const char* interface : hasIndicatorLed)
369 {
370 if (std::find(interfaces2.begin(), interfaces2.end(),
371 interface) != interfaces2.end())
372 {
373 getIndicatorLedState(asyncResp);
374 getLocationIndicatorActive(asyncResp);
375 break;
376 }
377 }
378
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200379 sdbusplus::asio::getAllProperties(
380 *crow::connections::systemBus, connectionName, path,
381 "xyz.openbmc_project.Inventory.Decorator.Asset",
Nan Zhoucf7eba02022-07-21 23:53:20 +0000382 [asyncResp, chassisId(std::string(chassisId))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800383 const boost::system::error_code& /*ec2*/,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000384 const dbus::utility::DBusPropertiesMap& propertiesList) {
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200385 const std::string* partNumber = nullptr;
386 const std::string* serialNumber = nullptr;
387 const std::string* manufacturer = nullptr;
388 const std::string* model = nullptr;
389 const std::string* sparePartNumber = nullptr;
390
391 const bool success = sdbusplus::unpackPropertiesNoThrow(
392 dbus_utils::UnpackErrorPrinter(), propertiesList,
393 "PartNumber", partNumber, "SerialNumber", serialNumber,
394 "Manufacturer", manufacturer, "Model", model,
395 "SparePartNumber", sparePartNumber);
396
397 if (!success)
Nan Zhoucf7eba02022-07-21 23:53:20 +0000398 {
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200399 messages::internalError(asyncResp->res);
400 return;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000401 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200402
403 if (partNumber != nullptr)
404 {
405 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
406 }
407
408 if (serialNumber != nullptr)
409 {
410 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
411 }
412
413 if (manufacturer != nullptr)
414 {
415 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
416 }
417
418 if (model != nullptr)
419 {
420 asyncResp->res.jsonValue["Model"] = *model;
421 }
422
423 // SparePartNumber is optional on D-Bus
424 // so skip if it is empty
425 if (sparePartNumber != nullptr && !sparePartNumber->empty())
426 {
427 asyncResp->res.jsonValue["SparePartNumber"] =
428 *sparePartNumber;
429 }
430
Nan Zhoucf7eba02022-07-21 23:53:20 +0000431 asyncResp->res.jsonValue["Name"] = chassisId;
432 asyncResp->res.jsonValue["Id"] = chassisId;
433#ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
434 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700435 boost::urls::format("/redfish/v1/Chassis/{}/Thermal",
436 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000437 // Power object
438 asyncResp->res.jsonValue["Power"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700439 boost::urls::format("/redfish/v1/Chassis/{}/Power",
440 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000441#endif
Xiaochao Ma29739632021-03-02 15:53:13 +0800442#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
443 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700444 boost::urls::format(
445 "/redfish/v1/Chassis/{}/ThermalSubsystem", chassisId);
Chicago Duan77b36432021-02-05 15:48:26 +0800446 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700447 boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem",
448 chassisId);
Albert Zhang4ca3ec32021-06-13 14:39:38 +0800449 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700450 boost::urls::format(
451 "/redfish/v1/Chassis/{}/EnvironmentMetrics", chassisId);
Xiaochao Ma29739632021-03-02 15:53:13 +0800452#endif
Nan Zhoucf7eba02022-07-21 23:53:20 +0000453 // SensorCollection
454 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700455 boost::urls::format("/redfish/v1/Chassis/{}/Sensors",
456 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000457 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
458
459 nlohmann::json::array_t computerSystems;
460 nlohmann::json::object_t system;
461 system["@odata.id"] = "/redfish/v1/Systems/system";
Patrick Williamsad539542023-05-12 10:10:08 -0500462 computerSystems.emplace_back(std::move(system));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000463 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
464 std::move(computerSystems);
465
466 nlohmann::json::array_t managedBy;
467 nlohmann::json::object_t manager;
468 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
Patrick Williamsad539542023-05-12 10:10:08 -0500469 managedBy.emplace_back(std::move(manager));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000470 asyncResp->res.jsonValue["Links"]["ManagedBy"] =
471 std::move(managedBy);
472 getChassisState(asyncResp);
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200473 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000474
475 for (const auto& interface : interfaces2)
476 {
477 if (interface == "xyz.openbmc_project.Common.UUID")
478 {
479 getChassisUUID(asyncResp, connectionName, path);
480 }
481 else if (interface ==
482 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
483 {
484 getChassisLocationCode(asyncResp, connectionName, path);
485 }
486 }
487
488 return;
489 }
490
491 // Couldn't find an object with that name. return an error
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800492 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
George Liue99073f2022-12-09 11:06:16 +0800493 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000494
495 getPhysicalSecurityData(asyncResp);
496}
497
498inline void
499 handleChassisPatch(App& app, const crow::Request& req,
500 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
501 const std::string& param)
502{
503 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
504 {
505 return;
506 }
507 std::optional<bool> locationIndicatorActive;
508 std::optional<std::string> indicatorLed;
509
510 if (param.empty())
511 {
512 return;
513 }
514
515 if (!json_util::readJsonPatch(
516 req, asyncResp->res, "LocationIndicatorActive",
517 locationIndicatorActive, "IndicatorLED", indicatorLed))
518 {
519 return;
520 }
521
522 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
523 if (!locationIndicatorActive && !indicatorLed)
524 {
525 return; // delete this when we support more patch properties
526 }
527 if (indicatorLed)
528 {
529 asyncResp->res.addHeader(
530 boost::beast::http::field::warning,
531 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
532 }
533
George Liue99073f2022-12-09 11:06:16 +0800534 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000535 "xyz.openbmc_project.Inventory.Item.Board",
536 "xyz.openbmc_project.Inventory.Item.Chassis"};
537
538 const std::string& chassisId = param;
539
George Liue99073f2022-12-09 11:06:16 +0800540 dbus::utility::getSubTree(
541 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000542 [asyncResp, chassisId, locationIndicatorActive,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800543 indicatorLed](const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000544 const dbus::utility::MapperGetSubTreeResponse& subtree) {
545 if (ec)
546 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400547 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000548 messages::internalError(asyncResp->res);
549 return;
550 }
551
552 // Iterate over all retrieved ObjectPaths.
553 for (const std::pair<
554 std::string,
555 std::vector<std::pair<std::string, std::vector<std::string>>>>&
556 object : subtree)
557 {
558 const std::string& path = object.first;
559 const std::vector<std::pair<std::string, std::vector<std::string>>>&
560 connectionNames = object.second;
561
562 sdbusplus::message::object_path objPath(path);
563 if (objPath.filename() != chassisId)
564 {
565 continue;
566 }
567
568 if (connectionNames.empty())
569 {
570 BMCWEB_LOG_ERROR << "Got 0 Connection names";
571 continue;
572 }
573
574 const std::vector<std::string>& interfaces3 =
575 connectionNames[0].second;
576
577 const std::array<const char*, 2> hasIndicatorLed = {
578 "xyz.openbmc_project.Inventory.Item.Panel",
579 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
580 bool indicatorChassis = false;
581 for (const char* interface : hasIndicatorLed)
582 {
583 if (std::find(interfaces3.begin(), interfaces3.end(),
584 interface) != interfaces3.end())
585 {
586 indicatorChassis = true;
587 break;
588 }
589 }
590 if (locationIndicatorActive)
591 {
592 if (indicatorChassis)
593 {
594 setLocationIndicatorActive(asyncResp,
595 *locationIndicatorActive);
596 }
597 else
598 {
599 messages::propertyUnknown(asyncResp->res,
600 "LocationIndicatorActive");
601 }
602 }
603 if (indicatorLed)
604 {
605 if (indicatorChassis)
606 {
607 setIndicatorLedState(asyncResp, *indicatorLed);
608 }
609 else
610 {
611 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
612 }
613 }
614 return;
615 }
616
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800617 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
George Liue99073f2022-12-09 11:06:16 +0800618 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000619}
620
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100621/**
622 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700623 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100624 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700625inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700627 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700628 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700629 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000630 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700631
632 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700633 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000635 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700636}
P.K. Leedd99e042020-06-17 19:43:16 +0800637
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400638/**
639 * Handle error responses from d-bus for chassis power cycles
640 */
641inline void handleChassisPowerCycleError(const boost::system::error_code& ec,
642 const sdbusplus::message_t& eMsg,
643 crow::Response& res)
644{
645 if (eMsg.get_error() == nullptr)
646 {
647 BMCWEB_LOG_ERROR << "D-Bus response error: " << ec;
648 messages::internalError(res);
649 return;
650 }
651 std::string_view errorMessage = eMsg.get_error()->name;
652
653 // If operation failed due to BMC not being in Ready state, tell
654 // user to retry in a bit
655 if (errorMessage ==
656 std::string_view("xyz.openbmc_project.State.Chassis.Error.BMCNotReady"))
657 {
658 BMCWEB_LOG_DEBUG << "BMC not ready, operation not allowed right now";
659 messages::serviceTemporarilyUnavailable(res, "10");
660 return;
661 }
662
663 BMCWEB_LOG_ERROR << "Chassis Power Cycle fail " << ec
664 << " sdbusplus:" << errorMessage;
665 messages::internalError(res);
666}
667
zhanghch058d1b46d2021-04-01 11:18:24 +0800668inline void
669 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800670{
George Liu7a1dbc42022-12-07 16:03:22 +0800671 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700672 "xyz.openbmc_project.State.Chassis"};
673
674 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800675 dbus::utility::getSubTreePaths(
676 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800677 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800678 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800679 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 if (ec)
681 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400682 BMCWEB_LOG_ERROR << "[mapper] Bad D-Bus request error: " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -0700683 messages::internalError(asyncResp->res);
684 return;
685 }
686
687 const char* processName = "xyz.openbmc_project.State.Chassis";
688 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
689 const char* destProperty = "RequestedPowerTransition";
690 const std::string propertyValue =
691 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
692 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
693
694 /* Look for system reset chassis path */
695 if ((std::find(chassisList.begin(), chassisList.end(), objectPath)) ==
696 chassisList.end())
697 {
698 /* We prefer to reset the full chassis_system, but if it doesn't
699 * exist on some platforms, fall back to a host-only power reset
700 */
701 objectPath = "/xyz/openbmc_project/state/chassis0";
702 }
703
704 crow::connections::systemBus->async_method_call(
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400705 [asyncResp](const boost::system::error_code& ec2,
706 sdbusplus::message_t& sdbusErrMsg) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700707 // Use "Set" method to set the property value.
Ed Tanous8a592812022-06-04 09:06:59 -0700708 if (ec2)
P.K. Leedd99e042020-06-17 19:43:16 +0800709 {
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400710 handleChassisPowerCycleError(ec2, sdbusErrMsg, asyncResp->res);
711
P.K. Leedd99e042020-06-17 19:43:16 +0800712 return;
713 }
714
Ed Tanous002d39b2022-05-31 08:59:27 -0700715 messages::success(asyncResp->res);
716 },
717 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
718 interfaceName, destProperty,
719 dbus::utility::DbusVariantType{propertyValue});
George Liu7a1dbc42022-12-07 16:03:22 +0800720 });
P.K. Leedd99e042020-06-17 19:43:16 +0800721}
722
Nan Zhoucf7eba02022-07-21 23:53:20 +0000723inline void handleChassisResetActionInfoPost(
724 App& app, const crow::Request& req,
725 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
726 const std::string& /*chassisId*/)
727{
728 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
729 {
730 return;
731 }
732 BMCWEB_LOG_DEBUG << "Post Chassis Reset.";
733
734 std::string resetType;
735
736 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
737 {
738 return;
739 }
740
741 if (resetType != "PowerCycle")
742 {
743 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
744 << resetType;
745 messages::actionParameterNotSupported(asyncResp->res, resetType,
746 "ResetType");
747
748 return;
749 }
750 doChassisPowerCycle(asyncResp);
751}
752
P.K. Leedd99e042020-06-17 19:43:16 +0800753/**
754 * ChassisResetAction class supports the POST method for the Reset
755 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700756 * Function handles POST method request.
757 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800758 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700759
760inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800761{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700762 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700763 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700764 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000765 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
766}
P.K. Leedd99e042020-06-17 19:43:16 +0800767
Nan Zhoucf7eba02022-07-21 23:53:20 +0000768inline void handleChassisResetActionInfoGet(
769 App& app, const crow::Request& req,
770 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
771 const std::string& chassisId)
772{
773 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
774 {
775 return;
776 }
777 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700778 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
779 "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000780 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800781
Nan Zhoucf7eba02022-07-21 23:53:20 +0000782 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
783 nlohmann::json::array_t parameters;
784 nlohmann::json::object_t parameter;
785 parameter["Name"] = "ResetType";
786 parameter["Required"] = true;
787 parameter["DataType"] = "String";
788 nlohmann::json::array_t allowed;
Patrick Williamsad539542023-05-12 10:10:08 -0500789 allowed.emplace_back("PowerCycle");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000790 parameter["AllowableValues"] = std::move(allowed);
Patrick Williamsad539542023-05-12 10:10:08 -0500791 parameters.emplace_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800792
Nan Zhoucf7eba02022-07-21 23:53:20 +0000793 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700794}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530795
796/**
797 * ChassisResetActionInfo derived class for delivering Chassis
798 * ResetType AllowableValues using ResetInfo schema.
799 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700800inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530801{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700802 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700803 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700804 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000805 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700806}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530807
Ed Tanous1abe55e2018-09-05 08:30:59 -0700808} // namespace redfish