045947c82e54f214ce139d42b1f774d3dbf11f50
[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 != NULL) {
61                                 if (strcmp(str, "online") == 0)
62                                         state = 'O';
63                                 else if (strcmp(str, "ready") == 0)
64                                         state = 'R';
65                         }
66
67                 } else if (strcmp(property, "AutoConnect") == 0) {
68                         dbus_message_iter_next(&entry);
69                         dbus_message_iter_recurse(&entry, &val);
70                         dbus_message_iter_get_basic(&val, &autoconn);
71
72                 } else if (strcmp(property, "Favorite") == 0) {
73                         dbus_message_iter_next(&entry);
74                         dbus_message_iter_recurse(&entry, &val);
75                         dbus_message_iter_get_basic(&val, &favorite);
76                 }
77
78                 count++;
79                 dbus_message_iter_next(iter);
80         }
81
82         str = strrchr(path, '/');
83         if (str != NULL)
84                 str++;
85         else
86                 str = path;
87
88         if (count > 0) {
89                 if (*name == '\0')
90                         name = "<hidden>";
91
92                 fprintf(stdout, "%c%c%c %-20s %s", favorite != 0 ? '*' : ' ',
93                                 autoconn != 0 ? 'A' : ' ', state, name, str);
94         } else
95                 fprintf(stdout, "%-24s %s", "unchanged", str);
96
97 }
98
99 static void list_service_array(DBusMessageIter *iter)
100 {
101         DBusMessageIter array, dict;
102         char *path = NULL;
103
104         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRUCT) {
105
106                 dbus_message_iter_recurse(iter, &array);
107                 if (dbus_message_iter_get_arg_type(&array)
108                                 != DBUS_TYPE_OBJECT_PATH)
109                         return;
110
111                 dbus_message_iter_get_basic(&array, &path);
112
113                 dbus_message_iter_next(&array);
114                 if (dbus_message_iter_get_arg_type(&array)
115                                 == DBUS_TYPE_ARRAY) {
116                         dbus_message_iter_recurse(&array, &dict);
117                         print_service(path, &dict);
118                 }
119
120                 if (dbus_message_iter_has_next(iter) == TRUE)
121                         fprintf(stdout, "\n");
122
123                 dbus_message_iter_next(iter);
124         }
125 }
126
127 void __connmanctl_services_list(DBusMessageIter *iter)
128 {
129         DBusMessageIter array;
130         char *path;
131
132         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
133                 return;
134
135         dbus_message_iter_recurse(iter, &array);
136         list_service_array(&array);
137
138         dbus_message_iter_next(iter);
139         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
140                 return;
141
142         fprintf(stdout, "\n}, {");
143
144         dbus_message_iter_recurse(iter, &array);
145         while (dbus_message_iter_get_arg_type(&array)
146                         == DBUS_TYPE_OBJECT_PATH) {
147                 dbus_message_iter_get_basic(&array, &path);
148                 fprintf(stdout, "\n%-24s %s", "removed", path);
149
150                 dbus_message_iter_next(&array);
151         }
152
153 }