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