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