Improve selecting of networks
[connman] / src / iface.c
index 4a655a1..27bae8f 100644 (file)
@@ -157,14 +157,11 @@ static void state_changed(struct connman_iface *iface)
 
        switch (iface->state) {
        case CONNMAN_IFACE_STATE_OFF:
-               __connman_dhcp_release(iface);
+               __connman_iface_stop(iface);
                break;
 
        case CONNMAN_IFACE_STATE_ENABLED:
-               __connman_dhcp_release(iface);
-               connman_iface_clear_ipv4(iface);
-               if (iface->driver->disconnect)
-                       iface->driver->disconnect(iface);
+               __connman_iface_start(iface);
                if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING)
                        state = CONNMAN_IFACE_STATE_SCANNING;
                break;
@@ -185,9 +182,7 @@ static void state_changed(struct connman_iface *iface)
                break;
 
        case CONNMAN_IFACE_STATE_SHUTDOWN:
-               __connman_dhcp_release(iface);
-               if (iface->driver->disconnect)
-                       iface->driver->disconnect(iface);
+               __connman_iface_stop(iface);
                if (iface->policy != CONNMAN_IFACE_POLICY_AUTO)
                        state = CONNMAN_IFACE_STATE_OFF;
                break;
@@ -207,28 +202,19 @@ static void state_changed(struct connman_iface *iface)
 
 static void switch_policy(struct connman_iface *iface)
 {
-       DBG("iface %p", iface);
+       DBG("iface %p policy %d", iface, iface->policy);
 
        switch (iface->policy) {
        case CONNMAN_IFACE_POLICY_OFF:
-               iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
-               state_changed(iface);
-               connman_iface_clear_ipv4(iface);
-               if (iface->driver->stop)
-                       iface->driver->stop(iface);
-               else
-                       __connman_iface_down(iface);
+               __connman_iface_stop(iface);
                break;
 
        case CONNMAN_IFACE_POLICY_IGNORE:
                break;
 
        case CONNMAN_IFACE_POLICY_AUTO:
-               if (iface->driver->start)
-                       iface->driver->start(iface);
-               else
-                       __connman_iface_up(iface);
-               state_changed(iface);
+       case CONNMAN_IFACE_POLICY_ASK:
+               __connman_iface_start(iface);
                break;
 
        default:
@@ -236,13 +222,12 @@ static void switch_policy(struct connman_iface *iface)
        }
 }
 
-void connman_iface_indicate_enabled(struct connman_iface *iface)
+void connman_iface_indicate_ifup(struct connman_iface *iface)
 {
        DBG("iface %p state %d", iface, iface->state);
 
        switch (iface->state) {
        case CONNMAN_IFACE_STATE_OFF:
-       case CONNMAN_IFACE_STATE_CARRIER:
                iface->state = CONNMAN_IFACE_STATE_ENABLED;
                state_changed(iface);
                break;
@@ -251,17 +236,13 @@ void connman_iface_indicate_enabled(struct connman_iface *iface)
        }
 }
 
-void connman_iface_indicate_disabled(struct connman_iface *iface)
+void connman_iface_indicate_ifdown(struct connman_iface *iface)
 {
        DBG("iface %p state %d", iface, iface->state);
 
-       if (iface->policy == CONNMAN_IFACE_POLICY_AUTO) {
+       if (iface->policy == CONNMAN_IFACE_POLICY_AUTO)
                iface->state = CONNMAN_IFACE_STATE_ENABLED;
-               if (iface->driver->start)
-                       iface->driver->start(iface);
-               else
-                       __connman_iface_up(iface);
-       } else
+       else
                iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
 
        state_changed(iface);
@@ -305,13 +286,10 @@ void connman_iface_indicate_carrier_off(struct connman_iface *iface)
        case CONNMAN_IFACE_STATE_CARRIER:
        case CONNMAN_IFACE_STATE_CONFIGURE:
        case CONNMAN_IFACE_STATE_READY:
-#if 0
-               if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING) {
-                       if (iface->driver->disconnect)
-                               iface->driver->disconnect(iface);
+               __connman_iface_disconnect(iface);
+               if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING)
                        iface->state = CONNMAN_IFACE_STATE_SCANNING;
-               } else
-#endif
+               else
                        iface->state = CONNMAN_IFACE_STATE_ENABLED;
                state_changed(iface);
                break;
@@ -328,6 +306,7 @@ void connman_iface_indicate_configured(struct connman_iface *iface)
        case CONNMAN_IFACE_STATE_CONFIGURE:
                iface->state = CONNMAN_IFACE_STATE_READY;
                state_changed(iface);
+               __connman_iface_store_current_network(iface);
                break;
        default:
                break;
@@ -360,7 +339,8 @@ void connman_iface_indicate_station(struct connman_iface *iface,
                                const char *name, int strength, int security)
 {
        DBusMessage *signal;
-       char *passphrase;
+       char *ssid, *passphrase;
+       int len;
 
        DBG("iface %p security %d name %s", iface, security, name);
 
@@ -377,24 +357,42 @@ void connman_iface_indicate_station(struct connman_iface *iface,
        dbus_connection_send(connection, signal, NULL);
        dbus_message_unref(signal);
 
-       if (iface->state != CONNMAN_IFACE_STATE_SCANNING)
+       switch (iface->state) {
+       case CONNMAN_IFACE_STATE_CONNECT:
+       case CONNMAN_IFACE_STATE_CONNECTED:
+       case CONNMAN_IFACE_STATE_CARRIER:
+       case CONNMAN_IFACE_STATE_CONFIGURE:
+       case CONNMAN_IFACE_STATE_READY:
+               return;
+       default:
+               break;
+       }
+
+       len = strlen(name);
+       ssid = strdup(name);
+       if (ssid == NULL)
                return;
 
-       passphrase = __connman_iface_find_passphrase(iface, name);
+       /* The D-Link access points return a 0x05 at the end of the SSID */
+       if (ssid[len - 1] == '\05')
+               ssid[len - 1] = '\0';
+
+       passphrase = __connman_iface_find_passphrase(iface, ssid);
        if (passphrase != NULL) {
-               DBG("network %s passphrase %s", name, passphrase);
+               DBG("network %s passphrase %s", ssid, passphrase);
 
                g_free(iface->network.identifier);
-               iface->network.identifier = g_strdup(name);
+               iface->network.identifier = g_strdup(ssid);
                g_free(iface->network.passphrase);
                iface->network.passphrase = passphrase;
 
-               if (iface->driver->connect) {
-                       iface->driver->connect(iface, &iface->network);
-                       iface->state = CONNMAN_IFACE_STATE_CONNECT;
-                       state_changed(iface);
-               }
+               __connman_iface_connect(iface, &iface->network);
+
+               iface->state = CONNMAN_IFACE_STATE_CONNECT;
+               state_changed(iface);
        }
+
+       free(ssid);
 }
 
 int connman_iface_get_ipv4(struct connman_iface *iface,
@@ -431,7 +429,6 @@ int connman_iface_set_ipv4(struct connman_iface *iface,
        struct ifreq ifr;
        struct rtentry rt;
        struct sockaddr_in *addr;
-       char cmd[128];
        int sk, err;
 
        if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
@@ -504,12 +501,7 @@ int connman_iface_set_ipv4(struct connman_iface *iface,
                return -1;
        }
 
-       sprintf(cmd, "echo \"nameserver %s\" | resolvconf -a %s",
-                               inet_ntoa(ipv4->nameserver), ifr.ifr_name);
-
-       DBG("%s", cmd);
-
-       err = system(cmd);
+       __connman_resolver_append(iface, inet_ntoa(ipv4->nameserver));
 
        return 0;
 }
@@ -518,7 +510,6 @@ int connman_iface_clear_ipv4(struct connman_iface *iface)
 {
        struct ifreq ifr;
        struct sockaddr_in *addr;
-       char cmd[128];
        int sk, err;
 
        if ((iface->flags & CONNMAN_IFACE_FLAG_RTNL) == 0)
@@ -554,11 +545,7 @@ int connman_iface_clear_ipv4(struct connman_iface *iface)
                return -1;
        }
 
-       sprintf(cmd, "resolvconf -d %s", ifr.ifr_name);
-
-       DBG("%s", cmd);
-
-       err = system(cmd);
+       __connman_resolver_remove(iface);
 
        return 0;
 }
@@ -576,11 +563,19 @@ static DBusMessage *scan_iface(DBusConnection *conn,
        if (reply == NULL)
                return NULL;
 
+       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+       switch (iface->state) {
+       case CONNMAN_IFACE_STATE_CONNECT:
+       case CONNMAN_IFACE_STATE_CONFIGURE:
+                       return reply;
+       default:
+               break;
+       }
+
        if (driver->scan)
                driver->scan(iface);
 
-       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
-
        return reply;
 }
 
@@ -843,11 +838,7 @@ static DBusMessage *set_network(DBusConnection *conn,
                        dbus_message_unref(signal);
                }
 
-               if (iface->driver->disconnect)
-                       iface->driver->disconnect(iface);
-
-               if (iface->driver->connect)
-                       iface->driver->connect(iface, &iface->network);
+               __connman_iface_connect(iface, &iface->network);
        }
 
        return reply;
@@ -859,21 +850,25 @@ static DBusMessage *select_network(DBusConnection *conn,
        struct connman_iface *iface = data;
        DBusMessage *reply;
        const char *network;
+       gchar *passphrase;
 
        DBG("conn %p", conn);
 
        dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &network,
                                                        DBUS_TYPE_INVALID);
 
+       passphrase = __connman_iface_find_passphrase(iface, network);
+       if (passphrase == NULL)
+               goto done;
+
        g_free(iface->network.identifier);
        iface->network.identifier = g_strdup(network);
+       g_free(iface->network.passphrase);
+       iface->network.passphrase = passphrase;
 
-       if (iface->driver->disconnect)
-               iface->driver->disconnect(iface);
-
-       if (iface->driver->connect)
-               iface->driver->connect(iface, &iface->network);
+       __connman_iface_connect(iface, &iface->network);
 
+done:
        reply = dbus_message_new_method_return(msg);
        if (reply == NULL)
                return NULL;
@@ -895,7 +890,7 @@ static void append_ipv4(DBusMessage *reply, struct connman_iface *iface)
                        DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
                        DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
 
-       str = __connman_ipv4_method2string(iface->ipv4.method);
+       str = __connman_ipv4_method2string(CONNMAN_IPV4_METHOD_DHCP);
        append_entry(&dict, "Method", DBUS_TYPE_STRING, &str);
 
        if (iface->ipv4.address.s_addr != INADDR_ANY) {
@@ -924,6 +919,15 @@ static DBusMessage *get_ipv4(DBusConnection *conn,
 
        DBG("conn %p", conn);
 
+       switch (iface->policy) {
+       case CONNMAN_IFACE_POLICY_OFF:
+       case CONNMAN_IFACE_POLICY_IGNORE:
+               return dbus_message_new_error(msg, CONNMAN_ERROR_INTERFACE
+                                               ".NotAvailable", "");
+       default:
+               break;
+       }
+
        reply = dbus_message_new_method_return(msg);
        if (reply == NULL)
                return NULL;
@@ -943,6 +947,9 @@ static DBusMessage *set_ipv4(DBusConnection *conn,
 
        DBG("conn %p", conn);
 
+       return dbus_message_new_error(msg, CONNMAN_ERROR_INTERFACE
+                                               ".NotImplemented", "");
+
        dbus_message_iter_init(msg, &array);
 
        dbus_message_iter_recurse(&array, &dict);
@@ -1164,6 +1171,8 @@ static int probe_device(LibHalContext *ctx,
 
        iface->driver = driver;
 
+       iface->policy = CONNMAN_IFACE_POLICY_AUTO;
+
        __connman_iface_load(iface);
 
        DBG("iface %p network %s secret %s", iface,
@@ -1196,6 +1205,8 @@ static int probe_device(LibHalContext *ctx,
 
        switch_policy(iface);
 
+       state_changed(iface);
+
        return 0;
 }