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