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