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