Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 1 | #include <boost/asio.hpp> |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 2 | #include <chrono> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 3 | #include <ctime> |
| 4 | #include <iostream> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 5 | #include <sdbusplus/asio/connection.hpp> |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 6 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/server.hpp> |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 9 | |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 10 | int foo(int test) |
| 11 | { |
| 12 | return ++test; |
| 13 | } |
| 14 | |
Vernon Mauery | 8ce61e4 | 2018-08-28 08:54:59 -0700 | [diff] [blame] | 15 | int methodWithMessage(sdbusplus::message::message& m, int test) |
| 16 | { |
| 17 | return ++test; |
| 18 | } |
| 19 | |
Vernon Mauery | 2c8f93f | 2018-09-05 12:02:39 -0700 | [diff] [blame] | 20 | int voidBar(void) |
| 21 | { |
| 22 | return 42; |
| 23 | } |
| 24 | |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 25 | int main() |
| 26 | { |
| 27 | using GetSubTreeType = std::vector<std::pair< |
| 28 | std::string, |
| 29 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 30 | using message = sdbusplus::message::message; |
| 31 | // setup connection to dbus |
| 32 | boost::asio::io_service io; |
| 33 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 34 | |
| 35 | // test async method call and async send |
| 36 | auto mesg = |
| 37 | conn->new_method_call("xyz.openbmc_project.ObjectMapper", |
| 38 | "/xyz/openbmc_project/object_mapper", |
| 39 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
| 40 | |
| 41 | static const auto depth = 2; |
| 42 | static const std::vector<std::string> interfaces = { |
| 43 | "xyz.openbmc_project.Sensor.Value"}; |
| 44 | mesg.append("/xyz/openbmc_project/Sensors", depth, interfaces); |
| 45 | |
| 46 | conn->async_send(mesg, [](boost::system::error_code ec, message& ret) { |
| 47 | std::cout << "async_send callback\n"; |
| 48 | if (ec || ret.is_method_error()) |
| 49 | { |
| 50 | std::cerr << "error with async_send\n"; |
| 51 | return; |
| 52 | } |
| 53 | GetSubTreeType data; |
| 54 | ret.read(data); |
| 55 | for (auto& item : data) |
| 56 | { |
| 57 | std::cout << item.first << "\n"; |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | conn->async_method_call( |
| 62 | [](boost::system::error_code ec, GetSubTreeType& subtree) { |
| 63 | std::cout << "async_method_call callback\n"; |
| 64 | if (ec) |
| 65 | { |
| 66 | std::cerr << "error with async_method_call\n"; |
| 67 | return; |
| 68 | } |
| 69 | for (auto& item : subtree) |
| 70 | { |
| 71 | std::cout << item.first << "\n"; |
| 72 | } |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 73 | }, |
| 74 | "xyz.openbmc_project.ObjectMapper", |
| 75 | "/xyz/openbmc_project/object_mapper", |
| 76 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 77 | "/org/openbmc/control", 2, std::vector<std::string>()); |
| 78 | |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 79 | // test object server |
| 80 | conn->request_name("xyz.openbmc_project.asio-test"); |
| 81 | auto server = sdbusplus::asio::object_server(conn); |
| 82 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 83 | server.add_interface("/xyz/openbmc_project/test", |
| 84 | "xyz.openbmc_project.test"); |
| 85 | // test generic properties |
| 86 | iface->register_property("int", 33, |
| 87 | sdbusplus::asio::PropertyPermission::readWrite); |
| 88 | std::vector<std::string> myStringVec = {"some", "test", "data"}; |
| 89 | std::vector<std::string> myStringVec2 = {"more", "test", "data"}; |
| 90 | |
| 91 | iface->register_property("myStringVec", myStringVec, |
| 92 | sdbusplus::asio::PropertyPermission::readWrite); |
| 93 | iface->register_property("myStringVec2", myStringVec2); |
| 94 | |
| 95 | // test properties with specialized callbacks |
| 96 | iface->register_property("lessThan50", 23, |
| 97 | // custom set |
| 98 | [](const int& req, int& propertyValue) { |
| 99 | if (req >= 50) |
| 100 | { |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | propertyValue = req; |
| 104 | return 1; // success |
| 105 | }); |
| 106 | iface->register_property( |
| 107 | "TrailTime", std::string("foo"), |
| 108 | // custom set |
| 109 | [](const std::string& req, std::string& propertyValue) { |
| 110 | propertyValue = req; |
| 111 | return 1; // success |
| 112 | }, |
| 113 | // custom get |
| 114 | [](const std::string& property) { |
| 115 | auto now = std::chrono::system_clock::now(); |
| 116 | auto timePoint = std::chrono::system_clock::to_time_t(now); |
| 117 | return property + std::ctime(&timePoint); |
| 118 | }); |
| 119 | |
| 120 | // test method creation |
| 121 | iface->register_method("TestMethod", [](const int32_t& callCount) { |
| 122 | return "success: " + std::to_string(callCount); |
| 123 | }); |
| 124 | |
| 125 | iface->register_method("TestFunction", foo); |
| 126 | |
Vernon Mauery | 8ce61e4 | 2018-08-28 08:54:59 -0700 | [diff] [blame] | 127 | iface->register_method("TestMethodWithMessage", methodWithMessage); |
| 128 | |
Vernon Mauery | 2c8f93f | 2018-09-05 12:02:39 -0700 | [diff] [blame] | 129 | iface->register_method("VoidFunctionReturnsInt", voidBar); |
| 130 | |
James Feist | fce038a | 2018-04-13 15:43:13 -0700 | [diff] [blame] | 131 | iface->initialize(); |
| 132 | iface->set_property("int", 45); |
James Feist | 284a0f9 | 2018-04-05 15:28:16 -0700 | [diff] [blame] | 133 | io.run(); |
| 134 | |
| 135 | return 0; |
| 136 | } |