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