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