blob: 1f3b9e29bdda2e69ea80b403402c697748646588 [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#include <crow/app.h>
2
3#include <tinyxml2.h>
Ed Tanous911ac312017-08-15 09:37:42 -07004#include <dbus_singleton.hpp>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07005#include <experimental/filesystem>
6#include <fstream>
Ed Tanouse0d918b2018-03-27 17:41:04 -07007#include <boost/algorithm/string.hpp>
Ed Tanous64530012018-02-06 17:08:16 -08008#include <boost/container/flat_set.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -07009
10namespace crow {
11namespace openbmc_mapper {
Ed Tanousba9f9a62017-10-11 16:40:35 -070012
Ed Tanous55c7b7a2018-05-22 15:27:24 -070013void 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 Tanousaa2e59c2018-04-12 12:17:20 -070017 [
Ed Tanous55c7b7a2018-05-22 15:27:24 -070018 &res, transaction, processName{std::move(process_name)},
19 objectPath{std::move(path)}
Ed Tanousaa2e59c2018-04-12 12:17:20 -070020 ](const boost::system::error_code ec, const std::string &introspect_xml) {
Ed Tanous911ac312017-08-15 09:37:42 -070021 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070022 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
23 << ec.message() << " on process: " << processName
24 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070025
26 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070027 transaction->push_back({{"path", objectPath}});
Ed Tanous911ac312017-08-15 09:37:42 -070028
29 tinyxml2::XMLDocument doc;
30
31 doc.Parse(introspect_xml.c_str());
32 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
33 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070034 BMCWEB_LOG_ERROR << "XML document failed to parse " << processName
35 << " " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070036
37 } else {
38 tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
39 while (node != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070040 std::string childPath = node->Attribute("name");
Ed Tanous911ac312017-08-15 09:37:42 -070041 std::string newpath;
Ed Tanous55c7b7a2018-05-22 15:27:24 -070042 if (objectPath != "/") {
43 newpath += objectPath;
Ed Tanous911ac312017-08-15 09:37:42 -070044 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070045 newpath += "/" + childPath;
Ed Tanous64530012018-02-06 17:08:16 -080046 // introspect the subobjects as well
Ed Tanous55c7b7a2018-05-22 15:27:24 -070047 introspectObjects(res, processName, newpath, transaction);
Ed Tanous911ac312017-08-15 09:37:42 -070048
49 node = node->NextSiblingElement("node");
50 }
51 }
52 }
53 // if we're the last outstanding caller, finish the request
Ed Tanous64530012018-02-06 17:08:16 -080054 if (transaction.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070055 res.jsonValue = {{"status", "ok"},
56 {"bus_name", processName},
Ed Tanousd4bb9bb2018-05-16 13:36:42 -070057 {"objects", std::move(*transaction)}};
Ed Tanous911ac312017-08-15 09:37:42 -070058 res.end();
59 }
60 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -070061 process_name, path, "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -070062}
Ed Tanous64530012018-02-06 17:08:16 -080063
Ed Tanousaa2e59c2018-04-12 12:17:20 -070064// A smattering of common types to unpack. TODO(ed) this should really iterate
65// the sdbusplus object directly and build the json response
66using 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
71using 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 Tanous55c7b7a2018-05-22 15:27:24 -070077void getManagedObjectsForEnumerate(
Ed Tanous64530012018-02-06 17:08:16 -080078 const std::string &object_name, const std::string &connection_name,
Ed Tanous55c7b7a2018-05-22 15:27:24 -070079 crow::Response &res, std::shared_ptr<nlohmann::json> transaction) {
80 crow::connections::systemBus->async_method_call(
Ed Tanous64530012018-02-06 17:08:16 -080081 [&res, transaction](const boost::system::error_code ec,
82 const ManagedObjectType &objects) {
83 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070084 BMCWEB_LOG_ERROR << ec;
Ed Tanous64530012018-02-06 17:08:16 -080085 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070086 nlohmann::json &dataJson = *transaction;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070087
Ed Tanous55c7b7a2018-05-22 15:27:24 -070088 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 Tanousaa2e59c2018-04-12 12:17:20 -070096 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070097 for (const auto &interface : objectPath.second) {
Ed Tanous64530012018-02-06 17:08:16 -080098 for (const auto &property : interface.second) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070099 nlohmann::json &propertyJson = objectJson[property.first];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700100 mapbox::util::apply_visitor(
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700101 [&propertyJson](auto &&val) { propertyJson = val; },
Ed Tanous64530012018-02-06 17:08:16 -0800102 property.second);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700103
104 // dbus-rest represents booleans as 1 or 0, implement to match
105 // TODO(ed) see if dbus-rest should be changed
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700106 const bool *propertyBool = propertyJson.get_ptr<const bool *>();
107 if (propertyBool != nullptr) {
108 propertyJson = *propertyBool ? 1 : 0;
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700109 }
Ed Tanous64530012018-02-06 17:08:16 -0800110 }
111 }
112 }
113 }
114
115 if (transaction.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700116 res.jsonValue = {{"message", "200 OK"},
117 {"status", "ok"},
118 {"data", std::move(*transaction)}};
Ed Tanous64530012018-02-06 17:08:16 -0800119 res.end();
120 }
121 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700122 connection_name, object_name, "org.freedesktop.DBus.ObjectManager",
123 "GetManagedObjects");
124}
Ed Tanous64530012018-02-06 17:08:16 -0800125
126using GetSubTreeType = std::vector<
127 std::pair<std::string,
128 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
129
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700130// Structure for storing data on an in progress action
131struct InProgressActionData {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700132 InProgressActionData(crow::Response &res) : res(res){};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700133 ~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 Tanous55c7b7a2018-05-22 15:27:24 -0700138 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700139 }
140 res.end();
141 }
142
143 void setErrorStatus() {
144 res.result(boost::beast::http::status::internal_server_error);
145 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700146 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700147 std::string path;
148 std::string method_name;
149 nlohmann::json arguments;
150};
151
152std::vector<std::string> dbus_arg_split(const std::string &string) {
153 std::vector<std::string> ret;
154 if (string.empty()) {
155 return ret;
156 }
157 ret.push_back("");
158 int container_depth = 0;
159 std::string::const_iterator character = string.begin();
160 while (character != string.end()) {
161 switch (*character) {
162 case ('a'):
163 ret.back() += *character;
164 break;
165 case ('('):
166 case ('{'):
167 ret.back() += *character;
168 container_depth++;
169 break;
170 case ('}'):
171 case (')'):
172 ret.back() += *character;
173 container_depth--;
174 if (container_depth == 0) {
175 character++;
176 if (character != string.end()) {
177 ret.push_back("");
178 }
179 continue;
180 }
181 break;
182 default:
183 ret.back() += *character;
184 if (container_depth == 0) {
185 character++;
186 if (character != string.end()) {
187 ret.push_back("");
188 }
189 continue;
190 }
191 break;
192 }
193 character++;
194 }
195}
196
197int convert_json_to_dbus(sd_bus_message *m, const std::string &arg_type,
198 const nlohmann::json &input_json) {
199 int r = 0;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700200 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700201 << " to type: " << arg_type;
202 const std::vector<std::string> arg_types = dbus_arg_split(arg_type);
203
204 // Assume a single object for now.
205 const nlohmann::json *j = &input_json;
206 nlohmann::json::const_iterator j_it = input_json.begin();
207
208 for (const std::string &arg_code : arg_types) {
209 // If we are decoding multiple objects, grab the pointer to the iterator,
210 // and increment it for the next loop
211 if (arg_types.size() > 1) {
212 if (j_it == input_json.end()) {
213 return -2;
214 }
215 j = &*j_it;
216 j_it++;
217 }
218 const int64_t *int_value = j->get_ptr<const int64_t *>();
219 const uint64_t *uint_value = j->get_ptr<const uint64_t *>();
220 const std::string *string_value = j->get_ptr<const std::string *>();
221 const double *double_value = j->get_ptr<const double *>();
222 const bool *b = j->get_ptr<const bool *>();
223 int64_t v = 0;
224 double d = 0.0;
225
226 // Do some basic type conversions that make sense. uint can be converted to
227 // int. int and uint can be converted to double
228 if (uint_value != nullptr && int_value == nullptr) {
229 v = static_cast<int64_t>(*uint_value);
230 int_value = &v;
231 }
232 if (uint_value != nullptr && double_value == nullptr) {
233 d = static_cast<double>(*uint_value);
234 double_value = &d;
235 }
236 if (int_value != nullptr && double_value == nullptr) {
237 d = static_cast<double>(*int_value);
238 double_value = &d;
239 }
240
241 if (arg_code == "s") {
242 if (string_value == nullptr) {
243 return -1;
244 }
245 r = sd_bus_message_append_basic(m, arg_code[0],
246 (void *)string_value->c_str());
247 if (r < 0) {
248 return r;
249 }
250 } else if (arg_code == "i") {
251 if (int_value == nullptr) {
252 return -1;
253 }
254 int32_t i = static_cast<int32_t>(*int_value);
255 r = sd_bus_message_append_basic(m, arg_code[0], &i);
256 if (r < 0) {
257 return r;
258 }
259 } else if (arg_code == "b") {
260 // lots of ways bool could be represented here. Try them all
261 int bool_int = false;
262 if (int_value != nullptr) {
263 bool_int = *int_value > 0 ? 1 : 0;
264 } else if (b != nullptr) {
265 bool_int = b ? 1 : 0;
266 } else if (string_value != nullptr) {
267 bool_int = boost::istarts_with(*string_value, "t") ? 1 : 0;
268 } else {
269 return -1;
270 }
271 r = sd_bus_message_append_basic(m, arg_code[0], &bool_int);
272 if (r < 0) {
273 return r;
274 }
275 } else if (arg_code == "n") {
276 if (int_value == nullptr) {
277 return -1;
278 }
279 int16_t n = static_cast<int16_t>(*int_value);
280 r = sd_bus_message_append_basic(m, arg_code[0], &n);
281 if (r < 0) {
282 return r;
283 }
284 } else if (arg_code == "x") {
285 if (int_value == nullptr) {
286 return -1;
287 }
288 r = sd_bus_message_append_basic(m, arg_code[0], int_value);
289 if (r < 0) {
290 return r;
291 }
292 } else if (arg_code == "y") {
293 if (uint_value == nullptr) {
294 return -1;
295 }
296 uint8_t y = static_cast<uint8_t>(*uint_value);
297 r = sd_bus_message_append_basic(m, arg_code[0], &y);
298 } else if (arg_code == "q") {
299 if (uint_value == nullptr) {
300 return -1;
301 }
302 uint16_t q = static_cast<uint16_t>(*uint_value);
303 r = sd_bus_message_append_basic(m, arg_code[0], &q);
304 } else if (arg_code == "u") {
305 if (uint_value == nullptr) {
306 return -1;
307 }
308 uint32_t u = static_cast<uint32_t>(*uint_value);
309 r = sd_bus_message_append_basic(m, arg_code[0], &u);
310 } else if (arg_code == "t") {
311 if (uint_value == nullptr) {
312 return -1;
313 }
314 r = sd_bus_message_append_basic(m, arg_code[0], uint_value);
315 } else if (arg_code == "d") {
316 sd_bus_message_append_basic(m, arg_code[0], double_value);
317 } else if (boost::starts_with(arg_code, "a")) {
318 std::string contained_type = arg_code.substr(1);
319 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
320 contained_type.c_str());
321 if (r < 0) {
322 return r;
323 }
324
325 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
326 ++it) {
327 r = convert_json_to_dbus(m, contained_type, *it);
328 if (r < 0) {
329 return r;
330 }
331
332 it++;
333 }
334 sd_bus_message_close_container(m);
335 } else if (boost::starts_with(arg_code, "v")) {
336 std::string contained_type = arg_code.substr(1);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700337 BMCWEB_LOG_DEBUG << "variant type: " << arg_code
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700338 << " appending variant of type: " << contained_type;
339 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
340 contained_type.c_str());
341 if (r < 0) {
342 return r;
343 }
344
345 r = convert_json_to_dbus(m, contained_type, input_json);
346 if (r < 0) {
347 return r;
348 }
349
350 r = sd_bus_message_close_container(m);
351 if (r < 0) {
352 return r;
353 }
354
355 } else if (boost::starts_with(arg_code, "(") &&
356 boost::ends_with(arg_code, ")")) {
357 std::string contained_type = arg_code.substr(1, arg_code.size() - 1);
358 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
359 contained_type.c_str());
360 nlohmann::json::const_iterator it = j->begin();
361 for (const std::string &arg_code : dbus_arg_split(arg_type)) {
362 if (it == j->end()) {
363 return -1;
364 }
365 r = convert_json_to_dbus(m, arg_code, *it);
366 if (r < 0) {
367 return r;
368 }
369 it++;
370 }
371 r = sd_bus_message_close_container(m);
372 } else if (boost::starts_with(arg_code, "{") &&
373 boost::ends_with(arg_code, "}")) {
374 std::string contained_type = arg_code.substr(1, arg_code.size() - 1);
375 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
376 contained_type.c_str());
377 std::vector<std::string> codes = dbus_arg_split(contained_type);
378 if (codes.size() != 2) {
379 return -1;
380 }
381 const std::string &key_type = codes[0];
382 const std::string &value_type = codes[1];
383 for (auto it : j->items()) {
384 r = convert_json_to_dbus(m, key_type, it.key());
385 if (r < 0) {
386 return r;
387 };
388
389 r = convert_json_to_dbus(m, value_type, it.value());
390 if (r < 0) {
391 return r;
392 }
393 }
394 r = sd_bus_message_close_container(m);
395 } else {
396 return -2;
397 }
398 if (r < 0) {
399 return r;
400 }
401
402 if (arg_types.size() > 1) {
403 j_it++;
404 }
405 }
406}
407
408void find_action_on_interface(std::shared_ptr<InProgressActionData> transaction,
409 const std::string &connectionName) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700410 BMCWEB_LOG_DEBUG << "find_action_on_interface for connection "
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700411 << connectionName;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700412 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700413 [
414 transaction, connectionName{std::string(connectionName)}
415 ](const boost::system::error_code ec, const std::string &introspect_xml) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700416 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700417 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700418 BMCWEB_LOG_ERROR << "Introspect call failed with error: "
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700419 << ec.message() << " on process: " << connectionName
420 << "\n";
421 } else {
422 tinyxml2::XMLDocument doc;
423
424 doc.Parse(introspect_xml.c_str());
425 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
426 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700427 BMCWEB_LOG_ERROR << "XML document failed to parse " << connectionName
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700428 << "\n";
429
430 } else {
431 tinyxml2::XMLElement *interface_node =
432 pRoot->FirstChildElement("interface");
433 while (interface_node != nullptr) {
434 std::string this_interface_name =
435 interface_node->Attribute("name");
436 tinyxml2::XMLElement *method_node =
437 interface_node->FirstChildElement("method");
438 while (method_node != nullptr) {
439 std::string this_method_name = method_node->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700440 BMCWEB_LOG_DEBUG << "Found method: " << this_method_name;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700441 if (this_method_name == transaction->method_name) {
442 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700443 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700444 connectionName.c_str(), transaction->path.c_str(),
445 this_interface_name.c_str(),
446 transaction->method_name.c_str());
447
448 tinyxml2::XMLElement *argument_node =
449 method_node->FirstChildElement("arg");
450
451 nlohmann::json::const_iterator arg_it =
452 transaction->arguments.begin();
453
454 while (argument_node != nullptr) {
455 std::string arg_direction =
456 argument_node->Attribute("direction");
457 if (arg_direction == "in") {
458 std::string arg_type = argument_node->Attribute("type");
459 if (arg_it == transaction->arguments.end()) {
460 transaction->setErrorStatus();
461 return;
462 }
463 if (convert_json_to_dbus(m.get(), arg_type, *arg_it) <
464 0) {
465 transaction->setErrorStatus();
466 return;
467 }
468
469 arg_it++;
470 }
471 argument_node = method_node->NextSiblingElement("arg");
472 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700473 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700474 m, [transaction](boost::system::error_code ec,
475 sdbusplus::message::message &m) {
476 if (ec) {
477 transaction->setErrorStatus();
478 return;
479 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700480 transaction->res.jsonValue = {{"status", "ok"},
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700481 {"message", "200 OK"},
482 {"data", nullptr}};
483 });
484 break;
485 }
486 method_node = method_node->NextSiblingElement("method");
487 }
488 interface_node = interface_node->NextSiblingElement("interface");
489 }
490 }
491 }
492 },
493 connectionName, transaction->path, "org.freedesktop.DBus.Introspectable",
494 "Introspect");
495}
496
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700497void handle_action(const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700498 const std::string &object_path,
499 const std::string &method_name) {
500 nlohmann::json request_dbus_data =
501 nlohmann::json::parse(req.body, nullptr, false);
502
503 if (request_dbus_data.is_discarded()) {
504 res.result(boost::beast::http::status::bad_request);
505 res.end();
506 return;
507 }
508 if (!request_dbus_data.is_array()) {
509 res.result(boost::beast::http::status::bad_request);
510 res.end();
511 return;
512 }
513 auto transaction = std::make_shared<InProgressActionData>(res);
514
515 transaction->path = object_path;
516 transaction->method_name = method_name;
517 transaction->arguments = std::move(request_dbus_data);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700518 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700519 [transaction](
520 const boost::system::error_code ec,
521 const std::vector<std::pair<std::string, std::vector<std::string>>>
522 &interface_names) {
523 if (ec || interface_names.size() <= 0) {
524 transaction->setErrorStatus();
525 return;
526 }
527
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700528 BMCWEB_LOG_DEBUG << "GetObject returned objects "
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700529 << interface_names.size();
530
531 for (const std::pair<std::string, std::vector<std::string>> &object :
532 interface_names) {
533 find_action_on_interface(transaction, object.first);
534 }
535 },
536 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
537 "xyz.openbmc_project.ObjectMapper", "GetObject", object_path,
538 std::array<std::string, 0>());
539}
540
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700541void handle_list(crow::Response &res, const std::string &objectPath) {
542 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700543 [&res](const boost::system::error_code ec,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700544 std::vector<std::string> &objectPaths) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700545 if (ec) {
546 res.result(boost::beast::http::status::internal_server_error);
547 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700548 res.jsonValue = {{"status", "ok"},
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700549 {"message", "200 OK"},
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700550 {"data", std::move(objectPaths)}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700551 }
552 res.end();
553 },
554 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700555 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700556 static_cast<int32_t>(99), std::array<std::string, 0>());
557}
558
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700559void handle_enumerate(crow::Response &res, const std::string &objectPath) {
560 crow::connections::systemBus->async_method_call(
561 [&res, objectPath{std::string(objectPath)} ](
Ed Tanous64530012018-02-06 17:08:16 -0800562 const boost::system::error_code ec,
563 const GetSubTreeType &object_names) {
564 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700565 res.jsonValue = {{"message", "200 OK"},
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700566 {"status", "ok"},
567 {"data", nlohmann::json::object()}};
568
Ed Tanous64530012018-02-06 17:08:16 -0800569 res.end();
570 return;
571 }
572
573 boost::container::flat_set<std::string> connections;
574
575 for (const auto &object : object_names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700576 for (const auto &Connection : object.second) {
577 connections.insert(Connection.first);
Ed Tanous64530012018-02-06 17:08:16 -0800578 }
579 }
580
581 if (connections.size() <= 0) {
Ed Tanouse0d918b2018-03-27 17:41:04 -0700582 res.result(boost::beast::http::status::not_found);
Ed Tanous64530012018-02-06 17:08:16 -0800583 res.end();
584 return;
585 }
586 auto transaction =
587 std::make_shared<nlohmann::json>(nlohmann::json::object());
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700588 for (const std::string &Connection : connections) {
589 getManagedObjectsForEnumerate(objectPath, Connection, res,
590 transaction);
Ed Tanous64530012018-02-06 17:08:16 -0800591 }
Ed Tanous64530012018-02-06 17:08:16 -0800592 },
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700593 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700594 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, (int32_t)0,
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700595 std::array<std::string, 0>());
Ed Tanous64530012018-02-06 17:08:16 -0800596}
Ed Tanous911ac312017-08-15 09:37:42 -0700597
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700598void handle_get(crow::Response &res, std::string &object_path,
599 std::string &dest_property) {
600 BMCWEB_LOG_DEBUG << "handle_get: " << object_path << " prop:" << dest_property;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700601 std::shared_ptr<std::string> property_name =
602 std::make_shared<std::string>(dest_property);
603 using GetObjectType =
604 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700605 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700606 [&res, object_path, property_name](const boost::system::error_code ec,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700607 const GetObjectType &object_names) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700608 if (ec || object_names.size() <= 0) {
609 res.result(boost::beast::http::status::not_found);
610 res.end();
611 return;
612 }
613 std::shared_ptr<nlohmann::json> response =
614 std::make_shared<nlohmann::json>(nlohmann::json::object());
615 // The mapper should never give us an empty interface names list, but
616 // check anyway
617 for (const std::pair<std::string, std::vector<std::string>> connection :
618 object_names) {
619 const std::vector<std::string> &interfaceNames = connection.second;
620
621 if (interfaceNames.size() <= 0) {
622 res.result(boost::beast::http::status::not_found);
623 res.end();
624 return;
625 }
626
627 for (const std::string &interface : interfaceNames) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700628 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700629 [&res, response, property_name](
630 const boost::system::error_code ec,
631 const std::vector<std::pair<
632 std::string, DbusRestVariantType>> &properties) {
633 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700634 BMCWEB_LOG_ERROR << "Bad dbus request error: " << ec;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700635 } else {
636 for (const std::pair<std::string, DbusRestVariantType>
637 &property : properties) {
638 // if property name is empty, or matches our search query,
639 // add it to the response json
640
641 if (property_name->empty()) {
642 mapbox::util::apply_visitor(
643 [&response, &property](auto &&val) {
644 (*response)[property.first] = val;
645 },
646 property.second);
647 } else if (property.first == *property_name) {
648 mapbox::util::apply_visitor(
649 [&response](auto &&val) { (*response) = val; },
650 property.second);
651 }
652 }
653 }
654 if (response.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700655 res.jsonValue = {{"status", "ok"},
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700656 {"message", "200 OK"},
657 {"data", *response}};
658
659 res.end();
660 }
661 },
662 connection.first, object_path,
663 "org.freedesktop.DBus.Properties", "GetAll", interface);
664 }
665 }
666 },
667 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
668 "xyz.openbmc_project.ObjectMapper", "GetObject", object_path,
669 std::array<std::string, 0>());
670}
671
672struct AsyncPutRequest {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700673 AsyncPutRequest(crow::Response &res) : res(res) {
674 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700675 {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}};
676 }
677 ~AsyncPutRequest() {
678 if (res.result() == boost::beast::http::status::internal_server_error) {
679 // Reset the json object to clear out any data that made it in before the
680 // error happened
681 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700682 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700683 }
684
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700685 if (res.jsonValue.empty()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700686 res.result(boost::beast::http::status::forbidden);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700687 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700688 {"status", "error"},
689 {"message", "403 Forbidden"},
690 {"data",
691 {{"message",
692 "The specified property cannot be created: " + propertyName}}}};
693 }
694
695 res.end();
696 }
697
698 void setErrorStatus() {
699 res.result(boost::beast::http::status::internal_server_error);
700 }
701
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700702 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700703 std::string objectPath;
704 std::string propertyName;
705 nlohmann::json propertyValue;
706};
707
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700708void handle_put(const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700709 const std::string &objectPath,
710 const std::string &destProperty) {
711 nlohmann::json request_dbus_data =
712 nlohmann::json::parse(req.body, nullptr, false);
713
714 if (request_dbus_data.is_discarded()) {
715 res.result(boost::beast::http::status::bad_request);
716 res.end();
717 return;
718 }
719
720 nlohmann::json::const_iterator property_it = request_dbus_data.find("data");
721 if (property_it == request_dbus_data.end()) {
722 res.result(boost::beast::http::status::bad_request);
723 res.end();
724 return;
725 }
726 const nlohmann::json &propertySetValue = *property_it;
727 auto transaction = std::make_shared<AsyncPutRequest>(res);
728 transaction->objectPath = objectPath;
729 transaction->propertyName = destProperty;
730 transaction->propertyValue = propertySetValue;
731
732 using GetObjectType =
733 std::vector<std::pair<std::string, std::vector<std::string>>>;
734
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700735 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700736 [transaction](const boost::system::error_code ec,
737 const GetObjectType &object_names) {
738 if (!ec && object_names.size() <= 0) {
739 transaction->res.result(boost::beast::http::status::not_found);
740 return;
741 }
742
743 for (const std::pair<std::string, std::vector<std::string>> connection :
744 object_names) {
745 const std::string &connectionName = connection.first;
746
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700747 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700748 [ connectionName{std::string(connectionName)}, transaction ](
749 const boost::system::error_code ec,
750 const std::string &introspectXml) {
751 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700752 BMCWEB_LOG_ERROR
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700753 << "Introspect call failed with error: " << ec.message()
754 << " on process: " << connectionName;
755 transaction->setErrorStatus();
756 return;
757 }
758 tinyxml2::XMLDocument doc;
759
760 doc.Parse(introspectXml.c_str());
761 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
762 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700763 BMCWEB_LOG_ERROR << "XML document failed to parse: "
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700764 << introspectXml;
765 transaction->setErrorStatus();
766 return;
767 }
768 tinyxml2::XMLElement *ifaceNode =
769 pRoot->FirstChildElement("interface");
770 while (ifaceNode != nullptr) {
771 const char *interfaceName = ifaceNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700772 BMCWEB_LOG_DEBUG << "found interface " << interfaceName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700773 tinyxml2::XMLElement *propNode =
774 ifaceNode->FirstChildElement("property");
775 while (propNode != nullptr) {
776 const char *propertyName = propNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700777 BMCWEB_LOG_DEBUG << "Found property " << propertyName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700778 if (propertyName == transaction->propertyName) {
779 const char *argType = propNode->Attribute("type");
780 if (argType != nullptr) {
781 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700782 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700783 connectionName.c_str(),
784 transaction->objectPath.c_str(),
785 "org.freedesktop.DBus.Properties", "Set");
786 m.append(interfaceName, transaction->propertyName);
787 int r = sd_bus_message_open_container(
788 m.get(), SD_BUS_TYPE_VARIANT, argType);
789 if (r < 0) {
790 transaction->setErrorStatus();
791 return;
792 }
793 r = convert_json_to_dbus(m.get(), argType,
794 transaction->propertyValue);
795 if (r < 0) {
796 transaction->setErrorStatus();
797 return;
798 }
799 r = sd_bus_message_close_container(m.get());
800 if (r < 0) {
801 transaction->setErrorStatus();
802 return;
803 }
804
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700805 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700806 m, [transaction](boost::system::error_code ec,
807 sdbusplus::message::message &m) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700808 BMCWEB_LOG_DEBUG << "sent";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700809 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700810 transaction->res.jsonValue["status"] = "error";
811 transaction->res.jsonValue["message"] =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700812 ec.message();
813 }
814 });
815 }
816 }
817 propNode = propNode->NextSiblingElement("property");
818 }
819 ifaceNode = ifaceNode->NextSiblingElement("interface");
820 }
821 },
822 connectionName, transaction->objectPath,
823 "org.freedesktop.DBus.Introspectable", "Introspect");
824 }
825 },
826 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
827 "xyz.openbmc_project.ObjectMapper", "GetObject", transaction->objectPath,
828 std::array<std::string, 0>());
829}
830
Ed Tanous911ac312017-08-15 09:37:42 -0700831template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700832void requestRoutes(Crow<Middlewares...> &app) {
833 BMCWEB_ROUTE(app, "/bus/")
834 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
835 res.jsonValue = {{"busses", {{{"name", "system"}}}}, {"status", "ok"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700836 });
Ed Tanous911ac312017-08-15 09:37:42 -0700837
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700838 BMCWEB_ROUTE(app, "/bus/system/")
839 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
840
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700841 auto myCallback = [&res](const boost::system::error_code ec,
842 std::vector<std::string> &names) {
843 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700844 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700845 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700846 } else {
847 std::sort(names.begin(), names.end());
848 nlohmann::json j{{"status", "ok"}};
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700849 auto &objectsSub = j["objects"];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700850 for (auto &name : names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700851 objectsSub.push_back({{"name", name}});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700852 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700853 res.jsonValue = std::move(j);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700854 }
855 res.end();
856 };
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700857 crow::connections::systemBus->async_method_call(
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700858 std::move(myCallback), "org.freedesktop.DBus", "/",
859 "org.freedesktop.DBus", "ListNames");
Ed Tanous911ac312017-08-15 09:37:42 -0700860 });
861
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700862 BMCWEB_ROUTE(app, "/list/")
863 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700864 handle_list(res, "/");
Ed Tanousba9f9a62017-10-11 16:40:35 -0700865 });
866
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700867 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700868 .methods("GET"_method, "PUT"_method,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700869 "POST"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700870 const std::string &path) {
Ed Tanousa4e18f22018-04-27 10:25:29 -0700871 std::string object_path = "/xyz/" + path;
872
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700873 // Trim any trailing "/" at the end
874 if (boost::ends_with(object_path, "/")) {
875 object_path.pop_back();
876 }
877
878 // If accessing a single attribute, fill in and update object_path,
879 // otherwise leave dest_property blank
880 std::string dest_property = "";
881 const char *attr_seperator = "/attr/";
Ed Tanousa4e18f22018-04-27 10:25:29 -0700882 size_t attr_position = path.find(attr_seperator);
883 if (attr_position != path.npos) {
shiyilei00b92f72017-11-12 16:21:16 +0800884 object_path = "/xyz/" + path.substr(0, attr_position);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700885 dest_property = path.substr(attr_position + strlen(attr_seperator),
Ed Tanousa4e18f22018-04-27 10:25:29 -0700886 path.length());
shiyilei00b92f72017-11-12 16:21:16 +0800887 }
888
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700889 if (req.method() == "POST"_method) {
890 constexpr const char *action_seperator = "/action/";
891 size_t action_position = path.find(action_seperator);
892 if (action_position != path.npos) {
893 object_path = "/xyz/" + path.substr(0, action_position);
894 std::string post_property = path.substr(
895 (action_position + strlen(action_seperator)), path.length());
896 handle_action(req, res, object_path, post_property);
897 return;
898 }
899 } else if (req.method() == "GET"_method) {
900 if (boost::ends_with(object_path, "/enumerate")) {
901 object_path.erase(object_path.end() - 10, object_path.end());
902 handle_enumerate(res, object_path);
903 } else if (boost::ends_with(object_path, "/list")) {
904 object_path.erase(object_path.end() - 5, object_path.end());
905 handle_list(res, object_path);
906 } else {
907 handle_get(res, object_path, dest_property);
908 }
909 return;
910 } else if (req.method() == "PUT"_method) {
911 handle_put(req, res, object_path, dest_property);
Ed Tanous64530012018-02-06 17:08:16 -0800912 return;
913 }
914
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700915 res.result(boost::beast::http::status::method_not_allowed);
916 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700917 });
shiyilei00b92f72017-11-12 16:21:16 +0800918
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700919 BMCWEB_ROUTE(app, "/bus/system/<str>/")
920 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
921 const std::string &Connection) {
Ed Tanous64530012018-02-06 17:08:16 -0800922 std::shared_ptr<nlohmann::json> transaction;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700923 introspectObjects(res, Connection, "/", transaction);
Ed Tanous911ac312017-08-15 09:37:42 -0700924 });
925
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700926 BMCWEB_ROUTE(app, "/download/dump/<str>/")
927 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700928 const std::string &dumpId) {
929 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
930 if (!std::regex_match(dumpId, validFilename)) {
931 res.result(boost::beast::http::status::not_found);
932 res.end();
933 return;
934 }
935 std::experimental::filesystem::path loc(
936 "/var/lib/phosphor-debug-collector/dumps");
937
938 loc += dumpId;
939
940 if (!std::experimental::filesystem::exists(loc) ||
941 !std::experimental::filesystem::is_directory(loc)) {
942 res.result(boost::beast::http::status::not_found);
943 res.end();
944 return;
945 }
946 std::experimental::filesystem::directory_iterator files(loc);
947 for (auto &file : files) {
948 std::ifstream readFile(file.path());
949 if (readFile.good()) {
950 continue;
951 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700952 res.addHeader("Content-Type", "application/octet-stream");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700953 res.body() = {std::istreambuf_iterator<char>(readFile),
954 std::istreambuf_iterator<char>()};
955 res.end();
956 }
957 res.result(boost::beast::http::status::not_found);
958 res.end();
959 return;
960 });
961
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700962 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
963 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
964 const std::string &processName,
965 const std::string &requestedPath) {
Ed Tanous911ac312017-08-15 09:37:42 -0700966 std::vector<std::string> strs;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700967 boost::split(strs, requestedPath, boost::is_any_of("/"));
968 std::string objectPath;
969 std::string interfaceName;
970 std::string methodName;
Ed Tanous911ac312017-08-15 09:37:42 -0700971 auto it = strs.begin();
972 if (it == strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700973 objectPath = "/";
Ed Tanous911ac312017-08-15 09:37:42 -0700974 }
975 while (it != strs.end()) {
976 // Check if segment contains ".". If it does, it must be an
977 // interface
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700978 if (it->find(".") != std::string::npos) {
Ed Tanous911ac312017-08-15 09:37:42 -0700979 break;
Ed Tanousba9f9a62017-10-11 16:40:35 -0700980 // THis check is neccesary as the trailing slash gets parsed as
Ed Tanous64530012018-02-06 17:08:16 -0800981 // part of our <path> specifier above, which causes the normal
982 // trailing backslash redirector to fail.
Ed Tanous911ac312017-08-15 09:37:42 -0700983 } else if (!it->empty()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700984 objectPath += "/" + *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700985 }
986 it++;
987 }
988 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700989 interfaceName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700990 it++;
991
992 // after interface, we might have a method name
993 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700994 methodName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700995 it++;
996 }
997 }
998 if (it != strs.end()) {
999 // if there is more levels past the method name, something went
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001000 // wrong, return not found
Ed Tanouse0d918b2018-03-27 17:41:04 -07001001 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -07001002 res.end();
1003 return;
1004 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001005 if (interfaceName.empty()) {
1006 crow::connections::systemBus->async_method_call(
1007 [&, processName, objectPath](
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001008 const boost::system::error_code ec,
1009 const std::string &introspect_xml) {
Ed Tanous911ac312017-08-15 09:37:42 -07001010 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001011 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001012 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001013 << " on process: " << processName
1014 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001015
1016 } else {
1017 tinyxml2::XMLDocument doc;
1018
1019 doc.Parse(introspect_xml.c_str());
1020 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1021 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001022 BMCWEB_LOG_ERROR << "XML document failed to parse "
1023 << processName << " " << objectPath
1024 << "\n";
1025 res.jsonValue = {{"status", "XML parse error"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -07001026 res.result(
1027 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001028 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001029 nlohmann::json interfacesArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001030 tinyxml2::XMLElement *interface =
1031 pRoot->FirstChildElement("interface");
1032
1033 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001034 std::string ifaceName = interface->Attribute("name");
1035 interfacesArray.push_back({{"name", ifaceName}});
Ed Tanous911ac312017-08-15 09:37:42 -07001036
1037 interface = interface->NextSiblingElement("interface");
1038 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001039 res.jsonValue = {{"status", "ok"},
1040 {"bus_name", processName},
1041 {"interfaces", interfacesArray},
1042 {"object_path", objectPath}};
Ed Tanous911ac312017-08-15 09:37:42 -07001043 }
1044 }
1045 res.end();
1046 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001047 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001048 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001049 } else {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001050 crow::connections::systemBus->async_method_call(
Ed Tanous911ac312017-08-15 09:37:42 -07001051 [
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001052 &, processName, objectPath,
1053 interface_name{std::move(interfaceName)}
Ed Tanous911ac312017-08-15 09:37:42 -07001054 ](const boost::system::error_code ec,
1055 const std::string &introspect_xml) {
1056 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001057 BMCWEB_LOG_ERROR
Ed Tanous911ac312017-08-15 09:37:42 -07001058 << "Introspect call failed with error: " << ec.message()
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001059 << " on process: " << processName
1060 << " path: " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -07001061
1062 } else {
1063 tinyxml2::XMLDocument doc;
1064
1065 doc.Parse(introspect_xml.c_str());
1066 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1067 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001068 BMCWEB_LOG_ERROR << "XML document failed to parse "
1069 << processName << " " << objectPath
1070 << "\n";
Ed Tanouse0d918b2018-03-27 17:41:04 -07001071 res.result(
1072 boost::beast::http::status::internal_server_error);
Ed Tanous911ac312017-08-15 09:37:42 -07001073
1074 } else {
1075 tinyxml2::XMLElement *node =
1076 pRoot->FirstChildElement("node");
1077
1078 // if we know we're the only call, build the json directly
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001079 nlohmann::json methodsArray = nlohmann::json::array();
1080 nlohmann::json signalsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001081 tinyxml2::XMLElement *interface =
1082 pRoot->FirstChildElement("interface");
1083
1084 while (interface != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001085 std::string ifaceName = interface->Attribute("name");
Ed Tanous911ac312017-08-15 09:37:42 -07001086
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001087 if (ifaceName == interfaceName) {
Ed Tanous911ac312017-08-15 09:37:42 -07001088 tinyxml2::XMLElement *methods =
1089 interface->FirstChildElement("method");
1090 while (methods != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001091 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001092 tinyxml2::XMLElement *arg =
1093 methods->FirstChildElement("arg");
1094 while (arg != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001095 argsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001096 {{"name", arg->Attribute("name")},
1097 {"type", arg->Attribute("type")},
1098 {"direction", arg->Attribute("direction")}});
1099 arg = arg->NextSiblingElement("arg");
1100 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001101 methodsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001102 {{"name", methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001103 {"uri", "/bus/system/" + processName +
1104 objectPath + "/" + interfaceName +
Ed Tanous64530012018-02-06 17:08:16 -08001105 "/" + methods->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001106 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001107 methods = methods->NextSiblingElement("method");
1108 }
1109 tinyxml2::XMLElement *signals =
1110 interface->FirstChildElement("signal");
1111 while (signals != nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001112 nlohmann::json argsArray = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -07001113
1114 tinyxml2::XMLElement *arg =
1115 signals->FirstChildElement("arg");
1116 while (arg != nullptr) {
1117 std::string name = arg->Attribute("name");
1118 std::string type = arg->Attribute("type");
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001119 argsArray.push_back({
Ed Tanous64530012018-02-06 17:08:16 -08001120 {"name", name},
1121 {"type", type},
Ed Tanous911ac312017-08-15 09:37:42 -07001122 });
1123 arg = arg->NextSiblingElement("arg");
1124 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001125 signalsArray.push_back(
Ed Tanous911ac312017-08-15 09:37:42 -07001126 {{"name", signals->Attribute("name")},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001127 {"args", argsArray}});
Ed Tanous911ac312017-08-15 09:37:42 -07001128 signals = signals->NextSiblingElement("signal");
1129 }
1130
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001131 res.jsonValue = {
Ed Tanous911ac312017-08-15 09:37:42 -07001132 {"status", "ok"},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001133 {"bus_name", processName},
1134 {"interface", interfaceName},
1135 {"methods", methodsArray},
1136 {"object_path", objectPath},
Ed Tanous911ac312017-08-15 09:37:42 -07001137 {"properties", nlohmann::json::object()},
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001138 {"signals", signalsArray}};
Ed Tanous911ac312017-08-15 09:37:42 -07001139
Ed Tanous911ac312017-08-15 09:37:42 -07001140 break;
1141 }
1142
1143 interface = interface->NextSiblingElement("interface");
1144 }
1145 if (interface == nullptr) {
1146 // if we got to the end of the list and never found a
1147 // match, throw 404
Ed Tanouse0d918b2018-03-27 17:41:04 -07001148 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -07001149 }
1150 }
1151 }
1152 res.end();
1153 },
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001154 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanousaa2e59c2018-04-12 12:17:20 -07001155 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -07001156 }
Ed Tanous911ac312017-08-15 09:37:42 -07001157 });
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001158}
Ed Tanous911ac312017-08-15 09:37:42 -07001159} // namespace openbmc_mapper
1160} // namespace crow