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