From 3ff2030313c116a4bf0421b0b0086e7c252e7ea4 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 23 Nov 2008 13:24:50 +0100 Subject: [PATCH] Add support for WiFi Security and Passphrase properties --- include/element.h | 5 +++++ include/property.h | 3 +++ src/element.c | 45 +++++++++++++++++++++++++++++++++++-- src/storage.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) diff --git a/include/element.h b/include/element.h index fedc407..96031db 100644 --- a/include/element.h +++ b/include/element.h @@ -100,6 +100,11 @@ struct connman_element { gchar *broadcast; gchar *nameserver; } ipv4; + + struct { + gchar *security; + gchar *passphrase; + } wifi; }; #define connman_element_lock(element) g_static_mutex_lock(&(element)->mutex) diff --git a/include/property.h b/include/property.h index b8c86c9..4c42f44 100644 --- a/include/property.h +++ b/include/property.h @@ -40,6 +40,9 @@ enum connman_property_id { CONNMAN_PROPERTY_ID_IPV4_NETMASK, CONNMAN_PROPERTY_ID_IPV4_GATEWAY, CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, + + CONNMAN_PROPERTY_ID_WIFI_SECURITY, + CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, }; /** diff --git a/src/element.c b/src/element.c index cb513ec..3e2e0c3 100644 --- a/src/element.c +++ b/src/element.c @@ -61,6 +61,12 @@ static struct { DBUS_TYPE_STRING, "IPv4.Gateway" }, { CONNMAN_PROPERTY_ID_IPV4_NAMESERVER, DBUS_TYPE_STRING, "IPv4.Nameserver" }, + + { CONNMAN_PROPERTY_ID_WIFI_SECURITY, + DBUS_TYPE_STRING, "WiFi.Security" }, + { CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, + DBUS_TYPE_STRING, "WiFi.Passphrase" }, + { } }; @@ -212,6 +218,13 @@ static DBusMessage *get_properties(DBusConnection *conn, connman_dbus_dict_append_variant(&dict, "IPv4.Gateway", DBUS_TYPE_STRING, &element->ipv4.gateway); + if (element->wifi.security != NULL) + connman_dbus_dict_append_variant(&dict, "WiFi.Security", + DBUS_TYPE_STRING, &element->wifi.security); + if (element->wifi.passphrase != NULL) + connman_dbus_dict_append_variant(&dict, "WiFi.Passphrase", + DBUS_TYPE_STRING, &element->wifi.passphrase); + connman_element_lock(element); for (list = element->properties; list; list = list->next) { @@ -922,6 +935,18 @@ int connman_element_set_property(struct connman_element *element, element->ipv4.nameserver = g_strdup(*((const char **) value)); connman_element_unlock(element); break; + case CONNMAN_PROPERTY_ID_WIFI_SECURITY: + connman_element_lock(element); + g_free(element->wifi.security); + element->wifi.security = g_strdup(*((const char **) value)); + connman_element_unlock(element); + break; + case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE: + connman_element_lock(element); + g_free(element->wifi.passphrase); + element->wifi.passphrase = g_strdup(*((const char **) value)); + connman_element_unlock(element); + break; default: return -EINVAL; } @@ -973,6 +998,22 @@ int connman_element_get_value(struct connman_element *element, *((char **) value) = element->ipv4.nameserver; connman_element_unlock(element); break; + case CONNMAN_PROPERTY_ID_WIFI_SECURITY: + if (element->wifi.security == NULL) + return connman_element_get_value(element->parent, + id, value); + connman_element_lock(element); + *((char **) value) = element->wifi.security; + connman_element_unlock(element); + break; + case CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE: + if (element->wifi.passphrase == NULL) + return connman_element_get_value(element->parent, + id, value); + connman_element_lock(element); + *((char **) value) = element->wifi.passphrase; + connman_element_unlock(element); + break; default: return -EINVAL; } @@ -1097,8 +1138,6 @@ int connman_element_register(struct connman_element *element, connman_element_lock(element); - __connman_element_load(element); - if (element->name == NULL) { element->name = g_strdup(type2string(element->type)); if (element->name == NULL) { @@ -1213,6 +1252,8 @@ static void register_element(gpointer data, gpointer user_data) DBG("element %p path %s", element, element->path); + __connman_element_load(element); + g_node_append_data(node, element); if (g_dbus_register_interface(connection, element->path, diff --git a/src/storage.c b/src/storage.c index 24d0a5c..06337c7 100644 --- a/src/storage.c +++ b/src/storage.c @@ -37,16 +37,67 @@ void __connman_storage_cleanup(void) DBG(""); } +static int do_load(GKeyFile *keyfile, struct connman_element *element) +{ + const gchar *value; + + DBG("element %p name %s", element, element->name); + + value = g_key_file_get_string(keyfile, element->path, + "WiFi.Security", NULL); + if (value != NULL) + connman_element_set_property(element, + CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value); + + value = g_key_file_get_string(keyfile, element->path, + "WiFi.Passphrase", NULL); + if (value != NULL) + connman_element_set_property(element, + CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value); + + return 0; +} + int __connman_element_load(struct connman_element *element) { + GKeyFile *keyfile; + gchar *pathname, *data = NULL; + gsize length; + DBG("element %p name %s", element, element->name); + pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR); + if (pathname == NULL) + return -ENOMEM; + + keyfile = g_key_file_new(); + + if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) { + g_free(pathname); + return -ENOENT; + } + + g_free(pathname); + + if (g_key_file_load_from_data(keyfile, data, length, + 0, NULL) == FALSE) { + g_free(data); + return -EILSEQ; + } + + g_free(data); + + do_load(keyfile, element); + + g_key_file_free(keyfile); + return 0; } static void do_update(GKeyFile *keyfile, struct connman_element *element) { GSList *list; + char *value; DBG("element %p name %s", element, element->name); @@ -72,6 +123,16 @@ static void do_update(GKeyFile *keyfile, struct connman_element *element) } connman_element_unlock(element); + + if (connman_element_get_value(element, + CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0) + g_key_file_set_string(keyfile, element->path, + "WiFi.Security", value); + + if (connman_element_get_value(element, + CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0) + g_key_file_set_string(keyfile, element->path, + "WiFi.Passphrase", value); } int __connman_element_store(struct connman_element *element) @@ -82,6 +143,10 @@ int __connman_element_store(struct connman_element *element) DBG("element %p name %s", element, element->name); + if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE && + element->type != CONNMAN_ELEMENT_TYPE_NETWORK) + return -EINVAL; + pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR); if (pathname == NULL) return -ENOMEM; -- 2.7.4