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