5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
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.
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.
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
38 #include "technology.h"
39 #include "data_manager.h"
41 static const char *get_service_name(DBusMessage *message, char *dbus_path)
43 DBusMessageIter iter, array;
45 dbus_message_iter_init(message, &iter);
46 dbus_message_iter_recurse(&iter, &array);
48 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
49 DBusMessageIter entry, dict;
50 struct service_data service;
53 dbus_message_iter_recurse(&array, &entry);
54 dbus_message_iter_get_basic(&entry, &path);
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);
62 dbus_message_iter_next(&array);
68 static void extract_tech_signal(DBusMessage *message)
70 DBusMessageIter iter, dict;
73 dbus_message_iter_init(message, &iter);
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);
79 dbus_message_iter_next(&iter);
81 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) {
82 dbus_message_iter_recurse(&iter, &dict);
83 extract_properties(&dict);
87 static void extract_signal_args(DBusMessage *message)
89 DBusMessageIter iter, array, dict;
97 dbus_message_iter_init(message, &iter);
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);
105 dbus_message_iter_next(&iter);
106 if (dbus_message_iter_get_arg_type(&iter) !=
108 dbus_message_iter_recurse(&iter, &array);
109 if (dbus_message_iter_get_arg_type(&array) ==
111 dbus_message_iter_get_basic(&array, &value);
112 printf("%s\n", value);
114 } else if (dbus_message_iter_get_arg_type(&array) ==
116 dbus_message_iter_get_basic(&array, &bvalue);
117 printf("%s\n", bvalue == TRUE ?
120 } else if (dbus_message_iter_get_arg_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);
128 iterate_array(&array);
131 dbus_message_iter_next(&iter);
136 int monitor_connman(DBusConnection *connection, char *interface,
139 char *rule = g_strdup_printf("type='signal',interface='net.connman.%s',"
140 "member='%s'", interface, signal_name);
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);
149 dbus_bus_add_match(connection, rule, &err);
151 if (dbus_error_is_set(&err)) {
152 fprintf(stderr, "Match Error: %s\n", err.message);
158 DBusHandlerResult service_property_changed(DBusConnection *connection,
159 DBusMessage *message,
162 DBusMessage *service_message;
163 struct service_data service;
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;
171 service.name = get_service_name(service_message,
172 (char *) dbus_message_get_path(message));
174 g_message("Path = %s, Interface = %s\nService = %s",
175 dbus_message_get_path(message),
176 dbus_message_get_interface(message),
178 extract_signal_args(message);
180 dbus_message_unref(service_message);
183 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
186 DBusHandlerResult tech_property_changed(DBusConnection *connection,
187 DBusMessage *message, void *user_data)
189 if (dbus_message_is_signal(message, "net.connman.Technology",
190 "PropertyChanged")) {
192 g_message("Path = %s, Interface = %s",
193 dbus_message_get_path(message),
194 dbus_message_get_interface(message));
195 extract_signal_args(message);
197 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
200 DBusHandlerResult tech_added_removed(DBusConnection *connection,
201 DBusMessage *message, void *user_data)
203 if (dbus_message_is_signal(message, "net.connman.Manager",
204 "TechnologyAdded")) {
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")) {
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);
221 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
224 DBusHandlerResult manager_services_changed(DBusConnection *connection,
225 DBusMessage *message, void *user_data)
227 if (dbus_message_is_signal(message, "net.connman.Manager",
228 "ServicesChanged")) {
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);
238 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
241 DBusHandlerResult manager_property_changed(DBusConnection *connection,
242 DBusMessage *message, void *user_data)
244 if (dbus_message_is_signal(message, "net.connman.Manager",
245 "PropertyChanged")) {
247 g_message("Path = %s, Interface = %s",
248 dbus_message_get_path(message),
249 dbus_message_get_interface(message));
250 extract_signal_args(message);
253 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;