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