blob: 54893516d49751d8a0823621391573d89fd79f4e [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
George Liue99073f2022-12-09 11:06:16 +080028#include <boost/system/error_code.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070029#include <sdbusplus/asio/property.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020030#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031
George Liu7a1dbc42022-12-07 16:03:22 +080032#include <array>
33#include <string_view>
34
Ed Tanous1abe55e2018-09-05 08:30:59 -070035namespace redfish
36{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010037
38/**
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060039 * @brief Retrieves chassis state properties over dbus
40 *
41 * @param[in] aResp - Shared pointer for completing asynchronous calls.
42 *
43 * @return None.
44 */
zhanghch058d1b46d2021-04-01 11:18:24 +080045inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> aResp)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060046{
Jonathan Doman1e1e5982021-06-11 09:36:17 -070047 // crow::connections::systemBus->async_method_call(
48 sdbusplus::asio::getProperty<std::string>(
49 *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
50 "/xyz/openbmc_project/state/chassis0",
51 "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
52 [aResp{std::move(aResp)}](const boost::system::error_code ec,
53 const std::string& chassisState) {
Ed Tanous002d39b2022-05-31 08:59:27 -070054 if (ec)
55 {
56 if (ec == boost::system::errc::host_unreachable)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060057 {
Ed Tanous002d39b2022-05-31 08:59:27 -070058 // Service not available, no error, just don't return
59 // chassis state info
60 BMCWEB_LOG_DEBUG << "Service not available " << ec;
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060061 return;
62 }
Ed Tanous002d39b2022-05-31 08:59:27 -070063 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
64 messages::internalError(aResp->res);
65 return;
66 }
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060067
Ed Tanous002d39b2022-05-31 08:59:27 -070068 BMCWEB_LOG_DEBUG << "Chassis state: " << chassisState;
69 // Verify Chassis State
70 if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
71 {
72 aResp->res.jsonValue["PowerState"] = "On";
73 aResp->res.jsonValue["Status"]["State"] = "Enabled";
74 }
75 else if (chassisState ==
76 "xyz.openbmc_project.State.Chassis.PowerState.Off")
77 {
78 aResp->res.jsonValue["PowerState"] = "Off";
79 aResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
80 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070081 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060082}
83
zhanghch058d1b46d2021-04-01 11:18:24 +080084inline void getIntrusionByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
Ed Tanous23a21a12020-07-25 04:45:05 +000085 const std::string& service,
86 const std::string& objPath)
Qiang XUc1819422019-02-27 13:51:32 +080087{
88 BMCWEB_LOG_DEBUG << "Get intrusion status by service \n";
89
Jonathan Doman1e1e5982021-06-11 09:36:17 -070090 sdbusplus::asio::getProperty<std::string>(
91 *crow::connections::systemBus, service, objPath,
92 "xyz.openbmc_project.Chassis.Intrusion", "Status",
Qiang XUc1819422019-02-27 13:51:32 +080093 [aResp{std::move(aResp)}](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -070094 const std::string& value) {
Ed Tanous002d39b2022-05-31 08:59:27 -070095 if (ec)
96 {
97 // do not add err msg in redfish response, because this is not
98 // mandatory property
99 BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n";
100 return;
101 }
Qiang XUc1819422019-02-27 13:51:32 +0800102
Ed Tanous002d39b2022-05-31 08:59:27 -0700103 aResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] = 1;
104 aResp->res.jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700105 });
Qiang XUc1819422019-02-27 13:51:32 +0800106}
107
108/**
109 * Retrieves physical security properties over dbus
110 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800111inline void getPhysicalSecurityData(std::shared_ptr<bmcweb::AsyncResp> aResp)
Qiang XUc1819422019-02-27 13:51:32 +0800112{
George Liue99073f2022-12-09 11:06:16 +0800113 constexpr std::array<std::string_view, 1> interfaces = {
114 "xyz.openbmc_project.Chassis.Intrusion"};
115 dbus::utility::getSubTree(
116 "/xyz/openbmc_project/Intrusion", 1, interfaces,
Qiang XUc1819422019-02-27 13:51:32 +0800117 [aResp{std::move(aResp)}](
George Liue99073f2022-12-09 11:06:16 +0800118 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800119 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700120 if (ec)
121 {
122 // do not add err msg in redfish response, because this is not
123 // mandatory property
124 BMCWEB_LOG_INFO << "DBUS error: no matched iface " << ec << "\n";
125 return;
126 }
127 // Iterate over all retrieved ObjectPaths.
128 for (const auto& object : subtree)
129 {
130 for (const auto& service : object.second)
Qiang XUc1819422019-02-27 13:51:32 +0800131 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 getIntrusionByService(aResp, service.first, object.first);
Qiang XUc1819422019-02-27 13:51:32 +0800133 return;
134 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700135 }
George Liue99073f2022-12-09 11:06:16 +0800136 });
Qiang XUc1819422019-02-27 13:51:32 +0800137}
138
Nan Zhoucf7eba02022-07-21 23:53:20 +0000139inline void handleChassisCollectionGet(
140 App& app, const crow::Request& req,
141 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
142{
143 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
144 {
145 return;
146 }
147 asyncResp->res.jsonValue["@odata.type"] =
148 "#ChassisCollection.ChassisCollection";
149 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
150 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
151
George Liu7a1dbc42022-12-07 16:03:22 +0800152 constexpr std::array<std::string_view, 2> interfaces{
153 "xyz.openbmc_project.Inventory.Item.Board",
154 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000155 collection_util::getCollectionMembers(
George Liu7a1dbc42022-12-07 16:03:22 +0800156 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000157}
158
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100159/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100160 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700161 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100162 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700163inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700166 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700167 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000168 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100170
Willy Tu308f70c2021-09-28 20:24:52 -0700171inline void
172 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
173 const std::string& connectionName,
174 const std::string& path)
175{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700176 sdbusplus::asio::getProperty<std::string>(
177 *crow::connections::systemBus, connectionName, path,
178 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Willy Tu308f70c2021-09-28 20:24:52 -0700179 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700180 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700181 if (ec)
182 {
183 BMCWEB_LOG_DEBUG << "DBUS response error for Location";
184 messages::internalError(asyncResp->res);
185 return;
186 }
Willy Tu308f70c2021-09-28 20:24:52 -0700187
Ed Tanous002d39b2022-05-31 08:59:27 -0700188 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
189 property;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700190 });
Willy Tu308f70c2021-09-28 20:24:52 -0700191}
192
193inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194 const std::string& connectionName,
195 const std::string& path)
196{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700197 sdbusplus::asio::getProperty<std::string>(
198 *crow::connections::systemBus, connectionName, path,
199 "xyz.openbmc_project.Common.UUID", "UUID",
Willy Tu308f70c2021-09-28 20:24:52 -0700200 [asyncResp](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700201 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700202 if (ec)
203 {
204 BMCWEB_LOG_DEBUG << "DBUS response error for UUID";
205 messages::internalError(asyncResp->res);
206 return;
207 }
208 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700209 });
Willy Tu308f70c2021-09-28 20:24:52 -0700210}
211
Nan Zhoucf7eba02022-07-21 23:53:20 +0000212inline void
213 handleChassisGet(App& app, const crow::Request& req,
214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
215 const std::string& chassisId)
216{
217 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
218 {
219 return;
220 }
George Liue99073f2022-12-09 11:06:16 +0800221 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000222 "xyz.openbmc_project.Inventory.Item.Board",
223 "xyz.openbmc_project.Inventory.Item.Chassis"};
224
George Liue99073f2022-12-09 11:06:16 +0800225 dbus::utility::getSubTree(
226 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000227 [asyncResp, chassisId(std::string(chassisId))](
George Liue99073f2022-12-09 11:06:16 +0800228 const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000229 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);
George Liue99073f2022-12-09 11:06:16 +0800457 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000458
459 getPhysicalSecurityData(asyncResp);
460}
461
462inline void
463 handleChassisPatch(App& app, const crow::Request& req,
464 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
465 const std::string& param)
466{
467 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
468 {
469 return;
470 }
471 std::optional<bool> locationIndicatorActive;
472 std::optional<std::string> indicatorLed;
473
474 if (param.empty())
475 {
476 return;
477 }
478
479 if (!json_util::readJsonPatch(
480 req, asyncResp->res, "LocationIndicatorActive",
481 locationIndicatorActive, "IndicatorLED", indicatorLed))
482 {
483 return;
484 }
485
486 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
487 if (!locationIndicatorActive && !indicatorLed)
488 {
489 return; // delete this when we support more patch properties
490 }
491 if (indicatorLed)
492 {
493 asyncResp->res.addHeader(
494 boost::beast::http::field::warning,
495 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
496 }
497
George Liue99073f2022-12-09 11:06:16 +0800498 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000499 "xyz.openbmc_project.Inventory.Item.Board",
500 "xyz.openbmc_project.Inventory.Item.Chassis"};
501
502 const std::string& chassisId = param;
503
George Liue99073f2022-12-09 11:06:16 +0800504 dbus::utility::getSubTree(
505 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000506 [asyncResp, chassisId, locationIndicatorActive,
507 indicatorLed](const boost::system::error_code ec,
508 const dbus::utility::MapperGetSubTreeResponse& subtree) {
509 if (ec)
510 {
511 messages::internalError(asyncResp->res);
512 return;
513 }
514
515 // Iterate over all retrieved ObjectPaths.
516 for (const std::pair<
517 std::string,
518 std::vector<std::pair<std::string, std::vector<std::string>>>>&
519 object : subtree)
520 {
521 const std::string& path = object.first;
522 const std::vector<std::pair<std::string, std::vector<std::string>>>&
523 connectionNames = object.second;
524
525 sdbusplus::message::object_path objPath(path);
526 if (objPath.filename() != chassisId)
527 {
528 continue;
529 }
530
531 if (connectionNames.empty())
532 {
533 BMCWEB_LOG_ERROR << "Got 0 Connection names";
534 continue;
535 }
536
537 const std::vector<std::string>& interfaces3 =
538 connectionNames[0].second;
539
540 const std::array<const char*, 2> hasIndicatorLed = {
541 "xyz.openbmc_project.Inventory.Item.Panel",
542 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
543 bool indicatorChassis = false;
544 for (const char* interface : hasIndicatorLed)
545 {
546 if (std::find(interfaces3.begin(), interfaces3.end(),
547 interface) != interfaces3.end())
548 {
549 indicatorChassis = true;
550 break;
551 }
552 }
553 if (locationIndicatorActive)
554 {
555 if (indicatorChassis)
556 {
557 setLocationIndicatorActive(asyncResp,
558 *locationIndicatorActive);
559 }
560 else
561 {
562 messages::propertyUnknown(asyncResp->res,
563 "LocationIndicatorActive");
564 }
565 }
566 if (indicatorLed)
567 {
568 if (indicatorChassis)
569 {
570 setIndicatorLedState(asyncResp, *indicatorLed);
571 }
572 else
573 {
574 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
575 }
576 }
577 return;
578 }
579
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800580 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
George Liue99073f2022-12-09 11:06:16 +0800581 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000582}
583
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100584/**
585 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700586 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100587 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700588inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700590 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700591 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000593 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700594
595 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700596 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700597 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000598 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700599}
P.K. Leedd99e042020-06-17 19:43:16 +0800600
zhanghch058d1b46d2021-04-01 11:18:24 +0800601inline void
602 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800603{
George Liu7a1dbc42022-12-07 16:03:22 +0800604 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700605 "xyz.openbmc_project.State.Chassis"};
606
607 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800608 dbus::utility::getSubTreePaths(
609 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800610 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800611 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800612 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 if (ec)
614 {
615 BMCWEB_LOG_DEBUG << "[mapper] Bad D-Bus request error: " << ec;
616 messages::internalError(asyncResp->res);
617 return;
618 }
619
620 const char* processName = "xyz.openbmc_project.State.Chassis";
621 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
622 const char* destProperty = "RequestedPowerTransition";
623 const std::string propertyValue =
624 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
625 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
626
627 /* Look for system reset chassis path */
628 if ((std::find(chassisList.begin(), chassisList.end(), objectPath)) ==
629 chassisList.end())
630 {
631 /* We prefer to reset the full chassis_system, but if it doesn't
632 * exist on some platforms, fall back to a host-only power reset
633 */
634 objectPath = "/xyz/openbmc_project/state/chassis0";
635 }
636
637 crow::connections::systemBus->async_method_call(
Ed Tanous8a592812022-06-04 09:06:59 -0700638 [asyncResp](const boost::system::error_code ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 // Use "Set" method to set the property value.
Ed Tanous8a592812022-06-04 09:06:59 -0700640 if (ec2)
P.K. Leedd99e042020-06-17 19:43:16 +0800641 {
Ed Tanous8a592812022-06-04 09:06:59 -0700642 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec2;
P.K. Leedd99e042020-06-17 19:43:16 +0800643 messages::internalError(asyncResp->res);
644 return;
645 }
646
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 messages::success(asyncResp->res);
648 },
649 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
650 interfaceName, destProperty,
651 dbus::utility::DbusVariantType{propertyValue});
George Liu7a1dbc42022-12-07 16:03:22 +0800652 });
P.K. Leedd99e042020-06-17 19:43:16 +0800653}
654
Nan Zhoucf7eba02022-07-21 23:53:20 +0000655inline void handleChassisResetActionInfoPost(
656 App& app, const crow::Request& req,
657 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
658 const std::string& /*chassisId*/)
659{
660 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
661 {
662 return;
663 }
664 BMCWEB_LOG_DEBUG << "Post Chassis Reset.";
665
666 std::string resetType;
667
668 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
669 {
670 return;
671 }
672
673 if (resetType != "PowerCycle")
674 {
675 BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
676 << resetType;
677 messages::actionParameterNotSupported(asyncResp->res, resetType,
678 "ResetType");
679
680 return;
681 }
682 doChassisPowerCycle(asyncResp);
683}
684
P.K. Leedd99e042020-06-17 19:43:16 +0800685/**
686 * ChassisResetAction class supports the POST method for the Reset
687 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700688 * Function handles POST method request.
689 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800690 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700691
692inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800693{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700694 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700695 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700696 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000697 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
698}
P.K. Leedd99e042020-06-17 19:43:16 +0800699
Nan Zhoucf7eba02022-07-21 23:53:20 +0000700inline void handleChassisResetActionInfoGet(
701 App& app, const crow::Request& req,
702 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
703 const std::string& chassisId)
704{
705 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
706 {
707 return;
708 }
709 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
710 asyncResp->res.jsonValue["@odata.id"] =
711 "/redfish/v1/Chassis/" + chassisId + "/ResetActionInfo";
712 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800713
Nan Zhoucf7eba02022-07-21 23:53:20 +0000714 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
715 nlohmann::json::array_t parameters;
716 nlohmann::json::object_t parameter;
717 parameter["Name"] = "ResetType";
718 parameter["Required"] = true;
719 parameter["DataType"] = "String";
720 nlohmann::json::array_t allowed;
721 allowed.push_back("PowerCycle");
722 parameter["AllowableValues"] = std::move(allowed);
723 parameters.push_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800724
Nan Zhoucf7eba02022-07-21 23:53:20 +0000725 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700726}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530727
728/**
729 * ChassisResetActionInfo derived class for delivering Chassis
730 * ResetType AllowableValues using ResetInfo schema.
731 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700732inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530733{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700734 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700735 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700736 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000737 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700738}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530739
Ed Tanous1abe55e2018-09-05 08:30:59 -0700740} // namespace redfish