Restart scanning after disconnecting
[connman] / plugins / wifi.c
index e463589..67ac993 100644 (file)
 #include "inet.h"
 #include "supplicant.h"
 
-#define CLEANUP_PENDING_TIMEOUT  8     /* in seconds */
+#define CLEANUP_TIMEOUT   8    /* in seconds */
+#define INACTIVE_TIMEOUT  12   /* in seconds */
 
 struct wifi_data {
        GSList *current;
        GSList *pending;
-       guint timer;
+       guint cleanup_timer;
+       guint inactive_timer;
        gchar *identifier;
        gboolean connected;
 };
@@ -65,6 +67,7 @@ static void network_remove(struct connman_element *element)
 
 static int network_enable(struct connman_element *element)
 {
+       struct connman_element *device = element->parent;
        char *name, *security = NULL, *passphrase = NULL;
        unsigned char *ssid;
        int ssid_len;
@@ -79,13 +82,13 @@ static int network_enable(struct connman_element *element)
                                "WiFi.SSID", &ssid, &ssid_len) == FALSE)
                return -EIO;
 
-       if (element->parent != NULL) {
-               struct wifi_data *data = connman_element_get_data(element->parent);
-
-               if (data->connected == TRUE)
-                       return -EBUSY;
+       if (device != NULL) {
+               struct wifi_data *data = connman_element_get_data(device);
 
                if (data != NULL) {
+                       if (data->connected == TRUE)
+                               return -EBUSY;
+
                        g_free(data->identifier);
                        data->identifier = g_strdup(name);
                }
@@ -160,14 +163,38 @@ static struct connman_element *find_pending_element(struct wifi_data *data,
        return NULL;
 }
 
-static void state_change(struct connman_element *parent,
+static gboolean inactive_scan(gpointer user_data)
+{
+       struct connman_element *device = user_data;
+       struct wifi_data *data = connman_element_get_data(device);
+
+       DBG("");
+
+       if (data->cleanup_timer > 0) {
+               g_source_remove(data->cleanup_timer);
+               data->cleanup_timer = 0;
+       }
+
+       __supplicant_scan(device);
+
+       data->inactive_timer = 0;
+
+       return FALSE;
+}
+
+static void state_change(struct connman_element *device,
                                                enum supplicant_state state)
 {
-       struct wifi_data *data = connman_element_get_data(parent);
+       struct wifi_data *data = connman_element_get_data(device);
        struct connman_element *element;
 
        DBG("state %d", state);
 
+       if ((state == STATE_INACTIVE || state == STATE_DISCONNECTED) &&
+                                               data->inactive_timer == 0)
+               data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
+                                                       inactive_scan, device);
+
        if (data == NULL)
                return;
 
@@ -212,14 +239,14 @@ static gboolean cleanup_pending(gpointer user_data)
        g_slist_free(data->pending);
        data->pending = NULL;
 
-       data->timer = 0;
+       data->cleanup_timer = 0;
 
        return FALSE;
 }
 
-static void clear_results(struct connman_element *parent)
+static void clear_results(struct connman_element *device)
 {
-       struct wifi_data *data = connman_element_get_data(parent);
+       struct wifi_data *data = connman_element_get_data(device);
 
        DBG("pending %d", g_slist_length(data->pending));
        DBG("current %d", g_slist_length(data->current));
@@ -227,15 +254,17 @@ static void clear_results(struct connman_element *parent)
        data->pending = data->current;
        data->current = NULL;
 
-       if (data->timer == 0)
-               data->timer = g_timeout_add_seconds(CLEANUP_PENDING_TIMEOUT,
+       if (data->cleanup_timer > 0)
+               return;
+
+       data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
                                                        cleanup_pending, data);
 }
 
-static void scan_result(struct connman_element *parent,
+static void scan_result(struct connman_element *device,
                                        struct supplicant_network *network)
 {
-       struct wifi_data *data = connman_element_get_data(parent);
+       struct wifi_data *data = connman_element_get_data(device);
        struct connman_element *element;
        gchar *temp;
        int i;
@@ -254,17 +283,10 @@ static void scan_result(struct connman_element *parent,
        temp = g_strdup(network->identifier);
 
        for (i = 0; i < strlen(temp); i++) {
-               if (temp[i] == ' ' || temp[i] == '.')
-                       temp[i] = '_';
-               else if (temp[i] == '-' || temp[i] == '+')
-                       temp[i] = '_';
-               else if (temp[i] == '!' || temp[i] == '?')
-                       temp[i] = '_';
-               else if (temp[i] == '(' || temp[i] == ')')
-                       temp[i] = '_';
-               else if (g_ascii_isprint(temp[i]) == FALSE)
+               gchar tmp = g_ascii_tolower(temp[i]);
+
+               if (tmp < 'a' || tmp > 'z')
                        temp[i] = '_';
-               temp[i] = g_ascii_tolower(temp[i]);
        }
 
        element = find_pending_element(data, network->identifier);
@@ -274,7 +296,7 @@ static void scan_result(struct connman_element *parent,
                element = connman_element_create(temp);
 
                element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
-               element->index = parent->index;
+               element->index = device->index;
 
                connman_element_add_static_property(element, "Name",
                                DBUS_TYPE_STRING, &network->identifier);
@@ -308,7 +330,7 @@ static void scan_result(struct connman_element *parent,
                DBG("%s (%s) strength %d", network->identifier,
                                        element->wifi.security, strength);
 
-               connman_element_register(element, parent);
+               connman_element_register(element, device);
        } else
                data->pending = g_slist_remove(data->pending, element);
 
@@ -385,9 +407,14 @@ static int wifi_disable(struct connman_element *element)
 
        DBG("element %p name %s", element, element->name);
 
-       if (data->timer > 0) {
-               g_source_remove(data->timer);
-               data->timer = 0;
+       if (data->cleanup_timer > 0) {
+               g_source_remove(data->cleanup_timer);
+               data->cleanup_timer = 0;
+       }
+
+       if (data->inactive_timer > 0) {
+               g_source_remove(data->inactive_timer);
+               data->inactive_timer = 0;
        }
 
        __supplicant_disconnect(element);