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 | |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 319 | // Uses GetObject to add the object info about the target /enumerate path to |
| 320 | // the results of GetSubTree, as GetSubTree will not return info for the |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 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 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 373 | // If we already know where the object manager is, we don't |
| 374 | // need to search for it, we can call directly in to |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 375 | // getManagedObjects |
| 376 | if (!connection.second.empty()) |
| 377 | { |
| 378 | getManagedObjectsForEnumerate( |
| 379 | transaction->objectPath, connection.second, |
| 380 | connection.first, transaction); |
| 381 | } |
| 382 | else |
| 383 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 384 | // otherwise we need to find the object manager path |
| 385 | // before we can continue |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 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 *>(); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 861 | // in theory this can't fail now, but lets be paranoid about it |
| 862 | // anyway |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 863 | if (keyPtr == nullptr) |
| 864 | { |
| 865 | return -1; |
| 866 | } |
| 867 | } |
| 868 | nlohmann::json &value = object[*keyPtr]; |
| 869 | |
| 870 | r = convertDBusToJSON(types[1], m, value); |
| 871 | if (r < 0) |
| 872 | { |
| 873 | return r; |
| 874 | } |
| 875 | |
| 876 | r = sd_bus_message_exit_container(m.get()); |
| 877 | if (r < 0) |
| 878 | { |
| 879 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 880 | return r; |
| 881 | } |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | int readArrayFromMessage(const std::string &typeCode, |
| 887 | sdbusplus::message::message &m, nlohmann::json &data) |
| 888 | { |
| 889 | if (typeCode.size() < 2) |
| 890 | { |
| 891 | BMCWEB_LOG_ERROR << "Type code " << typeCode |
| 892 | << " too small for an array"; |
| 893 | return -1; |
| 894 | } |
| 895 | |
| 896 | std::string containedType = typeCode.substr(1); |
| 897 | |
| 898 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY, |
| 899 | containedType.c_str()); |
| 900 | if (r < 0) |
| 901 | { |
| 902 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 903 | << r; |
| 904 | return r; |
| 905 | } |
| 906 | |
| 907 | bool dict = boost::starts_with(containedType, "{") && |
| 908 | boost::ends_with(containedType, "}"); |
| 909 | |
| 910 | if (dict) |
| 911 | { |
| 912 | // Remove the { } |
| 913 | containedType = containedType.substr(1, containedType.size() - 2); |
| 914 | data = nlohmann::json::object(); |
| 915 | } |
| 916 | else |
| 917 | { |
| 918 | data = nlohmann::json::array(); |
| 919 | } |
| 920 | |
| 921 | while (true) |
| 922 | { |
| 923 | r = sd_bus_message_at_end(m.get(), false); |
| 924 | if (r < 0) |
| 925 | { |
| 926 | BMCWEB_LOG_ERROR << "sd_bus_message_at_end failed"; |
| 927 | return r; |
| 928 | } |
| 929 | |
| 930 | if (r > 0) |
| 931 | { |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | // Dictionaries are only ever seen in an array |
| 936 | if (dict) |
| 937 | { |
| 938 | r = readDictEntryFromMessage(containedType, m, data); |
| 939 | if (r < 0) |
| 940 | { |
| 941 | return r; |
| 942 | } |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | data.push_back(nlohmann::json()); |
| 947 | |
| 948 | r = convertDBusToJSON(containedType, m, data.back()); |
| 949 | if (r < 0) |
| 950 | { |
| 951 | return r; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | r = sd_bus_message_exit_container(m.get()); |
| 957 | if (r < 0) |
| 958 | { |
| 959 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 960 | return r; |
| 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 966 | int readStructFromMessage(const std::string &typeCode, |
| 967 | sdbusplus::message::message &m, nlohmann::json &data) |
| 968 | { |
| 969 | if (typeCode.size() < 3) |
| 970 | { |
| 971 | BMCWEB_LOG_ERROR << "Type code " << typeCode |
| 972 | << " too small for a struct"; |
| 973 | return -1; |
| 974 | } |
| 975 | |
| 976 | std::string containedTypes = typeCode.substr(1, typeCode.size() - 2); |
| 977 | std::vector<std::string> types = dbusArgSplit(containedTypes); |
| 978 | |
| 979 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_STRUCT, |
| 980 | containedTypes.c_str()); |
| 981 | if (r < 0) |
| 982 | { |
| 983 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 984 | << r; |
| 985 | return r; |
| 986 | } |
| 987 | |
| 988 | for (const std::string &type : types) |
| 989 | { |
| 990 | data.push_back(nlohmann::json()); |
| 991 | r = convertDBusToJSON(type, m, data.back()); |
| 992 | if (r < 0) |
| 993 | { |
| 994 | return r; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | r = sd_bus_message_exit_container(m.get()); |
| 999 | if (r < 0) |
| 1000 | { |
| 1001 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 1002 | return r; |
| 1003 | } |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1007 | int readVariantFromMessage(sdbusplus::message::message &m, nlohmann::json &data) |
| 1008 | { |
| 1009 | const char *containerType; |
| 1010 | int r = sd_bus_message_peek_type(m.get(), NULL, &containerType); |
| 1011 | if (r < 0) |
| 1012 | { |
| 1013 | BMCWEB_LOG_ERROR << "sd_bus_message_peek_type failed"; |
| 1014 | return r; |
| 1015 | } |
| 1016 | |
| 1017 | r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_VARIANT, |
| 1018 | containerType); |
| 1019 | if (r < 0) |
| 1020 | { |
| 1021 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 1022 | << r; |
| 1023 | return r; |
| 1024 | } |
| 1025 | |
| 1026 | r = convertDBusToJSON(containerType, m, data); |
| 1027 | if (r < 0) |
| 1028 | { |
| 1029 | return r; |
| 1030 | } |
| 1031 | |
| 1032 | r = sd_bus_message_exit_container(m.get()); |
| 1033 | if (r < 0) |
| 1034 | { |
| 1035 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed"; |
| 1036 | return r; |
| 1037 | } |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1042 | int convertDBusToJSON(const std::string &returnType, |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1043 | sdbusplus::message::message &m, nlohmann::json &response) |
| 1044 | { |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1045 | int r = 0; |
| 1046 | const std::vector<std::string> returnTypes = dbusArgSplit(returnType); |
| 1047 | |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1048 | for (const std::string &typeCode : returnTypes) |
| 1049 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1050 | nlohmann::json *thisElement = &response; |
| 1051 | if (returnTypes.size() > 1) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1052 | { |
| 1053 | response.push_back(nlohmann::json{}); |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1054 | thisElement = &response.back(); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | if (typeCode == "s") |
| 1058 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1059 | r = readMessageItem<char *>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1060 | if (r < 0) |
| 1061 | { |
| 1062 | return r; |
| 1063 | } |
| 1064 | } |
| 1065 | else if (typeCode == "g") |
| 1066 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1067 | r = readMessageItem<char *>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1068 | if (r < 0) |
| 1069 | { |
| 1070 | return r; |
| 1071 | } |
| 1072 | } |
| 1073 | else if (typeCode == "o") |
| 1074 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1075 | r = readMessageItem<char *>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1076 | if (r < 0) |
| 1077 | { |
| 1078 | return r; |
| 1079 | } |
| 1080 | } |
| 1081 | else if (typeCode == "b") |
| 1082 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1083 | r = readMessageItem<int>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1084 | if (r < 0) |
| 1085 | { |
| 1086 | return r; |
| 1087 | } |
| 1088 | |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1089 | *thisElement = static_cast<bool>(thisElement->get<int>()); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1090 | } |
| 1091 | else if (typeCode == "u") |
| 1092 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1093 | r = readMessageItem<uint32_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1094 | if (r < 0) |
| 1095 | { |
| 1096 | return r; |
| 1097 | } |
| 1098 | } |
| 1099 | else if (typeCode == "i") |
| 1100 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1101 | r = readMessageItem<int32_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1102 | if (r < 0) |
| 1103 | { |
| 1104 | return r; |
| 1105 | } |
| 1106 | } |
| 1107 | else if (typeCode == "x") |
| 1108 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1109 | r = readMessageItem<int64_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1110 | if (r < 0) |
| 1111 | { |
| 1112 | return r; |
| 1113 | } |
| 1114 | } |
| 1115 | else if (typeCode == "t") |
| 1116 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1117 | r = readMessageItem<uint64_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1118 | if (r < 0) |
| 1119 | { |
| 1120 | return r; |
| 1121 | } |
| 1122 | } |
| 1123 | else if (typeCode == "n") |
| 1124 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1125 | r = readMessageItem<int16_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1126 | if (r < 0) |
| 1127 | { |
| 1128 | return r; |
| 1129 | } |
| 1130 | } |
| 1131 | else if (typeCode == "q") |
| 1132 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1133 | r = readMessageItem<uint16_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1134 | if (r < 0) |
| 1135 | { |
| 1136 | return r; |
| 1137 | } |
| 1138 | } |
| 1139 | else if (typeCode == "y") |
| 1140 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1141 | r = readMessageItem<uint8_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1142 | if (r < 0) |
| 1143 | { |
| 1144 | return r; |
| 1145 | } |
| 1146 | } |
| 1147 | else if (typeCode == "d") |
| 1148 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1149 | r = readMessageItem<double>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1150 | if (r < 0) |
| 1151 | { |
| 1152 | return r; |
| 1153 | } |
| 1154 | } |
| 1155 | else if (typeCode == "h") |
| 1156 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1157 | r = readMessageItem<int>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1158 | if (r < 0) |
| 1159 | { |
| 1160 | return r; |
| 1161 | } |
| 1162 | } |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1163 | else if (boost::starts_with(typeCode, "a")) |
| 1164 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1165 | r = readArrayFromMessage(typeCode, m, *thisElement); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1166 | if (r < 0) |
| 1167 | { |
| 1168 | return r; |
| 1169 | } |
| 1170 | } |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1171 | else if (boost::starts_with(typeCode, "(") && |
| 1172 | boost::ends_with(typeCode, ")")) |
| 1173 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1174 | r = readStructFromMessage(typeCode, m, *thisElement); |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1175 | if (r < 0) |
| 1176 | { |
| 1177 | return r; |
| 1178 | } |
| 1179 | } |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1180 | else if (boost::starts_with(typeCode, "v")) |
| 1181 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1182 | r = readVariantFromMessage(m, *thisElement); |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1183 | if (r < 0) |
| 1184 | { |
| 1185 | return r; |
| 1186 | } |
| 1187 | } |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1188 | else |
| 1189 | { |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1190 | BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode; |
| 1191 | return -2; |
| 1192 | } |
| 1193 | } |
| 1194 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | void handleMethodResponse(std::shared_ptr<InProgressActionData> transaction, |
| 1199 | sdbusplus::message::message &m, |
| 1200 | const std::string &returnType) |
| 1201 | { |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1202 | nlohmann::json data; |
| 1203 | |
| 1204 | int r = convertDBusToJSON(returnType, m, data); |
| 1205 | if (r < 0) |
| 1206 | { |
| 1207 | transaction->outputFailed = true; |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | if (data.is_null()) |
| 1212 | { |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | if (transaction->methodResponse.is_null()) |
| 1217 | { |
| 1218 | transaction->methodResponse = std::move(data); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | // If they're both dictionaries or arrays, merge into one. |
| 1223 | // Otherwise, make the results an array with every result |
| 1224 | // an entry. Could also just fail in that case, but it |
| 1225 | // seems better to get the data back somehow. |
| 1226 | |
| 1227 | if (transaction->methodResponse.is_object() && data.is_object()) |
| 1228 | { |
| 1229 | for (const auto &obj : data.items()) |
| 1230 | { |
| 1231 | // Note: Will overwrite the data for a duplicate key |
| 1232 | transaction->methodResponse.emplace(obj.key(), |
| 1233 | std::move(obj.value())); |
| 1234 | } |
| 1235 | return; |
| 1236 | } |
| 1237 | |
| 1238 | if (transaction->methodResponse.is_array() && data.is_array()) |
| 1239 | { |
| 1240 | for (auto &obj : data) |
| 1241 | { |
| 1242 | transaction->methodResponse.push_back(std::move(obj)); |
| 1243 | } |
| 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | if (!transaction->convertedToArray) |
| 1248 | { |
| 1249 | // They are different types. May as well turn them into an array |
| 1250 | nlohmann::json j = std::move(transaction->methodResponse); |
| 1251 | transaction->methodResponse = nlohmann::json::array(); |
| 1252 | transaction->methodResponse.push_back(std::move(j)); |
| 1253 | transaction->methodResponse.push_back(std::move(data)); |
| 1254 | transaction->convertedToArray = true; |
| 1255 | } |
| 1256 | else |
| 1257 | { |
| 1258 | transaction->methodResponse.push_back(std::move(data)); |
| 1259 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1260 | } |
| 1261 | |
Ed Tanous | d76323e | 2018-08-07 14:35:40 -0700 | [diff] [blame] | 1262 | void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1263 | const std::string &connectionName) |
| 1264 | { |
| 1265 | BMCWEB_LOG_DEBUG << "findActionOnInterface for connection " |
| 1266 | << connectionName; |
| 1267 | crow::connections::systemBus->async_method_call( |
| 1268 | [transaction, connectionName{std::string(connectionName)}]( |
| 1269 | const boost::system::error_code ec, |
| 1270 | const std::string &introspect_xml) { |
| 1271 | BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml; |
| 1272 | if (ec) |
| 1273 | { |
| 1274 | BMCWEB_LOG_ERROR |
| 1275 | << "Introspect call failed with error: " << ec.message() |
| 1276 | << " on process: " << connectionName << "\n"; |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1277 | return; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1278 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1279 | tinyxml2::XMLDocument doc; |
| 1280 | |
| 1281 | doc.Parse(introspect_xml.data(), introspect_xml.size()); |
| 1282 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 1283 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1284 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1285 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 1286 | << connectionName << "\n"; |
| 1287 | return; |
| 1288 | } |
| 1289 | tinyxml2::XMLElement *interfaceNode = |
| 1290 | pRoot->FirstChildElement("interface"); |
| 1291 | while (interfaceNode != nullptr) |
| 1292 | { |
| 1293 | const char *thisInterfaceName = |
| 1294 | interfaceNode->Attribute("name"); |
| 1295 | if (thisInterfaceName != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1296 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1297 | if (!transaction->interfaceName.empty() && |
| 1298 | (transaction->interfaceName != thisInterfaceName)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1299 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1300 | interfaceNode = |
| 1301 | interfaceNode->NextSiblingElement("interface"); |
| 1302 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1303 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1304 | |
| 1305 | tinyxml2::XMLElement *methodNode = |
| 1306 | interfaceNode->FirstChildElement("method"); |
| 1307 | while (methodNode != nullptr) |
| 1308 | { |
| 1309 | const char *thisMethodName = |
| 1310 | methodNode->Attribute("name"); |
| 1311 | BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName; |
| 1312 | if (thisMethodName != nullptr && |
| 1313 | thisMethodName == transaction->methodName) |
| 1314 | { |
| 1315 | BMCWEB_LOG_DEBUG |
| 1316 | << "Found method named " << thisMethodName |
| 1317 | << " on interface " << thisInterfaceName; |
| 1318 | sdbusplus::message::message m = |
| 1319 | crow::connections::systemBus->new_method_call( |
| 1320 | connectionName.c_str(), |
| 1321 | transaction->path.c_str(), |
| 1322 | thisInterfaceName, |
| 1323 | transaction->methodName.c_str()); |
| 1324 | |
| 1325 | tinyxml2::XMLElement *argumentNode = |
| 1326 | methodNode->FirstChildElement("arg"); |
| 1327 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1328 | std::string returnType; |
| 1329 | |
| 1330 | // Find the output type |
| 1331 | while (argumentNode != nullptr) |
| 1332 | { |
| 1333 | const char *argDirection = |
| 1334 | argumentNode->Attribute("direction"); |
| 1335 | const char *argType = |
| 1336 | argumentNode->Attribute("type"); |
| 1337 | if (argDirection != nullptr && |
| 1338 | argType != nullptr && |
| 1339 | std::string(argDirection) == "out") |
| 1340 | { |
| 1341 | returnType = argType; |
| 1342 | break; |
| 1343 | } |
| 1344 | argumentNode = |
| 1345 | argumentNode->NextSiblingElement("arg"); |
| 1346 | } |
| 1347 | |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1348 | nlohmann::json::const_iterator argIt = |
| 1349 | transaction->arguments.begin(); |
| 1350 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1351 | argumentNode = methodNode->FirstChildElement("arg"); |
| 1352 | |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1353 | while (argumentNode != nullptr) |
| 1354 | { |
| 1355 | const char *argDirection = |
| 1356 | argumentNode->Attribute("direction"); |
| 1357 | const char *argType = |
| 1358 | argumentNode->Attribute("type"); |
| 1359 | if (argDirection != nullptr && |
| 1360 | argType != nullptr && |
| 1361 | std::string(argDirection) == "in") |
| 1362 | { |
| 1363 | if (argIt == transaction->arguments.end()) |
| 1364 | { |
| 1365 | transaction->setErrorStatus( |
| 1366 | "Invalid method args"); |
| 1367 | return; |
| 1368 | } |
| 1369 | if (convertJsonToDbus(m.get(), |
| 1370 | std::string(argType), |
| 1371 | *argIt) < 0) |
| 1372 | { |
| 1373 | transaction->setErrorStatus( |
| 1374 | "Invalid method arg type"); |
| 1375 | return; |
| 1376 | } |
| 1377 | |
| 1378 | argIt++; |
| 1379 | } |
| 1380 | argumentNode = |
| 1381 | argumentNode->NextSiblingElement("arg"); |
| 1382 | } |
| 1383 | |
| 1384 | crow::connections::systemBus->async_send( |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1385 | m, [transaction, returnType]( |
| 1386 | boost::system::error_code ec, |
| 1387 | sdbusplus::message::message &m) { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1388 | if (ec) |
| 1389 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1390 | transaction->methodFailed = true; |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1393 | else |
| 1394 | { |
| 1395 | transaction->methodPassed = true; |
| 1396 | } |
| 1397 | |
| 1398 | handleMethodResponse(transaction, m, |
| 1399 | returnType); |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1400 | }); |
| 1401 | break; |
| 1402 | } |
| 1403 | methodNode = methodNode->NextSiblingElement("method"); |
| 1404 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1405 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1406 | interfaceNode = interfaceNode->NextSiblingElement("interface"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1407 | } |
| 1408 | }, |
| 1409 | connectionName, transaction->path, |
| 1410 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1413 | void handleAction(const crow::Request &req, crow::Response &res, |
| 1414 | const std::string &objectPath, const std::string &methodName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1415 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1416 | BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method " |
| 1417 | << methodName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1418 | nlohmann::json requestDbusData = |
| 1419 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1420 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1421 | if (requestDbusData.is_discarded()) |
| 1422 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1423 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1424 | noJsonDesc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1425 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1426 | return; |
| 1427 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1428 | nlohmann::json::iterator data = requestDbusData.find("data"); |
| 1429 | if (data == requestDbusData.end()) |
| 1430 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1431 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1432 | noJsonDesc, badReqMsg); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1433 | res.end(); |
| 1434 | return; |
| 1435 | } |
| 1436 | |
| 1437 | if (!data->is_array()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1438 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1439 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1440 | noJsonDesc, badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1441 | res.end(); |
| 1442 | return; |
| 1443 | } |
| 1444 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 1445 | |
| 1446 | transaction->path = objectPath; |
| 1447 | transaction->methodName = methodName; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1448 | transaction->arguments = std::move(*data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1449 | crow::connections::systemBus->async_method_call( |
| 1450 | [transaction]( |
| 1451 | const boost::system::error_code ec, |
| 1452 | const std::vector<std::pair<std::string, std::vector<std::string>>> |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1453 | &interfaceNames) { |
| 1454 | if (ec || interfaceNames.size() <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1455 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1456 | BMCWEB_LOG_ERROR << "Can't find object"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1457 | setErrorResponse(transaction->res, |
| 1458 | boost::beast::http::status::not_found, |
| 1459 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1463 | BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size() |
| 1464 | << " object(s)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1465 | |
| 1466 | for (const std::pair<std::string, std::vector<std::string>> |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1467 | &object : interfaceNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1468 | { |
| 1469 | findActionOnInterface(transaction, object.first); |
| 1470 | } |
| 1471 | }, |
| 1472 | "xyz.openbmc_project.ObjectMapper", |
| 1473 | "/xyz/openbmc_project/object_mapper", |
| 1474 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
| 1475 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1478 | void handleDelete(const crow::Request &req, crow::Response &res, |
| 1479 | const std::string &objectPath) |
| 1480 | { |
| 1481 | BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath; |
| 1482 | |
| 1483 | crow::connections::systemBus->async_method_call( |
| 1484 | [&res, objectPath]( |
| 1485 | const boost::system::error_code ec, |
| 1486 | const std::vector<std::pair<std::string, std::vector<std::string>>> |
| 1487 | &interfaceNames) { |
| 1488 | if (ec || interfaceNames.size() <= 0) |
| 1489 | { |
| 1490 | BMCWEB_LOG_ERROR << "Can't find object"; |
Matt Spinler | 62d2e8b | 2019-01-22 13:45:51 -0600 | [diff] [blame] | 1491 | setErrorResponse(res, |
| 1492 | boost::beast::http::status::method_not_allowed, |
| 1493 | methodNotAllowedDesc, methodNotAllowedMsg); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1494 | res.end(); |
| 1495 | return; |
| 1496 | } |
| 1497 | |
| 1498 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 1499 | transaction->path = objectPath; |
| 1500 | transaction->methodName = "Delete"; |
| 1501 | transaction->interfaceName = "xyz.openbmc_project.Object.Delete"; |
| 1502 | |
| 1503 | for (const std::pair<std::string, std::vector<std::string>> |
| 1504 | &object : interfaceNames) |
| 1505 | { |
| 1506 | findActionOnInterface(transaction, object.first); |
| 1507 | } |
| 1508 | }, |
| 1509 | "xyz.openbmc_project.ObjectMapper", |
| 1510 | "/xyz/openbmc_project/object_mapper", |
| 1511 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
| 1512 | std::array<const char *, 0>()); |
| 1513 | } |
| 1514 | |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1515 | void handleList(crow::Response &res, const std::string &objectPath, |
| 1516 | int32_t depth = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1517 | { |
| 1518 | crow::connections::systemBus->async_method_call( |
| 1519 | [&res](const boost::system::error_code ec, |
| 1520 | std::vector<std::string> &objectPaths) { |
| 1521 | if (ec) |
| 1522 | { |
Matt Spinler | d6091dd | 2018-12-06 14:08:27 -0600 | [diff] [blame] | 1523 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1524 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1525 | } |
| 1526 | else |
| 1527 | { |
| 1528 | res.jsonValue = {{"status", "ok"}, |
| 1529 | {"message", "200 OK"}, |
| 1530 | {"data", std::move(objectPaths)}}; |
| 1531 | } |
| 1532 | res.end(); |
| 1533 | }, |
| 1534 | "xyz.openbmc_project.ObjectMapper", |
| 1535 | "/xyz/openbmc_project/object_mapper", |
| 1536 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath, |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1537 | depth, std::array<std::string, 0>()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1538 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1539 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1540 | void handleEnumerate(crow::Response &res, const std::string &objectPath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1541 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1542 | BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath; |
| 1543 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res); |
| 1544 | |
| 1545 | asyncResp->res.jsonValue = {{"message", "200 OK"}, |
| 1546 | {"status", "ok"}, |
| 1547 | {"data", nlohmann::json::object()}}; |
| 1548 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1549 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1550 | [objectPath, asyncResp](const boost::system::error_code ec, |
| 1551 | GetSubTreeType &object_names) { |
| 1552 | auto transaction = std::make_shared<InProgressEnumerateData>( |
| 1553 | objectPath, asyncResp); |
| 1554 | |
| 1555 | transaction->subtree = |
| 1556 | std::make_shared<GetSubTreeType>(std::move(object_names)); |
| 1557 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1558 | if (ec) |
| 1559 | { |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 1560 | BMCWEB_LOG_ERROR << "GetSubTree failed on " |
| 1561 | << transaction->objectPath; |
| 1562 | setErrorResponse(transaction->asyncResp->res, |
| 1563 | boost::beast::http::status::not_found, |
| 1564 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1565 | return; |
| 1566 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1567 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1568 | // Add the data for the path passed in to the results |
| 1569 | // as if GetSubTree returned it, and continue on enumerating |
| 1570 | getObjectAndEnumerate(transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1571 | }, |
| 1572 | "xyz.openbmc_project.ObjectMapper", |
| 1573 | "/xyz/openbmc_project/object_mapper", |
| 1574 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1575 | static_cast<int32_t>(0), std::array<const char *, 0>()); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1576 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1577 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1578 | void handleGet(crow::Response &res, std::string &objectPath, |
| 1579 | std::string &destProperty) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1580 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1581 | BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty; |
| 1582 | std::shared_ptr<std::string> propertyName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1583 | std::make_shared<std::string>(std::move(destProperty)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1584 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1585 | std::shared_ptr<std::string> path = |
| 1586 | std::make_shared<std::string>(std::move(objectPath)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1587 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1588 | using GetObjectType = |
| 1589 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 1590 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1591 | [&res, path, propertyName](const boost::system::error_code ec, |
| 1592 | const GetObjectType &object_names) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1593 | if (ec || object_names.size() <= 0) |
| 1594 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1595 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1596 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1597 | res.end(); |
| 1598 | return; |
| 1599 | } |
| 1600 | std::shared_ptr<nlohmann::json> response = |
| 1601 | std::make_shared<nlohmann::json>(nlohmann::json::object()); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 1602 | // The mapper should never give us an empty interface names |
| 1603 | // list, but check anyway |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1604 | for (const std::pair<std::string, std::vector<std::string>> |
| 1605 | connection : object_names) |
| 1606 | { |
| 1607 | const std::vector<std::string> &interfaceNames = |
| 1608 | connection.second; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1609 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1610 | if (interfaceNames.size() <= 0) |
| 1611 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1612 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1613 | notFoundDesc, notFoundMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1614 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1615 | return; |
| 1616 | } |
| 1617 | |
| 1618 | for (const std::string &interface : interfaceNames) |
| 1619 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1620 | sdbusplus::message::message m = |
| 1621 | crow::connections::systemBus->new_method_call( |
| 1622 | connection.first.c_str(), path->c_str(), |
| 1623 | "org.freedesktop.DBus.Properties", "GetAll"); |
| 1624 | m.append(interface); |
| 1625 | crow::connections::systemBus->async_send( |
| 1626 | m, [&res, response, |
| 1627 | propertyName](const boost::system::error_code ec, |
| 1628 | sdbusplus::message::message &msg) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1629 | if (ec) |
| 1630 | { |
| 1631 | BMCWEB_LOG_ERROR << "Bad dbus request error: " |
| 1632 | << ec; |
| 1633 | } |
| 1634 | else |
| 1635 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1636 | nlohmann::json properties; |
| 1637 | int r = |
| 1638 | convertDBusToJSON("a{sv}", msg, properties); |
| 1639 | if (r < 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1640 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1641 | BMCWEB_LOG_ERROR |
| 1642 | << "convertDBusToJSON failed"; |
| 1643 | } |
| 1644 | else |
| 1645 | { |
| 1646 | for (auto &prop : properties.items()) |
| 1647 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 1648 | // if property name is empty, or |
| 1649 | // matches our search query, add it |
| 1650 | // to the response json |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1651 | |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1652 | if (propertyName->empty()) |
| 1653 | { |
| 1654 | (*response)[prop.key()] = |
| 1655 | std::move(prop.value()); |
| 1656 | } |
| 1657 | else if (prop.key() == *propertyName) |
| 1658 | { |
| 1659 | *response = std::move(prop.value()); |
| 1660 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | if (response.use_count() == 1) |
| 1665 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1666 | if (!propertyName->empty() && response->empty()) |
| 1667 | { |
| 1668 | setErrorResponse( |
| 1669 | res, |
| 1670 | boost::beast::http::status::not_found, |
| 1671 | propNotFoundDesc, notFoundMsg); |
| 1672 | } |
| 1673 | else |
| 1674 | { |
| 1675 | res.jsonValue = {{"status", "ok"}, |
| 1676 | {"message", "200 OK"}, |
| 1677 | {"data", *response}}; |
| 1678 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1679 | res.end(); |
| 1680 | } |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1681 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | }, |
| 1685 | "xyz.openbmc_project.ObjectMapper", |
| 1686 | "/xyz/openbmc_project/object_mapper", |
| 1687 | "xyz.openbmc_project.ObjectMapper", "GetObject", *path, |
| 1688 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1691 | struct AsyncPutRequest |
| 1692 | { |
| 1693 | AsyncPutRequest(crow::Response &res) : res(res) |
| 1694 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1695 | } |
| 1696 | ~AsyncPutRequest() |
| 1697 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1698 | if (res.jsonValue.empty()) |
| 1699 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1700 | setErrorResponse(res, boost::beast::http::status::forbidden, |
| 1701 | forbiddenMsg, forbiddenPropDesc); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | res.end(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1707 | void setErrorStatus(const std::string &desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1708 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1709 | setErrorResponse(res, boost::beast::http::status::internal_server_error, |
| 1710 | desc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1713 | crow::Response &res; |
| 1714 | std::string objectPath; |
| 1715 | std::string propertyName; |
| 1716 | nlohmann::json propertyValue; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1717 | }; |
| 1718 | |
Ed Tanous | d76323e | 2018-08-07 14:35:40 -0700 | [diff] [blame] | 1719 | void handlePut(const crow::Request &req, crow::Response &res, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1720 | const std::string &objectPath, const std::string &destProperty) |
| 1721 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1722 | if (destProperty.empty()) |
| 1723 | { |
| 1724 | setErrorResponse(res, boost::beast::http::status::forbidden, |
| 1725 | forbiddenResDesc, forbiddenMsg); |
| 1726 | res.end(); |
| 1727 | return; |
| 1728 | } |
| 1729 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1730 | nlohmann::json requestDbusData = |
| 1731 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1732 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1733 | if (requestDbusData.is_discarded()) |
| 1734 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1735 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1736 | noJsonDesc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1737 | res.end(); |
| 1738 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1739 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1740 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1741 | nlohmann::json::const_iterator propertyIt = requestDbusData.find("data"); |
| 1742 | if (propertyIt == requestDbusData.end()) |
| 1743 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1744 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1745 | noJsonDesc, badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1746 | res.end(); |
| 1747 | return; |
| 1748 | } |
| 1749 | const nlohmann::json &propertySetValue = *propertyIt; |
| 1750 | auto transaction = std::make_shared<AsyncPutRequest>(res); |
| 1751 | transaction->objectPath = objectPath; |
| 1752 | transaction->propertyName = destProperty; |
| 1753 | transaction->propertyValue = propertySetValue; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1754 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1755 | using GetObjectType = |
| 1756 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1757 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1758 | crow::connections::systemBus->async_method_call( |
| 1759 | [transaction](const boost::system::error_code ec, |
| 1760 | const GetObjectType &object_names) { |
| 1761 | if (!ec && object_names.size() <= 0) |
| 1762 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1763 | setErrorResponse(transaction->res, |
| 1764 | boost::beast::http::status::not_found, |
| 1765 | propNotFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1766 | return; |
| 1767 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1768 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1769 | for (const std::pair<std::string, std::vector<std::string>> |
| 1770 | connection : object_names) |
| 1771 | { |
| 1772 | const std::string &connectionName = connection.first; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1773 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1774 | crow::connections::systemBus->async_method_call( |
| 1775 | [connectionName{std::string(connectionName)}, |
| 1776 | transaction](const boost::system::error_code ec, |
| 1777 | const std::string &introspectXml) { |
| 1778 | if (ec) |
| 1779 | { |
| 1780 | BMCWEB_LOG_ERROR |
| 1781 | << "Introspect call failed with error: " |
| 1782 | << ec.message() |
| 1783 | << " on process: " << connectionName; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1784 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1785 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1786 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1787 | tinyxml2::XMLDocument doc; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1788 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1789 | doc.Parse(introspectXml.c_str()); |
| 1790 | tinyxml2::XMLNode *pRoot = |
| 1791 | doc.FirstChildElement("node"); |
| 1792 | if (pRoot == nullptr) |
| 1793 | { |
| 1794 | BMCWEB_LOG_ERROR << "XML document failed to parse: " |
| 1795 | << introspectXml; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1796 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1797 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1798 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1799 | tinyxml2::XMLElement *ifaceNode = |
| 1800 | pRoot->FirstChildElement("interface"); |
| 1801 | while (ifaceNode != nullptr) |
| 1802 | { |
| 1803 | const char *interfaceName = |
| 1804 | ifaceNode->Attribute("name"); |
| 1805 | BMCWEB_LOG_DEBUG << "found interface " |
| 1806 | << interfaceName; |
| 1807 | tinyxml2::XMLElement *propNode = |
| 1808 | ifaceNode->FirstChildElement("property"); |
| 1809 | while (propNode != nullptr) |
| 1810 | { |
| 1811 | const char *propertyName = |
| 1812 | propNode->Attribute("name"); |
| 1813 | BMCWEB_LOG_DEBUG << "Found property " |
| 1814 | << propertyName; |
| 1815 | if (propertyName == transaction->propertyName) |
| 1816 | { |
| 1817 | const char *argType = |
| 1818 | propNode->Attribute("type"); |
| 1819 | if (argType != nullptr) |
| 1820 | { |
| 1821 | sdbusplus::message::message m = |
| 1822 | crow::connections::systemBus |
| 1823 | ->new_method_call( |
| 1824 | connectionName.c_str(), |
| 1825 | transaction->objectPath |
| 1826 | .c_str(), |
| 1827 | "org.freedesktop.DBus." |
| 1828 | "Properties", |
| 1829 | "Set"); |
| 1830 | m.append(interfaceName, |
| 1831 | transaction->propertyName); |
| 1832 | int r = sd_bus_message_open_container( |
| 1833 | m.get(), SD_BUS_TYPE_VARIANT, |
| 1834 | argType); |
| 1835 | if (r < 0) |
| 1836 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1837 | transaction->setErrorStatus( |
| 1838 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1839 | return; |
| 1840 | } |
| 1841 | r = convertJsonToDbus( |
| 1842 | m.get(), argType, |
| 1843 | transaction->propertyValue); |
| 1844 | if (r < 0) |
| 1845 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1846 | transaction->setErrorStatus( |
| 1847 | "Invalid arg type"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1848 | return; |
| 1849 | } |
| 1850 | r = sd_bus_message_close_container( |
| 1851 | m.get()); |
| 1852 | if (r < 0) |
| 1853 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1854 | transaction->setErrorStatus( |
| 1855 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1856 | return; |
| 1857 | } |
Ed Tanous | 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 | { |
Lei YU | 97d2a47 | 2019-06-11 17:44:27 +0800 | [diff] [blame] | 1869 | const sd_bus_error *e = |
| 1870 | m.get_error(); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1871 | setErrorResponse( |
| 1872 | transaction->res, |
| 1873 | boost::beast::http:: |
| 1874 | status:: |
| 1875 | forbidden, |
Lei YU | 97d2a47 | 2019-06-11 17:44:27 +0800 | [diff] [blame] | 1876 | e->name, |
| 1877 | e->message); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1878 | } |
| 1879 | else |
| 1880 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1881 | transaction->res |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1882 | .jsonValue = { |
| 1883 | {"status", "ok"}, |
| 1884 | {"message", |
| 1885 | "200 OK"}, |
| 1886 | {"data", nullptr}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1887 | } |
| 1888 | }); |
| 1889 | } |
| 1890 | } |
| 1891 | propNode = |
| 1892 | propNode->NextSiblingElement("property"); |
| 1893 | } |
| 1894 | ifaceNode = |
| 1895 | ifaceNode->NextSiblingElement("interface"); |
| 1896 | } |
| 1897 | }, |
| 1898 | connectionName, transaction->objectPath, |
| 1899 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 1900 | } |
| 1901 | }, |
| 1902 | "xyz.openbmc_project.ObjectMapper", |
| 1903 | "/xyz/openbmc_project/object_mapper", |
| 1904 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 1905 | transaction->objectPath, std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1906 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1907 | |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1908 | inline void handleDBusUrl(const crow::Request &req, crow::Response &res, |
| 1909 | std::string &objectPath) |
| 1910 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1911 | |
| 1912 | // If accessing a single attribute, fill in and update objectPath, |
| 1913 | // otherwise leave destProperty blank |
| 1914 | std::string destProperty = ""; |
| 1915 | const char *attrSeperator = "/attr/"; |
| 1916 | size_t attrPosition = objectPath.find(attrSeperator); |
| 1917 | if (attrPosition != objectPath.npos) |
| 1918 | { |
| 1919 | destProperty = objectPath.substr(attrPosition + strlen(attrSeperator), |
| 1920 | objectPath.length()); |
| 1921 | objectPath = objectPath.substr(0, attrPosition); |
| 1922 | } |
| 1923 | |
| 1924 | if (req.method() == "POST"_method) |
| 1925 | { |
| 1926 | constexpr const char *actionSeperator = "/action/"; |
| 1927 | size_t actionPosition = objectPath.find(actionSeperator); |
| 1928 | if (actionPosition != objectPath.npos) |
| 1929 | { |
| 1930 | std::string postProperty = |
| 1931 | objectPath.substr((actionPosition + strlen(actionSeperator)), |
| 1932 | objectPath.length()); |
| 1933 | objectPath = objectPath.substr(0, actionPosition); |
| 1934 | handleAction(req, res, objectPath, postProperty); |
| 1935 | return; |
| 1936 | } |
| 1937 | } |
| 1938 | else if (req.method() == "GET"_method) |
| 1939 | { |
| 1940 | if (boost::ends_with(objectPath, "/enumerate")) |
| 1941 | { |
| 1942 | objectPath.erase(objectPath.end() - sizeof("enumerate"), |
| 1943 | objectPath.end()); |
| 1944 | handleEnumerate(res, objectPath); |
| 1945 | } |
| 1946 | else if (boost::ends_with(objectPath, "/list")) |
| 1947 | { |
| 1948 | objectPath.erase(objectPath.end() - sizeof("list"), |
| 1949 | objectPath.end()); |
| 1950 | handleList(res, objectPath); |
| 1951 | } |
| 1952 | else |
| 1953 | { |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1954 | // Trim any trailing "/" at the end |
| 1955 | if (boost::ends_with(objectPath, "/")) |
| 1956 | { |
| 1957 | objectPath.pop_back(); |
| 1958 | handleList(res, objectPath, 1); |
| 1959 | } |
| 1960 | else |
| 1961 | { |
| 1962 | handleGet(res, objectPath, destProperty); |
| 1963 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1964 | } |
| 1965 | return; |
| 1966 | } |
| 1967 | else if (req.method() == "PUT"_method) |
| 1968 | { |
| 1969 | handlePut(req, res, objectPath, destProperty); |
| 1970 | return; |
| 1971 | } |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1972 | else if (req.method() == "DELETE"_method) |
| 1973 | { |
| 1974 | handleDelete(req, res, objectPath); |
| 1975 | return; |
| 1976 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1977 | |
Matt Spinler | c4e8d21d6 | 2018-12-11 11:47:17 -0600 | [diff] [blame] | 1978 | setErrorResponse(res, boost::beast::http::status::method_not_allowed, |
| 1979 | methodNotAllowedDesc, methodNotAllowedMsg); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1980 | res.end(); |
| 1981 | } |
| 1982 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1983 | template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app) |
| 1984 | { |
| 1985 | BMCWEB_ROUTE(app, "/bus/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1986 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1987 | .methods("GET"_method)( |
| 1988 | [](const crow::Request &req, crow::Response &res) { |
| 1989 | res.jsonValue = {{"busses", {{{"name", "system"}}}}, |
| 1990 | {"status", "ok"}}; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1991 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1992 | }); |
| 1993 | |
| 1994 | BMCWEB_ROUTE(app, "/bus/system/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1995 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1996 | .methods("GET"_method)( |
| 1997 | [](const crow::Request &req, crow::Response &res) { |
| 1998 | auto myCallback = [&res](const boost::system::error_code ec, |
| 1999 | std::vector<std::string> &names) { |
| 2000 | if (ec) |
| 2001 | { |
| 2002 | BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec; |
| 2003 | res.result( |
| 2004 | boost::beast::http::status::internal_server_error); |
| 2005 | } |
| 2006 | else |
| 2007 | { |
| 2008 | std::sort(names.begin(), names.end()); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2009 | res.jsonValue = {{"status", "ok"}}; |
| 2010 | auto &objectsSub = res.jsonValue["objects"]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2011 | for (auto &name : names) |
| 2012 | { |
| 2013 | objectsSub.push_back({{"name", name}}); |
| 2014 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2015 | } |
| 2016 | res.end(); |
| 2017 | }; |
| 2018 | crow::connections::systemBus->async_method_call( |
| 2019 | std::move(myCallback), "org.freedesktop.DBus", "/", |
| 2020 | "org.freedesktop.DBus", "ListNames"); |
| 2021 | }); |
| 2022 | |
| 2023 | BMCWEB_ROUTE(app, "/list/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2024 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2025 | .methods("GET"_method)( |
| 2026 | [](const crow::Request &req, crow::Response &res) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2027 | handleList(res, "/"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2028 | }); |
| 2029 | |
| 2030 | BMCWEB_ROUTE(app, "/xyz/<path>") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2031 | .requires({"Login"}) |
| 2032 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
| 2033 | const std::string &path) { |
| 2034 | std::string objectPath = "/xyz/" + path; |
| 2035 | handleDBusUrl(req, res, objectPath); |
| 2036 | }); |
| 2037 | |
| 2038 | BMCWEB_ROUTE(app, "/xyz/<path>") |
| 2039 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 2040 | .methods("PUT"_method, "POST"_method, "DELETE"_method)( |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2041 | [](const crow::Request &req, crow::Response &res, |
| 2042 | const std::string &path) { |
| 2043 | std::string objectPath = "/xyz/" + path; |
| 2044 | handleDBusUrl(req, res, objectPath); |
| 2045 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2046 | |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2047 | BMCWEB_ROUTE(app, "/org/<path>") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2048 | .requires({"Login"}) |
| 2049 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
| 2050 | const std::string &path) { |
Ed Tanous | e128140 | 2019-04-03 07:07:10 -0700 | [diff] [blame] | 2051 | std::string objectPath = "/org/" + path; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2052 | handleDBusUrl(req, res, objectPath); |
| 2053 | }); |
| 2054 | |
| 2055 | BMCWEB_ROUTE(app, "/org/<path>") |
| 2056 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 2057 | .methods("PUT"_method, "POST"_method, "DELETE"_method)( |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2058 | [](const crow::Request &req, crow::Response &res, |
| 2059 | const std::string &path) { |
Ed Tanous | e128140 | 2019-04-03 07:07:10 -0700 | [diff] [blame] | 2060 | std::string objectPath = "/org/" + path; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2061 | handleDBusUrl(req, res, objectPath); |
| 2062 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2063 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2064 | BMCWEB_ROUTE(app, "/download/dump/<str>/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2065 | .requires({"ConfigureManager"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2066 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
| 2067 | const std::string &dumpId) { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2068 | std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2069 | if (!std::regex_match(dumpId, validFilename)) |
| 2070 | { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2071 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2072 | res.end(); |
| 2073 | return; |
| 2074 | } |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 2075 | std::filesystem::path loc( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2076 | "/var/lib/phosphor-debug-collector/dumps"); |
| 2077 | |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2078 | loc /= dumpId; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2079 | |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 2080 | if (!std::filesystem::exists(loc) || |
| 2081 | !std::filesystem::is_directory(loc)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2082 | { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2083 | BMCWEB_LOG_ERROR << loc << "Not found"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2084 | res.result(boost::beast::http::status::not_found); |
| 2085 | res.end(); |
| 2086 | return; |
| 2087 | } |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 2088 | std::filesystem::directory_iterator files(loc); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2089 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2090 | for (auto &file : files) |
| 2091 | { |
| 2092 | std::ifstream readFile(file.path()); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2093 | if (!readFile.good()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2094 | { |
| 2095 | continue; |
| 2096 | } |
| 2097 | res.addHeader("Content-Type", "application/octet-stream"); |
| 2098 | res.body() = {std::istreambuf_iterator<char>(readFile), |
| 2099 | std::istreambuf_iterator<char>()}; |
| 2100 | res.end(); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2101 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2102 | } |
| 2103 | res.result(boost::beast::http::status::not_found); |
| 2104 | res.end(); |
| 2105 | return; |
| 2106 | }); |
| 2107 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2108 | BMCWEB_ROUTE(app, "/bus/system/<str>/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2109 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2110 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2111 | const std::string &Connection) { |
| 2112 | introspectObjects(Connection, "/", |
| 2113 | std::make_shared<bmcweb::AsyncResp>(res)); |
| 2114 | }); |
| 2115 | |
| 2116 | BMCWEB_ROUTE(app, "/bus/system/<str>/<path>") |
| 2117 | .methods("GET"_method, |
| 2118 | "POST"_method)([](const crow::Request &req, |
| 2119 | crow::Response &res, |
| 2120 | const std::string &processName, |
| 2121 | const std::string &requestedPath) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2122 | std::vector<std::string> strs; |
| 2123 | boost::split(strs, requestedPath, boost::is_any_of("/")); |
| 2124 | std::string objectPath; |
| 2125 | std::string interfaceName; |
| 2126 | std::string methodName; |
| 2127 | auto it = strs.begin(); |
| 2128 | if (it == strs.end()) |
| 2129 | { |
| 2130 | objectPath = "/"; |
| 2131 | } |
| 2132 | while (it != strs.end()) |
| 2133 | { |
| 2134 | // Check if segment contains ".". If it does, it must be an |
| 2135 | // interface |
| 2136 | if (it->find(".") != std::string::npos) |
| 2137 | { |
| 2138 | break; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2139 | // This check is neccesary as the trailing slash gets |
| 2140 | // parsed as part of our <path> specifier above, which |
| 2141 | // causes the normal trailing backslash redirector to |
| 2142 | // fail. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2143 | } |
| 2144 | else if (!it->empty()) |
| 2145 | { |
| 2146 | objectPath += "/" + *it; |
| 2147 | } |
| 2148 | it++; |
| 2149 | } |
| 2150 | if (it != strs.end()) |
| 2151 | { |
| 2152 | interfaceName = *it; |
| 2153 | it++; |
| 2154 | |
| 2155 | // after interface, we might have a method name |
| 2156 | if (it != strs.end()) |
| 2157 | { |
| 2158 | methodName = *it; |
| 2159 | it++; |
| 2160 | } |
| 2161 | } |
| 2162 | if (it != strs.end()) |
| 2163 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2164 | // if there is more levels past the method name, something |
| 2165 | // went wrong, return not found |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2166 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2167 | return; |
| 2168 | } |
| 2169 | if (interfaceName.empty()) |
| 2170 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2171 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 2172 | std::make_shared<bmcweb::AsyncResp>(res); |
| 2173 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2174 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2175 | [asyncResp, processName, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2176 | objectPath](const boost::system::error_code ec, |
| 2177 | const std::string &introspect_xml) { |
| 2178 | if (ec) |
| 2179 | { |
| 2180 | BMCWEB_LOG_ERROR |
| 2181 | << "Introspect call failed with error: " |
| 2182 | << ec.message() |
| 2183 | << " on process: " << processName |
| 2184 | << " path: " << objectPath << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2185 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2186 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2187 | tinyxml2::XMLDocument doc; |
| 2188 | |
| 2189 | doc.Parse(introspect_xml.c_str()); |
| 2190 | tinyxml2::XMLNode *pRoot = |
| 2191 | doc.FirstChildElement("node"); |
| 2192 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2193 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2194 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 2195 | << processName << " " << objectPath |
| 2196 | << "\n"; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2197 | asyncResp->res.jsonValue = { |
| 2198 | {"status", "XML parse error"}}; |
| 2199 | asyncResp->res.result(boost::beast::http::status:: |
| 2200 | internal_server_error); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2201 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2202 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2203 | |
| 2204 | BMCWEB_LOG_DEBUG << introspect_xml; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2205 | asyncResp->res.jsonValue = { |
| 2206 | {"status", "ok"}, |
| 2207 | {"bus_name", processName}, |
| 2208 | {"object_path", objectPath}}; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2209 | nlohmann::json &interfacesArray = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2210 | asyncResp->res.jsonValue["interfaces"]; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2211 | interfacesArray = nlohmann::json::array(); |
| 2212 | tinyxml2::XMLElement *interface = |
| 2213 | pRoot->FirstChildElement("interface"); |
| 2214 | |
| 2215 | while (interface != nullptr) |
| 2216 | { |
| 2217 | const char *ifaceName = |
| 2218 | interface->Attribute("name"); |
| 2219 | if (ifaceName != nullptr) |
| 2220 | { |
| 2221 | interfacesArray.push_back( |
| 2222 | {{"name", ifaceName}}); |
| 2223 | } |
| 2224 | |
| 2225 | interface = |
| 2226 | interface->NextSiblingElement("interface"); |
| 2227 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2228 | }, |
| 2229 | processName, objectPath, |
| 2230 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 2231 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2232 | else if (methodName.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2233 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2234 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 2235 | std::make_shared<bmcweb::AsyncResp>(res); |
| 2236 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2237 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2238 | [asyncResp, processName, objectPath, |
| 2239 | interfaceName](const boost::system::error_code ec, |
| 2240 | const std::string &introspect_xml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2241 | if (ec) |
| 2242 | { |
| 2243 | BMCWEB_LOG_ERROR |
| 2244 | << "Introspect call failed with error: " |
| 2245 | << ec.message() |
| 2246 | << " on process: " << processName |
| 2247 | << " path: " << objectPath << "\n"; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2248 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2249 | } |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2250 | tinyxml2::XMLDocument doc; |
| 2251 | |
| 2252 | doc.Parse(introspect_xml.data(), introspect_xml.size()); |
| 2253 | tinyxml2::XMLNode *pRoot = |
| 2254 | doc.FirstChildElement("node"); |
| 2255 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2256 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2257 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 2258 | << processName << " " << objectPath |
| 2259 | << "\n"; |
| 2260 | asyncResp->res.result(boost::beast::http::status:: |
| 2261 | internal_server_error); |
| 2262 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2263 | } |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2264 | asyncResp->res.jsonValue = { |
| 2265 | {"status", "ok"}, |
| 2266 | {"bus_name", processName}, |
| 2267 | {"interface", interfaceName}, |
| 2268 | {"object_path", objectPath}}; |
| 2269 | |
| 2270 | nlohmann::json &methodsArray = |
| 2271 | asyncResp->res.jsonValue["methods"]; |
| 2272 | methodsArray = nlohmann::json::array(); |
| 2273 | |
| 2274 | nlohmann::json &signalsArray = |
| 2275 | asyncResp->res.jsonValue["signals"]; |
| 2276 | signalsArray = nlohmann::json::array(); |
| 2277 | |
| 2278 | nlohmann::json &propertiesObj = |
| 2279 | asyncResp->res.jsonValue["properties"]; |
| 2280 | propertiesObj = nlohmann::json::object(); |
| 2281 | |
| 2282 | // if we know we're the only call, build the |
| 2283 | // json directly |
| 2284 | tinyxml2::XMLElement *interface = |
| 2285 | pRoot->FirstChildElement("interface"); |
| 2286 | while (interface != nullptr) |
| 2287 | { |
| 2288 | const char *ifaceName = |
| 2289 | interface->Attribute("name"); |
| 2290 | |
| 2291 | if (ifaceName != nullptr && |
| 2292 | ifaceName == interfaceName) |
| 2293 | { |
| 2294 | break; |
| 2295 | } |
| 2296 | |
| 2297 | interface = |
| 2298 | interface->NextSiblingElement("interface"); |
| 2299 | } |
| 2300 | if (interface == nullptr) |
| 2301 | { |
| 2302 | // if we got to the end of the list and |
| 2303 | // never found a match, throw 404 |
| 2304 | asyncResp->res.result( |
| 2305 | boost::beast::http::status::not_found); |
| 2306 | return; |
| 2307 | } |
| 2308 | |
| 2309 | tinyxml2::XMLElement *methods = |
| 2310 | interface->FirstChildElement("method"); |
| 2311 | while (methods != nullptr) |
| 2312 | { |
| 2313 | nlohmann::json argsArray = nlohmann::json::array(); |
| 2314 | tinyxml2::XMLElement *arg = |
| 2315 | methods->FirstChildElement("arg"); |
| 2316 | while (arg != nullptr) |
| 2317 | { |
| 2318 | nlohmann::json thisArg; |
| 2319 | for (const char *fieldName : |
| 2320 | std::array<const char *, 3>{ |
| 2321 | "name", "direction", "type"}) |
| 2322 | { |
| 2323 | const char *fieldValue = |
| 2324 | arg->Attribute(fieldName); |
| 2325 | if (fieldValue != nullptr) |
| 2326 | { |
| 2327 | thisArg[fieldName] = fieldValue; |
| 2328 | } |
| 2329 | } |
| 2330 | argsArray.push_back(std::move(thisArg)); |
| 2331 | arg = arg->NextSiblingElement("arg"); |
| 2332 | } |
| 2333 | |
| 2334 | const char *name = methods->Attribute("name"); |
| 2335 | if (name != nullptr) |
| 2336 | { |
| 2337 | methodsArray.push_back( |
| 2338 | {{"name", name}, |
| 2339 | {"uri", "/bus/system/" + processName + |
| 2340 | objectPath + "/" + |
| 2341 | interfaceName + "/" + name}, |
| 2342 | {"args", argsArray}}); |
| 2343 | } |
| 2344 | methods = methods->NextSiblingElement("method"); |
| 2345 | } |
| 2346 | tinyxml2::XMLElement *signals = |
| 2347 | interface->FirstChildElement("signal"); |
| 2348 | while (signals != nullptr) |
| 2349 | { |
| 2350 | nlohmann::json argsArray = nlohmann::json::array(); |
| 2351 | |
| 2352 | tinyxml2::XMLElement *arg = |
| 2353 | signals->FirstChildElement("arg"); |
| 2354 | while (arg != nullptr) |
| 2355 | { |
| 2356 | const char *name = arg->Attribute("name"); |
| 2357 | const char *type = arg->Attribute("type"); |
| 2358 | if (name != nullptr && type != nullptr) |
| 2359 | { |
| 2360 | argsArray.push_back({ |
| 2361 | {"name", name}, |
| 2362 | {"type", type}, |
| 2363 | }); |
| 2364 | } |
| 2365 | arg = arg->NextSiblingElement("arg"); |
| 2366 | } |
| 2367 | const char *name = signals->Attribute("name"); |
| 2368 | if (name != nullptr) |
| 2369 | { |
| 2370 | signalsArray.push_back( |
| 2371 | {{"name", name}, {"args", argsArray}}); |
| 2372 | } |
| 2373 | |
| 2374 | signals = signals->NextSiblingElement("signal"); |
| 2375 | } |
| 2376 | |
| 2377 | tinyxml2::XMLElement *property = |
| 2378 | interface->FirstChildElement("property"); |
| 2379 | while (property != nullptr) |
| 2380 | { |
| 2381 | const char *name = property->Attribute("name"); |
| 2382 | const char *type = property->Attribute("type"); |
| 2383 | if (type != nullptr && name != nullptr) |
| 2384 | { |
| 2385 | sdbusplus::message::message m = |
| 2386 | crow::connections::systemBus |
| 2387 | ->new_method_call(processName.c_str(), |
| 2388 | objectPath.c_str(), |
| 2389 | "org.freedesktop." |
| 2390 | "DBus." |
| 2391 | "Properties", |
| 2392 | "Get"); |
| 2393 | m.append(interfaceName, name); |
| 2394 | nlohmann::json &propertyItem = |
| 2395 | propertiesObj[name]; |
| 2396 | crow::connections::systemBus->async_send( |
| 2397 | m, [&propertyItem, asyncResp]( |
| 2398 | boost::system::error_code &ec, |
| 2399 | sdbusplus::message::message &m) { |
| 2400 | if (ec) |
| 2401 | { |
| 2402 | return; |
| 2403 | } |
| 2404 | |
| 2405 | convertDBusToJSON("v", m, propertyItem); |
| 2406 | }); |
| 2407 | } |
| 2408 | property = property->NextSiblingElement("property"); |
| 2409 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2410 | }, |
| 2411 | processName, objectPath, |
| 2412 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 2413 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2414 | else |
| 2415 | { |
| 2416 | if (req.method() != "POST"_method) |
| 2417 | { |
| 2418 | res.result(boost::beast::http::status::not_found); |
| 2419 | res.end(); |
| 2420 | return; |
| 2421 | } |
| 2422 | |
| 2423 | nlohmann::json requestDbusData = |
| 2424 | nlohmann::json::parse(req.body, nullptr, false); |
| 2425 | |
| 2426 | if (requestDbusData.is_discarded()) |
| 2427 | { |
| 2428 | res.result(boost::beast::http::status::bad_request); |
| 2429 | res.end(); |
| 2430 | return; |
| 2431 | } |
| 2432 | if (!requestDbusData.is_array()) |
| 2433 | { |
| 2434 | res.result(boost::beast::http::status::bad_request); |
| 2435 | res.end(); |
| 2436 | return; |
| 2437 | } |
| 2438 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 2439 | |
| 2440 | transaction->path = objectPath; |
| 2441 | transaction->methodName = methodName; |
| 2442 | transaction->arguments = std::move(requestDbusData); |
| 2443 | |
| 2444 | findActionOnInterface(transaction, processName); |
| 2445 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2446 | }); |
| 2447 | } |
| 2448 | } // namespace openbmc_mapper |
| 2449 | } // namespace crow |