blob: b4f6265718b17cc5c2fff570f01957ef8e497957 [file] [log] [blame]
Patrick Venture95269db2018-08-31 09:19:17 -07001#include <boost/asio.hpp>
Vernon Mauery261e72b2018-09-25 12:34:25 -07002#include <boost/asio/spawn.hpp>
James Feistfce038a2018-04-13 15:43:13 -07003#include <chrono>
Patrick Venture95269db2018-08-31 09:19:17 -07004#include <ctime>
5#include <iostream>
James Feist284a0f92018-04-05 15:28:16 -07006#include <sdbusplus/asio/connection.hpp>
James Feistfce038a2018-04-13 15:43:13 -07007#include <sdbusplus/asio/object_server.hpp>
Vernon Mauery035c73b2018-09-05 12:15:27 -07008#include <sdbusplus/asio/sd_event.hpp>
Patrick Venture95269db2018-08-31 09:19:17 -07009#include <sdbusplus/bus.hpp>
Vernon Mauery076d14a2018-10-02 15:10:20 -070010#include <sdbusplus/exception.hpp>
Patrick Venture95269db2018-08-31 09:19:17 -070011#include <sdbusplus/server.hpp>
Vernon Mauery035c73b2018-09-05 12:15:27 -070012#include <sdbusplus/timer.hpp>
James Feist284a0f92018-04-05 15:28:16 -070013
Vernon Mauery261e72b2018-09-25 12:34:25 -070014using variant = sdbusplus::message::variant<int, std::string>;
Vernon Mauery076d14a2018-10-02 15:10:20 -070015
James Feistfce038a2018-04-13 15:43:13 -070016int foo(int test)
17{
Vernon Mauery076d14a2018-10-02 15:10:20 -070018 std::cout << "foo(" << test << ") -> " << (test + 1) << "\n";
James Feistfce038a2018-04-13 15:43:13 -070019 return ++test;
20}
21
Vernon Mauery076d14a2018-10-02 15:10:20 -070022// called from coroutine context, can make yielding dbus calls
23int fooYield(boost::asio::yield_context yield,
24 std::shared_ptr<sdbusplus::asio::connection> conn, int test)
25{
26 // fetch the real value from testFunction
27 boost::system::error_code ec;
28 std::cout << "fooYield(yield, " << test << ")...\n";
29 int testCount = conn->yield_method_call<int>(
30 yield[ec], "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
31 "xyz.openbmc_project.test", "TestFunction", test);
32 if (ec || testCount != (test + 1))
33 {
34 std::cout << "call to foo failed: ec = " << ec << '\n';
35 return -1;
36 }
37 std::cout << "yielding call to foo OK! (-> " << testCount << ")\n";
38 return testCount;
39}
40
Vernon Mauery8ce61e42018-08-28 08:54:59 -070041int methodWithMessage(sdbusplus::message::message& m, int test)
42{
Vernon Mauery076d14a2018-10-02 15:10:20 -070043 std::cout << "methodWithMessage(m, " << test << ") -> " << (test + 1)
44 << "\n";
Vernon Mauery8ce61e42018-08-28 08:54:59 -070045 return ++test;
46}
47
Vernon Mauery2c8f93f2018-09-05 12:02:39 -070048int voidBar(void)
49{
Vernon Mauery076d14a2018-10-02 15:10:20 -070050 std::cout << "voidBar() -> 42\n";
Vernon Mauery2c8f93f2018-09-05 12:02:39 -070051 return 42;
52}
53
Vernon Mauery261e72b2018-09-25 12:34:25 -070054void do_start_async_method_call_one(
55 std::shared_ptr<sdbusplus::asio::connection> conn,
56 boost::asio::yield_context yield)
57{
58 boost::system::error_code ec;
59 variant testValue;
60 conn->yield_method_call<>(yield[ec], "xyz.openbmc_project.asio-test",
61 "/xyz/openbmc_project/test",
62 "org.freedesktop.DBus.Properties", "Set",
63 "xyz.openbmc_project.test", "int", variant(24));
64 testValue = conn->yield_method_call<variant>(
65 yield[ec], "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
66 "org.freedesktop.DBus.Properties", "Get", "xyz.openbmc_project.test",
67 "int");
68 if (!ec && testValue.get<int>() == 24)
69 {
70 std::cout << "async call to Properties.Get serialized via yield OK!\n";
71 }
72 else
73 {
74 std::cout << "ec = " << ec << ": " << testValue.get<int>() << "\n";
75 }
76 conn->yield_method_call<void>(
77 yield[ec], "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
78 "org.freedesktop.DBus.Properties", "Set", "xyz.openbmc_project.test",
79 "int", variant(42));
80 testValue = conn->yield_method_call<variant>(
81 yield[ec], "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
82 "org.freedesktop.DBus.Properties", "Get", "xyz.openbmc_project.test",
83 "int");
84 if (!ec && testValue.get<int>() == 42)
85 {
86 std::cout << "async call to Properties.Get serialized via yield OK!\n";
87 }
88 else
89 {
90 std::cout << "ec = " << ec << ": " << testValue.get<int>() << "\n";
91 }
92}
93
Vernon Mauery076d14a2018-10-02 15:10:20 -070094void do_start_async_ipmi_call(std::shared_ptr<sdbusplus::asio::connection> conn,
95 boost::asio::yield_context yield)
96{
97 auto method = conn->new_method_call("xyz.openbmc_project.asio-test",
98 "/xyz/openbmc_project/test",
99 "xyz.openbmc_project.test", "execute");
100 constexpr uint8_t netFn = 6;
101 constexpr uint8_t lun = 0;
102 constexpr uint8_t cmd = 1;
103 std::map<std::string, variant> options = {{"username", variant("admin")},
104 {"privilege", variant(4)}};
105 std::vector<uint8_t> commandData = {4, 3, 2, 1};
106 method.append(netFn, lun, cmd, commandData, options);
107 boost::system::error_code ec;
108 sdbusplus::message::message reply = conn->async_send(method, yield[ec]);
109 std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
110 tupleOut;
111 try
112 {
113 reply.read(tupleOut);
114 }
115 catch (const sdbusplus::exception::SdBusError& e)
116 {
117 std::cerr << "failed to unpack; sig is " << reply.get_signature()
118 << "\n";
119 }
120 auto& [rnetFn, rlun, rcmd, cc, responseData] = tupleOut;
121 std::vector<uint8_t> expRsp = {1, 2, 3, 4};
122 if (rnetFn == uint8_t(netFn + 1) && rlun == lun && rcmd == cmd && cc == 0 &&
123 responseData == expRsp)
124 {
125 std::cerr << "ipmi call returns OK!\n";
126 }
127 else
128 {
129 std::cerr << "ipmi call returns unexpected response\n";
130 }
131}
132
133auto ipmiInterface(boost::asio::yield_context yield, uint8_t netFn, uint8_t lun,
134 uint8_t cmd, std::vector<uint8_t>& data,
135 const std::map<std::string, variant>& options)
136{
137 std::vector<uint8_t> reply = {1, 2, 3, 4};
138 uint8_t cc = 0;
139 std::cerr << "ipmiInterface:execute(" << int(netFn) << int(cmd) << ")\n";
140 return std::make_tuple(uint8_t(netFn + 1), lun, cmd, cc, reply);
141}
142
143void do_start_async_to_yield(std::shared_ptr<sdbusplus::asio::connection> conn,
144 boost::asio::yield_context yield)
Vernon Mauery261e72b2018-09-25 12:34:25 -0700145{
146 boost::system::error_code ec;
Vernon Mauery076d14a2018-10-02 15:10:20 -0700147 int testValue = 0;
148 try
149 {
150 testValue = conn->yield_method_call<int>(
Vernon Mauery261e72b2018-09-25 12:34:25 -0700151 yield[ec], "xyz.openbmc_project.asio-test",
152 "/xyz/openbmc_project/test", "xyz.openbmc_project.test",
Vernon Mauery076d14a2018-10-02 15:10:20 -0700153 "TestYieldFunction", int(41));
154 }
155 catch (sdbusplus::exception::SdBusError& e)
Vernon Mauery261e72b2018-09-25 12:34:25 -0700156 {
Vernon Mauery076d14a2018-10-02 15:10:20 -0700157 std::cout << "oops: " << e.what() << "\n";
158 }
159 if (!ec && testValue == 42)
160 {
161 std::cout
162 << "yielding call to TestYieldFunction serialized via yield OK!\n";
Vernon Mauery261e72b2018-09-25 12:34:25 -0700163 }
164 else
165 {
166 std::cout << "ec = " << ec << ": " << testValue << "\n";
167 }
168}
169
Vernon Mauery076d14a2018-10-02 15:10:20 -0700170int server()
James Feist284a0f92018-04-05 15:28:16 -0700171{
James Feist284a0f92018-04-05 15:28:16 -0700172 // setup connection to dbus
173 boost::asio::io_service io;
174 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
175
James Feistfce038a2018-04-13 15:43:13 -0700176 // test object server
177 conn->request_name("xyz.openbmc_project.asio-test");
178 auto server = sdbusplus::asio::object_server(conn);
179 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
180 server.add_interface("/xyz/openbmc_project/test",
181 "xyz.openbmc_project.test");
182 // test generic properties
183 iface->register_property("int", 33,
184 sdbusplus::asio::PropertyPermission::readWrite);
185 std::vector<std::string> myStringVec = {"some", "test", "data"};
186 std::vector<std::string> myStringVec2 = {"more", "test", "data"};
187
188 iface->register_property("myStringVec", myStringVec,
189 sdbusplus::asio::PropertyPermission::readWrite);
190 iface->register_property("myStringVec2", myStringVec2);
191
192 // test properties with specialized callbacks
193 iface->register_property("lessThan50", 23,
194 // custom set
195 [](const int& req, int& propertyValue) {
196 if (req >= 50)
197 {
198 return -EINVAL;
199 }
200 propertyValue = req;
201 return 1; // success
202 });
203 iface->register_property(
204 "TrailTime", std::string("foo"),
205 // custom set
206 [](const std::string& req, std::string& propertyValue) {
207 propertyValue = req;
208 return 1; // success
209 },
210 // custom get
211 [](const std::string& property) {
212 auto now = std::chrono::system_clock::now();
213 auto timePoint = std::chrono::system_clock::to_time_t(now);
214 return property + std::ctime(&timePoint);
215 });
216
217 // test method creation
218 iface->register_method("TestMethod", [](const int32_t& callCount) {
Vernon Mauery261e72b2018-09-25 12:34:25 -0700219 return std::make_tuple(callCount,
220 "success: " + std::to_string(callCount));
James Feistfce038a2018-04-13 15:43:13 -0700221 });
222
223 iface->register_method("TestFunction", foo);
224
Vernon Mauery076d14a2018-10-02 15:10:20 -0700225 // fooYield has boost::asio::yield_context as first argument
226 // so will be executed in coroutine context if called
227 iface->register_method("TestYieldFunction",
228 [conn](boost::asio::yield_context yield, int val) {
229 return fooYield(yield, conn, val);
230 });
231
Vernon Mauery8ce61e42018-08-28 08:54:59 -0700232 iface->register_method("TestMethodWithMessage", methodWithMessage);
233
Vernon Mauery2c8f93f2018-09-05 12:02:39 -0700234 iface->register_method("VoidFunctionReturnsInt", voidBar);
235
Vernon Mauery076d14a2018-10-02 15:10:20 -0700236 iface->register_method("execute", ipmiInterface);
237
James Feistfce038a2018-04-13 15:43:13 -0700238 iface->initialize();
Vernon Mauery076d14a2018-10-02 15:10:20 -0700239
240 io.run();
241
242 return 0;
243}
244
245int client()
246{
247 using GetSubTreeType = std::vector<std::pair<
248 std::string,
249 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
250 using message = sdbusplus::message::message;
251
252 // setup connection to dbus
253 boost::asio::io_service io;
254 auto conn = std::make_shared<sdbusplus::asio::connection>(io);
255
256 int ready = 0;
257 while (!ready)
258 {
259 auto readyMsg = conn->new_method_call(
260 "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
261 "xyz.openbmc_project.test", "VoidFunctionReturnsInt");
262 try
263 {
264 message intMsg = conn->call(readyMsg);
265 intMsg.read(ready);
266 }
267 catch (sdbusplus::exception::SdBusError& e)
268 {
269 ready = 0;
270 // pause to give the server a chance to start up
271 usleep(10000);
272 }
273 }
274
275 // test async method call and async send
276 auto mesg =
277 conn->new_method_call("xyz.openbmc_project.ObjectMapper",
278 "/xyz/openbmc_project/object_mapper",
279 "xyz.openbmc_project.ObjectMapper", "GetSubTree");
280
281 static const auto depth = 2;
282 static const std::vector<std::string> interfaces = {
283 "xyz.openbmc_project.Sensor.Value"};
284 mesg.append("/xyz/openbmc_project/Sensors", depth, interfaces);
285
286 conn->async_send(mesg, [](boost::system::error_code ec, message& ret) {
287 std::cout << "async_send callback\n";
288 if (ec || ret.is_method_error())
289 {
290 std::cerr << "error with async_send\n";
291 return;
292 }
293 GetSubTreeType data;
294 ret.read(data);
295 for (auto& item : data)
296 {
297 std::cout << item.first << "\n";
298 }
299 });
300
301 conn->async_method_call(
302 [](boost::system::error_code ec, GetSubTreeType& subtree) {
303 std::cout << "async_method_call callback\n";
304 if (ec)
305 {
306 std::cerr << "error with async_method_call\n";
307 return;
308 }
309 for (auto& item : subtree)
310 {
311 std::cout << item.first << "\n";
312 }
313 },
314 "xyz.openbmc_project.ObjectMapper",
315 "/xyz/openbmc_project/object_mapper",
316 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
317 "/org/openbmc/control", 2, std::vector<std::string>());
Vernon Mauery035c73b2018-09-05 12:15:27 -0700318
319 // sd_events work too using the default event loop
320 phosphor::Timer t1([]() { std::cerr << "*** tock ***\n"; });
321 t1.start(std::chrono::microseconds(1000000));
322 phosphor::Timer t2([]() { std::cerr << "*** tick ***\n"; });
323 t2.start(std::chrono::microseconds(500000), true);
324 // add the sd_event wrapper to the io object
325 sdbusplus::asio::sd_event_wrapper sdEvents(io);
326
Vernon Mauery261e72b2018-09-25 12:34:25 -0700327 // set up a client to make an async call to the server
328 // using coroutines (userspace cooperative multitasking)
329 boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
330 do_start_async_method_call_one(conn, yield);
331 });
332 boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
Vernon Mauery076d14a2018-10-02 15:10:20 -0700333 do_start_async_ipmi_call(conn, yield);
Vernon Mauery261e72b2018-09-25 12:34:25 -0700334 });
Vernon Mauery076d14a2018-10-02 15:10:20 -0700335 boost::asio::spawn(io, [conn](boost::asio::yield_context yield) {
336 do_start_async_to_yield(conn, yield);
337 });
338
339 conn->async_method_call(
340 [](boost::system::error_code ec, int32_t testValue) {
341 if (ec)
342 {
343 std::cerr << "TestYieldFunction returned error with "
344 "async_method_call (ec = "
345 << ec << ")\n";
346 return;
347 }
348 std::cout << "TestYieldFunction return " << testValue << "\n";
349 },
350 "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test",
351 "xyz.openbmc_project.test", "TestYieldFunction", int32_t(41));
James Feist284a0f92018-04-05 15:28:16 -0700352 io.run();
353
354 return 0;
355}
Vernon Mauery076d14a2018-10-02 15:10:20 -0700356
357int main(int argc, const char* argv[])
358{
359 if (argc == 1)
360 {
361 int pid = fork();
362 if (pid == 0)
363 {
364 return client();
365 }
366 else if (pid > 0)
367 {
368 return server();
369 }
370 return pid;
371 }
372 if (std::string(argv[1]) == "--server")
373 {
374 return server();
375 }
376 if (std::string(argv[1]) == "--client")
377 {
378 return client();
379 }
380 std::cout << "usage: " << argv[0] << " [--server | --client]\n";
381 return -1;
382}