Home | History | Annotate | Line # | Download | only in wpa_supplicant
bgscan_simple.c revision 1.1.1.2.4.1
      1          1.1  christos /*
      2          1.1  christos  * WPA Supplicant - background scan and roaming module: simple
      3          1.1  christos  * Copyright (c) 2009-2010, Jouni Malinen <j (at) w1.fi>
      4          1.1  christos  *
      5  1.1.1.2.4.1       snj  * This software may be distributed under the terms of the BSD license.
      6  1.1.1.2.4.1       snj  * See README for more details.
      7          1.1  christos  */
      8          1.1  christos 
      9          1.1  christos #include "includes.h"
     10          1.1  christos 
     11          1.1  christos #include "common.h"
     12          1.1  christos #include "eloop.h"
     13          1.1  christos #include "drivers/driver.h"
     14          1.1  christos #include "config_ssid.h"
     15          1.1  christos #include "wpa_supplicant_i.h"
     16          1.1  christos #include "driver_i.h"
     17          1.1  christos #include "scan.h"
     18          1.1  christos #include "bgscan.h"
     19          1.1  christos 
     20          1.1  christos struct bgscan_simple_data {
     21          1.1  christos 	struct wpa_supplicant *wpa_s;
     22          1.1  christos 	const struct wpa_ssid *ssid;
     23          1.1  christos 	int scan_interval;
     24          1.1  christos 	int signal_threshold;
     25  1.1.1.2.4.1       snj 	int short_scan_count; /* counter for scans using short scan interval */
     26  1.1.1.2.4.1       snj 	int max_short_scans; /* maximum times we short-scan before back-off */
     27          1.1  christos 	int short_interval; /* use if signal < threshold */
     28          1.1  christos 	int long_interval; /* use if signal > threshold */
     29  1.1.1.2.4.1       snj 	struct os_reltime last_bgscan;
     30          1.1  christos };
     31          1.1  christos 
     32          1.1  christos 
     33          1.1  christos static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx)
     34          1.1  christos {
     35          1.1  christos 	struct bgscan_simple_data *data = eloop_ctx;
     36          1.1  christos 	struct wpa_supplicant *wpa_s = data->wpa_s;
     37          1.1  christos 	struct wpa_driver_scan_params params;
     38          1.1  christos 
     39          1.1  christos 	os_memset(&params, 0, sizeof(params));
     40          1.1  christos 	params.num_ssids = 1;
     41          1.1  christos 	params.ssids[0].ssid = data->ssid->ssid;
     42          1.1  christos 	params.ssids[0].ssid_len = data->ssid->ssid_len;
     43          1.1  christos 	params.freqs = data->ssid->scan_freq;
     44          1.1  christos 
     45          1.1  christos 	/*
     46          1.1  christos 	 * A more advanced bgscan module would learn about most like channels
     47          1.1  christos 	 * over time and request scans only for some channels (probing others
     48          1.1  christos 	 * every now and then) to reduce effect on the data connection.
     49          1.1  christos 	 */
     50          1.1  christos 
     51          1.1  christos 	wpa_printf(MSG_DEBUG, "bgscan simple: Request a background scan");
     52          1.1  christos 	if (wpa_supplicant_trigger_scan(wpa_s, &params)) {
     53          1.1  christos 		wpa_printf(MSG_DEBUG, "bgscan simple: Failed to trigger scan");
     54          1.1  christos 		eloop_register_timeout(data->scan_interval, 0,
     55          1.1  christos 				       bgscan_simple_timeout, data, NULL);
     56  1.1.1.2.4.1       snj 	} else {
     57  1.1.1.2.4.1       snj 		if (data->scan_interval == data->short_interval) {
     58  1.1.1.2.4.1       snj 			data->short_scan_count++;
     59  1.1.1.2.4.1       snj 			/*
     60  1.1.1.2.4.1       snj 			 * Spend at most the duration of a long scan interval
     61  1.1.1.2.4.1       snj 			 * scanning at the short scan interval. After that,
     62  1.1.1.2.4.1       snj 			 * revert to the long scan interval.
     63  1.1.1.2.4.1       snj 			 */
     64  1.1.1.2.4.1       snj 			if (data->short_scan_count > data->max_short_scans) {
     65  1.1.1.2.4.1       snj 				data->scan_interval = data->long_interval;
     66  1.1.1.2.4.1       snj 				wpa_printf(MSG_DEBUG, "bgscan simple: Backing "
     67  1.1.1.2.4.1       snj 					   "off to long scan interval");
     68  1.1.1.2.4.1       snj 			}
     69  1.1.1.2.4.1       snj 		} else if (data->short_scan_count > 0) {
     70  1.1.1.2.4.1       snj 			/*
     71  1.1.1.2.4.1       snj 			 * If we lasted a long scan interval without any
     72  1.1.1.2.4.1       snj 			 * CQM triggers, decrease the short-scan count,
     73  1.1.1.2.4.1       snj 			 * which allows 1 more short-scan interval to
     74  1.1.1.2.4.1       snj 			 * occur in the future when CQM triggers.
     75  1.1.1.2.4.1       snj 			 */
     76  1.1.1.2.4.1       snj 			data->short_scan_count--;
     77  1.1.1.2.4.1       snj 		}
     78  1.1.1.2.4.1       snj 		os_get_reltime(&data->last_bgscan);
     79  1.1.1.2.4.1       snj 	}
     80          1.1  christos }
     81          1.1  christos 
     82          1.1  christos 
     83          1.1  christos static int bgscan_simple_get_params(struct bgscan_simple_data *data,
     84          1.1  christos 				    const char *params)
     85          1.1  christos {
     86          1.1  christos 	const char *pos;
     87          1.1  christos 
     88          1.1  christos 	if (params == NULL)
     89          1.1  christos 		return 0;
     90          1.1  christos 
     91          1.1  christos 	data->short_interval = atoi(params);
     92          1.1  christos 
     93          1.1  christos 	pos = os_strchr(params, ':');
     94          1.1  christos 	if (pos == NULL)
     95          1.1  christos 		return 0;
     96          1.1  christos 	pos++;
     97          1.1  christos 	data->signal_threshold = atoi(pos);
     98          1.1  christos 	pos = os_strchr(pos, ':');
     99          1.1  christos 	if (pos == NULL) {
    100          1.1  christos 		wpa_printf(MSG_ERROR, "bgscan simple: Missing scan interval "
    101          1.1  christos 			   "for high signal");
    102          1.1  christos 		return -1;
    103          1.1  christos 	}
    104          1.1  christos 	pos++;
    105          1.1  christos 	data->long_interval = atoi(pos);
    106          1.1  christos 
    107          1.1  christos 	return 0;
    108          1.1  christos }
    109          1.1  christos 
    110          1.1  christos 
    111          1.1  christos static void * bgscan_simple_init(struct wpa_supplicant *wpa_s,
    112          1.1  christos 				 const char *params,
    113          1.1  christos 				 const struct wpa_ssid *ssid)
    114          1.1  christos {
    115          1.1  christos 	struct bgscan_simple_data *data;
    116          1.1  christos 
    117          1.1  christos 	data = os_zalloc(sizeof(*data));
    118          1.1  christos 	if (data == NULL)
    119          1.1  christos 		return NULL;
    120          1.1  christos 	data->wpa_s = wpa_s;
    121          1.1  christos 	data->ssid = ssid;
    122          1.1  christos 	if (bgscan_simple_get_params(data, params) < 0) {
    123          1.1  christos 		os_free(data);
    124          1.1  christos 		return NULL;
    125          1.1  christos 	}
    126          1.1  christos 	if (data->short_interval <= 0)
    127          1.1  christos 		data->short_interval = 30;
    128          1.1  christos 	if (data->long_interval <= 0)
    129          1.1  christos 		data->long_interval = 30;
    130          1.1  christos 
    131          1.1  christos 	wpa_printf(MSG_DEBUG, "bgscan simple: Signal strength threshold %d  "
    132          1.1  christos 		   "Short bgscan interval %d  Long bgscan interval %d",
    133          1.1  christos 		   data->signal_threshold, data->short_interval,
    134          1.1  christos 		   data->long_interval);
    135          1.1  christos 
    136          1.1  christos 	if (data->signal_threshold &&
    137          1.1  christos 	    wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
    138          1.1  christos 		wpa_printf(MSG_ERROR, "bgscan simple: Failed to enable "
    139          1.1  christos 			   "signal strength monitoring");
    140          1.1  christos 	}
    141          1.1  christos 
    142          1.1  christos 	data->scan_interval = data->short_interval;
    143  1.1.1.2.4.1       snj 	data->max_short_scans = data->long_interval / data->short_interval + 1;
    144  1.1.1.2.4.1       snj 	if (data->signal_threshold) {
    145  1.1.1.2.4.1       snj 		/* Poll for signal info to set initial scan interval */
    146  1.1.1.2.4.1       snj 		struct wpa_signal_info siginfo;
    147  1.1.1.2.4.1       snj 		if (wpa_drv_signal_poll(wpa_s, &siginfo) == 0 &&
    148  1.1.1.2.4.1       snj 		    siginfo.current_signal >= data->signal_threshold)
    149  1.1.1.2.4.1       snj 			data->scan_interval = data->long_interval;
    150  1.1.1.2.4.1       snj 	}
    151  1.1.1.2.4.1       snj 	wpa_printf(MSG_DEBUG, "bgscan simple: Init scan interval: %d",
    152  1.1.1.2.4.1       snj 		   data->scan_interval);
    153          1.1  christos 	eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
    154          1.1  christos 			       data, NULL);
    155      1.1.1.2  christos 
    156      1.1.1.2  christos 	/*
    157      1.1.1.2  christos 	 * This function is called immediately after an association, so it is
    158      1.1.1.2  christos 	 * reasonable to assume that a scan was completed recently. This makes
    159      1.1.1.2  christos 	 * us skip an immediate new scan in cases where the current signal
    160      1.1.1.2  christos 	 * level is below the bgscan threshold.
    161      1.1.1.2  christos 	 */
    162  1.1.1.2.4.1       snj 	os_get_reltime(&data->last_bgscan);
    163      1.1.1.2  christos 
    164          1.1  christos 	return data;
    165          1.1  christos }
    166          1.1  christos 
    167          1.1  christos 
    168          1.1  christos static void bgscan_simple_deinit(void *priv)
    169          1.1  christos {
    170          1.1  christos 	struct bgscan_simple_data *data = priv;
    171          1.1  christos 	eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
    172          1.1  christos 	if (data->signal_threshold)
    173          1.1  christos 		wpa_drv_signal_monitor(data->wpa_s, 0, 0);
    174          1.1  christos 	os_free(data);
    175          1.1  christos }
    176          1.1  christos 
    177          1.1  christos 
    178  1.1.1.2.4.1       snj static int bgscan_simple_notify_scan(void *priv,
    179  1.1.1.2.4.1       snj 				     struct wpa_scan_results *scan_res)
    180          1.1  christos {
    181          1.1  christos 	struct bgscan_simple_data *data = priv;
    182          1.1  christos 
    183          1.1  christos 	wpa_printf(MSG_DEBUG, "bgscan simple: scan result notification");
    184          1.1  christos 
    185          1.1  christos 	eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
    186          1.1  christos 	eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
    187          1.1  christos 			       data, NULL);
    188          1.1  christos 
    189          1.1  christos 	/*
    190          1.1  christos 	 * A more advanced bgscan could process scan results internally, select
    191          1.1  christos 	 * the BSS and request roam if needed. This sample uses the existing
    192          1.1  christos 	 * BSS/ESS selection routine. Change this to return 1 if selection is
    193          1.1  christos 	 * done inside the bgscan module.
    194          1.1  christos 	 */
    195          1.1  christos 
    196          1.1  christos 	return 0;
    197          1.1  christos }
    198          1.1  christos 
    199          1.1  christos 
    200          1.1  christos static void bgscan_simple_notify_beacon_loss(void *priv)
    201          1.1  christos {
    202          1.1  christos 	wpa_printf(MSG_DEBUG, "bgscan simple: beacon loss");
    203          1.1  christos 	/* TODO: speed up background scanning */
    204          1.1  christos }
    205          1.1  christos 
    206          1.1  christos 
    207  1.1.1.2.4.1       snj static void bgscan_simple_notify_signal_change(void *priv, int above,
    208  1.1.1.2.4.1       snj 					       int current_signal,
    209  1.1.1.2.4.1       snj 					       int current_noise,
    210  1.1.1.2.4.1       snj 					       int current_txrate)
    211          1.1  christos {
    212          1.1  christos 	struct bgscan_simple_data *data = priv;
    213      1.1.1.2  christos 	int scan = 0;
    214  1.1.1.2.4.1       snj 	struct os_reltime now;
    215          1.1  christos 
    216          1.1  christos 	if (data->short_interval == data->long_interval ||
    217          1.1  christos 	    data->signal_threshold == 0)
    218          1.1  christos 		return;
    219          1.1  christos 
    220          1.1  christos 	wpa_printf(MSG_DEBUG, "bgscan simple: signal level changed "
    221  1.1.1.2.4.1       snj 		   "(above=%d current_signal=%d current_noise=%d "
    222  1.1.1.2.4.1       snj 		   "current_txrate=%d))", above, current_signal,
    223  1.1.1.2.4.1       snj 		   current_noise, current_txrate);
    224          1.1  christos 	if (data->scan_interval == data->long_interval && !above) {
    225      1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "bgscan simple: Start using short "
    226      1.1.1.2  christos 			   "bgscan interval");
    227          1.1  christos 		data->scan_interval = data->short_interval;
    228  1.1.1.2.4.1       snj 		os_get_reltime(&now);
    229  1.1.1.2.4.1       snj 		if (now.sec > data->last_bgscan.sec + 1 &&
    230  1.1.1.2.4.1       snj 		    data->short_scan_count <= data->max_short_scans)
    231  1.1.1.2.4.1       snj 			/*
    232  1.1.1.2.4.1       snj 			 * If we haven't just previously (<1 second ago)
    233  1.1.1.2.4.1       snj 			 * performed a scan, and we haven't depleted our
    234  1.1.1.2.4.1       snj 			 * budget for short-scans, perform a scan
    235  1.1.1.2.4.1       snj 			 * immediately.
    236  1.1.1.2.4.1       snj 			 */
    237      1.1.1.2  christos 			scan = 1;
    238  1.1.1.2.4.1       snj 		else if (data->last_bgscan.sec + data->long_interval >
    239  1.1.1.2.4.1       snj 			 now.sec + data->scan_interval) {
    240  1.1.1.2.4.1       snj 			/*
    241  1.1.1.2.4.1       snj 			 * Restart scan interval timer if currently scheduled
    242  1.1.1.2.4.1       snj 			 * scan is too far in the future.
    243  1.1.1.2.4.1       snj 			 */
    244  1.1.1.2.4.1       snj 			eloop_cancel_timeout(bgscan_simple_timeout, data,
    245  1.1.1.2.4.1       snj 					     NULL);
    246  1.1.1.2.4.1       snj 			eloop_register_timeout(data->scan_interval, 0,
    247  1.1.1.2.4.1       snj 					       bgscan_simple_timeout, data,
    248  1.1.1.2.4.1       snj 					       NULL);
    249  1.1.1.2.4.1       snj 		}
    250          1.1  christos 	} else if (data->scan_interval == data->short_interval && above) {
    251          1.1  christos 		wpa_printf(MSG_DEBUG, "bgscan simple: Start using long bgscan "
    252          1.1  christos 			   "interval");
    253          1.1  christos 		data->scan_interval = data->long_interval;
    254          1.1  christos 		eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
    255          1.1  christos 		eloop_register_timeout(data->scan_interval, 0,
    256          1.1  christos 				       bgscan_simple_timeout, data, NULL);
    257          1.1  christos 	} else if (!above) {
    258          1.1  christos 		/*
    259          1.1  christos 		 * Signal dropped further 4 dB. Request a new scan if we have
    260          1.1  christos 		 * not yet scanned in a while.
    261          1.1  christos 		 */
    262  1.1.1.2.4.1       snj 		os_get_reltime(&now);
    263      1.1.1.2  christos 		if (now.sec > data->last_bgscan.sec + 10)
    264      1.1.1.2  christos 			scan = 1;
    265      1.1.1.2  christos 	}
    266      1.1.1.2  christos 
    267      1.1.1.2  christos 	if (scan) {
    268      1.1.1.2  christos 		wpa_printf(MSG_DEBUG, "bgscan simple: Trigger immediate scan");
    269      1.1.1.2  christos 		eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
    270      1.1.1.2  christos 		eloop_register_timeout(0, 0, bgscan_simple_timeout, data,
    271      1.1.1.2  christos 				       NULL);
    272          1.1  christos 	}
    273          1.1  christos }
    274          1.1  christos 
    275          1.1  christos 
    276          1.1  christos const struct bgscan_ops bgscan_simple_ops = {
    277          1.1  christos 	.name = "simple",
    278          1.1  christos 	.init = bgscan_simple_init,
    279          1.1  christos 	.deinit = bgscan_simple_deinit,
    280          1.1  christos 	.notify_scan = bgscan_simple_notify_scan,
    281          1.1  christos 	.notify_beacon_loss = bgscan_simple_notify_beacon_loss,
    282          1.1  christos 	.notify_signal_change = bgscan_simple_notify_signal_change,
    283          1.1  christos };
    284