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