client: Fix up header inclusion mess
[platform/upstream/connman.git] / client / monitor.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  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 <unistd.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 #include <glib.h>
34 #include <gdbus.h>
35
36 #include "monitor.h"
37 #include "services.h"
38 #include "technology.h"
39 #include "data_manager.h"
40
41 static const char *get_service_name(DBusMessage *message, char *dbus_path)
42 {
43         DBusMessageIter iter, array;
44
45         dbus_message_iter_init(message, &iter);
46         dbus_message_iter_recurse(&iter, &array);
47
48         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
49                 DBusMessageIter entry, dict;
50                 struct service_data service;
51                 char *path;
52
53                 dbus_message_iter_recurse(&array, &entry);
54                 dbus_message_iter_get_basic(&entry, &path);
55
56                 if (g_strcmp0(path, dbus_path) == 0) {
57                         dbus_message_iter_next(&entry);
58                         dbus_message_iter_recurse(&entry, &dict);
59                         extract_service_name(&dict, &service);
60                         return service.name;
61                 } else {
62                         dbus_message_iter_next(&array);
63                 }
64         }
65         return NULL;
66 }
67
68 static void extract_tech_signal(DBusMessage *message)
69 {
70         DBusMessageIter iter, dict;
71         char *path;
72
73         dbus_message_iter_init(message, &iter);
74
75         if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_OBJECT_PATH) {
76                 dbus_message_iter_get_basic(&iter, &path);
77                 printf(" { %s }\n", path);
78         }
79         dbus_message_iter_next(&iter);
80
81         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) {
82                 dbus_message_iter_recurse(&iter, &dict);
83                 extract_properties(&dict);
84         }
85 }
86
87 static void extract_signal_args(DBusMessage *message)
88 {
89         DBusMessageIter iter, array, dict;
90         char *string, *value;
91         uint16_t key_int;
92         dbus_bool_t bvalue;
93
94         value = NULL;
95         key_int = 0;
96
97         dbus_message_iter_init(message, &iter);
98
99         while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) {
100                 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) {
101                         dbus_message_iter_get_basic(&iter, &string);
102                         printf("\n[%s] = ",
103                         string);
104                 }
105                 dbus_message_iter_next(&iter);
106                 if (dbus_message_iter_get_arg_type(&iter) !=
107                                                         DBUS_TYPE_INVALID) {
108                         dbus_message_iter_recurse(&iter, &array);
109                         if (dbus_message_iter_get_arg_type(&array) ==
110                                                         DBUS_TYPE_STRING) {
111                                 dbus_message_iter_get_basic(&array, &value);
112                                 printf("%s\n", value);
113                                 continue;
114                         } else if (dbus_message_iter_get_arg_type(&array) ==
115                                                         DBUS_TYPE_BOOLEAN) {
116                                 dbus_message_iter_get_basic(&array, &bvalue);
117                                 printf("%s\n", bvalue == TRUE ?
118                                                         "True" : "False");
119                                 continue;
120                         } else if (dbus_message_iter_get_arg_type(&array) ==
121                                                         DBUS_TYPE_ARRAY)
122                                 dbus_message_iter_recurse(&array, &dict);
123                         if (dbus_message_iter_get_arg_type(&dict) ==
124                                                 DBUS_TYPE_DICT_ENTRY) {
125                                 iterate_dict(&dict, value, key_int);
126                                 printf("\n");
127                         } else {
128                                 iterate_array(&array);
129                                 printf("\n");
130                         }
131                         dbus_message_iter_next(&iter);
132                 }
133         }
134 }
135
136 int monitor_connman(DBusConnection *connection, char *interface,
137                                 char *signal_name)
138 {
139         char *rule = g_strdup_printf("type='signal',interface='net.connman.%s',"
140                                         "member='%s'", interface, signal_name);
141         DBusError err;
142
143         dbus_error_init(&err);
144         g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
145         if (dbus_error_is_set(&err)) {
146                 fprintf(stderr, "Bus setup error:%s\n", err.message);
147                 return -1;
148         }
149         dbus_bus_add_match(connection, rule, &err);
150
151         if (dbus_error_is_set(&err)) {
152                 fprintf(stderr, "Match Error: %s\n", err.message);
153                 return -1;
154         }
155         return 0;
156 }
157
158 DBusHandlerResult service_property_changed(DBusConnection *connection,
159                                                 DBusMessage *message,
160                                                 void *user_data)
161 {
162         DBusMessage *service_message;
163         struct service_data service;
164
165         if (dbus_message_is_signal(message, "net.connman.Service",
166                                             "PropertyChanged")) {
167                 service_message = get_message(connection, "GetServices");
168                 if (service_message == NULL)
169                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
170
171                 service.name = get_service_name(service_message,
172                                 (char *) dbus_message_get_path(message));
173                 printf("\n");
174                 g_message("Path = %s, Interface = %s\nService = %s",
175                                 dbus_message_get_path(message),
176                                 dbus_message_get_interface(message),
177                                 service.name);
178                 extract_signal_args(message);
179
180                 dbus_message_unref(service_message);
181         }
182
183         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
184 }
185
186 DBusHandlerResult tech_property_changed(DBusConnection *connection,
187                                         DBusMessage *message, void *user_data)
188 {
189         if (dbus_message_is_signal(message, "net.connman.Technology",
190                                             "PropertyChanged")) {
191                 printf("\n");
192                 g_message("Path = %s, Interface = %s",
193                                 dbus_message_get_path(message),
194                                 dbus_message_get_interface(message));
195                 extract_signal_args(message);
196         }
197         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
198 }
199
200 DBusHandlerResult tech_added_removed(DBusConnection *connection,
201                                         DBusMessage *message, void *user_data)
202 {
203         if (dbus_message_is_signal(message, "net.connman.Manager",
204                                             "TechnologyAdded")) {
205                 printf("\n");
206                 g_message("Path = %s, Interface = %s",
207                                 dbus_message_get_path(message),
208                                 dbus_message_get_interface(message));
209                 printf("New technology added:\n");
210                 extract_tech_signal(message);
211         } else if (dbus_message_is_signal(message, "net.connman.Manager",
212                                                    "TechnologyRemoved")) {
213                 printf("\n");
214                 g_message("Path = %s, Interface = %s",
215                                 dbus_message_get_path(message),
216                                 dbus_message_get_interface(message));
217                 printf("Technology was removed:\n");
218                 extract_tech_signal(message);
219         }
220
221         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
222 }
223
224 DBusHandlerResult manager_services_changed(DBusConnection *connection,
225                                 DBusMessage *message, void *user_data)
226 {
227         if (dbus_message_is_signal(message, "net.connman.Manager",
228                                                 "ServicesChanged")) {
229                 printf("\n");
230                 g_message("Path = %s, Interface = %s",
231                                 dbus_message_get_path(message),
232                                 dbus_message_get_interface(message));
233                 printf("Services Changed, displaying updated "
234                                                         "list of services:\n");
235                 list_properties(connection, "GetServices", NULL);
236         }
237
238         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
239 }
240
241 DBusHandlerResult manager_property_changed(DBusConnection *connection,
242                                 DBusMessage *message, void *user_data)
243 {
244         if (dbus_message_is_signal(message, "net.connman.Manager",
245                                             "PropertyChanged")) {
246                 printf("\n");
247                 g_message("Path = %s, Interface = %s",
248                                 dbus_message_get_path(message),
249                                 dbus_message_get_interface(message));
250                 extract_signal_args(message);
251         }
252
253         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
254 }