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 version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "client/technology.h"
29 #include "src/connman.h"
31 void extract_properties(DBusMessageIter *dict)
33 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
34 DBusMessageIter entry, value;
35 const char *key, *sdata;
38 dbus_message_iter_recurse(dict, &entry);
39 dbus_message_iter_get_basic(&entry, &key);
40 printf(" [%s] = ", key);
42 dbus_message_iter_next(&entry);
44 dbus_message_iter_recurse(&entry, &value);
46 if (dbus_message_iter_get_arg_type(&value) ==
48 dbus_message_iter_get_basic(&value, &bdata);
49 printf("%s\n", bdata ? "True" : "False");
50 } else if (dbus_message_iter_get_arg_type(&value) ==
52 dbus_message_iter_get_basic(&value, &sdata);
53 printf("%s\n", sdata);
55 dbus_message_iter_next(dict);
59 void match_tech_name(DBusMessage *message, char *tech_name,
60 struct tech_data *tech)
62 DBusMessageIter iter, array;
64 dbus_message_iter_init(message, &iter);
65 dbus_message_iter_recurse(&iter, &array);
66 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
67 DBusMessageIter entry;
71 dbus_message_iter_recurse(&array, &entry);
72 dbus_message_iter_get_basic(&entry, &path);
73 tech->path = g_strdup(path);
74 name = strrchr(path, '/') + 1;
75 tech->name = g_strdup(name);
76 if (g_strcmp0(tech_name, tech->name) == 0) {
77 printf(" %-20s { %s } exists\n", tech->name,
81 dbus_message_iter_next(&array);
86 void extract_tech(DBusMessage *message)
88 DBusMessageIter iter, array;
90 dbus_message_iter_init(message, &iter);
91 dbus_message_iter_recurse(&iter, &array);
92 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
93 DBusMessageIter entry, dict;
97 dbus_message_iter_recurse(&array, &entry);
98 dbus_message_iter_get_basic(&entry, &path);
100 printf("{ %s }\n", path);
102 dbus_message_iter_next(&entry);
104 dbus_message_iter_recurse(&entry, &dict);
105 extract_properties(&dict);
107 dbus_message_iter_next(&array);
111 int scan_technology(DBusConnection *connection, DBusMessage *message,
114 DBusMessage *message_send;
115 struct tech_data technology;
118 match_tech_name(message, tech, &technology);
119 if (g_strcmp0(tech, technology.name) != 0) {
120 fprintf(stderr, "%s does not exist on the system\n", tech);
121 fprintf(stderr, "Use the 'tech' command to find available "
122 "technologies on your system.\n");
126 message_send = dbus_message_new_method_call("net.connman",
128 "net.connman.Technology",
130 if (message_send == NULL)
133 dbus_error_init(&err);
134 dbus_connection_send_with_reply_and_block(connection, message_send, -1,
137 if (dbus_error_is_set(&err)) {
138 printf("Scan failed; error: '%s'\n", err.message);
142 dbus_message_unref(message_send);
143 printf("Scanned for new services on %s.\n", technology.name);
144 g_free(technology.name);
145 g_free(technology.path);
150 int set_technology(DBusConnection *connection, DBusMessage *message, char *key,
151 char *tech, dbus_bool_t value)
153 DBusMessage *message_send;
154 DBusMessageIter iter;
155 struct tech_data technology;
157 match_tech_name(message, tech, &technology);
158 if (g_strcmp0(tech, technology.name) != 0) {
159 fprintf(stderr, "%s does not exist on the system\n", tech);
160 fprintf(stderr, "Use the 'tech' command to find available "
161 "technologies on your system.\n");
165 message_send = dbus_message_new_method_call("net.connman",
167 "net.connman.Technology",
169 if (message_send == NULL)
172 dbus_message_iter_init_append(message_send, &iter);
173 connman_dbus_property_append_basic(&iter, (const char *) key,
174 DBUS_TYPE_BOOLEAN, &value);
175 dbus_connection_send(connection, message_send, NULL);
176 dbus_connection_flush(connection);
177 dbus_message_unref(message_send);
178 g_free(technology.name);
179 g_free(technology.path);