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