stktest: Add send_with_reply utility
authorDenis Kenzior <denkenz@gmail.com>
Thu, 27 Sep 2012 17:36:25 +0000 (12:36 -0500)
committerDenis Kenzior <denkenz@gmail.com>
Wed, 3 Oct 2012 14:10:42 +0000 (09:10 -0500)
tools/stktest.c

index 2da0b86..030594c 100644 (file)
@@ -353,6 +353,64 @@ static gboolean has_stk_interface(DBusMessageIter *iter)
        return FALSE;
 }
 
+static int send_with_reply(const char *path, const char *interface,
+                               const char *method, DBusPendingCall **call,
+                               DBusPendingCallNotifyFunction cb,
+                               void *user_data, DBusFreeFunction free_func,
+                               int timeout, int type, ...)
+{
+       DBusMessage *msg;
+       DBusPendingCall *c;
+       va_list args;
+       int err;
+
+       msg = dbus_message_new_method_call(OFONO_SERVICE, path,
+                                               interface, method);
+       if (msg == NULL) {
+               g_printerr("Unable to allocate new D-Bus %s message\n", method);
+               err = -ENOMEM;
+               goto fail;
+       }
+
+       va_start(args, type);
+
+       if (!dbus_message_append_args_valist(msg, type, args)) {
+               va_end(args);
+               err = -EIO;
+               goto fail;
+       }
+
+       va_end(args);
+
+       if (timeout > 0)
+               timeout *= 1000;
+
+       if (!dbus_connection_send_with_reply(conn, msg, &c, timeout)) {
+               g_printerr("Sending %s failed\n", method);
+               err = -EIO;
+               goto fail;
+       }
+
+       if (call != NULL)
+               *call = c;
+
+       dbus_pending_call_set_notify(c, cb, user_data, free_func);
+       dbus_pending_call_unref(c);
+
+       dbus_message_unref(msg);
+
+       return 0;
+
+fail:
+       if (free_func && user_data)
+               free_func(user_data);
+
+       if (msg)
+               dbus_message_unref(msg);
+
+       return err;
+}
+
 static void set_property_reply(DBusPendingCall *call, void *user_data)
 {
        DBusMessage *reply = dbus_pending_call_steal_reply(call);