timeserver: Simplify timeserver IP address checking
[framework/connectivity/connman.git] / plugins / wifi.c
index d2f6799..3cd2be2 100644 (file)
@@ -222,6 +222,8 @@ static void stop_autoscan(struct connman_device *device)
        autoscan->timeout = 0;
        autoscan->interval = 0;
 
+       connman_device_set_scanning(device, FALSE);
+
        connman_device_unref(device);
 }
 
@@ -236,6 +238,10 @@ static void wifi_remove(struct connman_device *device)
 
        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);
@@ -321,6 +327,73 @@ static int add_scan_param(gchar *hex_ssid, int freq,
        return 0;
 }
 
+static int get_hidden_connections(int max_ssids,
+                               GSupplicantScanParams *scan_data)
+{
+       GKeyFile *keyfile;
+       gchar **services;
+       char *ssid;
+       gchar *str;
+       int i, freq;
+       gboolean value;
+       int num_ssids = 0, add_param_failed = 0;
+
+       services = connman_storage_get_services();
+       for (i = 0; services && services[i]; i++) {
+               if (strncmp(services[i], "wifi_", 5) != 0)
+                       continue;
+
+               keyfile = connman_storage_load_service(services[i]);
+
+               value = g_key_file_get_boolean(keyfile,
+                                       services[i], "Hidden", NULL);
+               if (value == FALSE) {
+                       g_key_file_free(keyfile);
+                       continue;
+               }
+
+               value = g_key_file_get_boolean(keyfile,
+                                       services[i], "Favorite", NULL);
+               if (value == FALSE) {
+                       g_key_file_free(keyfile);
+                       continue;
+               }
+
+               value = g_key_file_get_boolean(keyfile,
+                                       services[i], "AutoConnect", NULL);
+               if (value == FALSE) {
+                       g_key_file_free(keyfile);
+                       continue;
+               }
+
+               ssid = g_key_file_get_string(keyfile,
+                                       services[i], "SSID", NULL);
+
+               freq = g_key_file_get_integer(keyfile, services[i],
+                                       "Frequency", NULL);
+
+               if (add_scan_param(ssid, freq, scan_data, max_ssids) < 0) {
+                       str = g_key_file_get_string(keyfile,
+                                       services[i], "Name", NULL);
+                       DBG("Cannot scan %s (%s)", ssid, str);
+                       g_free(str);
+                       add_param_failed++;
+               }
+
+               num_ssids++;
+
+               g_key_file_free(keyfile);
+       }
+
+       if (add_param_failed > 0)
+               connman_warn("Unable to scan %d out of %d SSIDs (max is %d)",
+                       add_param_failed, num_ssids, max_ssids);
+
+       g_strfreev(services);
+
+       return num_ssids > max_ssids ? max_ssids : num_ssids;
+}
+
 static int throw_wifi_scan(struct connman_device *device,
                        GSupplicantInterfaceCallback callback)
 {
@@ -332,6 +405,9 @@ static int throw_wifi_scan(struct connman_device *device,
        if (wifi->tethering == TRUE)
                return 0;
 
+       if (connman_device_get_scanning(device) == TRUE)
+               return -EALREADY;
+
        connman_device_ref(device);
 
        ret = g_supplicant_interface_scan(wifi->interface, NULL,
@@ -344,13 +420,55 @@ static int throw_wifi_scan(struct connman_device *device,
        return ret;
 }
 
+static void hidden_scan_callback(int result,
+                       GSupplicantInterface *interface, void *user_data)
+{
+       struct connman_device *device = user_data;
+
+       DBG("result %d", result);
+
+       connman_device_set_scanning(device, FALSE);
+       connman_device_unref(device);
+}
+
 static void autoscan_scan_callback(int result,
                        GSupplicantInterface *interface, void *user_data)
 {
        struct connman_device *device = user_data;
+       struct wifi_data *wifi = connman_device_get_data(device);
+       int driver_max_ssids;
 
-       DBG("");
+       DBG("result %d", result);
 
+       /*
+        * Scan hidden networks so that we can autoconnect to them.
+        */
+       driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
+                                                       wifi->interface);
+       DBG("max ssids %d", driver_max_ssids);
+
+       if (driver_max_ssids > 0) {
+               GSupplicantScanParams *scan_params;
+               int ret;
+
+               scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
+               if (scan_params == NULL)
+                       goto out;
+
+               if (get_hidden_connections(driver_max_ssids,
+                                               scan_params) > 0) {
+                       ret = g_supplicant_interface_scan(wifi->interface,
+                                                       scan_params,
+                                                       hidden_scan_callback,
+                                                       device);
+                       if (ret == 0)
+                               return;
+               }
+
+               g_supplicant_free_scan_params(scan_params);
+       }
+
+out:
        connman_device_set_scanning(device, FALSE);
        connman_device_unref(device);
 }
@@ -707,6 +825,11 @@ 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;
+
        driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
                                                        wifi->interface);
        DBG("max ssids %d", driver_max_ssids);
@@ -723,8 +846,6 @@ static int wifi_scan_fast(struct connman_device *device)
                return wifi_scan(device);
        }
 
-       stop_autoscan(device);
-
        connman_device_ref(device);
        ret = g_supplicant_interface_scan(wifi->interface, scan_params,
                                                scan_callback, device);
@@ -738,6 +859,10 @@ static int wifi_scan_fast(struct connman_device *device)
        return ret;
 }
 
+/*
+ * This func is only used when connecting to this specific AP first time.
+ * It is not used when system autoconnects to hidden AP.
+ */
 static int wifi_scan_hidden(struct connman_device *device,
                const char *ssid, unsigned int ssid_len,
                const char *identity, const char* passphrase)
@@ -756,6 +881,11 @@ 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;
+
        scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
        if (scan_params == NULL)
                return -ENOMEM;
@@ -783,8 +913,6 @@ static int wifi_scan_hidden(struct connman_device *device,
        hidden->passphrase = g_strdup(passphrase);
        wifi->hidden = hidden;
 
-       stop_autoscan(device);
-
        connman_device_ref(device);
        ret = g_supplicant_interface_scan(wifi->interface, scan_params,
                        scan_callback, device);
@@ -1266,6 +1394,7 @@ static void interface_state(GSupplicantInterface *interface)
 
                connman_network_set_connected(network, FALSE);
                connman_network_set_associating(network, FALSE);
+               wifi->disconnecting = FALSE;
 
                start_autoscan(device);
 
@@ -1437,7 +1566,7 @@ static void network_added(GSupplicantNetwork *supplicant_network)
        if (ssid != NULL)
                connman_network_set_group(network, group);
 
-       if (wifi->hidden != NULL) {
+       if (wifi->hidden != NULL && ssid != NULL) {
                if (wifi->hidden->ssid_len == ssid_len &&
                                memcmp(wifi->hidden->ssid, ssid,
                                                ssid_len) == 0) {