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"
42 static void extract_manager_properties(DBusMessage *message)
44 DBusMessageIter iter, array;
46 dbus_message_iter_init(message, &iter);
47 dbus_message_iter_recurse(&iter, &array);
48 extract_properties(&array);
51 int store_proxy_input(DBusConnection *connection, DBusMessage *message,
52 char *name, int num_args, char *argv[])
56 gchar **servers = NULL;
57 gchar **excludes = NULL;
59 for (i = 0; argv[i] != NULL && strcmp(argv[i], "excludes") != 0 &&
60 strncmp(argv[i], "--", 2) != 0; i++) {
61 servers = g_try_realloc(servers, (i + 1) * sizeof(char *));
62 if (servers == NULL) {
63 fprintf(stderr, "Could not allocate memory for list\n");
66 servers[i] = g_strdup(argv[i]);
67 /* In case the user doesn't enter "excludes" */
68 if (i + 1 == num_args) {
74 for (j = 0; j + (i + 1) != num_args && argv[j + (i + 1)] != NULL &&
75 strncmp(argv[j + i + 1], "--", 2) != 0; j++) {
76 excludes = g_try_realloc(excludes, (j + 1) * sizeof(char *));
77 if (excludes == NULL) {
78 fprintf(stderr, "Could not allocate memory for list\n");
81 excludes[j] = g_strdup(argv[j + (i + 1)]);
85 error = set_proxy_manual(connection, message, name, servers, excludes,
88 for (k = 0; k < j - 1; k++)
92 for (k = 0; k < i - 1; k++)
106 int connect_service(DBusConnection *connection, char *name)
108 DBusMessage *message, *message_connect = NULL;
110 const char *path_name;
114 message = get_message(connection, "GetServices");
118 path_name = strip_service_path(name);
120 path = g_strdup_printf("/net/connman/service/%s", path_name);
121 message_connect = dbus_message_new_method_call("net.connman", path,
122 "net.connman.Service",
124 if (message_connect == NULL) {
129 dbus_error_init(&err);
130 dbus_connection_send_with_reply_and_block(connection, message_connect,
133 if (dbus_error_is_set(&err)) {
134 printf("Error '%s': %s\n", path, err.message);
135 dbus_error_free(&err);
140 dbus_connection_send(connection, message_connect, NULL);
141 dbus_connection_flush(connection);
145 dbus_message_unref(message);
146 if (message_connect != NULL)
147 dbus_message_unref(message_connect);
153 int disconnect_service(DBusConnection *connection, char *name)
155 DBusMessage *message, *message_disconnect = NULL;
157 const char *path_name;
161 message = get_message(connection, "GetServices");
165 path_name = strip_service_path(name);
166 path = g_strdup_printf("/net/connman/service/%s", path_name);
168 message_disconnect = dbus_message_new_method_call("net.connman", path,
169 "net.connman.Service",
171 if (message_disconnect == NULL) {
176 dbus_error_init(&err);
177 dbus_connection_send_with_reply_and_block(connection,
181 if (dbus_error_is_set(&err)) {
182 printf("Error '%s': %s\n", path, err.message);
183 dbus_error_free(&err);
188 dbus_connection_send(connection, message_disconnect, NULL);
189 dbus_connection_flush(connection);
193 dbus_message_unref(message);
194 if (message_disconnect != NULL)
195 dbus_message_unref(message_disconnect);
201 int set_manager(DBusConnection *connection, char *key, dbus_bool_t value)
203 DBusMessage *message;
204 DBusMessageIter iter;
207 message = dbus_message_new_method_call("net.connman", "/",
208 "net.connman.Manager",
213 dbus_message_iter_init_append(message, &iter);
214 dbus_property_append_basic(&iter, (const char *) key,
215 DBUS_TYPE_BOOLEAN, &value);
217 dbus_error_init(&err);
218 dbus_connection_send_with_reply_and_block(connection, message,
220 if (dbus_error_is_set(&err) == TRUE) {
221 printf("Error 'net.connman.Manager' %s\n", err.message);
222 dbus_error_free(&err);
225 dbus_message_unref(message);
230 /* Call with any given function we want connman to respond to */
231 DBusMessage *get_message(DBusConnection *connection, char *function)
233 DBusMessage *message, *reply;
236 message = dbus_message_new_method_call(CONNMAN_SERVICE,
237 CONNMAN_MANAGER_PATH,
238 CONNMAN_MANAGER_INTERFACE,
243 dbus_error_init(&error);
245 reply = dbus_connection_send_with_reply_and_block(connection,
246 message, -1, &error);
247 if (dbus_error_is_set(&error) == TRUE) {
248 fprintf(stderr, "%s\n", error.message);
249 dbus_error_free(&error);
250 } else if (reply == NULL)
251 fprintf(stderr, "Failed to receive message\n");
253 dbus_message_unref(message);
258 int list_properties(DBusConnection *connection, char *function,
261 DBusMessage *message;
263 message = get_message(connection, function);
267 if (strcmp(function, "GetProperties") == 0)
268 extract_manager_properties(message);
270 else if (strcmp(function, "GetServices") == 0 && service_name != NULL)
271 extract_services(message, service_name);
273 else if (strcmp(function, "GetServices") == 0 && service_name == NULL)
274 get_services(message);
276 else if (strcmp(function, "GetTechnologies") == 0)
277 extract_tech(message);
279 dbus_message_unref(message);