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 |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 3 | #pragma once |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 4 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 5 | #include "app.hpp" |
| 6 | #include "async_resp.hpp" |
| 7 | #include "dbus_singleton.hpp" |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 8 | #include "dbus_utility.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "error_messages.hpp" |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame] | 10 | #include "generated/enums/resource.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include "http_request.hpp" |
| 12 | #include "http_response.hpp" |
| 13 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 14 | #include "query.hpp" |
| 15 | #include "registries/privilege_registry.hpp" |
| 16 | #include "utils/collection.hpp" |
| 17 | #include "utils/dbus_utils.hpp" |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 18 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 19 | #include <asm-generic/errno.h> |
| 20 | |
| 21 | #include <boost/beast/http/verb.hpp> |
George Liu | e99073f | 2022-12-09 11:06:16 +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 <boost/url/url.hpp> |
| 25 | #include <sdbusplus/message/native_types.hpp> |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 26 | #include <sdbusplus/unpack_properties.hpp> |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 27 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 28 | #include <array> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 29 | #include <cmath> |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 30 | #include <functional> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 31 | #include <memory> |
| 32 | #include <string> |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 33 | #include <string_view> |
| 34 | |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 35 | namespace redfish |
| 36 | { |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 37 | |
| 38 | constexpr std::array<std::string_view, 1> cableInterfaces = { |
| 39 | "xyz.openbmc_project.Inventory.Item.Cable"}; |
| 40 | |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 41 | /** |
| 42 | * @brief Fill cable specific properties. |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 43 | * @param[in,out] asyncResp Async HTTP response. |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 44 | * @param[in] ec Error code corresponding to Async method call. |
| 45 | * @param[in] properties List of Cable Properties key/value pairs. |
| 46 | */ |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 47 | inline void fillCableProperties( |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 48 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 49 | const boost::system::error_code& ec, |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 50 | const dbus::utility::DBusPropertiesMap& properties) |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 51 | { |
| 52 | if (ec) |
| 53 | { |
Gunnar Mills | f933a6a | 2024-03-28 21:55:01 -0500 | [diff] [blame] | 54 | BMCWEB_LOG_ERROR("DBUS response error {}", ec); |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 55 | messages::internalError(asyncResp->res); |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 59 | const std::string* cableTypeDescription = nullptr; |
| 60 | const double* length = nullptr; |
| 61 | |
| 62 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 63 | dbus_utils::UnpackErrorPrinter(), properties, "CableTypeDescription", |
| 64 | cableTypeDescription, "Length", length); |
| 65 | |
| 66 | if (!success) |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 67 | { |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 68 | messages::internalError(asyncResp->res); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 69 | return; |
| 70 | } |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 71 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 72 | if (cableTypeDescription != nullptr) |
| 73 | { |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 74 | asyncResp->res.jsonValue["CableType"] = *cableTypeDescription; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (length != nullptr) |
| 78 | { |
| 79 | if (!std::isfinite(*length)) |
| 80 | { |
Shantappa Teekappanavar | 043360d | 2022-11-03 13:37:24 -0500 | [diff] [blame] | 81 | // Cable length is NaN by default, do not throw an error |
| 82 | if (!std::isnan(*length)) |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 83 | { |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 84 | BMCWEB_LOG_ERROR("Cable length value is invalid"); |
| 85 | messages::internalError(asyncResp->res); |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 86 | return; |
| 87 | } |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 88 | } |
Shantappa Teekappanavar | 043360d | 2022-11-03 13:37:24 -0500 | [diff] [blame] | 89 | else |
| 90 | { |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 91 | asyncResp->res.jsonValue["LengthMeters"] = *length; |
Shantappa Teekappanavar | 043360d | 2022-11-03 13:37:24 -0500 | [diff] [blame] | 92 | } |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 96 | inline void fillCableHealthState( |
| 97 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 98 | const std::string& cableObjectPath, const std::string& service) |
| 99 | { |
| 100 | dbus::utility::getProperty<bool>( |
| 101 | *crow::connections::systemBus, service, cableObjectPath, |
| 102 | "xyz.openbmc_project.Inventory.Item", "Present", |
| 103 | [asyncResp, |
| 104 | cableObjectPath](const boost::system::error_code& ec, bool present) { |
| 105 | if (ec) |
| 106 | { |
| 107 | if (ec.value() != EBADR) |
| 108 | { |
| 109 | BMCWEB_LOG_ERROR( |
| 110 | "get presence failed for Cable {} with error {}", |
| 111 | cableObjectPath, ec.value()); |
| 112 | messages::internalError(asyncResp->res); |
| 113 | } |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | if (!present) |
| 118 | { |
| 119 | asyncResp->res.jsonValue["Status"]["State"] = |
| 120 | resource::State::Absent; |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 125 | /** |
| 126 | * @brief Api to get Cable properties. |
| 127 | * @param[in,out] asyncResp Async HTTP response. |
| 128 | * @param[in] cableObjectPath Object path of the Cable. |
| 129 | * @param[in] serviceMap A map to hold Service and corresponding |
| 130 | * interface list for the given cable id. |
| 131 | */ |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 132 | inline void getCableProperties( |
| 133 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 134 | const std::string& cableObjectPath, |
| 135 | const dbus::utility::MapperServiceMap& serviceMap) |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 136 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 137 | BMCWEB_LOG_DEBUG("Get Properties for cable {}", cableObjectPath); |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 138 | |
| 139 | for (const auto& [service, interfaces] : serviceMap) |
| 140 | { |
| 141 | for (const auto& interface : interfaces) |
| 142 | { |
Akshit Shah | 0c2ba59 | 2023-01-24 01:03:26 +0000 | [diff] [blame] | 143 | if (interface == "xyz.openbmc_project.Inventory.Item.Cable") |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 144 | { |
Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 145 | dbus::utility::getAllProperties( |
Akshit Shah | 0c2ba59 | 2023-01-24 01:03:26 +0000 | [diff] [blame] | 146 | *crow::connections::systemBus, service, cableObjectPath, |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 147 | interface, std::bind_front(fillCableProperties, asyncResp)); |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 148 | } |
Akshit Shah | 0c2ba59 | 2023-01-24 01:03:26 +0000 | [diff] [blame] | 149 | else if (interface == "xyz.openbmc_project.Inventory.Item") |
| 150 | { |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 151 | fillCableHealthState(asyncResp, cableObjectPath, service); |
Akshit Shah | 0c2ba59 | 2023-01-24 01:03:26 +0000 | [diff] [blame] | 152 | } |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 157 | inline void afterHandleCableGet( |
| 158 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 159 | const std::string& cableId, const boost::system::error_code& ec, |
| 160 | const dbus::utility::MapperGetSubTreeResponse& subtree) |
| 161 | { |
| 162 | if (ec.value() == EBADR) |
| 163 | { |
| 164 | messages::resourceNotFound(asyncResp->res, "Cable", cableId); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (ec) |
| 169 | { |
| 170 | BMCWEB_LOG_ERROR("DBUS response error {}", ec.value()); |
| 171 | messages::internalError(asyncResp->res); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | for (const auto& [objectPath, serviceMap] : subtree) |
| 176 | { |
| 177 | sdbusplus::message::object_path path(objectPath); |
| 178 | if (path.filename() != cableId) |
| 179 | { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | asyncResp->res.jsonValue["@odata.type"] = "#Cable.v1_0_0.Cable"; |
| 184 | asyncResp->res.jsonValue["@odata.id"] = |
| 185 | boost::urls::format("/redfish/v1/Cables/{}", cableId); |
| 186 | asyncResp->res.jsonValue["Id"] = cableId; |
| 187 | asyncResp->res.jsonValue["Name"] = "Cable"; |
| 188 | asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled; |
| 189 | |
| 190 | getCableProperties(asyncResp, objectPath, serviceMap); |
| 191 | return; |
| 192 | } |
| 193 | messages::resourceNotFound(asyncResp->res, "Cable", cableId); |
| 194 | } |
| 195 | |
| 196 | inline void handleCableGet(App& app, const crow::Request& req, |
| 197 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 198 | const std::string& cableId) |
| 199 | { |
| 200 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 201 | { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | BMCWEB_LOG_DEBUG("Cable Id: {}", cableId); |
| 206 | |
| 207 | dbus::utility::getSubTree( |
| 208 | "/xyz/openbmc_project/inventory", 0, cableInterfaces, |
| 209 | std::bind_front(afterHandleCableGet, asyncResp, cableId)); |
| 210 | } |
| 211 | |
| 212 | inline void handleCableCollectionGet( |
| 213 | App& app, const crow::Request& req, |
| 214 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 215 | { |
| 216 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 217 | { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | asyncResp->res.jsonValue["@odata.type"] = |
| 222 | "#CableCollection.CableCollection"; |
| 223 | asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Cables"; |
| 224 | asyncResp->res.jsonValue["Name"] = "Cable Collection"; |
| 225 | asyncResp->res.jsonValue["Description"] = "Collection of Cable Entries"; |
| 226 | collection_util::getCollectionMembers( |
| 227 | asyncResp, boost::urls::url("/redfish/v1/Cables"), cableInterfaces, |
| 228 | "/xyz/openbmc_project/inventory"); |
| 229 | } |
| 230 | |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 231 | /** |
| 232 | * The Cable schema |
| 233 | */ |
| 234 | inline void requestRoutesCable(App& app) |
| 235 | { |
| 236 | BMCWEB_ROUTE(app, "/redfish/v1/Cables/<str>/") |
| 237 | .privileges(redfish::privileges::getCable) |
| 238 | .methods(boost::beast::http::verb::get)( |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 239 | std::bind_front(handleCableGet, std::ref(app))); |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Collection of Cable resource instances |
| 244 | */ |
| 245 | inline void requestRoutesCableCollection(App& app) |
| 246 | { |
| 247 | BMCWEB_ROUTE(app, "/redfish/v1/Cables/") |
| 248 | .privileges(redfish::privileges::getCableCollection) |
| 249 | .methods(boost::beast::http::verb::get)( |
Myung Bae | ab8cbe4 | 2025-03-05 06:12:02 -0800 | [diff] [blame] | 250 | std::bind_front(handleCableCollectionGet, std::ref(app))); |
Shantappa Teekappanavar | 9c929be | 2021-12-16 19:02:52 -0600 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | } // namespace redfish |