1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-send.c Utility program to send messages from the command line
4 * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <dbus/dbus.h>
28 #include "dbus-print-message.h"
30 static const char *appname;
35 fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", appname);
40 append_arg (DBusMessageIter *iter, int type, const char *value)
46 dbus_bool_t v_BOOLEAN;
48 /* FIXME - we are ignoring OOM returns on all these functions */
52 byte = strtoul (value, NULL, 0);
53 dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
56 case DBUS_TYPE_DOUBLE:
57 d = strtod (value, NULL);
58 dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
62 int32 = strtol (value, NULL, 0);
63 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
66 case DBUS_TYPE_UINT32:
67 uint32 = strtoul (value, NULL, 0);
68 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
71 case DBUS_TYPE_STRING:
72 dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
75 case DBUS_TYPE_OBJECT_PATH:
76 dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
79 case DBUS_TYPE_BOOLEAN:
80 if (strcmp (value, "true") == 0)
83 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
85 else if (strcmp (value, "false") == 0)
88 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
92 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
98 fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
104 append_array (DBusMessageIter *iter, int type, const char *value)
108 append_arg (iter, type, value);
110 while ((c = strchr (c + 1, ',')) != NULL)
112 append_arg (iter, type, c);
117 main (int argc, char *argv[])
119 DBusConnection *connection;
121 DBusMessage *message;
124 DBusMessageIter iter;
126 DBusBusType type = DBUS_BUS_SESSION;
127 const char *dest = NULL;
128 const char *name = NULL;
129 const char *path = NULL;
130 int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
131 const char *type_str = NULL;
141 for (i = 1; i < argc && name == NULL; i++)
145 if (strcmp (arg, "--system") == 0)
146 type = DBUS_BUS_SYSTEM;
147 else if (strcmp (arg, "--session") == 0)
148 type = DBUS_BUS_SESSION;
149 else if (strcmp (arg, "--print-reply") == 0)
152 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
154 else if (strstr (arg, "--reply-timeout=") == arg)
156 reply_timeout = strtol (strchr (arg, '=') + 1,
159 else if (strstr (arg, "--dest=") == arg)
160 dest = strchr (arg, '=') + 1;
161 else if (strstr (arg, "--type=") == arg)
162 type_str = strchr (arg, '=') + 1;
163 else if (!strcmp(arg, "--help"))
165 else if (arg[0] == '-')
167 else if (path == NULL)
169 else if (name == NULL)
178 if (type_str != NULL)
180 message_type = dbus_message_type_from_string (type_str);
181 if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
182 message_type == DBUS_MESSAGE_TYPE_SIGNAL))
184 fprintf (stderr, "Message type \"%s\" is not supported\n",
190 dbus_error_init (&error);
191 connection = dbus_bus_get (type, &error);
192 if (connection == NULL)
194 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
195 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
197 dbus_error_free (&error);
201 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
205 last_dot = strrchr (name, '.');
206 if (last_dot == NULL)
208 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
214 message = dbus_message_new_method_call (NULL,
219 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
223 last_dot = strrchr (name, '.');
224 if (last_dot == NULL)
226 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
232 message = dbus_message_new_signal (path, name, last_dot + 1);
236 fprintf (stderr, "Internal error, unknown message type\n");
242 fprintf (stderr, "Couldn't allocate D-BUS message\n");
246 if (dest && !dbus_message_set_destination (message, dest))
248 fprintf (stderr, "Not enough memory\n");
252 dbus_message_iter_init_append (message, &iter);
260 DBusMessageIter *target_iter;
261 DBusMessageIter container_iter;
263 type = DBUS_TYPE_INVALID;
265 c = strchr (arg, ':');
269 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
275 container_type = DBUS_TYPE_INVALID;
277 if (strcmp (arg, "variant") == 0)
278 container_type = DBUS_TYPE_VARIANT;
279 else if (strcmp (arg, "array") == 0)
280 container_type = DBUS_TYPE_ARRAY;
282 if (container_type != DBUS_TYPE_INVALID)
285 c = strchr (arg, ':');
288 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
294 if (arg[0] == 0 || !strcmp (arg, "string"))
295 type = DBUS_TYPE_STRING;
296 else if (!strcmp (arg, "int32"))
297 type = DBUS_TYPE_INT32;
298 else if (!strcmp (arg, "uint32"))
299 type = DBUS_TYPE_UINT32;
300 else if (!strcmp (arg, "double"))
301 type = DBUS_TYPE_DOUBLE;
302 else if (!strcmp (arg, "byte"))
303 type = DBUS_TYPE_BYTE;
304 else if (!strcmp (arg, "boolean"))
305 type = DBUS_TYPE_BOOLEAN;
306 else if (!strcmp (arg, "objpath"))
307 type = DBUS_TYPE_OBJECT_PATH;
310 fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
314 if (container_type != DBUS_TYPE_INVALID)
319 dbus_message_iter_open_container (&iter,
323 target_iter = &container_iter;
328 if (container_type == DBUS_TYPE_ARRAY)
330 append_array (target_iter, type, c);
333 append_arg (target_iter, type, c);
335 if (container_type != DBUS_TYPE_INVALID)
337 dbus_message_iter_close_container (&iter,
346 dbus_error_init (&error);
347 reply = dbus_connection_send_with_reply_and_block (connection,
348 message, reply_timeout,
350 if (dbus_error_is_set (&error))
352 fprintf (stderr, "Error: %s\n",
359 print_message (reply);
360 dbus_message_unref (reply);
365 dbus_connection_send (connection, message, NULL);
366 dbus_connection_flush (connection);
369 dbus_message_unref (message);
371 dbus_connection_disconnect (connection);