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