Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 1 | #include <iostream> |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 2 | #include <cassert> |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 3 | #include <sdbusplus/message.hpp> |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 5 | |
| 6 | // Global to share the dbus type string between client and server. |
| 7 | static std::string verifyTypeString; |
| 8 | |
| 9 | static constexpr auto SERVICE = "sdbusplus.test"; |
| 10 | static constexpr auto INTERFACE = SERVICE; |
| 11 | static constexpr auto TEST_METHOD = "test"; |
| 12 | static constexpr auto QUIT_METHOD = "quit"; |
| 13 | |
| 14 | // Open up the sdbus and claim SERVICE name. |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 15 | auto serverInit() |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 16 | { |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 17 | auto b = sdbusplus::bus::new_default(); |
| 18 | b.request_name(SERVICE); |
| 19 | |
| 20 | return std::move(b); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Thread to run the dbus server. |
| 24 | void* server(void* b) |
| 25 | { |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 26 | auto bus = sdbusplus::bus::bus(reinterpret_cast<sdbusplus::bus::busp_t>(b)); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 27 | |
| 28 | while(1) |
| 29 | { |
| 30 | // Wait for messages. |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 31 | sd_bus_message *m = bus.process(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 32 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 33 | if(m == nullptr) |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 34 | { |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 35 | bus.wait(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 36 | 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 Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 56 | auto newMethodCall__test(sdbusplus::bus::bus& b) |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 57 | { |
| 58 | // Allocate a method-call message for INTERFACE,TEST_METHOD. |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 59 | return b.new_method_call(SERVICE, "/", INTERFACE, TEST_METHOD); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void runTests() |
| 63 | { |
| 64 | using namespace std::literals; |
| 65 | |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 66 | auto b = sdbusplus::bus::new_default(); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 67 | |
| 68 | // Test r-value int. |
| 69 | { |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 70 | auto m = newMethodCall__test(b); |
| 71 | m.append(1); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 72 | verifyTypeString = "i"; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 73 | b.call_noreply(m); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 74 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 75 | // Test l-value int. |
| 76 | { |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 77 | auto m = newMethodCall__test(b); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 78 | int a = 1; |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 79 | m.append(a, a); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 80 | verifyTypeString = "ii"; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 81 | b.call_noreply(m); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Test multiple ints. |
| 85 | { |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 86 | auto m = newMethodCall__test(b); |
| 87 | m.append(1, 2, 3, 4, 5); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 88 | verifyTypeString = "iiiii"; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 89 | b.call_noreply(m); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Test r-value string. |
| 93 | { |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 94 | auto m = newMethodCall__test(b); |
| 95 | m.append("asdf"s); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 96 | verifyTypeString = "s"; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 97 | b.call_noreply(m); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Test multiple strings, various forms. |
| 101 | { |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 102 | auto m = newMethodCall__test(b); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 103 | auto str = "jkl;"s; |
| 104 | auto str2 = "JKL:"s; |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 105 | m.append(1, "asdf", "ASDF"s, str, |
| 106 | std::move(str2), 5); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 107 | verifyTypeString = "issssi"; |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 108 | b.call_noreply(m); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Shutdown server. |
Patrick Williams | 7802c07 | 2016-09-02 15:20:22 -0500 | [diff] [blame^] | 112 | { |
| 113 | auto m = b.new_method_call(SERVICE, "/", INTERFACE, QUIT_METHOD); |
| 114 | b.call_noreply(m); |
| 115 | } |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int main() |
| 119 | { |
| 120 | // Initialize and start server thread. |
| 121 | pthread_t t; |
| 122 | { |
Patrick Williams | 5b48579 | 2016-08-02 07:35:14 -0500 | [diff] [blame] | 123 | auto b = serverInit(); |
| 124 | pthread_create(&t, NULL, server, b.release()); |
Patrick Williams | 1807fa4 | 2016-08-01 22:23:30 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | runTests(); |
| 128 | |
| 129 | // Wait for server thread to exit. |
| 130 | pthread_join(t, NULL); |
| 131 | |
| 132 | return 0; |
| 133 | } |