Send signals for Powered and Connected changes
[connman] / plugins / wifi.c
index f175494..660ea9f 100644 (file)
@@ -42,9 +42,9 @@
 #include "supplicant.h"
 
 struct wifi_data {
-       GStaticMutex mutex;
        GSList *list;
        gchar *identifier;
+       gboolean connected;
 };
 
 static int network_probe(struct connman_element *element)
@@ -61,14 +61,14 @@ static void network_remove(struct connman_element *element)
 
 static int network_enable(struct connman_element *element)
 {
-       char *identifier, *passphrase = NULL;
+       char *name, *security = NULL, *passphrase = NULL;
        unsigned char *ssid;
        int ssid_len;
 
        DBG("element %p name %s", element, element->name);
 
        if (connman_element_get_static_property(element,
-                                       "WiFi.Name", &identifier) == FALSE)
+                                               "Name", &name) == FALSE)
                return -EIO;
 
        if (connman_element_get_static_array_property(element,
@@ -78,18 +78,26 @@ static int network_enable(struct connman_element *element)
        if (element->parent != NULL) {
                struct wifi_data *data = connman_element_get_data(element->parent);
 
+               if (data->connected == TRUE)
+                       return -EBUSY;
+
                if (data != NULL) {
                        g_free(data->identifier);
-                       data->identifier = g_strdup(identifier);
+                       data->identifier = g_strdup(name);
                }
        }
 
        connman_element_get_value(element,
+                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
+
+       connman_element_get_value(element,
                        CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
 
-       DBG("identifier %s passhprase %s", identifier, passphrase);
+       DBG("name %s security %s passhprase %s",
+                                       name, security, passphrase);
 
-       if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
+       if (__supplicant_connect(element, ssid, ssid_len,
+                                               security, passphrase) < 0)
                connman_error("Failed to initiate connect");
 
        return 0;
@@ -125,7 +133,7 @@ static struct connman_element *find_element(struct wifi_data *data,
                struct connman_element *element = list->data;
 
                if (connman_element_match_static_property(element,
-                                       "WiFi.Name", &identifier) == TRUE)
+                                       "Name", &identifier) == TRUE)
                        return element;
        }
 
@@ -140,6 +148,9 @@ static void state_change(struct connman_element *parent,
 
        DBG("state %d", state);
 
+       if (data == NULL)
+               return;
+
        if (data->identifier == NULL)
                return;
 
@@ -150,13 +161,16 @@ static void state_change(struct connman_element *parent,
        if (state == STATE_COMPLETED) {
                struct connman_element *dhcp;
 
+               data->connected = TRUE;
+
                dhcp = connman_element_create(NULL);
 
                dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
                dhcp->index = element->index;
 
                connman_element_register(dhcp, element);
-       }
+       } else if (state == STATE_DISCONNECTED || state == STATE_INACTIVE)
+               data->connected = FALSE;
 }
 
 static void scan_result(struct connman_element *parent,
@@ -181,19 +195,23 @@ 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] == '-')
+               if (temp[i] == ' ' || temp[i] == '.')
                        temp[i] = '_';
-               if (temp[i] == '(' || temp[i] == ')')
+               else if (temp[i] == '-' || temp[i] == '+')
                        temp[i] = '_';
-               if (g_ascii_isprint(temp[i]) == FALSE)
+               else if (temp[i] == '!' || temp[i] == '?')
+                       temp[i] = '_';
+               else if (temp[i] == '(' || temp[i] == ')')
+                       temp[i] = '_';
+               else if (g_ascii_isprint(temp[i]) == FALSE)
                        temp[i] = '_';
                temp[i] = g_ascii_tolower(temp[i]);
        }
 
-       g_static_mutex_lock(&data->mutex);
-
        element = find_element(data, network->identifier);
        if (element == NULL) {
+               guint8 strength;
+
                element = connman_element_create(temp);
 
                element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
@@ -201,16 +219,42 @@ static void scan_result(struct connman_element *parent,
 
                data->list = g_slist_append(data->list, element);
 
-               connman_element_add_static_property(element, "WiFi.Name",
+               connman_element_add_static_property(element, "Name",
                                DBUS_TYPE_STRING, &network->identifier);
 
                connman_element_add_static_array_property(element, "WiFi.SSID",
                        DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
 
+               if (element->wifi.security == NULL) {
+                       const char *security;
+
+                       if (network->has_rsn == TRUE)
+                               security = "wpa2";
+                       else if (network->has_wpa == TRUE)
+                               security = "wpa";
+                       else if (network->has_wep == TRUE)
+                               security = "wep";
+                       else
+                               security = "none";
+
+                       element->wifi.security = g_strdup(security);
+               }
+
+               strength = network->quality;
+
+               connman_element_add_static_property(element, "WiFi.Strength",
+                                               DBUS_TYPE_BYTE, &strength);
+
+               //connman_element_add_static_property(element, "WiFi.Noise",
+               //                      DBUS_TYPE_INT32, &network->noise);
+
+               DBG("%s (%s) strength %d", network->identifier,
+                                       element->wifi.security, strength);
+
                connman_element_register(element, parent);
        }
 
-       g_static_mutex_unlock(&data->mutex);
+       element->available = TRUE;
 
        g_free(temp);
 }
@@ -230,7 +274,7 @@ static int wifi_probe(struct connman_element *element)
        if (data == NULL)
                return -ENOMEM;
 
-       g_static_mutex_init(&data->mutex);
+       data->connected = FALSE;
 
        connman_element_set_data(element, data);
 
@@ -282,8 +326,6 @@ static int wifi_disable(struct connman_element *element)
 
        __supplicant_disconnect(element);
 
-       g_static_mutex_lock(&data->mutex);
-
        for (list = data->list; list; list = list->next) {
                struct connman_element *network = list->data;
 
@@ -293,10 +335,10 @@ static int wifi_disable(struct connman_element *element)
        g_slist_free(data->list);
        data->list = NULL;
 
-       g_static_mutex_unlock(&data->mutex);
-
        connman_element_unregister_children(element);
 
+       __supplicant_stop(element);
+
        return 0;
 }
 
@@ -311,7 +353,6 @@ static struct connman_driver wifi_driver = {
        .disable        = wifi_disable,
 };
 
-static GStaticMutex device_mutex = G_STATIC_MUTEX_INIT;
 static GSList *device_list = NULL;
 
 static void wifi_newlink(unsigned short type, int index,
@@ -320,7 +361,7 @@ static void wifi_newlink(unsigned short type, int index,
        struct connman_element *device;
        GSList *list;
        gboolean exists = FALSE;
-       gchar *name;
+       gchar *name, *devname;
        struct iwreq iwr;
        int sk;
 
@@ -329,10 +370,11 @@ static void wifi_newlink(unsigned short type, int index,
        if (type != ARPHRD_ETHER)
                return;
 
-       name = inet_index2name(index);
+       name = inet_index2ident(index, "dev_");
+       devname = inet_index2name(index);
 
        memset(&iwr, 0, sizeof(iwr));
-       strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
+       strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
 
        sk = socket(PF_INET, SOCK_DGRAM, 0);
 
@@ -344,8 +386,6 @@ static void wifi_newlink(unsigned short type, int index,
 
        close(sk);
 
-       g_static_mutex_lock(&device_mutex);
-
        for (list = device_list; list; list = list->next) {
                struct connman_element *device = list->data;
 
@@ -355,8 +395,6 @@ static void wifi_newlink(unsigned short type, int index,
                }
        }
 
-       g_static_mutex_unlock(&device_mutex);
-
        if (exists == TRUE) {
                g_free(name);
                return;
@@ -368,13 +406,10 @@ static void wifi_newlink(unsigned short type, int index,
 
        device->index = index;
        device->name = name;
-
-       g_static_mutex_lock(&device_mutex);
+       device->devname = devname;
 
        connman_element_register(device, NULL);
        device_list = g_slist_append(device_list, device);
-
-       g_static_mutex_unlock(&device_mutex);
 }
 
 static void wifi_dellink(unsigned short type, int index,
@@ -384,8 +419,6 @@ static void wifi_dellink(unsigned short type, int index,
 
        DBG("index %d", index);
 
-       g_static_mutex_lock(&device_mutex);
-
        for (list = device_list; list; list = list->next) {
                struct connman_element *device = list->data;
 
@@ -396,8 +429,6 @@ static void wifi_dellink(unsigned short type, int index,
                        break;
                }
        }
-
-       g_static_mutex_unlock(&device_mutex);
 }
 
 static struct connman_rtnl wifi_rtnl = {
@@ -426,8 +457,6 @@ static void supplicant_disconnect(DBusConnection *connection, void *user_data)
 
        connman_rtnl_unregister(&wifi_rtnl);
 
-       g_static_mutex_lock(&device_mutex);
-
        for (list = device_list; list; list = list->next) {
                struct connman_element *device = list->data;
 
@@ -438,8 +467,6 @@ static void supplicant_disconnect(DBusConnection *connection, void *user_data)
        g_slist_free(device_list);
        device_list = NULL;
 
-       g_static_mutex_unlock(&device_mutex);
-
        __supplicant_exit();
 }
 
@@ -489,5 +516,5 @@ static void wifi_exit(void)
        dbus_connection_unref(connection);
 }
 
-CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
+CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
                                                        wifi_init, wifi_exit)