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