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