Imported Upstream version 1.37
[platform/upstream/connman.git] / client / services.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012-2013  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 <stdlib.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <errno.h>
33
34 #include <glib.h>
35
36 #include "services.h"
37
38 static void print_service(char *path, DBusMessageIter *iter)
39 {
40         char *name = "", *str = NULL;
41         int autoconn = 0, favorite = 0, count = 0;
42         char *property;
43         char state = ' ';
44         DBusMessageIter entry, val;
45
46         while (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
47
48                 dbus_message_iter_recurse(iter, &entry);
49                 dbus_message_iter_get_basic(&entry, &property);
50                 if (strcmp(property, "Name") == 0) {
51                         dbus_message_iter_next(&entry);
52                         dbus_message_iter_recurse(&entry, &val);
53                         dbus_message_iter_get_basic(&val, &name);
54
55                 } else if (strcmp(property, "State") == 0) {
56                         dbus_message_iter_next(&entry);
57                         dbus_message_iter_recurse(&entry, &val);
58                         dbus_message_iter_get_basic(&val, &str);
59
60                         if (str) {
61                                 if (strcmp(str, "online") == 0)
62                                         state = 'O';
63                                 else if (strcmp(str, "ready") == 0)
64                                         state = 'R';
65                                 else if (!strcmp(str, "association"))
66                                         state = 'a';
67                                 else if (!strcmp(str, "configuration"))
68                                         state = 'c';
69                                 else if (!strcmp(str, "disconnect"))
70                                         state = 'd';
71                         }
72
73                 } else if (strcmp(property, "AutoConnect") == 0) {
74                         dbus_message_iter_next(&entry);
75                         dbus_message_iter_recurse(&entry, &val);
76                         dbus_message_iter_get_basic(&val, &autoconn);
77
78                 } else if (strcmp(property, "Favorite") == 0) {
79                         dbus_message_iter_next(&entry);
80                         dbus_message_iter_recurse(&entry, &val);
81                         dbus_message_iter_get_basic(&val, &favorite);
82                 }
83
84                 count++;
85                 dbus_message_iter_next(iter);
86         }
87
88         str = strrchr(path, '/');
89         if (str)
90                 str++;
91         else
92                 str = path;
93
94         if (count > 0)
95                 fprintf(stdout, "%c%c%c %-20s %s", favorite != 0 ? '*' : ' ',
96                                 autoconn != 0 ? 'A' : ' ', state, name, str);
97         else
98                 fprintf(stdout, "%-24s %s", "unchanged", str);
99
100 }
101
102 static void list_service_array(DBusMessageIter *iter)
103 {
104         DBusMessageIter array, dict;
105         char *path = NULL;
106
107         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
108
109                 dbus_message_iter_recurse(iter, &array);
110                 if (dbus_message_iter_get_arg_type(&array)
111                                 != DBUS_TYPE_OBJECT_PATH)
112                         return;
113
114                 dbus_message_iter_get_basic(&array, &path);
115
116                 dbus_message_iter_next(&array);
117                 if (dbus_message_iter_get_arg_type(&array)
118                                 == DBUS_TYPE_ARRAY) {
119                         dbus_message_iter_recurse(&array, &dict);
120                         print_service(path, &dict);
121                 }
122
123                 if (dbus_message_iter_has_next(iter))
124                         fprintf(stdout, "\n");
125
126                 dbus_message_iter_next(iter);
127         }
128 }
129
130 void __connmanctl_services_list(DBusMessageIter *iter)
131 {
132         DBusMessageIter array;
133         char *path;
134
135         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
136                 return;
137
138         dbus_message_iter_recurse(iter, &array);
139         list_service_array(&array);
140
141         dbus_message_iter_next(iter);
142         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
143                 return;
144
145         fprintf(stdout, "\n}, {");
146
147         dbus_message_iter_recurse(iter, &array);
148         while (dbus_message_iter_get_arg_type(&array)
149                         == DBUS_TYPE_OBJECT_PATH) {
150                 dbus_message_iter_get_basic(&array, &path);
151                 fprintf(stdout, "\n%-24s %s", "removed", path);
152
153                 dbus_message_iter_next(&array);
154         }
155
156 }