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 |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 16 | #include "filesystem.hpp" |
| 17 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 18 | #include <crow/app.h> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 19 | #include <tinyxml2.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 21 | #include <async_resp.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | #include <boost/algorithm/string.hpp> |
| 23 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 24 | #include <dbus_singleton.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 25 | #include <dbus_utility.hpp> |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 26 | #include <fstream> |
William A. Kennington III | 0a63b1c | 2018-10-18 13:37:19 -0700 | [diff] [blame] | 27 | #include <sdbusplus/message/types.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | namespace crow |
| 30 | { |
| 31 | namespace openbmc_mapper |
| 32 | { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 33 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 34 | using GetSubTreeType = std::vector< |
| 35 | std::pair<std::string, |
| 36 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 37 | |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 38 | const std::string notFoundMsg = "404 Not Found"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 39 | const std::string badReqMsg = "400 Bad Request"; |
Matt Spinler | c4e8d21d6 | 2018-12-11 11:47:17 -0600 | [diff] [blame] | 40 | const std::string methodNotAllowedMsg = "405 Method Not Allowed"; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 41 | const std::string forbiddenMsg = "403 Forbidden"; |
Matt Spinler | c0eb9bd | 2019-01-09 15:22:30 -0600 | [diff] [blame] | 42 | const std::string methodFailedMsg = "500 Method Call Failed"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 43 | |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 44 | const std::string notFoundDesc = |
| 45 | "org.freedesktop.DBus.Error.FileNotFound: path or object not found"; |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 46 | const std::string propNotFoundDesc = "The specified property cannot be found"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 47 | const std::string noJsonDesc = "No JSON object could be decoded"; |
| 48 | const std::string methodNotFoundDesc = "The specified method cannot be found"; |
Matt Spinler | c4e8d21d6 | 2018-12-11 11:47:17 -0600 | [diff] [blame] | 49 | const std::string methodNotAllowedDesc = "Method not allowed"; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 50 | const std::string forbiddenPropDesc = |
| 51 | "The specified property cannot be created"; |
| 52 | const std::string forbiddenResDesc = "The specified resource cannot be created"; |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 53 | |
| 54 | void setErrorResponse(crow::Response &res, boost::beast::http::status result, |
| 55 | const std::string &desc, const std::string &msg) |
| 56 | { |
| 57 | res.result(result); |
| 58 | res.jsonValue = {{"data", {{"description", desc}}}, |
| 59 | {"message", msg}, |
| 60 | {"status", "error"}}; |
| 61 | } |
| 62 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 63 | void introspectObjects(const std::string &processName, |
| 64 | const std::string &objectPath, |
| 65 | std::shared_ptr<bmcweb::AsyncResp> transaction) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 67 | if (transaction->res.jsonValue.is_null()) |
| 68 | { |
| 69 | transaction->res.jsonValue = {{"status", "ok"}, |
| 70 | {"bus_name", processName}, |
| 71 | {"objects", nlohmann::json::array()}}; |
| 72 | } |
| 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 75 | [transaction, processName{std::string(processName)}, |
| 76 | objectPath{std::string(objectPath)}]( |
| 77 | const boost::system::error_code ec, |
| 78 | const std::string &introspect_xml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | if (ec) |
| 80 | { |
| 81 | BMCWEB_LOG_ERROR |
| 82 | << "Introspect call failed with error: " << ec.message() |
| 83 | << " on process: " << processName << " path: " << objectPath |
| 84 | << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | transaction->res.jsonValue["objects"].push_back( |
| 88 | {{"path", objectPath}}); |
| 89 | |
| 90 | tinyxml2::XMLDocument doc; |
| 91 | |
| 92 | doc.Parse(introspect_xml.c_str()); |
| 93 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 94 | if (pRoot == nullptr) |
| 95 | { |
| 96 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 97 | << processName << " " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 98 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 99 | else |
| 100 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 101 | tinyxml2::XMLElement *node = pRoot->FirstChildElement("node"); |
| 102 | while (node != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 104 | const char *childPath = node->Attribute("name"); |
| 105 | if (childPath != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | std::string newpath; |
| 108 | if (objectPath != "/") |
| 109 | { |
| 110 | newpath += objectPath; |
| 111 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 112 | newpath += std::string("/") + childPath; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | // introspect the subobjects as well |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 114 | introspectObjects(processName, newpath, transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 116 | |
| 117 | node = node->NextSiblingElement("node"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | }, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 121 | processName, objectPath, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | "Introspect"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 123 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 124 | |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 125 | void getPropertiesForEnumerate(const std::string &objectPath, |
| 126 | const std::string &service, |
| 127 | const std::string &interface, |
| 128 | std::shared_ptr<bmcweb::AsyncResp> asyncResp) |
| 129 | { |
| 130 | BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " " |
| 131 | << service << " " << interface; |
| 132 | |
| 133 | crow::connections::systemBus->async_method_call( |
| 134 | [asyncResp, objectPath, service, |
| 135 | interface](const boost::system::error_code ec, |
| 136 | const std::vector< |
| 137 | std::pair<std::string, dbus::utility::DbusVariantType>> |
| 138 | &propertiesList) { |
| 139 | if (ec) |
| 140 | { |
| 141 | BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface " |
| 142 | << interface << " service " << service |
| 143 | << " failed with code " << ec; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | nlohmann::json &dataJson = asyncResp->res.jsonValue["data"]; |
| 148 | nlohmann::json &objectJson = dataJson[objectPath]; |
| 149 | if (objectJson.is_null()) |
| 150 | { |
| 151 | objectJson = nlohmann::json::object(); |
| 152 | } |
| 153 | |
| 154 | for (const auto &[name, value] : propertiesList) |
| 155 | { |
| 156 | nlohmann::json &propertyJson = objectJson[name]; |
| 157 | sdbusplus::message::variant_ns::visit( |
| 158 | [&propertyJson](auto &&val) { propertyJson = val; }, value); |
| 159 | } |
| 160 | }, |
| 161 | service, objectPath, "org.freedesktop.DBus.Properties", "GetAll", |
| 162 | interface); |
| 163 | } |
| 164 | |
| 165 | // Find any results that weren't picked up by ObjectManagers, to be |
| 166 | // called after all ObjectManagers are searched for and called. |
| 167 | void findRemainingObjectsForEnumerate( |
| 168 | const std::string &objectPath, std::shared_ptr<GetSubTreeType> subtree, |
| 169 | std::shared_ptr<bmcweb::AsyncResp> asyncResp) |
| 170 | { |
| 171 | BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate"; |
| 172 | const nlohmann::json &dataJson = asyncResp->res.jsonValue["data"]; |
| 173 | |
| 174 | for (const auto &[path, interface_map] : *subtree) |
| 175 | { |
| 176 | if (path == objectPath) |
| 177 | { |
| 178 | // An enumerate does not return the target path's properties |
| 179 | continue; |
| 180 | } |
| 181 | if (dataJson.find(path) == dataJson.end()) |
| 182 | { |
| 183 | for (const auto &[service, interfaces] : interface_map) |
| 184 | { |
| 185 | for (const auto &interface : interfaces) |
| 186 | { |
| 187 | if (!boost::starts_with(interface, "org.freedesktop.DBus")) |
| 188 | { |
| 189 | getPropertiesForEnumerate(path, service, interface, |
| 190 | asyncResp); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 198 | struct InProgressEnumerateData |
| 199 | { |
| 200 | InProgressEnumerateData(const std::string &objectPath, |
| 201 | std::shared_ptr<bmcweb::AsyncResp> asyncResp) : |
| 202 | objectPath(objectPath), |
| 203 | asyncResp(asyncResp) |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | ~InProgressEnumerateData() |
| 208 | { |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 209 | findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp); |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | const std::string objectPath; |
| 213 | std::shared_ptr<GetSubTreeType> subtree; |
| 214 | std::shared_ptr<bmcweb::AsyncResp> asyncResp; |
| 215 | }; |
| 216 | |
| 217 | void getManagedObjectsForEnumerate( |
| 218 | const std::string &object_name, const std::string &object_manager_path, |
| 219 | const std::string &connection_name, |
| 220 | std::shared_ptr<InProgressEnumerateData> transaction) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 221 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 222 | BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << object_name |
| 223 | << " object_manager_path " << object_manager_path |
| 224 | << " connection_name " << connection_name; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 225 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 226 | [transaction, object_name, |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 227 | connection_name](const boost::system::error_code ec, |
| 228 | const dbus::utility::ManagedObjectType &objects) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 229 | if (ec) |
| 230 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 231 | BMCWEB_LOG_ERROR << "GetManagedObjects on path " << object_name |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 232 | << " on connection " << connection_name |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 233 | << " failed with code " << ec; |
| 234 | return; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 235 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 236 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 237 | nlohmann::json &dataJson = |
| 238 | transaction->asyncResp->res.jsonValue["data"]; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 239 | |
| 240 | for (const auto &objectPath : objects) |
| 241 | { |
| 242 | if (boost::starts_with(objectPath.first.str, object_name)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 243 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 244 | BMCWEB_LOG_DEBUG << "Reading object " |
| 245 | << objectPath.first.str; |
| 246 | nlohmann::json &objectJson = dataJson[objectPath.first.str]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 247 | if (objectJson.is_null()) |
| 248 | { |
| 249 | objectJson = nlohmann::json::object(); |
| 250 | } |
| 251 | for (const auto &interface : objectPath.second) |
| 252 | { |
| 253 | for (const auto &property : interface.second) |
| 254 | { |
| 255 | nlohmann::json &propertyJson = |
| 256 | objectJson[property.first]; |
William A. Kennington III | 0a63b1c | 2018-10-18 13:37:19 -0700 | [diff] [blame] | 257 | sdbusplus::message::variant_ns::visit( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 258 | [&propertyJson](auto &&val) { |
| 259 | propertyJson = val; |
| 260 | }, |
| 261 | property.second); |
| 262 | } |
| 263 | } |
| 264 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 265 | for (const auto &interface : objectPath.second) |
| 266 | { |
| 267 | if (interface.first == "org.freedesktop.DBus.ObjectManager") |
| 268 | { |
| 269 | getManagedObjectsForEnumerate( |
| 270 | objectPath.first.str, objectPath.first.str, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 271 | connection_name, transaction); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 274 | } |
| 275 | }, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 276 | connection_name, object_manager_path, |
| 277 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 278 | } |
| 279 | |
| 280 | void findObjectManagerPathForEnumerate( |
| 281 | const std::string &object_name, const std::string &connection_name, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 282 | std::shared_ptr<InProgressEnumerateData> transaction) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 283 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 284 | BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name |
| 285 | << " on connection:" << connection_name; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 286 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 287 | [transaction, object_name, connection_name]( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 288 | const boost::system::error_code ec, |
| 289 | const boost::container::flat_map< |
| 290 | std::string, boost::container::flat_map< |
| 291 | std::string, std::vector<std::string>>> |
| 292 | &objects) { |
| 293 | if (ec) |
| 294 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 295 | BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name |
| 296 | << " failed with code " << ec; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | |
Ed Tanous | f254ba7 | 2018-10-12 13:40:35 -0700 | [diff] [blame] | 300 | for (const auto &pathGroup : objects) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 301 | { |
Ed Tanous | f254ba7 | 2018-10-12 13:40:35 -0700 | [diff] [blame] | 302 | for (const auto &connectionGroup : pathGroup.second) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 303 | { |
| 304 | if (connectionGroup.first == connection_name) |
| 305 | { |
| 306 | // Found the object manager path for this resource. |
| 307 | getManagedObjectsForEnumerate( |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 308 | object_name, pathGroup.first, connection_name, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 309 | transaction); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | }, |
| 315 | "xyz.openbmc_project.ObjectMapper", |
| 316 | "/xyz/openbmc_project/object_mapper", |
| 317 | "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name, |
| 318 | std::array<const char *, 1>{"org.freedesktop.DBus.ObjectManager"}); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 319 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 320 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 321 | // Uses GetObject to add the object info about the target /enumerate path to the |
| 322 | // results of GetSubTree, as GetSubTree will not return info for the |
| 323 | // target path, and then continues on enumerating the rest of the tree. |
| 324 | void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction) |
| 325 | { |
| 326 | using GetObjectType = |
| 327 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 328 | |
| 329 | crow::connections::systemBus->async_method_call( |
| 330 | [transaction](const boost::system::error_code ec, |
| 331 | const GetObjectType &objects) { |
| 332 | if (ec) |
| 333 | { |
| 334 | BMCWEB_LOG_ERROR << "GetObject for path " |
| 335 | << transaction->objectPath |
| 336 | << " failed with code " << ec; |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath |
| 341 | << " has " << objects.size() << " entries"; |
| 342 | if (!objects.empty()) |
| 343 | { |
| 344 | transaction->subtree->emplace_back(transaction->objectPath, |
| 345 | objects); |
| 346 | } |
| 347 | |
| 348 | // Map indicating connection name, and the path where the object |
| 349 | // manager exists |
| 350 | boost::container::flat_map<std::string, std::string> connections; |
| 351 | |
| 352 | for (const auto &object : *(transaction->subtree)) |
| 353 | { |
| 354 | for (const auto &connection : object.second) |
| 355 | { |
| 356 | std::string &objectManagerPath = |
| 357 | connections[connection.first]; |
| 358 | for (const auto &interface : connection.second) |
| 359 | { |
| 360 | BMCWEB_LOG_DEBUG << connection.first |
| 361 | << " has interface " << interface; |
| 362 | if (interface == "org.freedesktop.DBus.ObjectManager") |
| 363 | { |
| 364 | BMCWEB_LOG_DEBUG << "found object manager path " |
| 365 | << object.first; |
| 366 | objectManagerPath = object.first; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections"; |
| 372 | |
| 373 | for (const auto &connection : connections) |
| 374 | { |
| 375 | // If we already know where the object manager is, we don't need |
| 376 | // to search for it, we can call directly in to |
| 377 | // getManagedObjects |
| 378 | if (!connection.second.empty()) |
| 379 | { |
| 380 | getManagedObjectsForEnumerate( |
| 381 | transaction->objectPath, connection.second, |
| 382 | connection.first, transaction); |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | // otherwise we need to find the object manager path before |
| 387 | // we can continue |
| 388 | findObjectManagerPathForEnumerate( |
| 389 | transaction->objectPath, connection.first, transaction); |
| 390 | } |
| 391 | } |
| 392 | }, |
| 393 | "xyz.openbmc_project.ObjectMapper", |
| 394 | "/xyz/openbmc_project/object_mapper", |
| 395 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 396 | transaction->objectPath, std::array<const char *, 0>()); |
| 397 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 398 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 399 | // Structure for storing data on an in progress action |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 400 | struct InProgressActionData |
| 401 | { |
| 402 | InProgressActionData(crow::Response &res) : res(res){}; |
| 403 | ~InProgressActionData() |
| 404 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 405 | // If still no JSON filled in, then we never found the method. |
| 406 | if (res.jsonValue.is_null()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 407 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 408 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 409 | methodNotFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 410 | } |
| 411 | res.end(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 412 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 413 | |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 414 | void setErrorStatus(const std::string &desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 415 | { |
Matt Spinler | c0eb9bd | 2019-01-09 15:22:30 -0600 | [diff] [blame] | 416 | setErrorResponse(res, boost::beast::http::status::bad_request, desc, |
| 417 | badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 418 | } |
| 419 | crow::Response &res; |
| 420 | std::string path; |
| 421 | std::string methodName; |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 422 | std::string interfaceName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 423 | nlohmann::json arguments; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 426 | std::vector<std::string> dbusArgSplit(const std::string &string) |
| 427 | { |
| 428 | std::vector<std::string> ret; |
| 429 | if (string.empty()) |
| 430 | { |
| 431 | return ret; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 432 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 433 | ret.push_back(""); |
| 434 | int containerDepth = 0; |
| 435 | |
| 436 | for (std::string::const_iterator character = string.begin(); |
| 437 | character != string.end(); character++) |
| 438 | { |
| 439 | ret.back() += *character; |
| 440 | switch (*character) |
| 441 | { |
| 442 | case ('a'): |
| 443 | break; |
| 444 | case ('('): |
| 445 | case ('{'): |
| 446 | containerDepth++; |
| 447 | break; |
| 448 | case ('}'): |
| 449 | case (')'): |
| 450 | containerDepth--; |
| 451 | if (containerDepth == 0) |
| 452 | { |
| 453 | if (character + 1 != string.end()) |
| 454 | { |
| 455 | ret.push_back(""); |
| 456 | } |
| 457 | } |
| 458 | break; |
| 459 | default: |
| 460 | if (containerDepth == 0) |
| 461 | { |
| 462 | if (character + 1 != string.end()) |
| 463 | { |
| 464 | ret.push_back(""); |
| 465 | } |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | } |
Matt Spinler | 4ae611d | 2019-01-11 15:37:06 -0600 | [diff] [blame] | 470 | |
| 471 | return ret; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Ed Tanous | d76323e | 2018-08-07 14:35:40 -0700 | [diff] [blame] | 474 | int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 475 | const nlohmann::json &input_json) |
| 476 | { |
| 477 | int r = 0; |
| 478 | BMCWEB_LOG_DEBUG << "Converting " << input_json.dump() |
| 479 | << " to type: " << arg_type; |
| 480 | const std::vector<std::string> argTypes = dbusArgSplit(arg_type); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 481 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 482 | // Assume a single object for now. |
| 483 | const nlohmann::json *j = &input_json; |
| 484 | nlohmann::json::const_iterator jIt = input_json.begin(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 485 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 486 | for (const std::string &argCode : argTypes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 487 | { |
| 488 | // If we are decoding multiple objects, grab the pointer to the |
| 489 | // iterator, and increment it for the next loop |
| 490 | if (argTypes.size() > 1) |
| 491 | { |
| 492 | if (jIt == input_json.end()) |
| 493 | { |
| 494 | return -2; |
| 495 | } |
| 496 | j = &*jIt; |
| 497 | jIt++; |
| 498 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 499 | const int64_t *intValue = j->get_ptr<const int64_t *>(); |
| 500 | const uint64_t *uintValue = j->get_ptr<const uint64_t *>(); |
| 501 | const std::string *stringValue = j->get_ptr<const std::string *>(); |
| 502 | const double *doubleValue = j->get_ptr<const double *>(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 503 | const bool *b = j->get_ptr<const bool *>(); |
| 504 | int64_t v = 0; |
| 505 | double d = 0.0; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 506 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 507 | // Do some basic type conversions that make sense. uint can be |
| 508 | // converted to int. int and uint can be converted to double |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 509 | if (uintValue != nullptr && intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 510 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 511 | v = static_cast<int64_t>(*uintValue); |
| 512 | intValue = &v; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 513 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 514 | if (uintValue != nullptr && doubleValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 516 | d = static_cast<double>(*uintValue); |
| 517 | doubleValue = &d; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 518 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 519 | if (intValue != nullptr && doubleValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 520 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 521 | d = static_cast<double>(*intValue); |
| 522 | doubleValue = &d; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 525 | if (argCode == "s") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 526 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 527 | if (stringValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 528 | { |
| 529 | return -1; |
| 530 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 531 | r = sd_bus_message_append_basic(m, argCode[0], |
| 532 | (void *)stringValue->c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 533 | if (r < 0) |
| 534 | { |
| 535 | return r; |
| 536 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 537 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 538 | else if (argCode == "i") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 539 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 540 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 541 | { |
| 542 | return -1; |
| 543 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 544 | int32_t i = static_cast<int32_t>(*intValue); |
| 545 | r = sd_bus_message_append_basic(m, argCode[0], &i); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 546 | if (r < 0) |
| 547 | { |
| 548 | return r; |
| 549 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 550 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 551 | else if (argCode == "b") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 552 | { |
| 553 | // lots of ways bool could be represented here. Try them all |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 554 | int boolInt = false; |
| 555 | if (intValue != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 556 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 557 | boolInt = *intValue > 0 ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 558 | } |
| 559 | else if (b != nullptr) |
| 560 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 561 | boolInt = b ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 562 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 563 | else if (stringValue != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 564 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 565 | boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 566 | } |
| 567 | else |
| 568 | { |
| 569 | return -1; |
| 570 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 571 | r = sd_bus_message_append_basic(m, argCode[0], &boolInt); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 572 | if (r < 0) |
| 573 | { |
| 574 | return r; |
| 575 | } |
| 576 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 577 | else if (argCode == "n") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 578 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 579 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 580 | { |
| 581 | return -1; |
| 582 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 583 | int16_t n = static_cast<int16_t>(*intValue); |
| 584 | r = sd_bus_message_append_basic(m, argCode[0], &n); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 585 | if (r < 0) |
| 586 | { |
| 587 | return r; |
| 588 | } |
| 589 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 590 | else if (argCode == "x") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 591 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 592 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 593 | { |
| 594 | return -1; |
| 595 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 596 | r = sd_bus_message_append_basic(m, argCode[0], intValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 597 | if (r < 0) |
| 598 | { |
| 599 | return r; |
| 600 | } |
| 601 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 602 | else if (argCode == "y") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 603 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 604 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | { |
| 606 | return -1; |
| 607 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 608 | uint8_t y = static_cast<uint8_t>(*uintValue); |
| 609 | r = sd_bus_message_append_basic(m, argCode[0], &y); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 610 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 611 | else if (argCode == "q") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 612 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 613 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 614 | { |
| 615 | return -1; |
| 616 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 617 | uint16_t q = static_cast<uint16_t>(*uintValue); |
| 618 | r = sd_bus_message_append_basic(m, argCode[0], &q); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 619 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 620 | else if (argCode == "u") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 621 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 622 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 623 | { |
| 624 | return -1; |
| 625 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 626 | uint32_t u = static_cast<uint32_t>(*uintValue); |
| 627 | r = sd_bus_message_append_basic(m, argCode[0], &u); |
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 (argCode == "t") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 630 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 631 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 632 | { |
| 633 | return -1; |
| 634 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 635 | r = sd_bus_message_append_basic(m, argCode[0], uintValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 637 | else if (argCode == "d") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 638 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 639 | sd_bus_message_append_basic(m, argCode[0], doubleValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 640 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 641 | else if (boost::starts_with(argCode, "a")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 642 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 643 | std::string containedType = argCode.substr(1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 644 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 645 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 646 | if (r < 0) |
| 647 | { |
| 648 | return r; |
| 649 | } |
| 650 | |
| 651 | for (nlohmann::json::const_iterator it = j->begin(); it != j->end(); |
| 652 | ++it) |
| 653 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 654 | r = convertJsonToDbus(m, containedType, *it); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 655 | if (r < 0) |
| 656 | { |
| 657 | return r; |
| 658 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 659 | } |
| 660 | sd_bus_message_close_container(m); |
| 661 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 662 | else if (boost::starts_with(argCode, "v")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 663 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 664 | std::string containedType = argCode.substr(1); |
| 665 | BMCWEB_LOG_DEBUG << "variant type: " << argCode |
| 666 | << " appending variant of type: " << containedType; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 667 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 668 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 669 | if (r < 0) |
| 670 | { |
| 671 | return r; |
| 672 | } |
| 673 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 674 | r = convertJsonToDbus(m, containedType, input_json); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 675 | if (r < 0) |
| 676 | { |
| 677 | return r; |
| 678 | } |
| 679 | |
| 680 | r = sd_bus_message_close_container(m); |
| 681 | if (r < 0) |
| 682 | { |
| 683 | return r; |
| 684 | } |
| 685 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 686 | else if (boost::starts_with(argCode, "(") && |
| 687 | boost::ends_with(argCode, ")")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 688 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 689 | std::string containedType = argCode.substr(1, argCode.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 690 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 691 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 692 | nlohmann::json::const_iterator it = j->begin(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 693 | for (const std::string &argCode : dbusArgSplit(arg_type)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 694 | { |
| 695 | if (it == j->end()) |
| 696 | { |
| 697 | return -1; |
| 698 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 699 | r = convertJsonToDbus(m, argCode, *it); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 700 | if (r < 0) |
| 701 | { |
| 702 | return r; |
| 703 | } |
| 704 | it++; |
| 705 | } |
| 706 | r = sd_bus_message_close_container(m); |
| 707 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 708 | else if (boost::starts_with(argCode, "{") && |
| 709 | boost::ends_with(argCode, "}")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 710 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 711 | std::string containedType = argCode.substr(1, argCode.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 712 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 713 | containedType.c_str()); |
| 714 | std::vector<std::string> codes = dbusArgSplit(containedType); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 715 | if (codes.size() != 2) |
| 716 | { |
| 717 | return -1; |
| 718 | } |
| 719 | const std::string &key_type = codes[0]; |
| 720 | const std::string &value_type = codes[1]; |
| 721 | for (auto it : j->items()) |
| 722 | { |
| 723 | r = convertJsonToDbus(m, key_type, it.key()); |
| 724 | if (r < 0) |
| 725 | { |
| 726 | return r; |
| 727 | } |
| 728 | |
| 729 | r = convertJsonToDbus(m, value_type, it.value()); |
| 730 | if (r < 0) |
| 731 | { |
| 732 | return r; |
| 733 | } |
| 734 | } |
| 735 | r = sd_bus_message_close_container(m); |
| 736 | } |
| 737 | else |
| 738 | { |
| 739 | return -2; |
| 740 | } |
| 741 | if (r < 0) |
| 742 | { |
| 743 | return r; |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 744 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 745 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 746 | if (argTypes.size() > 1) |
| 747 | { |
| 748 | jIt++; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 749 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 750 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Ed Tanous | d76323e | 2018-08-07 14:35:40 -0700 | [diff] [blame] | 753 | void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 754 | const std::string &connectionName) |
| 755 | { |
| 756 | BMCWEB_LOG_DEBUG << "findActionOnInterface for connection " |
| 757 | << connectionName; |
| 758 | crow::connections::systemBus->async_method_call( |
| 759 | [transaction, connectionName{std::string(connectionName)}]( |
| 760 | const boost::system::error_code ec, |
| 761 | const std::string &introspect_xml) { |
| 762 | BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml; |
| 763 | if (ec) |
| 764 | { |
| 765 | BMCWEB_LOG_ERROR |
| 766 | << "Introspect call failed with error: " << ec.message() |
| 767 | << " on process: " << connectionName << "\n"; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 768 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 769 | else |
| 770 | { |
| 771 | tinyxml2::XMLDocument doc; |
| 772 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 773 | doc.Parse(introspect_xml.data(), introspect_xml.size()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 774 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 775 | if (pRoot == nullptr) |
| 776 | { |
| 777 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 778 | << connectionName << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 779 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 780 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 781 | tinyxml2::XMLElement *interfaceNode = |
| 782 | pRoot->FirstChildElement("interface"); |
| 783 | while (interfaceNode != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 784 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 785 | const char *thisInterfaceName = |
| 786 | interfaceNode->Attribute("name"); |
| 787 | if (thisInterfaceName != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 788 | { |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 789 | if (!transaction->interfaceName.empty() && |
| 790 | (transaction->interfaceName != thisInterfaceName)) |
| 791 | { |
| 792 | interfaceNode = |
| 793 | interfaceNode->NextSiblingElement("interface"); |
| 794 | continue; |
| 795 | } |
| 796 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 797 | tinyxml2::XMLElement *methodNode = |
| 798 | interfaceNode->FirstChildElement("method"); |
| 799 | while (methodNode != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 800 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 801 | const char *thisMethodName = |
| 802 | methodNode->Attribute("name"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 803 | BMCWEB_LOG_DEBUG << "Found method: " |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 804 | << thisMethodName; |
| 805 | if (thisMethodName != nullptr && |
| 806 | thisMethodName == transaction->methodName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 807 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 808 | BMCWEB_LOG_DEBUG |
| 809 | << "Found method named " << thisMethodName |
| 810 | << " on interface " << thisInterfaceName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 811 | sdbusplus::message::message m = |
| 812 | crow::connections::systemBus |
| 813 | ->new_method_call( |
| 814 | connectionName.c_str(), |
| 815 | transaction->path.c_str(), |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 816 | thisInterfaceName, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 817 | transaction->methodName.c_str()); |
| 818 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 819 | tinyxml2::XMLElement *argumentNode = |
| 820 | methodNode->FirstChildElement("arg"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 821 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 822 | nlohmann::json::const_iterator argIt = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 823 | transaction->arguments.begin(); |
| 824 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 825 | while (argumentNode != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 826 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 827 | const char *argDirection = |
| 828 | argumentNode->Attribute("direction"); |
| 829 | const char *argType = |
| 830 | argumentNode->Attribute("type"); |
| 831 | if (argDirection != nullptr && |
| 832 | argType != nullptr && |
| 833 | std::string(argDirection) == "in") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 834 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 835 | if (argIt == |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 836 | transaction->arguments.end()) |
| 837 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 838 | transaction->setErrorStatus( |
| 839 | "Invalid method args"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 840 | return; |
| 841 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 842 | if (convertJsonToDbus( |
| 843 | m.get(), std::string(argType), |
| 844 | *argIt) < 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 845 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 846 | transaction->setErrorStatus( |
| 847 | "Invalid method arg type"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 851 | argIt++; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 852 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 853 | argumentNode = |
AppaRao Puli | 4d72dcc | 2018-12-26 20:26:22 +0530 | [diff] [blame] | 854 | argumentNode->NextSiblingElement("arg"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 855 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 856 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 857 | crow::connections::systemBus->async_send( |
| 858 | m, [transaction]( |
| 859 | boost::system::error_code ec, |
| 860 | sdbusplus::message::message &m) { |
| 861 | if (ec) |
| 862 | { |
Matt Spinler | c0eb9bd | 2019-01-09 15:22:30 -0600 | [diff] [blame] | 863 | setErrorResponse( |
| 864 | transaction->res, |
| 865 | boost::beast::http::status:: |
| 866 | internal_server_error, |
| 867 | "Method call failed", |
| 868 | methodFailedMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 869 | return; |
| 870 | } |
| 871 | transaction->res.jsonValue = { |
| 872 | {"status", "ok"}, |
| 873 | {"message", "200 OK"}, |
| 874 | {"data", nullptr}}; |
| 875 | }); |
| 876 | break; |
| 877 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 878 | methodNode = |
| 879 | methodNode->NextSiblingElement("method"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 880 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 881 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 882 | interfaceNode = |
| 883 | interfaceNode->NextSiblingElement("interface"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | }, |
| 887 | connectionName, transaction->path, |
| 888 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 891 | void handleAction(const crow::Request &req, crow::Response &res, |
| 892 | const std::string &objectPath, const std::string &methodName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 893 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 894 | BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method " |
| 895 | << methodName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 896 | nlohmann::json requestDbusData = |
| 897 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 898 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 899 | if (requestDbusData.is_discarded()) |
| 900 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 901 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 902 | noJsonDesc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 903 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 904 | return; |
| 905 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 906 | nlohmann::json::iterator data = requestDbusData.find("data"); |
| 907 | if (data == requestDbusData.end()) |
| 908 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 909 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 910 | noJsonDesc, badReqMsg); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 911 | res.end(); |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | if (!data->is_array()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 916 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 917 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 918 | noJsonDesc, badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 919 | res.end(); |
| 920 | return; |
| 921 | } |
| 922 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 923 | |
| 924 | transaction->path = objectPath; |
| 925 | transaction->methodName = methodName; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 926 | transaction->arguments = std::move(*data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 927 | crow::connections::systemBus->async_method_call( |
| 928 | [transaction]( |
| 929 | const boost::system::error_code ec, |
| 930 | const std::vector<std::pair<std::string, std::vector<std::string>>> |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 931 | &interfaceNames) { |
| 932 | if (ec || interfaceNames.size() <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 933 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 934 | BMCWEB_LOG_ERROR << "Can't find object"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 935 | setErrorResponse(transaction->res, |
| 936 | boost::beast::http::status::not_found, |
| 937 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 938 | return; |
| 939 | } |
| 940 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 941 | BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size() |
| 942 | << " object(s)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 943 | |
| 944 | for (const std::pair<std::string, std::vector<std::string>> |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 945 | &object : interfaceNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 946 | { |
| 947 | findActionOnInterface(transaction, object.first); |
| 948 | } |
| 949 | }, |
| 950 | "xyz.openbmc_project.ObjectMapper", |
| 951 | "/xyz/openbmc_project/object_mapper", |
| 952 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
| 953 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 956 | void handleDelete(const crow::Request &req, crow::Response &res, |
| 957 | const std::string &objectPath) |
| 958 | { |
| 959 | BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath; |
| 960 | |
| 961 | crow::connections::systemBus->async_method_call( |
| 962 | [&res, objectPath]( |
| 963 | const boost::system::error_code ec, |
| 964 | const std::vector<std::pair<std::string, std::vector<std::string>>> |
| 965 | &interfaceNames) { |
| 966 | if (ec || interfaceNames.size() <= 0) |
| 967 | { |
| 968 | BMCWEB_LOG_ERROR << "Can't find object"; |
| 969 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 970 | notFoundDesc, notFoundMsg); |
| 971 | res.end(); |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 976 | transaction->path = objectPath; |
| 977 | transaction->methodName = "Delete"; |
| 978 | transaction->interfaceName = "xyz.openbmc_project.Object.Delete"; |
| 979 | |
| 980 | for (const std::pair<std::string, std::vector<std::string>> |
| 981 | &object : interfaceNames) |
| 982 | { |
| 983 | findActionOnInterface(transaction, object.first); |
| 984 | } |
| 985 | }, |
| 986 | "xyz.openbmc_project.ObjectMapper", |
| 987 | "/xyz/openbmc_project/object_mapper", |
| 988 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
| 989 | std::array<const char *, 0>()); |
| 990 | } |
| 991 | |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 992 | void handleList(crow::Response &res, const std::string &objectPath, |
| 993 | int32_t depth = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 994 | { |
| 995 | crow::connections::systemBus->async_method_call( |
| 996 | [&res](const boost::system::error_code ec, |
| 997 | std::vector<std::string> &objectPaths) { |
| 998 | if (ec) |
| 999 | { |
Matt Spinler | d6091dd | 2018-12-06 14:08:27 -0600 | [diff] [blame] | 1000 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1001 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1002 | } |
| 1003 | else |
| 1004 | { |
| 1005 | res.jsonValue = {{"status", "ok"}, |
| 1006 | {"message", "200 OK"}, |
| 1007 | {"data", std::move(objectPaths)}}; |
| 1008 | } |
| 1009 | res.end(); |
| 1010 | }, |
| 1011 | "xyz.openbmc_project.ObjectMapper", |
| 1012 | "/xyz/openbmc_project/object_mapper", |
| 1013 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath, |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1014 | depth, std::array<std::string, 0>()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1015 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1016 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1017 | void handleEnumerate(crow::Response &res, const std::string &objectPath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1018 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1019 | BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath; |
| 1020 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res); |
| 1021 | |
| 1022 | asyncResp->res.jsonValue = {{"message", "200 OK"}, |
| 1023 | {"status", "ok"}, |
| 1024 | {"data", nlohmann::json::object()}}; |
| 1025 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1026 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1027 | [objectPath, asyncResp](const boost::system::error_code ec, |
| 1028 | GetSubTreeType &object_names) { |
| 1029 | auto transaction = std::make_shared<InProgressEnumerateData>( |
| 1030 | objectPath, asyncResp); |
| 1031 | |
| 1032 | transaction->subtree = |
| 1033 | std::make_shared<GetSubTreeType>(std::move(object_names)); |
| 1034 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1035 | if (ec) |
| 1036 | { |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 1037 | BMCWEB_LOG_ERROR << "GetSubTree failed on " |
| 1038 | << transaction->objectPath; |
| 1039 | setErrorResponse(transaction->asyncResp->res, |
| 1040 | boost::beast::http::status::not_found, |
| 1041 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1042 | return; |
| 1043 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1044 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1045 | // Add the data for the path passed in to the results |
| 1046 | // as if GetSubTree returned it, and continue on enumerating |
| 1047 | getObjectAndEnumerate(transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1048 | }, |
| 1049 | "xyz.openbmc_project.ObjectMapper", |
| 1050 | "/xyz/openbmc_project/object_mapper", |
| 1051 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1052 | static_cast<int32_t>(0), std::array<const char *, 0>()); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1053 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1054 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1055 | void handleGet(crow::Response &res, std::string &objectPath, |
| 1056 | std::string &destProperty) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1057 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1058 | BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty; |
| 1059 | std::shared_ptr<std::string> propertyName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1060 | std::make_shared<std::string>(std::move(destProperty)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1061 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1062 | std::shared_ptr<std::string> path = |
| 1063 | std::make_shared<std::string>(std::move(objectPath)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1064 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1065 | using GetObjectType = |
| 1066 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 1067 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1068 | [&res, path, propertyName](const boost::system::error_code ec, |
| 1069 | const GetObjectType &object_names) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1070 | if (ec || object_names.size() <= 0) |
| 1071 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1072 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1073 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1074 | res.end(); |
| 1075 | return; |
| 1076 | } |
| 1077 | std::shared_ptr<nlohmann::json> response = |
| 1078 | std::make_shared<nlohmann::json>(nlohmann::json::object()); |
| 1079 | // The mapper should never give us an empty interface names list, |
| 1080 | // but check anyway |
| 1081 | for (const std::pair<std::string, std::vector<std::string>> |
| 1082 | connection : object_names) |
| 1083 | { |
| 1084 | const std::vector<std::string> &interfaceNames = |
| 1085 | connection.second; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1086 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1087 | if (interfaceNames.size() <= 0) |
| 1088 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1089 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1090 | notFoundDesc, notFoundMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1091 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | for (const std::string &interface : interfaceNames) |
| 1096 | { |
| 1097 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1098 | [&res, response, propertyName]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1099 | const boost::system::error_code ec, |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1100 | const std::vector<std::pair< |
| 1101 | std::string, dbus::utility::DbusVariantType>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1102 | &properties) { |
| 1103 | if (ec) |
| 1104 | { |
| 1105 | BMCWEB_LOG_ERROR << "Bad dbus request error: " |
| 1106 | << ec; |
| 1107 | } |
| 1108 | else |
| 1109 | { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1110 | for (const std::pair< |
| 1111 | std::string, |
| 1112 | dbus::utility::DbusVariantType> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1113 | &property : properties) |
| 1114 | { |
| 1115 | // if property name is empty, or matches our |
| 1116 | // search query, add it to the response json |
| 1117 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1118 | if (propertyName->empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1119 | { |
William A. Kennington III | 0a63b1c | 2018-10-18 13:37:19 -0700 | [diff] [blame] | 1120 | sdbusplus::message::variant_ns::visit( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1121 | [&response, &property](auto &&val) { |
| 1122 | (*response)[property.first] = |
| 1123 | val; |
| 1124 | }, |
| 1125 | property.second); |
| 1126 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1127 | else if (property.first == *propertyName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1128 | { |
William A. Kennington III | 0a63b1c | 2018-10-18 13:37:19 -0700 | [diff] [blame] | 1129 | sdbusplus::message::variant_ns::visit( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1130 | [&response](auto &&val) { |
| 1131 | (*response) = val; |
| 1132 | }, |
| 1133 | property.second); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | if (response.use_count() == 1) |
| 1138 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1139 | if (!propertyName->empty() && response->empty()) |
| 1140 | { |
| 1141 | setErrorResponse( |
| 1142 | res, |
| 1143 | boost::beast::http::status::not_found, |
| 1144 | propNotFoundDesc, notFoundMsg); |
| 1145 | } |
| 1146 | else |
| 1147 | { |
| 1148 | res.jsonValue = {{"status", "ok"}, |
| 1149 | {"message", "200 OK"}, |
| 1150 | {"data", *response}}; |
| 1151 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1152 | res.end(); |
| 1153 | } |
| 1154 | }, |
| 1155 | connection.first, *path, |
| 1156 | "org.freedesktop.DBus.Properties", "GetAll", interface); |
| 1157 | } |
| 1158 | } |
| 1159 | }, |
| 1160 | "xyz.openbmc_project.ObjectMapper", |
| 1161 | "/xyz/openbmc_project/object_mapper", |
| 1162 | "xyz.openbmc_project.ObjectMapper", "GetObject", *path, |
| 1163 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1166 | struct AsyncPutRequest |
| 1167 | { |
| 1168 | AsyncPutRequest(crow::Response &res) : res(res) |
| 1169 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1170 | } |
| 1171 | ~AsyncPutRequest() |
| 1172 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1173 | if (res.jsonValue.empty()) |
| 1174 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1175 | setErrorResponse(res, boost::beast::http::status::forbidden, |
| 1176 | forbiddenMsg, forbiddenPropDesc); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | res.end(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1182 | void setErrorStatus(const std::string &desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1183 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1184 | setErrorResponse(res, boost::beast::http::status::internal_server_error, |
| 1185 | desc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1188 | crow::Response &res; |
| 1189 | std::string objectPath; |
| 1190 | std::string propertyName; |
| 1191 | nlohmann::json propertyValue; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1192 | }; |
| 1193 | |
Ed Tanous | d76323e | 2018-08-07 14:35:40 -0700 | [diff] [blame] | 1194 | void handlePut(const crow::Request &req, crow::Response &res, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1195 | const std::string &objectPath, const std::string &destProperty) |
| 1196 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1197 | if (destProperty.empty()) |
| 1198 | { |
| 1199 | setErrorResponse(res, boost::beast::http::status::forbidden, |
| 1200 | forbiddenResDesc, forbiddenMsg); |
| 1201 | res.end(); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1205 | nlohmann::json requestDbusData = |
| 1206 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1207 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1208 | if (requestDbusData.is_discarded()) |
| 1209 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1210 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1211 | noJsonDesc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1212 | res.end(); |
| 1213 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1214 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1215 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1216 | nlohmann::json::const_iterator propertyIt = requestDbusData.find("data"); |
| 1217 | if (propertyIt == requestDbusData.end()) |
| 1218 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1219 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1220 | noJsonDesc, badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1221 | res.end(); |
| 1222 | return; |
| 1223 | } |
| 1224 | const nlohmann::json &propertySetValue = *propertyIt; |
| 1225 | auto transaction = std::make_shared<AsyncPutRequest>(res); |
| 1226 | transaction->objectPath = objectPath; |
| 1227 | transaction->propertyName = destProperty; |
| 1228 | transaction->propertyValue = propertySetValue; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1229 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1230 | using GetObjectType = |
| 1231 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1232 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1233 | crow::connections::systemBus->async_method_call( |
| 1234 | [transaction](const boost::system::error_code ec, |
| 1235 | const GetObjectType &object_names) { |
| 1236 | if (!ec && object_names.size() <= 0) |
| 1237 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1238 | setErrorResponse(transaction->res, |
| 1239 | boost::beast::http::status::not_found, |
| 1240 | propNotFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1241 | return; |
| 1242 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1243 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1244 | for (const std::pair<std::string, std::vector<std::string>> |
| 1245 | connection : object_names) |
| 1246 | { |
| 1247 | const std::string &connectionName = connection.first; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1248 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1249 | crow::connections::systemBus->async_method_call( |
| 1250 | [connectionName{std::string(connectionName)}, |
| 1251 | transaction](const boost::system::error_code ec, |
| 1252 | const std::string &introspectXml) { |
| 1253 | if (ec) |
| 1254 | { |
| 1255 | BMCWEB_LOG_ERROR |
| 1256 | << "Introspect call failed with error: " |
| 1257 | << ec.message() |
| 1258 | << " on process: " << connectionName; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1259 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1260 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1261 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1262 | tinyxml2::XMLDocument doc; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1263 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1264 | doc.Parse(introspectXml.c_str()); |
| 1265 | tinyxml2::XMLNode *pRoot = |
| 1266 | doc.FirstChildElement("node"); |
| 1267 | if (pRoot == nullptr) |
| 1268 | { |
| 1269 | BMCWEB_LOG_ERROR << "XML document failed to parse: " |
| 1270 | << introspectXml; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1271 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1272 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1273 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1274 | tinyxml2::XMLElement *ifaceNode = |
| 1275 | pRoot->FirstChildElement("interface"); |
| 1276 | while (ifaceNode != nullptr) |
| 1277 | { |
| 1278 | const char *interfaceName = |
| 1279 | ifaceNode->Attribute("name"); |
| 1280 | BMCWEB_LOG_DEBUG << "found interface " |
| 1281 | << interfaceName; |
| 1282 | tinyxml2::XMLElement *propNode = |
| 1283 | ifaceNode->FirstChildElement("property"); |
| 1284 | while (propNode != nullptr) |
| 1285 | { |
| 1286 | const char *propertyName = |
| 1287 | propNode->Attribute("name"); |
| 1288 | BMCWEB_LOG_DEBUG << "Found property " |
| 1289 | << propertyName; |
| 1290 | if (propertyName == transaction->propertyName) |
| 1291 | { |
| 1292 | const char *argType = |
| 1293 | propNode->Attribute("type"); |
| 1294 | if (argType != nullptr) |
| 1295 | { |
| 1296 | sdbusplus::message::message m = |
| 1297 | crow::connections::systemBus |
| 1298 | ->new_method_call( |
| 1299 | connectionName.c_str(), |
| 1300 | transaction->objectPath |
| 1301 | .c_str(), |
| 1302 | "org.freedesktop.DBus." |
| 1303 | "Properties", |
| 1304 | "Set"); |
| 1305 | m.append(interfaceName, |
| 1306 | transaction->propertyName); |
| 1307 | int r = sd_bus_message_open_container( |
| 1308 | m.get(), SD_BUS_TYPE_VARIANT, |
| 1309 | argType); |
| 1310 | if (r < 0) |
| 1311 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1312 | transaction->setErrorStatus( |
| 1313 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | r = convertJsonToDbus( |
| 1317 | m.get(), argType, |
| 1318 | transaction->propertyValue); |
| 1319 | if (r < 0) |
| 1320 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1321 | transaction->setErrorStatus( |
| 1322 | "Invalid arg type"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1323 | return; |
| 1324 | } |
| 1325 | r = sd_bus_message_close_container( |
| 1326 | m.get()); |
| 1327 | if (r < 0) |
| 1328 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1329 | transaction->setErrorStatus( |
| 1330 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1331 | return; |
| 1332 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1333 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1334 | crow::connections::systemBus |
| 1335 | ->async_send( |
| 1336 | m, |
| 1337 | [transaction]( |
| 1338 | boost::system::error_code |
| 1339 | ec, |
| 1340 | sdbusplus::message::message |
| 1341 | &m) { |
| 1342 | BMCWEB_LOG_DEBUG << "sent"; |
| 1343 | if (ec) |
| 1344 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1345 | setErrorResponse( |
| 1346 | transaction->res, |
| 1347 | boost::beast::http:: |
| 1348 | status:: |
| 1349 | forbidden, |
| 1350 | forbiddenPropDesc, |
| 1351 | ec.message()); |
| 1352 | } |
| 1353 | else |
| 1354 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1355 | transaction->res |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1356 | .jsonValue = { |
| 1357 | {"status", "ok"}, |
| 1358 | {"message", |
| 1359 | "200 OK"}, |
| 1360 | {"data", nullptr}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1361 | } |
| 1362 | }); |
| 1363 | } |
| 1364 | } |
| 1365 | propNode = |
| 1366 | propNode->NextSiblingElement("property"); |
| 1367 | } |
| 1368 | ifaceNode = |
| 1369 | ifaceNode->NextSiblingElement("interface"); |
| 1370 | } |
| 1371 | }, |
| 1372 | connectionName, transaction->objectPath, |
| 1373 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 1374 | } |
| 1375 | }, |
| 1376 | "xyz.openbmc_project.ObjectMapper", |
| 1377 | "/xyz/openbmc_project/object_mapper", |
| 1378 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 1379 | transaction->objectPath, std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1380 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1381 | |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1382 | inline void handleDBusUrl(const crow::Request &req, crow::Response &res, |
| 1383 | std::string &objectPath) |
| 1384 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1385 | |
| 1386 | // If accessing a single attribute, fill in and update objectPath, |
| 1387 | // otherwise leave destProperty blank |
| 1388 | std::string destProperty = ""; |
| 1389 | const char *attrSeperator = "/attr/"; |
| 1390 | size_t attrPosition = objectPath.find(attrSeperator); |
| 1391 | if (attrPosition != objectPath.npos) |
| 1392 | { |
| 1393 | destProperty = objectPath.substr(attrPosition + strlen(attrSeperator), |
| 1394 | objectPath.length()); |
| 1395 | objectPath = objectPath.substr(0, attrPosition); |
| 1396 | } |
| 1397 | |
| 1398 | if (req.method() == "POST"_method) |
| 1399 | { |
| 1400 | constexpr const char *actionSeperator = "/action/"; |
| 1401 | size_t actionPosition = objectPath.find(actionSeperator); |
| 1402 | if (actionPosition != objectPath.npos) |
| 1403 | { |
| 1404 | std::string postProperty = |
| 1405 | objectPath.substr((actionPosition + strlen(actionSeperator)), |
| 1406 | objectPath.length()); |
| 1407 | objectPath = objectPath.substr(0, actionPosition); |
| 1408 | handleAction(req, res, objectPath, postProperty); |
| 1409 | return; |
| 1410 | } |
| 1411 | } |
| 1412 | else if (req.method() == "GET"_method) |
| 1413 | { |
| 1414 | if (boost::ends_with(objectPath, "/enumerate")) |
| 1415 | { |
| 1416 | objectPath.erase(objectPath.end() - sizeof("enumerate"), |
| 1417 | objectPath.end()); |
| 1418 | handleEnumerate(res, objectPath); |
| 1419 | } |
| 1420 | else if (boost::ends_with(objectPath, "/list")) |
| 1421 | { |
| 1422 | objectPath.erase(objectPath.end() - sizeof("list"), |
| 1423 | objectPath.end()); |
| 1424 | handleList(res, objectPath); |
| 1425 | } |
| 1426 | else |
| 1427 | { |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1428 | // Trim any trailing "/" at the end |
| 1429 | if (boost::ends_with(objectPath, "/")) |
| 1430 | { |
| 1431 | objectPath.pop_back(); |
| 1432 | handleList(res, objectPath, 1); |
| 1433 | } |
| 1434 | else |
| 1435 | { |
| 1436 | handleGet(res, objectPath, destProperty); |
| 1437 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1438 | } |
| 1439 | return; |
| 1440 | } |
| 1441 | else if (req.method() == "PUT"_method) |
| 1442 | { |
| 1443 | handlePut(req, res, objectPath, destProperty); |
| 1444 | return; |
| 1445 | } |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1446 | else if (req.method() == "DELETE"_method) |
| 1447 | { |
| 1448 | handleDelete(req, res, objectPath); |
| 1449 | return; |
| 1450 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1451 | |
Matt Spinler | c4e8d21d6 | 2018-12-11 11:47:17 -0600 | [diff] [blame] | 1452 | setErrorResponse(res, boost::beast::http::status::method_not_allowed, |
| 1453 | methodNotAllowedDesc, methodNotAllowedMsg); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1454 | res.end(); |
| 1455 | } |
| 1456 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1457 | template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app) |
| 1458 | { |
| 1459 | BMCWEB_ROUTE(app, "/bus/") |
| 1460 | .methods("GET"_method)( |
| 1461 | [](const crow::Request &req, crow::Response &res) { |
| 1462 | res.jsonValue = {{"busses", {{{"name", "system"}}}}, |
| 1463 | {"status", "ok"}}; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1464 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1465 | }); |
| 1466 | |
| 1467 | BMCWEB_ROUTE(app, "/bus/system/") |
| 1468 | .methods("GET"_method)( |
| 1469 | [](const crow::Request &req, crow::Response &res) { |
| 1470 | auto myCallback = [&res](const boost::system::error_code ec, |
| 1471 | std::vector<std::string> &names) { |
| 1472 | if (ec) |
| 1473 | { |
| 1474 | BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec; |
| 1475 | res.result( |
| 1476 | boost::beast::http::status::internal_server_error); |
| 1477 | } |
| 1478 | else |
| 1479 | { |
| 1480 | std::sort(names.begin(), names.end()); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1481 | res.jsonValue = {{"status", "ok"}}; |
| 1482 | auto &objectsSub = res.jsonValue["objects"]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1483 | for (auto &name : names) |
| 1484 | { |
| 1485 | objectsSub.push_back({{"name", name}}); |
| 1486 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1487 | } |
| 1488 | res.end(); |
| 1489 | }; |
| 1490 | crow::connections::systemBus->async_method_call( |
| 1491 | std::move(myCallback), "org.freedesktop.DBus", "/", |
| 1492 | "org.freedesktop.DBus", "ListNames"); |
| 1493 | }); |
| 1494 | |
| 1495 | BMCWEB_ROUTE(app, "/list/") |
| 1496 | .methods("GET"_method)( |
| 1497 | [](const crow::Request &req, crow::Response &res) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1498 | handleList(res, "/"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1499 | }); |
| 1500 | |
| 1501 | BMCWEB_ROUTE(app, "/xyz/<path>") |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1502 | .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)( |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1503 | [](const crow::Request &req, crow::Response &res, |
| 1504 | const std::string &path) { |
| 1505 | std::string objectPath = "/xyz/" + path; |
| 1506 | handleDBusUrl(req, res, objectPath); |
| 1507 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1508 | |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1509 | BMCWEB_ROUTE(app, "/org/<path>") |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1510 | .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)( |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1511 | [](const crow::Request &req, crow::Response &res, |
| 1512 | const std::string &path) { |
| 1513 | std::string objectPath = "/org/" + path; |
| 1514 | handleDBusUrl(req, res, objectPath); |
| 1515 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1516 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1517 | BMCWEB_ROUTE(app, "/download/dump/<str>/") |
| 1518 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
| 1519 | const std::string &dumpId) { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1520 | std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1521 | if (!std::regex_match(dumpId, validFilename)) |
| 1522 | { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1523 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1524 | res.end(); |
| 1525 | return; |
| 1526 | } |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 1527 | std::filesystem::path loc( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1528 | "/var/lib/phosphor-debug-collector/dumps"); |
| 1529 | |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1530 | loc /= dumpId; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1531 | |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 1532 | if (!std::filesystem::exists(loc) || |
| 1533 | !std::filesystem::is_directory(loc)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1534 | { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1535 | BMCWEB_LOG_ERROR << loc << "Not found"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1536 | res.result(boost::beast::http::status::not_found); |
| 1537 | res.end(); |
| 1538 | return; |
| 1539 | } |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 1540 | std::filesystem::directory_iterator files(loc); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1541 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1542 | for (auto &file : files) |
| 1543 | { |
| 1544 | std::ifstream readFile(file.path()); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1545 | if (!readFile.good()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1546 | { |
| 1547 | continue; |
| 1548 | } |
| 1549 | res.addHeader("Content-Type", "application/octet-stream"); |
| 1550 | res.body() = {std::istreambuf_iterator<char>(readFile), |
| 1551 | std::istreambuf_iterator<char>()}; |
| 1552 | res.end(); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 1553 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1554 | } |
| 1555 | res.result(boost::beast::http::status::not_found); |
| 1556 | res.end(); |
| 1557 | return; |
| 1558 | }); |
| 1559 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1560 | BMCWEB_ROUTE(app, "/bus/system/<str>/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1561 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1562 | const std::string &Connection) { |
| 1563 | introspectObjects(Connection, "/", |
| 1564 | std::make_shared<bmcweb::AsyncResp>(res)); |
| 1565 | }); |
| 1566 | |
| 1567 | BMCWEB_ROUTE(app, "/bus/system/<str>/<path>") |
| 1568 | .methods("GET"_method, |
| 1569 | "POST"_method)([](const crow::Request &req, |
| 1570 | crow::Response &res, |
| 1571 | const std::string &processName, |
| 1572 | const std::string &requestedPath) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1573 | std::vector<std::string> strs; |
| 1574 | boost::split(strs, requestedPath, boost::is_any_of("/")); |
| 1575 | std::string objectPath; |
| 1576 | std::string interfaceName; |
| 1577 | std::string methodName; |
| 1578 | auto it = strs.begin(); |
| 1579 | if (it == strs.end()) |
| 1580 | { |
| 1581 | objectPath = "/"; |
| 1582 | } |
| 1583 | while (it != strs.end()) |
| 1584 | { |
| 1585 | // Check if segment contains ".". If it does, it must be an |
| 1586 | // interface |
| 1587 | if (it->find(".") != std::string::npos) |
| 1588 | { |
| 1589 | break; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1590 | // This check is neccesary as the trailing slash gets parsed |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1591 | // as part of our <path> specifier above, which causes the |
| 1592 | // normal trailing backslash redirector to fail. |
| 1593 | } |
| 1594 | else if (!it->empty()) |
| 1595 | { |
| 1596 | objectPath += "/" + *it; |
| 1597 | } |
| 1598 | it++; |
| 1599 | } |
| 1600 | if (it != strs.end()) |
| 1601 | { |
| 1602 | interfaceName = *it; |
| 1603 | it++; |
| 1604 | |
| 1605 | // after interface, we might have a method name |
| 1606 | if (it != strs.end()) |
| 1607 | { |
| 1608 | methodName = *it; |
| 1609 | it++; |
| 1610 | } |
| 1611 | } |
| 1612 | if (it != strs.end()) |
| 1613 | { |
| 1614 | // if there is more levels past the method name, something went |
| 1615 | // wrong, return not found |
| 1616 | res.result(boost::beast::http::status::not_found); |
| 1617 | res.end(); |
| 1618 | return; |
| 1619 | } |
| 1620 | if (interfaceName.empty()) |
| 1621 | { |
| 1622 | crow::connections::systemBus->async_method_call( |
| 1623 | [&, processName, |
| 1624 | objectPath](const boost::system::error_code ec, |
| 1625 | const std::string &introspect_xml) { |
| 1626 | if (ec) |
| 1627 | { |
| 1628 | BMCWEB_LOG_ERROR |
| 1629 | << "Introspect call failed with error: " |
| 1630 | << ec.message() |
| 1631 | << " on process: " << processName |
| 1632 | << " path: " << objectPath << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1633 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1634 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1635 | tinyxml2::XMLDocument doc; |
| 1636 | |
| 1637 | doc.Parse(introspect_xml.c_str()); |
| 1638 | tinyxml2::XMLNode *pRoot = |
| 1639 | doc.FirstChildElement("node"); |
| 1640 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1641 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1642 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 1643 | << processName << " " << objectPath |
| 1644 | << "\n"; |
| 1645 | res.jsonValue = {{"status", "XML parse error"}}; |
| 1646 | res.result(boost::beast::http::status:: |
| 1647 | internal_server_error); |
| 1648 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1649 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1650 | |
| 1651 | BMCWEB_LOG_DEBUG << introspect_xml; |
| 1652 | res.jsonValue = {{"status", "ok"}, |
| 1653 | {"bus_name", processName}, |
| 1654 | {"object_path", objectPath}}; |
| 1655 | nlohmann::json &interfacesArray = |
| 1656 | res.jsonValue["interfaces"]; |
| 1657 | interfacesArray = nlohmann::json::array(); |
| 1658 | tinyxml2::XMLElement *interface = |
| 1659 | pRoot->FirstChildElement("interface"); |
| 1660 | |
| 1661 | while (interface != nullptr) |
| 1662 | { |
| 1663 | const char *ifaceName = |
| 1664 | interface->Attribute("name"); |
| 1665 | if (ifaceName != nullptr) |
| 1666 | { |
| 1667 | interfacesArray.push_back( |
| 1668 | {{"name", ifaceName}}); |
| 1669 | } |
| 1670 | |
| 1671 | interface = |
| 1672 | interface->NextSiblingElement("interface"); |
| 1673 | } |
| 1674 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1675 | res.end(); |
| 1676 | }, |
| 1677 | processName, objectPath, |
| 1678 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 1679 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1680 | else if (methodName.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1681 | { |
| 1682 | crow::connections::systemBus->async_method_call( |
| 1683 | [&, processName, objectPath, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1684 | interfaceName{std::move(interfaceName)}]( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1685 | const boost::system::error_code ec, |
| 1686 | const std::string &introspect_xml) { |
| 1687 | if (ec) |
| 1688 | { |
| 1689 | BMCWEB_LOG_ERROR |
| 1690 | << "Introspect call failed with error: " |
| 1691 | << ec.message() |
| 1692 | << " on process: " << processName |
| 1693 | << " path: " << objectPath << "\n"; |
| 1694 | } |
| 1695 | else |
| 1696 | { |
| 1697 | tinyxml2::XMLDocument doc; |
| 1698 | |
| 1699 | doc.Parse(introspect_xml.c_str()); |
| 1700 | tinyxml2::XMLNode *pRoot = |
| 1701 | doc.FirstChildElement("node"); |
| 1702 | if (pRoot == nullptr) |
| 1703 | { |
| 1704 | BMCWEB_LOG_ERROR |
| 1705 | << "XML document failed to parse " |
| 1706 | << processName << " " << objectPath << "\n"; |
| 1707 | res.result(boost::beast::http::status:: |
| 1708 | internal_server_error); |
| 1709 | } |
| 1710 | else |
| 1711 | { |
| 1712 | tinyxml2::XMLElement *node = |
| 1713 | pRoot->FirstChildElement("node"); |
| 1714 | |
| 1715 | // if we know we're the only call, build the |
| 1716 | // json directly |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1717 | tinyxml2::XMLElement *interface = |
| 1718 | pRoot->FirstChildElement("interface"); |
| 1719 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1720 | res.jsonValue = { |
| 1721 | {"status", "ok"}, |
| 1722 | {"bus_name", processName}, |
| 1723 | {"interface", interfaceName}, |
| 1724 | {"object_path", objectPath}, |
| 1725 | {"properties", nlohmann::json::object()}}; |
| 1726 | |
| 1727 | nlohmann::json &methodsArray = |
| 1728 | res.jsonValue["methods"]; |
| 1729 | methodsArray = nlohmann::json::array(); |
| 1730 | |
| 1731 | nlohmann::json &signalsArray = |
| 1732 | res.jsonValue["signals"]; |
| 1733 | signalsArray = nlohmann::json::array(); |
| 1734 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1735 | while (interface != nullptr) |
| 1736 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1737 | const char *ifaceName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1738 | interface->Attribute("name"); |
| 1739 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1740 | if (ifaceName != nullptr && |
| 1741 | ifaceName == interfaceName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1742 | { |
| 1743 | tinyxml2::XMLElement *methods = |
| 1744 | interface->FirstChildElement( |
| 1745 | "method"); |
| 1746 | while (methods != nullptr) |
| 1747 | { |
| 1748 | nlohmann::json argsArray = |
| 1749 | nlohmann::json::array(); |
| 1750 | tinyxml2::XMLElement *arg = |
| 1751 | methods->FirstChildElement( |
| 1752 | "arg"); |
| 1753 | while (arg != nullptr) |
| 1754 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1755 | nlohmann::json thisArg; |
| 1756 | for (const char *fieldName : |
| 1757 | std::array<const char *, |
| 1758 | 3>{"name", |
| 1759 | "direction", |
| 1760 | "type"}) |
| 1761 | { |
| 1762 | const char *fieldValue = |
| 1763 | arg->Attribute( |
| 1764 | fieldName); |
| 1765 | if (fieldValue != nullptr) |
| 1766 | { |
| 1767 | thisArg[fieldName] = |
| 1768 | fieldValue; |
| 1769 | } |
| 1770 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1771 | argsArray.push_back( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1772 | std::move(thisArg)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1773 | arg = arg->NextSiblingElement( |
| 1774 | "arg"); |
| 1775 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1776 | |
| 1777 | const char *name = |
| 1778 | methods->Attribute("name"); |
| 1779 | if (name != nullptr) |
| 1780 | { |
| 1781 | methodsArray.push_back( |
| 1782 | {{"name", name}, |
| 1783 | {"uri", "/bus/system/" + |
| 1784 | processName + |
| 1785 | objectPath + |
| 1786 | "/" + |
| 1787 | interfaceName + |
| 1788 | "/" + name}, |
| 1789 | {"args", argsArray}}); |
| 1790 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1791 | methods = |
| 1792 | methods->NextSiblingElement( |
| 1793 | "method"); |
| 1794 | } |
| 1795 | tinyxml2::XMLElement *signals = |
| 1796 | interface->FirstChildElement( |
| 1797 | "signal"); |
| 1798 | while (signals != nullptr) |
| 1799 | { |
| 1800 | nlohmann::json argsArray = |
| 1801 | nlohmann::json::array(); |
| 1802 | |
| 1803 | tinyxml2::XMLElement *arg = |
| 1804 | signals->FirstChildElement( |
| 1805 | "arg"); |
| 1806 | while (arg != nullptr) |
| 1807 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1808 | const char *name = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1809 | arg->Attribute("name"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1810 | const char *type = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1811 | arg->Attribute("type"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1812 | if (name != nullptr && |
| 1813 | type != nullptr) |
| 1814 | { |
| 1815 | argsArray.push_back({ |
| 1816 | {"name", name}, |
| 1817 | {"type", type}, |
| 1818 | }); |
| 1819 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1820 | arg = arg->NextSiblingElement( |
| 1821 | "arg"); |
| 1822 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1823 | const char *name = |
| 1824 | signals->Attribute("name"); |
| 1825 | if (name != nullptr) |
| 1826 | { |
| 1827 | signalsArray.push_back( |
| 1828 | {{"name", name}, |
| 1829 | {"args", argsArray}}); |
| 1830 | } |
| 1831 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1832 | signals = |
| 1833 | signals->NextSiblingElement( |
| 1834 | "signal"); |
| 1835 | } |
| 1836 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1837 | break; |
| 1838 | } |
| 1839 | |
| 1840 | interface = interface->NextSiblingElement( |
| 1841 | "interface"); |
| 1842 | } |
| 1843 | if (interface == nullptr) |
| 1844 | { |
| 1845 | // if we got to the end of the list and |
| 1846 | // never found a match, throw 404 |
| 1847 | res.result( |
| 1848 | boost::beast::http::status::not_found); |
| 1849 | } |
| 1850 | } |
| 1851 | } |
| 1852 | res.end(); |
| 1853 | }, |
| 1854 | processName, objectPath, |
| 1855 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 1856 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1857 | else |
| 1858 | { |
| 1859 | if (req.method() != "POST"_method) |
| 1860 | { |
| 1861 | res.result(boost::beast::http::status::not_found); |
| 1862 | res.end(); |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | nlohmann::json requestDbusData = |
| 1867 | nlohmann::json::parse(req.body, nullptr, false); |
| 1868 | |
| 1869 | if (requestDbusData.is_discarded()) |
| 1870 | { |
| 1871 | res.result(boost::beast::http::status::bad_request); |
| 1872 | res.end(); |
| 1873 | return; |
| 1874 | } |
| 1875 | if (!requestDbusData.is_array()) |
| 1876 | { |
| 1877 | res.result(boost::beast::http::status::bad_request); |
| 1878 | res.end(); |
| 1879 | return; |
| 1880 | } |
| 1881 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 1882 | |
| 1883 | transaction->path = objectPath; |
| 1884 | transaction->methodName = methodName; |
| 1885 | transaction->arguments = std::move(requestDbusData); |
| 1886 | |
| 1887 | findActionOnInterface(transaction, processName); |
| 1888 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1889 | }); |
| 1890 | } |
| 1891 | } // namespace openbmc_mapper |
| 1892 | } // namespace crow |