Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 3 | #pragma once |
| 4 | |
| 5 | #include "app.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "async_resp.hpp" |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 7 | #include "dbus_utility.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 8 | #include "error_messages.hpp" |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame] | 9 | #include "generated/enums/resource.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 10 | #include "http_request.hpp" |
| 11 | #include "logging.hpp" |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 12 | #include "query.hpp" |
| 13 | #include "registries/privilege_registry.hpp" |
| 14 | #include "utils/chassis_utils.hpp" |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 15 | #include "utils/dbus_utils.hpp" |
Hieu Huynh | b519006 | 2024-07-11 03:47:21 +0000 | [diff] [blame] | 16 | #include "utils/time_utils.hpp" |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 17 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 18 | #include <asm-generic/errno.h> |
| 19 | |
| 20 | #include <boost/beast/http/field.hpp> |
| 21 | #include <boost/beast/http/verb.hpp> |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 22 | #include <boost/system/error_code.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 23 | #include <boost/url/format.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
| 25 | #include <sdbusplus/unpack_properties.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 26 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 27 | #include <array> |
| 28 | #include <cstdint> |
| 29 | #include <functional> |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 30 | #include <memory> |
| 31 | #include <optional> |
| 32 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 33 | #include <string_view> |
| 34 | #include <utility> |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 35 | |
| 36 | namespace redfish |
| 37 | { |
| 38 | |
Lakshmi Yadlapati | 788fe6c | 2023-06-21 14:39:08 -0500 | [diff] [blame] | 39 | static constexpr std::array<std::string_view, 1> powerSupplyInterface = { |
| 40 | "xyz.openbmc_project.Inventory.Item.PowerSupply"}; |
| 41 | |
| 42 | inline void updatePowerSupplyList( |
| 43 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 44 | const std::string& chassisId, |
| 45 | const dbus::utility::MapperGetSubTreePathsResponse& powerSupplyPaths) |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 46 | { |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 47 | nlohmann::json& powerSupplyList = asyncResp->res.jsonValue["Members"]; |
Lakshmi Yadlapati | 788fe6c | 2023-06-21 14:39:08 -0500 | [diff] [blame] | 48 | for (const std::string& powerSupplyPath : powerSupplyPaths) |
| 49 | { |
| 50 | std::string powerSupplyName = |
| 51 | sdbusplus::message::object_path(powerSupplyPath).filename(); |
| 52 | if (powerSupplyName.empty()) |
| 53 | { |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | nlohmann::json item = nlohmann::json::object(); |
| 58 | item["@odata.id"] = boost::urls::format( |
| 59 | "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies/{}", chassisId, |
| 60 | powerSupplyName); |
| 61 | |
| 62 | powerSupplyList.emplace_back(std::move(item)); |
| 63 | } |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 64 | asyncResp->res.jsonValue["Members@odata.count"] = powerSupplyList.size(); |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 65 | } |
| 66 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 67 | inline void doPowerSupplyCollection( |
| 68 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 69 | const std::string& chassisId, const boost::system::error_code& ec, |
| 70 | const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 71 | { |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 72 | if (ec) |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 73 | { |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 74 | if (ec.value() == boost::system::errc::io_error) |
| 75 | { |
| 76 | BMCWEB_LOG_WARNING("Chassis not found"); |
| 77 | messages::resourceNotFound(asyncResp->res, "Chassis", chassisId); |
| 78 | return; |
| 79 | } |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 80 | if (ec.value() != EBADR) |
| 81 | { |
| 82 | BMCWEB_LOG_ERROR("DBUS response error{}", ec.value()); |
| 83 | messages::internalError(asyncResp->res); |
| 84 | } |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 85 | return; |
| 86 | } |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 87 | asyncResp->res.addHeader( |
| 88 | boost::beast::http::field::link, |
| 89 | "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby"); |
| 90 | asyncResp->res.jsonValue["@odata.type"] = |
| 91 | "#PowerSupplyCollection.PowerSupplyCollection"; |
| 92 | asyncResp->res.jsonValue["Name"] = "Power Supply Collection"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 93 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 94 | "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies", chassisId); |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 95 | asyncResp->res.jsonValue["Description"] = |
| 96 | "The collection of PowerSupply resource instances."; |
George Liu | 7a2bb2c | 2023-04-11 10:44:33 +0800 | [diff] [blame] | 97 | asyncResp->res.jsonValue["Members"] = nlohmann::json::array(); |
| 98 | asyncResp->res.jsonValue["Members@odata.count"] = 0; |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 99 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 100 | updatePowerSupplyList(asyncResp, chassisId, subtreePaths); |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | inline void handlePowerSupplyCollectionHead( |
| 104 | App& app, const crow::Request& req, |
| 105 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 106 | const std::string& chassisId) |
| 107 | { |
| 108 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 109 | { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | redfish::chassis_utils::getValidChassisPath( |
| 114 | asyncResp, chassisId, |
| 115 | [asyncResp, |
| 116 | chassisId](const std::optional<std::string>& validChassisPath) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 117 | if (!validChassisPath) |
| 118 | { |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 119 | BMCWEB_LOG_WARNING("Chassis not found"); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 120 | messages::resourceNotFound(asyncResp->res, "Chassis", |
| 121 | chassisId); |
| 122 | return; |
| 123 | } |
| 124 | asyncResp->res.addHeader( |
| 125 | boost::beast::http::field::link, |
| 126 | "</redfish/v1/JsonSchemas/PowerSupplyCollection/PowerSupplyCollection.json>; rel=describedby"); |
| 127 | }); |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | inline void handlePowerSupplyCollectionGet( |
| 131 | App& app, const crow::Request& req, |
| 132 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 133 | const std::string& chassisId) |
| 134 | { |
| 135 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 136 | { |
| 137 | return; |
| 138 | } |
| 139 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 140 | const std::string reqpath = "/xyz/openbmc_project/inventory"; |
| 141 | |
| 142 | dbus::utility::getAssociatedSubTreePathsById( |
Myung Bae | 3f95a27 | 2024-03-13 07:32:02 -0700 | [diff] [blame] | 143 | chassisId, reqpath, chassisInterfaces, "powered_by", |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 144 | powerSupplyInterface, |
| 145 | [asyncResp, chassisId]( |
| 146 | const boost::system::error_code& ec, |
| 147 | const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) { |
| 148 | doPowerSupplyCollection(asyncResp, chassisId, ec, subtreePaths); |
| 149 | }); |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | inline void requestRoutesPowerSupplyCollection(App& app) |
| 153 | { |
| 154 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/") |
| 155 | .privileges(redfish::privileges::headPowerSupplyCollection) |
| 156 | .methods(boost::beast::http::verb::head)( |
| 157 | std::bind_front(handlePowerSupplyCollectionHead, std::ref(app))); |
| 158 | |
| 159 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/") |
| 160 | .privileges(redfish::privileges::getPowerSupplyCollection) |
| 161 | .methods(boost::beast::http::verb::get)( |
| 162 | std::bind_front(handlePowerSupplyCollectionGet, std::ref(app))); |
| 163 | } |
| 164 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 165 | inline void afterGetValidPowerSupplyPath( |
| 166 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 167 | const std::string& powerSupplyId, const boost::system::error_code& ec, |
| 168 | const dbus::utility::MapperGetSubTreeResponse& subtree, |
| 169 | const std::function<void(const std::string& powerSupplyPath, |
| 170 | const std::string& service)>& callback) |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 171 | { |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 172 | if (ec) |
| 173 | { |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 174 | if (ec.value() == boost::system::errc::io_error) |
| 175 | { |
| 176 | // Not found |
| 177 | callback(std::string(), std::string()); |
| 178 | return; |
| 179 | } |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 180 | if (ec.value() != EBADR) |
| 181 | { |
| 182 | BMCWEB_LOG_ERROR("DBUS response error{}", ec.value()); |
| 183 | messages::internalError(asyncResp->res); |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 184 | return; |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 185 | } |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 186 | callback(std::string(), std::string()); |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | for (const auto& [objectPath, service] : subtree) |
| 190 | { |
| 191 | sdbusplus::message::object_path path(objectPath); |
Myung Bae | d8e2b61 | 2024-10-08 12:19:19 -0500 | [diff] [blame] | 192 | if (path.filename() == powerSupplyId) |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 193 | { |
| 194 | callback(path, service.begin()->first); |
| 195 | return; |
| 196 | } |
| 197 | } |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 198 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 199 | BMCWEB_LOG_WARNING("Power supply not found: {}", powerSupplyId); |
| 200 | messages::resourceNotFound(asyncResp->res, "PowerSupplies", powerSupplyId); |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 201 | } |
| 202 | |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 203 | inline void getValidPowerSupplyPath( |
| 204 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 205 | const std::string& chassisId, const std::string& powerSupplyId, |
| 206 | std::function<void(const std::string& powerSupplyPath, |
| 207 | const std::string& service)>&& callback) |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 208 | { |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 209 | const std::string reqpath = "/xyz/openbmc_project/inventory"; |
| 210 | |
| 211 | dbus::utility::getAssociatedSubTreeById( |
Myung Bae | 3f95a27 | 2024-03-13 07:32:02 -0700 | [diff] [blame] | 212 | chassisId, reqpath, chassisInterfaces, "powered_by", |
Lakshmi Yadlapati | 788fe6c | 2023-06-21 14:39:08 -0500 | [diff] [blame] | 213 | powerSupplyInterface, |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 214 | [asyncResp, chassisId, powerSupplyId, callback{std::move(callback)}]( |
Lakshmi Yadlapati | 788fe6c | 2023-06-21 14:39:08 -0500 | [diff] [blame] | 215 | const boost::system::error_code& ec, |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 216 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
| 217 | afterGetValidPowerSupplyPath(asyncResp, powerSupplyId, ec, subtree, |
| 218 | callback); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 219 | }); |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 220 | } |
| 221 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 222 | inline void getPowerSupplyState( |
| 223 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 224 | const std::string& service, const std::string& path) |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 225 | { |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 226 | dbus::utility::getProperty<bool>( |
| 227 | service, path, "xyz.openbmc_project.Inventory.Item", "Present", |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 228 | [asyncResp](const boost::system::error_code& ec, const bool value) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 229 | if (ec) |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 230 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 231 | if (ec.value() != EBADR) |
| 232 | { |
| 233 | BMCWEB_LOG_ERROR("DBUS response error for State {}", |
| 234 | ec.value()); |
| 235 | messages::internalError(asyncResp->res); |
| 236 | } |
| 237 | return; |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 238 | } |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 239 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 240 | if (!value) |
| 241 | { |
| 242 | asyncResp->res.jsonValue["Status"]["State"] = |
| 243 | resource::State::Absent; |
| 244 | } |
| 245 | }); |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 246 | } |
| 247 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 248 | inline void getPowerSupplyHealth( |
| 249 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 250 | const std::string& service, const std::string& path) |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 251 | { |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 252 | dbus::utility::getProperty<bool>( |
| 253 | service, path, "xyz.openbmc_project.State.Decorator.OperationalStatus", |
| 254 | "Functional", |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 255 | [asyncResp](const boost::system::error_code& ec, const bool value) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 256 | if (ec) |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 257 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 258 | if (ec.value() != EBADR) |
| 259 | { |
| 260 | BMCWEB_LOG_ERROR("DBUS response error for Health {}", |
| 261 | ec.value()); |
| 262 | messages::internalError(asyncResp->res); |
| 263 | } |
| 264 | return; |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 265 | } |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 266 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 267 | if (!value) |
| 268 | { |
| 269 | asyncResp->res.jsonValue["Status"]["Health"] = |
| 270 | resource::Health::Critical; |
| 271 | } |
| 272 | }); |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 275 | inline void getPowerSupplyAsset( |
| 276 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 277 | const std::string& service, const std::string& path) |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 278 | { |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 279 | dbus::utility::getAllProperties( |
| 280 | service, path, "xyz.openbmc_project.Inventory.Decorator.Asset", |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 281 | [asyncResp](const boost::system::error_code& ec, |
| 282 | const dbus::utility::DBusPropertiesMap& propertiesList) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 283 | if (ec) |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 284 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 285 | if (ec.value() != EBADR) |
| 286 | { |
| 287 | BMCWEB_LOG_ERROR("DBUS response error for Asset {}", |
| 288 | ec.value()); |
| 289 | messages::internalError(asyncResp->res); |
| 290 | } |
| 291 | return; |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 292 | } |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 293 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 294 | const std::string* partNumber = nullptr; |
| 295 | const std::string* serialNumber = nullptr; |
| 296 | const std::string* manufacturer = nullptr; |
| 297 | const std::string* model = nullptr; |
| 298 | const std::string* sparePartNumber = nullptr; |
Hieu Huynh | b519006 | 2024-07-11 03:47:21 +0000 | [diff] [blame] | 299 | const std::string* buildDate = nullptr; |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 300 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 301 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 302 | dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber", |
| 303 | partNumber, "SerialNumber", serialNumber, "Manufacturer", |
| 304 | manufacturer, "Model", model, "SparePartNumber", |
Hieu Huynh | b519006 | 2024-07-11 03:47:21 +0000 | [diff] [blame] | 305 | sparePartNumber, "BuildDate", buildDate); |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 306 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 307 | if (!success) |
| 308 | { |
| 309 | messages::internalError(asyncResp->res); |
| 310 | return; |
| 311 | } |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 312 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 313 | if (partNumber != nullptr) |
| 314 | { |
| 315 | asyncResp->res.jsonValue["PartNumber"] = *partNumber; |
| 316 | } |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 317 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 318 | if (serialNumber != nullptr) |
| 319 | { |
| 320 | asyncResp->res.jsonValue["SerialNumber"] = *serialNumber; |
| 321 | } |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 322 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 323 | if (manufacturer != nullptr) |
| 324 | { |
| 325 | asyncResp->res.jsonValue["Manufacturer"] = *manufacturer; |
| 326 | } |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 327 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 328 | if (model != nullptr) |
| 329 | { |
| 330 | asyncResp->res.jsonValue["Model"] = *model; |
| 331 | } |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 332 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 333 | // SparePartNumber is optional on D-Bus so skip if it is empty |
| 334 | if (sparePartNumber != nullptr && !sparePartNumber->empty()) |
| 335 | { |
| 336 | asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber; |
| 337 | } |
Hieu Huynh | b519006 | 2024-07-11 03:47:21 +0000 | [diff] [blame] | 338 | |
| 339 | if (buildDate != nullptr) |
| 340 | { |
| 341 | time_utils::productionDateReport(asyncResp->res, *buildDate); |
| 342 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 343 | }); |
George Liu | 2b45fb3 | 2022-10-05 17:00:00 +0800 | [diff] [blame] | 344 | } |
| 345 | |
George Liu | a0dba87 | 2022-10-05 17:03:20 +0800 | [diff] [blame] | 346 | inline void getPowerSupplyFirmwareVersion( |
| 347 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 348 | const std::string& service, const std::string& path) |
| 349 | { |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 350 | dbus::utility::getProperty<std::string>( |
| 351 | service, path, "xyz.openbmc_project.Software.Version", "Version", |
George Liu | a0dba87 | 2022-10-05 17:03:20 +0800 | [diff] [blame] | 352 | [asyncResp](const boost::system::error_code& ec, |
| 353 | const std::string& value) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 354 | if (ec) |
George Liu | a0dba87 | 2022-10-05 17:03:20 +0800 | [diff] [blame] | 355 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 356 | if (ec.value() != EBADR) |
| 357 | { |
| 358 | BMCWEB_LOG_ERROR( |
| 359 | "DBUS response error for FirmwareVersion {}", |
| 360 | ec.value()); |
| 361 | messages::internalError(asyncResp->res); |
| 362 | } |
| 363 | return; |
George Liu | a0dba87 | 2022-10-05 17:03:20 +0800 | [diff] [blame] | 364 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 365 | asyncResp->res.jsonValue["FirmwareVersion"] = value; |
| 366 | }); |
George Liu | a0dba87 | 2022-10-05 17:03:20 +0800 | [diff] [blame] | 367 | } |
| 368 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 369 | inline void getPowerSupplyLocation( |
| 370 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 371 | const std::string& service, const std::string& path) |
George Liu | 44845e5 | 2022-10-05 17:09:21 +0800 | [diff] [blame] | 372 | { |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 373 | dbus::utility::getProperty<std::string>( |
| 374 | service, path, "xyz.openbmc_project.Inventory.Decorator.LocationCode", |
| 375 | "LocationCode", |
George Liu | 44845e5 | 2022-10-05 17:09:21 +0800 | [diff] [blame] | 376 | [asyncResp](const boost::system::error_code& ec, |
| 377 | const std::string& value) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 378 | if (ec) |
George Liu | 44845e5 | 2022-10-05 17:09:21 +0800 | [diff] [blame] | 379 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 380 | if (ec.value() != EBADR) |
| 381 | { |
| 382 | BMCWEB_LOG_ERROR("DBUS response error for Location {}", |
| 383 | ec.value()); |
| 384 | messages::internalError(asyncResp->res); |
| 385 | } |
| 386 | return; |
George Liu | 44845e5 | 2022-10-05 17:09:21 +0800 | [diff] [blame] | 387 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 388 | asyncResp->res |
| 389 | .jsonValue["Location"]["PartLocation"]["ServiceLabel"] = value; |
| 390 | }); |
George Liu | 44845e5 | 2022-10-05 17:09:21 +0800 | [diff] [blame] | 391 | } |
| 392 | |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 393 | inline void handleGetEfficiencyResponse( |
| 394 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 395 | const boost::system::error_code& ec, uint32_t value) |
| 396 | { |
| 397 | if (ec) |
| 398 | { |
| 399 | if (ec.value() != EBADR) |
| 400 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 401 | BMCWEB_LOG_ERROR("DBUS response error for DeratingFactor {}", |
| 402 | ec.value()); |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 403 | messages::internalError(asyncResp->res); |
| 404 | } |
| 405 | return; |
| 406 | } |
| 407 | // The PDI default value is 0, if it hasn't been set leave off |
| 408 | if (value == 0) |
| 409 | { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | nlohmann::json::array_t efficiencyRatings; |
| 414 | nlohmann::json::object_t efficiencyPercent; |
| 415 | efficiencyPercent["EfficiencyPercent"] = value; |
| 416 | efficiencyRatings.emplace_back(std::move(efficiencyPercent)); |
| 417 | asyncResp->res.jsonValue["EfficiencyRatings"] = |
| 418 | std::move(efficiencyRatings); |
| 419 | } |
| 420 | |
| 421 | inline void handlePowerSupplyAttributesSubTreeResponse( |
| 422 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 423 | const boost::system::error_code& ec, |
| 424 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
| 425 | { |
| 426 | if (ec) |
| 427 | { |
| 428 | if (ec.value() != EBADR) |
| 429 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 430 | BMCWEB_LOG_ERROR("DBUS response error for EfficiencyPercent {}", |
| 431 | ec.value()); |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 432 | messages::internalError(asyncResp->res); |
| 433 | } |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | if (subtree.empty()) |
| 438 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 439 | BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!"); |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | |
| 443 | if (subtree.size() != 1) |
| 444 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 445 | BMCWEB_LOG_ERROR( |
| 446 | "Unexpected number of paths returned by getSubTree: {}", |
| 447 | subtree.size()); |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 448 | messages::internalError(asyncResp->res); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | const auto& [path, serviceMap] = *subtree.begin(); |
| 453 | const auto& [service, interfaces] = *serviceMap.begin(); |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 454 | dbus::utility::getProperty<uint32_t>( |
| 455 | service, path, "xyz.openbmc_project.Control.PowerSupplyAttributes", |
| 456 | "DeratingFactor", |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 457 | [asyncResp](const boost::system::error_code& ec1, uint32_t value) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 458 | handleGetEfficiencyResponse(asyncResp, ec1, value); |
| 459 | }); |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 460 | } |
| 461 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 462 | inline void getEfficiencyPercent( |
| 463 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 464 | { |
| 465 | constexpr std::array<std::string_view, 1> efficiencyIntf = { |
| 466 | "xyz.openbmc_project.Control.PowerSupplyAttributes"}; |
| 467 | |
| 468 | dbus::utility::getSubTree( |
| 469 | "/xyz/openbmc_project", 0, efficiencyIntf, |
| 470 | [asyncResp](const boost::system::error_code& ec, |
| 471 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 472 | handlePowerSupplyAttributesSubTreeResponse(asyncResp, ec, subtree); |
| 473 | }); |
George Liu | ddceee0 | 2022-10-06 08:57:11 +0800 | [diff] [blame] | 474 | } |
| 475 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 476 | inline void doPowerSupplyGet( |
| 477 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 478 | const std::string& chassisId, const std::string& powerSupplyId, |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 479 | const std::string& powerSupplyPath, const std::string& service) |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 480 | { |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 481 | if (powerSupplyPath.empty() || service.empty()) |
| 482 | { |
| 483 | BMCWEB_LOG_WARNING("PowerSupply not found"); |
| 484 | messages::resourceNotFound(asyncResp->res, "PowerSupply", |
| 485 | powerSupplyId); |
| 486 | return; |
| 487 | } |
| 488 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 489 | asyncResp->res.addHeader( |
| 490 | boost::beast::http::field::link, |
| 491 | "</redfish/v1/JsonSchemas/PowerSupply/PowerSupply.json>; rel=describedby"); |
| 492 | asyncResp->res.jsonValue["@odata.type"] = "#PowerSupply.v1_5_0.PowerSupply"; |
| 493 | asyncResp->res.jsonValue["Name"] = "Power Supply"; |
| 494 | asyncResp->res.jsonValue["Id"] = powerSupplyId; |
| 495 | asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 496 | "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies/{}", chassisId, |
| 497 | powerSupplyId); |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 498 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 499 | asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled; |
| 500 | asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK; |
George Liu | 34dfcb9 | 2022-10-05 16:47:44 +0800 | [diff] [blame] | 501 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 502 | getPowerSupplyState(asyncResp, service, powerSupplyPath); |
| 503 | getPowerSupplyHealth(asyncResp, service, powerSupplyPath); |
| 504 | getPowerSupplyAsset(asyncResp, service, powerSupplyPath); |
| 505 | getPowerSupplyFirmwareVersion(asyncResp, service, powerSupplyPath); |
| 506 | getPowerSupplyLocation(asyncResp, service, powerSupplyPath); |
| 507 | getEfficiencyPercent(asyncResp); |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 508 | } |
| 509 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 510 | inline void handlePowerSupplyHead( |
| 511 | App& app, const crow::Request& req, |
| 512 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 513 | const std::string& chassisId, const std::string& powerSupplyId) |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 514 | { |
| 515 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 516 | { |
| 517 | return; |
| 518 | } |
| 519 | |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 520 | // Get the correct Path and Service that match the input parameters |
| 521 | getValidPowerSupplyPath( |
| 522 | asyncResp, chassisId, powerSupplyId, |
Myung Bae | 193582b | 2025-03-31 07:21:31 -0500 | [diff] [blame^] | 523 | [asyncResp, powerSupplyId](const std::string& powerSupplyPath, |
| 524 | const std::string& service) { |
| 525 | if (powerSupplyPath.empty() || service.empty()) |
| 526 | { |
| 527 | BMCWEB_LOG_WARNING("PowerSupply not found"); |
| 528 | messages::resourceNotFound(asyncResp->res, "PowerSupply", |
| 529 | powerSupplyId); |
| 530 | return; |
| 531 | } |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 532 | asyncResp->res.addHeader( |
| 533 | boost::beast::http::field::link, |
| 534 | "</redfish/v1/JsonSchemas/PowerSupply/PowerSupply.json>; rel=describedby"); |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 535 | }); |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 536 | } |
| 537 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 538 | inline void handlePowerSupplyGet( |
| 539 | App& app, const crow::Request& req, |
| 540 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 541 | const std::string& chassisId, const std::string& powerSupplyId) |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 542 | { |
| 543 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 544 | { |
| 545 | return; |
| 546 | } |
Lakshmi Yadlapati | 3e42a32 | 2024-06-26 18:28:06 -0500 | [diff] [blame] | 547 | getValidPowerSupplyPath( |
| 548 | asyncResp, chassisId, powerSupplyId, |
George Liu | 00ef5dc | 2022-10-05 16:27:52 +0800 | [diff] [blame] | 549 | std::bind_front(doPowerSupplyGet, asyncResp, chassisId, powerSupplyId)); |
| 550 | } |
| 551 | |
| 552 | inline void requestRoutesPowerSupply(App& app) |
| 553 | { |
| 554 | BMCWEB_ROUTE( |
| 555 | app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/<str>/") |
| 556 | .privileges(redfish::privileges::headPowerSupply) |
| 557 | .methods(boost::beast::http::verb::head)( |
| 558 | std::bind_front(handlePowerSupplyHead, std::ref(app))); |
| 559 | |
| 560 | BMCWEB_ROUTE( |
| 561 | app, "/redfish/v1/Chassis/<str>/PowerSubsystem/PowerSupplies/<str>/") |
| 562 | .privileges(redfish::privileges::getPowerSupply) |
| 563 | .methods(boost::beast::http::verb::get)( |
| 564 | std::bind_front(handlePowerSupplyGet, std::ref(app))); |
| 565 | } |
| 566 | |
George Liu | a721002 | 2022-10-05 15:44:11 +0800 | [diff] [blame] | 567 | } // namespace redfish |