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