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