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