Add option for interface limitation
[connman] / src / iface.c
index 2c7fdfe..18c44f6 100644 (file)
@@ -47,6 +47,8 @@
 
 static DBusConnection *connection = NULL;
 
+static gchar *ifname_filter = NULL;
+
 static GSList *drivers = NULL;
 
 int connman_iface_register(struct connman_iface_driver *driver)
@@ -95,56 +97,308 @@ void __connman_iface_list(DBusMessageIter *iter)
        }
 }
 
-int connman_iface_update(struct connman_iface *iface,
-                                       enum connman_iface_state state)
+static void append_entry(DBusMessageIter *dict,
+                               const char *key, int type, void *val)
 {
-       const char *str;
+       DBusMessageIter entry, value;
+       const char *signature;
+
+       dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
+                                                               NULL, &entry);
 
-       iface->state = state;
+       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
 
-       str = __connman_iface_state2string(iface->state);
+       switch (type) {
+       case DBUS_TYPE_STRING:
+               signature = DBUS_TYPE_STRING_AS_STRING;
+               break;
+       case DBUS_TYPE_UINT16:
+               signature = DBUS_TYPE_UINT16_AS_STRING;
+               break;
+       default:
+               signature = DBUS_TYPE_VARIANT_AS_STRING;
+               break;
+       }
+
+       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
+                                                       signature, &value);
+       dbus_message_iter_append_basic(&value, type, val);
+       dbus_message_iter_close_container(&entry, &value);
+
+       dbus_message_iter_close_container(dict, &entry);
+}
+
+static gboolean scan_timeout(gpointer user_data)
+{
+       struct connman_iface *iface = user_data;
+
+       switch (iface->state) {
+       case CONNMAN_IFACE_STATE_SCANNING:
+       case CONNMAN_IFACE_STATE_READY:
+               if (iface->driver->scan)
+                       iface->driver->scan(iface);
+               return TRUE;
+       default:
+               break;
+       }
+
+       return FALSE;
+}
+
+static void state_changed(struct connman_iface *iface)
+{
+       const char *str = __connman_iface_state2string(iface->state);
+       enum connman_iface_state state = iface->state;
+
+       DBG("iface %p state %s", iface, str);
 
        g_dbus_emit_signal(connection, iface->path,
                                CONNMAN_IFACE_INTERFACE, "StateChanged",
                                DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
 
-       switch (state) {
+       switch (iface->state) {
        case CONNMAN_IFACE_STATE_OFF:
                __connman_dhcp_release(iface);
                break;
 
        case CONNMAN_IFACE_STATE_ENABLED:
-               if (iface->type == CONNMAN_IFACE_TYPE_80211) {
-                       if (iface->driver->connect)
-                               iface->driver->connect(iface, NULL);
-               }
+               __connman_dhcp_release(iface);
+               connman_iface_clear_ipv4(iface);
+               if (iface->driver->disconnect)
+                       iface->driver->disconnect(iface);
+               if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING)
+                       state = CONNMAN_IFACE_STATE_SCANNING;
+               break;
+
+       case CONNMAN_IFACE_STATE_SCANNING:
+               if (iface->driver->scan)
+                       iface->driver->scan(iface);
+               g_timeout_add(8000, scan_timeout, iface);
                break;
 
        case CONNMAN_IFACE_STATE_CARRIER:
+               if (iface->policy == CONNMAN_IFACE_POLICY_AUTO)
+                       state = CONNMAN_IFACE_STATE_CONFIGURE;
+               break;
+
+       case CONNMAN_IFACE_STATE_CONFIGURE:
                __connman_dhcp_request(iface);
                break;
 
        case CONNMAN_IFACE_STATE_SHUTDOWN:
                __connman_dhcp_release(iface);
-               if (iface->driver->shutdown)
-                       iface->driver->shutdown(iface);
+               if (iface->driver->disconnect)
+                       iface->driver->disconnect(iface);
+               if (iface->policy != CONNMAN_IFACE_POLICY_AUTO)
+                       state = CONNMAN_IFACE_STATE_OFF;
+               break;
+
+       case CONNMAN_IFACE_STATE_READY:
+               if (iface->flags & CONNMAN_IFACE_FLAG_SCANNING)
+                       g_timeout_add(20000, scan_timeout, iface);
                break;
 
        default:
                break;
        }
 
-       return 0;
+       if (iface->state != state) {
+               iface->state = state;
+               state_changed(iface);
+       }
+}
+
+static void switch_policy(struct connman_iface *iface)
+{
+       DBG("iface %p", iface);
+
+       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);
+               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);
+               break;
+
+       default:
+               break;
+       }
 }
 
-void connman_iface_indicate_carrier(struct connman_iface *iface, int carrier)
+void connman_iface_indicate_enabled(struct connman_iface *iface)
 {
-       DBG("iface %p carrier %d", iface, carrier);
+       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;
+       default:
+               break;
+       }
+}
+
+void connman_iface_indicate_disabled(struct connman_iface *iface)
+{
+       DBG("iface %p state %d", iface, iface->state);
+
+       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
+               iface->state = CONNMAN_IFACE_STATE_SHUTDOWN;
+
+       state_changed(iface);
+}
+
+void connman_iface_indicate_connected(struct connman_iface *iface)
+{
+       DBG("iface %p state %d", iface, iface->state);
+
+       switch (iface->state) {
+       case CONNMAN_IFACE_STATE_CONNECT:
+               iface->state = CONNMAN_IFACE_STATE_CONNECTED;
+               state_changed(iface);
+               break;
+       default:
+               break;
+       }
+}
+
+void connman_iface_indicate_carrier_on(struct connman_iface *iface)
+{
+       DBG("iface %p state %d", iface, iface->state);
+
+       switch (iface->state) {
+       case CONNMAN_IFACE_STATE_ENABLED:
+       case CONNMAN_IFACE_STATE_CONNECT:
+       case CONNMAN_IFACE_STATE_CONNECTED:
+               iface->state = CONNMAN_IFACE_STATE_CARRIER;
+               state_changed(iface);
+               break;
+       default:
+               break;
+       }
+}
+
+void connman_iface_indicate_carrier_off(struct connman_iface *iface)
+{
+       DBG("iface %p state %d", iface, iface->state);
+
+       switch (iface->state) {
+       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);
+                       iface->state = CONNMAN_IFACE_STATE_SCANNING;
+               } else
+#endif
+                       iface->state = CONNMAN_IFACE_STATE_ENABLED;
+               state_changed(iface);
+               break;
+       default:
+               break;
+       }
+}
+
+void connman_iface_indicate_configured(struct connman_iface *iface)
+{
+       DBG("iface %p state %d", iface, iface->state);
+
+       switch (iface->state) {
+       case CONNMAN_IFACE_STATE_CONFIGURE:
+               iface->state = CONNMAN_IFACE_STATE_READY;
+               state_changed(iface);
+               break;
+       default:
+               break;
+       }
+}
+
+static void append_station(DBusMessage *reply, const char *name,
+                                               int signal, int security)
+{
+       DBusMessageIter array, dict;
+       const char *wpa = "WPA";
+
+       dbus_message_iter_init_append(reply, &array);
+
+       dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
+                       DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+                       DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
+                       DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
+
+       append_entry(&dict, "ESSID", DBUS_TYPE_STRING, &name);
+       append_entry(&dict, "Signal", DBUS_TYPE_UINT16, &signal);
+
+       if (security > 0)
+               append_entry(&dict, "Security", DBUS_TYPE_STRING, &wpa);
+
+       dbus_message_iter_close_container(&array, &dict);
+}
+
+void connman_iface_indicate_station(struct connman_iface *iface,
+                               const char *name, int strength, int security)
+{
+       DBusMessage *signal;
+
+       DBG("iface %p security %d name %s", iface, security, name);
+
+       if (name == NULL || strlen(name) == 0)
+               return;
+
+       signal = dbus_message_new_signal(iface->path,
+                               CONNMAN_IFACE_INTERFACE, "NetworkFound");
+       if (signal == NULL)
+               return;
+
+       append_station(signal, name, strength, security);
+
+       dbus_connection_send(connection, signal, NULL);
+       dbus_message_unref(signal);
+
+       if (g_str_equal(name, iface->network.essid) == TRUE &&
+                       iface->state == CONNMAN_IFACE_STATE_SCANNING) {
+               if (iface->driver->set_network)
+                       iface->driver->set_network(iface, name);
+               if (iface->driver->set_passphrase)
+                       iface->driver->set_passphrase(iface,
+                                               iface->network.psk);
+
+               if (iface->driver->connect) {
+                       iface->driver->connect(iface, NULL);
+                       iface->state = CONNMAN_IFACE_STATE_CONNECT;
+                       state_changed(iface);
+               }
+       }
 }
 
 int connman_iface_get_ipv4(struct connman_iface *iface,
                                                struct connman_ipv4 *ipv4)
 {
+#if 0
        struct {
                struct nlmsghdr hdr;
                struct rtgenmsg msg;
@@ -164,6 +418,7 @@ int connman_iface_get_ipv4(struct connman_iface *iface,
        req.msg.rtgen_family = AF_INET;
 
        __connman_rtnl_send(&req, sizeof(req));
+#endif
 
        return 0;
 }
@@ -252,7 +507,7 @@ int connman_iface_set_ipv4(struct connman_iface *iface,
 
        DBG("%s", cmd);
 
-       system(cmd);
+       err = system(cmd);
 
        return 0;
 }
@@ -301,7 +556,7 @@ int connman_iface_clear_ipv4(struct connman_iface *iface)
 
        DBG("%s", cmd);
 
-       system(cmd);
+       err = system(cmd);
 
        return 0;
 }
@@ -319,45 +574,15 @@ static DBusMessage *scan_iface(DBusConnection *conn,
        if (reply == NULL)
                return NULL;
 
-       if (driver->scan)
-               driver->scan(iface);
+       if (driver->scan) {
+               //driver->scan(iface);
+       }
 
        dbus_message_append_args(reply, DBUS_TYPE_INVALID);
 
        return reply;
 }
 
-static void append_entry(DBusMessageIter *dict,
-                               const char *key, int type, void *val)
-{
-       DBusMessageIter entry, value;
-       const char *signature;
-
-       dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
-                                                               NULL, &entry);
-
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
-
-       switch (type) {
-       case DBUS_TYPE_STRING:
-               signature = DBUS_TYPE_STRING_AS_STRING;
-               break;
-       case DBUS_TYPE_UINT16:
-               signature = DBUS_TYPE_UINT16_AS_STRING;
-               break;
-       default:
-               signature = DBUS_TYPE_VARIANT_AS_STRING;
-               break;
-       }
-
-       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
-                                                       signature, &value);
-       dbus_message_iter_append_basic(&value, type, val);
-       dbus_message_iter_close_container(&entry, &value);
-
-       dbus_message_iter_close_container(dict, &entry);
-}
-
 static DBusMessage *get_properties(DBusConnection *conn,
                                        DBusMessage *msg, void *data)
 {
@@ -501,13 +726,7 @@ static DBusMessage *set_policy(DBusConnection *conn,
                iface->policy = new_policy;
                __connman_iface_store(iface);
 
-               if (new_policy == CONNMAN_IFACE_POLICY_AUTO) {
-                       if (iface->driver->activate)
-                               iface->driver->activate(iface);
-               } else
-                       connman_iface_update(iface,
-                                       CONNMAN_IFACE_STATE_SHUTDOWN);
-
+               switch_policy(iface);
                policy = __connman_iface_policy2string(new_policy);
 
                g_dbus_emit_signal(conn, iface->path, CONNMAN_IFACE_INTERFACE,
@@ -602,7 +821,7 @@ static DBusMessage *set_network(DBusConnection *conn,
                if (g_strcasecmp(key, "PSK") == 0) {
                        g_free(iface->network.psk);
                        iface->network.psk = g_strdup(val);
-                       if (iface->driver->set_network)
+                       if (iface->driver->set_passphrase)
                                iface->driver->set_passphrase(iface, val);
                        changed = TRUE;
                }
@@ -631,6 +850,33 @@ static DBusMessage *set_network(DBusConnection *conn,
        return reply;
 }
 
+static DBusMessage *select_network(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       struct connman_iface *iface = data;
+       DBusMessage *reply;
+       const char *network;
+
+       DBG("conn %p", conn);
+
+       dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &network,
+                                                       DBUS_TYPE_INVALID);
+
+       g_free(iface->network.essid);
+       iface->network.essid = g_strdup(network);
+
+       if (iface->driver->set_network)
+               iface->driver->set_network(iface, network);
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_append_args(reply, DBUS_TYPE_INVALID);
+
+       return reply;
+}
+
 static void append_ipv4(DBusMessage *reply, struct connman_iface *iface)
 {
        DBusMessageIter array, dict;
@@ -776,6 +1022,7 @@ static GDBusMethodTable iface_methods[] = {
        { "SetPolicy",     "s",     "",      set_policy     },
        { "GetNetwork",    "",      "a{sv}", get_network    },
        { "SetNetwork",    "a{sv}", "",      set_network    },
+       { "SelectNetwork", "s",     "",      select_network },
        { "GetIPv4",       "",      "a{sv}", get_ipv4       },
        { "SetIPv4",       "a{sv}", "",      set_ipv4       },
        { },
@@ -785,6 +1032,7 @@ static GDBusSignalTable iface_signals[] = {
        { "StateChanged",   "s"     },
        { "SignalChanged",  "q"     },
        { "PolicyChanged",  "s"     },
+       { "NetworkFound",   "a{sv}" },
        { "NetworkChanged", "a{sv}" },
        { "IPv4Changed",    "a{sv}" },
        { },
@@ -849,7 +1097,7 @@ static int probe_device(LibHalContext *ctx,
 {
        DBusConnection *conn;
        struct connman_iface *iface;
-       char *temp, *sysfs;
+       char *temp, *sysfs, *ifname;
        int err;
 
        DBG("ctx %p driver %p udi %s", ctx, driver, udi);
@@ -878,10 +1126,20 @@ static int probe_device(LibHalContext *ctx,
 
        iface->index = -1;
 
-       if (g_str_has_prefix(driver->capability, "net") == TRUE)
+       if (g_str_has_prefix(driver->capability, "net") == TRUE) {
                iface->index = libhal_device_get_property_int(ctx, udi,
                                                "net.linux.ifindex", NULL);
 
+               ifname = libhal_device_get_property_string(ctx, udi,
+                                               "net.interface", NULL);
+               if (ifname != NULL && ifname_filter != NULL &&
+                                               *ifname_filter != '\0' &&
+                               g_str_equal(ifname, ifname_filter) == FALSE) {
+                       device_free(iface);
+                       return -1;
+               }
+       }
+
        iface->type = CONNMAN_IFACE_TYPE_UNKNOWN;
        iface->flags = 0;
        iface->state = CONNMAN_IFACE_STATE_UNKNOWN;
@@ -901,6 +1159,9 @@ static int probe_device(LibHalContext *ctx,
 
        __connman_iface_load(iface);
 
+       DBG("iface %p network %s secret %s", iface,
+                               iface->network.essid, iface->network.psk);
+
        conn = libhal_ctx_get_dbus_connection(ctx);
 
        g_dbus_register_object(conn, iface->path, iface, device_free);
@@ -908,10 +1169,7 @@ static int probe_device(LibHalContext *ctx,
        interfaces = g_slist_append(interfaces, iface);
 
        if (iface->flags & CONNMAN_IFACE_FLAG_IPV4) {
-               if (driver->get_ipv4)
-                       driver->get_ipv4(iface, &iface->ipv4);
-               else
-                       connman_iface_get_ipv4(iface, &iface->ipv4);
+               connman_iface_get_ipv4(iface, &iface->ipv4);
 
                DBG("address %s", inet_ntoa(iface->ipv4.address));
        }
@@ -928,10 +1186,7 @@ static int probe_device(LibHalContext *ctx,
                                        DBUS_TYPE_OBJECT_PATH, &iface->path,
                                        DBUS_TYPE_INVALID);
 
-       if (iface->policy == CONNMAN_IFACE_POLICY_AUTO) {
-               if (driver->activate)
-                       driver->activate(iface);
-       }
+       switch_policy(iface);
 
        return 0;
 }
@@ -1097,7 +1352,7 @@ static void hal_cleanup(void *data)
 
 static guint hal_watch = 0;
 
-int __connman_iface_init(DBusConnection *conn)
+int __connman_iface_init(DBusConnection *conn, const char *interface)
 {
        DBG("conn %p", conn);
 
@@ -1105,6 +1360,9 @@ int __connman_iface_init(DBusConnection *conn)
        if (connection == NULL)
                return -1;
 
+       if (interface != NULL)
+               ifname_filter = g_strdup(interface);
+
        hal_init(connection);
 
        hal_watch = g_dbus_add_watch(connection, "org.freedesktop.Hal",
@@ -1121,5 +1379,7 @@ void __connman_iface_cleanup(void)
 
        hal_cleanup(connection);
 
+       g_free(ifname_filter);
+
        dbus_connection_unref(connection);
 }