blob: ad6104be7a6c7517d42069452b25dc15c761d8ca [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"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -070056 {"bus_name", processName},
57 {"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 Tanous8ceb2ec2018-08-13 11:11:56 -0700201 << " to type: " << arg_type;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700202 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 Tanous8ceb2ec2018-08-13 11:11:56 -0700338 << " appending variant of type: " << contained_type;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700339 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 Tanous8ceb2ec2018-08-13 11:11:56 -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 Tanous8ceb2ec2018-08-13 11:11:56 -0700419 << ec.message() << " on process: " << connectionName
420 << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700421 } 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 Tanous8ceb2ec2018-08-13 11:11:56 -0700427 BMCWEB_LOG_ERROR << "XML document failed to parse "
428 << connectionName << "\n";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700429
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 Tanous8ceb2ec2018-08-13 11:11:56 -0700481 {"message", "200 OK"},
482 {"data", nullptr}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700483 });
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 Tanous8ceb2ec2018-08-13 11:11:56 -0700529 << interface_names.size();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700530
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 Tanous8ceb2ec2018-08-13 11:11:56 -0700549 {"message", "200 OK"},
550 {"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 Tanous8ceb2ec2018-08-13 11:11:56 -0700566 {"status", "ok"},
567 {"data", nlohmann::json::object()}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700568
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) {
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700600 BMCWEB_LOG_DEBUG << "handle_get: " << object_path
601 << " prop:" << dest_property;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700602 std::shared_ptr<std::string> property_name =
603 std::make_shared<std::string>(dest_property);
604 using GetObjectType =
605 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700606 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700607 [&res, object_path, property_name](const boost::system::error_code ec,
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700608 const GetObjectType &object_names) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700609 if (ec || object_names.size() <= 0) {
610 res.result(boost::beast::http::status::not_found);
611 res.end();
612 return;
613 }
614 std::shared_ptr<nlohmann::json> response =
615 std::make_shared<nlohmann::json>(nlohmann::json::object());
616 // The mapper should never give us an empty interface names list, but
617 // check anyway
618 for (const std::pair<std::string, std::vector<std::string>> connection :
619 object_names) {
620 const std::vector<std::string> &interfaceNames = connection.second;
621
622 if (interfaceNames.size() <= 0) {
623 res.result(boost::beast::http::status::not_found);
624 res.end();
625 return;
626 }
627
628 for (const std::string &interface : interfaceNames) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700629 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700630 [&res, response, property_name](
631 const boost::system::error_code ec,
632 const std::vector<std::pair<
633 std::string, DbusRestVariantType>> &properties) {
634 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700635 BMCWEB_LOG_ERROR << "Bad dbus request error: " << ec;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700636 } else {
637 for (const std::pair<std::string, DbusRestVariantType>
638 &property : properties) {
639 // if property name is empty, or matches our search query,
640 // add it to the response json
641
642 if (property_name->empty()) {
643 mapbox::util::apply_visitor(
644 [&response, &property](auto &&val) {
645 (*response)[property.first] = val;
646 },
647 property.second);
648 } else if (property.first == *property_name) {
649 mapbox::util::apply_visitor(
650 [&response](auto &&val) { (*response) = val; },
651 property.second);
652 }
653 }
654 }
655 if (response.use_count() == 1) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700656 res.jsonValue = {{"status", "ok"},
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700657 {"message", "200 OK"},
658 {"data", *response}};
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700659
660 res.end();
661 }
662 },
663 connection.first, object_path,
664 "org.freedesktop.DBus.Properties", "GetAll", interface);
665 }
666 }
667 },
668 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
669 "xyz.openbmc_project.ObjectMapper", "GetObject", object_path,
670 std::array<std::string, 0>());
671}
672
673struct AsyncPutRequest {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700674 AsyncPutRequest(crow::Response &res) : res(res) {
675 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700676 {"status", "ok"}, {"message", "200 OK"}, {"data", nullptr}};
677 }
678 ~AsyncPutRequest() {
679 if (res.result() == boost::beast::http::status::internal_server_error) {
680 // Reset the json object to clear out any data that made it in before the
681 // error happened
682 // todo(ed) handle error condition with proper code
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700683 res.jsonValue = nlohmann::json::object();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700684 }
685
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700686 if (res.jsonValue.empty()) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700687 res.result(boost::beast::http::status::forbidden);
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700688 res.jsonValue = {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700689 {"status", "error"},
690 {"message", "403 Forbidden"},
691 {"data",
692 {{"message",
693 "The specified property cannot be created: " + propertyName}}}};
694 }
695
696 res.end();
697 }
698
699 void setErrorStatus() {
700 res.result(boost::beast::http::status::internal_server_error);
701 }
702
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700703 crow::Response &res;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700704 std::string objectPath;
705 std::string propertyName;
706 nlohmann::json propertyValue;
707};
708
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700709void handle_put(const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700710 const std::string &objectPath,
711 const std::string &destProperty) {
712 nlohmann::json request_dbus_data =
713 nlohmann::json::parse(req.body, nullptr, false);
714
715 if (request_dbus_data.is_discarded()) {
716 res.result(boost::beast::http::status::bad_request);
717 res.end();
718 return;
719 }
720
721 nlohmann::json::const_iterator property_it = request_dbus_data.find("data");
722 if (property_it == request_dbus_data.end()) {
723 res.result(boost::beast::http::status::bad_request);
724 res.end();
725 return;
726 }
727 const nlohmann::json &propertySetValue = *property_it;
728 auto transaction = std::make_shared<AsyncPutRequest>(res);
729 transaction->objectPath = objectPath;
730 transaction->propertyName = destProperty;
731 transaction->propertyValue = propertySetValue;
732
733 using GetObjectType =
734 std::vector<std::pair<std::string, std::vector<std::string>>>;
735
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700736 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700737 [transaction](const boost::system::error_code ec,
738 const GetObjectType &object_names) {
739 if (!ec && object_names.size() <= 0) {
740 transaction->res.result(boost::beast::http::status::not_found);
741 return;
742 }
743
744 for (const std::pair<std::string, std::vector<std::string>> connection :
745 object_names) {
746 const std::string &connectionName = connection.first;
747
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700748 crow::connections::systemBus->async_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700749 [ connectionName{std::string(connectionName)}, transaction ](
750 const boost::system::error_code ec,
751 const std::string &introspectXml) {
752 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700753 BMCWEB_LOG_ERROR
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700754 << "Introspect call failed with error: " << ec.message()
755 << " on process: " << connectionName;
756 transaction->setErrorStatus();
757 return;
758 }
759 tinyxml2::XMLDocument doc;
760
761 doc.Parse(introspectXml.c_str());
762 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
763 if (pRoot == nullptr) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700764 BMCWEB_LOG_ERROR << "XML document failed to parse: "
Ed Tanous8ceb2ec2018-08-13 11:11:56 -0700765 << introspectXml;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700766 transaction->setErrorStatus();
767 return;
768 }
769 tinyxml2::XMLElement *ifaceNode =
770 pRoot->FirstChildElement("interface");
771 while (ifaceNode != nullptr) {
772 const char *interfaceName = ifaceNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700773 BMCWEB_LOG_DEBUG << "found interface " << interfaceName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700774 tinyxml2::XMLElement *propNode =
775 ifaceNode->FirstChildElement("property");
776 while (propNode != nullptr) {
777 const char *propertyName = propNode->Attribute("name");
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700778 BMCWEB_LOG_DEBUG << "Found property " << propertyName;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700779 if (propertyName == transaction->propertyName) {
780 const char *argType = propNode->Attribute("type");
781 if (argType != nullptr) {
782 sdbusplus::message::message m =
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700783 crow::connections::systemBus->new_method_call(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700784 connectionName.c_str(),
785 transaction->objectPath.c_str(),
786 "org.freedesktop.DBus.Properties", "Set");
787 m.append(interfaceName, transaction->propertyName);
788 int r = sd_bus_message_open_container(
789 m.get(), SD_BUS_TYPE_VARIANT, argType);
790 if (r < 0) {
791 transaction->setErrorStatus();
792 return;
793 }
794 r = convert_json_to_dbus(m.get(), argType,
795 transaction->propertyValue);
796 if (r < 0) {
797 transaction->setErrorStatus();
798 return;
799 }
800 r = sd_bus_message_close_container(m.get());
801 if (r < 0) {
802 transaction->setErrorStatus();
803 return;
804 }
805
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700806 crow::connections::systemBus->async_send(
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700807 m, [transaction](boost::system::error_code ec,
808 sdbusplus::message::message &m) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700809 BMCWEB_LOG_DEBUG << "sent";
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700810 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700811 transaction->res.jsonValue["status"] = "error";
812 transaction->res.jsonValue["message"] =
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700813 ec.message();
814 }
815 });
816 }
817 }
818 propNode = propNode->NextSiblingElement("property");
819 }
820 ifaceNode = ifaceNode->NextSiblingElement("interface");
821 }
822 },
823 connectionName, transaction->objectPath,
824 "org.freedesktop.DBus.Introspectable", "Introspect");
825 }
826 },
827 "xyz.openbmc_project.ObjectMapper", "/xyz/openbmc_project/object_mapper",
828 "xyz.openbmc_project.ObjectMapper", "GetObject", transaction->objectPath,
829 std::array<std::string, 0>());
830}
831
Ed Tanous911ac312017-08-15 09:37:42 -0700832template <typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700833void requestRoutes(Crow<Middlewares...> &app) {
834 BMCWEB_ROUTE(app, "/bus/")
835 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
836 res.jsonValue = {{"busses", {{{"name", "system"}}}}, {"status", "ok"}};
Ed Tanouse0d918b2018-03-27 17:41:04 -0700837 });
Ed Tanous911ac312017-08-15 09:37:42 -0700838
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700839 BMCWEB_ROUTE(app, "/bus/system/")
840 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
841
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700842 auto myCallback = [&res](const boost::system::error_code ec,
843 std::vector<std::string> &names) {
844 if (ec) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700845 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700846 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700847 } else {
848 std::sort(names.begin(), names.end());
849 nlohmann::json j{{"status", "ok"}};
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700850 auto &objectsSub = j["objects"];
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700851 for (auto &name : names) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700852 objectsSub.push_back({{"name", name}});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700853 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700854 res.jsonValue = std::move(j);
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700855 }
856 res.end();
857 };
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700858 crow::connections::systemBus->async_method_call(
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700859 std::move(myCallback), "org.freedesktop.DBus", "/",
860 "org.freedesktop.DBus", "ListNames");
Ed Tanous911ac312017-08-15 09:37:42 -0700861 });
862
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700863 BMCWEB_ROUTE(app, "/list/")
864 .methods("GET"_method)([](const crow::Request &req, crow::Response &res) {
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700865 handle_list(res, "/");
Ed Tanousba9f9a62017-10-11 16:40:35 -0700866 });
867
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700868 BMCWEB_ROUTE(app, "/xyz/<path>")
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700869 .methods("GET"_method, "PUT"_method,
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700870 "POST"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700871 const std::string &path) {
Ed Tanousa4e18f22018-04-27 10:25:29 -0700872 std::string object_path = "/xyz/" + path;
873
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700874 // Trim any trailing "/" at the end
875 if (boost::ends_with(object_path, "/")) {
876 object_path.pop_back();
877 }
878
879 // If accessing a single attribute, fill in and update object_path,
880 // otherwise leave dest_property blank
881 std::string dest_property = "";
882 const char *attr_seperator = "/attr/";
Ed Tanousa4e18f22018-04-27 10:25:29 -0700883 size_t attr_position = path.find(attr_seperator);
884 if (attr_position != path.npos) {
shiyilei00b92f72017-11-12 16:21:16 +0800885 object_path = "/xyz/" + path.substr(0, attr_position);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700886 dest_property = path.substr(attr_position + strlen(attr_seperator),
Ed Tanousa4e18f22018-04-27 10:25:29 -0700887 path.length());
shiyilei00b92f72017-11-12 16:21:16 +0800888 }
889
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700890 if (req.method() == "POST"_method) {
891 constexpr const char *action_seperator = "/action/";
892 size_t action_position = path.find(action_seperator);
893 if (action_position != path.npos) {
894 object_path = "/xyz/" + path.substr(0, action_position);
895 std::string post_property = path.substr(
896 (action_position + strlen(action_seperator)), path.length());
897 handle_action(req, res, object_path, post_property);
898 return;
899 }
900 } else if (req.method() == "GET"_method) {
901 if (boost::ends_with(object_path, "/enumerate")) {
902 object_path.erase(object_path.end() - 10, object_path.end());
903 handle_enumerate(res, object_path);
904 } else if (boost::ends_with(object_path, "/list")) {
905 object_path.erase(object_path.end() - 5, object_path.end());
906 handle_list(res, object_path);
907 } else {
908 handle_get(res, object_path, dest_property);
909 }
910 return;
911 } else if (req.method() == "PUT"_method) {
912 handle_put(req, res, object_path, dest_property);
Ed Tanous64530012018-02-06 17:08:16 -0800913 return;
914 }
915
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700916 res.result(boost::beast::http::status::method_not_allowed);
917 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700918 });
shiyilei00b92f72017-11-12 16:21:16 +0800919
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700920 BMCWEB_ROUTE(app, "/bus/system/<str>/")
921 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
922 const std::string &Connection) {
Ed Tanous64530012018-02-06 17:08:16 -0800923 std::shared_ptr<nlohmann::json> transaction;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700924 introspectObjects(res, Connection, "/", transaction);
Ed Tanous911ac312017-08-15 09:37:42 -0700925 });
926
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700927 BMCWEB_ROUTE(app, "/download/dump/<str>/")
928 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700929 const std::string &dumpId) {
930 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]+)$");
931 if (!std::regex_match(dumpId, validFilename)) {
932 res.result(boost::beast::http::status::not_found);
933 res.end();
934 return;
935 }
936 std::experimental::filesystem::path loc(
937 "/var/lib/phosphor-debug-collector/dumps");
938
939 loc += dumpId;
940
941 if (!std::experimental::filesystem::exists(loc) ||
942 !std::experimental::filesystem::is_directory(loc)) {
943 res.result(boost::beast::http::status::not_found);
944 res.end();
945 return;
946 }
947 std::experimental::filesystem::directory_iterator files(loc);
948 for (auto &file : files) {
949 std::ifstream readFile(file.path());
950 if (readFile.good()) {
951 continue;
952 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700953 res.addHeader("Content-Type", "application/octet-stream");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700954 res.body() = {std::istreambuf_iterator<char>(readFile),
955 std::istreambuf_iterator<char>()};
956 res.end();
957 }
958 res.result(boost::beast::http::status::not_found);
959 res.end();
960 return;
961 });
962
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700963 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
964 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
965 const std::string &processName,
966 const std::string &requestedPath) {
Ed Tanous911ac312017-08-15 09:37:42 -0700967 std::vector<std::string> strs;
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700968 boost::split(strs, requestedPath, boost::is_any_of("/"));
969 std::string objectPath;
970 std::string interfaceName;
971 std::string methodName;
Ed Tanous911ac312017-08-15 09:37:42 -0700972 auto it = strs.begin();
973 if (it == strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700974 objectPath = "/";
Ed Tanous911ac312017-08-15 09:37:42 -0700975 }
976 while (it != strs.end()) {
977 // Check if segment contains ".". If it does, it must be an
978 // interface
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700979 if (it->find(".") != std::string::npos) {
Ed Tanous911ac312017-08-15 09:37:42 -0700980 break;
Ed Tanousba9f9a62017-10-11 16:40:35 -0700981 // THis check is neccesary as the trailing slash gets parsed as
Ed Tanous64530012018-02-06 17:08:16 -0800982 // part of our <path> specifier above, which causes the normal
983 // trailing backslash redirector to fail.
Ed Tanous911ac312017-08-15 09:37:42 -0700984 } else if (!it->empty()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700985 objectPath += "/" + *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700986 }
987 it++;
988 }
989 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700990 interfaceName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700991 it++;
992
993 // after interface, we might have a method name
994 if (it != strs.end()) {
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700995 methodName = *it;
Ed Tanous911ac312017-08-15 09:37:42 -0700996 it++;
997 }
998 }
999 if (it != strs.end()) {
1000 // if there is more levels past the method name, something went
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001001 // wrong, return not found
Ed Tanouse0d918b2018-03-27 17:41:04 -07001002 res.result(boost::beast::http::status::not_found);
Ed Tanous911ac312017-08-15 09:37:42 -07001003 res.end();
1004 return;
1005 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -07001006 if (interfaceName.empty()) {
1007 crow::connections::systemBus->async_method_call(
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001008 [&, processName, objectPath](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