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