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