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
35 #include "technology.h"
38 void extract_properties(DBusMessageIter *dict)
40 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
41 DBusMessageIter entry, value;
42 const char *key, *sdata;
45 dbus_message_iter_recurse(dict, &entry);
46 dbus_message_iter_get_basic(&entry, &key);
47 printf(" [%s] = ", key);
49 dbus_message_iter_next(&entry);
51 dbus_message_iter_recurse(&entry, &value);
53 if (dbus_message_iter_get_arg_type(&value) ==
55 dbus_message_iter_get_basic(&value, &bdata);
56 printf("%s\n", bdata ? "True" : "False");
57 } else if (dbus_message_iter_get_arg_type(&value) ==
59 dbus_message_iter_get_basic(&value, &sdata);
60 printf("%s\n", sdata);
62 dbus_message_iter_next(dict);
66 void match_tech_name(DBusMessage *message, char *tech_name,
67 struct tech_data *tech)
69 DBusMessageIter iter, array;
71 dbus_message_iter_init(message, &iter);
72 dbus_message_iter_recurse(&iter, &array);
73 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
74 DBusMessageIter entry;
78 dbus_message_iter_recurse(&array, &entry);
79 dbus_message_iter_get_basic(&entry, &path);
80 tech->path = g_strdup(path);
81 name = strrchr(path, '/') + 1;
82 tech->name = g_strdup(name);
83 if (g_strcmp0(tech_name, tech->name) == 0) {
84 printf(" %-20s { %s } exists\n", tech->name,
88 dbus_message_iter_next(&array);
93 void extract_tech(DBusMessage *message)
95 DBusMessageIter iter, array;
97 dbus_message_iter_init(message, &iter);
98 dbus_message_iter_recurse(&iter, &array);
99 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
100 DBusMessageIter entry, dict;
104 dbus_message_iter_recurse(&array, &entry);
105 dbus_message_iter_get_basic(&entry, &path);
107 printf("{ %s }\n", path);
109 dbus_message_iter_next(&entry);
111 dbus_message_iter_recurse(&entry, &dict);
112 extract_properties(&dict);
114 dbus_message_iter_next(&array);
118 int scan_technology(DBusConnection *connection, DBusMessage *message,
121 DBusMessage *message_send;
122 struct tech_data technology;
125 match_tech_name(message, tech, &technology);
126 if (g_strcmp0(tech, technology.name) != 0) {
127 fprintf(stderr, "%s does not exist on the system\n", tech);
128 fprintf(stderr, "Use the 'tech' command to find available "
129 "technologies on your system.\n");
133 message_send = dbus_message_new_method_call("net.connman",
135 "net.connman.Technology",
137 if (message_send == NULL)
140 dbus_error_init(&err);
141 dbus_connection_send_with_reply_and_block(connection, message_send, -1,
144 if (dbus_error_is_set(&err)) {
145 printf("Scan failed; error: '%s'\n", err.message);
149 dbus_message_unref(message_send);
150 printf("Scanned for new services on %s.\n", technology.name);
151 g_free(technology.name);
152 g_free(technology.path);
157 int set_technology(DBusConnection *connection, DBusMessage *message, char *key,
158 char *tech, dbus_bool_t value)
160 DBusMessage *message_send;
161 DBusMessageIter iter;
162 struct tech_data technology;
164 match_tech_name(message, tech, &technology);
165 if (g_strcmp0(tech, technology.name) != 0) {
166 fprintf(stderr, "%s does not exist on the system\n", tech);
167 fprintf(stderr, "Use the 'tech' command to find available "
168 "technologies on your system.\n");
172 message_send = dbus_message_new_method_call("net.connman",
174 "net.connman.Technology",
176 if (message_send == NULL)
179 dbus_message_iter_init_append(message_send, &iter);
180 dbus_property_append_basic(&iter, (const char *) key,
181 DBUS_TYPE_BOOLEAN, &value);
182 dbus_connection_send(connection, message_send, NULL);
183 dbus_connection_flush(connection);
184 dbus_message_unref(message_send);
185 g_free(technology.name);
186 g_free(technology.path);