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