2 * WPA Supplicant - background scan and roaming module: simple
3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "drivers/driver.h"
14 #include "config_ssid.h"
15 #include "wpa_supplicant_i.h"
20 struct bgscan_simple_data
{
21 struct wpa_supplicant
*wpa_s
;
22 const struct wpa_ssid
*ssid
;
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
;
33 static void bgscan_simple_timeout(void *eloop_ctx
, void *timeout_ctx
)
35 struct bgscan_simple_data
*data
= eloop_ctx
;
36 struct wpa_supplicant
*wpa_s
= data
->wpa_s
;
37 struct wpa_driver_scan_params params
;
39 os_memset(¶ms
, 0, sizeof(params
));
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
;
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.
51 wpa_printf(MSG_DEBUG
, "bgscan simple: Request a background scan");
52 if (wpa_supplicant_trigger_scan(wpa_s
, ¶ms
)) {
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
);
57 if (data
->scan_interval
== data
->short_interval
) {
58 data
->short_scan_count
++;
60 * Spend at most the duration of a long scan interval
61 * scanning at the short scan interval. After that,
62 * revert to the long scan interval.
64 if (data
->short_scan_count
> data
->max_short_scans
) {
65 data
->scan_interval
= data
->long_interval
;
66 wpa_printf(MSG_DEBUG
, "bgscan simple: Backing "
67 "off to long scan interval");
69 } else if (data
->short_scan_count
> 0) {
71 * If we lasted a long scan interval without any
72 * CQM triggers, decrease the short-scan count,
73 * which allows 1 more short-scan interval to
74 * occur in the future when CQM triggers.
76 data
->short_scan_count
--;
78 os_get_reltime(&data
->last_bgscan
);
83 static int bgscan_simple_get_params(struct bgscan_simple_data
*data
,
91 data
->short_interval
= atoi(params
);
93 pos
= os_strchr(params
, ':');
97 data
->signal_threshold
= atoi(pos
);
98 pos
= os_strchr(pos
, ':');
100 wpa_printf(MSG_ERROR
, "bgscan simple: Missing scan interval "
105 data
->long_interval
= atoi(pos
);
111 static void * bgscan_simple_init(struct wpa_supplicant
*wpa_s
,
113 const struct wpa_ssid
*ssid
)
115 struct bgscan_simple_data
*data
;
117 data
= os_zalloc(sizeof(*data
));
122 if (bgscan_simple_get_params(data
, params
) < 0) {
126 if (data
->short_interval
<= 0)
127 data
->short_interval
= 30;
128 if (data
->long_interval
<= 0)
129 data
->long_interval
= 30;
131 wpa_printf(MSG_DEBUG
, "bgscan simple: Signal strength threshold %d "
132 "Short bgscan interval %d Long bgscan interval %d",
133 data
->signal_threshold
, data
->short_interval
,
134 data
->long_interval
);
136 if (data
->signal_threshold
&&
137 wpa_drv_signal_monitor(wpa_s
, data
->signal_threshold
, 4) < 0) {
138 wpa_printf(MSG_ERROR
, "bgscan simple: Failed to enable "
139 "signal strength monitoring");
142 data
->scan_interval
= data
->short_interval
;
143 data
->max_short_scans
= data
->long_interval
/ data
->short_interval
+ 1;
144 if (data
->signal_threshold
) {
145 /* Poll for signal info to set initial scan interval */
146 struct wpa_signal_info siginfo
;
147 if (wpa_drv_signal_poll(wpa_s
, &siginfo
) == 0 &&
148 siginfo
.current_signal
>= data
->signal_threshold
)
149 data
->scan_interval
= data
->long_interval
;
151 wpa_printf(MSG_DEBUG
, "bgscan simple: Init scan interval: %d",
152 data
->scan_interval
);
153 eloop_register_timeout(data
->scan_interval
, 0, bgscan_simple_timeout
,
157 * This function is called immediately after an association, so it is
158 * reasonable to assume that a scan was completed recently. This makes
159 * us skip an immediate new scan in cases where the current signal
160 * level is below the bgscan threshold.
162 os_get_reltime(&data
->last_bgscan
);
168 static void bgscan_simple_deinit(void *priv
)
170 struct bgscan_simple_data
*data
= priv
;
171 eloop_cancel_timeout(bgscan_simple_timeout
, data
, NULL
);
172 if (data
->signal_threshold
)
173 wpa_drv_signal_monitor(data
->wpa_s
, 0, 0);
178 static int bgscan_simple_notify_scan(void *priv
,
179 struct wpa_scan_results
*scan_res
)
181 struct bgscan_simple_data
*data
= priv
;
183 wpa_printf(MSG_DEBUG
, "bgscan simple: scan result notification");
185 eloop_cancel_timeout(bgscan_simple_timeout
, data
, NULL
);
186 eloop_register_timeout(data
->scan_interval
, 0, bgscan_simple_timeout
,
190 * A more advanced bgscan could process scan results internally, select
191 * the BSS and request roam if needed. This sample uses the existing
192 * BSS/ESS selection routine. Change this to return 1 if selection is
193 * done inside the bgscan module.
200 static void bgscan_simple_notify_beacon_loss(void *priv
)
202 wpa_printf(MSG_DEBUG
, "bgscan simple: beacon loss");
203 /* TODO: speed up background scanning */
207 static void bgscan_simple_notify_signal_change(void *priv
, int above
,
212 struct bgscan_simple_data
*data
= priv
;
214 struct os_reltime now
;
216 if (data
->short_interval
== data
->long_interval
||
217 data
->signal_threshold
== 0)
220 wpa_printf(MSG_DEBUG
, "bgscan simple: signal level changed "
221 "(above=%d current_signal=%d current_noise=%d "
222 "current_txrate=%d))", above
, current_signal
,
223 current_noise
, current_txrate
);
224 if (data
->scan_interval
== data
->long_interval
&& !above
) {
225 wpa_printf(MSG_DEBUG
, "bgscan simple: Start using short "
227 data
->scan_interval
= data
->short_interval
;
228 os_get_reltime(&now
);
229 if (now
.sec
> data
->last_bgscan
.sec
+ 1 &&
230 data
->short_scan_count
<= data
->max_short_scans
)
232 * If we haven't just previously (<1 second ago)
233 * performed a scan, and we haven't depleted our
234 * budget for short-scans, perform a scan
238 else if (data
->last_bgscan
.sec
+ data
->long_interval
>
239 now
.sec
+ data
->scan_interval
) {
241 * Restart scan interval timer if currently scheduled
242 * scan is too far in the future.
244 eloop_cancel_timeout(bgscan_simple_timeout
, data
,
246 eloop_register_timeout(data
->scan_interval
, 0,
247 bgscan_simple_timeout
, data
,
250 } else if (data
->scan_interval
== data
->short_interval
&& above
) {
251 wpa_printf(MSG_DEBUG
, "bgscan simple: Start using long bgscan "
253 data
->scan_interval
= data
->long_interval
;
254 eloop_cancel_timeout(bgscan_simple_timeout
, data
, NULL
);
255 eloop_register_timeout(data
->scan_interval
, 0,
256 bgscan_simple_timeout
, data
, NULL
);
259 * Signal dropped further 4 dB. Request a new scan if we have
260 * not yet scanned in a while.
262 os_get_reltime(&now
);
263 if (now
.sec
> data
->last_bgscan
.sec
+ 10)
268 wpa_printf(MSG_DEBUG
, "bgscan simple: Trigger immediate scan");
269 eloop_cancel_timeout(bgscan_simple_timeout
, data
, NULL
);
270 eloop_register_timeout(0, 0, bgscan_simple_timeout
, data
,
276 const struct bgscan_ops bgscan_simple_ops
= {
278 .init
= bgscan_simple_init
,
279 .deinit
= bgscan_simple_deinit
,
280 .notify_scan
= bgscan_simple_notify_scan
,
281 .notify_beacon_loss
= bgscan_simple_notify_beacon_loss
,
282 .notify_signal_change
= bgscan_simple_notify_signal_change
,