client: Add connmanctl D-Bus helper functions
authorPatrik Flykt <patrik.flykt@linux.intel.com>
Wed, 27 Mar 2013 11:53:47 +0000 (13:53 +0200)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Thu, 4 Apr 2013 07:20:22 +0000 (10:20 +0300)
Add helper functions for pretty-printing D-Bus messages and making a
method call.

Makefile.am
client/dbus_helpers.c [new file with mode: 0644]
client/dbus_helpers.h [new file with mode: 0644]

index 78b1b33..21b7070 100644 (file)
@@ -229,6 +229,7 @@ noinst_PROGRAMS += client/connmanctl
 MANUAL_PAGES += doc/connmanctl.1
 
 client_connmanctl_SOURCES =  $(gdbus_sources) src/connman.h \
+                       client/dbus_helpers.h client/dbus_helpers.c \
                        client/dbus.h client/dbus.c \
                        client/data_manager.h client/data_manager.c \
                        client/services.h client/services.c \
diff --git a/client/dbus_helpers.c b/client/dbus_helpers.c
new file mode 100644 (file)
index 0000000..816da73
--- /dev/null
@@ -0,0 +1,193 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <glib.h>
+
+#include "dbus_helpers.h"
+
+#define TIMEOUT         60000
+
+void __connmanctl_dbus_print(DBusMessageIter *iter, const char *pre,
+               const char *dict, const char *sep)
+{
+       int arg_type;
+       dbus_bool_t b;
+       unsigned char c;
+       unsigned int i;
+       double d;
+
+       char *str;
+       DBusMessageIter entry;
+
+       if (pre == NULL)
+               pre = "";
+
+       while ((arg_type = dbus_message_iter_get_arg_type(iter))
+                       != DBUS_TYPE_INVALID) {
+
+               fprintf(stdout, "%s", pre);
+
+               switch (arg_type) {
+               case DBUS_TYPE_STRUCT:
+                       fprintf(stdout, "{ ");
+                       dbus_message_iter_recurse(iter, &entry);
+                       __connmanctl_dbus_print(&entry, "", "=", " ");
+                       fprintf(stdout, " }");
+                       break;
+
+               case DBUS_TYPE_ARRAY:
+                       fprintf(stdout, "[ ");
+
+                       dbus_message_iter_recurse(iter, &entry);
+                       __connmanctl_dbus_print(&entry, "", "=", ", ");
+
+                       fprintf(stdout, " ]");
+                       break;
+
+               case DBUS_TYPE_DICT_ENTRY:
+
+                       dbus_message_iter_recurse(iter, &entry);
+                       __connmanctl_dbus_print(&entry, "", dict, dict);
+                       break;
+
+               case DBUS_TYPE_STRING:
+               case DBUS_TYPE_OBJECT_PATH:
+                       dbus_message_iter_get_basic(iter, &str);
+                       fprintf(stdout, "%s", str);
+                       break;
+
+               case DBUS_TYPE_VARIANT:
+                       dbus_message_iter_recurse(iter, &entry);
+                       __connmanctl_dbus_print(&entry, pre, dict, sep);
+                       break;
+
+               case DBUS_TYPE_BOOLEAN:
+                       dbus_message_iter_get_basic(iter, &b);
+                       if (b == FALSE)
+                               fprintf(stdout, "False");
+                       else
+                               fprintf(stdout, "True");
+                       break;
+
+               case DBUS_TYPE_BYTE:
+                       dbus_message_iter_get_basic(iter, &c);
+                       fprintf(stdout, "%d", c);
+                       break;
+
+               case DBUS_TYPE_UINT16:
+               case DBUS_TYPE_UINT32:
+                       dbus_message_iter_get_basic(iter, &i);
+                       fprintf(stdout, "%d", i);
+                       break;
+
+               case DBUS_TYPE_DOUBLE:
+                       dbus_message_iter_get_basic(iter, &d);
+                       fprintf(stdout, "%f", d);
+                       break;
+
+               default:
+                       fprintf(stdout, "<type %c>", arg_type);
+                       break;
+               }
+
+               if (dbus_message_iter_has_next(iter) == TRUE)
+                       fprintf(stdout, "%s", sep);
+
+               dbus_message_iter_next(iter);
+       }
+}
+
+struct dbus_callback {
+       connmanctl_dbus_method_return_func_t cb;
+       void *user_data;
+};
+
+static void dbus_method_reply(DBusPendingCall *call, void *user_data)
+{
+       struct dbus_callback *callback = user_data;
+       DBusMessage *reply;
+       DBusMessageIter iter;
+
+       reply = dbus_pending_call_steal_reply(call);
+       if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
+               DBusError err;
+
+               dbus_error_init(&err);
+               dbus_set_error_from_message(&err, reply);
+
+               callback->cb(NULL, err.message, callback->user_data);
+
+               dbus_error_free(&err);
+               goto end;
+       }
+
+       dbus_message_iter_init(reply, &iter);
+       callback->cb(&iter, NULL, callback->user_data);
+
+end:
+       g_free(callback);
+       dbus_message_unref(reply);
+}
+
+int __connmanctl_dbus_method_call(DBusConnection *connection, const char *path,
+               const char *interface, const char *method,
+               connmanctl_dbus_method_return_func_t cb, void * user_data,
+               int arg1, ...)
+{
+       int res = -ENXIO;
+       DBusMessage *message;
+       va_list args;
+       DBusPendingCall *call;
+       struct dbus_callback *callback;
+
+       message = dbus_message_new_method_call("net.connman", path,
+                       interface, method);
+
+       if (message == NULL)
+               return -ENOMEM;
+
+       va_start(args, arg1);
+       dbus_message_append_args_valist(message, arg1, args);
+       va_end(args);
+
+       if (dbus_connection_send_with_reply(connection, message, &call,
+                                       TIMEOUT) == FALSE)
+               goto end;
+
+        if (call == NULL)
+                goto end;
+
+       if (cb != NULL) {
+               callback = g_new0(struct dbus_callback, 1);
+               callback->cb = cb;
+               callback->user_data = user_data;
+               dbus_pending_call_set_notify(call, dbus_method_reply,
+                               callback, NULL);
+               res = -EINPROGRESS;
+       }
+
+end:
+        dbus_message_unref(message);
+       return res;
+}
diff --git a/client/dbus_helpers.h b/client/dbus_helpers.h
new file mode 100644 (file)
index 0000000..be312d7
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __CONNMANCTL_DBUS_HELPERS_H
+#define __CONNMANCTL_DBUS_HELPERS_H
+
+#include <dbus/dbus.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void __connmanctl_dbus_print(DBusMessageIter *iter, const char *pre,
+               const char *dict, const char *sep);
+
+typedef void (*connmanctl_dbus_method_return_func_t)(DBusMessageIter *iter,
+               const char *error, void *user_data);
+int __connmanctl_dbus_method_call(DBusConnection *connection, const char *path,
+               const char *interface, const char *method,
+               connmanctl_dbus_method_return_func_t cb, void * user_data,
+               int arg1, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif