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