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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <chrono>
#include <ctime>
#include <iostream>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <sdbusplus/asio/sd_event.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/exception.hpp>
#include <sdbusplus/server.hpp>
#include <sdbusplus/timer.hpp>
#include <variant>
using variant = std::variant<int, std::string>;
int foo(int test)
{
std::cout << "foo(" << test << ") -> " << (test + 1) << "\n";
return ++test;
}
// called from coroutine context, can make yielding dbus calls
int fooYield(boost::asio::yield_context yield,
std::shared_ptr<sdbusplus::asio::connection> conn, int test)
{
// fetch the real value from testFunction
boost::system::error_code ec;
std::cout << "fooYield(yield, " << test << ")...\n";
int testCount = conn->yield_method_call<int>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "TestFunction", test);
if (ec || testCount != (test + 1))
{
std::cout << "call to foo failed: ec = " << ec << '\n';
return -1;
}
std::cout << "yielding call to foo OK! (-> " << testCount << ")\n";
return testCount;
}
int methodWithMessage(sdbusplus::message::message& m, int test)
{
std::cout << "methodWithMessage(m, " << test << ") -> " << (test + 1)
<< "\n";
return ++test;
}
int voidBar(void)
{
std::cout << "voidBar() -> 42\n";
return 42;
}
void do_start_async_method_call_one(
std::shared_ptr<sdbusplus::asio::connection> conn,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
variant testValue;
conn->yield_method_call<>(yield, ec, "xyz.openbmc_project.asio-test",
"/xyz/openbmc_project/test",
"org.freedesktop.DBus.Properties", "Set",
"xyz.openbmc_project.test", "int", variant(24));
testValue = conn->yield_method_call<variant>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"org.freedesktop.DBus.Properties", "Get", "xyz.openbmc_project.test",
"int");
if (!ec && std::get<int>(testValue) == 24)
{
std::cout << "async call to Properties.Get serialized via yield OK!\n";
}
else
{
std::cout << "ec = " << ec << ": " << std::get<int>(testValue) << "\n";
}
conn->yield_method_call<void>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"org.freedesktop.DBus.Properties", "Set", "xyz.openbmc_project.test",
"int", variant(42));
testValue = conn->yield_method_call<variant>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"org.freedesktop.DBus.Properties", "Get", "xyz.openbmc_project.test",
"int");
if (!ec && std::get<int>(testValue) == 42)
{
std::cout << "async call to Properties.Get serialized via yield OK!\n";
}
else
{
std::cout << "ec = " << ec << ": " << std::get<int>(testValue) << "\n";
}
}
void do_start_async_ipmi_call(std::shared_ptr<sdbusplus::asio::connection> conn,
boost::asio::yield_context yield)
{
auto method = conn->new_method_call("xyz.openbmc_project.asio-test",
"/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "execute");
constexpr uint8_t netFn = 6;
constexpr uint8_t lun = 0;
constexpr uint8_t cmd = 1;
std::map<std::string, variant> options = {{"username", variant("admin")},
{"privilege", variant(4)}};
std::vector<uint8_t> commandData = {4, 3, 2, 1};
method.append(netFn, lun, cmd, commandData, options);
boost::system::error_code ec;
sdbusplus::message::message reply = conn->async_send(method, yield[ec]);
std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
tupleOut;
try
{
reply.read(tupleOut);
}
catch (const sdbusplus::exception::SdBusError& e)
{
std::cerr << "failed to unpack; sig is " << reply.get_signature()
<< "\n";
}
auto& [rnetFn, rlun, rcmd, cc, responseData] = tupleOut;
std::vector<uint8_t> expRsp = {1, 2, 3, 4};
if (rnetFn == uint8_t(netFn + 1) && rlun == lun && rcmd == cmd && cc == 0 &&
responseData == expRsp)
{
std::cerr << "ipmi call returns OK!\n";
}
else
{
std::cerr << "ipmi call returns unexpected response\n";
}
}
auto ipmiInterface(boost::asio::yield_context yield, uint8_t netFn, uint8_t lun,
uint8_t cmd, std::vector<uint8_t>& data,
const std::map<std::string, variant>& options)
{
std::vector<uint8_t> reply = {1, 2, 3, 4};
uint8_t cc = 0;
std::cerr << "ipmiInterface:execute(" << int(netFn) << int(cmd) << ")\n";
return std::make_tuple(uint8_t(netFn + 1), lun, cmd, cc, reply);
}
void do_start_async_to_yield(std::shared_ptr<sdbusplus::asio::connection> conn,
boost::asio::yield_context yield)
{
boost::system::error_code ec;
int testValue = 0;
testValue = conn->yield_method_call<int>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "TestYieldFunction", int(41));
if (!ec && testValue == 42)
{
std::cout
<< "yielding call to TestYieldFunction serialized via yield OK!\n";
}
else
{
std::cout << "ec = " << ec << ": " << testValue << "\n";
}
ec.clear();
auto badValue = conn->yield_method_call<std::string>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "TestYieldFunction", int(41));
if (!ec)
{
std::cout
<< "yielding call to TestYieldFunction returned the wrong type\n";
}
else
{
std::cout << "TestYieldFunction expected error: " << ec << "\n";
}
ec.clear();
auto unUsedValue = conn->yield_method_call<std::string>(
yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "TestYieldFunctionNotExits", int(41));
if (!ec)
{
std::cout << "TestYieldFunctionNotExists returned unexpectedly\n";
}
else
{
std::cout << "TestYieldFunctionNotExits expected error: " << ec << "\n";
}
}
int server()
{
// setup connection to dbus
boost::asio::io_context io;
auto conn = std::make_shared<sdbusplus::asio::connection>(io);
// test object server
conn->request_name("xyz.openbmc_project.asio-test");
auto server = sdbusplus::asio::object_server(conn);
std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
server.add_interface("/xyz/openbmc_project/test",
"xyz.openbmc_project.test");
// test generic properties
iface->register_property("int", 33,
sdbusplus::asio::PropertyPermission::readWrite);
std::vector<std::string> myStringVec = {"some", "test", "data"};
std::vector<std::string> myStringVec2 = {"more", "test", "data"};
iface->register_property("myStringVec", myStringVec,
sdbusplus::asio::PropertyPermission::readWrite);
iface->register_property("myStringVec2", myStringVec2);
// test properties with specialized callbacks
iface->register_property("lessThan50", 23,
// custom set
[](const int& req, int& propertyValue) {
if (req >= 50)
{
return -EINVAL;
}
propertyValue = req;
return 1; // success
});
iface->register_property(
"TrailTime", std::string("foo"),
// custom set
[](const std::string& req, std::string& propertyValue) {
propertyValue = req;
return 1; // success
},
// custom get
[](const std::string& property) {
auto now = std::chrono::system_clock::now();
auto timePoint = std::chrono::system_clock::to_time_t(now);
return property + std::ctime(&timePoint);
});
// test method creation
iface->register_method("TestMethod", [](const int32_t& callCount) {
return std::make_tuple(callCount,
"success: " + std::to_string(callCount));
});
iface->register_method("TestFunction", foo);
// fooYield has boost::asio::yield_context as first argument
// so will be executed in coroutine context if called
iface->register_method("TestYieldFunction",
[conn](boost::asio::yield_context yield, int val) {
return fooYield(yield, conn, val);
});
iface->register_method("TestMethodWithMessage", methodWithMessage);
iface->register_method("VoidFunctionReturnsInt", voidBar);
iface->register_method("execute", ipmiInterface);
iface->initialize();
io.run();
return 0;
}
int client()
{
using GetSubTreeType = std::vector<std::pair<
std::string,
std::vector<std::pair<std::string, std::vector<std::string>>>>>;
using message = sdbusplus::message::message;
// setup connection to dbus
boost::asio::io_context io;
auto conn = std::make_shared<sdbusplus::asio::connection>(io);
int ready = 0;
while (!ready)
{
auto readyMsg = conn->new_method_call(
"xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "VoidFunctionReturnsInt");
try
{
message intMsg = conn->call(readyMsg);
intMsg.read(ready);
}
catch (sdbusplus::exception::SdBusError& e)
{
ready = 0;
// pause to give the server a chance to start up
usleep(10000);
}
}
// test async method call and async send
auto mesg =
conn->new_method_call("xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetSubTree");
static const auto depth = 2;
static const std::vector<std::string> interfaces = {
"xyz.openbmc_project.Sensor.Value"};
mesg.append("/xyz/openbmc_project/Sensors", depth, interfaces);
conn->async_send(mesg, [](boost::system::error_code ec, message& ret) {
std::cout << "async_send callback\n";
if (ec || ret.is_method_error())
{
std::cerr << "error with async_send\n";
return;
}
GetSubTreeType data;
ret.read(data);
for (auto& item : data)
{
std::cout << item.first << "\n";
}
});
conn->async_method_call(
[](boost::system::error_code ec, GetSubTreeType& subtree) {
std::cout << "async_method_call callback\n";
if (ec)
{
std::cerr << "error with async_method_call\n";
return;
}
for (auto& item : subtree)
{
std::cout << item.first << "\n";
}
},
"xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetSubTree",
"/org/openbmc/control", 2, std::vector<std::string>());
std::string nonConstCapture = "lalalala";
conn->async_method_call(
[nonConstCapture = std::move(nonConstCapture)](
boost::system::error_code ec,
const std::vector<std::string>& things) mutable {
std::cout << "async_method_call callback\n";
nonConstCapture += " stuff";
if (ec)
{
std::cerr << "async_method_call expected failure: " << ec
<< "\n";
}
else
{
std::cerr << "async_method_call should have failed!\n";
}
},
"xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetSubTree",
"/xyz/openbmc_project/sensors", depth, interfaces);
// sd_events work too using the default event loop
phosphor::Timer t1([]() { std::cerr << "*** tock ***\n"; });
t1.start(std::chrono::microseconds(1000000));
phosphor::Timer t2([]() { std::cerr << "*** tick ***\n"; });
t2.start(std::chrono::microseconds(500000), true);
// add the sd_event wrapper to the io object
sdbusplus::asio::sd_event_wrapper sdEvents(io);
// set up a client to make an async call to the server
// using coroutines (userspace cooperative multitasking)
boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
do_start_async_method_call_one(conn, yield);
});
boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
do_start_async_ipmi_call(conn, yield);
});
boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
do_start_async_to_yield(conn, yield);
});
conn->async_method_call(
[](boost::system::error_code ec, int32_t testValue) {
if (ec)
{
std::cerr << "TestYieldFunction returned error with "
"async_method_call (ec = "
<< ec << ")\n";
return;
}
std::cout << "TestYieldFunction return " << testValue << "\n";
},
"xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
"xyz.openbmc_project.test", "TestYieldFunction", int32_t(41));
io.run();
return 0;
}
int main(int argc, const char* argv[])
{
if (argc == 1)
{
int pid = fork();
if (pid == 0)
{
return client();
}
else if (pid > 0)
{
return server();
}
return pid;
}
if (std::string(argv[1]) == "--server")
{
return server();
}
if (std::string(argv[1]) == "--client")
{
return client();
}
std::cout << "usage: " << argv[0] << " [--server | --client]\n";
return -1;
}
|