X-Git-Url: https://vcs.maemo.org/git/?a=blobdiff_plain;f=plugins%2Fwifi.c;h=6dafad47cf31ce402766287669b37fd9bb36bfc3;hb=c4cca8087b85ee569b450a9b4ea91eb7f0b8a3d4;hp=93312caaebaf2282f1a5155547a3de2ca54a695f;hpb=63c934f2f9daeb743416c0f7a440adf72690a3fc;p=connman diff --git a/plugins/wifi.c b/plugins/wifi.c index 93312ca..6dafad4 100644 --- a/plugins/wifi.c +++ b/plugins/wifi.c @@ -41,8 +41,14 @@ #include "inet.h" #include "supplicant.h" +#define CLEANUP_TIMEOUT 8 /* in seconds */ +#define INACTIVE_TIMEOUT 12 /* in seconds */ + struct wifi_data { - GSList *list; + GSList *current; + GSList *pending; + guint cleanup_timer; + guint inactive_timer; gchar *identifier; gboolean connected; }; @@ -61,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; @@ -75,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); } @@ -124,12 +131,12 @@ static struct connman_driver network_driver = { .disable = network_disable, }; -static struct connman_element *find_element(struct wifi_data *data, - const char *identifier) +static struct connman_element *find_current_element(struct wifi_data *data, + const char *identifier) { GSList *list; - for (list = data->list; list; list = list->next) { + for (list = data->current; list; list = list->next) { struct connman_element *element = list->data; if (connman_element_match_static_property(element, @@ -140,25 +147,91 @@ static struct connman_element *find_element(struct wifi_data *data, return NULL; } -static void state_change(struct connman_element *parent, +static struct connman_element *find_pending_element(struct wifi_data *data, + const char *identifier) +{ + GSList *list; + + for (list = data->pending; list; list = list->next) { + struct connman_element *element = list->data; + + if (connman_element_match_static_property(element, + "Name", &identifier) == TRUE) + return element; + } + + return NULL; +} + +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 connect_known_networks(struct connman_element *device) +{ + struct wifi_data *data = connman_element_get_data(device); + GSList *list; + + DBG(""); + + if (data->inactive_timer > 0) { + g_source_remove(data->inactive_timer); + data->inactive_timer = 0; + } + + for (list = data->current; list; list = list->next) { + struct connman_element *element = list->data; + + if (element->policy == CONNMAN_ELEMENT_POLICY_AUTO && + element->remember == TRUE && + element->available == TRUE) { + if (network_enable(element) == 0) + return; + } + } + + data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT, + inactive_scan, device); +} + +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 (data->identifier == NULL) + if (data == NULL) return; - element = find_element(data, data->identifier); + if (data->identifier == NULL) + goto reconnect; + + element = find_current_element(data, data->identifier); if (element == NULL) - return; + goto reconnect; if (state == STATE_COMPLETED) { struct connman_element *dhcp; data->connected = TRUE; + connman_element_set_enabled(element, TRUE); dhcp = connman_element_create(NULL); @@ -166,14 +239,65 @@ static void state_change(struct connman_element *parent, dhcp->index = element->index; connman_element_register(dhcp, element); - } else if (state == STATE_DISCONNECTED || state == STATE_INACTIVE) + } else if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) { data->connected = FALSE; + connman_element_set_enabled(element, FALSE); + + connman_element_unregister_children(element); + } + +reconnect: + if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) { + data->connected = FALSE; + connect_known_networks(device); + } +} + +static gboolean cleanup_pending(gpointer user_data) +{ + struct wifi_data *data = user_data; + GSList *list; + + DBG(""); + + for (list = data->pending; list; list = list->next) { + struct connman_element *element = list->data; + + DBG("element %p name %s", element, element->name); + + connman_element_unregister(element); + connman_element_unref(element); + } + + g_slist_free(data->pending); + data->pending = NULL; + + data->cleanup_timer = 0; + + return FALSE; +} + +static void clear_results(struct connman_element *device) +{ + 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)); + + data->pending = data->current; + data->current = NULL; + + 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; @@ -192,29 +316,20 @@ 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_element(data, network->identifier); + element = find_pending_element(data, network->identifier); if (element == NULL) { guint8 strength; element = connman_element_create(temp); element->type = CONNMAN_ELEMENT_TYPE_NETWORK; - element->index = parent->index; - - data->list = g_slist_append(data->list, element); + element->index = device->index; connman_element_add_static_property(element, "Name", DBUS_TYPE_STRING, &network->identifier); @@ -248,8 +363,11 @@ 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); + + data->current = g_slist_append(data->current, element); element->available = TRUE; @@ -258,6 +376,7 @@ static void scan_result(struct connman_element *parent, static struct supplicant_callback wifi_callback = { .state_change = state_change, + .clear_results = clear_results, .scan_result = scan_result, }; @@ -321,19 +440,40 @@ static int wifi_disable(struct connman_element *element) DBG("element %p name %s", element, element->name); + 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); - for (list = data->list; list; list = list->next) { + for (list = data->pending; list; list = list->next) { struct connman_element *network = list->data; connman_element_unref(network); } - g_slist_free(data->list); - data->list = NULL; + g_slist_free(data->pending); + data->pending = NULL; + + for (list = data->current; list; list = list->next) { + struct connman_element *network = list->data; + + connman_element_unref(network); + } + + g_slist_free(data->current); + data->current = NULL; connman_element_unregister_children(element); + __supplicant_stop(element); + return 0; }