blob: bae72b934a3b173b3d62d8b51db502cb2a0105d8 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - background scan and roaming module: simple
3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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
20struct 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 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080026 int max_short_scans; /* maximum times we short-scan before back-off */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027 int short_interval; /* use if signal < threshold */
28 int long_interval; /* use if signal > threshold */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080029 struct os_reltime last_bgscan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030};
31
32
33static 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++;
Dmitry Shmidt29333592017-01-09 12:27:11 -080059 if (data->short_scan_count >= data->max_short_scans) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 data->scan_interval = data->long_interval;
61 wpa_printf(MSG_DEBUG, "bgscan simple: Backing "
62 "off to long scan interval");
63 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080064 } 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--;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080073 os_get_reltime(&data->last_bgscan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 }
75}
76
77
78static int bgscan_simple_get_params(struct bgscan_simple_data *data,
79 const char *params)
80{
81 const char *pos;
82
83 if (params == NULL)
84 return 0;
85
86 data->short_interval = atoi(params);
87
88 pos = os_strchr(params, ':');
89 if (pos == NULL)
90 return 0;
91 pos++;
92 data->signal_threshold = atoi(pos);
93 pos = os_strchr(pos, ':');
94 if (pos == NULL) {
95 wpa_printf(MSG_ERROR, "bgscan simple: Missing scan interval "
96 "for high signal");
97 return -1;
98 }
99 pos++;
100 data->long_interval = atoi(pos);
101
102 return 0;
103}
104
105
106static void * bgscan_simple_init(struct wpa_supplicant *wpa_s,
107 const char *params,
108 const struct wpa_ssid *ssid)
109{
110 struct bgscan_simple_data *data;
111
112 data = os_zalloc(sizeof(*data));
113 if (data == NULL)
114 return NULL;
115 data->wpa_s = wpa_s;
116 data->ssid = ssid;
117 if (bgscan_simple_get_params(data, params) < 0) {
118 os_free(data);
119 return NULL;
120 }
121 if (data->short_interval <= 0)
122 data->short_interval = 30;
123 if (data->long_interval <= 0)
124 data->long_interval = 30;
125
126 wpa_printf(MSG_DEBUG, "bgscan simple: Signal strength threshold %d "
127 "Short bgscan interval %d Long bgscan interval %d",
128 data->signal_threshold, data->short_interval,
129 data->long_interval);
130
131 if (data->signal_threshold &&
132 wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
133 wpa_printf(MSG_ERROR, "bgscan simple: Failed to enable "
134 "signal strength monitoring");
135 }
136
137 data->scan_interval = data->short_interval;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800138 data->max_short_scans = data->long_interval / data->short_interval + 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139 if (data->signal_threshold) {
140 /* Poll for signal info to set initial scan interval */
141 struct wpa_signal_info siginfo;
142 if (wpa_drv_signal_poll(wpa_s, &siginfo) == 0 &&
143 siginfo.current_signal >= data->signal_threshold)
144 data->scan_interval = data->long_interval;
145 }
146 wpa_printf(MSG_DEBUG, "bgscan simple: Init scan interval: %d",
147 data->scan_interval);
148 eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
149 data, NULL);
150
151 /*
152 * This function is called immediately after an association, so it is
153 * reasonable to assume that a scan was completed recently. This makes
154 * us skip an immediate new scan in cases where the current signal
155 * level is below the bgscan threshold.
156 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800157 os_get_reltime(&data->last_bgscan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159 return data;
160}
161
162
163static void bgscan_simple_deinit(void *priv)
164{
165 struct bgscan_simple_data *data = priv;
166 eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
167 if (data->signal_threshold)
168 wpa_drv_signal_monitor(data->wpa_s, 0, 0);
169 os_free(data);
170}
171
172
173static int bgscan_simple_notify_scan(void *priv,
174 struct wpa_scan_results *scan_res)
175{
176 struct bgscan_simple_data *data = priv;
177
178 wpa_printf(MSG_DEBUG, "bgscan simple: scan result notification");
179
180 eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
181 eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
182 data, NULL);
183
184 /*
185 * A more advanced bgscan could process scan results internally, select
186 * the BSS and request roam if needed. This sample uses the existing
187 * BSS/ESS selection routine. Change this to return 1 if selection is
188 * done inside the bgscan module.
189 */
190
191 return 0;
192}
193
194
195static void bgscan_simple_notify_beacon_loss(void *priv)
196{
197 wpa_printf(MSG_DEBUG, "bgscan simple: beacon loss");
198 /* TODO: speed up background scanning */
199}
200
201
202static void bgscan_simple_notify_signal_change(void *priv, int above,
203 int current_signal,
204 int current_noise,
205 int current_txrate)
206{
207 struct bgscan_simple_data *data = priv;
208 int scan = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800209 struct os_reltime now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210
211 if (data->short_interval == data->long_interval ||
212 data->signal_threshold == 0)
213 return;
214
215 wpa_printf(MSG_DEBUG, "bgscan simple: signal level changed "
216 "(above=%d current_signal=%d current_noise=%d "
217 "current_txrate=%d))", above, current_signal,
218 current_noise, current_txrate);
219 if (data->scan_interval == data->long_interval && !above) {
220 wpa_printf(MSG_DEBUG, "bgscan simple: Start using short "
221 "bgscan interval");
222 data->scan_interval = data->short_interval;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800223 os_get_reltime(&now);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800224 if (now.sec > data->last_bgscan.sec + 1 &&
225 data->short_scan_count <= data->max_short_scans)
226 /*
227 * If we haven't just previously (<1 second ago)
228 * performed a scan, and we haven't depleted our
229 * budget for short-scans, perform a scan
230 * immediately.
231 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 scan = 1;
233 else if (data->last_bgscan.sec + data->long_interval >
234 now.sec + data->scan_interval) {
235 /*
236 * Restart scan interval timer if currently scheduled
237 * scan is too far in the future.
238 */
239 eloop_cancel_timeout(bgscan_simple_timeout, data,
240 NULL);
241 eloop_register_timeout(data->scan_interval, 0,
242 bgscan_simple_timeout, data,
243 NULL);
244 }
245 } else if (data->scan_interval == data->short_interval && above) {
246 wpa_printf(MSG_DEBUG, "bgscan simple: Start using long bgscan "
247 "interval");
248 data->scan_interval = data->long_interval;
249 eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
250 eloop_register_timeout(data->scan_interval, 0,
251 bgscan_simple_timeout, data, NULL);
252 } else if (!above) {
253 /*
254 * Signal dropped further 4 dB. Request a new scan if we have
255 * not yet scanned in a while.
256 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800257 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 if (now.sec > data->last_bgscan.sec + 10)
259 scan = 1;
260 }
261
262 if (scan) {
263 wpa_printf(MSG_DEBUG, "bgscan simple: Trigger immediate scan");
264 eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
265 eloop_register_timeout(0, 0, bgscan_simple_timeout, data,
266 NULL);
267 }
268}
269
270
271const struct bgscan_ops bgscan_simple_ops = {
272 .name = "simple",
273 .init = bgscan_simple_init,
274 .deinit = bgscan_simple_deinit,
275 .notify_scan = bgscan_simple_notify_scan,
276 .notify_beacon_loss = bgscan_simple_notify_beacon_loss,
277 .notify_signal_change = bgscan_simple_notify_signal_change,
278};