Ed Tanous | b65dc1c | 2023-03-07 17:41:57 -0800 | [diff] [blame] | 1 | #include <boost/asio/io_context.hpp> |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 2 | #include <boost/asio/spawn.hpp> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 3 | #include <sdbusplus/asio/connection.hpp> |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 4 | #include <sdbusplus/asio/object_server.hpp> |
Vernon Mauery | 035c73b | 2018-09-05 12:15:27 -0700 | [diff] [blame] | 5 | #include <sdbusplus/asio/sd_event.hpp> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 8 | #include <sdbusplus/server.hpp> |
Vernon Mauery | 035c73b | 2018-09-05 12:15:27 -0700 | [diff] [blame] | 9 | #include <sdbusplus/timer.hpp> |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 10 | |
| 11 | #include <chrono> |
| 12 | #include <ctime> |
| 13 | #include <iostream> |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 14 | #include <variant> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 15 | |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 16 | using variant = std::variant<int, std::string>; |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 17 | |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 18 | int foo(int test) |
| 19 | { |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 20 | std::cout << "foo(" << test << ") -> " << (test + 1) << "\n"; |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 21 | return ++test; |
| 22 | } |
| 23 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 24 | // called from coroutine context, can make yielding dbus calls |
| 25 | int fooYield(boost::asio::yield_context yield, |
| 26 | std::shared_ptr<sdbusplus::asio::connection> conn, int test) |
| 27 | { |
| 28 | // fetch the real value from testFunction |
| 29 | boost::system::error_code ec; |
| 30 | std::cout << "fooYield(yield, " << test << ")...\n"; |
| 31 | int testCount = conn->yield_method_call<int>( |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 32 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 33 | "xyz.openbmc_project.test", "TestFunction", test); |
| 34 | if (ec || testCount != (test + 1)) |
| 35 | { |
| 36 | std::cout << "call to foo failed: ec = " << ec << '\n'; |
| 37 | return -1; |
| 38 | } |
| 39 | std::cout << "yielding call to foo OK! (-> " << testCount << ")\n"; |
| 40 | return testCount; |
| 41 | } |
| 42 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 43 | int methodWithMessage(sdbusplus::message_t& /*m*/, int test) |
Vernon Mauery | 8ce61e4 | 2018-08-28 08:54:59 -0700 | [diff] [blame] | 44 | { |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 45 | std::cout << "methodWithMessage(m, " << test << ") -> " << (test + 1) |
| 46 | << "\n"; |
Vernon Mauery | 8ce61e4 | 2018-08-28 08:54:59 -0700 | [diff] [blame] | 47 | return ++test; |
| 48 | } |
| 49 | |
Vernon Mauery | 2c8f93f | 2018-09-05 12:02:39 -0700 | [diff] [blame] | 50 | int voidBar(void) |
| 51 | { |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 52 | std::cout << "voidBar() -> 42\n"; |
Vernon Mauery | 2c8f93f | 2018-09-05 12:02:39 -0700 | [diff] [blame] | 53 | return 42; |
| 54 | } |
| 55 | |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 56 | void do_start_async_method_call_one( |
| 57 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 58 | boost::asio::yield_context yield) |
| 59 | { |
| 60 | boost::system::error_code ec; |
| 61 | variant testValue; |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 62 | conn->yield_method_call<>(yield, ec, "xyz.openbmc_project.asio-test", |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 63 | "/xyz/openbmc_project/test", |
| 64 | "org.freedesktop.DBus.Properties", "Set", |
| 65 | "xyz.openbmc_project.test", "int", variant(24)); |
| 66 | testValue = conn->yield_method_call<variant>( |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 67 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 68 | "org.freedesktop.DBus.Properties", "Get", "xyz.openbmc_project.test", |
| 69 | "int"); |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 70 | if (!ec && std::get<int>(testValue) == 24) |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 71 | { |
| 72 | std::cout << "async call to Properties.Get serialized via yield OK!\n"; |
| 73 | } |
| 74 | else |
| 75 | { |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 76 | std::cout << "ec = " << ec << ": " << std::get<int>(testValue) << "\n"; |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 77 | } |
| 78 | conn->yield_method_call<void>( |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 79 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 80 | "org.freedesktop.DBus.Properties", "Set", "xyz.openbmc_project.test", |
| 81 | "int", variant(42)); |
| 82 | testValue = conn->yield_method_call<variant>( |
Vernon Mauery | 37a5e61 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 83 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 84 | "org.freedesktop.DBus.Properties", "Get", "xyz.openbmc_project.test", |
| 85 | "int"); |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 86 | if (!ec && std::get<int>(testValue) == 42) |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 87 | { |
| 88 | std::cout << "async call to Properties.Get serialized via yield OK!\n"; |
| 89 | } |
| 90 | else |
| 91 | { |
William A. Kennington III | 4274c11 | 2018-11-26 09:50:13 -0800 | [diff] [blame] | 92 | std::cout << "ec = " << ec << ": " << std::get<int>(testValue) << "\n"; |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 96 | void do_start_async_ipmi_call(std::shared_ptr<sdbusplus::asio::connection> conn, |
| 97 | boost::asio::yield_context yield) |
| 98 | { |
| 99 | auto method = conn->new_method_call("xyz.openbmc_project.asio-test", |
| 100 | "/xyz/openbmc_project/test", |
| 101 | "xyz.openbmc_project.test", "execute"); |
| 102 | constexpr uint8_t netFn = 6; |
| 103 | constexpr uint8_t lun = 0; |
| 104 | constexpr uint8_t cmd = 1; |
| 105 | std::map<std::string, variant> options = {{"username", variant("admin")}, |
| 106 | {"privilege", variant(4)}}; |
| 107 | std::vector<uint8_t> commandData = {4, 3, 2, 1}; |
| 108 | method.append(netFn, lun, cmd, commandData, options); |
| 109 | boost::system::error_code ec; |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 110 | sdbusplus::message_t reply = conn->async_send(method, yield[ec]); |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 111 | std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>> |
| 112 | tupleOut; |
| 113 | try |
| 114 | { |
| 115 | reply.read(tupleOut); |
| 116 | } |
| 117 | catch (const sdbusplus::exception::SdBusError& e) |
| 118 | { |
| 119 | std::cerr << "failed to unpack; sig is " << reply.get_signature() |
| 120 | << "\n"; |
| 121 | } |
| 122 | auto& [rnetFn, rlun, rcmd, cc, responseData] = tupleOut; |
| 123 | std::vector<uint8_t> expRsp = {1, 2, 3, 4}; |
| 124 | if (rnetFn == uint8_t(netFn + 1) && rlun == lun && rcmd == cmd && cc == 0 && |
| 125 | responseData == expRsp) |
| 126 | { |
| 127 | std::cerr << "ipmi call returns OK!\n"; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | std::cerr << "ipmi call returns unexpected response\n"; |
| 132 | } |
| 133 | } |
| 134 | |
Patrick Williams | 78b7803 | 2020-05-20 10:32:05 -0500 | [diff] [blame] | 135 | auto ipmiInterface(boost::asio::yield_context /*yield*/, uint8_t netFn, |
| 136 | uint8_t lun, uint8_t cmd, std::vector<uint8_t>& /*data*/, |
| 137 | const std::map<std::string, variant>& /*options*/) |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 138 | { |
| 139 | std::vector<uint8_t> reply = {1, 2, 3, 4}; |
| 140 | uint8_t cc = 0; |
| 141 | std::cerr << "ipmiInterface:execute(" << int(netFn) << int(cmd) << ")\n"; |
| 142 | return std::make_tuple(uint8_t(netFn + 1), lun, cmd, cc, reply); |
| 143 | } |
| 144 | |
| 145 | void do_start_async_to_yield(std::shared_ptr<sdbusplus::asio::connection> conn, |
| 146 | boost::asio::yield_context yield) |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 147 | { |
| 148 | boost::system::error_code ec; |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 149 | int testValue = 0; |
Vernon Mauery | c077190 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 150 | testValue = conn->yield_method_call<int>( |
| 151 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
| 152 | "xyz.openbmc_project.test", "TestYieldFunction", int(41)); |
| 153 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 154 | if (!ec && testValue == 42) |
| 155 | { |
| 156 | std::cout |
| 157 | << "yielding call to TestYieldFunction serialized via yield OK!\n"; |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 158 | } |
| 159 | else |
| 160 | { |
| 161 | std::cout << "ec = " << ec << ": " << testValue << "\n"; |
| 162 | } |
Vernon Mauery | c077190 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 163 | |
| 164 | ec.clear(); |
| 165 | auto badValue = conn->yield_method_call<std::string>( |
| 166 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
| 167 | "xyz.openbmc_project.test", "TestYieldFunction", int(41)); |
| 168 | |
| 169 | if (!ec) |
| 170 | { |
| 171 | std::cout |
| 172 | << "yielding call to TestYieldFunction returned the wrong type\n"; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | std::cout << "TestYieldFunction expected error: " << ec << "\n"; |
| 177 | } |
| 178 | |
| 179 | ec.clear(); |
| 180 | auto unUsedValue = conn->yield_method_call<std::string>( |
| 181 | yield, ec, "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
| 182 | "xyz.openbmc_project.test", "TestYieldFunctionNotExits", int(41)); |
| 183 | |
| 184 | if (!ec) |
| 185 | { |
| 186 | std::cout << "TestYieldFunctionNotExists returned unexpectedly\n"; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | std::cout << "TestYieldFunctionNotExits expected error: " << ec << "\n"; |
| 191 | } |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 194 | int server() |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 195 | { |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 196 | // setup connection to dbus |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 197 | boost::asio::io_context io; |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 198 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 199 | |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 200 | // test object server |
| 201 | conn->request_name("xyz.openbmc_project.asio-test"); |
| 202 | auto server = sdbusplus::asio::object_server(conn); |
| 203 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 204 | server.add_interface("/xyz/openbmc_project/test", |
| 205 | "xyz.openbmc_project.test"); |
| 206 | // test generic properties |
| 207 | iface->register_property("int", 33, |
| 208 | sdbusplus::asio::PropertyPermission::readWrite); |
| 209 | std::vector<std::string> myStringVec = {"some", "test", "data"}; |
| 210 | std::vector<std::string> myStringVec2 = {"more", "test", "data"}; |
| 211 | |
| 212 | iface->register_property("myStringVec", myStringVec, |
| 213 | sdbusplus::asio::PropertyPermission::readWrite); |
| 214 | iface->register_property("myStringVec2", myStringVec2); |
| 215 | |
| 216 | // test properties with specialized callbacks |
| 217 | iface->register_property("lessThan50", 23, |
| 218 | // custom set |
| 219 | [](const int& req, int& propertyValue) { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 220 | if (req >= 50) |
| 221 | { |
| 222 | return false; |
| 223 | } |
| 224 | propertyValue = req; |
| 225 | return true; |
| 226 | }); |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 227 | iface->register_property( |
| 228 | "TrailTime", std::string("foo"), |
| 229 | // custom set |
| 230 | [](const std::string& req, std::string& propertyValue) { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 231 | propertyValue = req; |
| 232 | return true; |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 233 | }, |
| 234 | // custom get |
| 235 | [](const std::string& property) { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 236 | auto now = std::chrono::system_clock::now(); |
| 237 | auto timePoint = std::chrono::system_clock::to_time_t(now); |
| 238 | return property + std::ctime(&timePoint); |
| 239 | }); |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 240 | |
| 241 | // test method creation |
| 242 | iface->register_method("TestMethod", [](const int32_t& callCount) { |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 243 | return std::make_tuple(callCount, |
| 244 | "success: " + std::to_string(callCount)); |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 245 | }); |
| 246 | |
| 247 | iface->register_method("TestFunction", foo); |
| 248 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 249 | // fooYield has boost::asio::yield_context as first argument |
| 250 | // so will be executed in coroutine context if called |
| 251 | iface->register_method("TestYieldFunction", |
| 252 | [conn](boost::asio::yield_context yield, int val) { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 253 | return fooYield(yield, conn, val); |
| 254 | }); |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 255 | |
Vernon Mauery | 8ce61e4 | 2018-08-28 08:54:59 -0700 | [diff] [blame] | 256 | iface->register_method("TestMethodWithMessage", methodWithMessage); |
| 257 | |
Vernon Mauery | 2c8f93f | 2018-09-05 12:02:39 -0700 | [diff] [blame] | 258 | iface->register_method("VoidFunctionReturnsInt", voidBar); |
| 259 | |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 260 | iface->register_method("execute", ipmiInterface); |
| 261 | |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 262 | iface->initialize(); |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 263 | |
| 264 | io.run(); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | int client() |
| 270 | { |
| 271 | using GetSubTreeType = std::vector<std::pair< |
| 272 | std::string, |
| 273 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 274 | using message = sdbusplus::message_t; |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 275 | |
| 276 | // setup connection to dbus |
Ed Tanous | c7d104d | 2019-01-02 14:15:50 -0800 | [diff] [blame] | 277 | boost::asio::io_context io; |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 278 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 279 | |
| 280 | int ready = 0; |
| 281 | while (!ready) |
| 282 | { |
| 283 | auto readyMsg = conn->new_method_call( |
| 284 | "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
| 285 | "xyz.openbmc_project.test", "VoidFunctionReturnsInt"); |
| 286 | try |
| 287 | { |
| 288 | message intMsg = conn->call(readyMsg); |
| 289 | intMsg.read(ready); |
| 290 | } |
Patrick Williams | b466765 | 2021-10-06 12:16:17 -0500 | [diff] [blame] | 291 | catch (const sdbusplus::exception::SdBusError& e) |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 292 | { |
| 293 | ready = 0; |
| 294 | // pause to give the server a chance to start up |
| 295 | usleep(10000); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // test async method call and async send |
Patrick Williams | 1a25a10 | 2022-09-29 17:18:26 -0500 | [diff] [blame] | 300 | auto mesg = conn->new_method_call("xyz.openbmc_project.ObjectMapper", |
| 301 | "/xyz/openbmc_project/object_mapper", |
| 302 | "xyz.openbmc_project.ObjectMapper", |
| 303 | "GetSubTree"); |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 304 | |
Ed Tanous | d375eba | 2023-01-06 13:29:59 -0800 | [diff] [blame] | 305 | int32_t depth = 2; |
| 306 | constexpr std::array<std::string_view, 1> interfaces{ |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 307 | "xyz.openbmc_project.Sensor.Value"}; |
| 308 | mesg.append("/xyz/openbmc_project/Sensors", depth, interfaces); |
| 309 | |
| 310 | conn->async_send(mesg, [](boost::system::error_code ec, message& ret) { |
| 311 | std::cout << "async_send callback\n"; |
| 312 | if (ec || ret.is_method_error()) |
| 313 | { |
| 314 | std::cerr << "error with async_send\n"; |
| 315 | return; |
| 316 | } |
| 317 | GetSubTreeType data; |
| 318 | ret.read(data); |
| 319 | for (auto& item : data) |
| 320 | { |
| 321 | std::cout << item.first << "\n"; |
| 322 | } |
| 323 | }); |
| 324 | |
| 325 | conn->async_method_call( |
| 326 | [](boost::system::error_code ec, GetSubTreeType& subtree) { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 327 | std::cout << "async_method_call callback\n"; |
| 328 | if (ec) |
| 329 | { |
| 330 | std::cerr << "error with async_method_call\n"; |
| 331 | return; |
| 332 | } |
| 333 | for (auto& item : subtree) |
| 334 | { |
| 335 | std::cout << item.first << "\n"; |
| 336 | } |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 337 | }, |
| 338 | "xyz.openbmc_project.ObjectMapper", |
| 339 | "/xyz/openbmc_project/object_mapper", |
| 340 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 341 | "/org/openbmc/control", 2, std::vector<std::string>()); |
Vernon Mauery | 035c73b | 2018-09-05 12:15:27 -0700 | [diff] [blame] | 342 | |
James Feist | c14699f | 2019-06-04 14:11:48 -0700 | [diff] [blame] | 343 | std::string nonConstCapture = "lalalala"; |
Vernon Mauery | c077190 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 344 | conn->async_method_call( |
James Feist | c14699f | 2019-06-04 14:11:48 -0700 | [diff] [blame] | 345 | [nonConstCapture = std::move(nonConstCapture)]( |
| 346 | boost::system::error_code ec, |
Patrick Williams | 78b7803 | 2020-05-20 10:32:05 -0500 | [diff] [blame] | 347 | const std::vector<std::string>& /*things*/) mutable { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 348 | std::cout << "async_method_call callback\n"; |
| 349 | nonConstCapture += " stuff"; |
| 350 | if (ec) |
| 351 | { |
| 352 | std::cerr << "async_method_call expected failure: " << ec << "\n"; |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | std::cerr << "async_method_call should have failed!\n"; |
| 357 | } |
Vernon Mauery | c077190 | 2019-05-07 16:53:50 -0700 | [diff] [blame] | 358 | }, |
| 359 | "xyz.openbmc_project.ObjectMapper", |
| 360 | "/xyz/openbmc_project/object_mapper", |
| 361 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 362 | "/xyz/openbmc_project/sensors", depth, interfaces); |
| 363 | |
Vernon Mauery | 035c73b | 2018-09-05 12:15:27 -0700 | [diff] [blame] | 364 | // sd_events work too using the default event loop |
| 365 | phosphor::Timer t1([]() { std::cerr << "*** tock ***\n"; }); |
| 366 | t1.start(std::chrono::microseconds(1000000)); |
| 367 | phosphor::Timer t2([]() { std::cerr << "*** tick ***\n"; }); |
| 368 | t2.start(std::chrono::microseconds(500000), true); |
| 369 | // add the sd_event wrapper to the io object |
| 370 | sdbusplus::asio::sd_event_wrapper sdEvents(io); |
| 371 | |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 372 | // set up a client to make an async call to the server |
| 373 | // using coroutines (userspace cooperative multitasking) |
| 374 | boost::asio::spawn(io, [conn](boost::asio::yield_context yield) { |
| 375 | do_start_async_method_call_one(conn, yield); |
| 376 | }); |
| 377 | boost::asio::spawn(io, [conn](boost::asio::yield_context yield) { |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 378 | do_start_async_ipmi_call(conn, yield); |
Vernon Mauery | 261e72b | 2018-09-25 12:34:25 -0700 | [diff] [blame] | 379 | }); |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 380 | boost::asio::spawn(io, [conn](boost::asio::yield_context yield) { |
| 381 | do_start_async_to_yield(conn, yield); |
| 382 | }); |
| 383 | |
| 384 | conn->async_method_call( |
| 385 | [](boost::system::error_code ec, int32_t testValue) { |
Patrick Williams | d214904 | 2023-05-10 07:50:13 -0500 | [diff] [blame] | 386 | if (ec) |
| 387 | { |
| 388 | std::cerr << "TestYieldFunction returned error with " |
| 389 | "async_method_call (ec = " |
| 390 | << ec << ")\n"; |
| 391 | return; |
| 392 | } |
| 393 | std::cout << "TestYieldFunction return " << testValue << "\n"; |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 394 | }, |
| 395 | "xyz.openbmc_project.asio-test", "/xyz/openbmc_project/test", |
| 396 | "xyz.openbmc_project.test", "TestYieldFunction", int32_t(41)); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 397 | io.run(); |
| 398 | |
| 399 | return 0; |
| 400 | } |
Vernon Mauery | 076d14a | 2018-10-02 15:10:20 -0700 | [diff] [blame] | 401 | |
| 402 | int main(int argc, const char* argv[]) |
| 403 | { |
| 404 | if (argc == 1) |
| 405 | { |
| 406 | int pid = fork(); |
| 407 | if (pid == 0) |
| 408 | { |
| 409 | return client(); |
| 410 | } |
| 411 | else if (pid > 0) |
| 412 | { |
| 413 | return server(); |
| 414 | } |
| 415 | return pid; |
| 416 | } |
| 417 | if (std::string(argv[1]) == "--server") |
| 418 | { |
| 419 | return server(); |
| 420 | } |
| 421 | if (std::string(argv[1]) == "--client") |
| 422 | { |
| 423 | return client(); |
| 424 | } |
| 425 | std::cout << "usage: " << argv[0] << " [--server | --client]\n"; |
| 426 | return -1; |
| 427 | } |