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