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