1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
#include <crow/app.h>
#include <tinyxml2.h>
#include <dbus/connection.hpp>
#include <dbus/endpoint.hpp>
#include <dbus/filter.hpp>
#include <dbus/match.hpp>
#include <dbus/message.hpp>
#include <dbus_singleton.hpp>
namespace crow {
namespace openbmc_mapper {
// TODO(ed) having these as scope globals, and as simple as they are limits the
// ability to queue multiple async operations at once. Being able to register
// "done" callbacks to a queue here that also had a count attached would allow
// multiple requests to be running at once
std::atomic<std::size_t> outstanding_async_calls(0);
nlohmann::json object_paths;
void introspect_objects(crow::response &res, std::string process_name,
std::string path) {
dbus::endpoint introspect_endpoint(
process_name, path, "org.freedesktop.DBus.Introspectable", "Introspect");
outstanding_async_calls++;
crow::connections::system_bus->async_method_call(
[&, process_name{std::move(process_name)}, object_path{std::move(path)} ](
const boost::system::error_code ec,
const std::string &introspect_xml) {
outstanding_async_calls--;
if (ec) {
std::cerr << "Introspect call failed with error: " << ec.message()
<< " on process: " << process_name
<< " path: " << object_path << "\n";
} else {
object_paths.push_back({{"path", object_path}});
tinyxml2::XMLDocument doc;
doc.Parse(introspect_xml.c_str());
tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
if (pRoot == nullptr) {
std::cerr << "XML document failed to parse " << process_name << " "
<< path << "\n";
} else {
tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
while (node != nullptr) {
std::string child_path = node->Attribute("name");
std::string newpath;
if (object_path != "/") {
newpath += object_path;
}
newpath += "/" + child_path;
// intropect the subobjects as well
introspect_objects(res, process_name, newpath);
node = node->NextSiblingElement("node");
}
}
}
// if we're the last outstanding caller, finish the request
if (outstanding_async_calls == 0) {
nlohmann::json j{{"status", "ok"},
{"bus_name", process_name},
{"objects", object_paths}};
res.write(j.dump());
object_paths.clear();
res.end();
}
},
introspect_endpoint);
}
template <typename... Middlewares>
void request_routes(Crow<Middlewares...> &app) {
CROW_ROUTE(app, "/bus/").methods("GET"_method)([](const crow::request &req) {
return nlohmann::json{{"busses", {{{"name", "system"}}}}, {"status", "ok"}};
});
CROW_ROUTE(app, "/bus/system/")
.methods("GET"_method)([](const crow::request &req, crow::response &res) {
crow::connections::system_bus->async_method_call(
[&](const boost::system::error_code ec,
std::vector<std::string> &names) {
std::sort(names.begin(), names.end());
if (ec) {
res.code = 500;
} else {
nlohmann::json j{{"status", "ok"}};
auto &objects_sub = j["objects"];
for (auto &name : names) {
objects_sub.push_back({{"name", name}});
}
res.write(j.dump());
}
res.end();
},
{"org.freedesktop.DBus", "/", "org.freedesktop.DBus", "ListNames"});
});
CROW_ROUTE(app, "/list/")
.methods("GET"_method)([](const crow::request &req, crow::response &res) {
crow::connections::system_bus->async_method_call(
[&](const boost::system::error_code ec,
const std::vector<std::string> &object_paths) {
if (ec) {
res.code = 500;
} else {
nlohmann::json j{{"status", "ok"},
{"message", "200 OK"},
{"data", object_paths}};
res.body = j.dump();
}
res.end();
},
{"xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetSubTreePaths"},
"", static_cast<int32_t>(99), std::array<std::string, 0>());
});
CROW_ROUTE(app, "/xyz/<path>")
.methods("GET"_method)([](const crow::request &req, crow::response &res,
const std::string &path) {
if (outstanding_async_calls != 0) {
res.code = 500;
res.body = "request in progress";
res.end();
return;
}
using GetObjectType =
std::vector<std::pair<std::string, std::vector<std::string>>>;
std::string object_path = "/xyz/" + path;
crow::connections::system_bus->async_method_call(
// object_path intentially captured by value
[&, object_path](const boost::system::error_code ec,
const GetObjectType &object_names) {
if (ec) {
res.code = 500;
res.end();
return;
}
if (object_names.size() != 1) {
res.code = 404;
res.end();
return;
}
for (auto &interface : object_names[0].second) {
outstanding_async_calls++;
crow::connections::system_bus->async_method_call(
[&](const boost::system::error_code ec,
const std::vector<std::pair<
std::string, dbus::dbus_variant>> &properties) {
outstanding_async_calls--;
if (ec) {
std::cerr << "Bad dbus request error: " << ec;
} else {
for (auto &property : properties) {
boost::apply_visitor(
[&](auto val) {
object_paths[property.first] = val;
},
property.second);
}
}
if (outstanding_async_calls == 0) {
nlohmann::json j{{"status", "ok"},
{"message", "200 OK"},
{"data", object_paths}};
res.body = j.dump();
res.end();
object_paths.clear();
}
},
{object_names[0].first, object_path,
"org.freedesktop.DBus.Properties", "GetAll"},
interface);
}
},
{"xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetObject"},
object_path, std::array<std::string, 0>());
});
CROW_ROUTE(app, "/bus/system/<str>/")
.methods("GET"_method)([](const crow::request &req, crow::response &res,
const std::string &connection) {
if (outstanding_async_calls != 0) {
res.code = 500;
res.body = "request in progress";
res.end();
return;
}
introspect_objects(res, connection, "/");
});
CROW_ROUTE(app, "/bus/system/<str>/<path>")
.methods("GET"_method)([](const crow::request &req, crow::response &res,
const std::string &process_name,
const std::string &requested_path) {
std::vector<std::string> strs;
boost::split(strs, requested_path, boost::is_any_of("/"));
std::string object_path;
std::string interface_name;
std::string method_name;
auto it = strs.begin();
if (it == strs.end()) {
object_path = "/";
}
while (it != strs.end()) {
// Check if segment contains ".". If it does, it must be an
// interface
if ((*it).find(".") != std::string::npos) {
break;
// THis check is neccesary as the trailing slash gets parsed as
// part
// of our <path> specifier above, which causes the normal trailing
// backslash redirector to fail.
} else if (!it->empty()) {
object_path += "/" + *it;
}
it++;
}
if (it != strs.end()) {
interface_name = *it;
it++;
// after interface, we might have a method name
if (it != strs.end()) {
method_name = *it;
it++;
}
}
if (it != strs.end()) {
// if there is more levels past the method name, something went
// wrong, throw an error
res.code = 404;
res.end();
return;
}
dbus::endpoint introspect_endpoint(
process_name, object_path, "org.freedesktop.DBus.Introspectable",
"Introspect");
if (interface_name.empty()) {
crow::connections::system_bus->async_method_call(
[
&, process_name{std::move(process_name)},
object_path{std::move(object_path)}
](const boost::system::error_code ec,
const std::string &introspect_xml) {
if (ec) {
std::cerr
<< "Introspect call failed with error: " << ec.message()
<< " on process: " << process_name
<< " path: " << object_path << "\n";
} else {
tinyxml2::XMLDocument doc;
doc.Parse(introspect_xml.c_str());
tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
if (pRoot == nullptr) {
std::cerr << "XML document failed to parse " << process_name
<< " " << object_path << "\n";
res.write(nlohmann::json{{"status", "XML parse error"}});
res.code = 500;
} else {
nlohmann::json interfaces_array = nlohmann::json::array();
tinyxml2::XMLElement *interface =
pRoot->FirstChildElement("interface");
while (interface != nullptr) {
std::string iface_name = interface->Attribute("name");
interfaces_array.push_back({{"name", iface_name}});
interface = interface->NextSiblingElement("interface");
}
nlohmann::json j{{"status", "ok"},
{"bus_name", process_name},
{"interfaces", interfaces_array},
{"object_path", object_path}};
res.write(j.dump());
}
}
res.end();
},
introspect_endpoint);
} else {
crow::connections::system_bus->async_method_call(
[
&, process_name{std::move(process_name)},
interface_name{std::move(interface_name)},
object_path{std::move(object_path)}
](const boost::system::error_code ec,
const std::string &introspect_xml) {
if (ec) {
std::cerr
<< "Introspect call failed with error: " << ec.message()
<< " on process: " << process_name
<< " path: " << object_path << "\n";
} else {
tinyxml2::XMLDocument doc;
doc.Parse(introspect_xml.c_str());
tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
if (pRoot == nullptr) {
std::cerr << "XML document failed to parse " << process_name
<< " " << object_path << "\n";
res.code = 500;
} else {
tinyxml2::XMLElement *node =
pRoot->FirstChildElement("node");
// if we know we're the only call, build the json directly
nlohmann::json methods_array = nlohmann::json::array();
nlohmann::json signals_array = nlohmann::json::array();
tinyxml2::XMLElement *interface =
pRoot->FirstChildElement("interface");
while (interface != nullptr) {
std::string iface_name = interface->Attribute("name");
if (iface_name == interface_name) {
tinyxml2::XMLElement *methods =
interface->FirstChildElement("method");
while (methods != nullptr) {
nlohmann::json args_array = nlohmann::json::array();
tinyxml2::XMLElement *arg =
methods->FirstChildElement("arg");
while (arg != nullptr) {
args_array.push_back(
{{"name", arg->Attribute("name")},
{"type", arg->Attribute("type")},
{"direction", arg->Attribute("direction")}});
arg = arg->NextSiblingElement("arg");
}
methods_array.push_back(
{{"name", methods->Attribute("name")},
{"uri",
"/bus/system/" + process_name + object_path +
"/" + interface_name + "/" +
methods->Attribute("name")},
{"args", args_array}});
methods = methods->NextSiblingElement("method");
}
tinyxml2::XMLElement *signals =
interface->FirstChildElement("signal");
while (signals != nullptr) {
nlohmann::json args_array = nlohmann::json::array();
tinyxml2::XMLElement *arg =
signals->FirstChildElement("arg");
while (arg != nullptr) {
std::string name = arg->Attribute("name");
std::string type = arg->Attribute("type");
args_array.push_back({
{"name", name}, {"type", type},
});
arg = arg->NextSiblingElement("arg");
}
signals_array.push_back(
{{"name", signals->Attribute("name")},
{"args", args_array}});
signals = signals->NextSiblingElement("signal");
}
nlohmann::json j{
{"status", "ok"},
{"bus_name", process_name},
{"interface", interface_name},
{"methods", methods_array},
{"object_path", object_path},
{"properties", nlohmann::json::object()},
{"signals", signals_array}};
res.write(j.dump());
break;
}
interface = interface->NextSiblingElement("interface");
}
if (interface == nullptr) {
// if we got to the end of the list and never found a
// match, throw 404
res.code = 404;
}
}
}
res.end();
},
introspect_endpoint);
}
});
}
} // namespace openbmc_mapper
} // namespace crow
|