James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2018 Intel Corporation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 14 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 15 | #pragma once |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 16 | #include <tinyxml2.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 18 | #include <app.hpp> |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 19 | #include <async_resp.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string.hpp> |
| 21 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 22 | #include <dbus_singleton.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 23 | #include <dbus_utility.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | #include <sdbusplus/message/types.hpp> |
| 25 | |
James Feist | 4418c7f | 2019-04-15 11:09:15 -0700 | [diff] [blame] | 26 | #include <filesystem> |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 27 | #include <fstream> |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 28 | #include <regex> |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 29 | #include <utility> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | namespace crow |
| 32 | { |
| 33 | namespace openbmc_mapper |
| 34 | { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 35 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 36 | using GetSubTreeType = std::vector< |
| 37 | std::pair<std::string, |
| 38 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 39 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 40 | const constexpr char* notFoundMsg = "404 Not Found"; |
| 41 | const constexpr char* badReqMsg = "400 Bad Request"; |
| 42 | const constexpr char* methodNotAllowedMsg = "405 Method Not Allowed"; |
| 43 | const constexpr char* forbiddenMsg = "403 Forbidden"; |
| 44 | const constexpr char* methodFailedMsg = "500 Method Call Failed"; |
| 45 | const constexpr char* methodOutputFailedMsg = "500 Method Output Error"; |
| 46 | const constexpr char* notFoundDesc = |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 47 | "org.freedesktop.DBus.Error.FileNotFound: path or object not found"; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 48 | const constexpr char* propNotFoundDesc = |
| 49 | "The specified property cannot be found"; |
| 50 | const constexpr char* noJsonDesc = "No JSON object could be decoded"; |
| 51 | const constexpr char* methodNotFoundDesc = |
| 52 | "The specified method cannot be found"; |
| 53 | const constexpr char* methodNotAllowedDesc = "Method not allowed"; |
| 54 | const constexpr char* forbiddenPropDesc = |
| 55 | "The specified property cannot be created"; |
| 56 | const constexpr char* forbiddenResDesc = |
| 57 | "The specified resource cannot be created"; |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 58 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 59 | inline void setErrorResponse(crow::Response& res, |
| 60 | boost::beast::http::status result, |
| 61 | const std::string& desc, |
| 62 | const std::string_view msg) |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 63 | { |
| 64 | res.result(result); |
| 65 | res.jsonValue = {{"data", {{"description", desc}}}, |
| 66 | {"message", msg}, |
| 67 | {"status", "error"}}; |
| 68 | } |
| 69 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 70 | inline void |
| 71 | introspectObjects(const std::string& processName, |
| 72 | const std::string& objectPath, |
| 73 | const std::shared_ptr<bmcweb::AsyncResp>& transaction) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 75 | if (transaction->res.jsonValue.is_null()) |
| 76 | { |
| 77 | transaction->res.jsonValue = {{"status", "ok"}, |
| 78 | {"bus_name", processName}, |
| 79 | {"objects", nlohmann::json::array()}}; |
| 80 | } |
| 81 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 83 | [transaction, processName{std::string(processName)}, |
| 84 | objectPath{std::string(objectPath)}]( |
| 85 | const boost::system::error_code ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 86 | const std::string& introspectXml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | if (ec) |
| 88 | { |
| 89 | BMCWEB_LOG_ERROR |
| 90 | << "Introspect call failed with error: " << ec.message() |
| 91 | << " on process: " << processName << " path: " << objectPath |
| 92 | << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | transaction->res.jsonValue["objects"].push_back( |
| 96 | {{"path", objectPath}}); |
| 97 | |
| 98 | tinyxml2::XMLDocument doc; |
| 99 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 100 | doc.Parse(introspectXml.c_str()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 101 | tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 102 | if (pRoot == nullptr) |
| 103 | { |
| 104 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 105 | << processName << " " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 106 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | else |
| 108 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 109 | tinyxml2::XMLElement* node = pRoot->FirstChildElement("node"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 110 | while (node != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 112 | const char* childPath = node->Attribute("name"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 113 | if (childPath != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | std::string newpath; |
| 116 | if (objectPath != "/") |
| 117 | { |
| 118 | newpath += objectPath; |
| 119 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 120 | newpath += std::string("/") + childPath; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | // introspect the subobjects as well |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 122 | introspectObjects(processName, newpath, transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 124 | |
| 125 | node = node->NextSiblingElement("node"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 128 | }, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 129 | processName, objectPath, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | "Introspect"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 131 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 132 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 133 | inline void getPropertiesForEnumerate( |
| 134 | const std::string& objectPath, const std::string& service, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 135 | const std::string& interface, |
| 136 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 137 | { |
| 138 | BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " " |
| 139 | << service << " " << interface; |
| 140 | |
| 141 | crow::connections::systemBus->async_method_call( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 142 | [asyncResp, objectPath, service, interface]( |
| 143 | const boost::system::error_code ec, |
| 144 | const std::vector<std::pair< |
| 145 | std::string, dbus::utility::DbusVariantType>>& propertiesList) { |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 146 | if (ec) |
| 147 | { |
| 148 | BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface " |
| 149 | << interface << " service " << service |
| 150 | << " failed with code " << ec; |
| 151 | return; |
| 152 | } |
| 153 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 154 | nlohmann::json& dataJson = asyncResp->res.jsonValue["data"]; |
| 155 | nlohmann::json& objectJson = dataJson[objectPath]; |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 156 | if (objectJson.is_null()) |
| 157 | { |
| 158 | objectJson = nlohmann::json::object(); |
| 159 | } |
| 160 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 161 | for (const auto& [name, value] : propertiesList) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 162 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 163 | nlohmann::json& propertyJson = objectJson[name]; |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 164 | std::visit( |
| 165 | [&propertyJson](auto&& val) { |
| 166 | if constexpr (std::is_same_v< |
| 167 | std::decay_t<decltype(val)>, |
| 168 | sdbusplus::message::unix_fd>) |
| 169 | { |
| 170 | propertyJson = val.fd; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | |
| 175 | propertyJson = val; |
| 176 | } |
| 177 | }, |
| 178 | value); |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 179 | } |
| 180 | }, |
| 181 | service, objectPath, "org.freedesktop.DBus.Properties", "GetAll", |
| 182 | interface); |
| 183 | } |
| 184 | |
| 185 | // Find any results that weren't picked up by ObjectManagers, to be |
| 186 | // called after all ObjectManagers are searched for and called. |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 187 | inline void findRemainingObjectsForEnumerate( |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 188 | const std::string& objectPath, |
| 189 | const std::shared_ptr<GetSubTreeType>& subtree, |
| 190 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 191 | { |
| 192 | BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 193 | const nlohmann::json& dataJson = asyncResp->res.jsonValue["data"]; |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 194 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 195 | for (const auto& [path, interface_map] : *subtree) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 196 | { |
| 197 | if (path == objectPath) |
| 198 | { |
| 199 | // An enumerate does not return the target path's properties |
| 200 | continue; |
| 201 | } |
| 202 | if (dataJson.find(path) == dataJson.end()) |
| 203 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 204 | for (const auto& [service, interfaces] : interface_map) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 205 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 206 | for (const auto& interface : interfaces) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 207 | { |
| 208 | if (!boost::starts_with(interface, "org.freedesktop.DBus")) |
| 209 | { |
| 210 | getPropertiesForEnumerate(path, service, interface, |
| 211 | asyncResp); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 219 | struct InProgressEnumerateData |
| 220 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 221 | InProgressEnumerateData( |
| 222 | const std::string& objectPathIn, |
| 223 | const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 224 | objectPath(objectPathIn), |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 225 | asyncResp(asyncRespIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 226 | {} |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 227 | |
| 228 | ~InProgressEnumerateData() |
| 229 | { |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 230 | findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp); |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | const std::string objectPath; |
| 234 | std::shared_ptr<GetSubTreeType> subtree; |
| 235 | std::shared_ptr<bmcweb::AsyncResp> asyncResp; |
| 236 | }; |
| 237 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 238 | inline void getManagedObjectsForEnumerate( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 239 | const std::string& objectName, const std::string& objectManagerPath, |
| 240 | const std::string& connectionName, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 241 | const std::shared_ptr<InProgressEnumerateData>& transaction) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 242 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 243 | BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << objectName |
| 244 | << " object_manager_path " << objectManagerPath |
| 245 | << " connection_name " << connectionName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 246 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 247 | [transaction, objectName, |
| 248 | connectionName](const boost::system::error_code ec, |
| 249 | const dbus::utility::ManagedObjectType& objects) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 250 | if (ec) |
| 251 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 252 | BMCWEB_LOG_ERROR << "GetManagedObjects on path " << objectName |
| 253 | << " on connection " << connectionName |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 254 | << " failed with code " << ec; |
| 255 | return; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 256 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 257 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 258 | nlohmann::json& dataJson = |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 259 | transaction->asyncResp->res.jsonValue["data"]; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 260 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 261 | for (const auto& objectPath : objects) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 262 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 263 | if (boost::starts_with(objectPath.first.str, objectName)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 264 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 265 | BMCWEB_LOG_DEBUG << "Reading object " |
| 266 | << objectPath.first.str; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 267 | nlohmann::json& objectJson = dataJson[objectPath.first.str]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | if (objectJson.is_null()) |
| 269 | { |
| 270 | objectJson = nlohmann::json::object(); |
| 271 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 272 | for (const auto& interface : objectPath.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 274 | for (const auto& property : interface.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 275 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 276 | nlohmann::json& propertyJson = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 277 | objectJson[property.first]; |
Ed Tanous | d1a6481 | 2021-12-13 12:14:05 -0800 | [diff] [blame] | 278 | std::visit( |
| 279 | [&propertyJson](auto&& val) { |
| 280 | if constexpr ( |
| 281 | std::is_same_v< |
| 282 | std::decay_t<decltype(val)>, |
| 283 | sdbusplus::message::unix_fd>) |
| 284 | { |
| 285 | propertyJson = val.fd; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | |
| 290 | propertyJson = val; |
| 291 | } |
| 292 | }, |
| 293 | property.second); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 297 | for (const auto& interface : objectPath.second) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 298 | { |
| 299 | if (interface.first == "org.freedesktop.DBus.ObjectManager") |
| 300 | { |
| 301 | getManagedObjectsForEnumerate( |
| 302 | objectPath.first.str, objectPath.first.str, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 303 | connectionName, transaction); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 306 | } |
| 307 | }, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 308 | connectionName, objectManagerPath, "org.freedesktop.DBus.ObjectManager", |
| 309 | "GetManagedObjects"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 312 | inline void findObjectManagerPathForEnumerate( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 313 | const std::string& objectName, const std::string& connectionName, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 314 | const std::shared_ptr<InProgressEnumerateData>& transaction) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 315 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 316 | BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << objectName |
| 317 | << " on connection:" << connectionName; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 318 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 319 | [transaction, objectName, connectionName]( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 320 | const boost::system::error_code ec, |
| 321 | const boost::container::flat_map< |
| 322 | std::string, boost::container::flat_map< |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 323 | std::string, std::vector<std::string>>>& |
| 324 | objects) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 325 | if (ec) |
| 326 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 327 | BMCWEB_LOG_ERROR << "GetAncestors on path " << objectName |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 328 | << " failed with code " << ec; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 332 | for (const auto& pathGroup : objects) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 333 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 334 | for (const auto& connectionGroup : pathGroup.second) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 335 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 336 | if (connectionGroup.first == connectionName) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 337 | { |
| 338 | // Found the object manager path for this resource. |
| 339 | getManagedObjectsForEnumerate( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 340 | objectName, pathGroup.first, connectionName, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 341 | transaction); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | }, |
| 347 | "xyz.openbmc_project.ObjectMapper", |
| 348 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 349 | "xyz.openbmc_project.ObjectMapper", "GetAncestors", objectName, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 350 | std::array<const char*, 1>{"org.freedesktop.DBus.ObjectManager"}); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 351 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 352 | |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 353 | // Uses GetObject to add the object info about the target /enumerate path to |
| 354 | // the results of GetSubTree, as GetSubTree will not return info for the |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 355 | // target path, and then continues on enumerating the rest of the tree. |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 356 | inline void getObjectAndEnumerate( |
| 357 | const std::shared_ptr<InProgressEnumerateData>& transaction) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 358 | { |
| 359 | using GetObjectType = |
| 360 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 361 | |
| 362 | crow::connections::systemBus->async_method_call( |
| 363 | [transaction](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 364 | const GetObjectType& objects) { |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 365 | if (ec) |
| 366 | { |
| 367 | BMCWEB_LOG_ERROR << "GetObject for path " |
| 368 | << transaction->objectPath |
| 369 | << " failed with code " << ec; |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath |
| 374 | << " has " << objects.size() << " entries"; |
| 375 | if (!objects.empty()) |
| 376 | { |
| 377 | transaction->subtree->emplace_back(transaction->objectPath, |
| 378 | objects); |
| 379 | } |
| 380 | |
| 381 | // Map indicating connection name, and the path where the object |
| 382 | // manager exists |
| 383 | boost::container::flat_map<std::string, std::string> connections; |
| 384 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 385 | for (const auto& object : *(transaction->subtree)) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 386 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 387 | for (const auto& connection : object.second) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 388 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 389 | std::string& objectManagerPath = |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 390 | connections[connection.first]; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 391 | for (const auto& interface : connection.second) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 392 | { |
| 393 | BMCWEB_LOG_DEBUG << connection.first |
| 394 | << " has interface " << interface; |
| 395 | if (interface == "org.freedesktop.DBus.ObjectManager") |
| 396 | { |
| 397 | BMCWEB_LOG_DEBUG << "found object manager path " |
| 398 | << object.first; |
| 399 | objectManagerPath = object.first; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections"; |
| 405 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 406 | for (const auto& connection : connections) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 407 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 408 | // If we already know where the object manager is, we don't |
| 409 | // need to search for it, we can call directly in to |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 410 | // getManagedObjects |
| 411 | if (!connection.second.empty()) |
| 412 | { |
| 413 | getManagedObjectsForEnumerate( |
| 414 | transaction->objectPath, connection.second, |
| 415 | connection.first, transaction); |
| 416 | } |
| 417 | else |
| 418 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 419 | // otherwise we need to find the object manager path |
| 420 | // before we can continue |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 421 | findObjectManagerPathForEnumerate( |
| 422 | transaction->objectPath, connection.first, transaction); |
| 423 | } |
| 424 | } |
| 425 | }, |
| 426 | "xyz.openbmc_project.ObjectMapper", |
| 427 | "/xyz/openbmc_project/object_mapper", |
| 428 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 429 | transaction->objectPath, std::array<const char*, 0>()); |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 430 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 431 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 432 | // Structure for storing data on an in progress action |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 433 | struct InProgressActionData |
| 434 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 435 | InProgressActionData(crow::Response& resIn) : res(resIn) |
| 436 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 437 | ~InProgressActionData() |
| 438 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 439 | // Methods could have been called across different owners |
| 440 | // and interfaces, where some calls failed and some passed. |
| 441 | // |
| 442 | // The rules for this are: |
| 443 | // * if no method was called - error |
| 444 | // * if a method failed and none passed - error |
| 445 | // (converse: if at least one method passed - OK) |
| 446 | // * for the method output: |
| 447 | // * if output processing didn't fail, return the data |
| 448 | |
| 449 | // Only deal with method returns if nothing failed earlier |
| 450 | if (res.result() == boost::beast::http::status::ok) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 451 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 452 | if (!methodPassed) |
| 453 | { |
Matt Spinler | 06b1b63 | 2019-06-18 16:09:25 -0500 | [diff] [blame] | 454 | if (!methodFailed) |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 455 | { |
| 456 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 457 | methodNotFoundDesc, notFoundMsg); |
| 458 | } |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | if (outputFailed) |
| 463 | { |
| 464 | setErrorResponse( |
| 465 | res, boost::beast::http::status::internal_server_error, |
| 466 | "Method output failure", methodOutputFailedMsg); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | res.jsonValue = {{"status", "ok"}, |
| 471 | {"message", "200 OK"}, |
| 472 | {"data", methodResponse}}; |
| 473 | } |
| 474 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 475 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 476 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 477 | res.end(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 478 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 479 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 480 | void setErrorStatus(const std::string& desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 481 | { |
Matt Spinler | c0eb9bd | 2019-01-09 15:22:30 -0600 | [diff] [blame] | 482 | setErrorResponse(res, boost::beast::http::status::bad_request, desc, |
| 483 | badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 484 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 485 | crow::Response& res; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 486 | std::string path; |
| 487 | std::string methodName; |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 488 | std::string interfaceName; |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 489 | bool methodPassed = false; |
| 490 | bool methodFailed = false; |
| 491 | bool outputFailed = false; |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 492 | bool convertedToArray = false; |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 493 | nlohmann::json methodResponse; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 494 | nlohmann::json arguments; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 497 | inline std::vector<std::string> dbusArgSplit(const std::string& string) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 498 | { |
| 499 | std::vector<std::string> ret; |
| 500 | if (string.empty()) |
| 501 | { |
| 502 | return ret; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 503 | } |
Ed Tanous | 0f0353b | 2019-10-24 11:37:51 -0700 | [diff] [blame] | 504 | ret.emplace_back(""); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 505 | int containerDepth = 0; |
| 506 | |
| 507 | for (std::string::const_iterator character = string.begin(); |
| 508 | character != string.end(); character++) |
| 509 | { |
| 510 | ret.back() += *character; |
| 511 | switch (*character) |
| 512 | { |
| 513 | case ('a'): |
| 514 | break; |
| 515 | case ('('): |
| 516 | case ('{'): |
| 517 | containerDepth++; |
| 518 | break; |
| 519 | case ('}'): |
| 520 | case (')'): |
| 521 | containerDepth--; |
| 522 | if (containerDepth == 0) |
| 523 | { |
| 524 | if (character + 1 != string.end()) |
| 525 | { |
Ed Tanous | 0f0353b | 2019-10-24 11:37:51 -0700 | [diff] [blame] | 526 | ret.emplace_back(""); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | break; |
| 530 | default: |
| 531 | if (containerDepth == 0) |
| 532 | { |
| 533 | if (character + 1 != string.end()) |
| 534 | { |
Ed Tanous | 0f0353b | 2019-10-24 11:37:51 -0700 | [diff] [blame] | 535 | ret.emplace_back(""); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | } |
Matt Spinler | 4ae611d | 2019-01-11 15:37:06 -0600 | [diff] [blame] | 541 | |
| 542 | return ret; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 545 | inline int convertJsonToDbus(sd_bus_message* m, const std::string& argType, |
| 546 | const nlohmann::json& inputJson) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 547 | { |
| 548 | int r = 0; |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 549 | BMCWEB_LOG_DEBUG << "Converting " |
| 550 | << inputJson.dump(2, ' ', true, |
| 551 | nlohmann::json::error_handler_t::replace) |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 552 | << " to type: " << argType; |
| 553 | const std::vector<std::string> argTypes = dbusArgSplit(argType); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 554 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 555 | // Assume a single object for now. |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 556 | const nlohmann::json* j = &inputJson; |
| 557 | nlohmann::json::const_iterator jIt = inputJson.begin(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 558 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 559 | for (const std::string& argCode : argTypes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 560 | { |
| 561 | // If we are decoding multiple objects, grab the pointer to the |
| 562 | // iterator, and increment it for the next loop |
| 563 | if (argTypes.size() > 1) |
| 564 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 565 | if (jIt == inputJson.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 566 | { |
| 567 | return -2; |
| 568 | } |
| 569 | j = &*jIt; |
| 570 | jIt++; |
| 571 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 572 | const int64_t* intValue = j->get_ptr<const int64_t*>(); |
| 573 | const std::string* stringValue = j->get_ptr<const std::string*>(); |
| 574 | const double* doubleValue = j->get_ptr<const double*>(); |
| 575 | const bool* b = j->get_ptr<const bool*>(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 576 | int64_t v = 0; |
| 577 | double d = 0.0; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 578 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 579 | // Do some basic type conversions that make sense. uint can be |
| 580 | // converted to int. int and uint can be converted to double |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 581 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 582 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 583 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 584 | if (uintValue != nullptr) |
| 585 | { |
| 586 | v = static_cast<int64_t>(*uintValue); |
| 587 | intValue = &v; |
| 588 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 589 | } |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 590 | if (doubleValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 591 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 592 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 593 | if (uintValue != nullptr) |
| 594 | { |
| 595 | d = static_cast<double>(*uintValue); |
| 596 | doubleValue = &d; |
| 597 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 598 | } |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 599 | if (doubleValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 600 | { |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 601 | if (intValue != nullptr) |
| 602 | { |
| 603 | d = static_cast<double>(*intValue); |
| 604 | doubleValue = &d; |
| 605 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 608 | if (argCode == "s") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 609 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 610 | if (stringValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 611 | { |
| 612 | return -1; |
| 613 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 614 | r = sd_bus_message_append_basic( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 615 | m, argCode[0], static_cast<const void*>(stringValue->data())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 616 | if (r < 0) |
| 617 | { |
| 618 | return r; |
| 619 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 620 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 621 | else if (argCode == "i") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 622 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 623 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 624 | { |
| 625 | return -1; |
| 626 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 627 | if ((*intValue < std::numeric_limits<int32_t>::lowest()) || |
| 628 | (*intValue > std::numeric_limits<int32_t>::max())) |
| 629 | { |
| 630 | return -ERANGE; |
| 631 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 632 | int32_t i = static_cast<int32_t>(*intValue); |
| 633 | r = sd_bus_message_append_basic(m, argCode[0], &i); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 634 | if (r < 0) |
| 635 | { |
| 636 | return r; |
| 637 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 638 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 639 | else if (argCode == "b") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 640 | { |
| 641 | // lots of ways bool could be represented here. Try them all |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 642 | int boolInt = false; |
| 643 | if (intValue != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 644 | { |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 645 | if (*intValue == 1) |
| 646 | { |
| 647 | boolInt = true; |
| 648 | } |
| 649 | else if (*intValue == 0) |
| 650 | { |
| 651 | boolInt = false; |
| 652 | } |
| 653 | else |
| 654 | { |
| 655 | return -ERANGE; |
| 656 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | } |
| 658 | else if (b != nullptr) |
| 659 | { |
Matt Spinler | a2f0263 | 2019-01-18 10:15:35 -0600 | [diff] [blame] | 660 | boolInt = *b ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 661 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 662 | else if (stringValue != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 663 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 664 | boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 665 | } |
| 666 | else |
| 667 | { |
| 668 | return -1; |
| 669 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 670 | r = sd_bus_message_append_basic(m, argCode[0], &boolInt); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 671 | if (r < 0) |
| 672 | { |
| 673 | return r; |
| 674 | } |
| 675 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 676 | else if (argCode == "n") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 677 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 678 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 679 | { |
| 680 | return -1; |
| 681 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 682 | if ((*intValue < std::numeric_limits<int16_t>::lowest()) || |
| 683 | (*intValue > std::numeric_limits<int16_t>::max())) |
| 684 | { |
| 685 | return -ERANGE; |
| 686 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 687 | int16_t n = static_cast<int16_t>(*intValue); |
| 688 | r = sd_bus_message_append_basic(m, argCode[0], &n); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 689 | if (r < 0) |
| 690 | { |
| 691 | return r; |
| 692 | } |
| 693 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 694 | else if (argCode == "x") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 695 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 696 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 697 | { |
| 698 | return -1; |
| 699 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 700 | r = sd_bus_message_append_basic(m, argCode[0], intValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 701 | if (r < 0) |
| 702 | { |
| 703 | return r; |
| 704 | } |
| 705 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 706 | else if (argCode == "y") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 707 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 708 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 709 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 710 | { |
| 711 | return -1; |
| 712 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 713 | if (*uintValue > std::numeric_limits<uint8_t>::max()) |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 714 | { |
| 715 | return -ERANGE; |
| 716 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 717 | uint8_t y = static_cast<uint8_t>(*uintValue); |
| 718 | r = sd_bus_message_append_basic(m, argCode[0], &y); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 719 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 720 | else if (argCode == "q") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 721 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 722 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 723 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 724 | { |
| 725 | return -1; |
| 726 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 727 | if (*uintValue > std::numeric_limits<uint16_t>::max()) |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 728 | { |
| 729 | return -ERANGE; |
| 730 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 731 | uint16_t q = static_cast<uint16_t>(*uintValue); |
| 732 | r = sd_bus_message_append_basic(m, argCode[0], &q); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 733 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 734 | else if (argCode == "u") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 735 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 736 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 737 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 738 | { |
| 739 | return -1; |
| 740 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 741 | if (*uintValue > std::numeric_limits<uint32_t>::max()) |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 742 | { |
| 743 | return -ERANGE; |
| 744 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 745 | uint32_t u = static_cast<uint32_t>(*uintValue); |
| 746 | r = sd_bus_message_append_basic(m, argCode[0], &u); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 747 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 748 | else if (argCode == "t") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 749 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 750 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 751 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 752 | { |
| 753 | return -1; |
| 754 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 755 | r = sd_bus_message_append_basic(m, argCode[0], uintValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 756 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 757 | else if (argCode == "d") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 758 | { |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 759 | if (doubleValue == nullptr) |
| 760 | { |
| 761 | return -1; |
| 762 | } |
| 763 | if ((*doubleValue < std::numeric_limits<double>::lowest()) || |
| 764 | (*doubleValue > std::numeric_limits<double>::max())) |
| 765 | { |
| 766 | return -ERANGE; |
| 767 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 768 | sd_bus_message_append_basic(m, argCode[0], doubleValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 769 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 770 | else if (boost::starts_with(argCode, "a")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 771 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 772 | std::string containedType = argCode.substr(1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 773 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 774 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 775 | if (r < 0) |
| 776 | { |
| 777 | return r; |
| 778 | } |
| 779 | |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 780 | for (const auto& it : *j) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 781 | { |
Ed Tanous | 0dfeda6 | 2019-10-24 11:21:38 -0700 | [diff] [blame] | 782 | r = convertJsonToDbus(m, containedType, it); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 783 | if (r < 0) |
| 784 | { |
| 785 | return r; |
| 786 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 787 | } |
| 788 | sd_bus_message_close_container(m); |
| 789 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 790 | else if (boost::starts_with(argCode, "v")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 791 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 792 | std::string containedType = argCode.substr(1); |
| 793 | BMCWEB_LOG_DEBUG << "variant type: " << argCode |
| 794 | << " appending variant of type: " << containedType; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 795 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 796 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 797 | if (r < 0) |
| 798 | { |
| 799 | return r; |
| 800 | } |
| 801 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 802 | r = convertJsonToDbus(m, containedType, inputJson); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 803 | if (r < 0) |
| 804 | { |
| 805 | return r; |
| 806 | } |
| 807 | |
| 808 | r = sd_bus_message_close_container(m); |
| 809 | if (r < 0) |
| 810 | { |
| 811 | return r; |
| 812 | } |
| 813 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 814 | else if (boost::starts_with(argCode, "(") && |
| 815 | boost::ends_with(argCode, ")")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 816 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 817 | std::string containedType = argCode.substr(1, argCode.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 818 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 819 | containedType.c_str()); |
Ed Tanous | f1eebf0 | 2019-03-04 15:57:09 -0800 | [diff] [blame] | 820 | if (r < 0) |
| 821 | { |
| 822 | return r; |
| 823 | } |
| 824 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 825 | nlohmann::json::const_iterator it = j->begin(); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 826 | for (const std::string& argCode2 : dbusArgSplit(argType)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 827 | { |
| 828 | if (it == j->end()) |
| 829 | { |
| 830 | return -1; |
| 831 | } |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 832 | r = convertJsonToDbus(m, argCode2, *it); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 833 | if (r < 0) |
| 834 | { |
| 835 | return r; |
| 836 | } |
| 837 | it++; |
| 838 | } |
| 839 | r = sd_bus_message_close_container(m); |
| 840 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 841 | else if (boost::starts_with(argCode, "{") && |
| 842 | boost::ends_with(argCode, "}")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 843 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 844 | std::string containedType = argCode.substr(1, argCode.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 845 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 846 | containedType.c_str()); |
Ed Tanous | f1eebf0 | 2019-03-04 15:57:09 -0800 | [diff] [blame] | 847 | if (r < 0) |
| 848 | { |
| 849 | return r; |
| 850 | } |
| 851 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 852 | std::vector<std::string> codes = dbusArgSplit(containedType); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 853 | if (codes.size() != 2) |
| 854 | { |
| 855 | return -1; |
| 856 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 857 | const std::string& keyType = codes[0]; |
| 858 | const std::string& valueType = codes[1]; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 859 | for (const auto& it : j->items()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 860 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 861 | r = convertJsonToDbus(m, keyType, it.key()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 862 | if (r < 0) |
| 863 | { |
| 864 | return r; |
| 865 | } |
| 866 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 867 | r = convertJsonToDbus(m, valueType, it.value()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 868 | if (r < 0) |
| 869 | { |
| 870 | return r; |
| 871 | } |
| 872 | } |
| 873 | r = sd_bus_message_close_container(m); |
| 874 | } |
| 875 | else |
| 876 | { |
| 877 | return -2; |
| 878 | } |
| 879 | if (r < 0) |
| 880 | { |
| 881 | return r; |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 882 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 883 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 884 | if (argTypes.size() > 1) |
| 885 | { |
| 886 | jIt++; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 887 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 888 | } |
Matt Spinler | 127ea54 | 2019-01-14 11:04:28 -0600 | [diff] [blame] | 889 | |
| 890 | return r; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 893 | template <typename T> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 894 | int readMessageItem(const std::string& typeCode, sdbusplus::message::message& m, |
| 895 | nlohmann::json& data) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 896 | { |
| 897 | T value; |
| 898 | |
| 899 | int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value); |
| 900 | if (r < 0) |
| 901 | { |
| 902 | BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode |
| 903 | << " failed!"; |
| 904 | return r; |
| 905 | } |
| 906 | |
| 907 | data = value; |
| 908 | return 0; |
| 909 | } |
| 910 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 911 | int convertDBusToJSON(const std::string& returnType, |
| 912 | sdbusplus::message::message& m, nlohmann::json& response); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 913 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 914 | inline int readDictEntryFromMessage(const std::string& typeCode, |
| 915 | sdbusplus::message::message& m, |
| 916 | nlohmann::json& object) |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 917 | { |
| 918 | std::vector<std::string> types = dbusArgSplit(typeCode); |
| 919 | if (types.size() != 2) |
| 920 | { |
| 921 | BMCWEB_LOG_ERROR << "wrong number contained types in dictionary: " |
| 922 | << types.size(); |
| 923 | return -1; |
| 924 | } |
| 925 | |
| 926 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_DICT_ENTRY, |
| 927 | typeCode.c_str()); |
| 928 | if (r < 0) |
| 929 | { |
| 930 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container with rc " << r; |
| 931 | return r; |
| 932 | } |
| 933 | |
| 934 | nlohmann::json key; |
| 935 | r = convertDBusToJSON(types[0], m, key); |
| 936 | if (r < 0) |
| 937 | { |
| 938 | return r; |
| 939 | } |
| 940 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 941 | const std::string* keyPtr = key.get_ptr<const std::string*>(); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 942 | if (keyPtr == nullptr) |
| 943 | { |
| 944 | // json doesn't support non-string keys. If we hit this condition, |
| 945 | // convert the result to a string so we can proceed |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 946 | key = key.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 947 | keyPtr = key.get_ptr<const std::string*>(); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 948 | // in theory this can't fail now, but lets be paranoid about it |
| 949 | // anyway |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 950 | if (keyPtr == nullptr) |
| 951 | { |
| 952 | return -1; |
| 953 | } |
| 954 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 955 | nlohmann::json& value = object[*keyPtr]; |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 956 | |
| 957 | r = convertDBusToJSON(types[1], m, value); |
| 958 | if (r < 0) |
| 959 | { |
| 960 | return r; |
| 961 | } |
| 962 | |
| 963 | r = sd_bus_message_exit_container(m.get()); |
| 964 | if (r < 0) |
| 965 | { |
| 966 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 967 | return r; |
| 968 | } |
| 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 973 | inline int readArrayFromMessage(const std::string& typeCode, |
| 974 | sdbusplus::message::message& m, |
| 975 | nlohmann::json& data) |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 976 | { |
| 977 | if (typeCode.size() < 2) |
| 978 | { |
| 979 | BMCWEB_LOG_ERROR << "Type code " << typeCode |
| 980 | << " too small for an array"; |
| 981 | return -1; |
| 982 | } |
| 983 | |
| 984 | std::string containedType = typeCode.substr(1); |
| 985 | |
| 986 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY, |
| 987 | containedType.c_str()); |
| 988 | if (r < 0) |
| 989 | { |
| 990 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 991 | << r; |
| 992 | return r; |
| 993 | } |
| 994 | |
| 995 | bool dict = boost::starts_with(containedType, "{") && |
| 996 | boost::ends_with(containedType, "}"); |
| 997 | |
| 998 | if (dict) |
| 999 | { |
| 1000 | // Remove the { } |
| 1001 | containedType = containedType.substr(1, containedType.size() - 2); |
| 1002 | data = nlohmann::json::object(); |
| 1003 | } |
| 1004 | else |
| 1005 | { |
| 1006 | data = nlohmann::json::array(); |
| 1007 | } |
| 1008 | |
| 1009 | while (true) |
| 1010 | { |
| 1011 | r = sd_bus_message_at_end(m.get(), false); |
| 1012 | if (r < 0) |
| 1013 | { |
| 1014 | BMCWEB_LOG_ERROR << "sd_bus_message_at_end failed"; |
| 1015 | return r; |
| 1016 | } |
| 1017 | |
| 1018 | if (r > 0) |
| 1019 | { |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | // Dictionaries are only ever seen in an array |
| 1024 | if (dict) |
| 1025 | { |
| 1026 | r = readDictEntryFromMessage(containedType, m, data); |
| 1027 | if (r < 0) |
| 1028 | { |
| 1029 | return r; |
| 1030 | } |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | data.push_back(nlohmann::json()); |
| 1035 | |
| 1036 | r = convertDBusToJSON(containedType, m, data.back()); |
| 1037 | if (r < 0) |
| 1038 | { |
| 1039 | return r; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | r = sd_bus_message_exit_container(m.get()); |
| 1045 | if (r < 0) |
| 1046 | { |
| 1047 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 1048 | return r; |
| 1049 | } |
| 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1054 | inline int readStructFromMessage(const std::string& typeCode, |
| 1055 | sdbusplus::message::message& m, |
| 1056 | nlohmann::json& data) |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1057 | { |
| 1058 | if (typeCode.size() < 3) |
| 1059 | { |
| 1060 | BMCWEB_LOG_ERROR << "Type code " << typeCode |
| 1061 | << " too small for a struct"; |
| 1062 | return -1; |
| 1063 | } |
| 1064 | |
| 1065 | std::string containedTypes = typeCode.substr(1, typeCode.size() - 2); |
| 1066 | std::vector<std::string> types = dbusArgSplit(containedTypes); |
| 1067 | |
| 1068 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_STRUCT, |
| 1069 | containedTypes.c_str()); |
| 1070 | if (r < 0) |
| 1071 | { |
| 1072 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 1073 | << r; |
| 1074 | return r; |
| 1075 | } |
| 1076 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1077 | for (const std::string& type : types) |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1078 | { |
| 1079 | data.push_back(nlohmann::json()); |
| 1080 | r = convertDBusToJSON(type, m, data.back()); |
| 1081 | if (r < 0) |
| 1082 | { |
| 1083 | return r; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | r = sd_bus_message_exit_container(m.get()); |
| 1088 | if (r < 0) |
| 1089 | { |
| 1090 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 1091 | return r; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1096 | inline int readVariantFromMessage(sdbusplus::message::message& m, |
| 1097 | nlohmann::json& data) |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1098 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1099 | const char* containerType; |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 1100 | int r = sd_bus_message_peek_type(m.get(), nullptr, &containerType); |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1101 | if (r < 0) |
| 1102 | { |
| 1103 | BMCWEB_LOG_ERROR << "sd_bus_message_peek_type failed"; |
| 1104 | return r; |
| 1105 | } |
| 1106 | |
| 1107 | r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_VARIANT, |
| 1108 | containerType); |
| 1109 | if (r < 0) |
| 1110 | { |
| 1111 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 1112 | << r; |
| 1113 | return r; |
| 1114 | } |
| 1115 | |
| 1116 | r = convertDBusToJSON(containerType, m, data); |
| 1117 | if (r < 0) |
| 1118 | { |
| 1119 | return r; |
| 1120 | } |
| 1121 | |
| 1122 | r = sd_bus_message_exit_container(m.get()); |
| 1123 | if (r < 0) |
| 1124 | { |
| 1125 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed"; |
| 1126 | return r; |
| 1127 | } |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1132 | inline int convertDBusToJSON(const std::string& returnType, |
| 1133 | sdbusplus::message::message& m, |
| 1134 | nlohmann::json& response) |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1135 | { |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1136 | int r = 0; |
| 1137 | const std::vector<std::string> returnTypes = dbusArgSplit(returnType); |
| 1138 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1139 | for (const std::string& typeCode : returnTypes) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1140 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1141 | nlohmann::json* thisElement = &response; |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1142 | if (returnTypes.size() > 1) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1143 | { |
| 1144 | response.push_back(nlohmann::json{}); |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1145 | thisElement = &response.back(); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1146 | } |
| 1147 | |
Ed Tanous | d4d2579 | 2020-09-29 15:15:03 -0700 | [diff] [blame] | 1148 | if (typeCode == "s" || typeCode == "g" || typeCode == "o") |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1149 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1150 | r = readMessageItem<char*>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1151 | if (r < 0) |
| 1152 | { |
| 1153 | return r; |
| 1154 | } |
| 1155 | } |
| 1156 | else if (typeCode == "b") |
| 1157 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1158 | r = readMessageItem<int>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1159 | if (r < 0) |
| 1160 | { |
| 1161 | return r; |
| 1162 | } |
| 1163 | |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1164 | *thisElement = static_cast<bool>(thisElement->get<int>()); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1165 | } |
| 1166 | else if (typeCode == "u") |
| 1167 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1168 | r = readMessageItem<uint32_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1169 | if (r < 0) |
| 1170 | { |
| 1171 | return r; |
| 1172 | } |
| 1173 | } |
| 1174 | else if (typeCode == "i") |
| 1175 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1176 | r = readMessageItem<int32_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1177 | if (r < 0) |
| 1178 | { |
| 1179 | return r; |
| 1180 | } |
| 1181 | } |
| 1182 | else if (typeCode == "x") |
| 1183 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1184 | r = readMessageItem<int64_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1185 | if (r < 0) |
| 1186 | { |
| 1187 | return r; |
| 1188 | } |
| 1189 | } |
| 1190 | else if (typeCode == "t") |
| 1191 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1192 | r = readMessageItem<uint64_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1193 | if (r < 0) |
| 1194 | { |
| 1195 | return r; |
| 1196 | } |
| 1197 | } |
| 1198 | else if (typeCode == "n") |
| 1199 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1200 | r = readMessageItem<int16_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1201 | if (r < 0) |
| 1202 | { |
| 1203 | return r; |
| 1204 | } |
| 1205 | } |
| 1206 | else if (typeCode == "q") |
| 1207 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1208 | r = readMessageItem<uint16_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1209 | if (r < 0) |
| 1210 | { |
| 1211 | return r; |
| 1212 | } |
| 1213 | } |
| 1214 | else if (typeCode == "y") |
| 1215 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1216 | r = readMessageItem<uint8_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1217 | if (r < 0) |
| 1218 | { |
| 1219 | return r; |
| 1220 | } |
| 1221 | } |
| 1222 | else if (typeCode == "d") |
| 1223 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1224 | r = readMessageItem<double>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1225 | if (r < 0) |
| 1226 | { |
| 1227 | return r; |
| 1228 | } |
| 1229 | } |
| 1230 | else if (typeCode == "h") |
| 1231 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1232 | r = readMessageItem<int>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1233 | if (r < 0) |
| 1234 | { |
| 1235 | return r; |
| 1236 | } |
| 1237 | } |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1238 | else if (boost::starts_with(typeCode, "a")) |
| 1239 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1240 | r = readArrayFromMessage(typeCode, m, *thisElement); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1241 | if (r < 0) |
| 1242 | { |
| 1243 | return r; |
| 1244 | } |
| 1245 | } |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1246 | else if (boost::starts_with(typeCode, "(") && |
| 1247 | boost::ends_with(typeCode, ")")) |
| 1248 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1249 | r = readStructFromMessage(typeCode, m, *thisElement); |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1250 | if (r < 0) |
| 1251 | { |
| 1252 | return r; |
| 1253 | } |
| 1254 | } |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1255 | else if (boost::starts_with(typeCode, "v")) |
| 1256 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1257 | r = readVariantFromMessage(m, *thisElement); |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1258 | if (r < 0) |
| 1259 | { |
| 1260 | return r; |
| 1261 | } |
| 1262 | } |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1263 | else |
| 1264 | { |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1265 | BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode; |
| 1266 | return -2; |
| 1267 | } |
| 1268 | } |
| 1269 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1273 | inline void handleMethodResponse( |
| 1274 | const std::shared_ptr<InProgressActionData>& transaction, |
| 1275 | sdbusplus::message::message& m, const std::string& returnType) |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1276 | { |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1277 | nlohmann::json data; |
| 1278 | |
| 1279 | int r = convertDBusToJSON(returnType, m, data); |
| 1280 | if (r < 0) |
| 1281 | { |
| 1282 | transaction->outputFailed = true; |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | if (data.is_null()) |
| 1287 | { |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | if (transaction->methodResponse.is_null()) |
| 1292 | { |
| 1293 | transaction->methodResponse = std::move(data); |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | // If they're both dictionaries or arrays, merge into one. |
| 1298 | // Otherwise, make the results an array with every result |
| 1299 | // an entry. Could also just fail in that case, but it |
| 1300 | // seems better to get the data back somehow. |
| 1301 | |
| 1302 | if (transaction->methodResponse.is_object() && data.is_object()) |
| 1303 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1304 | for (const auto& obj : data.items()) |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1305 | { |
| 1306 | // Note: Will overwrite the data for a duplicate key |
| 1307 | transaction->methodResponse.emplace(obj.key(), |
| 1308 | std::move(obj.value())); |
| 1309 | } |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | if (transaction->methodResponse.is_array() && data.is_array()) |
| 1314 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1315 | for (auto& obj : data) |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1316 | { |
| 1317 | transaction->methodResponse.push_back(std::move(obj)); |
| 1318 | } |
| 1319 | return; |
| 1320 | } |
| 1321 | |
| 1322 | if (!transaction->convertedToArray) |
| 1323 | { |
| 1324 | // They are different types. May as well turn them into an array |
| 1325 | nlohmann::json j = std::move(transaction->methodResponse); |
| 1326 | transaction->methodResponse = nlohmann::json::array(); |
| 1327 | transaction->methodResponse.push_back(std::move(j)); |
| 1328 | transaction->methodResponse.push_back(std::move(data)); |
| 1329 | transaction->convertedToArray = true; |
| 1330 | } |
| 1331 | else |
| 1332 | { |
| 1333 | transaction->methodResponse.push_back(std::move(data)); |
| 1334 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1335 | } |
| 1336 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1337 | inline void findActionOnInterface( |
| 1338 | const std::shared_ptr<InProgressActionData>& transaction, |
| 1339 | const std::string& connectionName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1340 | { |
| 1341 | BMCWEB_LOG_DEBUG << "findActionOnInterface for connection " |
| 1342 | << connectionName; |
| 1343 | crow::connections::systemBus->async_method_call( |
| 1344 | [transaction, connectionName{std::string(connectionName)}]( |
| 1345 | const boost::system::error_code ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1346 | const std::string& introspectXml) { |
| 1347 | BMCWEB_LOG_DEBUG << "got xml:\n " << introspectXml; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1348 | if (ec) |
| 1349 | { |
| 1350 | BMCWEB_LOG_ERROR |
| 1351 | << "Introspect call failed with error: " << ec.message() |
| 1352 | << " on process: " << connectionName << "\n"; |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1353 | return; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1354 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1355 | tinyxml2::XMLDocument doc; |
| 1356 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1357 | doc.Parse(introspectXml.data(), introspectXml.size()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1358 | tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node"); |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1359 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1360 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1361 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 1362 | << connectionName << "\n"; |
| 1363 | return; |
| 1364 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1365 | tinyxml2::XMLElement* interfaceNode = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1366 | pRoot->FirstChildElement("interface"); |
| 1367 | while (interfaceNode != nullptr) |
| 1368 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1369 | const char* thisInterfaceName = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1370 | interfaceNode->Attribute("name"); |
| 1371 | if (thisInterfaceName != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1372 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1373 | if (!transaction->interfaceName.empty() && |
| 1374 | (transaction->interfaceName != thisInterfaceName)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1375 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1376 | interfaceNode = |
| 1377 | interfaceNode->NextSiblingElement("interface"); |
| 1378 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1379 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1380 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1381 | tinyxml2::XMLElement* methodNode = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1382 | interfaceNode->FirstChildElement("method"); |
| 1383 | while (methodNode != nullptr) |
| 1384 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1385 | const char* thisMethodName = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1386 | methodNode->Attribute("name"); |
| 1387 | BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName; |
| 1388 | if (thisMethodName != nullptr && |
| 1389 | thisMethodName == transaction->methodName) |
| 1390 | { |
| 1391 | BMCWEB_LOG_DEBUG |
| 1392 | << "Found method named " << thisMethodName |
| 1393 | << " on interface " << thisInterfaceName; |
| 1394 | sdbusplus::message::message m = |
| 1395 | crow::connections::systemBus->new_method_call( |
| 1396 | connectionName.c_str(), |
| 1397 | transaction->path.c_str(), |
| 1398 | thisInterfaceName, |
| 1399 | transaction->methodName.c_str()); |
| 1400 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1401 | tinyxml2::XMLElement* argumentNode = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1402 | methodNode->FirstChildElement("arg"); |
| 1403 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1404 | std::string returnType; |
| 1405 | |
| 1406 | // Find the output type |
| 1407 | while (argumentNode != nullptr) |
| 1408 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1409 | const char* argDirection = |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1410 | argumentNode->Attribute("direction"); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1411 | const char* argType = |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1412 | argumentNode->Attribute("type"); |
| 1413 | if (argDirection != nullptr && |
| 1414 | argType != nullptr && |
| 1415 | std::string(argDirection) == "out") |
| 1416 | { |
| 1417 | returnType = argType; |
| 1418 | break; |
| 1419 | } |
| 1420 | argumentNode = |
| 1421 | argumentNode->NextSiblingElement("arg"); |
| 1422 | } |
| 1423 | |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1424 | nlohmann::json::const_iterator argIt = |
| 1425 | transaction->arguments.begin(); |
| 1426 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1427 | argumentNode = methodNode->FirstChildElement("arg"); |
| 1428 | |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1429 | while (argumentNode != nullptr) |
| 1430 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1431 | const char* argDirection = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1432 | argumentNode->Attribute("direction"); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1433 | const char* argType = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1434 | argumentNode->Attribute("type"); |
| 1435 | if (argDirection != nullptr && |
| 1436 | argType != nullptr && |
| 1437 | std::string(argDirection) == "in") |
| 1438 | { |
| 1439 | if (argIt == transaction->arguments.end()) |
| 1440 | { |
| 1441 | transaction->setErrorStatus( |
| 1442 | "Invalid method args"); |
| 1443 | return; |
| 1444 | } |
| 1445 | if (convertJsonToDbus(m.get(), |
| 1446 | std::string(argType), |
| 1447 | *argIt) < 0) |
| 1448 | { |
| 1449 | transaction->setErrorStatus( |
| 1450 | "Invalid method arg type"); |
| 1451 | return; |
| 1452 | } |
| 1453 | |
| 1454 | argIt++; |
| 1455 | } |
| 1456 | argumentNode = |
| 1457 | argumentNode->NextSiblingElement("arg"); |
| 1458 | } |
| 1459 | |
| 1460 | crow::connections::systemBus->async_send( |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1461 | m, [transaction, returnType]( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1462 | boost::system::error_code ec2, |
| 1463 | sdbusplus::message::message& m2) { |
| 1464 | if (ec2) |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1465 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1466 | transaction->methodFailed = true; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1467 | const sd_bus_error* e = m2.get_error(); |
Matt Spinler | 06b1b63 | 2019-06-18 16:09:25 -0500 | [diff] [blame] | 1468 | |
| 1469 | if (e) |
| 1470 | { |
| 1471 | setErrorResponse( |
| 1472 | transaction->res, |
| 1473 | boost::beast::http::status:: |
| 1474 | bad_request, |
| 1475 | e->name, e->message); |
| 1476 | } |
| 1477 | else |
| 1478 | { |
| 1479 | setErrorResponse( |
| 1480 | transaction->res, |
| 1481 | boost::beast::http::status:: |
| 1482 | bad_request, |
| 1483 | "Method call failed", |
| 1484 | methodFailedMsg); |
| 1485 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1486 | return; |
| 1487 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1488 | transaction->methodPassed = true; |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1489 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1490 | handleMethodResponse(transaction, m2, |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1491 | returnType); |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1492 | }); |
| 1493 | break; |
| 1494 | } |
| 1495 | methodNode = methodNode->NextSiblingElement("method"); |
| 1496 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1497 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1498 | interfaceNode = interfaceNode->NextSiblingElement("interface"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1499 | } |
| 1500 | }, |
| 1501 | connectionName, transaction->path, |
| 1502 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1505 | inline void handleAction(const crow::Request& req, |
| 1506 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1507 | const std::string& objectPath, |
| 1508 | const std::string& methodName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1509 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1510 | BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method " |
| 1511 | << methodName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1512 | nlohmann::json requestDbusData = |
| 1513 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1514 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1515 | if (requestDbusData.is_discarded()) |
| 1516 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1517 | setErrorResponse(asyncResp->res, |
| 1518 | boost::beast::http::status::bad_request, noJsonDesc, |
| 1519 | badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1520 | return; |
| 1521 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1522 | nlohmann::json::iterator data = requestDbusData.find("data"); |
| 1523 | if (data == requestDbusData.end()) |
| 1524 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1525 | setErrorResponse(asyncResp->res, |
| 1526 | boost::beast::http::status::bad_request, noJsonDesc, |
| 1527 | badReqMsg); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | if (!data->is_array()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1532 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1533 | setErrorResponse(asyncResp->res, |
| 1534 | boost::beast::http::status::bad_request, noJsonDesc, |
| 1535 | badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1536 | return; |
| 1537 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1538 | auto transaction = std::make_shared<InProgressActionData>(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1539 | |
| 1540 | transaction->path = objectPath; |
| 1541 | transaction->methodName = methodName; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1542 | transaction->arguments = std::move(*data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1543 | crow::connections::systemBus->async_method_call( |
| 1544 | [transaction]( |
| 1545 | const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1546 | const std::vector<std::pair<std::string, std::vector<std::string>>>& |
| 1547 | interfaceNames) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1548 | if (ec || interfaceNames.size() <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1549 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1550 | BMCWEB_LOG_ERROR << "Can't find object"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1551 | setErrorResponse(transaction->res, |
| 1552 | boost::beast::http::status::not_found, |
| 1553 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1554 | return; |
| 1555 | } |
| 1556 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1557 | BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size() |
| 1558 | << " object(s)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1559 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1560 | for (const std::pair<std::string, std::vector<std::string>>& |
| 1561 | object : interfaceNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1562 | { |
| 1563 | findActionOnInterface(transaction, object.first); |
| 1564 | } |
| 1565 | }, |
| 1566 | "xyz.openbmc_project.ObjectMapper", |
| 1567 | "/xyz/openbmc_project/object_mapper", |
| 1568 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
| 1569 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1572 | inline void handleDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1573 | const std::string& objectPath) |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1574 | { |
| 1575 | BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath; |
| 1576 | |
| 1577 | crow::connections::systemBus->async_method_call( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1578 | [asyncResp, objectPath]( |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1579 | const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1580 | const std::vector<std::pair<std::string, std::vector<std::string>>>& |
| 1581 | interfaceNames) { |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1582 | if (ec || interfaceNames.size() <= 0) |
| 1583 | { |
| 1584 | BMCWEB_LOG_ERROR << "Can't find object"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1585 | setErrorResponse(asyncResp->res, |
Matt Spinler | 62d2e8b | 2019-01-22 13:45:51 -0600 | [diff] [blame] | 1586 | boost::beast::http::status::method_not_allowed, |
| 1587 | methodNotAllowedDesc, methodNotAllowedMsg); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1588 | return; |
| 1589 | } |
| 1590 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1591 | auto transaction = |
| 1592 | std::make_shared<InProgressActionData>(asyncResp->res); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1593 | transaction->path = objectPath; |
| 1594 | transaction->methodName = "Delete"; |
| 1595 | transaction->interfaceName = "xyz.openbmc_project.Object.Delete"; |
| 1596 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1597 | for (const std::pair<std::string, std::vector<std::string>>& |
| 1598 | object : interfaceNames) |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1599 | { |
| 1600 | findActionOnInterface(transaction, object.first); |
| 1601 | } |
| 1602 | }, |
| 1603 | "xyz.openbmc_project.ObjectMapper", |
| 1604 | "/xyz/openbmc_project/object_mapper", |
| 1605 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1606 | std::array<const char*, 0>()); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1607 | } |
| 1608 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1609 | inline void handleList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1610 | const std::string& objectPath, int32_t depth = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1611 | { |
| 1612 | crow::connections::systemBus->async_method_call( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1613 | [asyncResp](const boost::system::error_code ec, |
| 1614 | std::vector<std::string>& objectPaths) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1615 | if (ec) |
| 1616 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1617 | setErrorResponse(asyncResp->res, |
| 1618 | boost::beast::http::status::not_found, |
Matt Spinler | d6091dd | 2018-12-06 14:08:27 -0600 | [diff] [blame] | 1619 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1620 | } |
| 1621 | else |
| 1622 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1623 | asyncResp->res.jsonValue = {{"status", "ok"}, |
| 1624 | {"message", "200 OK"}, |
| 1625 | {"data", std::move(objectPaths)}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1626 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1627 | }, |
| 1628 | "xyz.openbmc_project.ObjectMapper", |
| 1629 | "/xyz/openbmc_project/object_mapper", |
| 1630 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath, |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1631 | depth, std::array<std::string, 0>()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1632 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1633 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1634 | inline void handleEnumerate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1635 | const std::string& objectPath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1636 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1637 | BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1638 | |
| 1639 | asyncResp->res.jsonValue = {{"message", "200 OK"}, |
| 1640 | {"status", "ok"}, |
| 1641 | {"data", nlohmann::json::object()}}; |
| 1642 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1643 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1644 | [objectPath, asyncResp](const boost::system::error_code ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1645 | GetSubTreeType& objectNames) { |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1646 | auto transaction = std::make_shared<InProgressEnumerateData>( |
| 1647 | objectPath, asyncResp); |
| 1648 | |
| 1649 | transaction->subtree = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1650 | std::make_shared<GetSubTreeType>(std::move(objectNames)); |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1651 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1652 | if (ec) |
| 1653 | { |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 1654 | BMCWEB_LOG_ERROR << "GetSubTree failed on " |
| 1655 | << transaction->objectPath; |
| 1656 | setErrorResponse(transaction->asyncResp->res, |
| 1657 | boost::beast::http::status::not_found, |
| 1658 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1659 | return; |
| 1660 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1661 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1662 | // Add the data for the path passed in to the results |
| 1663 | // as if GetSubTree returned it, and continue on enumerating |
| 1664 | getObjectAndEnumerate(transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1665 | }, |
| 1666 | "xyz.openbmc_project.ObjectMapper", |
| 1667 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1668 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, 0, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1669 | std::array<const char*, 0>()); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1670 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1671 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1672 | inline void handleGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1673 | std::string& objectPath, std::string& destProperty) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1674 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1675 | BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty; |
| 1676 | std::shared_ptr<std::string> propertyName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1677 | std::make_shared<std::string>(std::move(destProperty)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1678 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1679 | std::shared_ptr<std::string> path = |
| 1680 | std::make_shared<std::string>(std::move(objectPath)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1681 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1682 | using GetObjectType = |
| 1683 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 1684 | crow::connections::systemBus->async_method_call( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1685 | [asyncResp, path, propertyName](const boost::system::error_code ec, |
| 1686 | const GetObjectType& objectNames) { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1687 | if (ec || objectNames.size() <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1688 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1689 | setErrorResponse(asyncResp->res, |
| 1690 | boost::beast::http::status::not_found, |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1691 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1692 | return; |
| 1693 | } |
| 1694 | std::shared_ptr<nlohmann::json> response = |
| 1695 | std::make_shared<nlohmann::json>(nlohmann::json::object()); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 1696 | // The mapper should never give us an empty interface names |
| 1697 | // list, but check anyway |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1698 | for (const std::pair<std::string, std::vector<std::string>>& |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1699 | connection : objectNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1700 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1701 | const std::vector<std::string>& interfaceNames = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1702 | connection.second; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1703 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1704 | if (interfaceNames.size() <= 0) |
| 1705 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1706 | setErrorResponse(asyncResp->res, |
| 1707 | boost::beast::http::status::not_found, |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1708 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1709 | return; |
| 1710 | } |
| 1711 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1712 | for (const std::string& interface : interfaceNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1713 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1714 | sdbusplus::message::message m = |
| 1715 | crow::connections::systemBus->new_method_call( |
| 1716 | connection.first.c_str(), path->c_str(), |
| 1717 | "org.freedesktop.DBus.Properties", "GetAll"); |
| 1718 | m.append(interface); |
| 1719 | crow::connections::systemBus->async_send( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1720 | m, [asyncResp, response, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1721 | propertyName](const boost::system::error_code ec2, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1722 | sdbusplus::message::message& msg) { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1723 | if (ec2) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1724 | { |
| 1725 | BMCWEB_LOG_ERROR << "Bad dbus request error: " |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1726 | << ec2; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1727 | } |
| 1728 | else |
| 1729 | { |
Ed Tanous | 984a4c2 | 2021-06-02 13:38:26 -0700 | [diff] [blame] | 1730 | nlohmann::json properties; |
| 1731 | int r = |
| 1732 | convertDBusToJSON("a{sv}", msg, properties); |
| 1733 | if (r < 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1734 | { |
Ed Tanous | 984a4c2 | 2021-06-02 13:38:26 -0700 | [diff] [blame] | 1735 | BMCWEB_LOG_ERROR |
| 1736 | << "convertDBusToJSON failed"; |
| 1737 | } |
| 1738 | else |
| 1739 | { |
| 1740 | for (auto& prop : properties.items()) |
| 1741 | { |
| 1742 | // if property name is empty, or |
| 1743 | // matches our search query, add it |
| 1744 | // to the response json |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1745 | |
Ed Tanous | 984a4c2 | 2021-06-02 13:38:26 -0700 | [diff] [blame] | 1746 | if (propertyName->empty()) |
| 1747 | { |
| 1748 | (*response)[prop.key()] = |
| 1749 | std::move(prop.value()); |
| 1750 | } |
| 1751 | else if (prop.key() == *propertyName) |
| 1752 | { |
| 1753 | *response = std::move(prop.value()); |
| 1754 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | if (response.use_count() == 1) |
| 1759 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1760 | if (!propertyName->empty() && response->empty()) |
| 1761 | { |
| 1762 | setErrorResponse( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1763 | asyncResp->res, |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1764 | boost::beast::http::status::not_found, |
| 1765 | propNotFoundDesc, notFoundMsg); |
| 1766 | } |
| 1767 | else |
| 1768 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1769 | asyncResp->res.jsonValue = { |
| 1770 | {"status", "ok"}, |
| 1771 | {"message", "200 OK"}, |
| 1772 | {"data", *response}}; |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1773 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1774 | } |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1775 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | }, |
| 1779 | "xyz.openbmc_project.ObjectMapper", |
| 1780 | "/xyz/openbmc_project/object_mapper", |
| 1781 | "xyz.openbmc_project.ObjectMapper", "GetObject", *path, |
| 1782 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1785 | struct AsyncPutRequest |
| 1786 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1787 | AsyncPutRequest(const std::shared_ptr<bmcweb::AsyncResp>& resIn) : |
| 1788 | asyncResp(resIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1789 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1790 | ~AsyncPutRequest() |
| 1791 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1792 | if (asyncResp->res.jsonValue.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1793 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1794 | setErrorResponse(asyncResp->res, |
| 1795 | boost::beast::http::status::forbidden, |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1796 | forbiddenMsg, forbiddenPropDesc); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1797 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1800 | void setErrorStatus(const std::string& desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1801 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1802 | setErrorResponse(asyncResp->res, |
| 1803 | boost::beast::http::status::internal_server_error, |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1804 | desc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1807 | const std::shared_ptr<bmcweb::AsyncResp> asyncResp; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1808 | std::string objectPath; |
| 1809 | std::string propertyName; |
| 1810 | nlohmann::json propertyValue; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1811 | }; |
| 1812 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1813 | inline void handlePut(const crow::Request& req, |
| 1814 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1815 | const std::string& objectPath, |
| 1816 | const std::string& destProperty) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1817 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1818 | if (destProperty.empty()) |
| 1819 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1820 | setErrorResponse(asyncResp->res, boost::beast::http::status::forbidden, |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1821 | forbiddenResDesc, forbiddenMsg); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1825 | nlohmann::json requestDbusData = |
| 1826 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1827 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1828 | if (requestDbusData.is_discarded()) |
| 1829 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1830 | setErrorResponse(asyncResp->res, |
| 1831 | boost::beast::http::status::bad_request, noJsonDesc, |
| 1832 | badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1833 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1834 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1835 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1836 | nlohmann::json::const_iterator propertyIt = requestDbusData.find("data"); |
| 1837 | if (propertyIt == requestDbusData.end()) |
| 1838 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1839 | setErrorResponse(asyncResp->res, |
| 1840 | boost::beast::http::status::bad_request, noJsonDesc, |
| 1841 | badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1842 | return; |
| 1843 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1844 | const nlohmann::json& propertySetValue = *propertyIt; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1845 | auto transaction = std::make_shared<AsyncPutRequest>(asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1846 | transaction->objectPath = objectPath; |
| 1847 | transaction->propertyName = destProperty; |
| 1848 | transaction->propertyValue = propertySetValue; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1849 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1850 | using GetObjectType = |
| 1851 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1852 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1853 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1854 | [transaction](const boost::system::error_code ec2, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1855 | const GetObjectType& objectNames) { |
| 1856 | if (!ec2 && objectNames.size() <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1857 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1858 | setErrorResponse(transaction->asyncResp->res, |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1859 | boost::beast::http::status::not_found, |
| 1860 | propNotFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1861 | return; |
| 1862 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1863 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1864 | for (const std::pair<std::string, std::vector<std::string>>& |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1865 | connection : objectNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1866 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1867 | const std::string& connectionName = connection.first; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1868 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1869 | crow::connections::systemBus->async_method_call( |
| 1870 | [connectionName{std::string(connectionName)}, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1871 | transaction](const boost::system::error_code ec3, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1872 | const std::string& introspectXml) { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1873 | if (ec3) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1874 | { |
| 1875 | BMCWEB_LOG_ERROR |
| 1876 | << "Introspect call failed with error: " |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1877 | << ec3.message() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1878 | << " on process: " << connectionName; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1879 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1880 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1881 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1882 | tinyxml2::XMLDocument doc; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1883 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1884 | doc.Parse(introspectXml.c_str()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1885 | tinyxml2::XMLNode* pRoot = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1886 | doc.FirstChildElement("node"); |
| 1887 | if (pRoot == nullptr) |
| 1888 | { |
| 1889 | BMCWEB_LOG_ERROR << "XML document failed to parse: " |
| 1890 | << introspectXml; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1891 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1892 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1893 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1894 | tinyxml2::XMLElement* ifaceNode = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1895 | pRoot->FirstChildElement("interface"); |
| 1896 | while (ifaceNode != nullptr) |
| 1897 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1898 | const char* interfaceName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1899 | ifaceNode->Attribute("name"); |
| 1900 | BMCWEB_LOG_DEBUG << "found interface " |
| 1901 | << interfaceName; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1902 | tinyxml2::XMLElement* propNode = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1903 | ifaceNode->FirstChildElement("property"); |
| 1904 | while (propNode != nullptr) |
| 1905 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1906 | const char* propertyName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1907 | propNode->Attribute("name"); |
| 1908 | BMCWEB_LOG_DEBUG << "Found property " |
| 1909 | << propertyName; |
| 1910 | if (propertyName == transaction->propertyName) |
| 1911 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1912 | const char* argType = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1913 | propNode->Attribute("type"); |
| 1914 | if (argType != nullptr) |
| 1915 | { |
| 1916 | sdbusplus::message::message m = |
| 1917 | crow::connections::systemBus |
| 1918 | ->new_method_call( |
| 1919 | connectionName.c_str(), |
| 1920 | transaction->objectPath |
| 1921 | .c_str(), |
| 1922 | "org.freedesktop.DBus." |
| 1923 | "Properties", |
| 1924 | "Set"); |
| 1925 | m.append(interfaceName, |
| 1926 | transaction->propertyName); |
| 1927 | int r = sd_bus_message_open_container( |
| 1928 | m.get(), SD_BUS_TYPE_VARIANT, |
| 1929 | argType); |
| 1930 | if (r < 0) |
| 1931 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1932 | transaction->setErrorStatus( |
| 1933 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1934 | return; |
| 1935 | } |
| 1936 | r = convertJsonToDbus( |
| 1937 | m.get(), argType, |
| 1938 | transaction->propertyValue); |
| 1939 | if (r < 0) |
| 1940 | { |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 1941 | if (r == -ERANGE) |
| 1942 | { |
| 1943 | transaction->setErrorStatus( |
| 1944 | "Provided property value " |
| 1945 | "is out of range for the " |
| 1946 | "property type"); |
| 1947 | } |
| 1948 | else |
| 1949 | { |
| 1950 | transaction->setErrorStatus( |
| 1951 | "Invalid arg type"); |
| 1952 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1953 | return; |
| 1954 | } |
| 1955 | r = sd_bus_message_close_container( |
| 1956 | m.get()); |
| 1957 | if (r < 0) |
| 1958 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1959 | transaction->setErrorStatus( |
| 1960 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1961 | return; |
| 1962 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1963 | crow::connections::systemBus |
| 1964 | ->async_send( |
| 1965 | m, |
| 1966 | [transaction]( |
| 1967 | boost::system::error_code |
| 1968 | ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1969 | sdbusplus::message::message& |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1970 | m2) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1971 | BMCWEB_LOG_DEBUG << "sent"; |
| 1972 | if (ec) |
| 1973 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1974 | const sd_bus_error* e = |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1975 | m2.get_error(); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1976 | setErrorResponse( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1977 | transaction |
| 1978 | ->asyncResp |
| 1979 | ->res, |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1980 | boost::beast::http:: |
| 1981 | status:: |
| 1982 | forbidden, |
Matt Spinler | 06b1b63 | 2019-06-18 16:09:25 -0500 | [diff] [blame] | 1983 | (e) ? e->name |
| 1984 | : ec.category() |
| 1985 | .name(), |
| 1986 | (e) ? e->message |
| 1987 | : ec.message()); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1988 | } |
| 1989 | else |
| 1990 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1991 | transaction->asyncResp |
| 1992 | ->res.jsonValue = { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1993 | {"status", "ok"}, |
| 1994 | {"message", |
| 1995 | "200 OK"}, |
| 1996 | {"data", nullptr}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1997 | } |
| 1998 | }); |
| 1999 | } |
| 2000 | } |
| 2001 | propNode = |
| 2002 | propNode->NextSiblingElement("property"); |
| 2003 | } |
| 2004 | ifaceNode = |
| 2005 | ifaceNode->NextSiblingElement("interface"); |
| 2006 | } |
| 2007 | }, |
| 2008 | connectionName, transaction->objectPath, |
| 2009 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 2010 | } |
| 2011 | }, |
| 2012 | "xyz.openbmc_project.ObjectMapper", |
| 2013 | "/xyz/openbmc_project/object_mapper", |
| 2014 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 2015 | transaction->objectPath, std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 2016 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2017 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2018 | inline void handleDBusUrl(const crow::Request& req, |
| 2019 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2020 | std::string& objectPath) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2021 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2022 | |
| 2023 | // If accessing a single attribute, fill in and update objectPath, |
| 2024 | // otherwise leave destProperty blank |
| 2025 | std::string destProperty = ""; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2026 | const char* attrSeperator = "/attr/"; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2027 | size_t attrPosition = objectPath.find(attrSeperator); |
| 2028 | if (attrPosition != objectPath.npos) |
| 2029 | { |
| 2030 | destProperty = objectPath.substr(attrPosition + strlen(attrSeperator), |
| 2031 | objectPath.length()); |
| 2032 | objectPath = objectPath.substr(0, attrPosition); |
| 2033 | } |
| 2034 | |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2035 | if (req.method() == boost::beast::http::verb::post) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2036 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2037 | constexpr const char* actionSeperator = "/action/"; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2038 | size_t actionPosition = objectPath.find(actionSeperator); |
| 2039 | if (actionPosition != objectPath.npos) |
| 2040 | { |
| 2041 | std::string postProperty = |
| 2042 | objectPath.substr((actionPosition + strlen(actionSeperator)), |
| 2043 | objectPath.length()); |
| 2044 | objectPath = objectPath.substr(0, actionPosition); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2045 | handleAction(req, asyncResp, objectPath, postProperty); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2046 | return; |
| 2047 | } |
| 2048 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2049 | else if (req.method() == boost::beast::http::verb::get) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2050 | { |
| 2051 | if (boost::ends_with(objectPath, "/enumerate")) |
| 2052 | { |
| 2053 | objectPath.erase(objectPath.end() - sizeof("enumerate"), |
| 2054 | objectPath.end()); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2055 | handleEnumerate(asyncResp, objectPath); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2056 | } |
| 2057 | else if (boost::ends_with(objectPath, "/list")) |
| 2058 | { |
| 2059 | objectPath.erase(objectPath.end() - sizeof("list"), |
| 2060 | objectPath.end()); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2061 | handleList(asyncResp, objectPath); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2062 | } |
| 2063 | else |
| 2064 | { |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 2065 | // Trim any trailing "/" at the end |
| 2066 | if (boost::ends_with(objectPath, "/")) |
| 2067 | { |
| 2068 | objectPath.pop_back(); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2069 | handleList(asyncResp, objectPath, 1); |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 2070 | } |
| 2071 | else |
| 2072 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2073 | handleGet(asyncResp, objectPath, destProperty); |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 2074 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2075 | } |
| 2076 | return; |
| 2077 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2078 | else if (req.method() == boost::beast::http::verb::put) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2079 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2080 | handlePut(req, asyncResp, objectPath, destProperty); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2081 | return; |
| 2082 | } |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2083 | else if (req.method() == boost::beast::http::verb::delete_) |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 2084 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2085 | handleDelete(asyncResp, objectPath); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 2086 | return; |
| 2087 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2088 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2089 | setErrorResponse(asyncResp->res, |
| 2090 | boost::beast::http::status::method_not_allowed, |
Matt Spinler | c4e8d21d6 | 2018-12-11 11:47:17 -0600 | [diff] [blame] | 2091 | methodNotAllowedDesc, methodNotAllowedMsg); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 2094 | inline void requestRoutes(App& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2095 | { |
| 2096 | BMCWEB_ROUTE(app, "/bus/") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2097 | .privileges({{"Login"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2098 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2099 | [](const crow::Request&, |
| 2100 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 2101 | asyncResp->res.jsonValue = {{"buses", {{{"name", "system"}}}}, |
| 2102 | {"status", "ok"}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2103 | }); |
| 2104 | |
| 2105 | BMCWEB_ROUTE(app, "/bus/system/") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2106 | .privileges({{"Login"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2107 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2108 | [](const crow::Request&, |
| 2109 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
P Dheeraj Srujan Kumar | 83fd8e1 | 2021-07-13 02:53:03 +0530 | [diff] [blame] | 2110 | auto myCallback = [asyncResp]( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2111 | const boost::system::error_code ec, |
| 2112 | std::vector<std::string>& names) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2113 | if (ec) |
| 2114 | { |
| 2115 | BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2116 | asyncResp->res.result( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2117 | boost::beast::http::status::internal_server_error); |
| 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | std::sort(names.begin(), names.end()); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2122 | asyncResp->res.jsonValue = {{"status", "ok"}}; |
| 2123 | auto& objectsSub = asyncResp->res.jsonValue["objects"]; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2124 | for (auto& name : names) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2125 | { |
| 2126 | objectsSub.push_back({{"name", name}}); |
| 2127 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2128 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2129 | }; |
| 2130 | crow::connections::systemBus->async_method_call( |
| 2131 | std::move(myCallback), "org.freedesktop.DBus", "/", |
| 2132 | "org.freedesktop.DBus", "ListNames"); |
| 2133 | }); |
| 2134 | |
| 2135 | BMCWEB_ROUTE(app, "/list/") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2136 | .privileges({{"Login"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2137 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2138 | [](const crow::Request&, |
| 2139 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 2140 | handleList(asyncResp, "/"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2141 | }); |
| 2142 | |
| 2143 | BMCWEB_ROUTE(app, "/xyz/<path>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2144 | .privileges({{"Login"}}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2145 | .methods(boost::beast::http::verb::get)( |
| 2146 | [](const crow::Request& req, |
| 2147 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2148 | const std::string& path) { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2149 | std::string objectPath = "/xyz/" + path; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2150 | handleDBusUrl(req, asyncResp, objectPath); |
| 2151 | }); |
| 2152 | |
| 2153 | BMCWEB_ROUTE(app, "/xyz/<path>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2154 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2155 | .methods(boost::beast::http::verb::put, boost::beast::http::verb::post, |
| 2156 | boost::beast::http::verb::delete_)( |
| 2157 | [](const crow::Request& req, |
| 2158 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2159 | const std::string& path) { |
| 2160 | std::string objectPath = "/xyz/" + path; |
| 2161 | handleDBusUrl(req, asyncResp, objectPath); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2162 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2163 | |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2164 | BMCWEB_ROUTE(app, "/org/<path>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2165 | .privileges({{"Login"}}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2166 | .methods(boost::beast::http::verb::get)( |
| 2167 | [](const crow::Request& req, |
| 2168 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2169 | const std::string& path) { |
| 2170 | std::string objectPath = "/org/" + path; |
| 2171 | handleDBusUrl(req, asyncResp, objectPath); |
| 2172 | }); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2173 | |
| 2174 | BMCWEB_ROUTE(app, "/org/<path>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2175 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2176 | .methods(boost::beast::http::verb::put, boost::beast::http::verb::post, |
| 2177 | boost::beast::http::verb::delete_)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2178 | [](const crow::Request& req, |
| 2179 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2180 | const std::string& path) { |
Ed Tanous | e128140 | 2019-04-03 07:07:10 -0700 | [diff] [blame] | 2181 | std::string objectPath = "/org/" + path; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2182 | handleDBusUrl(req, asyncResp, objectPath); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2183 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2184 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2185 | BMCWEB_ROUTE(app, "/download/dump/<str>/") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2186 | .privileges({{"ConfigureManager"}}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2187 | .methods(boost::beast::http::verb::get)( |
| 2188 | [](const crow::Request&, |
| 2189 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2190 | const std::string& dumpId) { |
| 2191 | std::regex validFilename(R"(^[\w\- ]+(\.?[\w\- ]*)$)"); |
| 2192 | if (!std::regex_match(dumpId, validFilename)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2193 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2194 | asyncResp->res.result( |
| 2195 | boost::beast::http::status::bad_request); |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 2196 | return; |
| 2197 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2198 | std::filesystem::path loc( |
| 2199 | "/var/lib/phosphor-debug-collector/dumps"); |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 2200 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2201 | loc /= dumpId; |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 2202 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2203 | if (!std::filesystem::exists(loc) || |
| 2204 | !std::filesystem::is_directory(loc)) |
| 2205 | { |
| 2206 | BMCWEB_LOG_ERROR << loc << "Not found"; |
| 2207 | asyncResp->res.result( |
| 2208 | boost::beast::http::status::not_found); |
| 2209 | return; |
| 2210 | } |
| 2211 | std::filesystem::directory_iterator files(loc); |
| 2212 | |
| 2213 | for (auto& file : files) |
| 2214 | { |
| 2215 | std::ifstream readFile(file.path()); |
| 2216 | if (!readFile.good()) |
| 2217 | { |
| 2218 | continue; |
| 2219 | } |
| 2220 | |
| 2221 | asyncResp->res.addHeader("Content-Type", |
| 2222 | "application/octet-stream"); |
| 2223 | |
| 2224 | // Assuming only one dump file will be present in the dump |
| 2225 | // id directory |
| 2226 | std::string dumpFileName = file.path().filename().string(); |
| 2227 | |
| 2228 | // Filename should be in alphanumeric, dot and underscore |
| 2229 | // Its based on phosphor-debug-collector application |
| 2230 | // dumpfile format |
| 2231 | std::regex dumpFileRegex("[a-zA-Z0-9\\._]+"); |
| 2232 | if (!std::regex_match(dumpFileName, dumpFileRegex)) |
| 2233 | { |
| 2234 | BMCWEB_LOG_ERROR << "Invalid dump filename " |
| 2235 | << dumpFileName; |
| 2236 | asyncResp->res.result( |
| 2237 | boost::beast::http::status::not_found); |
| 2238 | return; |
| 2239 | } |
| 2240 | std::string contentDispositionParam = |
| 2241 | "attachment; filename=\"" + dumpFileName + "\""; |
| 2242 | |
| 2243 | asyncResp->res.addHeader("Content-Disposition", |
| 2244 | contentDispositionParam); |
| 2245 | |
| 2246 | asyncResp->res.body() = { |
| 2247 | std::istreambuf_iterator<char>(readFile), |
| 2248 | std::istreambuf_iterator<char>()}; |
| 2249 | return; |
| 2250 | } |
| 2251 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2252 | return; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2253 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2254 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2255 | BMCWEB_ROUTE(app, "/bus/system/<str>/") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2256 | .privileges({{"Login"}}) |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2257 | |
| 2258 | .methods(boost::beast::http::verb::get)( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2259 | [](const crow::Request&, |
| 2260 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2261 | const std::string& connection) { |
Gunnar Mills | 9062d47 | 2021-11-16 11:37:47 -0600 | [diff] [blame] | 2262 | introspectObjects( |
| 2263 | connection, "/", |
| 2264 | std::make_shared<bmcweb::AsyncResp>(asyncResp->res)); |
Ed Tanous | b41187f | 2019-10-24 16:30:02 -0700 | [diff] [blame] | 2265 | }); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2266 | |
| 2267 | BMCWEB_ROUTE(app, "/bus/system/<str>/<path>") |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 2268 | .privileges({{"ConfigureComponents", "ConfigureManager"}}) |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2269 | .methods(boost::beast::http::verb::get, boost::beast::http::verb::post)( |
| 2270 | [](const crow::Request& req, |
| 2271 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2272 | const std::string& processName, |
| 2273 | const std::string& requestedPath) { |
| 2274 | std::vector<std::string> strs; |
| 2275 | boost::split(strs, requestedPath, boost::is_any_of("/")); |
| 2276 | std::string objectPath; |
| 2277 | std::string interfaceName; |
| 2278 | std::string methodName; |
| 2279 | auto it = strs.begin(); |
| 2280 | if (it == strs.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2281 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2282 | objectPath = "/"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2283 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2284 | while (it != strs.end()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2285 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2286 | // Check if segment contains ".". If it does, it must be an |
| 2287 | // interface |
| 2288 | if (it->find(".") != std::string::npos) |
| 2289 | { |
| 2290 | break; |
| 2291 | // This check is necessary as the trailing slash gets |
| 2292 | // parsed as part of our <path> specifier above, which |
| 2293 | // causes the normal trailing backslash redirector to |
| 2294 | // fail. |
| 2295 | } |
| 2296 | if (!it->empty()) |
| 2297 | { |
| 2298 | objectPath += "/" + *it; |
| 2299 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2300 | it++; |
| 2301 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2302 | if (it != strs.end()) |
| 2303 | { |
| 2304 | interfaceName = *it; |
| 2305 | it++; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2306 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2307 | // after interface, we might have a method name |
| 2308 | if (it != strs.end()) |
| 2309 | { |
| 2310 | methodName = *it; |
| 2311 | it++; |
| 2312 | } |
| 2313 | } |
| 2314 | if (it != strs.end()) |
| 2315 | { |
| 2316 | // if there is more levels past the method name, something |
| 2317 | // went wrong, return not found |
| 2318 | asyncResp->res.result( |
| 2319 | boost::beast::http::status::not_found); |
| 2320 | return; |
| 2321 | } |
| 2322 | if (interfaceName.empty()) |
| 2323 | { |
| 2324 | crow::connections::systemBus->async_method_call( |
| 2325 | [asyncResp, processName, |
| 2326 | objectPath](const boost::system::error_code ec, |
| 2327 | const std::string& introspectXml) { |
| 2328 | if (ec) |
| 2329 | { |
| 2330 | BMCWEB_LOG_ERROR |
| 2331 | << "Introspect call failed with error: " |
| 2332 | << ec.message() |
| 2333 | << " on process: " << processName |
| 2334 | << " path: " << objectPath << "\n"; |
| 2335 | return; |
| 2336 | } |
| 2337 | tinyxml2::XMLDocument doc; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2338 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2339 | doc.Parse(introspectXml.c_str()); |
| 2340 | tinyxml2::XMLNode* pRoot = |
| 2341 | doc.FirstChildElement("node"); |
| 2342 | if (pRoot == nullptr) |
| 2343 | { |
| 2344 | BMCWEB_LOG_ERROR |
| 2345 | << "XML document failed to parse " |
| 2346 | << processName << " " << objectPath << "\n"; |
| 2347 | asyncResp->res.jsonValue = { |
| 2348 | {"status", "XML parse error"}}; |
| 2349 | asyncResp->res.result( |
| 2350 | boost::beast::http::status:: |
| 2351 | internal_server_error); |
| 2352 | return; |
| 2353 | } |
| 2354 | |
| 2355 | BMCWEB_LOG_DEBUG << introspectXml; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2356 | asyncResp->res.jsonValue = { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2357 | {"status", "ok"}, |
| 2358 | {"bus_name", processName}, |
| 2359 | {"object_path", objectPath}}; |
| 2360 | nlohmann::json& interfacesArray = |
| 2361 | asyncResp->res.jsonValue["interfaces"]; |
| 2362 | interfacesArray = nlohmann::json::array(); |
| 2363 | tinyxml2::XMLElement* interface = |
| 2364 | pRoot->FirstChildElement("interface"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2365 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2366 | while (interface != nullptr) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2367 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2368 | const char* ifaceName = |
| 2369 | interface->Attribute("name"); |
| 2370 | if (ifaceName != nullptr) |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2371 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2372 | interfacesArray.push_back( |
| 2373 | {{"name", ifaceName}}); |
| 2374 | } |
| 2375 | |
| 2376 | interface = |
| 2377 | interface->NextSiblingElement("interface"); |
| 2378 | } |
| 2379 | }, |
| 2380 | processName, objectPath, |
| 2381 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 2382 | } |
| 2383 | else if (methodName.empty()) |
| 2384 | { |
| 2385 | crow::connections::systemBus->async_method_call( |
| 2386 | [asyncResp, processName, objectPath, |
| 2387 | interfaceName](const boost::system::error_code ec, |
| 2388 | const std::string& introspectXml) { |
| 2389 | if (ec) |
| 2390 | { |
| 2391 | BMCWEB_LOG_ERROR |
| 2392 | << "Introspect call failed with error: " |
| 2393 | << ec.message() |
| 2394 | << " on process: " << processName |
| 2395 | << " path: " << objectPath << "\n"; |
| 2396 | return; |
| 2397 | } |
| 2398 | tinyxml2::XMLDocument doc; |
| 2399 | |
| 2400 | doc.Parse(introspectXml.data(), |
| 2401 | introspectXml.size()); |
| 2402 | tinyxml2::XMLNode* pRoot = |
| 2403 | doc.FirstChildElement("node"); |
| 2404 | if (pRoot == nullptr) |
| 2405 | { |
| 2406 | BMCWEB_LOG_ERROR |
| 2407 | << "XML document failed to parse " |
| 2408 | << processName << " " << objectPath << "\n"; |
| 2409 | asyncResp->res.result( |
| 2410 | boost::beast::http::status:: |
| 2411 | internal_server_error); |
| 2412 | return; |
| 2413 | } |
| 2414 | asyncResp->res.jsonValue = { |
| 2415 | {"status", "ok"}, |
| 2416 | {"bus_name", processName}, |
| 2417 | {"interface", interfaceName}, |
| 2418 | {"object_path", objectPath}}; |
| 2419 | |
| 2420 | nlohmann::json& methodsArray = |
| 2421 | asyncResp->res.jsonValue["methods"]; |
| 2422 | methodsArray = nlohmann::json::array(); |
| 2423 | |
| 2424 | nlohmann::json& signalsArray = |
| 2425 | asyncResp->res.jsonValue["signals"]; |
| 2426 | signalsArray = nlohmann::json::array(); |
| 2427 | |
| 2428 | nlohmann::json& propertiesObj = |
| 2429 | asyncResp->res.jsonValue["properties"]; |
| 2430 | propertiesObj = nlohmann::json::object(); |
| 2431 | |
| 2432 | // if we know we're the only call, build the |
| 2433 | // json directly |
| 2434 | tinyxml2::XMLElement* interface = |
| 2435 | pRoot->FirstChildElement("interface"); |
| 2436 | while (interface != nullptr) |
| 2437 | { |
| 2438 | const char* ifaceName = |
| 2439 | interface->Attribute("name"); |
| 2440 | |
| 2441 | if (ifaceName != nullptr && |
| 2442 | ifaceName == interfaceName) |
| 2443 | { |
| 2444 | break; |
| 2445 | } |
| 2446 | |
| 2447 | interface = |
| 2448 | interface->NextSiblingElement("interface"); |
| 2449 | } |
| 2450 | if (interface == nullptr) |
| 2451 | { |
| 2452 | // if we got to the end of the list and |
| 2453 | // never found a match, throw 404 |
| 2454 | asyncResp->res.result( |
| 2455 | boost::beast::http::status::not_found); |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | tinyxml2::XMLElement* methods = |
| 2460 | interface->FirstChildElement("method"); |
| 2461 | while (methods != nullptr) |
| 2462 | { |
| 2463 | nlohmann::json argsArray = |
| 2464 | nlohmann::json::array(); |
| 2465 | tinyxml2::XMLElement* arg = |
| 2466 | methods->FirstChildElement("arg"); |
| 2467 | while (arg != nullptr) |
| 2468 | { |
| 2469 | nlohmann::json thisArg; |
| 2470 | for (const char* fieldName : |
| 2471 | std::array<const char*, 3>{ |
| 2472 | "name", "direction", "type"}) |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2473 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2474 | const char* fieldValue = |
| 2475 | arg->Attribute(fieldName); |
| 2476 | if (fieldValue != nullptr) |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2477 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2478 | thisArg[fieldName] = fieldValue; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2479 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2480 | } |
| 2481 | argsArray.push_back(std::move(thisArg)); |
| 2482 | arg = arg->NextSiblingElement("arg"); |
| 2483 | } |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2484 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2485 | const char* name = methods->Attribute("name"); |
| 2486 | if (name != nullptr) |
| 2487 | { |
| 2488 | std::string uri; |
| 2489 | uri.reserve(14 + processName.size() + |
| 2490 | objectPath.size() + |
| 2491 | interfaceName.size() + |
| 2492 | strlen(name)); |
| 2493 | uri += "/bus/system/"; |
| 2494 | uri += processName; |
| 2495 | uri += objectPath; |
| 2496 | uri += "/"; |
| 2497 | uri += interfaceName; |
| 2498 | uri += "/"; |
| 2499 | uri += name; |
| 2500 | methodsArray.push_back( |
| 2501 | {{"name", name}, |
| 2502 | {"uri", std::move(uri)}, |
| 2503 | {"args", argsArray}}); |
| 2504 | } |
| 2505 | methods = methods->NextSiblingElement("method"); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2506 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2507 | tinyxml2::XMLElement* signals = |
| 2508 | interface->FirstChildElement("signal"); |
| 2509 | while (signals != nullptr) |
| 2510 | { |
| 2511 | nlohmann::json argsArray = |
| 2512 | nlohmann::json::array(); |
| 2513 | |
| 2514 | tinyxml2::XMLElement* arg = |
| 2515 | signals->FirstChildElement("arg"); |
| 2516 | while (arg != nullptr) |
| 2517 | { |
| 2518 | const char* name = arg->Attribute("name"); |
| 2519 | const char* type = arg->Attribute("type"); |
| 2520 | if (name != nullptr && type != nullptr) |
| 2521 | { |
| 2522 | argsArray.push_back({ |
| 2523 | {"name", name}, |
| 2524 | {"type", type}, |
| 2525 | }); |
| 2526 | } |
| 2527 | arg = arg->NextSiblingElement("arg"); |
| 2528 | } |
| 2529 | const char* name = signals->Attribute("name"); |
| 2530 | if (name != nullptr) |
| 2531 | { |
| 2532 | signalsArray.push_back( |
| 2533 | {{"name", name}, {"args", argsArray}}); |
| 2534 | } |
| 2535 | |
| 2536 | signals = signals->NextSiblingElement("signal"); |
| 2537 | } |
| 2538 | |
| 2539 | tinyxml2::XMLElement* property = |
| 2540 | interface->FirstChildElement("property"); |
| 2541 | while (property != nullptr) |
| 2542 | { |
| 2543 | const char* name = property->Attribute("name"); |
| 2544 | const char* type = property->Attribute("type"); |
| 2545 | if (type != nullptr && name != nullptr) |
| 2546 | { |
| 2547 | sdbusplus::message::message m = |
| 2548 | crow::connections::systemBus |
| 2549 | ->new_method_call( |
| 2550 | processName.c_str(), |
| 2551 | objectPath.c_str(), |
| 2552 | "org.freedesktop." |
| 2553 | "DBus." |
| 2554 | "Properties", |
| 2555 | "Get"); |
| 2556 | m.append(interfaceName, name); |
| 2557 | nlohmann::json& propertyItem = |
| 2558 | propertiesObj[name]; |
| 2559 | crow::connections::systemBus->async_send( |
| 2560 | m, |
| 2561 | [&propertyItem, asyncResp]( |
| 2562 | boost::system::error_code& e, |
| 2563 | sdbusplus::message::message& msg) { |
| 2564 | if (e) |
| 2565 | { |
| 2566 | return; |
| 2567 | } |
| 2568 | |
| 2569 | convertDBusToJSON("v", msg, |
| 2570 | propertyItem); |
| 2571 | }); |
| 2572 | } |
| 2573 | property = |
| 2574 | property->NextSiblingElement("property"); |
| 2575 | } |
| 2576 | }, |
| 2577 | processName, objectPath, |
| 2578 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2579 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2580 | else |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2581 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2582 | if (req.method() != boost::beast::http::verb::post) |
| 2583 | { |
| 2584 | asyncResp->res.result( |
| 2585 | boost::beast::http::status::not_found); |
| 2586 | return; |
| 2587 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2588 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2589 | nlohmann::json requestDbusData = |
| 2590 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2591 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2592 | if (requestDbusData.is_discarded()) |
| 2593 | { |
| 2594 | asyncResp->res.result( |
| 2595 | boost::beast::http::status::bad_request); |
| 2596 | return; |
| 2597 | } |
| 2598 | if (!requestDbusData.is_array()) |
| 2599 | { |
| 2600 | asyncResp->res.result( |
| 2601 | boost::beast::http::status::bad_request); |
| 2602 | return; |
| 2603 | } |
| 2604 | auto transaction = |
| 2605 | std::make_shared<InProgressActionData>(asyncResp->res); |
| 2606 | |
| 2607 | transaction->path = objectPath; |
| 2608 | transaction->methodName = methodName; |
| 2609 | transaction->arguments = std::move(requestDbusData); |
| 2610 | |
| 2611 | findActionOnInterface(transaction, processName); |
| 2612 | } |
| 2613 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2614 | } |
| 2615 | } // namespace openbmc_mapper |
| 2616 | } // namespace crow |