wifi: Use common scan callback handling
[framework/connectivity/connman.git] / plugins / wifi.c
index 3f9afad..3e48999 100644 (file)
@@ -108,6 +108,8 @@ struct wifi_data {
 
 static GList *iface_list = NULL;
 
+static void start_autoscan(struct connman_device *device);
+
 static void handle_tethering(struct wifi_data *wifi)
 {
        if (wifi->tethering == FALSE)
@@ -203,7 +205,7 @@ static void remove_networks(struct connman_device *device,
        wifi->networks = NULL;
 }
 
-static void stop_autoscan(struct connman_device *device)
+static void reset_autoscan(struct connman_device *device)
 {
        struct wifi_data *wifi = connman_device_get_data(device);
        struct autoscan_params *autoscan;
@@ -223,11 +225,16 @@ static void stop_autoscan(struct connman_device *device)
        autoscan->timeout = 0;
        autoscan->interval = 0;
 
-       connman_device_set_scanning(device, FALSE);
-
        connman_device_unref(device);
 }
 
+static void stop_autoscan(struct connman_device *device)
+{
+       reset_autoscan(device);
+
+       connman_device_set_scanning(device, FALSE);
+}
+
 static void wifi_remove(struct connman_device *device)
 {
        struct wifi_data *wifi = connman_device_get_data(device);
@@ -237,12 +244,6 @@ static void wifi_remove(struct connman_device *device)
        if (wifi == NULL)
                return;
 
-       stop_autoscan(device);
-
-       /* In case of a user scan, device is still referenced */
-       if (connman_device_get_scanning(device) == TRUE)
-               connman_device_unref(wifi->device);
-
        iface_list = g_list_remove(iface_list, wifi);
 
        remove_networks(device, wifi);
@@ -421,18 +422,39 @@ static int throw_wifi_scan(struct connman_device *device,
        return ret;
 }
 
-static void hidden_scan_callback(int result,
-                       GSupplicantInterface *interface, void *user_data)
+static void hidden_free(struct hidden_params *hidden)
+{
+       if (hidden == NULL)
+               return;
+
+       g_free(hidden->identity);
+       g_free(hidden->passphrase);
+       g_free(hidden);
+}
+
+static void scan_callback(int result, GSupplicantInterface *interface,
+                                               void *user_data)
 {
        struct connman_device *device = user_data;
+       struct wifi_data *wifi = connman_device_get_data(device);
 
        DBG("result %d", result);
 
+       if (wifi != NULL && wifi->hidden != NULL) {
+               connman_network_clear_hidden(wifi->hidden->user_data);
+               hidden_free(wifi->hidden);
+               wifi->hidden = NULL;
+       }
+
+       if (result < 0)
+               connman_device_reset_scanning(device);
+
        connman_device_set_scanning(device, FALSE);
+       start_autoscan(device);
        connman_device_unref(device);
 }
 
-static void autoscan_scan_callback(int result,
+static void scan_callback_hidden(int result,
                        GSupplicantInterface *interface, void *user_data)
 {
        struct connman_device *device = user_data;
@@ -460,7 +482,7 @@ static void autoscan_scan_callback(int result,
                                                scan_params) > 0) {
                        ret = g_supplicant_interface_scan(wifi->interface,
                                                        scan_params,
-                                                       hidden_scan_callback,
+                                                       scan_callback,
                                                        device);
                        if (ret == 0)
                                return;
@@ -470,8 +492,7 @@ static void autoscan_scan_callback(int result,
        }
 
 out:
-       connman_device_set_scanning(device, FALSE);
-       connman_device_unref(device);
+       scan_callback(result, interface, user_data);
 }
 
 static gboolean autoscan_timeout(gpointer data)
@@ -492,7 +513,7 @@ static gboolean autoscan_timeout(gpointer data)
        if (autoscan->interval >= autoscan->limit)
                interval = autoscan->limit;
 
-       throw_wifi_scan(wifi->device, autoscan_scan_callback);
+       throw_wifi_scan(wifi->device, scan_callback_hidden);
 
 set_interval:
        DBG("interval %d", interval);
@@ -637,6 +658,14 @@ static int wifi_disable(struct connman_device *device)
        if (wifi->pending_network != NULL)
                wifi->pending_network = NULL;
 
+       stop_autoscan(device);
+
+       /* In case of a user scan, device is still referenced */
+       if (connman_device_get_scanning(device) == TRUE) {
+               connman_device_set_scanning(device, FALSE);
+               connman_device_unref(wifi->device);
+       }
+
        remove_networks(device, wifi);
 
        ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
@@ -646,39 +675,6 @@ static int wifi_disable(struct connman_device *device)
        return -EINPROGRESS;
 }
 
-static void hidden_free(struct hidden_params *hidden)
-{
-       if (hidden == NULL)
-               return;
-
-       g_free(hidden->identity);
-       g_free(hidden->passphrase);
-       g_free(hidden);
-}
-
-static void scan_callback(int result, GSupplicantInterface *interface,
-                                               void *user_data)
-{
-       struct connman_device *device = user_data;
-       struct wifi_data *wifi = connman_device_get_data(device);
-
-       DBG("result %d", result);
-
-       if (wifi != NULL && wifi->hidden != NULL) {
-               connman_network_clear_hidden(wifi->hidden->user_data);
-               hidden_free(wifi->hidden);
-               wifi->hidden = NULL;
-       }
-
-       if (result < 0)
-               connman_device_reset_scanning(device);
-
-       connman_device_set_scanning(device, FALSE);
-       connman_device_unref(device);
-
-       start_autoscan(device);
-}
-
 struct last_connected {
        GTimeVal modified;
        gchar *ssid;
@@ -810,9 +806,9 @@ static int get_latest_connections(int max_ssids,
 
 static int wifi_scan(struct connman_device *device)
 {
-       stop_autoscan(device);
+       reset_autoscan(device);
 
-       return throw_wifi_scan(device, scan_callback);
+       return throw_wifi_scan(device, scan_callback_hidden);
 }
 
 static int wifi_scan_fast(struct connman_device *device)
@@ -827,8 +823,6 @@ static int wifi_scan_fast(struct connman_device *device)
        if (wifi->tethering == TRUE)
                return 0;
 
-       stop_autoscan(device);
-
        if (connman_device_get_scanning(device) == TRUE)
                return -EALREADY;
 
@@ -849,6 +843,8 @@ static int wifi_scan_fast(struct connman_device *device)
        }
 
        connman_device_ref(device);
+       reset_autoscan(device);
+
        ret = g_supplicant_interface_scan(wifi->interface, scan_params,
                                                scan_callback, device);
        if (ret == 0)
@@ -884,8 +880,6 @@ static int wifi_scan_hidden(struct connman_device *device,
        if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
                return -EINVAL;
 
-       stop_autoscan(device);
-
        if (connman_device_get_scanning(device) == TRUE)
                return -EALREADY;
 
@@ -918,6 +912,9 @@ static int wifi_scan_hidden(struct connman_device *device,
        wifi->hidden = hidden;
 
        connman_device_ref(device);
+
+       reset_autoscan(device);
+
        ret = g_supplicant_interface_scan(wifi->interface, scan_params,
                        scan_callback, device);
        if (ret == 0)
@@ -1463,7 +1460,7 @@ static void interface_removed(GSupplicantInterface *interface)
                return;
 
        if (wifi == NULL || wifi->device == NULL) {
-               connman_error("Wrong wifi pointer");
+               DBG("wifi interface already removed");
                return;
        }