2 * Copyright 2008-2011 Novell, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include "dbind/dbind.h"
28 static int dbind_timeout = -1;
31 * FIXME: compare types - to ensure they match &
32 * do dynamic padding of structures etc.
35 /*---------------------------------------------------------------------------*/
37 typedef struct _SpiReentrantCallClosure
40 } SpiReentrantCallClosure;
43 set_reply (DBusPendingCall * pending, void *user_data)
45 SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data;
47 closure->reply = dbus_pending_call_steal_reply (pending);
48 dbus_pending_call_unref (pending);
52 time_elapsed (struct timeval *origin)
56 gettimeofday (&tv, NULL);
57 return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
61 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
63 DBusPendingCall *pending;
64 SpiReentrantCallClosure closure;
65 const char *unique_name = dbus_bus_get_unique_name (bus);
66 const char *destination = dbus_message_get_destination (message);
69 if (unique_name && destination &&
70 strcmp (destination, unique_name) != 0)
71 return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
74 dbus_connection_setup_with_g_main(bus, NULL);
75 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout))
79 dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
82 gettimeofday (&tv, NULL);
83 while (!closure.reply)
85 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
87 if (time_elapsed (&tv) > dbind_timeout)
95 dbind_method_call_reentrant_va (DBusConnection *cnx,
98 const char *interface,
100 DBusError *opt_error,
101 const char *arg_types,
104 dbus_bool_t success = FALSE;
105 DBusMessage *msg = NULL, *reply = NULL;
106 DBusMessageIter iter;
107 DBusError *err, real_err;
109 va_list args_demarshal;
111 va_copy (args_demarshal, args);
115 dbus_error_init (&real_err);
119 msg = dbus_message_new_method_call (bus_name, path, interface, method);
124 dbus_message_iter_init_append (msg, &iter);
125 dbind_any_marshal_va (&iter, &p, args);
127 reply = dbind_send_and_allow_reentry (cnx, msg, err);
131 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
133 const char *name = dbus_message_get_error_name (reply);
137 if (p[0] == '=' && p[1] == '>')
139 DBusMessageIter iter;
140 dbus_message_iter_init (reply, &iter);
142 dbind_any_demarshal_va (&iter, &p, args_demarshal);
148 dbus_message_unref (msg);
151 dbus_message_unref (reply);
153 if (err == &real_err)
154 dbus_error_free (err);
156 va_end (args_demarshal);
161 * dbind_method_call_reentrant:
163 * @cnx: A D-Bus Connection used to make the method call.
164 * @bus_name: The D-Bus bus name of the program where the method call should
166 * @path: The D-Bus object path that should handle the method.
167 * @interface: The D-Bus interface used to scope the method name.
168 * @method: Method to be invoked.
169 * @opt_error: D-Bus error.
170 * @arg_types: Variable length arguments interleaving D-Bus argument types
171 * and pointers to argument data.
173 * Makes a D-Bus method call using the supplied location data, method name and
174 * argument data.This function is re-entrant. It continuously reads from the D-Bus
175 * bus and dispatches messages until a reply has been recieved.
178 dbind_method_call_reentrant (DBusConnection *cnx,
179 const char *bus_name,
181 const char *interface,
183 DBusError *opt_error,
184 const char *arg_types,
187 dbus_bool_t success = FALSE;
190 va_start (args, arg_types);
191 success = dbind_method_call_reentrant_va (cnx,
204 /*---------------------------------------------------------------------------*/
207 dbind_emit_signal_va (DBusConnection *cnx,
209 const char *interface,
211 DBusError *opt_error,
212 const char *arg_types,
215 dbus_bool_t success = FALSE;
216 DBusMessage *msg = NULL;
217 DBusMessageIter iter;
218 DBusError *err, real_err;
224 dbus_error_init (&real_err);
228 msg = dbus_message_new_signal (path, interface, signal);
233 dbus_message_iter_init_append (msg, &iter);
234 dbind_any_marshal_va (&iter, &p, args);
236 if (!dbus_connection_send (cnx, msg, NULL))
243 dbus_message_unref (msg);
245 if (err == &real_err)
246 dbus_error_free (err);
254 * @cnx: A D-Bus Connection used to make the method call.
255 * @path: The D-Bus object path that this signal is emitted from.
256 * @interface: The D-Bus interface used to scope the method name.
257 * @signal: Name of signal to emit.
258 * @opt_error: D-Bus error.
259 * @arg_types: Variable length arguments interleaving D-Bus argument types
260 * and pointers to argument data.
262 * Emits a D-Bus signal using the supplied signal name and argument data.
265 dbind_emit_signal (DBusConnection *cnx,
267 const char *interface,
269 DBusError *opt_error,
270 const char *arg_types,
273 dbus_bool_t success = FALSE;
276 va_start (args, arg_types);
277 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
283 dbind_set_timeout (int timeout)
285 dbind_timeout = timeout;
289 /*END------------------------------------------------------------------------*/