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