5 * Copyright (C) 2007-2010 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
34 static DBusConnection *connection = NULL;
35 static guint agent_watch = 0;
36 static gchar *agent_path = NULL;
37 static gchar *agent_sender = NULL;
39 static void agent_free(void)
50 static void agent_disconnect(DBusConnection *connection, void *data)
57 int __connman_agent_register(const char *sender, const char *path)
59 DBG("sender %s path %s", sender, path);
61 if (agent_path != NULL)
64 agent_sender = g_strdup(sender);
65 agent_path = g_strdup(path);
67 agent_watch = g_dbus_add_disconnect_watch(connection, sender,
68 agent_disconnect, NULL, NULL);
73 int __connman_agent_unregister(const char *sender, const char *path)
75 DBG("sender %s path %s", sender, path);
77 if (agent_path == NULL)
81 g_dbus_remove_watch(connection, agent_watch);
88 struct request_input_reply {
89 struct connman_service *service;
90 passphrase_cb_t callback;
94 static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data)
96 struct request_input_reply *passphrase_reply = user_data;
97 char *passphrase = NULL;
99 DBusMessageIter iter, dict;
100 DBusMessage *reply = dbus_pending_call_steal_reply(call);
102 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
105 dbus_message_iter_init(reply, &iter);
106 dbus_message_iter_recurse(&iter, &dict);
107 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
108 DBusMessageIter entry, value;
110 dbus_message_iter_recurse(&dict, &entry);
111 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
114 dbus_message_iter_get_basic(&entry, &key);
115 if (g_str_equal(key, "Passphrase")) {
116 dbus_message_iter_next(&entry);
117 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
119 dbus_message_iter_recurse(&entry, &value);
120 dbus_message_iter_get_basic(&value, &passphrase);
123 dbus_message_iter_next(&dict);
127 passphrase_reply->callback(passphrase_reply->service,
128 passphrase, passphrase_reply->user_data);
129 connman_service_unref(passphrase_reply->service);
130 dbus_message_unref(reply);
131 g_free(passphrase_reply);
134 static void request_input_append_passphrase(DBusMessageIter *iter, void *user_data)
136 struct connman_service *service = user_data;
139 switch (__connman_service_get_security(service)) {
140 case CONNMAN_SERVICE_SECURITY_WEP:
143 case CONNMAN_SERVICE_SECURITY_PSK:
150 connman_dbus_dict_append_basic(iter, "Type",
151 DBUS_TYPE_STRING, &value);
153 connman_dbus_dict_append_basic(iter, "Requirement",
154 DBUS_TYPE_STRING, &value);
157 int __connman_agent_request_input(struct connman_service *service,
158 passphrase_cb_t callback, void *user_data)
160 DBusMessage *message;
162 DBusMessageIter iter;
163 DBusMessageIter dict;
164 DBusPendingCall *call;
165 struct request_input_reply *passphrase_reply;
167 if (service == NULL || agent_path == NULL || callback == NULL)
170 message = dbus_message_new_method_call(agent_sender, agent_path,
171 CONNMAN_AGENT_INTERFACE,
176 dbus_message_iter_init_append(message, &iter);
178 path = __connman_service_get_path(service);
179 dbus_message_iter_append_basic(&iter,
180 DBUS_TYPE_OBJECT_PATH, &path);
182 connman_dbus_dict_open(&iter, &dict);
183 connman_dbus_dict_append_dict(&dict, "Passphrase",
184 request_input_append_passphrase, service);
185 connman_dbus_dict_close(&iter, &dict);
187 passphrase_reply = g_try_new0(struct request_input_reply, 1);
188 if (passphrase_reply == NULL) {
189 dbus_message_unref(message);
193 if (dbus_connection_send_with_reply(connection, message,
194 &call, -1) == FALSE) {
195 dbus_message_unref(message);
196 g_free(passphrase_reply);
201 dbus_message_unref(message);
202 g_free(passphrase_reply);
206 passphrase_reply->service = connman_service_ref(service);
207 passphrase_reply->callback = callback;
208 passphrase_reply->user_data = user_data;
210 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
211 passphrase_reply, NULL);
213 dbus_message_unref(message);
218 int __connman_agent_init(void)
222 connection = connman_dbus_get_connection();
223 if (connection == NULL)
229 void __connman_agent_cleanup(void)
231 DBusMessage *message;
235 if (connection == NULL)
239 g_dbus_remove_watch(connection, agent_watch);
241 if (agent_path == NULL)
244 message = dbus_message_new_method_call(agent_sender, agent_path,
245 CONNMAN_AGENT_INTERFACE, "Release");
249 dbus_message_set_no_reply(message, TRUE);
251 g_dbus_send_message(connection, message);
255 dbus_connection_unref(connection);