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