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