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 authentication_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 connman_bool_t wps = FALSE;
98 char *identity = NULL;
99 char *passphrase = NULL;
102 DBusMessageIter iter, dict;
103 DBusMessage *reply = dbus_pending_call_steal_reply(call);
105 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
108 dbus_message_iter_init(reply, &iter);
109 dbus_message_iter_recurse(&iter, &dict);
110 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
111 DBusMessageIter entry, value;
113 dbus_message_iter_recurse(&dict, &entry);
114 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
117 dbus_message_iter_get_basic(&entry, &key);
119 if (g_str_equal(key, "Identity")) {
120 dbus_message_iter_next(&entry);
121 if (dbus_message_iter_get_arg_type(&entry)
122 != DBUS_TYPE_VARIANT)
124 dbus_message_iter_recurse(&entry, &value);
125 dbus_message_iter_get_basic(&value, &identity);
127 } else if (g_str_equal(key, "Passphrase")) {
128 dbus_message_iter_next(&entry);
129 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
131 dbus_message_iter_recurse(&entry, &value);
132 dbus_message_iter_get_basic(&value, &passphrase);
134 } else if (g_str_equal(key, "WPS")) {
137 dbus_message_iter_next(&entry);
138 if (dbus_message_iter_get_arg_type(&entry)
139 != DBUS_TYPE_VARIANT)
141 dbus_message_iter_recurse(&entry, &value);
142 dbus_message_iter_get_basic(&value, &wpspin);
145 dbus_message_iter_next(&dict);
149 struct connman_network *network;
151 network = __connman_service_get_network(
152 passphrase_reply->service);
156 connman_network_set_bool(network, "WiFi.UseWPS", wps);
158 if (wpspin != NULL && strlen(wpspin) > 0)
159 connman_network_set_string(network,
160 "WiFi.PinWPS", wpspin);
162 connman_network_set_string(network,
163 "WiFi.PinWPS", NULL);
167 passphrase_reply->callback(passphrase_reply->service, identity,
168 passphrase, passphrase_reply->user_data);
169 connman_service_unref(passphrase_reply->service);
170 dbus_message_unref(reply);
171 g_free(passphrase_reply);
174 static void request_input_append_alternates(DBusMessageIter *iter,
177 const char *str = user_data;
178 char **alternates, **alternative;
183 alternates = g_strsplit(str, ",", 0);
184 if (alternates == NULL)
187 for (alternative = alternates; *alternative != NULL; alternative++)
188 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
191 g_strfreev(alternates);
194 static void request_input_append_identity(DBusMessageIter *iter,
197 char *str = "string";
199 connman_dbus_dict_append_basic(iter, "Type",
200 DBUS_TYPE_STRING, &str);
202 connman_dbus_dict_append_basic(iter, "Requirement",
203 DBUS_TYPE_STRING, &str);
206 static void request_input_append_passphrase(DBusMessageIter *iter,
209 struct connman_service *service = user_data;
213 switch (__connman_service_get_security(service)) {
214 case CONNMAN_SERVICE_SECURITY_WEP:
217 case CONNMAN_SERVICE_SECURITY_PSK:
220 case CONNMAN_SERVICE_SECURITY_8021X:
221 phase2 = __connman_service_get_phase2(service);
223 if (phase2 != NULL && (
224 g_str_has_suffix(phase2, "GTC") == TRUE ||
225 g_str_has_suffix(phase2, "OTP") == TRUE))
228 value = "passphrase";
235 connman_dbus_dict_append_basic(iter, "Type",
236 DBUS_TYPE_STRING, &value);
238 connman_dbus_dict_append_basic(iter, "Requirement",
239 DBUS_TYPE_STRING, &value);
241 if (__connman_service_wps_enabled(service) == TRUE) {
242 connman_dbus_dict_append_array(iter, "Alternates",
244 request_input_append_alternates,
249 static void request_input_append_wps(DBusMessageIter *iter, void *user_data)
251 const char *str = "wpspin";
253 connman_dbus_dict_append_basic(iter, "Type",
254 DBUS_TYPE_STRING, &str);
256 connman_dbus_dict_append_basic(iter, "Requirement",
257 DBUS_TYPE_STRING, &str);
260 int __connman_agent_request_input(struct connman_service *service,
261 authentication_cb_t callback, void *user_data)
263 DBusMessage *message;
265 DBusMessageIter iter;
266 DBusMessageIter dict;
267 DBusPendingCall *call;
268 struct request_input_reply *passphrase_reply;
270 if (service == NULL || agent_path == NULL || callback == NULL)
273 message = dbus_message_new_method_call(agent_sender, agent_path,
274 CONNMAN_AGENT_INTERFACE,
279 dbus_message_iter_init_append(message, &iter);
281 path = __connman_service_get_path(service);
282 dbus_message_iter_append_basic(&iter,
283 DBUS_TYPE_OBJECT_PATH, &path);
285 connman_dbus_dict_open(&iter, &dict);
287 if (__connman_service_get_security(service) ==
288 CONNMAN_SERVICE_SECURITY_8021X) {
289 connman_dbus_dict_append_dict(&dict, "Identity",
290 request_input_append_identity, service);
293 connman_dbus_dict_append_dict(&dict, "Passphrase",
294 request_input_append_passphrase, service);
296 if (__connman_service_wps_enabled(service) == TRUE) {
297 connman_dbus_dict_append_dict(&dict, "WPS",
298 request_input_append_wps, NULL);
301 connman_dbus_dict_close(&iter, &dict);
303 passphrase_reply = g_try_new0(struct request_input_reply, 1);
304 if (passphrase_reply == NULL) {
305 dbus_message_unref(message);
309 if (dbus_connection_send_with_reply(connection, message,
310 &call, -1) == FALSE) {
311 dbus_message_unref(message);
312 g_free(passphrase_reply);
317 dbus_message_unref(message);
318 g_free(passphrase_reply);
322 passphrase_reply->service = connman_service_ref(service);
323 passphrase_reply->callback = callback;
324 passphrase_reply->user_data = user_data;
326 dbus_pending_call_set_notify(call, request_input_passphrase_reply,
327 passphrase_reply, NULL);
329 dbus_message_unref(message);
334 struct report_error_data {
335 struct connman_service *service;
336 report_error_cb_t callback;
340 static void report_error_reply(DBusPendingCall *call, void *user_data)
342 struct report_error_data *report_error = user_data;
343 DBusMessage *reply = dbus_pending_call_steal_reply(call);
344 gboolean retry = FALSE;
345 const char *dbus_err;
347 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
348 dbus_err = dbus_message_get_error_name(reply);
349 if (dbus_err != NULL &&
351 CONNMAN_AGENT_INTERFACE ".Error.Retry") == 0)
355 report_error->callback(report_error->service, retry,
356 report_error->user_data);
357 connman_service_unref(report_error->service);
358 g_free(report_error);
359 dbus_message_unref(reply);
362 int __connman_agent_report_error(struct connman_service *service,
364 report_error_cb_t callback, void *user_data)
366 DBusMessage *message;
367 DBusMessageIter iter;
369 struct report_error_data *report_error;
370 DBusPendingCall *call;
372 if (service == NULL || agent_path == NULL || error == NULL ||
376 message = dbus_message_new_method_call(agent_sender, agent_path,
377 CONNMAN_AGENT_INTERFACE,
382 dbus_message_iter_init_append(message, &iter);
384 path = __connman_service_get_path(service);
385 dbus_message_iter_append_basic(&iter,
386 DBUS_TYPE_OBJECT_PATH, &path);
387 dbus_message_iter_append_basic(&iter,
388 DBUS_TYPE_STRING, &error);
390 report_error = g_try_new0(struct report_error_data, 1);
391 if (report_error == NULL) {
392 dbus_message_unref(message);
396 if (dbus_connection_send_with_reply(connection, message,
397 &call, -1) == FALSE) {
398 dbus_message_unref(message);
399 g_free(report_error);
404 dbus_message_unref(message);
405 g_free(report_error);
409 report_error->service = connman_service_ref(service);
410 report_error->callback = callback;
411 report_error->user_data = user_data;
412 dbus_pending_call_set_notify(call, report_error_reply,
414 dbus_message_unref(message);
419 int __connman_agent_init(void)
423 connection = connman_dbus_get_connection();
424 if (connection == NULL)
430 void __connman_agent_cleanup(void)
432 DBusMessage *message;
436 if (connection == NULL)
440 g_dbus_remove_watch(connection, agent_watch);
442 if (agent_path == NULL)
445 message = dbus_message_new_method_call(agent_sender, agent_path,
446 CONNMAN_AGENT_INTERFACE, "Release");
450 dbus_message_set_no_reply(message, TRUE);
452 g_dbus_send_message(connection, message);
456 dbus_connection_unref(connection);