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