[connman] Added Tizen Wi-Fi Mesh
[platform/upstream/connman.git] / client / peers.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2014  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "peers.h"
31
32 static void print_peer(char *path, DBusMessageIter *iter)
33 {
34         char *name = "", *state = "";
35         char *str, *property;
36         DBusMessageIter entry, val;
37         int count = 0;
38
39         while (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
40                 dbus_message_iter_recurse(iter, &entry);
41                 dbus_message_iter_get_basic(&entry, &property);
42
43                 if (strcmp(property, "Name") == 0) {
44                         dbus_message_iter_next(&entry);
45                         dbus_message_iter_recurse(&entry, &val);
46                         dbus_message_iter_get_basic(&val, &name);
47                 } else if (strcmp(property, "State") == 0) {
48                         dbus_message_iter_next(&entry);
49                         dbus_message_iter_recurse(&entry, &val);
50                         dbus_message_iter_get_basic(&val, &state);
51                 }
52
53                 dbus_message_iter_next(iter);
54                 count++;
55         }
56
57         str = strrchr(path, '/');
58         if (str)
59                 str++;
60         else
61                 str = path;
62
63         if (count > 0)
64                 fprintf(stdout, "%s %s %s", name, state, str);
65         else
66                 fprintf(stdout, "%s %s", "unchanged", str);
67 }
68
69 static void list_peer_array(DBusMessageIter *iter)
70 {
71         DBusMessageIter array, dict;
72         char *path = NULL;
73
74         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
75                 dbus_message_iter_recurse(iter, &array);
76                 if (dbus_message_iter_get_arg_type(&array)
77                                 != DBUS_TYPE_OBJECT_PATH)
78                         return;
79
80                 dbus_message_iter_get_basic(&array, &path);
81
82                 dbus_message_iter_next(&array);
83                 if (dbus_message_iter_get_arg_type(&array)
84                                                 == DBUS_TYPE_ARRAY) {
85                         dbus_message_iter_recurse(&array, &dict);
86                         print_peer(path, &dict);
87                 }
88
89                 if (dbus_message_iter_has_next(iter))
90                         fprintf(stdout, "\n");
91
92                 dbus_message_iter_next(iter);
93         }
94 }
95
96 void __connmanctl_peers_list(DBusMessageIter *iter)
97 {
98         DBusMessageIter array;
99         char *path;
100
101         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
102                 return;
103
104         dbus_message_iter_recurse(iter, &array);
105         list_peer_array(&array);
106
107         dbus_message_iter_next(iter);
108         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
109                 return;
110
111         fprintf(stdout, "\n}, {");
112
113         dbus_message_iter_recurse(iter, &array);
114         while (dbus_message_iter_get_arg_type(&array)
115                                         == DBUS_TYPE_OBJECT_PATH) {
116                 dbus_message_iter_get_basic(&array, &path);
117                 fprintf(stdout, "\n%s %s", "removed", path);
118
119                 dbus_message_iter_next(&array);
120         }
121
122 }