Use list of known networks to re-connect to
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 13 Mar 2008 22:27:05 +0000 (23:27 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 13 Mar 2008 22:27:05 +0000 (23:27 +0100)
src/connman.h
src/iface-storage.c
src/iface.c

index c253a8e..f9416d2 100644 (file)
@@ -70,6 +70,8 @@ int __connman_iface_init_via_inet(struct connman_iface *iface);
 int __connman_iface_up(struct connman_iface *iface);
 int __connman_iface_down(struct connman_iface *iface);
 
+char *__connman_iface_find_passphrase(struct connman_iface *iface,
+                                                       const char *network);
 int __connman_iface_load(struct connman_iface *iface);
 int __connman_iface_store(struct connman_iface *iface);
 
index a5b57a3..7872f7c 100644 (file)
 
 #define GROUP_CONFIG  "Config"
 
+char *__connman_iface_find_passphrase(struct connman_iface *iface,
+                                                       const char *network)
+{
+       GKeyFile *keyfile;
+       gchar *pathname, *result = NULL;
+       gchar **list;
+       gsize list_len;
+       int i;
+
+       DBG("iface %p", iface);
+
+       if (iface->identifier == NULL)
+               return NULL;
+
+       pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
+                                                       iface->identifier);
+       if (pathname == NULL)
+               return NULL;
+
+       keyfile = g_key_file_new();
+
+       g_key_file_set_list_separator(keyfile, ',');
+
+       if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
+               goto done;
+
+       if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
+               goto done;
+
+       list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
+                                       "KnownNetworks", &list_len, NULL);
+       for (i = 0; i < list_len; i++)
+               if (g_str_equal(list[i], network) == TRUE) {
+                       result = g_key_file_get_string(keyfile, network,
+                                                               "PSK", NULL);
+                       if (result == NULL)
+                               result = g_strdup("");
+                       break;
+               }
+
+       g_strfreev(list);
+
+done:
+       g_key_file_free(keyfile);
+
+       g_free(pathname);
+
+       return result;
+}
+
 int __connman_iface_load(struct connman_iface *iface)
 {
        GKeyFile *keyfile;
        gchar *pathname, *str;
+       gchar **list;
+       gsize list_len;
 
        DBG("iface %p", iface);
 
@@ -49,6 +101,8 @@ int __connman_iface_load(struct connman_iface *iface)
 
        keyfile = g_key_file_new();
 
+       g_key_file_set_list_separator(keyfile, ',');
+
        if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
                goto done;
 
@@ -61,7 +115,13 @@ int __connman_iface_load(struct connman_iface *iface)
                g_free(str);
        }
 
-       str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Network", NULL);
+       list = g_key_file_get_string_list(keyfile, GROUP_CONFIG,
+                                       "KnownNetworks", &list_len, NULL);
+
+       g_strfreev(list);
+
+       str = g_key_file_get_string(keyfile, GROUP_CONFIG,
+                                               "LastNetwork", NULL);
        if (str != NULL) {
                g_free(iface->network.identifier);
                iface->network.identifier = str;
@@ -93,9 +153,10 @@ static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
 
        if (iface->network.identifier != NULL) {
                g_key_file_set_string(keyfile, GROUP_CONFIG,
-                               "Network", iface->network.identifier);
+                               "LastNetwork", iface->network.identifier);
        } else
-               g_key_file_remove_key(keyfile, GROUP_CONFIG, "Network", NULL);
+               g_key_file_remove_key(keyfile, GROUP_CONFIG,
+                                               "LastNetwork", NULL);
 
        if (iface->network.identifier != NULL)
                g_key_file_set_string(keyfile, iface->network.identifier,
index b9fe513..a35d22a 100644 (file)
@@ -360,6 +360,7 @@ void connman_iface_indicate_station(struct connman_iface *iface,
                                const char *name, int strength, int security)
 {
        DBusMessage *signal;
+       char *passphrase;
 
        DBG("iface %p security %d name %s", iface, security, name);
 
@@ -376,9 +377,15 @@ void connman_iface_indicate_station(struct connman_iface *iface,
        dbus_connection_send(connection, signal, NULL);
        dbus_message_unref(signal);
 
-       if (g_str_equal(name, iface->network.identifier) == TRUE &&
-                       iface->state == CONNMAN_IFACE_STATE_SCANNING) {
+       if (iface->state != CONNMAN_IFACE_STATE_SCANNING)
+               return;
+
+       passphrase = __connman_iface_find_passphrase(iface, name);
+       if (passphrase != NULL) {
+               g_free(iface->network.identifier);
                iface->network.identifier = g_strdup(name);
+               g_free(iface->network.passphrase);
+               iface->network.passphrase = passphrase;
 
                if (iface->driver->connect) {
                        iface->driver->connect(iface, &iface->network);