blob: 99e305bde693dc42a9697a1cbed75fd4c6194b66 [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 *
45 * @param[in] aResp - Shared pointer for completing asynchronous calls.
46 *
47 * @return None.
48 */
zhanghch058d1b46d2021-04-01 11:18:24 +080049inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> aResp)
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 Tanous5e7e2dc2023-02-16 10:37:01 -080056 [aResp{std::move(aResp)}](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -070057 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 }
Andrew Geissler51dab2a2023-05-30 15:07:46 -040067 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -070068 messages::internalError(aResp->res);
69 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 {
76 aResp->res.jsonValue["PowerState"] = "On";
77 aResp->res.jsonValue["Status"]["State"] = "Enabled";
78 }
79 else if (chassisState ==
80 "xyz.openbmc_project.State.Chassis.PowerState.Off")
81 {
82 aResp->res.jsonValue["PowerState"] = "Off";
83 aResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
84 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070085 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060086}
87
zhanghch058d1b46d2021-04-01 11:18:24 +080088inline void getIntrusionByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
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 Tanous5e7e2dc2023-02-16 10:37:01 -080097 [aResp{std::move(aResp)}](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -070098 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 Tanous002d39b2022-05-31 08:59:27 -0700107 aResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] = 1;
108 aResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700109 });
Qiang XUc1819422019-02-27 13:51:32 +0800110}
111
112/**
113 * Retrieves physical security properties over dbus
114 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800115inline void getPhysicalSecurityData(std::shared_ptr<bmcweb::AsyncResp> aResp)
Qiang XUc1819422019-02-27 13:51:32 +0800116{
George Liue99073f2022-12-09 11:06:16 +0800117 constexpr std::array<std::string_view, 1> interfaces = {
118 "xyz.openbmc_project.Chassis.Intrusion"};
119 dbus::utility::getSubTree(
120 "/xyz/openbmc_project/Intrusion", 1, interfaces,
Qiang XUc1819422019-02-27 13:51:32 +0800121 [aResp{std::move(aResp)}](
George Liue99073f2022-12-09 11:06:16 +0800122 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800123 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700124 if (ec)
125 {
126 // do not add err msg in redfish response, because this is not
127 // mandatory property
128 BMCWEB_LOG_INFO << "DBUS error: no matched iface " << ec << "\n";
129 return;
130 }
131 // Iterate over all retrieved ObjectPaths.
132 for (const auto& object : subtree)
133 {
Patrick Williams840a9ff2023-05-12 10:18:43 -0500134 if (!object.second.empty())
Qiang XUc1819422019-02-27 13:51:32 +0800135 {
Patrick Williams840a9ff2023-05-12 10:18:43 -0500136 const auto service = object.second.front();
Ed Tanous002d39b2022-05-31 08:59:27 -0700137 getIntrusionByService(aResp, service.first, object.first);
Qiang XUc1819422019-02-27 13:51:32 +0800138 return;
139 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700140 }
George Liue99073f2022-12-09 11:06:16 +0800141 });
Qiang XUc1819422019-02-27 13:51:32 +0800142}
143
Nan Zhoucf7eba02022-07-21 23:53:20 +0000144inline void handleChassisCollectionGet(
145 App& app, const crow::Request& req,
146 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
147{
148 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
149 {
150 return;
151 }
152 asyncResp->res.jsonValue["@odata.type"] =
153 "#ChassisCollection.ChassisCollection";
154 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
155 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
156
George Liu7a1dbc42022-12-07 16:03:22 +0800157 constexpr std::array<std::string_view, 2> interfaces{
158 "xyz.openbmc_project.Inventory.Item.Board",
159 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000160 collection_util::getCollectionMembers(
George Liu7a1dbc42022-12-07 16:03:22 +0800161 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000162}
163
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100164/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100165 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700166 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100167 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700171 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700172 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000173 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700174}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100175
Willy Tu308f70c2021-09-28 20:24:52 -0700176inline void
177 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
178 const std::string& connectionName,
179 const std::string& path)
180{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700181 sdbusplus::asio::getProperty<std::string>(
182 *crow::connections::systemBus, connectionName, path,
183 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800184 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700185 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700186 if (ec)
187 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400188 BMCWEB_LOG_ERROR << "DBUS response error for Location";
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 messages::internalError(asyncResp->res);
190 return;
191 }
Willy Tu308f70c2021-09-28 20:24:52 -0700192
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
194 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700195 });
Willy Tu308f70c2021-09-28 20:24:52 -0700196}
197
198inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
199 const std::string& connectionName,
200 const std::string& path)
201{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700202 sdbusplus::asio::getProperty<std::string>(
203 *crow::connections::systemBus, connectionName, path,
204 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800205 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700206 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700207 if (ec)
208 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400209 BMCWEB_LOG_ERROR << "DBUS response error for UUID";
Ed Tanous002d39b2022-05-31 08:59:27 -0700210 messages::internalError(asyncResp->res);
211 return;
212 }
213 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700214 });
Willy Tu308f70c2021-09-28 20:24:52 -0700215}
216
Nan Zhoucf7eba02022-07-21 23:53:20 +0000217inline void
218 handleChassisGet(App& app, const crow::Request& req,
219 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
220 const std::string& chassisId)
221{
222 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
223 {
224 return;
225 }
George Liue99073f2022-12-09 11:06:16 +0800226 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000227 "xyz.openbmc_project.Inventory.Item.Board",
228 "xyz.openbmc_project.Inventory.Item.Chassis"};
229
George Liue99073f2022-12-09 11:06:16 +0800230 dbus::utility::getSubTree(
231 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000232 [asyncResp, chassisId(std::string(chassisId))](
George Liue99073f2022-12-09 11:06:16 +0800233 const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000234 const dbus::utility::MapperGetSubTreeResponse& subtree) {
235 if (ec)
236 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400237 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000238 messages::internalError(asyncResp->res);
239 return;
240 }
241 // Iterate over all retrieved ObjectPaths.
242 for (const std::pair<
243 std::string,
244 std::vector<std::pair<std::string, std::vector<std::string>>>>&
245 object : subtree)
246 {
247 const std::string& path = object.first;
248 const std::vector<std::pair<std::string, std::vector<std::string>>>&
249 connectionNames = object.second;
250
251 sdbusplus::message::object_path objPath(path);
252 if (objPath.filename() != chassisId)
253 {
254 continue;
255 }
256
257 auto health = std::make_shared<HealthPopulate>(asyncResp);
258
Willy Tu13451e32023-05-24 16:08:18 -0700259 if constexpr (bmcwebEnableHealthPopulate)
260 {
261 dbus::utility::getAssociationEndPoints(
262 path + "/all_sensors",
263 [health](const boost::system::error_code& ec2,
264 const dbus::utility::MapperEndPoints& resp) {
265 if (ec2)
266 {
267 return; // no sensors = no failures
268 }
269 health->inventory = resp;
270 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000271
Willy Tu13451e32023-05-24 16:08:18 -0700272 health->populate();
273 }
Nan Zhoucf7eba02022-07-21 23:53:20 +0000274
275 if (connectionNames.empty())
276 {
277 BMCWEB_LOG_ERROR << "Got 0 Connection names";
278 continue;
279 }
280
281 asyncResp->res.jsonValue["@odata.type"] =
Logananth Sundararaj523d4862023-01-24 11:56:28 +0530282 "#Chassis.v1_22_0.Chassis";
Nan Zhoucf7eba02022-07-21 23:53:20 +0000283 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700284 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000285 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
286 asyncResp->res.jsonValue["ChassisType"] = "RackMount";
287 asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700288 boost::urls::format(
289 "/redfish/v1/Chassis/{}/Actions/Chassis.Reset", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000290 asyncResp->res
291 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700292 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
293 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000294 asyncResp->res.jsonValue["PCIeDevices"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700295 "/redfish/v1/Systems/system/PCIeDevices";
Nan Zhoucf7eba02022-07-21 23:53:20 +0000296
George Liu6c3e9452023-03-03 13:55:29 +0800297 dbus::utility::getAssociationEndPoints(
298 path + "/drive",
299 [asyncResp,
300 chassisId](const boost::system::error_code& ec3,
301 const dbus::utility::MapperEndPoints& resp) {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000302 if (ec3 || resp.empty())
303 {
304 return; // no drives = no failures
305 }
306
307 nlohmann::json reference;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700308 reference["@odata.id"] = boost::urls::format(
309 "/redfish/v1/Chassis/{}/Drives", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000310 asyncResp->res.jsonValue["Drives"] = std::move(reference);
311 });
312
313 const std::string& connectionName = connectionNames[0].first;
314
315 const std::vector<std::string>& interfaces2 =
316 connectionNames[0].second;
317 const std::array<const char*, 2> hasIndicatorLed = {
318 "xyz.openbmc_project.Inventory.Item.Panel",
319 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
320
321 const std::string assetTagInterface =
322 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
Logananth Sundararaj523d4862023-01-24 11:56:28 +0530323 const std::string replaceableInterface =
324 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
325 for (const auto& interface : interfaces2)
Nan Zhoucf7eba02022-07-21 23:53:20 +0000326 {
Logananth Sundararaj523d4862023-01-24 11:56:28 +0530327 if (interface == assetTagInterface)
328 {
329 sdbusplus::asio::getProperty<std::string>(
330 *crow::connections::systemBus, connectionName, path,
331 assetTagInterface, "AssetTag",
332 [asyncResp,
333 chassisId](const boost::system::error_code& ec2,
334 const std::string& property) {
335 if (ec2)
336 {
337 BMCWEB_LOG_ERROR
338 << "DBus response error for AssetTag: " << ec2;
339 messages::internalError(asyncResp->res);
340 return;
341 }
342 asyncResp->res.jsonValue["AssetTag"] = property;
343 });
344 }
345 else if (interface == replaceableInterface)
346 {
347 sdbusplus::asio::getProperty<bool>(
348 *crow::connections::systemBus, connectionName, path,
349 replaceableInterface, "HotPluggable",
350 [asyncResp,
351 chassisId](const boost::system::error_code& ec2,
352 const bool property) {
353 if (ec2)
354 {
355 BMCWEB_LOG_ERROR
356 << "DBus response error for HotPluggable: "
357 << ec2;
358 messages::internalError(asyncResp->res);
359 return;
360 }
361 asyncResp->res.jsonValue["HotPluggable"] = property;
362 });
363 }
Nan Zhoucf7eba02022-07-21 23:53:20 +0000364 }
365
366 for (const char* interface : hasIndicatorLed)
367 {
368 if (std::find(interfaces2.begin(), interfaces2.end(),
369 interface) != interfaces2.end())
370 {
371 getIndicatorLedState(asyncResp);
372 getLocationIndicatorActive(asyncResp);
373 break;
374 }
375 }
376
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200377 sdbusplus::asio::getAllProperties(
378 *crow::connections::systemBus, connectionName, path,
379 "xyz.openbmc_project.Inventory.Decorator.Asset",
Nan Zhoucf7eba02022-07-21 23:53:20 +0000380 [asyncResp, chassisId(std::string(chassisId))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800381 const boost::system::error_code& /*ec2*/,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000382 const dbus::utility::DBusPropertiesMap& propertiesList) {
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200383 const std::string* partNumber = nullptr;
384 const std::string* serialNumber = nullptr;
385 const std::string* manufacturer = nullptr;
386 const std::string* model = nullptr;
387 const std::string* sparePartNumber = nullptr;
388
389 const bool success = sdbusplus::unpackPropertiesNoThrow(
390 dbus_utils::UnpackErrorPrinter(), propertiesList,
391 "PartNumber", partNumber, "SerialNumber", serialNumber,
392 "Manufacturer", manufacturer, "Model", model,
393 "SparePartNumber", sparePartNumber);
394
395 if (!success)
Nan Zhoucf7eba02022-07-21 23:53:20 +0000396 {
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200397 messages::internalError(asyncResp->res);
398 return;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000399 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200400
401 if (partNumber != nullptr)
402 {
403 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
404 }
405
406 if (serialNumber != nullptr)
407 {
408 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
409 }
410
411 if (manufacturer != nullptr)
412 {
413 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
414 }
415
416 if (model != nullptr)
417 {
418 asyncResp->res.jsonValue["Model"] = *model;
419 }
420
421 // SparePartNumber is optional on D-Bus
422 // so skip if it is empty
423 if (sparePartNumber != nullptr && !sparePartNumber->empty())
424 {
425 asyncResp->res.jsonValue["SparePartNumber"] =
426 *sparePartNumber;
427 }
428
Nan Zhoucf7eba02022-07-21 23:53:20 +0000429 asyncResp->res.jsonValue["Name"] = chassisId;
430 asyncResp->res.jsonValue["Id"] = chassisId;
431#ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
432 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700433 boost::urls::format("/redfish/v1/Chassis/{}/Thermal",
434 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000435 // Power object
436 asyncResp->res.jsonValue["Power"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700437 boost::urls::format("/redfish/v1/Chassis/{}/Power",
438 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000439#endif
Xiaochao Ma29739632021-03-02 15:53:13 +0800440#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
441 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700442 boost::urls::format(
443 "/redfish/v1/Chassis/{}/ThermalSubsystem", chassisId);
Chicago Duan77b36432021-02-05 15:48:26 +0800444 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700445 boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem",
446 chassisId);
Albert Zhang4ca3ec32021-06-13 14:39:38 +0800447 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700448 boost::urls::format(
449 "/redfish/v1/Chassis/{}/EnvironmentMetrics", chassisId);
Xiaochao Ma29739632021-03-02 15:53:13 +0800450#endif
Nan Zhoucf7eba02022-07-21 23:53:20 +0000451 // SensorCollection
452 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -0700453 boost::urls::format("/redfish/v1/Chassis/{}/Sensors",
454 chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000455 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
456
457 nlohmann::json::array_t computerSystems;
458 nlohmann::json::object_t system;
459 system["@odata.id"] = "/redfish/v1/Systems/system";
Patrick Williamsad539542023-05-12 10:10:08 -0500460 computerSystems.emplace_back(std::move(system));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000461 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
462 std::move(computerSystems);
463
464 nlohmann::json::array_t managedBy;
465 nlohmann::json::object_t manager;
466 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
Patrick Williamsad539542023-05-12 10:10:08 -0500467 managedBy.emplace_back(std::move(manager));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000468 asyncResp->res.jsonValue["Links"]["ManagedBy"] =
469 std::move(managedBy);
470 getChassisState(asyncResp);
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200471 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000472
473 for (const auto& interface : interfaces2)
474 {
475 if (interface == "xyz.openbmc_project.Common.UUID")
476 {
477 getChassisUUID(asyncResp, connectionName, path);
478 }
479 else if (interface ==
480 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
481 {
482 getChassisLocationCode(asyncResp, connectionName, path);
483 }
484 }
485
486 return;
487 }
488
489 // Couldn't find an object with that name. return an error
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800490 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
George Liue99073f2022-12-09 11:06:16 +0800491 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000492
493 getPhysicalSecurityData(asyncResp);
494}
495
496inline void
497 handleChassisPatch(App& app, const crow::Request& req,
498 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
499 const std::string& param)
500{
501 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
502 {
503 return;
504 }
505 std::optional<bool> locationIndicatorActive;
506 std::optional<std::string> indicatorLed;
507
508 if (param.empty())
509 {
510 return;
511 }
512
513 if (!json_util::readJsonPatch(
514 req, asyncResp->res, "LocationIndicatorActive",
515 locationIndicatorActive, "IndicatorLED", indicatorLed))
516 {
517 return;
518 }
519
520 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
521 if (!locationIndicatorActive && !indicatorLed)
522 {
523 return; // delete this when we support more patch properties
524 }
525 if (indicatorLed)
526 {
527 asyncResp->res.addHeader(
528 boost::beast::http::field::warning,
529 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
530 }
531
George Liue99073f2022-12-09 11:06:16 +0800532 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000533 "xyz.openbmc_project.Inventory.Item.Board",
534 "xyz.openbmc_project.Inventory.Item.Chassis"};
535
536 const std::string& chassisId = param;
537
George Liue99073f2022-12-09 11:06:16 +0800538 dbus::utility::getSubTree(
539 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000540 [asyncResp, chassisId, locationIndicatorActive,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800541 indicatorLed](const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000542 const dbus::utility::MapperGetSubTreeResponse& subtree) {
543 if (ec)
544 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400545 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000546 messages::internalError(asyncResp->res);
547 return;
548 }
549
550 // Iterate over all retrieved ObjectPaths.
551 for (const std::pair<
552 std::string,
553 std::vector<std::pair<std::string, std::vector<std::string>>>>&
554 object : subtree)
555 {
556 const std::string& path = object.first;
557 const std::vector<std::pair<std::string, std::vector<std::string>>>&
558 connectionNames = object.second;
559
560 sdbusplus::message::object_path objPath(path);
561 if (objPath.filename() != chassisId)
562 {
563 continue;
564 }
565
566 if (connectionNames.empty())
567 {
568 BMCWEB_LOG_ERROR << "Got 0 Connection names";
569 continue;
570 }
571
572 const std::vector<std::string>& interfaces3 =
573 connectionNames[0].second;
574
575 const std::array<const char*, 2> hasIndicatorLed = {
576 "xyz.openbmc_project.Inventory.Item.Panel",
577 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
578 bool indicatorChassis = false;
579 for (const char* interface : hasIndicatorLed)
580 {
581 if (std::find(interfaces3.begin(), interfaces3.end(),
582 interface) != interfaces3.end())
583 {
584 indicatorChassis = true;
585 break;
586 }
587 }
588 if (locationIndicatorActive)
589 {
590 if (indicatorChassis)
591 {
592 setLocationIndicatorActive(asyncResp,
593 *locationIndicatorActive);
594 }
595 else
596 {
597 messages::propertyUnknown(asyncResp->res,
598 "LocationIndicatorActive");
599 }
600 }
601 if (indicatorLed)
602 {
603 if (indicatorChassis)
604 {
605 setIndicatorLedState(asyncResp, *indicatorLed);
606 }
607 else
608 {
609 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
610 }
611 }
612 return;
613 }
614
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800615 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
George Liue99073f2022-12-09 11:06:16 +0800616 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000617}
618
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100619/**
620 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700621 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100622 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700623inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700624{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700625 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700626 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700627 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000628 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700629
630 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700631 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700632 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000633 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700634}
P.K. Leedd99e042020-06-17 19:43:16 +0800635
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400636/**
637 * Handle error responses from d-bus for chassis power cycles
638 */
639inline void handleChassisPowerCycleError(const boost::system::error_code& ec,
640 const sdbusplus::message_t& eMsg,
641 crow::Response& res)
642{
643 if (eMsg.get_error() == nullptr)
644 {
645 BMCWEB_LOG_ERROR << "D-Bus response error: " << ec;
646 messages::internalError(res);
647 return;
648 }
649 std::string_view errorMessage = eMsg.get_error()->name;
650
651 // If operation failed due to BMC not being in Ready state, tell
652 // user to retry in a bit
653 if (errorMessage ==
654 std::string_view("xyz.openbmc_project.State.Chassis.Error.BMCNotReady"))
655 {
656 BMCWEB_LOG_DEBUG << "BMC not ready, operation not allowed right now";
657 messages::serviceTemporarilyUnavailable(res, "10");
658 return;
659 }
660
661 BMCWEB_LOG_ERROR << "Chassis Power Cycle fail " << ec
662 << " sdbusplus:" << errorMessage;
663 messages::internalError(res);
664}
665
zhanghch058d1b46d2021-04-01 11:18:24 +0800666inline void
667 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800668{
George Liu7a1dbc42022-12-07 16:03:22 +0800669 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700670 "xyz.openbmc_project.State.Chassis"};
671
672 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800673 dbus::utility::getSubTreePaths(
674 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800675 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800676 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800677 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700678 if (ec)
679 {
Andrew Geissler51dab2a2023-05-30 15:07:46 -0400680 BMCWEB_LOG_ERROR << "[mapper] Bad D-Bus request error: " << ec;
Ed Tanous002d39b2022-05-31 08:59:27 -0700681 messages::internalError(asyncResp->res);
682 return;
683 }
684
685 const char* processName = "xyz.openbmc_project.State.Chassis";
686 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
687 const char* destProperty = "RequestedPowerTransition";
688 const std::string propertyValue =
689 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
690 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
691
692 /* Look for system reset chassis path */
693 if ((std::find(chassisList.begin(), chassisList.end(), objectPath)) ==
694 chassisList.end())
695 {
696 /* We prefer to reset the full chassis_system, but if it doesn't
697 * exist on some platforms, fall back to a host-only power reset
698 */
699 objectPath = "/xyz/openbmc_project/state/chassis0";
700 }
701
702 crow::connections::systemBus->async_method_call(
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400703 [asyncResp](const boost::system::error_code& ec2,
704 sdbusplus::message_t& sdbusErrMsg) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700705 // Use "Set" method to set the property value.
Ed Tanous8a592812022-06-04 09:06:59 -0700706 if (ec2)
P.K. Leedd99e042020-06-17 19:43:16 +0800707 {
Andrew Geisslerfc903b32023-05-31 14:15:42 -0400708 handleChassisPowerCycleError(ec2, sdbusErrMsg, asyncResp->res);
709
P.K. Leedd99e042020-06-17 19:43:16 +0800710 return;
711 }
712
Ed Tanous002d39b2022-05-31 08:59:27 -0700713 messages::success(asyncResp->res);
714 },
715 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
716 interfaceName, destProperty,
717 dbus::utility::DbusVariantType{propertyValue});
George Liu7a1dbc42022-12-07 16:03:22 +0800718 });
P.K. Leedd99e042020-06-17 19:43:16 +0800719}
720
Nan Zhoucf7eba02022-07-21 23:53:20 +0000721inline void handleChassisResetActionInfoPost(
722 App& app, const crow::Request& req,
723 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
724 const std::string& /*chassisId*/)
725{
726 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
727 {
728 return;
729 }
730 BMCWEB_LOG_DEBUG << "Post Chassis Reset.";
731
732 std::string resetType;
733
734 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
735 {
736 return;
737 }
738
739 if (resetType != "PowerCycle")
740 {
741 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
742 << resetType;
743 messages::actionParameterNotSupported(asyncResp->res, resetType,
744 "ResetType");
745
746 return;
747 }
748 doChassisPowerCycle(asyncResp);
749}
750
P.K. Leedd99e042020-06-17 19:43:16 +0800751/**
752 * ChassisResetAction class supports the POST method for the Reset
753 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700754 * Function handles POST method request.
755 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800756 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700757
758inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800759{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700760 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700761 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700762 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000763 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
764}
P.K. Leedd99e042020-06-17 19:43:16 +0800765
Nan Zhoucf7eba02022-07-21 23:53:20 +0000766inline void handleChassisResetActionInfoGet(
767 App& app, const crow::Request& req,
768 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
769 const std::string& chassisId)
770{
771 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
772 {
773 return;
774 }
775 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700776 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
777 "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000778 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800779
Nan Zhoucf7eba02022-07-21 23:53:20 +0000780 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
781 nlohmann::json::array_t parameters;
782 nlohmann::json::object_t parameter;
783 parameter["Name"] = "ResetType";
784 parameter["Required"] = true;
785 parameter["DataType"] = "String";
786 nlohmann::json::array_t allowed;
Patrick Williamsad539542023-05-12 10:10:08 -0500787 allowed.emplace_back("PowerCycle");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000788 parameter["AllowableValues"] = std::move(allowed);
Patrick Williamsad539542023-05-12 10:10:08 -0500789 parameters.emplace_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800790
Nan Zhoucf7eba02022-07-21 23:53:20 +0000791 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700792}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530793
794/**
795 * ChassisResetActionInfo derived class for delivering Chassis
796 * ResetType AllowableValues using ResetInfo schema.
797 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700798inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530799{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700800 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700801 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700802 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000803 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700804}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530805
Ed Tanous1abe55e2018-09-05 08:30:59 -0700806} // namespace redfish