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"
31 usage (char *name, int ecode)
33 fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=SERVICE] [--type=TYPE] [--print-reply] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", name);
38 main (int argc, char *argv[])
40 DBusConnection *connection;
47 DBusBusType type = DBUS_BUS_SESSION;
48 const char *dest = NULL;
49 const char *name = NULL;
50 const char *path = NULL;
51 int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
52 const char *type_str = NULL;
60 for (i = 1; i < argc && name == NULL; i++)
64 if (strcmp (arg, "--system") == 0)
65 type = DBUS_BUS_SYSTEM;
66 else if (strcmp (arg, "--session") == 0)
67 type = DBUS_BUS_SESSION;
68 else if (strcmp (arg, "--print-reply") == 0)
71 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
73 else if (strstr (arg, "--reply-timeout=") == arg)
75 reply_timeout = strtol (strchr (arg, '=') + 1,
78 else if (strstr (arg, "--dest=") == arg)
79 dest = strchr (arg, '=') + 1;
80 else if (strstr (arg, "--type=") == arg)
81 type_str = strchr (arg, '=') + 1;
82 else if (!strcmp(arg, "--help"))
84 else if (arg[0] == '-')
86 else if (path == NULL)
88 else if (name == NULL)
99 message_type = dbus_message_type_from_string (type_str);
100 if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
101 message_type == DBUS_MESSAGE_TYPE_SIGNAL))
103 fprintf (stderr, "Message type \"%s\" is not supported\n",
109 dbus_error_init (&error);
110 connection = dbus_bus_get (type, &error);
111 if (connection == NULL)
113 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
114 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
116 dbus_error_free (&error);
120 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
124 last_dot = strrchr (name, '.');
125 if (last_dot == NULL)
127 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
133 message = dbus_message_new_method_call (NULL,
138 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
142 last_dot = strrchr (name, '.');
143 if (last_dot == NULL)
145 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
151 message = dbus_message_new_signal (path, name, last_dot + 1);
155 fprintf (stderr, "Internal error, unknown message type\n");
161 fprintf (stderr, "Couldn't allocate D-BUS message\n");
165 if (dest && !dbus_message_set_destination (message, dest))
167 fprintf (stderr, "Not enough memory\n");
171 dbus_message_iter_init_append (message, &iter);
178 dbus_uint32_t uint32;
183 type = DBUS_TYPE_INVALID;
185 c = strchr (arg, ':');
189 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
195 if (arg[0] == 0 || !strcmp (arg, "string"))
196 type = DBUS_TYPE_STRING;
197 else if (!strcmp (arg, "int32"))
198 type = DBUS_TYPE_INT32;
199 else if (!strcmp (arg, "uint32"))
200 type = DBUS_TYPE_UINT32;
201 else if (!strcmp (arg, "double"))
202 type = DBUS_TYPE_DOUBLE;
203 else if (!strcmp (arg, "byte"))
204 type = DBUS_TYPE_BYTE;
205 else if (!strcmp (arg, "boolean"))
206 type = DBUS_TYPE_BOOLEAN;
209 fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
213 /* FIXME - we are ignoring OOM returns on all these functions */
217 byte = strtoul (c, NULL, 0);
218 dbus_message_iter_append_basic (&iter, DBUS_TYPE_BYTE, &byte);
221 case DBUS_TYPE_DOUBLE:
222 d = strtod (c, NULL);
223 dbus_message_iter_append_basic (&iter, DBUS_TYPE_DOUBLE, &d);
226 case DBUS_TYPE_INT32:
227 int32 = strtol (c, NULL, 0);
228 dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &int32);
231 case DBUS_TYPE_UINT32:
232 uint32 = strtoul (c, NULL, 0);
233 dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &uint32);
236 case DBUS_TYPE_STRING:
237 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &c);
240 case DBUS_TYPE_BOOLEAN:
241 if (strcmp(c, "true") == 0)
244 dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &c);
246 else if (strcmp(c, "false") == 0)
249 dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &c);
253 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", argv[0], c);
259 fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
268 dbus_error_init (&error);
269 reply = dbus_connection_send_with_reply_and_block (connection,
270 message, reply_timeout,
272 if (dbus_error_is_set (&error))
274 fprintf (stderr, "Error: %s\n",
281 print_message (reply);
282 dbus_message_unref (reply);
287 dbus_connection_send (connection, message, NULL);
288 dbus_connection_flush (connection);
291 dbus_message_unref (message);
293 dbus_connection_disconnect (connection);