blob: 6bc7fbaed7cfe0d9b8bb1d0ce2dd7f16fb9eeeac [file] [log] [blame]
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -06001#pragma once
Ed Tanousb9d36b42022-02-26 21:42:46 -08002#include <dbus_utility.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -07003#include <query.hpp>
Krzysztof Grobelny89474492022-09-06 16:30:38 +02004#include <sdbusplus/asio/property.hpp>
5#include <sdbusplus/unpack_properties.hpp>
6#include <utils/dbus_utils.hpp>
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -06007#include <utils/json_utils.hpp>
8
9namespace redfish
10{
11/**
12 * @brief Fill cable specific properties.
13 * @param[in,out] resp HTTP response.
14 * @param[in] ec Error code corresponding to Async method call.
15 * @param[in] properties List of Cable Properties key/value pairs.
16 */
17inline void
18 fillCableProperties(crow::Response& resp,
19 const boost::system::error_code ec,
20 const dbus::utility::DBusPropertiesMap& properties)
21{
22 if (ec)
23 {
24 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
25 messages::internalError(resp);
26 return;
27 }
28
Krzysztof Grobelny89474492022-09-06 16:30:38 +020029 const std::string* cableTypeDescription = nullptr;
30 const double* length = nullptr;
31
32 const bool success = sdbusplus::unpackPropertiesNoThrow(
33 dbus_utils::UnpackErrorPrinter(), properties, "CableTypeDescription",
34 cableTypeDescription, "Length", length);
35
36 if (!success)
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060037 {
Krzysztof Grobelny89474492022-09-06 16:30:38 +020038 messages::internalError(resp);
39 return;
40 }
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060041
Krzysztof Grobelny89474492022-09-06 16:30:38 +020042 if (cableTypeDescription != nullptr)
43 {
44 resp.jsonValue["CableType"] = *cableTypeDescription;
45 }
46
47 if (length != nullptr)
48 {
49 if (!std::isfinite(*length))
50 {
Shantappa Teekappanavar043360d2022-11-03 13:37:24 -050051 // Cable length is NaN by default, do not throw an error
52 if (!std::isnan(*length))
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060053 {
Shantappa Teekappanavar043360d2022-11-03 13:37:24 -050054 messages::internalError(resp);
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060055 return;
56 }
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060057 }
Shantappa Teekappanavar043360d2022-11-03 13:37:24 -050058 else
59 {
60 resp.jsonValue["LengthMeters"] = *length;
61 }
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060062 }
63}
64
65/**
66 * @brief Api to get Cable properties.
67 * @param[in,out] asyncResp Async HTTP response.
68 * @param[in] cableObjectPath Object path of the Cable.
69 * @param[in] serviceMap A map to hold Service and corresponding
70 * interface list for the given cable id.
71 */
72inline void
73 getCableProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
74 const std::string& cableObjectPath,
75 const dbus::utility::MapperServiceMap& serviceMap)
76{
77 BMCWEB_LOG_DEBUG << "Get Properties for cable " << cableObjectPath;
78
79 for (const auto& [service, interfaces] : serviceMap)
80 {
81 for (const auto& interface : interfaces)
82 {
83 if (interface != "xyz.openbmc_project.Inventory.Item.Cable")
84 {
85 continue;
86 }
87
Krzysztof Grobelny89474492022-09-06 16:30:38 +020088 sdbusplus::asio::getAllProperties(
89 *crow::connections::systemBus, service, cableObjectPath,
90 interface,
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060091 [asyncResp](
92 const boost::system::error_code ec,
93 const dbus::utility::DBusPropertiesMap& properties) {
Ed Tanous002d39b2022-05-31 08:59:27 -070094 fillCableProperties(asyncResp->res, ec, properties);
Krzysztof Grobelny89474492022-09-06 16:30:38 +020095 });
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -060096 }
97 }
98}
99
100/**
101 * The Cable schema
102 */
103inline void requestRoutesCable(App& app)
104{
105 BMCWEB_ROUTE(app, "/redfish/v1/Cables/<str>/")
106 .privileges(redfish::privileges::getCable)
107 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700108 [&app](const crow::Request& req,
109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
110 const std::string& cableId) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000111 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700112 {
113 return;
114 }
115 BMCWEB_LOG_DEBUG << "Cable Id: " << cableId;
116 auto respHandler =
117 [asyncResp,
118 cableId](const boost::system::error_code ec,
119 const dbus::utility::MapperGetSubTreeResponse& subtree) {
120 if (ec.value() == EBADR)
121 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800122 messages::resourceNotFound(asyncResp->res, "Cable", cableId);
Ed Tanous002d39b2022-05-31 08:59:27 -0700123 return;
124 }
125
126 if (ec)
127 {
128 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
129 messages::internalError(asyncResp->res);
130 return;
131 }
132
133 for (const auto& [objectPath, serviceMap] : subtree)
134 {
135 sdbusplus::message::object_path path(objectPath);
136 if (path.filename() != cableId)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700137 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700138 continue;
Ed Tanous45ca1b82022-03-25 13:07:27 -0700139 }
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -0600140
Ed Tanous002d39b2022-05-31 08:59:27 -0700141 asyncResp->res.jsonValue["@odata.type"] = "#Cable.v1_0_0.Cable";
142 asyncResp->res.jsonValue["@odata.id"] =
143 "/redfish/v1/Cables/" + cableId;
144 asyncResp->res.jsonValue["Id"] = cableId;
145 asyncResp->res.jsonValue["Name"] = "Cable";
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -0600146
Ed Tanous002d39b2022-05-31 08:59:27 -0700147 getCableProperties(asyncResp, objectPath, serviceMap);
148 return;
149 }
150 messages::resourceNotFound(asyncResp->res, "Cable", cableId);
151 };
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -0600152
Ed Tanous002d39b2022-05-31 08:59:27 -0700153 crow::connections::systemBus->async_method_call(
154 respHandler, "xyz.openbmc_project.ObjectMapper",
155 "/xyz/openbmc_project/object_mapper",
156 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
157 "/xyz/openbmc_project/inventory", 0,
158 std::array<const char*, 1>{
159 "xyz.openbmc_project.Inventory.Item.Cable"});
160 });
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -0600161}
162
163/**
164 * Collection of Cable resource instances
165 */
166inline void requestRoutesCableCollection(App& app)
167{
168 BMCWEB_ROUTE(app, "/redfish/v1/Cables/")
169 .privileges(redfish::privileges::getCableCollection)
170 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700171 [&app](const crow::Request& req,
172 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000173 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700174 {
175 return;
176 }
177 asyncResp->res.jsonValue["@odata.type"] =
178 "#CableCollection.CableCollection";
179 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Cables";
180 asyncResp->res.jsonValue["Name"] = "Cable Collection";
181 asyncResp->res.jsonValue["Description"] = "Collection of Cable Entries";
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -0600182
Ed Tanous002d39b2022-05-31 08:59:27 -0700183 collection_util::getCollectionMembers(
Willy Tuae9031f2022-09-27 05:48:07 +0000184 asyncResp, boost::urls::url("/redfish/v1/Cables"),
Ed Tanous002d39b2022-05-31 08:59:27 -0700185 {"xyz.openbmc_project.Inventory.Item.Cable"});
186 });
Shantappa Teekappanavar9c929be2021-12-16 19:02:52 -0600187}
188
189} // namespace redfish