blob: 81f82543ec5134f48b8e67929c93b9dec7c69903 [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 Feistb49ac872019-05-21 15:12:01 -070020#include "health.hpp"
James Feist1c8fba92019-12-20 15:12:07 -080021#include "led.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#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
Jonathan Doman1e1e5982021-06-11 09:36:17 -070028#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020029#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050030
George Liu7a1dbc42022-12-07 16:03:22 +080031#include <array>
32#include <string_view>
33
Ed Tanous1abe55e2018-09-05 08:30:59 -070034namespace redfish
35{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010036
37/**
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060038 * @brief Retrieves chassis state properties over dbus
39 *
40 * @param[in] aResp - Shared pointer for completing asynchronous calls.
41 *
42 * @return None.
43 */
zhanghch058d1b46d2021-04-01 11:18:24 +080044inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> aResp)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060045{
Jonathan Doman1e1e5982021-06-11 09:36:17 -070046 // crow::connections::systemBus->async_method_call(
47 sdbusplus::asio::getProperty<std::string>(
48 *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
49 "/xyz/openbmc_project/state/chassis0",
50 "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
51 [aResp{std::move(aResp)}](const boost::system::error_code ec,
52 const std::string& chassisState) {
Ed Tanous002d39b2022-05-31 08:59:27 -070053 if (ec)
54 {
55 if (ec == boost::system::errc::host_unreachable)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060056 {
Ed Tanous002d39b2022-05-31 08:59:27 -070057 // Service not available, no error, just don't return
58 // chassis state info
59 BMCWEB_LOG_DEBUG << "Service not available " << ec;
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060060 return;
61 }
Ed Tanous002d39b2022-05-31 08:59:27 -070062 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
63 messages::internalError(aResp->res);
64 return;
65 }
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060066
Ed Tanous002d39b2022-05-31 08:59:27 -070067 BMCWEB_LOG_DEBUG << "Chassis state: " << chassisState;
68 // Verify Chassis State
69 if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
70 {
71 aResp->res.jsonValue["PowerState"] = "On";
72 aResp->res.jsonValue["Status"]["State"] = "Enabled";
73 }
74 else if (chassisState ==
75 "xyz.openbmc_project.State.Chassis.PowerState.Off")
76 {
77 aResp->res.jsonValue["PowerState"] = "Off";
78 aResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
79 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070080 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060081}
82
zhanghch058d1b46d2021-04-01 11:18:24 +080083inline void getIntrusionByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
Ed Tanous23a21a12020-07-25 04:45:05 +000084 const std::string& service,
85 const std::string& objPath)
Qiang XUc1819422019-02-27 13:51:32 +080086{
87 BMCWEB_LOG_DEBUG << "Get intrusion status by service \n";
88
Jonathan Doman1e1e5982021-06-11 09:36:17 -070089 sdbusplus::asio::getProperty<std::string>(
90 *crow::connections::systemBus, service, objPath,
91 "xyz.openbmc_project.Chassis.Intrusion", "Status",
Qiang XUc1819422019-02-27 13:51:32 +080092 [aResp{std::move(aResp)}](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -070093 const std::string& value) {
Ed Tanous002d39b2022-05-31 08:59:27 -070094 if (ec)
95 {
96 // do not add err msg in redfish response, because this is not
97 // mandatory property
98 BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n";
99 return;
100 }
Qiang XUc1819422019-02-27 13:51:32 +0800101
Ed Tanous002d39b2022-05-31 08:59:27 -0700102 aResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] = 1;
103 aResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700104 });
Qiang XUc1819422019-02-27 13:51:32 +0800105}
106
107/**
108 * Retrieves physical security properties over dbus
109 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800110inline void getPhysicalSecurityData(std::shared_ptr<bmcweb::AsyncResp> aResp)
Qiang XUc1819422019-02-27 13:51:32 +0800111{
112 crow::connections::systemBus->async_method_call(
113 [aResp{std::move(aResp)}](
114 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800115 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700116 if (ec)
117 {
118 // do not add err msg in redfish response, because this is not
119 // mandatory property
120 BMCWEB_LOG_INFO << "DBUS error: no matched iface " << ec << "\n";
121 return;
122 }
123 // Iterate over all retrieved ObjectPaths.
124 for (const auto& object : subtree)
125 {
126 for (const auto& service : object.second)
Qiang XUc1819422019-02-27 13:51:32 +0800127 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700128 getIntrusionByService(aResp, service.first, object.first);
Qiang XUc1819422019-02-27 13:51:32 +0800129 return;
130 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700131 }
Qiang XUc1819422019-02-27 13:51:32 +0800132 },
133 "xyz.openbmc_project.ObjectMapper",
134 "/xyz/openbmc_project/object_mapper",
135 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
Ed Tanous271584a2019-07-09 16:24:22 -0700136 "/xyz/openbmc_project/Intrusion", 1,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500137 std::array<const char*, 1>{"xyz.openbmc_project.Chassis.Intrusion"});
Qiang XUc1819422019-02-27 13:51:32 +0800138}
139
Nan Zhoucf7eba02022-07-21 23:53:20 +0000140inline void handleChassisCollectionGet(
141 App& app, const crow::Request& req,
142 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
143{
144 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
145 {
146 return;
147 }
148 asyncResp->res.jsonValue["@odata.type"] =
149 "#ChassisCollection.ChassisCollection";
150 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
151 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
152
George Liu7a1dbc42022-12-07 16:03:22 +0800153 constexpr std::array<std::string_view, 2> interfaces{
154 "xyz.openbmc_project.Inventory.Item.Board",
155 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000156 collection_util::getCollectionMembers(
George Liu7a1dbc42022-12-07 16:03:22 +0800157 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000158}
159
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100160/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100161 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700162 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100163 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700164inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700166 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700167 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000169 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100171
Willy Tu308f70c2021-09-28 20:24:52 -0700172inline void
173 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
174 const std::string& connectionName,
175 const std::string& path)
176{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700177 sdbusplus::asio::getProperty<std::string>(
178 *crow::connections::systemBus, connectionName, path,
179 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Willy Tu308f70c2021-09-28 20:24:52 -0700180 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700181 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700182 if (ec)
183 {
184 BMCWEB_LOG_DEBUG << "DBUS response error for Location";
185 messages::internalError(asyncResp->res);
186 return;
187 }
Willy Tu308f70c2021-09-28 20:24:52 -0700188
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
190 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700191 });
Willy Tu308f70c2021-09-28 20:24:52 -0700192}
193
194inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
195 const std::string& connectionName,
196 const std::string& path)
197{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700198 sdbusplus::asio::getProperty<std::string>(
199 *crow::connections::systemBus, connectionName, path,
200 "xyz.openbmc_project.Common.UUID", "UUID",
Willy Tu308f70c2021-09-28 20:24:52 -0700201 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700202 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700203 if (ec)
204 {
205 BMCWEB_LOG_DEBUG << "DBUS response error for UUID";
206 messages::internalError(asyncResp->res);
207 return;
208 }
209 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700210 });
Willy Tu308f70c2021-09-28 20:24:52 -0700211}
212
Nan Zhoucf7eba02022-07-21 23:53:20 +0000213inline void
214 handleChassisGet(App& app, const crow::Request& req,
215 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216 const std::string& chassisId)
217{
218 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
219 {
220 return;
221 }
222 const std::array<const char*, 2> interfaces = {
223 "xyz.openbmc_project.Inventory.Item.Board",
224 "xyz.openbmc_project.Inventory.Item.Chassis"};
225
226 crow::connections::systemBus->async_method_call(
227 [asyncResp, chassisId(std::string(chassisId))](
228 const boost::system::error_code ec,
229 const dbus::utility::MapperGetSubTreeResponse& subtree) {
230 if (ec)
231 {
232 messages::internalError(asyncResp->res);
233 return;
234 }
235 // Iterate over all retrieved ObjectPaths.
236 for (const std::pair<
237 std::string,
238 std::vector<std::pair<std::string, std::vector<std::string>>>>&
239 object : subtree)
240 {
241 const std::string& path = object.first;
242 const std::vector<std::pair<std::string, std::vector<std::string>>>&
243 connectionNames = object.second;
244
245 sdbusplus::message::object_path objPath(path);
246 if (objPath.filename() != chassisId)
247 {
248 continue;
249 }
250
251 auto health = std::make_shared<HealthPopulate>(asyncResp);
252
253 sdbusplus::asio::getProperty<std::vector<std::string>>(
254 *crow::connections::systemBus,
255 "xyz.openbmc_project.ObjectMapper", path + "/all_sensors",
256 "xyz.openbmc_project.Association", "endpoints",
257 [health](const boost::system::error_code ec2,
258 const std::vector<std::string>& resp) {
259 if (ec2)
260 {
261 return; // no sensors = no failures
262 }
263 health->inventory = resp;
264 });
265
266 health->populate();
267
268 if (connectionNames.empty())
269 {
270 BMCWEB_LOG_ERROR << "Got 0 Connection names";
271 continue;
272 }
273
274 asyncResp->res.jsonValue["@odata.type"] =
275 "#Chassis.v1_16_0.Chassis";
276 asyncResp->res.jsonValue["@odata.id"] =
277 "/redfish/v1/Chassis/" + chassisId;
278 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
279 asyncResp->res.jsonValue["ChassisType"] = "RackMount";
280 asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
281 "/redfish/v1/Chassis/" + chassisId + "/Actions/Chassis.Reset";
282 asyncResp->res
283 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
284 "/redfish/v1/Chassis/" + chassisId + "/ResetActionInfo";
285 asyncResp->res.jsonValue["PCIeDevices"]["@odata.id"] =
286 "/redfish/v1/Systems/system/PCIeDevices";
287
288 sdbusplus::asio::getProperty<std::vector<std::string>>(
289 *crow::connections::systemBus,
290 "xyz.openbmc_project.ObjectMapper", path + "/drive",
291 "xyz.openbmc_project.Association", "endpoints",
292 [asyncResp, chassisId](const boost::system::error_code ec3,
293 const std::vector<std::string>& resp) {
294 if (ec3 || resp.empty())
295 {
296 return; // no drives = no failures
297 }
298
299 nlohmann::json reference;
John Edward Broadbenta0cb40c2022-06-29 17:27:38 -0700300 reference["@odata.id"] = crow::utility::urlFromPieces(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000301 "redfish", "v1", "Chassis", chassisId, "Drives");
302 asyncResp->res.jsonValue["Drives"] = std::move(reference);
303 });
304
305 const std::string& connectionName = connectionNames[0].first;
306
307 const std::vector<std::string>& interfaces2 =
308 connectionNames[0].second;
309 const std::array<const char*, 2> hasIndicatorLed = {
310 "xyz.openbmc_project.Inventory.Item.Panel",
311 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
312
313 const std::string assetTagInterface =
314 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
315 if (std::find(interfaces2.begin(), interfaces2.end(),
316 assetTagInterface) != interfaces2.end())
317 {
318 sdbusplus::asio::getProperty<std::string>(
319 *crow::connections::systemBus, connectionName, path,
320 assetTagInterface, "AssetTag",
321 [asyncResp, chassisId(std::string(chassisId))](
322 const boost::system::error_code ec2,
323 const std::string& property) {
324 if (ec2)
325 {
326 BMCWEB_LOG_DEBUG << "DBus response error for AssetTag";
327 messages::internalError(asyncResp->res);
328 return;
329 }
330 asyncResp->res.jsonValue["AssetTag"] = property;
331 });
332 }
333
334 for (const char* interface : hasIndicatorLed)
335 {
336 if (std::find(interfaces2.begin(), interfaces2.end(),
337 interface) != interfaces2.end())
338 {
339 getIndicatorLedState(asyncResp);
340 getLocationIndicatorActive(asyncResp);
341 break;
342 }
343 }
344
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200345 sdbusplus::asio::getAllProperties(
346 *crow::connections::systemBus, connectionName, path,
347 "xyz.openbmc_project.Inventory.Decorator.Asset",
Nan Zhoucf7eba02022-07-21 23:53:20 +0000348 [asyncResp, chassisId(std::string(chassisId))](
349 const boost::system::error_code /*ec2*/,
350 const dbus::utility::DBusPropertiesMap& propertiesList) {
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200351 const std::string* partNumber = nullptr;
352 const std::string* serialNumber = nullptr;
353 const std::string* manufacturer = nullptr;
354 const std::string* model = nullptr;
355 const std::string* sparePartNumber = nullptr;
356
357 const bool success = sdbusplus::unpackPropertiesNoThrow(
358 dbus_utils::UnpackErrorPrinter(), propertiesList,
359 "PartNumber", partNumber, "SerialNumber", serialNumber,
360 "Manufacturer", manufacturer, "Model", model,
361 "SparePartNumber", sparePartNumber);
362
363 if (!success)
Nan Zhoucf7eba02022-07-21 23:53:20 +0000364 {
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200365 messages::internalError(asyncResp->res);
366 return;
Nan Zhoucf7eba02022-07-21 23:53:20 +0000367 }
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200368
369 if (partNumber != nullptr)
370 {
371 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
372 }
373
374 if (serialNumber != nullptr)
375 {
376 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
377 }
378
379 if (manufacturer != nullptr)
380 {
381 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
382 }
383
384 if (model != nullptr)
385 {
386 asyncResp->res.jsonValue["Model"] = *model;
387 }
388
389 // SparePartNumber is optional on D-Bus
390 // so skip if it is empty
391 if (sparePartNumber != nullptr && !sparePartNumber->empty())
392 {
393 asyncResp->res.jsonValue["SparePartNumber"] =
394 *sparePartNumber;
395 }
396
Nan Zhoucf7eba02022-07-21 23:53:20 +0000397 asyncResp->res.jsonValue["Name"] = chassisId;
398 asyncResp->res.jsonValue["Id"] = chassisId;
399#ifdef BMCWEB_ALLOW_DEPRECATED_POWER_THERMAL
400 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
401 "/redfish/v1/Chassis/" + chassisId + "/Thermal";
402 // Power object
403 asyncResp->res.jsonValue["Power"]["@odata.id"] =
404 "/redfish/v1/Chassis/" + chassisId + "/Power";
405#endif
Xiaochao Ma29739632021-03-02 15:53:13 +0800406#ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
407 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
408 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
409 chassisId, "ThermalSubsystem");
Chicago Duan77b36432021-02-05 15:48:26 +0800410 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
411 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
412 chassisId, "PowerSubsystem");
Albert Zhang4ca3ec32021-06-13 14:39:38 +0800413 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
414 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
415 chassisId,
416 "EnvironmentMetrics");
Xiaochao Ma29739632021-03-02 15:53:13 +0800417#endif
Nan Zhoucf7eba02022-07-21 23:53:20 +0000418 // SensorCollection
419 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
420 "/redfish/v1/Chassis/" + chassisId + "/Sensors";
421 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
422
423 nlohmann::json::array_t computerSystems;
424 nlohmann::json::object_t system;
425 system["@odata.id"] = "/redfish/v1/Systems/system";
426 computerSystems.push_back(std::move(system));
427 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
428 std::move(computerSystems);
429
430 nlohmann::json::array_t managedBy;
431 nlohmann::json::object_t manager;
432 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
433 managedBy.push_back(std::move(manager));
434 asyncResp->res.jsonValue["Links"]["ManagedBy"] =
435 std::move(managedBy);
436 getChassisState(asyncResp);
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +0200437 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000438
439 for (const auto& interface : interfaces2)
440 {
441 if (interface == "xyz.openbmc_project.Common.UUID")
442 {
443 getChassisUUID(asyncResp, connectionName, path);
444 }
445 else if (interface ==
446 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
447 {
448 getChassisLocationCode(asyncResp, connectionName, path);
449 }
450 }
451
452 return;
453 }
454
455 // Couldn't find an object with that name. return an error
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800456 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000457 },
458 "xyz.openbmc_project.ObjectMapper",
459 "/xyz/openbmc_project/object_mapper",
460 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
461 "/xyz/openbmc_project/inventory", 0, interfaces);
462
463 getPhysicalSecurityData(asyncResp);
464}
465
466inline void
467 handleChassisPatch(App& app, const crow::Request& req,
468 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
469 const std::string& param)
470{
471 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
472 {
473 return;
474 }
475 std::optional<bool> locationIndicatorActive;
476 std::optional<std::string> indicatorLed;
477
478 if (param.empty())
479 {
480 return;
481 }
482
483 if (!json_util::readJsonPatch(
484 req, asyncResp->res, "LocationIndicatorActive",
485 locationIndicatorActive, "IndicatorLED", indicatorLed))
486 {
487 return;
488 }
489
490 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
491 if (!locationIndicatorActive && !indicatorLed)
492 {
493 return; // delete this when we support more patch properties
494 }
495 if (indicatorLed)
496 {
497 asyncResp->res.addHeader(
498 boost::beast::http::field::warning,
499 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
500 }
501
502 const std::array<const char*, 2> interfaces = {
503 "xyz.openbmc_project.Inventory.Item.Board",
504 "xyz.openbmc_project.Inventory.Item.Chassis"};
505
506 const std::string& chassisId = param;
507
508 crow::connections::systemBus->async_method_call(
509 [asyncResp, chassisId, locationIndicatorActive,
510 indicatorLed](const boost::system::error_code ec,
511 const dbus::utility::MapperGetSubTreeResponse& subtree) {
512 if (ec)
513 {
514 messages::internalError(asyncResp->res);
515 return;
516 }
517
518 // Iterate over all retrieved ObjectPaths.
519 for (const std::pair<
520 std::string,
521 std::vector<std::pair<std::string, std::vector<std::string>>>>&
522 object : subtree)
523 {
524 const std::string& path = object.first;
525 const std::vector<std::pair<std::string, std::vector<std::string>>>&
526 connectionNames = object.second;
527
528 sdbusplus::message::object_path objPath(path);
529 if (objPath.filename() != chassisId)
530 {
531 continue;
532 }
533
534 if (connectionNames.empty())
535 {
536 BMCWEB_LOG_ERROR << "Got 0 Connection names";
537 continue;
538 }
539
540 const std::vector<std::string>& interfaces3 =
541 connectionNames[0].second;
542
543 const std::array<const char*, 2> hasIndicatorLed = {
544 "xyz.openbmc_project.Inventory.Item.Panel",
545 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
546 bool indicatorChassis = false;
547 for (const char* interface : hasIndicatorLed)
548 {
549 if (std::find(interfaces3.begin(), interfaces3.end(),
550 interface) != interfaces3.end())
551 {
552 indicatorChassis = true;
553 break;
554 }
555 }
556 if (locationIndicatorActive)
557 {
558 if (indicatorChassis)
559 {
560 setLocationIndicatorActive(asyncResp,
561 *locationIndicatorActive);
562 }
563 else
564 {
565 messages::propertyUnknown(asyncResp->res,
566 "LocationIndicatorActive");
567 }
568 }
569 if (indicatorLed)
570 {
571 if (indicatorChassis)
572 {
573 setIndicatorLedState(asyncResp, *indicatorLed);
574 }
575 else
576 {
577 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
578 }
579 }
580 return;
581 }
582
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800583 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000584 },
585 "xyz.openbmc_project.ObjectMapper",
586 "/xyz/openbmc_project/object_mapper",
587 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
588 "/xyz/openbmc_project/inventory", 0, interfaces);
589}
590
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100591/**
592 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700593 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100594 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700595inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700596{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700597 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700598 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000600 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700601
602 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700603 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700604 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000605 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700606}
P.K. Leedd99e042020-06-17 19:43:16 +0800607
zhanghch058d1b46d2021-04-01 11:18:24 +0800608inline void
609 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800610{
George Liu7a1dbc42022-12-07 16:03:22 +0800611 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700612 "xyz.openbmc_project.State.Chassis"};
613
614 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800615 dbus::utility::getSubTreePaths(
616 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800617 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800618 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800619 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700620 if (ec)
621 {
622 BMCWEB_LOG_DEBUG << "[mapper] Bad D-Bus request error: " << ec;
623 messages::internalError(asyncResp->res);
624 return;
625 }
626
627 const char* processName = "xyz.openbmc_project.State.Chassis";
628 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
629 const char* destProperty = "RequestedPowerTransition";
630 const std::string propertyValue =
631 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
632 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
633
634 /* Look for system reset chassis path */
635 if ((std::find(chassisList.begin(), chassisList.end(), objectPath)) ==
636 chassisList.end())
637 {
638 /* We prefer to reset the full chassis_system, but if it doesn't
639 * exist on some platforms, fall back to a host-only power reset
640 */
641 objectPath = "/xyz/openbmc_project/state/chassis0";
642 }
643
644 crow::connections::systemBus->async_method_call(
Ed Tanous8a592812022-06-04 09:06:59 -0700645 [asyncResp](const boost::system::error_code ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700646 // Use "Set" method to set the property value.
Ed Tanous8a592812022-06-04 09:06:59 -0700647 if (ec2)
P.K. Leedd99e042020-06-17 19:43:16 +0800648 {
Ed Tanous8a592812022-06-04 09:06:59 -0700649 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec2;
P.K. Leedd99e042020-06-17 19:43:16 +0800650 messages::internalError(asyncResp->res);
651 return;
652 }
653
Ed Tanous002d39b2022-05-31 08:59:27 -0700654 messages::success(asyncResp->res);
655 },
656 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
657 interfaceName, destProperty,
658 dbus::utility::DbusVariantType{propertyValue});
George Liu7a1dbc42022-12-07 16:03:22 +0800659 });
P.K. Leedd99e042020-06-17 19:43:16 +0800660}
661
Nan Zhoucf7eba02022-07-21 23:53:20 +0000662inline void handleChassisResetActionInfoPost(
663 App& app, const crow::Request& req,
664 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
665 const std::string& /*chassisId*/)
666{
667 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
668 {
669 return;
670 }
671 BMCWEB_LOG_DEBUG << "Post Chassis Reset.";
672
673 std::string resetType;
674
675 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
676 {
677 return;
678 }
679
680 if (resetType != "PowerCycle")
681 {
682 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
683 << resetType;
684 messages::actionParameterNotSupported(asyncResp->res, resetType,
685 "ResetType");
686
687 return;
688 }
689 doChassisPowerCycle(asyncResp);
690}
691
P.K. Leedd99e042020-06-17 19:43:16 +0800692/**
693 * ChassisResetAction class supports the POST method for the Reset
694 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700695 * Function handles POST method request.
696 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800697 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700698
699inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800700{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700701 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700702 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700703 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000704 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
705}
P.K. Leedd99e042020-06-17 19:43:16 +0800706
Nan Zhoucf7eba02022-07-21 23:53:20 +0000707inline void handleChassisResetActionInfoGet(
708 App& app, const crow::Request& req,
709 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
710 const std::string& chassisId)
711{
712 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
713 {
714 return;
715 }
716 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
717 asyncResp->res.jsonValue["@odata.id"] =
718 "/redfish/v1/Chassis/" + chassisId + "/ResetActionInfo";
719 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800720
Nan Zhoucf7eba02022-07-21 23:53:20 +0000721 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
722 nlohmann::json::array_t parameters;
723 nlohmann::json::object_t parameter;
724 parameter["Name"] = "ResetType";
725 parameter["Required"] = true;
726 parameter["DataType"] = "String";
727 nlohmann::json::array_t allowed;
728 allowed.push_back("PowerCycle");
729 parameter["AllowableValues"] = std::move(allowed);
730 parameters.push_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800731
Nan Zhoucf7eba02022-07-21 23:53:20 +0000732 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700733}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530734
735/**
736 * ChassisResetActionInfo derived class for delivering Chassis
737 * ResetType AllowableValues using ResetInfo schema.
738 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700739inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530740{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700741 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700742 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700743 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000744 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700745}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530746
Ed Tanous1abe55e2018-09-05 08:30:59 -0700747} // namespace redfish