1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
36 #include "bus-message.h"
39 int main(int argc, char *argv[]) {
40 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
42 const char *x, *y, *z, *a, *b, *c, *d;
47 const int32_t integer_array[] = { -1, -2, 0, 1, 2 }, *return_array;
50 r = sd_bus_message_new_method_call(NULL, "foobar.waldo", "/", "foobar.waldo", "Piep", &m);
53 r = sd_bus_message_append(m, "s", "a string");
56 r = sd_bus_message_append(m, "s", NULL);
59 r = sd_bus_message_append(m, "as", 2, "string #1", "string #2");
62 r = sd_bus_message_append(m, "sass", "foobar", 5, "foo", "bar", "waldo", "piep", "pap", "after");
65 r = sd_bus_message_append(m, "a{yv}", 2, 3, "s", "foo", 5, "s", "waldo");
68 r = sd_bus_message_append(m, "ba(ss)", 255, 3, "aaa", "1", "bbb", "2", "ccc", "3");
71 r = sd_bus_message_open_container(m, 'a', "s");
74 r = sd_bus_message_append_basic(m, 's', "foobar");
77 r = sd_bus_message_append_basic(m, 's', "waldo");
80 r = sd_bus_message_close_container(m);
83 r = sd_bus_message_append_string_space(m, 5, &s);
87 r = sd_bus_message_append_array(m, 'i', integer_array, sizeof(integer_array));
90 r = bus_message_seal(m, 4711);
95 r = bus_message_get_blob(m, &buffer, &sz);
98 h = hexmem(buffer, sz);
101 log_info("message size = %lu, contents =\n%s", (unsigned long) sz, h);
109 #if !defined(GLIB_VERSION_2_36)
113 g = g_dbus_message_new_from_blob(buffer, sz, 0, NULL);
114 p = g_dbus_message_print(g, 0);
125 dbus_error_init(&error);
127 w = dbus_message_demarshal(buffer, sz, &error);
129 log_error("%s", error.message);
131 dbus_message_unref(w);
134 m = sd_bus_message_unref(m);
136 r = bus_message_from_malloc(buffer, sz, NULL, 0, NULL, NULL, &m);
141 assert_se(sd_bus_message_rewind(m, true) >= 0);
143 r = sd_bus_message_read(m, "sas", &x, 2, &y, &z);
145 assert_se(streq(x, "a string"));
146 assert_se(streq(y, "string #1"));
147 assert_se(streq(z, "string #2"));
149 r = sd_bus_message_read(m, "sass", &x, 5, &y, &z, &a, &b, &c, &d);
151 assert_se(streq(x, "foobar"));
152 assert_se(streq(y, "foo"));
153 assert_se(streq(z, "bar"));
154 assert_se(streq(a, "waldo"));
155 assert_se(streq(b, "piep"));
156 assert_se(streq(c, "pap"));
157 assert_se(streq(d, "after"));
159 r = sd_bus_message_read(m, "a{yv}", 2, &u, "s", &x, &v, "s", &y);
162 assert_se(streq(x, "foo"));
164 assert_se(streq(y, "waldo"));
166 r = sd_bus_message_read(m, "ba(ss)", &boolean, 3, &x, &y, &a, &b, &c, &d);
169 assert_se(streq(x, "aaa"));
170 assert_se(streq(y, "1"));
171 assert_se(streq(a, "bbb"));
172 assert_se(streq(b, "2"));
173 assert_se(streq(c, "ccc"));
174 assert_se(streq(d, "3"));
176 r = sd_bus_message_read(m, "as", 2, &x, &y);
178 assert_se(streq(x, "foobar"));
179 assert_se(streq(y, "waldo"));
181 r = sd_bus_message_read_basic(m, 's', &s);
183 assert_se(streq(s, "hallo"));
185 r = sd_bus_message_read_array(m, 'i', (const void**) &return_array, &sz);
187 assert_se(sz == sizeof(integer_array));
188 assert_se(memcmp(integer_array, return_array, sz) == 0);
190 r = sd_bus_message_peek_type(m, NULL, NULL);