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