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 int __connman_agent_request_passphrase(struct connman_service *service,
89 passphrase_cb_t callback, void *user_data)
94 DBG("service %p", service);
96 if (agent_path == NULL)
99 message = dbus_message_new_method_call(agent_sender, agent_path,
100 CONNMAN_AGENT_INTERFACE, "RequestPassphrase");
104 dbus_message_set_no_reply(message, TRUE);
106 path = __connman_service_get_path(service);
108 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &path,
111 g_dbus_send_message(connection, message);
116 struct request_input_reply {
117 struct connman_service *service;
118 passphrase_cb_t callback;
122 static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data)
124 struct request_input_reply *passphrase_reply = user_data;
125 char *passphrase = NULL;
127 DBusMessageIter iter, dict;
128 DBusMessage *reply = dbus_pending_call_steal_reply(call);
130 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
133 dbus_message_iter_init(reply, &iter);
134 dbus_message_iter_recurse(&iter, &dict);
135 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
136 DBusMessageIter entry, value;
138 dbus_message_iter_recurse(&dict, &entry);
139 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
142 dbus_message_iter_get_basic(&entry, &key);
143 if (g_str_equal(key, "Passphrase")) {
144 dbus_message_iter_next(&entry);
145 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
147 dbus_message_iter_recurse(&entry, &value);
148 dbus_message_iter_get_basic(&value, &passphrase);
151 dbus_message_iter_next(&dict);
155 passphrase_reply->callback(passphrase_reply->service,
156 passphrase, passphrase_reply->user_data);
157 connman_service_unref(passphrase_reply->service);
158 dbus_message_unref(reply);
159 g_free(passphrase_reply);
162 static void request_input_append_passphrase(DBusMessageIter *iter, void *user_data)
164 struct connman_service *service = user_data;
167 switch (__connman_service_get_security(service)) {
168 case CONNMAN_SERVICE_SECURITY_WEP:
171 case CONNMAN_SERVICE_SECURITY_PSK:
178 connman_dbus_dict_append_basic(iter, "Type",
179 DBUS_TYPE_STRING, &value);
181 connman_dbus_dict_append_basic(iter, "Requirement",
182 DBUS_TYPE_STRING, &value);
185 int __connman_agent_request_input(struct connman_service *service,
186 passphrase_cb_t callback, void *user_data)
188 DBusMessage *message;
190 DBusMessageIter iter;
191 DBusMessageIter dict;
192 DBusPendingCall *call;
193 struct request_input_reply *passphrase_reply;
195 if (service == NULL || agent_path == NULL || callback == NULL)
198 message = dbus_message_new_method_call(agent_sender, agent_path,
199 CONNMAN_AGENT_INTERFACE,
204 dbus_message_iter_init_append(message, &iter);
206 path = __connman_service_get_path(service);
207 dbus_message_iter_append_basic(&iter,
208 DBUS_TYPE_OBJECT_PATH, &path);
210 connman_dbus_dict_open(&iter, &dict);
211 connman_dbus_dict_append_dict(&dict, "Passphrase",
212 request_input_append_passphrase, service);
213 connman_dbus_dict_close(&iter, &dict);
215 passphrase_reply = g_try_new0(struct request_input_reply, 1);
216 if (passphrase_reply == NULL) {
217 dbus_message_unref(message);
221 if (dbus_connection_send_with_reply(connection, message,
222 &call, -1) == FALSE) {
223 dbus_message_unref(message);
224 g_free(passphrase_reply);
229 dbus_message_unref(message);
230 g_free(passphrase_reply);
234 passphrase_reply->service = connman_service_ref(service);
235 passphrase_reply->callback = callback;
236 passphrase_reply->user_data = user_data;
238 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
239 passphrase_reply, NULL);
241 dbus_message_unref(message);
246 int __connman_agent_init(void)
250 connection = connman_dbus_get_connection();
251 if (connection == NULL)
257 void __connman_agent_cleanup(void)
259 DBusMessage *message;
263 if (connection == NULL)
267 g_dbus_remove_watch(connection, agent_watch);
269 if (agent_path == NULL)
272 message = dbus_message_new_method_call(agent_sender, agent_path,
273 CONNMAN_AGENT_INTERFACE, "Release");
277 dbus_message_set_no_reply(message, TRUE);
279 g_dbus_send_message(connection, message);
283 dbus_connection_unref(connection);