blob: 7477357ff3d33d7461dcb76b9752f9b160b2a531 [file] [log] [blame]
James Feist284a0f92018-04-05 15:28:16 -07001#include <iostream>
James Feistfce038a2018-04-13 15:43:13 -07002#include <ctime>
3#include <chrono>
James Feist284a0f92018-04-05 15:28:16 -07004#include <sdbusplus/bus.hpp>
5#include <sdbusplus/server.hpp>
6#include <sdbusplus/asio/connection.hpp>
James Feistfce038a2018-04-13 15:43:13 -07007#include <sdbusplus/asio/object_server.hpp>
James Feist284a0f92018-04-05 15:28:16 -07008#include <boost/asio.hpp>
9
James Feistfce038a2018-04-13 15:43:13 -070010int foo(int test)
11{
12 return ++test;
13}
14
Vernon Mauery8ce61e42018-08-28 08:54:59 -070015int methodWithMessage(sdbusplus::message::message& m, int test)
16{
17 return ++test;
18}
19
James Feist284a0f92018-04-05 15:28:16 -070020int 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 }
James Feist284a0f92018-04-05 15:28:16 -070068 },
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
James Feistfce038a2018-04-13 15:43:13 -070074 // 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
Vernon Mauery8ce61e42018-08-28 08:54:59 -0700122 iface->register_method("TestMethodWithMessage", methodWithMessage);
123
James Feistfce038a2018-04-13 15:43:13 -0700124 iface->initialize();
125 iface->set_property("int", 45);
James Feist284a0f92018-04-05 15:28:16 -0700126 io.run();
127
128 return 0;
129}