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