Create unique device name and object path
[connman] / plugins / wifi.c
index f7280b1..93312ca 100644 (file)
 #include <config.h>
 #endif
 
+#include <stdio.h>
+#include <unistd.h>
 #include <string.h>
-#include <dbus/dbus.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/if_arp.h>
+#include <linux/wireless.h>
+
+#include <gdbus.h>
 
 #include <connman/plugin.h>
 #include <connman/driver.h>
+#include <connman/rtnl.h>
 #include <connman/log.h>
 
+#include "inet.h"
 #include "supplicant.h"
 
 struct wifi_data {
-       GStaticMutex mutex;
        GSList *list;
        gchar *identifier;
+       gboolean connected;
 };
 
 static int network_probe(struct connman_element *element)
@@ -52,32 +61,43 @@ 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,
-                                       "Identifier", &identifier) == FALSE)
+                                               "Name", &name) == FALSE)
                return -EIO;
 
        if (connman_element_get_static_array_property(element,
-                                       "SSID", &ssid, &ssid_len) == FALSE)
+                               "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 (data != NULL) {
                        g_free(data->identifier);
-                       data->identifier = g_strdup(identifier);
+                       data->identifier = g_strdup(name);
                }
        }
 
-       DBG("identifier %s passhprase %s", identifier, passphrase);
+       connman_element_get_value(element,
+                       CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
+
+       connman_element_get_value(element,
+                       CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
 
-       if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
+       DBG("name %s security %s passhprase %s",
+                                       name, security, passphrase);
+
+       if (__supplicant_connect(element, ssid, ssid_len,
+                                               security, passphrase) < 0)
                connman_error("Failed to initiate connect");
 
        return 0;
@@ -113,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,
-                                       "Identifier", &identifier) == TRUE)
+                                       "Name", &identifier) == TRUE)
                        return element;
        }
 
@@ -138,13 +158,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,
@@ -169,19 +192,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;
@@ -189,16 +216,42 @@ static void scan_result(struct connman_element *parent,
 
                data->list = g_slist_append(data->list, element);
 
-               connman_element_add_static_property(element, "Identifier",
+               connman_element_add_static_property(element, "Name",
                                DBUS_TYPE_STRING, &network->identifier);
 
-               connman_element_add_static_array_property(element, "SSID",
+               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);
 }
@@ -218,7 +271,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);
 
@@ -270,8 +323,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;
 
@@ -281,8 +332,6 @@ 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);
 
        return 0;
@@ -299,27 +348,153 @@ static struct connman_driver wifi_driver = {
        .disable        = wifi_disable,
 };
 
+static GSList *device_list = NULL;
+
+static void wifi_newlink(unsigned short type, int index,
+                                       unsigned flags, unsigned change)
+{
+       struct connman_element *device;
+       GSList *list;
+       gboolean exists = FALSE;
+       gchar *name, *devname;
+       struct iwreq iwr;
+       int sk;
+
+       DBG("index %d", index);
+
+       if (type != ARPHRD_ETHER)
+               return;
+
+       name = inet_index2ident(index, "dev_");
+       devname = inet_index2name(index);
+
+       memset(&iwr, 0, sizeof(iwr));
+       strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
+
+       sk = socket(PF_INET, SOCK_DGRAM, 0);
+
+       if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
+               g_free(name);
+               close(sk);
+               return;
+       }
+
+       close(sk);
+
+       for (list = device_list; list; list = list->next) {
+               struct connman_element *device = list->data;
+
+               if (device->index == index) {
+                       exists = TRUE;
+                       break;
+               }
+       }
+
+       if (exists == TRUE) {
+               g_free(name);
+               return;
+       }
+
+       device = connman_element_create(NULL);
+       device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
+       device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
+
+       device->index = index;
+       device->name = name;
+       device->devname = devname;
+
+       connman_element_register(device, NULL);
+       device_list = g_slist_append(device_list, device);
+}
+
+static void wifi_dellink(unsigned short type, int index,
+                                       unsigned flags, unsigned change)
+{
+       GSList *list;
+
+       DBG("index %d", index);
+
+       for (list = device_list; list; list = list->next) {
+               struct connman_element *device = list->data;
+
+               if (device->index == index) {
+                       device_list = g_slist_remove(device_list, device);
+                       connman_element_unregister(device);
+                       connman_element_unref(device);
+                       break;
+               }
+       }
+}
+
+static struct connman_rtnl wifi_rtnl = {
+       .name           = "wifi",
+       .newlink        = wifi_newlink,
+       .dellink        = wifi_dellink,
+};
+
+static void supplicant_connect(DBusConnection *connection, void *user_data)
+{
+       DBG("connection %p", connection);
+
+       __supplicant_init(connection);
+
+       if (connman_rtnl_register(&wifi_rtnl) < 0)
+               return;
+
+       connman_rtnl_send_getlink();
+}
+
+static void supplicant_disconnect(DBusConnection *connection, void *user_data)
+{
+       GSList *list;
+
+       DBG("connection %p", connection);
+
+       connman_rtnl_unregister(&wifi_rtnl);
+
+       for (list = device_list; list; list = list->next) {
+               struct connman_element *device = list->data;
+
+               connman_element_unregister(device);
+               connman_element_unref(device);
+       }
+
+       g_slist_free(device_list);
+       device_list = NULL;
+
+       __supplicant_exit();
+}
+
+static DBusConnection *connection;
+static guint watch;
+
 static int wifi_init(void)
 {
        int err;
 
-       err = __supplicant_init();
-       if (err < 0)
-               return err;
+       connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (connection == NULL)
+               return -EIO;
 
        err = connman_driver_register(&network_driver);
        if (err < 0) {
-               __supplicant_exit();
+               dbus_connection_unref(connection);
                return err;
        }
 
        err = connman_driver_register(&wifi_driver);
        if (err < 0) {
                connman_driver_unregister(&network_driver);
-               __supplicant_exit();
+               dbus_connection_unref(connection);
                return err;
        }
 
+       watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
+                       supplicant_connect, supplicant_disconnect, NULL, NULL);
+
+       if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
+               supplicant_connect(connection, NULL);
+
        return 0;
 }
 
@@ -328,8 +503,13 @@ static void wifi_exit(void)
        connman_driver_unregister(&network_driver);
        connman_driver_unregister(&wifi_driver);
 
-       __supplicant_exit();
+       if (watch > 0)
+               g_dbus_remove_watch(connection, watch);
+
+       supplicant_disconnect(connection, NULL);
+
+       dbus_connection_unref(connection);
 }
 
-CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
+CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
                                                        wifi_init, wifi_exit)