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