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