Handle PeersChanged() signal and GetPeers() method from Manager API
relevantly.
---
 Makefile.am       |   1 +
 client/commands.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++-
 client/peers.c    | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 client/peers.h    |  38 +++++++++++++++++
 4 files changed, 274 insertions(+), 2 deletions(-)
 create mode 100644 client/peers.c
 create mode 100644 client/peers.h

diff --git a/Makefile.am b/Makefile.am
index c0f6c97..382bf2a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -243,6 +243,7 @@ client_connmanctl_SOURCES = client/dbus_helpers.h 
client/dbus_helpers.c \
                        client/commands.h client/commands.c \
                        client/input.h client/input.c \
                        client/agent.h client/agent.c \
+                       client/peers.h client/peers.c \
                        client/vpnconnections.h client/vpnconnections.c \
                        client/main.c
 
diff --git a/client/commands.c b/client/commands.c
index 1797323..7881c98 100644
--- a/client/commands.c
+++ b/client/commands.c
@@ -38,12 +38,14 @@
 #include "dbus_helpers.h"
 #include "input.h"
 #include "services.h"
+#include "peers.h"
 #include "commands.h"
 #include "agent.h"
 #include "vpnconnections.h"
 
 static DBusConnection *connection;
 static GHashTable *service_hash;
+static GHashTable *peer_hash;
 static GHashTable *technology_hash;
 static char *session_notify_path;
 static char *session_path;
@@ -275,6 +277,18 @@ static int services_list(DBusMessageIter *iter, const char 
*error,
        return 0;
 }
 
+static int peers_list(DBusMessageIter *iter,
+                                       const char *error, void *user_data)
+{
+       if (!error) {
+               __connmanctl_peers_list(iter);
+               fprintf(stdout, "\n");
+       } else
+               fprintf(stderr, "Error: %s\n", error);
+
+       return 0;
+}
+
 static int services_properties(DBusMessageIter *iter, const char *error,
                void *user_data)
 {
@@ -346,6 +360,17 @@ static int cmd_services(char *args[], int num, struct 
connman_option *options)
                        services_properties, path, NULL, NULL);
 }
 
+static int cmd_peers(char *args[], int num, struct connman_option *options)
+{
+       if (num > 1)
+               return -E2BIG;
+
+       return __connmanctl_dbus_method_call(connection,
+                               CONNMAN_SERVICE, CONNMAN_PATH,
+                               "net.connman.Manager", "GetPeers",
+                               peers_list, NULL, NULL, NULL);
+}
+
 static int technology_print(DBusMessageIter *iter, const char *error,
                void *user_data)
 {
@@ -1054,10 +1079,18 @@ static DBusHandlerResult monitor_changed(DBusConnection 
*connection,
                __connmanctl_redraw_rl();
 
                return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-       }
+       } else if (dbus_message_is_signal(message, "net.connman.Manager",
+                                                       "PeersChanged")) {
+               fprintf(stdout, "%-12s %-20s = {\n", interface,
+                                                       "PeersChanged");
+               dbus_message_iter_init(message, &iter);
+               __connmanctl_peers_list(&iter);
+               fprintf(stdout, "\n}\n");
 
+               __connmanctl_redraw_rl();
 
-       if (dbus_message_is_signal(message, "net.connman.vpn.Manager",
+               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       } else if (dbus_message_is_signal(message, "net.connman.vpn.Manager",
                                        "ConnectionAdded") ||
                        dbus_message_is_signal(message,
                                        "net.connman.vpn.Manager",
@@ -2031,6 +2064,8 @@ static const struct {
          lookup_tether },
        { "services",     "[<service>]",  service_options, cmd_services,
          "Display services", lookup_service_arg },
+       { "peers",        NULL,           NULL,            cmd_peers,
+         "Display peers", NULL },
        { "scan",         "<technology>", NULL,            cmd_scan,
          "Scans for new services for given technology",
          lookup_technology_arg },
@@ -2238,6 +2273,67 @@ static int populate_service_hash(DBusMessageIter *iter, 
const char *error,
        return 0;
 }
 
+static void add_peer_id(const char *path)
+{
+       g_hash_table_replace(peer_hash, g_strdup(path), GINT_TO_POINTER(TRUE));
+}
+
+static void remove_peer_id(const char *path)
+{
+       g_hash_table_remove(peer_hash, path);
+}
+
+static void peers_added(DBusMessageIter *iter)
+{
+       DBusMessageIter array;
+       char *path = NULL;
+
+       while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
+
+               dbus_message_iter_recurse(iter, &array);
+               if (dbus_message_iter_get_arg_type(&array) !=
+                                               DBUS_TYPE_OBJECT_PATH)
+                       return;
+
+               dbus_message_iter_get_basic(&array, &path);
+               add_peer_id(get_path(path));
+
+               dbus_message_iter_next(iter);
+       }
+}
+
+static void update_peers(DBusMessageIter *iter)
+{
+       DBusMessageIter array;
+       char *path;
+
+       if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
+               return;
+
+       dbus_message_iter_recurse(iter, &array);
+       peers_added(&array);
+
+       dbus_message_iter_next(iter);
+       if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
+               return;
+
+       dbus_message_iter_recurse(iter, &array);
+       while (dbus_message_iter_get_arg_type(&array) ==
+                                               DBUS_TYPE_OBJECT_PATH) {
+               dbus_message_iter_get_basic(&array, &path);
+               remove_peer_id(get_path(path));
+
+               dbus_message_iter_next(&array);
+       }
+}
+
+static int populate_peer_hash(DBusMessageIter *iter,
+                                       const char *error, void *user_data)
+{
+       update_peers(iter);
+       return 0;
+}
+
 static void add_technology_id(const char *path)
 {
        g_hash_table_replace(technology_hash, g_strdup(path),
@@ -2320,6 +2416,13 @@ static DBusHandlerResult monitor_completions_changed(
        }
 
        if (dbus_message_is_signal(message, "net.connman.Manager",
+                                               "PeersChanged")) {
+               dbus_message_iter_init(message, &iter);
+               update_peers(&iter);
+               return handled;
+       }
+
+       if (dbus_message_is_signal(message, "net.connman.Manager",
                                        "TechnologyAdded")) {
                dbus_message_iter_init(message, &iter);
                add_technology(&iter);
@@ -2370,6 +2473,9 @@ void __connmanctl_monitor_completions(DBusConnection 
*dbus_conn)
        service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                                g_free, NULL);
 
+       peer_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                                               g_free, NULL);
+
        technology_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
                                                                g_free, NULL);
 
@@ -2380,6 +2486,11 @@ void __connmanctl_monitor_completions(DBusConnection 
*dbus_conn)
 
        __connmanctl_dbus_method_call(connection,
                                CONNMAN_SERVICE, CONNMAN_PATH,
+                               "net.connman.Manager", "GetPeers",
+                               populate_peer_hash, NULL, NULL, NULL);
+
+       __connmanctl_dbus_method_call(connection,
+                               CONNMAN_SERVICE, CONNMAN_PATH,
                                "net.connman.Manager", "GetTechnologies",
                                populate_technology_hash, NULL, NULL, NULL);
 
diff --git a/client/peers.c b/client/peers.c
new file mode 100644
index 0000000..a572e3c
--- /dev/null
+++ b/client/peers.c
@@ -0,0 +1,122 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2014  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
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+
+#include "services.h"
+
+static void print_peer(char *path, DBusMessageIter *iter)
+{
+       char *name = "", *state = "";
+       char *str, *property;
+       DBusMessageIter entry, val;
+       int count = 0;
+
+       while (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
+               dbus_message_iter_recurse(iter, &entry);
+               dbus_message_iter_get_basic(&entry, &property);
+
+               if (strcmp(property, "Name") == 0) {
+                       dbus_message_iter_next(&entry);
+                       dbus_message_iter_recurse(&entry, &val);
+                       dbus_message_iter_get_basic(&val, &name);
+               } else if (strcmp(property, "State") == 0) {
+                       dbus_message_iter_next(&entry);
+                       dbus_message_iter_recurse(&entry, &val);
+                       dbus_message_iter_get_basic(&val, &state);
+               }
+
+               dbus_message_iter_next(iter);
+               count++;
+       }
+
+       str = strrchr(path, '/');
+       if (str)
+               str++;
+       else
+               str = path;
+
+       if (count > 0)
+               fprintf(stdout, "%s %s %s", name, state, str);
+       else
+               fprintf(stdout, "%s %s", "unchanged", str);
+}
+
+static void list_peer_array(DBusMessageIter *iter)
+{
+       DBusMessageIter array, dict;
+       char *path = NULL;
+
+       while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
+               dbus_message_iter_recurse(iter, &array);
+               if (dbus_message_iter_get_arg_type(&array)
+                               != DBUS_TYPE_OBJECT_PATH)
+                       return;
+
+               dbus_message_iter_get_basic(&array, &path);
+
+               dbus_message_iter_next(&array);
+               if (dbus_message_iter_get_arg_type(&array)
+                                               == DBUS_TYPE_ARRAY) {
+                       dbus_message_iter_recurse(&array, &dict);
+                       print_peer(path, &dict);
+               }
+
+               if (dbus_message_iter_has_next(iter))
+                       fprintf(stdout, "\n");
+
+               dbus_message_iter_next(iter);
+       }
+}
+
+void __connmanctl_peers_list(DBusMessageIter *iter)
+{
+       DBusMessageIter array;
+       char *path;
+
+       if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
+               return;
+
+       dbus_message_iter_recurse(iter, &array);
+       list_peer_array(&array);
+
+       dbus_message_iter_next(iter);
+       if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
+               return;
+
+       fprintf(stdout, "\n}, {");
+
+       dbus_message_iter_recurse(iter, &array);
+       while (dbus_message_iter_get_arg_type(&array)
+                                       == DBUS_TYPE_OBJECT_PATH) {
+               dbus_message_iter_get_basic(&array, &path);
+               fprintf(stdout, "\n%s %s", "removed", path);
+
+               dbus_message_iter_next(&array);
+       }
+
+}
diff --git a/client/peers.h b/client/peers.h
new file mode 100644
index 0000000..fdf7ce7
--- /dev/null
+++ b/client/peers.h
@@ -0,0 +1,38 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2014  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_PEERS_H
+#define __CONNMANCTL_PEERS_H
+
+#include <dbus/dbus.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void __connmanctl_peers_list(DBusMessageIter *iter);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __CONNMANCTL_PEERS_H */
-- 
1.8.3.2

_______________________________________________
connman mailing list
[email protected]
https://lists.connman.net/mailman/listinfo/connman

Reply via email to