From b1527c31c9aa5ed571909dcde0a4c6851e829bd3 Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Tue, 21 Sep 2010 10:07:49 +0300 Subject: [PATCH] Agent API RequestInput method for requesting passphrases request_input_passphrase_reply creates and sends an Agent API RequestInput method call and waits for the method call return. The function expects a passhphrase_cb_t to be supplied. Only the "Passphrase" dictionary field is requested by the method call and only the "Passphrase" dictionary field in the method call return is used. --- src/agent.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/connman.h | 2 + 2 files changed, 132 insertions(+) diff --git a/src/agent.c b/src/agent.c index 2452b1b..c8341d4 100644 --- a/src/agent.c +++ b/src/agent.c @@ -113,6 +113,136 @@ int __connman_agent_request_passphrase(struct connman_service *service, return -EIO; } +struct request_input_reply { + struct connman_service *service; + passphrase_cb_t callback; + void *user_data; +}; + +static void request_input_passphrase_reply(DBusPendingCall *call, void *user_data) +{ + struct request_input_reply *passphrase_reply = user_data; + char *passphrase = NULL; + char *key; + DBusMessageIter iter, dict; + DBusMessage *reply = dbus_pending_call_steal_reply(call); + + if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) + goto done; + + dbus_message_iter_init(reply, &iter); + dbus_message_iter_recurse(&iter, &dict); + while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) { + DBusMessageIter entry, value; + + dbus_message_iter_recurse(&dict, &entry); + if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING) + break; + + dbus_message_iter_get_basic(&entry, &key); + if (g_str_equal(key, "Passphrase")) { + dbus_message_iter_next(&entry); + if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT) + break; + dbus_message_iter_recurse(&entry, &value); + dbus_message_iter_get_basic(&value, &passphrase); + break; + } + dbus_message_iter_next(&dict); + } + +done: + passphrase_reply->callback(passphrase_reply->service, + passphrase, passphrase_reply->user_data); + connman_service_unref(passphrase_reply->service); + dbus_message_unref(reply); + g_free(passphrase_reply); +} + +static void request_input_append_passphrase(DBusMessageIter *iter, void *user_data) +{ + struct connman_service *service = user_data; + char *value; + + switch (__connman_service_get_security(service)) { + case CONNMAN_SERVICE_SECURITY_WEP: + value = "wep"; + break; + case CONNMAN_SERVICE_SECURITY_PSK: + value = "psk"; + break; + default: + value = "string"; + break; + } + connman_dbus_dict_append_basic(iter, "Type", + DBUS_TYPE_STRING, &value); + value = "Mandatory"; + connman_dbus_dict_append_basic(iter, "Requirement", + DBUS_TYPE_STRING, &value); +} + +int __connman_agent_request_input(struct connman_service *service, + passphrase_cb_t callback, void *user_data) +{ + DBusMessage *message; + const char *path; + DBusMessageIter iter; + DBusMessageIter dict; + DBusPendingCall *call; + struct request_input_reply *passphrase_reply; + + if (service == NULL || agent_path == NULL || callback == NULL) + return -ESRCH; + + message = dbus_message_new_method_call(agent_sender, agent_path, + CONNMAN_AGENT_INTERFACE, + "RequestInput"); + if (message == NULL) + return -ENOMEM; + + dbus_message_iter_init_append(message, &iter); + + path = __connman_service_get_path(service); + dbus_message_iter_append_basic(&iter, + DBUS_TYPE_OBJECT_PATH, &path); + + connman_dbus_dict_open(&iter, &dict); + connman_dbus_dict_append_dict(&dict, "Passphrase", + request_input_append_passphrase, service); + connman_dbus_dict_close(&iter, &dict); + + passphrase_reply = g_try_new0(struct request_input_reply, 1); + if (passphrase_reply == NULL) { + dbus_message_unref(message); + return -ENOMEM; + } + + if (dbus_connection_send_with_reply(connection, message, + &call, -1) == FALSE) { + dbus_message_unref(message); + g_free(passphrase_reply); + return -ESRCH; + } + + if (call == NULL) { + dbus_message_unref(message); + g_free(passphrase_reply); + return -ESRCH; + } + + passphrase_reply->service = connman_service_ref(service); + passphrase_reply->callback = callback; + passphrase_reply->user_data = user_data; + + dbus_pending_call_set_notify(call, request_input_passphrase_reply, + passphrase_reply, NULL); + + dbus_message_unref(message); + + return -EIO; +} + int __connman_agent_init(void) { DBG(""); diff --git a/src/connman.h b/src/connman.h index f4ba942..1e5496a 100644 --- a/src/connman.h +++ b/src/connman.h @@ -82,6 +82,8 @@ typedef void (* passphrase_cb_t) (struct connman_service *service, int __connman_agent_request_passphrase(struct connman_service *service, passphrase_cb_t callback, void *user_data); +int __connman_agent_request_input(struct connman_service *service, + passphrase_cb_t callback, void *user_data); #include -- 2.7.4