blob: 1cc56fff393b089859460de13426d0e14d61de49 [file] [log] [blame]
Patrick Williams1807fa42016-08-01 22:23:30 -05001#include <iostream>
Patrick Williams1807fa42016-08-01 22:23:30 -05002#include <cassert>
Patrick Williams7802c072016-09-02 15:20:22 -05003#include <sdbusplus/message.hpp>
Patrick Williams5b485792016-08-02 07:35:14 -05004#include <sdbusplus/bus.hpp>
Patrick Williams1807fa42016-08-01 22:23:30 -05005
6// Global to share the dbus type string between client and server.
7static std::string verifyTypeString;
8
9static constexpr auto SERVICE = "sdbusplus.test";
10static constexpr auto INTERFACE = SERVICE;
11static constexpr auto TEST_METHOD = "test";
12static constexpr auto QUIT_METHOD = "quit";
13
14// Open up the sdbus and claim SERVICE name.
Patrick Williams5b485792016-08-02 07:35:14 -050015auto serverInit()
Patrick Williams1807fa42016-08-01 22:23:30 -050016{
Patrick Williams5b485792016-08-02 07:35:14 -050017 auto b = sdbusplus::bus::new_default();
18 b.request_name(SERVICE);
19
20 return std::move(b);
Patrick Williams1807fa42016-08-01 22:23:30 -050021}
22
23// Thread to run the dbus server.
24void* server(void* b)
25{
Patrick Williams5b485792016-08-02 07:35:14 -050026 auto bus = sdbusplus::bus::bus(reinterpret_cast<sdbusplus::bus::busp_t>(b));
Patrick Williams1807fa42016-08-01 22:23:30 -050027
28 while(1)
29 {
30 // Wait for messages.
Patrick Williams5b485792016-08-02 07:35:14 -050031 sd_bus_message *m = bus.process();
Patrick Williams1807fa42016-08-01 22:23:30 -050032
Patrick Williams5b485792016-08-02 07:35:14 -050033 if(m == nullptr)
Patrick Williams1807fa42016-08-01 22:23:30 -050034 {
Patrick Williams5b485792016-08-02 07:35:14 -050035 bus.wait();
Patrick Williams1807fa42016-08-01 22:23:30 -050036 continue;
37 }
38
39 if (sd_bus_message_is_method_call(m, INTERFACE, TEST_METHOD))
40 {
41 // Verify the message type matches what the test expects.
42 // TODO: It would be nice to verify content here as well.
43 assert(verifyTypeString == sd_bus_message_get_signature(m, true));
44 // Reply to client.
45 sd_bus_reply_method_return(m, nullptr);
46 }
47 else if (sd_bus_message_is_method_call(m, INTERFACE, QUIT_METHOD))
48 {
49 // Reply and exit.
50 sd_bus_reply_method_return(m, nullptr);
51 break;
52 }
53 }
54}
55
Patrick Williams7802c072016-09-02 15:20:22 -050056auto newMethodCall__test(sdbusplus::bus::bus& b)
Patrick Williams1807fa42016-08-01 22:23:30 -050057{
58 // Allocate a method-call message for INTERFACE,TEST_METHOD.
Patrick Williams7802c072016-09-02 15:20:22 -050059 return b.new_method_call(SERVICE, "/", INTERFACE, TEST_METHOD);
Patrick Williams1807fa42016-08-01 22:23:30 -050060}
61
62void runTests()
63{
64 using namespace std::literals;
65
Patrick Williams5b485792016-08-02 07:35:14 -050066 auto b = sdbusplus::bus::new_default();
Patrick Williams1807fa42016-08-01 22:23:30 -050067
68 // Test r-value int.
69 {
Patrick Williams7802c072016-09-02 15:20:22 -050070 auto m = newMethodCall__test(b);
71 m.append(1);
Patrick Williams1807fa42016-08-01 22:23:30 -050072 verifyTypeString = "i";
Patrick Williams5b485792016-08-02 07:35:14 -050073 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -050074 }
Patrick Williams1807fa42016-08-01 22:23:30 -050075 // Test l-value int.
76 {
Patrick Williams7802c072016-09-02 15:20:22 -050077 auto m = newMethodCall__test(b);
Patrick Williams1807fa42016-08-01 22:23:30 -050078 int a = 1;
Patrick Williams7802c072016-09-02 15:20:22 -050079 m.append(a, a);
Patrick Williams1807fa42016-08-01 22:23:30 -050080 verifyTypeString = "ii";
Patrick Williams5b485792016-08-02 07:35:14 -050081 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -050082 }
83
84 // Test multiple ints.
85 {
Patrick Williams7802c072016-09-02 15:20:22 -050086 auto m = newMethodCall__test(b);
87 m.append(1, 2, 3, 4, 5);
Patrick Williams1807fa42016-08-01 22:23:30 -050088 verifyTypeString = "iiiii";
Patrick Williams5b485792016-08-02 07:35:14 -050089 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -050090 }
91
92 // Test r-value string.
93 {
Patrick Williams7802c072016-09-02 15:20:22 -050094 auto m = newMethodCall__test(b);
95 m.append("asdf"s);
Patrick Williams1807fa42016-08-01 22:23:30 -050096 verifyTypeString = "s";
Patrick Williams5b485792016-08-02 07:35:14 -050097 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -050098 }
99
100 // Test multiple strings, various forms.
101 {
Patrick Williams7802c072016-09-02 15:20:22 -0500102 auto m = newMethodCall__test(b);
Patrick Williams1807fa42016-08-01 22:23:30 -0500103 auto str = "jkl;"s;
104 auto str2 = "JKL:"s;
Patrick Williams7802c072016-09-02 15:20:22 -0500105 m.append(1, "asdf", "ASDF"s, str,
106 std::move(str2), 5);
Patrick Williams1807fa42016-08-01 22:23:30 -0500107 verifyTypeString = "issssi";
Patrick Williams5b485792016-08-02 07:35:14 -0500108 b.call_noreply(m);
Patrick Williams1807fa42016-08-01 22:23:30 -0500109 }
110
111 // Shutdown server.
Patrick Williams7802c072016-09-02 15:20:22 -0500112 {
113 auto m = b.new_method_call(SERVICE, "/", INTERFACE, QUIT_METHOD);
114 b.call_noreply(m);
115 }
Patrick Williams1807fa42016-08-01 22:23:30 -0500116}
117
118int main()
119{
120 // Initialize and start server thread.
121 pthread_t t;
122 {
Patrick Williams5b485792016-08-02 07:35:14 -0500123 auto b = serverInit();
124 pthread_create(&t, NULL, server, b.release());
Patrick Williams1807fa42016-08-01 22:23:30 -0500125 }
126
127 runTests();
128
129 // Wait for server thread to exit.
130 pthread_join(t, NULL);
131
132 return 0;
133}