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