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
36 #include "src/connman.h"
37 #include "client/services.h"
38 #include "client/technology.h"
39 #include "client/data_manager.h"
40 #include "gdbus/gdbus.h"
43 static void extract_manager_properties(DBusMessage *message)
45 DBusMessageIter iter, array;
47 dbus_message_iter_init(message, &iter);
48 dbus_message_iter_recurse(&iter, &array);
49 extract_properties(&array);
52 int store_proxy_input(DBusConnection *connection, DBusMessage *message,
53 char *name, int num_args, char *argv[])
57 gchar **servers = NULL;
58 gchar **excludes = NULL;
60 for (i = 0; strcmp(argv[i], "excludes") != 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; j++) {
75 excludes = g_try_realloc(excludes, (j + 1) * sizeof(char *));
76 if (excludes == NULL) {
77 fprintf(stderr, "Could not allocate memory for list\n");
80 excludes[j] = g_strdup(argv[j + (i + 1)]);
84 error = set_proxy_manual(connection, message, name, servers, excludes,
87 for (k = 0; k < j - 1; k++)
91 for (k = 0; k < i - 1; k++)
98 int connect_service(DBusConnection *connection, char *name)
100 DBusMessage *message, *message_connect = NULL;
101 struct service_data service;
103 const char *path_name;
107 message = get_message(connection, "GetServices");
111 path_name = find_service(connection, message, name, &service);
112 if (path_name == NULL) {
117 path = g_strdup_printf("/net/connman/service/%s", path_name);
118 message_connect = dbus_message_new_method_call("net.connman", path,
119 "net.connman.Service",
121 if (message_connect == NULL) {
126 dbus_error_init(&err);
127 dbus_connection_send_with_reply_and_block(connection, message_connect,
130 if (dbus_error_is_set(&err)) {
131 printf("Connection failed; error: '%s'\n", err.message);
136 dbus_connection_send(connection, message_connect, NULL);
137 dbus_connection_flush(connection);
141 dbus_message_unref(message);
142 if (message_connect != NULL)
143 dbus_message_unref(message_connect);
149 int disconnect_service(DBusConnection *connection, char *name)
151 DBusMessage *message, *message_disconnect = NULL;
152 struct service_data service;
154 const char *path_name;
158 message = get_message(connection, "GetServices");
162 path_name = find_service(connection, message, name, &service);
163 if (path_name == NULL) {
168 path = g_strdup_printf("/net/connman/service/%s", path_name);
169 printf("%s\n", path);
170 message_disconnect = dbus_message_new_method_call("net.connman", path,
171 "net.connman.Service",
173 if (message_disconnect == NULL) {
178 dbus_error_init(&err);
179 dbus_connection_send_with_reply_and_block(connection,
183 if (dbus_error_is_set(&err)) {
184 printf("Connection failed; error: '%s'\n", err.message);
189 dbus_connection_send(connection, message_disconnect, NULL);
190 dbus_connection_flush(connection);
194 dbus_message_unref(message);
195 if (message_disconnect != NULL)
196 dbus_message_unref(message_disconnect);
202 int set_manager(DBusConnection *connection, char *key, dbus_bool_t value)
204 DBusMessage *message;
205 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);
216 dbus_connection_send(connection, message, NULL);
217 dbus_connection_flush(connection);
218 dbus_message_unref(message);
223 /* Call with any given function we want connman to respond to */
224 DBusMessage *get_message(DBusConnection *connection, char *function)
226 DBusMessage *message, *reply;
229 message = dbus_message_new_method_call(CONNMAN_SERVICE,
230 CONNMAN_MANAGER_PATH,
231 CONNMAN_MANAGER_INTERFACE,
236 dbus_error_init(&error);
238 reply = dbus_connection_send_with_reply_and_block(connection,
239 message, -1, &error);
240 if (dbus_error_is_set(&error) == TRUE) {
241 fprintf(stderr, "%s\n", error.message);
242 dbus_error_free(&error);
243 } else if (reply == NULL)
244 fprintf(stderr, "Failed to receive message\n");
246 dbus_message_unref(message);
251 int list_properties(DBusConnection *connection, char *function,
254 DBusMessage *message;
256 message = get_message(connection, function);
260 if (strcmp(function, "GetProperties") == 0)
261 extract_manager_properties(message);
263 else if (strcmp(function, "GetServices") == 0 && service_name != NULL)
264 extract_services(message, service_name);
266 else if (strcmp(function, "GetServices") == 0 && service_name == NULL)
267 get_services(message);
269 else if (strcmp(function, "GetTechnologies") == 0)
270 extract_tech(message);
272 dbus_message_unref(message);