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