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