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