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