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