Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #include <crow/app.h> |
| 2 | |
| 3 | #include <tinyxml2.h> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 4 | #include <dbus_singleton.hpp> |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 5 | #include <experimental/filesystem> |
| 6 | #include <fstream> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 7 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 8 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 9 | |
| 10 | namespace crow { |
| 11 | namespace openbmc_mapper { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 12 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 13 | void introspectObjects(crow::Response &res, std::string process_name, |
| 14 | std::string path, |
| 15 | std::shared_ptr<nlohmann::json> transaction) { |
| 16 | crow::connections::systemBus->async_method_call( |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 17 | [ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 18 | &res, transaction, processName{std::move(process_name)}, |
| 19 | objectPath{std::move(path)} |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 20 | ](const boost::system::error_code ec, const std::string &introspect_xml) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 21 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 22 | BMCWEB_LOG_ERROR << "Introspect call failed with error: " |
| 23 | << ec.message() << " on process: " << processName |
| 24 | << " path: " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 25 | |
| 26 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 27 | transaction->push_back({{"path", objectPath}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 28 | |
| 29 | tinyxml2::XMLDocument doc; |
| 30 | |
| 31 | doc.Parse(introspect_xml.c_str()); |
| 32 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 33 | if (pRoot == nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 34 | BMCWEB_LOG_ERROR << "XML document failed to parse " << processName |
| 35 | << " " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 36 | |
| 37 | } else { |
| 38 | tinyxml2::XMLElement *node = pRoot->FirstChildElement("node"); |
| 39 | while (node != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 40 | std::string childPath = node->Attribute("name"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 41 | std::string newpath; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 42 | if (objectPath != "/") { |
| 43 | newpath += objectPath; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 44 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 45 | newpath += "/" + childPath; |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 46 | // introspect the subobjects as well |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 47 | introspectObjects(res, processName, newpath, transaction); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 48 | |
| 49 | node = node->NextSiblingElement("node"); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | // if we're the last outstanding caller, finish the request |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 54 | if (transaction.use_count() == 1) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 55 | res.jsonValue = {{"status", "ok"}, |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 56 | {"bus_name", processName}, |
| 57 | {"objects", std::move(*transaction)}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 58 | res.end(); |
| 59 | } |
| 60 | }, |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 61 | process_name, path, "org.freedesktop.DBus.Introspectable", "Introspect"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 62 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 63 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 64 | // A smattering of common types to unpack. TODO(ed) this should really iterate |
| 65 | // the sdbusplus object directly and build the json response |
| 66 | using DbusRestVariantType = sdbusplus::message::variant< |
| 67 | std::vector<std::tuple<std::string, std::string, std::string>>, std::string, |
| 68 | int64_t, uint64_t, double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, |
| 69 | bool>; |
| 70 | |
| 71 | using ManagedObjectType = std::vector<std::pair< |
| 72 | sdbusplus::message::object_path, |
| 73 | boost::container::flat_map< |
| 74 | std::string, |
| 75 | boost::container::flat_map<std::string, DbusRestVariantType>>>>; |
| 76 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 77 | void getManagedObjectsForEnumerate( |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 78 | const std::string &object_name, const std::string &connection_name, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 79 | crow::Response &res, std::shared_ptr<nlohmann::json> transaction) { |
| 80 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 81 | [&res, transaction](const boost::system::error_code ec, |
| 82 | const ManagedObjectType &objects) { |
| 83 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 84 | BMCWEB_LOG_ERROR << ec; |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 85 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 86 | nlohmann::json &dataJson = *transaction; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 87 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 88 | for (auto &objectPath : objects) { |
| 89 | BMCWEB_LOG_DEBUG |
| 90 | << "Reading object " |
| 91 | << static_cast<const std::string &>(objectPath.first); |
| 92 | nlohmann::json &objectJson = |
| 93 | dataJson[static_cast<const std::string &>(objectPath.first)]; |
| 94 | if (objectJson.is_null()) { |
| 95 | objectJson = nlohmann::json::object(); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 96 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 97 | for (const auto &interface : objectPath.second) { |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 98 | for (const auto &property : interface.second) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 99 | nlohmann::json &propertyJson = objectJson[property.first]; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 100 | mapbox::util::apply_visitor( |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 101 | [&propertyJson](auto &&val) { propertyJson = val; }, |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 102 | property.second); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 103 | |
| 104 | // dbus-rest represents booleans as 1 or 0, implement to match |
| 105 | // TODO(ed) see if dbus-rest should be changed |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 106 | const bool *propertyBool = propertyJson.get_ptr<const bool *>(); |
| 107 | if (propertyBool != nullptr) { |
| 108 | propertyJson = *propertyBool ? 1 : 0; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (transaction.use_count() == 1) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 116 | res.jsonValue = {{"message", "200 OK"}, |
| 117 | {"status", "ok"}, |
| 118 | {"data", std::move(*transaction)}}; |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 119 | res.end(); |
| 120 | } |
| 121 | }, |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 122 | connection_name, object_name, "org.freedesktop.DBus.ObjectManager", |
| 123 | "GetManagedObjects"); |
| 124 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 125 | |
| 126 | using GetSubTreeType = std::vector< |
| 127 | std::pair<std::string, |
| 128 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 129 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 130 | // Structure for storing data on an in progress action |
| 131 | struct InProgressActionData { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 132 | InProgressActionData(crow::Response &res) : res(res){}; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 133 | ~InProgressActionData() { |
| 134 | if (res.result() == boost::beast::http::status::internal_server_error) { |
| 135 | // Reset the json object to clear out any data that made it in before the |
| 136 | // error happened |
| 137 | // todo(ed) handle error condition with proper code |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 138 | res.jsonValue = nlohmann::json::object(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 139 | } |
| 140 | res.end(); |
| 141 | } |
| 142 | |
| 143 | void setErrorStatus() { |
| 144 | res.result(boost::beast::http::status::internal_server_error); |
| 145 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 146 | crow::Response &res; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 147 | std::string path; |
| 148 | std::string method_name; |
| 149 | nlohmann::json arguments; |
| 150 | }; |
| 151 | |
| 152 | std::vector<std::string> dbus_arg_split(const std::string &string) { |
| 153 | std::vector<std::string> ret; |
| 154 | if (string.empty()) { |
| 155 | return ret; |
| 156 | } |
| 157 | ret.push_back(""); |
| 158 | int container_depth = 0; |
| 159 | std::string::const_iterator character = string.begin(); |
| 160 | while (character != string.end()) { |
| 161 | switch (*character) { |
| 162 | case ('a'): |
| 163 | ret.back() += *character; |
| 164 | break; |
| 165 | case ('('): |
| 166 | case ('{'): |
| 167 | ret.back() += *character; |
| 168 | container_depth++; |
| 169 | break; |
| 170 | case ('}'): |
| 171 | case (')'): |
| 172 | ret.back() += *character; |
| 173 | container_depth--; |
| 174 | if (container_depth == 0) { |
| 175 | character++; |
| 176 | if (character != string.end()) { |
| 177 | ret.push_back(""); |
| 178 | } |
| 179 | continue; |
| 180 | } |
| 181 | break; |
| 182 | default: |
| 183 | ret.back() += *character; |
| 184 | if (container_depth == 0) { |
| 185 | character++; |
| 186 | if (character != string.end()) { |
| 187 | ret.push_back(""); |
| 188 | } |
| 189 | continue; |
| 190 | } |
| 191 | break; |
| 192 | } |
| 193 | character++; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int convert_json_to_dbus(sd_bus_message *m, const std::string &arg_type, |
| 198 | const nlohmann::json &input_json) { |
| 199 | int r = 0; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 200 | BMCWEB_LOG_DEBUG << "Converting " << input_json.dump() |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 201 | << " to type: " << arg_type; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 202 | const std::vector<std::string> arg_types = dbus_arg_split(arg_type); |
| 203 | |
| 204 | // Assume a single object for now. |
| 205 | const nlohmann::json *j = &input_json; |
| 206 | nlohmann::json::const_iterator j_it = input_json.begin(); |
| 207 | |
| 208 | for (const std::string &arg_code : arg_types) { |
| 209 | // If we are decoding multiple objects, grab the pointer to the iterator, |
| 210 | // and increment it for the next loop |
| 211 | if (arg_types.size() > 1) { |
| 212 | if (j_it == input_json.end()) { |
| 213 | return -2; |
| 214 | } |
| 215 | j = &*j_it; |
| 216 | j_it++; |
| 217 | } |
| 218 | const int64_t *int_value = j->get_ptr<const int64_t *>(); |
| 219 | const uint64_t *uint_value = j->get_ptr<const uint64_t *>(); |
| 220 | const std::string *string_value = j->get_ptr<const std::string *>(); |
| 221 | const double *double_value = j->get_ptr<const double *>(); |
| 222 | const bool *b = j->get_ptr<const bool *>(); |
| 223 | int64_t v = 0; |
| 224 | double d = 0.0; |
| 225 | |
| 226 | // Do some basic type conversions that make sense. uint can be converted to |
| 227 | // int. int and uint can be converted to double |
| 228 | if (uint_value != nullptr && int_value == nullptr) { |
| 229 | v = static_cast<int64_t>(*uint_value); |
| 230 | int_value = &v; |
| 231 | } |
| 232 | if (uint_value != nullptr && double_value == nullptr) { |
| 233 | d = static_cast<double>(*uint_value); |
| 234 | double_value = &d; |
| 235 | } |
| 236 | if (int_value != nullptr && double_value == nullptr) { |
| 237 | d = static_cast<double>(*int_value); |
| 238 | double_value = &d; |
| 239 | } |
| 240 | |
| 241 | if (arg_code == "s") { |
| 242 | if (string_value == nullptr) { |
| 243 | return -1; |
| 244 | } |
| 245 | r = sd_bus_message_append_basic(m, arg_code[0], |
| 246 | (void *)string_value->c_str()); |
| 247 | if (r < 0) { |
| 248 | return r; |
| 249 | } |
| 250 | } else if (arg_code == "i") { |
| 251 | if (int_value == nullptr) { |
| 252 | return -1; |
| 253 | } |
| 254 | int32_t i = static_cast<int32_t>(*int_value); |
| 255 | r = sd_bus_message_append_basic(m, arg_code[0], &i); |
| 256 | if (r < 0) { |
| 257 | return r; |
| 258 | } |
| 259 | } else if (arg_code == "b") { |
| 260 | // lots of ways bool could be represented here. Try them all |
| 261 | int bool_int = false; |
| 262 | if (int_value != nullptr) { |
| 263 | bool_int = *int_value > 0 ? 1 : 0; |
| 264 | } else if (b != nullptr) { |
| 265 | bool_int = b ? 1 : 0; |
| 266 | } else if (string_value != nullptr) { |
| 267 | bool_int = boost::istarts_with(*string_value, "t") ? 1 : 0; |
| 268 | } else { |
| 269 | return -1; |
| 270 | } |
| 271 | r = sd_bus_message_append_basic(m, arg_code[0], &bool_int); |
| 272 | if (r < 0) { |
| 273 | return r; |
| 274 | } |
| 275 | } else if (arg_code == "n") { |
| 276 | if (int_value == nullptr) { |
| 277 | return -1; |
| 278 | } |
| 279 | int16_t n = static_cast<int16_t>(*int_value); |
| 280 | r = sd_bus_message_append_basic(m, arg_code[0], &n); |
| 281 | if (r < 0) { |
| 282 | return r; |
| 283 | } |
| 284 | } else if (arg_code == "x") { |
| 285 | if (int_value == nullptr) { |
| 286 | return -1; |
| 287 | } |
| 288 | r = sd_bus_message_append_basic(m, arg_code[0], int_value); |
| 289 | if (r < 0) { |
| 290 | return r; |
| 291 | } |
| 292 | } else if (arg_code == "y") { |
| 293 | if (uint_value == nullptr) { |
| 294 | return -1; |
| 295 | } |
| 296 | uint8_t y = static_cast<uint8_t>(*uint_value); |
| 297 | r = sd_bus_message_append_basic(m, arg_code[0], &y); |
| 298 | } else if (arg_code == "q") { |
| 299 | if (uint_value == nullptr) { |
| 300 | return -1; |
| 301 | } |
| 302 | uint16_t q = static_cast<uint16_t>(*uint_value); |
| 303 | r = sd_bus_message_append_basic(m, arg_code[0], &q); |
| 304 | } else if (arg_code == "u") { |
| 305 | if (uint_value == nullptr) { |
| 306 | return -1; |
| 307 | } |
| 308 | uint32_t u = static_cast<uint32_t>(*uint_value); |
| 309 | r = sd_bus_message_append_basic(m, arg_code[0], &u); |
| 310 | } else if (arg_code == "t") { |
| 311 | if (uint_value == nullptr) { |
| 312 | return -1; |
| 313 | } |
| 314 | r = sd_bus_message_append_basic(m, arg_code[0], uint_value); |
| 315 | } else if (arg_code == "d") { |
| 316 | sd_bus_message_append_basic(m, arg_code[0], double_value); |
| 317 | } else if (boost::starts_with(arg_code, "a")) { |
| 318 | std::string contained_type = arg_code.substr(1); |
| 319 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, |
| 320 | contained_type.c_str()); |
| 321 | if (r < 0) { |
| 322 | return r; |
| 323 | } |
| 324 | |
| 325 | for (nlohmann::json::const_iterator it = j->begin(); it != j->end(); |
| 326 | ++it) { |
| 327 | r = convert_json_to_dbus(m, contained_type, *it); |
| 328 | if (r < 0) { |
| 329 | return r; |
| 330 | } |
| 331 | |
| 332 | it++; |
| 333 | } |
| 334 | sd_bus_message_close_container(m); |
| 335 | } else if (boost::starts_with(arg_code, "v")) { |
| 336 | std::string contained_type = arg_code.substr(1); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 337 | BMCWEB_LOG_DEBUG << "variant type: " << arg_code |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 338 | << " appending variant of type: " << contained_type; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 339 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, |
| 340 | contained_type.c_str()); |
| 341 | if (r < 0) { |
| 342 | return r; |
| 343 | } |
| 344 | |
| 345 | r = convert_json_to_dbus(m, contained_type, input_json); |
| 346 | if (r < 0) { |
| 347 | return r; |
| 348 | } |
| 349 | |
| 350 | r = sd_bus_message_close_container(m); |
| 351 | if (r < 0) { |
| 352 | return r; |
| 353 | } |
| 354 | |
| 355 | } else if (boost::starts_with(arg_code, "(") && |
| 356 | boost::ends_with(arg_code, ")")) { |
| 357 | std::string contained_type = arg_code.substr(1, arg_code.size() - 1); |
| 358 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, |
| 359 | contained_type.c_str()); |
| 360 | nlohmann::json::const_iterator it = j->begin(); |
| 361 | for (const std::string &arg_code : dbus_arg_split(arg_type)) { |
| 362 | if (it == j->end()) { |
| 363 | return -1; |
| 364 | } |
| 365 | r = convert_json_to_dbus(m, arg_code, *it); |
| 366 | if (r < 0) { |
| 367 | return r; |
| 368 | } |
| 369 | it++; |
| 370 | } |
| 371 | r = sd_bus_message_close_container(m); |
| 372 | } else if (boost::starts_with(arg_code, "{") && |
| 373 | boost::ends_with(arg_code, "}")) { |
| 374 | std::string contained_type = arg_code.substr(1, arg_code.size() - 1); |
| 375 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY, |
| 376 | contained_type.c_str()); |
| 377 | std::vector<std::string> codes = dbus_arg_split(contained_type); |
| 378 | if (codes.size() != 2) { |
| 379 | return -1; |
| 380 | } |
| 381 | const std::string &key_type = codes[0]; |
| 382 | const std::string &value_type = codes[1]; |
| 383 | for (auto it : j->items()) { |
| 384 | r = convert_json_to_dbus(m, key_type, it.key()); |
| 385 | if (r < 0) { |
| 386 | return r; |
| 387 | }; |
| 388 | |
| 389 | r = convert_json_to_dbus(m, value_type, it.value()); |
| 390 | if (r < 0) { |
| 391 | return r; |
| 392 | } |
| 393 | } |
| 394 | r = sd_bus_message_close_container(m); |
| 395 | } else { |
| 396 | return -2; |
| 397 | } |
| 398 | if (r < 0) { |
| 399 | return r; |
| 400 | } |
| 401 | |
| 402 | if (arg_types.size() > 1) { |
| 403 | j_it++; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void find_action_on_interface(std::shared_ptr<InProgressActionData> transaction, |
| 409 | const std::string &connectionName) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 410 | BMCWEB_LOG_DEBUG << "find_action_on_interface for connection " |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 411 | << connectionName; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 412 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 413 | [ |
| 414 | transaction, connectionName{std::string(connectionName)} |
| 415 | ](const boost::system::error_code ec, const std::string &introspect_xml) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 416 | BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 417 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 418 | BMCWEB_LOG_ERROR << "Introspect call failed with error: " |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 419 | << ec.message() << " on process: " << connectionName |
| 420 | << "\n"; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 421 | } else { |
| 422 | tinyxml2::XMLDocument doc; |
| 423 | |
| 424 | doc.Parse(introspect_xml.c_str()); |
| 425 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 426 | if (pRoot == nullptr) { |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 427 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 428 | << connectionName << "\n"; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 429 | |
| 430 | } else { |
| 431 | tinyxml2::XMLElement *interface_node = |
| 432 | pRoot->FirstChildElement("interface"); |
| 433 | while (interface_node != nullptr) { |
| 434 | std::string this_interface_name = |
| 435 | interface_node->Attribute("name"); |
| 436 | tinyxml2::XMLElement *method_node = |
| 437 | interface_node->FirstChildElement("method"); |
| 438 | while (method_node != nullptr) { |
| 439 | std::string this_method_name = method_node->Attribute("name"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 440 | BMCWEB_LOG_DEBUG << "Found method: " << this_method_name; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 441 | if (this_method_name == transaction->method_name) { |
| 442 | sdbusplus::message::message m = |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 443 | crow::connections::systemBus->new_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 444 | connectionName.c_str(), transaction->path.c_str(), |
| 445 | this_interface_name.c_str(), |
| 446 | transaction->method_name.c_str()); |
| 447 | |
| 448 | tinyxml2::XMLElement *argument_node = |
| 449 | method_node->FirstChildElement("arg"); |
| 450 | |
| 451 | nlohmann::json::const_iterator arg_it = |
| 452 | transaction->arguments.begin(); |
| 453 | |
| 454 | while (argument_node != nullptr) { |
| 455 | std::string arg_direction = |
| 456 | argument_node->Attribute("direction"); |
| 457 | if (arg_direction == "in") { |
| 458 | std::string arg_type = argument_node->Attribute("type"); |
| 459 | if (arg_it == transaction->arguments.end()) { |
| 460 | transaction->setErrorStatus(); |
| 461 | return; |
| 462 | } |
| 463 | if (convert_json_to_dbus(m.get(), arg_type, *arg_it) < |
| 464 | 0) { |
| 465 | transaction->setErrorStatus(); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | arg_it++; |
| 470 | } |
| 471 | argument_node = method_node->NextSiblingElement("arg"); |
| 472 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 473 | crow::connections::systemBus->async_send( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 474 | m, [transaction](boost::system::error_code ec, |
| 475 | sdbusplus::message::message &m) { |
| 476 | if (ec) { |
| 477 | transaction->setErrorStatus(); |
| 478 | return; |
| 479 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 480 | transaction->res.jsonValue = {{"status", "ok"}, |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 481 | {"message", "200 OK"}, |
| 482 | {"data", nullptr}}; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 483 | }); |
| 484 | break; |
| 485 | } |
| 486 | method_node = method_node->NextSiblingElement("method"); |
| 487 | } |
| 488 | interface_node = interface_node->NextSiblingElement("interface"); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | }, |
| 493 | connectionName, transaction->path, "org.freedesktop.DBus.Introspectable", |
| 494 | "Introspect"); |
| 495 | } |
| 496 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 497 | void handle_action(const crow::Request &req, crow::Response &res, |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 498 | const std::string &object_path, |
| 499 | const std::string &method_name) { |
| 500 | nlohmann::json request_dbus_data = |
| 501 | nlohmann::json::parse(req.body, nullptr, false); |
| 502 | |
| 503 | if (request_dbus_data.is_discarded()) { |
| 504 | res.result(boost::beast::http::status::bad_request); |
| 505 | res.end(); |
| 506 | return; |
| 507 | } |
| 508 | if (!request_dbus_data.is_array()) { |
| 509 | res.result(boost::beast::http::status::bad_request); |
| 510 | res.end(); |
| 511 | return; |
| 512 | } |
| 513 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 514 | |
| 515 | transaction->path = object_path; |
| 516 | transaction->method_name = method_name; |
| 517 | transaction->arguments = std::move(request_dbus_data); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 518 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 519 | [transaction]( |
| 520 | const boost::system::error_code ec, |
| 521 | const std::vector<std::pair<std::string, std::vector<std::string>>> |
| 522 | &interface_names) { |
| 523 | if (ec || interface_names.size() <= 0) { |
| 524 | transaction->setErrorStatus(); |
| 525 | return; |
| 526 | } |
| 527 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 528 | BMCWEB_LOG_DEBUG << "GetObject returned objects " |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 529 | << interface_names.size(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 530 | |
| 531 | for (const std::pair<std::string, std::vector<std::string>> &object : |
| 532 | interface_names) { |
| 533 | find_action_on_interface(transaction, object.first); |
| 534 | } |
| 535 | }, |
| 536 | "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper", |
| 537 | "xyz.openbmc_project.ObjectMapper", "GetObject", object_path, |
| 538 | std::array<std::string, 0>()); |
| 539 | } |
| 540 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 541 | void handle_list(crow::Response &res, const std::string &objectPath) { |
| 542 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 543 | [&res](const boost::system::error_code ec, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 544 | std::vector<std::string> &objectPaths) { |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 545 | if (ec) { |
| 546 | res.result(boost::beast::http::status::internal_server_error); |
| 547 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 548 | res.jsonValue = {{"status", "ok"}, |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 549 | {"message", "200 OK"}, |
| 550 | {"data", std::move(objectPaths)}}; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 551 | } |
| 552 | res.end(); |
| 553 | }, |
| 554 | "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 555 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath, |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 556 | static_cast<int32_t>(99), std::array<std::string, 0>()); |
| 557 | } |
| 558 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 559 | void handle_enumerate(crow::Response &res, const std::string &objectPath) { |
| 560 | crow::connections::systemBus->async_method_call( |
| 561 | [&res, objectPath{std::string(objectPath)} ]( |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 562 | const boost::system::error_code ec, |
| 563 | const GetSubTreeType &object_names) { |
| 564 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 565 | res.jsonValue = {{"message", "200 OK"}, |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 566 | {"status", "ok"}, |
| 567 | {"data", nlohmann::json::object()}}; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 568 | |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 569 | res.end(); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | boost::container::flat_set<std::string> connections; |
| 574 | |
| 575 | for (const auto &object : object_names) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 576 | for (const auto &Connection : object.second) { |
| 577 | connections.insert(Connection.first); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | if (connections.size() <= 0) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 582 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 583 | res.end(); |
| 584 | return; |
| 585 | } |
| 586 | auto transaction = |
| 587 | std::make_shared<nlohmann::json>(nlohmann::json::object()); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 588 | for (const std::string &Connection : connections) { |
| 589 | getManagedObjectsForEnumerate(objectPath, Connection, res, |
| 590 | transaction); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 591 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 592 | }, |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 593 | "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 594 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, (int32_t)0, |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 595 | std::array<std::string, 0>()); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 596 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 597 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 598 | void handle_get(crow::Response &res, std::string &object_path, |
| 599 | std::string &dest_property) { |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 600 | BMCWEB_LOG_DEBUG << "handle_get: " << object_path |
| 601 | << " prop:" << dest_property; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 602 | std::shared_ptr<std::string> property_name = |
| 603 | std::make_shared<std::string>(dest_property); |
| 604 | using GetObjectType = |
| 605 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 606 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 607 | [&res, object_path, property_name](const boost::system::error_code ec, |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 608 | const GetObjectType &object_names) { |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 609 | if (ec || object_names.size() <= 0) { |
| 610 | res.result(boost::beast::http::status::not_found); |
| 611 | res.end(); |
| 612 | return; |
| 613 | } |
| 614 | std::shared_ptr<nlohmann::json> response = |
| 615 | std::make_shared<nlohmann::json>(nlohmann::json::object()); |
| 616 | // The mapper should never give us an empty interface names list, but |
| 617 | // check anyway |
| 618 | for (const std::pair<std::string, std::vector<std::string>> connection : |
| 619 | object_names) { |
| 620 | const std::vector<std::string> &interfaceNames = connection.second; |
| 621 | |
| 622 | if (interfaceNames.size() <= 0) { |
| 623 | res.result(boost::beast::http::status::not_found); |
| 624 | res.end(); |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | for (const std::string &interface : interfaceNames) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 629 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 630 | [&res, response, property_name]( |
| 631 | const boost::system::error_code ec, |
| 632 | const std::vector<std::pair< |
| 633 | std::string, DbusRestVariantType>> &properties) { |
| 634 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 635 | BMCWEB_LOG_ERROR << "Bad dbus request error: " << ec; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 636 | } else { |
| 637 | for (const std::pair<std::string, DbusRestVariantType> |
| 638 | &property : properties) { |
| 639 | // if property name is empty, or matches our search query, |
| 640 | // add it to the response json |
| 641 | |
| 642 | if (property_name->empty()) { |
| 643 | mapbox::util::apply_visitor( |
| 644 | [&response, &property](auto &&val) { |
| 645 | (*response)[property.first] = val; |
| 646 | }, |
| 647 | property.second); |
| 648 | } else if (property.first == *property_name) { |
| 649 | mapbox::util::apply_visitor( |
| 650 | [&response](auto &&val) { (*response) = val; }, |
| 651 | property.second); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | if (response.use_count() == 1) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 656 | res.jsonValue = {{"status", "ok"}, |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 657 | {"message", "200 OK"}, |
| 658 | {"data", *response}}; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 659 | |
| 660 | res.end(); |
| 661 | } |
| 662 | }, |
| 663 | connection.first, object_path, |
| 664 | "org.freedesktop.DBus.Properties", "GetAll", interface); |
| 665 | } |
| 666 | } |
| 667 | }, |
| 668 | "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper", |
| 669 | "xyz.openbmc_project.ObjectMapper", "GetObject", object_path, |
| 670 | std::array<std::string, 0>()); |
| 671 | } |
| 672 | |
| 673 | struct AsyncPutRequest { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 674 | AsyncPutRequest(crow::Response &res) : res(res) { |
| 675 | res.jsonValue = { |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 676 | {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}}; |
| 677 | } |
| 678 | ~AsyncPutRequest() { |
| 679 | if (res.result() == boost::beast::http::status::internal_server_error) { |
| 680 | // Reset the json object to clear out any data that made it in before the |
| 681 | // error happened |
| 682 | // todo(ed) handle error condition with proper code |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 683 | res.jsonValue = nlohmann::json::object(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 686 | if (res.jsonValue.empty()) { |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 687 | res.result(boost::beast::http::status::forbidden); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 688 | res.jsonValue = { |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 689 | {"status", "error"}, |
| 690 | {"message", "403 Forbidden"}, |
| 691 | {"data", |
| 692 | {{"message", |
| 693 | "The specified property cannot be created: " + propertyName}}}}; |
| 694 | } |
| 695 | |
| 696 | res.end(); |
| 697 | } |
| 698 | |
| 699 | void setErrorStatus() { |
| 700 | res.result(boost::beast::http::status::internal_server_error); |
| 701 | } |
| 702 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 703 | crow::Response &res; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 704 | std::string objectPath; |
| 705 | std::string propertyName; |
| 706 | nlohmann::json propertyValue; |
| 707 | }; |
| 708 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 709 | void handle_put(const crow::Request &req, crow::Response &res, |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 710 | const std::string &objectPath, |
| 711 | const std::string &destProperty) { |
| 712 | nlohmann::json request_dbus_data = |
| 713 | nlohmann::json::parse(req.body, nullptr, false); |
| 714 | |
| 715 | if (request_dbus_data.is_discarded()) { |
| 716 | res.result(boost::beast::http::status::bad_request); |
| 717 | res.end(); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | nlohmann::json::const_iterator property_it = request_dbus_data.find("data"); |
| 722 | if (property_it == request_dbus_data.end()) { |
| 723 | res.result(boost::beast::http::status::bad_request); |
| 724 | res.end(); |
| 725 | return; |
| 726 | } |
| 727 | const nlohmann::json &propertySetValue = *property_it; |
| 728 | auto transaction = std::make_shared<AsyncPutRequest>(res); |
| 729 | transaction->objectPath = objectPath; |
| 730 | transaction->propertyName = destProperty; |
| 731 | transaction->propertyValue = propertySetValue; |
| 732 | |
| 733 | using GetObjectType = |
| 734 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 735 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 736 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 737 | [transaction](const boost::system::error_code ec, |
| 738 | const GetObjectType &object_names) { |
| 739 | if (!ec && object_names.size() <= 0) { |
| 740 | transaction->res.result(boost::beast::http::status::not_found); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | for (const std::pair<std::string, std::vector<std::string>> connection : |
| 745 | object_names) { |
| 746 | const std::string &connectionName = connection.first; |
| 747 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 748 | crow::connections::systemBus->async_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 749 | [ connectionName{std::string(connectionName)}, transaction ]( |
| 750 | const boost::system::error_code ec, |
| 751 | const std::string &introspectXml) { |
| 752 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 753 | BMCWEB_LOG_ERROR |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 754 | << "Introspect call failed with error: " << ec.message() |
| 755 | << " on process: " << connectionName; |
| 756 | transaction->setErrorStatus(); |
| 757 | return; |
| 758 | } |
| 759 | tinyxml2::XMLDocument doc; |
| 760 | |
| 761 | doc.Parse(introspectXml.c_str()); |
| 762 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 763 | if (pRoot == nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 764 | BMCWEB_LOG_ERROR << "XML document failed to parse: " |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 765 | << introspectXml; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 766 | transaction->setErrorStatus(); |
| 767 | return; |
| 768 | } |
| 769 | tinyxml2::XMLElement *ifaceNode = |
| 770 | pRoot->FirstChildElement("interface"); |
| 771 | while (ifaceNode != nullptr) { |
| 772 | const char *interfaceName = ifaceNode->Attribute("name"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 773 | BMCWEB_LOG_DEBUG << "found interface " << interfaceName; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 774 | tinyxml2::XMLElement *propNode = |
| 775 | ifaceNode->FirstChildElement("property"); |
| 776 | while (propNode != nullptr) { |
| 777 | const char *propertyName = propNode->Attribute("name"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 778 | BMCWEB_LOG_DEBUG << "Found property " << propertyName; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 779 | if (propertyName == transaction->propertyName) { |
| 780 | const char *argType = propNode->Attribute("type"); |
| 781 | if (argType != nullptr) { |
| 782 | sdbusplus::message::message m = |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 783 | crow::connections::systemBus->new_method_call( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 784 | connectionName.c_str(), |
| 785 | transaction->objectPath.c_str(), |
| 786 | "org.freedesktop.DBus.Properties", "Set"); |
| 787 | m.append(interfaceName, transaction->propertyName); |
| 788 | int r = sd_bus_message_open_container( |
| 789 | m.get(), SD_BUS_TYPE_VARIANT, argType); |
| 790 | if (r < 0) { |
| 791 | transaction->setErrorStatus(); |
| 792 | return; |
| 793 | } |
| 794 | r = convert_json_to_dbus(m.get(), argType, |
| 795 | transaction->propertyValue); |
| 796 | if (r < 0) { |
| 797 | transaction->setErrorStatus(); |
| 798 | return; |
| 799 | } |
| 800 | r = sd_bus_message_close_container(m.get()); |
| 801 | if (r < 0) { |
| 802 | transaction->setErrorStatus(); |
| 803 | return; |
| 804 | } |
| 805 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 806 | crow::connections::systemBus->async_send( |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 807 | m, [transaction](boost::system::error_code ec, |
| 808 | sdbusplus::message::message &m) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 809 | BMCWEB_LOG_DEBUG << "sent"; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 810 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 811 | transaction->res.jsonValue["status"] = "error"; |
| 812 | transaction->res.jsonValue["message"] = |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 813 | ec.message(); |
| 814 | } |
| 815 | }); |
| 816 | } |
| 817 | } |
| 818 | propNode = propNode->NextSiblingElement("property"); |
| 819 | } |
| 820 | ifaceNode = ifaceNode->NextSiblingElement("interface"); |
| 821 | } |
| 822 | }, |
| 823 | connectionName, transaction->objectPath, |
| 824 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 825 | } |
| 826 | }, |
| 827 | "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper", |
| 828 | "xyz.openbmc_project.ObjectMapper", "GetObject", transaction->objectPath, |
| 829 | std::array<std::string, 0>()); |
| 830 | } |
| 831 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 832 | template <typename... Middlewares> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 833 | void requestRoutes(Crow<Middlewares...> &app) { |
| 834 | BMCWEB_ROUTE(app, "/bus/") |
| 835 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res) { |
| 836 | res.jsonValue = {{"busses", {{{"name", "system"}}}}, {"status", "ok"}}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 837 | }); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 838 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 839 | BMCWEB_ROUTE(app, "/bus/system/") |
| 840 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res) { |
| 841 | |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 842 | auto myCallback = [&res](const boost::system::error_code ec, |
| 843 | std::vector<std::string> &names) { |
| 844 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 845 | BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 846 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 847 | } else { |
| 848 | std::sort(names.begin(), names.end()); |
| 849 | nlohmann::json j{{"status", "ok"}}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 850 | auto &objectsSub = j["objects"]; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 851 | for (auto &name : names) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 852 | objectsSub.push_back({{"name", name}}); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 853 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 854 | res.jsonValue = std::move(j); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 855 | } |
| 856 | res.end(); |
| 857 | }; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 858 | crow::connections::systemBus->async_method_call( |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 859 | std::move(myCallback), "org.freedesktop.DBus", "/", |
| 860 | "org.freedesktop.DBus", "ListNames"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 861 | }); |
| 862 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 863 | BMCWEB_ROUTE(app, "/list/") |
| 864 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res) { |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 865 | handle_list(res, "/"); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 866 | }); |
| 867 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 868 | BMCWEB_ROUTE(app, "/xyz/<path>") |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 869 | .methods("GET"_method, "PUT"_method, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 870 | "POST"_method)([](const crow::Request &req, crow::Response &res, |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 871 | const std::string &path) { |
Ed Tanous | a4e18f2 | 2018-04-27 10:25:29 -0700 | [diff] [blame] | 872 | std::string object_path = "/xyz/" + path; |
| 873 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 874 | // Trim any trailing "/" at the end |
| 875 | if (boost::ends_with(object_path, "/")) { |
| 876 | object_path.pop_back(); |
| 877 | } |
| 878 | |
| 879 | // If accessing a single attribute, fill in and update object_path, |
| 880 | // otherwise leave dest_property blank |
| 881 | std::string dest_property = ""; |
| 882 | const char *attr_seperator = "/attr/"; |
Ed Tanous | a4e18f2 | 2018-04-27 10:25:29 -0700 | [diff] [blame] | 883 | size_t attr_position = path.find(attr_seperator); |
| 884 | if (attr_position != path.npos) { |
shiyilei | 00b92f7 | 2017-11-12 16:21:16 +0800 | [diff] [blame] | 885 | object_path = "/xyz/" + path.substr(0, attr_position); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 886 | dest_property = path.substr(attr_position + strlen(attr_seperator), |
Ed Tanous | a4e18f2 | 2018-04-27 10:25:29 -0700 | [diff] [blame] | 887 | path.length()); |
shiyilei | 00b92f7 | 2017-11-12 16:21:16 +0800 | [diff] [blame] | 888 | } |
| 889 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 890 | if (req.method() == "POST"_method) { |
| 891 | constexpr const char *action_seperator = "/action/"; |
| 892 | size_t action_position = path.find(action_seperator); |
| 893 | if (action_position != path.npos) { |
| 894 | object_path = "/xyz/" + path.substr(0, action_position); |
| 895 | std::string post_property = path.substr( |
| 896 | (action_position + strlen(action_seperator)), path.length()); |
| 897 | handle_action(req, res, object_path, post_property); |
| 898 | return; |
| 899 | } |
| 900 | } else if (req.method() == "GET"_method) { |
| 901 | if (boost::ends_with(object_path, "/enumerate")) { |
| 902 | object_path.erase(object_path.end() - 10, object_path.end()); |
| 903 | handle_enumerate(res, object_path); |
| 904 | } else if (boost::ends_with(object_path, "/list")) { |
| 905 | object_path.erase(object_path.end() - 5, object_path.end()); |
| 906 | handle_list(res, object_path); |
| 907 | } else { |
| 908 | handle_get(res, object_path, dest_property); |
| 909 | } |
| 910 | return; |
| 911 | } else if (req.method() == "PUT"_method) { |
| 912 | handle_put(req, res, object_path, dest_property); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 913 | return; |
| 914 | } |
| 915 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 916 | res.result(boost::beast::http::status::method_not_allowed); |
| 917 | res.end(); |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 918 | }); |
shiyilei | 00b92f7 | 2017-11-12 16:21:16 +0800 | [diff] [blame] | 919 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 920 | BMCWEB_ROUTE(app, "/bus/system/<str>/") |
| 921 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
| 922 | const std::string &Connection) { |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 923 | std::shared_ptr<nlohmann::json> transaction; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 924 | introspectObjects(res, Connection, "/", transaction); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 925 | }); |
| 926 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 927 | BMCWEB_ROUTE(app, "/download/dump/<str>/") |
| 928 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 929 | const std::string &dumpId) { |
| 930 | std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$"); |
| 931 | if (!std::regex_match(dumpId, validFilename)) { |
| 932 | res.result(boost::beast::http::status::not_found); |
| 933 | res.end(); |
| 934 | return; |
| 935 | } |
| 936 | std::experimental::filesystem::path loc( |
| 937 | "/var/lib/phosphor-debug-collector/dumps"); |
| 938 | |
| 939 | loc += dumpId; |
| 940 | |
| 941 | if (!std::experimental::filesystem::exists(loc) || |
| 942 | !std::experimental::filesystem::is_directory(loc)) { |
| 943 | res.result(boost::beast::http::status::not_found); |
| 944 | res.end(); |
| 945 | return; |
| 946 | } |
| 947 | std::experimental::filesystem::directory_iterator files(loc); |
| 948 | for (auto &file : files) { |
| 949 | std::ifstream readFile(file.path()); |
| 950 | if (readFile.good()) { |
| 951 | continue; |
| 952 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 953 | res.addHeader("Content-Type", "application/octet-stream"); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 954 | res.body() = {std::istreambuf_iterator<char>(readFile), |
| 955 | std::istreambuf_iterator<char>()}; |
| 956 | res.end(); |
| 957 | } |
| 958 | res.result(boost::beast::http::status::not_found); |
| 959 | res.end(); |
| 960 | return; |
| 961 | }); |
| 962 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 963 | BMCWEB_ROUTE(app, "/bus/system/<str>/<path>") |
| 964 | .methods("GET"_method)([](const crow::Request &req, crow::Response &res, |
| 965 | const std::string &processName, |
| 966 | const std::string &requestedPath) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 967 | std::vector<std::string> strs; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 968 | boost::split(strs, requestedPath, boost::is_any_of("/")); |
| 969 | std::string objectPath; |
| 970 | std::string interfaceName; |
| 971 | std::string methodName; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 972 | auto it = strs.begin(); |
| 973 | if (it == strs.end()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 974 | objectPath = "/"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 975 | } |
| 976 | while (it != strs.end()) { |
| 977 | // Check if segment contains ".". If it does, it must be an |
| 978 | // interface |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 979 | if (it->find(".") != std::string::npos) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 980 | break; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 981 | // THis check is neccesary as the trailing slash gets parsed as |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 982 | // part of our <path> specifier above, which causes the normal |
| 983 | // trailing backslash redirector to fail. |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 984 | } else if (!it->empty()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 985 | objectPath += "/" + *it; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 986 | } |
| 987 | it++; |
| 988 | } |
| 989 | if (it != strs.end()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 990 | interfaceName = *it; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 991 | it++; |
| 992 | |
| 993 | // after interface, we might have a method name |
| 994 | if (it != strs.end()) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 995 | methodName = *it; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 996 | it++; |
| 997 | } |
| 998 | } |
| 999 | if (it != strs.end()) { |
| 1000 | // if there is more levels past the method name, something went |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1001 | // wrong, return not found |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1002 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1003 | res.end(); |
| 1004 | return; |
| 1005 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1006 | if (interfaceName.empty()) { |
| 1007 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame^] | 1008 | [&, processName, objectPath](const boost::system::error_code ec, |
| 1009 | const std::string &introspect_xml) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1010 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1011 | BMCWEB_LOG_ERROR |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1012 | << "Introspect call failed with error: " << ec.message() |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1013 | << " on process: " << processName |
| 1014 | << " path: " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1015 | |
| 1016 | } else { |
| 1017 | tinyxml2::XMLDocument doc; |
| 1018 | |
| 1019 | doc.Parse(introspect_xml.c_str()); |
| 1020 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 1021 | if (pRoot == nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1022 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 1023 | << processName << " " << objectPath |
| 1024 | << "\n"; |
| 1025 | res.jsonValue = {{"status", "XML parse error"}}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1026 | res.result( |
| 1027 | boost::beast::http::status::internal_server_error); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1028 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1029 | nlohmann::json interfacesArray = nlohmann::json::array(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1030 | tinyxml2::XMLElement *interface = |
| 1031 | pRoot->FirstChildElement("interface"); |
| 1032 | |
| 1033 | while (interface != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1034 | std::string ifaceName = interface->Attribute("name"); |
| 1035 | interfacesArray.push_back({{"name", ifaceName}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1036 | |
| 1037 | interface = interface->NextSiblingElement("interface"); |
| 1038 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1039 | res.jsonValue = {{"status", "ok"}, |
| 1040 | {"bus_name", processName}, |
| 1041 | {"interfaces", interfacesArray}, |
| 1042 | {"object_path", objectPath}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | res.end(); |
| 1046 | }, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1047 | processName, objectPath, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 1048 | "Introspect"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1049 | } else { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1050 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1051 | [ |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1052 | &, processName, objectPath, |
| 1053 | interface_name{std::move(interfaceName)} |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1054 | ](const boost::system::error_code ec, |
| 1055 | const std::string &introspect_xml) { |
| 1056 | if (ec) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1057 | BMCWEB_LOG_ERROR |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1058 | << "Introspect call failed with error: " << ec.message() |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1059 | << " on process: " << processName |
| 1060 | << " path: " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1061 | |
| 1062 | } else { |
| 1063 | tinyxml2::XMLDocument doc; |
| 1064 | |
| 1065 | doc.Parse(introspect_xml.c_str()); |
| 1066 | tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node"); |
| 1067 | if (pRoot == nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1068 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 1069 | << processName << " " << objectPath |
| 1070 | << "\n"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1071 | res.result( |
| 1072 | boost::beast::http::status::internal_server_error); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1073 | |
| 1074 | } else { |
| 1075 | tinyxml2::XMLElement *node = |
| 1076 | pRoot->FirstChildElement("node"); |
| 1077 | |
| 1078 | // if we know we're the only call, build the json directly |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1079 | nlohmann::json methodsArray = nlohmann::json::array(); |
| 1080 | nlohmann::json signalsArray = nlohmann::json::array(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1081 | tinyxml2::XMLElement *interface = |
| 1082 | pRoot->FirstChildElement("interface"); |
| 1083 | |
| 1084 | while (interface != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1085 | std::string ifaceName = interface->Attribute("name"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1086 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1087 | if (ifaceName == interfaceName) { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1088 | tinyxml2::XMLElement *methods = |
| 1089 | interface->FirstChildElement("method"); |
| 1090 | while (methods != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1091 | nlohmann::json argsArray = nlohmann::json::array(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1092 | tinyxml2::XMLElement *arg = |
| 1093 | methods->FirstChildElement("arg"); |
| 1094 | while (arg != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1095 | argsArray.push_back( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1096 | {{"name", arg->Attribute("name")}, |
| 1097 | {"type", arg->Attribute("type")}, |
| 1098 | {"direction", arg->Attribute("direction")}}); |
| 1099 | arg = arg->NextSiblingElement("arg"); |
| 1100 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1101 | methodsArray.push_back( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1102 | {{"name", methods->Attribute("name")}, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1103 | {"uri", "/bus/system/" + processName + |
| 1104 | objectPath + "/" + interfaceName + |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1105 | "/" + methods->Attribute("name")}, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1106 | {"args", argsArray}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1107 | methods = methods->NextSiblingElement("method"); |
| 1108 | } |
| 1109 | tinyxml2::XMLElement *signals = |
| 1110 | interface->FirstChildElement("signal"); |
| 1111 | while (signals != nullptr) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1112 | nlohmann::json argsArray = nlohmann::json::array(); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1113 | |
| 1114 | tinyxml2::XMLElement *arg = |
| 1115 | signals->FirstChildElement("arg"); |
| 1116 | while (arg != nullptr) { |
| 1117 | std::string name = arg->Attribute("name"); |
| 1118 | std::string type = arg->Attribute("type"); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1119 | argsArray.push_back({ |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1120 | {"name", name}, |
| 1121 | {"type", type}, |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1122 | }); |
| 1123 | arg = arg->NextSiblingElement("arg"); |
| 1124 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1125 | signalsArray.push_back( |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1126 | {{"name", signals->Attribute("name")}, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1127 | {"args", argsArray}}); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1128 | signals = signals->NextSiblingElement("signal"); |
| 1129 | } |
| 1130 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1131 | res.jsonValue = { |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1132 | {"status", "ok"}, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1133 | {"bus_name", processName}, |
| 1134 | {"interface", interfaceName}, |
| 1135 | {"methods", methodsArray}, |
| 1136 | {"object_path", objectPath}, |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1137 | {"properties", nlohmann::json::object()}, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1138 | {"signals", signalsArray}}; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1139 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | interface = interface->NextSiblingElement("interface"); |
| 1144 | } |
| 1145 | if (interface == nullptr) { |
| 1146 | // if we got to the end of the list and never found a |
| 1147 | // match, throw 404 |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1148 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | res.end(); |
| 1153 | }, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 1154 | processName, objectPath, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 1155 | "Introspect"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1156 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1157 | }); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1158 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1159 | } // namespace openbmc_mapper |
| 1160 | } // namespace crow |