]>
Commit | Line | Data |
---|---|---|
c4d71c25 JM |
1 | /* |
2 | * WPA Supplicant - background scan and roaming module: learn | |
3 | * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
15 | #include "includes.h" | |
16 | ||
17 | #include "common.h" | |
18 | #include "eloop.h" | |
fc480e88 JM |
19 | #include "list.h" |
20 | #include "common/ieee802_11_defs.h" | |
c4d71c25 JM |
21 | #include "drivers/driver.h" |
22 | #include "config_ssid.h" | |
23 | #include "wpa_supplicant_i.h" | |
24 | #include "driver_i.h" | |
25 | #include "scan.h" | |
26 | #include "bgscan.h" | |
27 | ||
fc480e88 JM |
28 | struct bgscan_learn_bss { |
29 | struct dl_list list; | |
30 | u8 bssid[ETH_ALEN]; | |
31 | int freq; | |
3b7442e5 JM |
32 | u8 *neigh; /* num_neigh * ETH_ALEN buffer */ |
33 | size_t num_neigh; | |
fc480e88 JM |
34 | }; |
35 | ||
c4d71c25 JM |
36 | struct bgscan_learn_data { |
37 | struct wpa_supplicant *wpa_s; | |
38 | const struct wpa_ssid *ssid; | |
39 | int scan_interval; | |
40 | int signal_threshold; | |
41 | int short_interval; /* use if signal < threshold */ | |
42 | int long_interval; /* use if signal > threshold */ | |
43 | struct os_time last_bgscan; | |
2e8d6ae3 | 44 | char *fname; |
fc480e88 | 45 | struct dl_list bss; |
2e2a8d07 JM |
46 | int *supp_freqs; |
47 | int probe_idx; | |
c4d71c25 JM |
48 | }; |
49 | ||
50 | ||
3b7442e5 JM |
51 | static void bss_free(struct bgscan_learn_bss *bss) |
52 | { | |
53 | os_free(bss->neigh); | |
54 | os_free(bss); | |
55 | } | |
56 | ||
57 | ||
58 | static int bssid_in_array(u8 *array, size_t array_len, const u8 *bssid) | |
59 | { | |
60 | size_t i; | |
61 | ||
62 | if (array == NULL || array_len == 0) | |
63 | return 0; | |
64 | ||
65 | for (i = 0; i < array_len; i++) { | |
66 | if (os_memcmp(array + i * ETH_ALEN, bssid, ETH_ALEN) == 0) | |
67 | return 1; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | ||
74 | static void bgscan_learn_add_neighbor(struct bgscan_learn_bss *bss, | |
75 | const u8 *bssid) | |
76 | { | |
77 | u8 *n; | |
78 | ||
79 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) | |
80 | return; | |
81 | if (bssid_in_array(bss->neigh, bss->num_neigh, bssid)) | |
82 | return; | |
83 | ||
84 | n = os_realloc(bss->neigh, (bss->num_neigh + 1) * ETH_ALEN); | |
85 | if (n == NULL) | |
86 | return; | |
87 | ||
88 | os_memcpy(n + bss->num_neigh * ETH_ALEN, bssid, ETH_ALEN); | |
89 | bss->neigh = n; | |
90 | bss->num_neigh++; | |
3b7442e5 JM |
91 | } |
92 | ||
93 | ||
fc480e88 JM |
94 | static struct bgscan_learn_bss * bgscan_learn_get_bss( |
95 | struct bgscan_learn_data *data, const u8 *bssid) | |
96 | { | |
97 | struct bgscan_learn_bss *bss; | |
98 | ||
99 | dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) { | |
100 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) | |
101 | return bss; | |
102 | } | |
103 | return NULL; | |
104 | } | |
105 | ||
106 | ||
2e8d6ae3 JM |
107 | static int bgscan_learn_load(struct bgscan_learn_data *data) |
108 | { | |
109 | FILE *f; | |
110 | char buf[128]; | |
3b7442e5 | 111 | struct bgscan_learn_bss *bss; |
2e8d6ae3 JM |
112 | |
113 | if (data->fname == NULL) | |
114 | return 0; | |
115 | ||
116 | f = fopen(data->fname, "r"); | |
117 | if (f == NULL) | |
118 | return 0; | |
119 | ||
120 | wpa_printf(MSG_DEBUG, "bgscan learn: Loading data from %s", | |
121 | data->fname); | |
122 | ||
123 | if (fgets(buf, sizeof(buf), f) == NULL || | |
124 | os_strncmp(buf, "wpa_supplicant-bgscan-learn\n", 28) != 0) { | |
125 | wpa_printf(MSG_INFO, "bgscan learn: Invalid data file %s", | |
126 | data->fname); | |
127 | fclose(f); | |
128 | return -1; | |
129 | } | |
130 | ||
fc480e88 JM |
131 | while (fgets(buf, sizeof(buf), f)) { |
132 | if (os_strncmp(buf, "BSS ", 4) == 0) { | |
fc480e88 JM |
133 | bss = os_zalloc(sizeof(*bss)); |
134 | if (!bss) | |
135 | continue; | |
136 | if (hwaddr_aton(buf + 4, bss->bssid) < 0) { | |
3b7442e5 | 137 | bss_free(bss); |
fc480e88 JM |
138 | continue; |
139 | } | |
140 | bss->freq = atoi(buf + 4 + 18); | |
141 | dl_list_add(&data->bss, &bss->list); | |
142 | wpa_printf(MSG_DEBUG, "bgscan learn: Loaded BSS " | |
143 | "entry: " MACSTR " freq=%d", | |
144 | MAC2STR(bss->bssid), bss->freq); | |
145 | } | |
3b7442e5 JM |
146 | |
147 | if (os_strncmp(buf, "NEIGHBOR ", 9) == 0) { | |
148 | u8 addr[ETH_ALEN]; | |
149 | ||
150 | if (hwaddr_aton(buf + 9, addr) < 0) | |
151 | continue; | |
152 | bss = bgscan_learn_get_bss(data, addr); | |
153 | if (bss == NULL) | |
154 | continue; | |
155 | if (hwaddr_aton(buf + 9 + 18, addr) < 0) | |
156 | continue; | |
157 | ||
158 | bgscan_learn_add_neighbor(bss, addr); | |
159 | } | |
fc480e88 JM |
160 | } |
161 | ||
2e8d6ae3 JM |
162 | fclose(f); |
163 | return 0; | |
164 | } | |
165 | ||
166 | ||
167 | static void bgscan_learn_save(struct bgscan_learn_data *data) | |
168 | { | |
169 | FILE *f; | |
fc480e88 | 170 | struct bgscan_learn_bss *bss; |
2e8d6ae3 JM |
171 | |
172 | if (data->fname == NULL) | |
173 | return; | |
174 | ||
175 | wpa_printf(MSG_DEBUG, "bgscan learn: Saving data to %s", | |
176 | data->fname); | |
177 | ||
178 | f = fopen(data->fname, "w"); | |
179 | if (f == NULL) | |
180 | return; | |
181 | fprintf(f, "wpa_supplicant-bgscan-learn\n"); | |
182 | ||
fc480e88 JM |
183 | dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) { |
184 | fprintf(f, "BSS " MACSTR " %d\n", | |
185 | MAC2STR(bss->bssid), bss->freq); | |
186 | } | |
187 | ||
3b7442e5 JM |
188 | dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) { |
189 | size_t i; | |
190 | for (i = 0; i < bss->num_neigh; i++) { | |
191 | fprintf(f, "NEIGHBOR " MACSTR " " MACSTR "\n", | |
192 | MAC2STR(bss->bssid), | |
193 | MAC2STR(bss->neigh + i * ETH_ALEN)); | |
194 | } | |
195 | } | |
196 | ||
2e8d6ae3 JM |
197 | fclose(f); |
198 | } | |
199 | ||
200 | ||
fc480e88 JM |
201 | static int in_array(int *array, int val) |
202 | { | |
203 | int i; | |
204 | ||
205 | if (array == NULL) | |
206 | return 0; | |
207 | ||
208 | for (i = 0; array[i]; i++) { | |
209 | if (array[i] == val) | |
210 | return 1; | |
211 | } | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | ||
217 | static int * bgscan_learn_get_freqs(struct bgscan_learn_data *data, | |
218 | size_t *count) | |
219 | { | |
220 | struct bgscan_learn_bss *bss; | |
221 | int *freqs = NULL, *n; | |
222 | ||
223 | *count = 0; | |
224 | ||
225 | dl_list_for_each(bss, &data->bss, struct bgscan_learn_bss, list) { | |
226 | if (in_array(freqs, bss->freq)) | |
227 | continue; | |
228 | n = os_realloc(freqs, (*count + 2) * sizeof(int)); | |
229 | if (n == NULL) | |
230 | return freqs; | |
231 | freqs = n; | |
232 | freqs[*count] = bss->freq; | |
233 | (*count)++; | |
234 | freqs[*count] = 0; | |
235 | } | |
236 | ||
237 | return freqs; | |
238 | } | |
239 | ||
240 | ||
2e2a8d07 JM |
241 | static int * bgscan_learn_get_probe_freq(struct bgscan_learn_data *data, |
242 | int *freqs, size_t count) | |
243 | { | |
244 | int idx, *n; | |
245 | ||
246 | if (data->supp_freqs == NULL) | |
247 | return freqs; | |
248 | ||
249 | idx = data->probe_idx + 1; | |
250 | while (idx != data->probe_idx) { | |
251 | if (data->supp_freqs[idx] == 0) | |
252 | idx = 0; | |
253 | if (!in_array(freqs, data->supp_freqs[idx])) { | |
254 | wpa_printf(MSG_DEBUG, "bgscan learn: Probe new freq " | |
255 | "%u", data->supp_freqs[idx]); | |
256 | data->probe_idx = idx; | |
257 | n = os_realloc(freqs, (count + 2) * sizeof(int)); | |
258 | if (n == NULL) | |
259 | return freqs; | |
260 | freqs = n; | |
261 | freqs[count] = data->supp_freqs[idx]; | |
262 | count++; | |
263 | freqs[count] = 0; | |
264 | break; | |
265 | } | |
266 | ||
267 | idx++; | |
268 | } | |
269 | ||
270 | return freqs; | |
271 | } | |
272 | ||
273 | ||
c4d71c25 JM |
274 | static void bgscan_learn_timeout(void *eloop_ctx, void *timeout_ctx) |
275 | { | |
276 | struct bgscan_learn_data *data = eloop_ctx; | |
277 | struct wpa_supplicant *wpa_s = data->wpa_s; | |
278 | struct wpa_driver_scan_params params; | |
fc480e88 | 279 | int *freqs = NULL; |
2e2a8d07 JM |
280 | size_t count, i; |
281 | char msg[100], *pos; | |
c4d71c25 JM |
282 | |
283 | os_memset(¶ms, 0, sizeof(params)); | |
284 | params.num_ssids = 1; | |
285 | params.ssids[0].ssid = data->ssid->ssid; | |
286 | params.ssids[0].ssid_len = data->ssid->ssid_len; | |
fc480e88 JM |
287 | if (data->ssid->scan_freq) |
288 | params.freqs = data->ssid->scan_freq; | |
289 | else { | |
290 | freqs = bgscan_learn_get_freqs(data, &count); | |
291 | wpa_printf(MSG_DEBUG, "bgscan learn: BSSes in this ESS have " | |
292 | "been seen on %u channels", (unsigned int) count); | |
2e2a8d07 JM |
293 | freqs = bgscan_learn_get_probe_freq(data, freqs, count); |
294 | ||
295 | msg[0] = '\0'; | |
296 | pos = msg; | |
297 | for (i = 0; freqs && freqs[i]; i++) { | |
298 | int ret; | |
299 | ret = os_snprintf(pos, msg + sizeof(msg) - pos, " %d", | |
300 | freqs[i]); | |
301 | if (ret < 0 || ret >= msg + sizeof(msg) - pos) | |
302 | break; | |
303 | pos += ret; | |
304 | } | |
305 | pos[0] = '\0'; | |
306 | wpa_printf(MSG_DEBUG, "bgscan learn: Scanning frequencies:%s", | |
307 | msg); | |
fc480e88 JM |
308 | params.freqs = freqs; |
309 | } | |
c4d71c25 JM |
310 | |
311 | wpa_printf(MSG_DEBUG, "bgscan learn: Request a background scan"); | |
312 | if (wpa_supplicant_trigger_scan(wpa_s, ¶ms)) { | |
313 | wpa_printf(MSG_DEBUG, "bgscan learn: Failed to trigger scan"); | |
314 | eloop_register_timeout(data->scan_interval, 0, | |
315 | bgscan_learn_timeout, data, NULL); | |
316 | } else | |
317 | os_get_time(&data->last_bgscan); | |
fc480e88 | 318 | os_free(freqs); |
c4d71c25 JM |
319 | } |
320 | ||
321 | ||
322 | static int bgscan_learn_get_params(struct bgscan_learn_data *data, | |
323 | const char *params) | |
324 | { | |
325 | const char *pos; | |
326 | ||
327 | if (params == NULL) | |
328 | return 0; | |
329 | ||
330 | data->short_interval = atoi(params); | |
331 | ||
332 | pos = os_strchr(params, ':'); | |
333 | if (pos == NULL) | |
334 | return 0; | |
335 | pos++; | |
336 | data->signal_threshold = atoi(pos); | |
337 | pos = os_strchr(pos, ':'); | |
338 | if (pos == NULL) { | |
339 | wpa_printf(MSG_ERROR, "bgscan learn: Missing scan interval " | |
340 | "for high signal"); | |
341 | return -1; | |
342 | } | |
343 | pos++; | |
344 | data->long_interval = atoi(pos); | |
2e8d6ae3 JM |
345 | pos = os_strchr(pos, ':'); |
346 | if (pos) { | |
347 | pos++; | |
348 | data->fname = os_strdup(pos); | |
349 | } | |
c4d71c25 JM |
350 | |
351 | return 0; | |
352 | } | |
353 | ||
354 | ||
2e2a8d07 JM |
355 | static int * bgscan_learn_get_supp_freqs(struct wpa_supplicant *wpa_s) |
356 | { | |
357 | struct hostapd_hw_modes *modes; | |
2e2a8d07 JM |
358 | int i, j, *freqs = NULL, *n; |
359 | size_t count = 0; | |
360 | ||
6bf731e8 CL |
361 | modes = wpa_s->hw.modes; |
362 | if (modes == NULL) | |
2e2a8d07 JM |
363 | return NULL; |
364 | ||
6bf731e8 | 365 | for (i = 0; i < wpa_s->hw.num_modes; i++) { |
2e2a8d07 JM |
366 | for (j = 0; j < modes[i].num_channels; j++) { |
367 | if (modes[i].channels[j].flag & HOSTAPD_CHAN_DISABLED) | |
368 | continue; | |
369 | n = os_realloc(freqs, (count + 2) * sizeof(int)); | |
6bf731e8 | 370 | if (n == NULL) |
2e2a8d07 JM |
371 | continue; |
372 | ||
373 | freqs = n; | |
374 | freqs[count] = modes[i].channels[j].freq; | |
375 | count++; | |
376 | freqs[count] = 0; | |
377 | } | |
2e2a8d07 | 378 | } |
2e2a8d07 JM |
379 | |
380 | return freqs; | |
381 | } | |
382 | ||
383 | ||
c4d71c25 JM |
384 | static void * bgscan_learn_init(struct wpa_supplicant *wpa_s, |
385 | const char *params, | |
386 | const struct wpa_ssid *ssid) | |
387 | { | |
388 | struct bgscan_learn_data *data; | |
389 | ||
390 | data = os_zalloc(sizeof(*data)); | |
391 | if (data == NULL) | |
392 | return NULL; | |
fc480e88 | 393 | dl_list_init(&data->bss); |
c4d71c25 JM |
394 | data->wpa_s = wpa_s; |
395 | data->ssid = ssid; | |
396 | if (bgscan_learn_get_params(data, params) < 0) { | |
2e8d6ae3 | 397 | os_free(data->fname); |
c4d71c25 JM |
398 | os_free(data); |
399 | return NULL; | |
400 | } | |
401 | if (data->short_interval <= 0) | |
402 | data->short_interval = 30; | |
403 | if (data->long_interval <= 0) | |
404 | data->long_interval = 30; | |
405 | ||
2e8d6ae3 JM |
406 | if (bgscan_learn_load(data) < 0) { |
407 | os_free(data->fname); | |
408 | os_free(data); | |
409 | return NULL; | |
410 | } | |
411 | ||
c4d71c25 JM |
412 | wpa_printf(MSG_DEBUG, "bgscan learn: Signal strength threshold %d " |
413 | "Short bgscan interval %d Long bgscan interval %d", | |
414 | data->signal_threshold, data->short_interval, | |
415 | data->long_interval); | |
416 | ||
417 | if (data->signal_threshold && | |
418 | wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) { | |
419 | wpa_printf(MSG_ERROR, "bgscan learn: Failed to enable " | |
420 | "signal strength monitoring"); | |
421 | } | |
422 | ||
2e2a8d07 | 423 | data->supp_freqs = bgscan_learn_get_supp_freqs(wpa_s); |
c4d71c25 JM |
424 | data->scan_interval = data->short_interval; |
425 | eloop_register_timeout(data->scan_interval, 0, bgscan_learn_timeout, | |
426 | data, NULL); | |
9ff80a10 JM |
427 | |
428 | /* | |
429 | * This function is called immediately after an association, so it is | |
430 | * reasonable to assume that a scan was completed recently. This makes | |
431 | * us skip an immediate new scan in cases where the current signal | |
432 | * level is below the bgscan threshold. | |
433 | */ | |
434 | os_get_time(&data->last_bgscan); | |
435 | ||
c4d71c25 JM |
436 | return data; |
437 | } | |
438 | ||
439 | ||
440 | static void bgscan_learn_deinit(void *priv) | |
441 | { | |
442 | struct bgscan_learn_data *data = priv; | |
fc480e88 JM |
443 | struct bgscan_learn_bss *bss, *n; |
444 | ||
2e8d6ae3 | 445 | bgscan_learn_save(data); |
c4d71c25 JM |
446 | eloop_cancel_timeout(bgscan_learn_timeout, data, NULL); |
447 | if (data->signal_threshold) | |
448 | wpa_drv_signal_monitor(data->wpa_s, 0, 0); | |
2e8d6ae3 | 449 | os_free(data->fname); |
fc480e88 JM |
450 | dl_list_for_each_safe(bss, n, &data->bss, struct bgscan_learn_bss, |
451 | list) { | |
452 | dl_list_del(&bss->list); | |
3b7442e5 | 453 | bss_free(bss); |
fc480e88 | 454 | } |
2e2a8d07 | 455 | os_free(data->supp_freqs); |
c4d71c25 JM |
456 | os_free(data); |
457 | } | |
458 | ||
459 | ||
fc480e88 JM |
460 | static int bgscan_learn_bss_match(struct bgscan_learn_data *data, |
461 | struct wpa_scan_res *bss) | |
462 | { | |
463 | const u8 *ie; | |
464 | ||
465 | ie = wpa_scan_get_ie(bss, WLAN_EID_SSID); | |
466 | if (ie == NULL) | |
467 | return 0; | |
468 | ||
469 | if (data->ssid->ssid_len != ie[1] || | |
470 | os_memcmp(data->ssid->ssid, ie + 2, ie[1]) != 0) | |
471 | return 0; /* SSID mismatch */ | |
472 | ||
473 | return 1; | |
474 | } | |
475 | ||
476 | ||
c4d71c25 JM |
477 | static int bgscan_learn_notify_scan(void *priv, |
478 | struct wpa_scan_results *scan_res) | |
479 | { | |
480 | struct bgscan_learn_data *data = priv; | |
3b7442e5 JM |
481 | size_t i, j; |
482 | #define MAX_BSS 50 | |
483 | u8 bssid[MAX_BSS * ETH_ALEN]; | |
484 | size_t num_bssid = 0; | |
c4d71c25 JM |
485 | |
486 | wpa_printf(MSG_DEBUG, "bgscan learn: scan result notification"); | |
487 | ||
488 | eloop_cancel_timeout(bgscan_learn_timeout, data, NULL); | |
489 | eloop_register_timeout(data->scan_interval, 0, bgscan_learn_timeout, | |
490 | data, NULL); | |
491 | ||
3b7442e5 JM |
492 | for (i = 0; i < scan_res->num; i++) { |
493 | struct wpa_scan_res *res = scan_res->res[i]; | |
494 | if (!bgscan_learn_bss_match(data, res)) | |
495 | continue; | |
496 | ||
497 | if (num_bssid < MAX_BSS) { | |
498 | os_memcpy(bssid + num_bssid * ETH_ALEN, res->bssid, | |
499 | ETH_ALEN); | |
500 | num_bssid++; | |
501 | } | |
502 | } | |
503 | wpa_printf(MSG_DEBUG, "bgscan learn: %u matching BSSes in scan " | |
504 | "results", (unsigned int) num_bssid); | |
505 | ||
fc480e88 JM |
506 | for (i = 0; i < scan_res->num; i++) { |
507 | struct wpa_scan_res *res = scan_res->res[i]; | |
508 | struct bgscan_learn_bss *bss; | |
509 | ||
510 | if (!bgscan_learn_bss_match(data, res)) | |
511 | continue; | |
3b7442e5 | 512 | |
fc480e88 JM |
513 | bss = bgscan_learn_get_bss(data, res->bssid); |
514 | if (bss && bss->freq != res->freq) { | |
515 | wpa_printf(MSG_DEBUG, "bgscan learn: Update BSS " | |
516 | MACSTR " freq %d -> %d", | |
517 | MAC2STR(res->bssid), bss->freq, res->freq); | |
518 | bss->freq = res->freq; | |
519 | } else if (!bss) { | |
520 | wpa_printf(MSG_DEBUG, "bgscan learn: Add BSS " MACSTR | |
521 | " freq=%d", MAC2STR(res->bssid), res->freq); | |
522 | bss = os_zalloc(sizeof(*bss)); | |
523 | if (!bss) | |
524 | continue; | |
525 | os_memcpy(bss->bssid, res->bssid, ETH_ALEN); | |
526 | bss->freq = res->freq; | |
527 | dl_list_add(&data->bss, &bss->list); | |
528 | } | |
3b7442e5 JM |
529 | |
530 | for (j = 0; j < num_bssid; j++) { | |
531 | u8 *addr = bssid + j * ETH_ALEN; | |
532 | bgscan_learn_add_neighbor(bss, addr); | |
533 | } | |
fc480e88 JM |
534 | } |
535 | ||
c4d71c25 JM |
536 | /* |
537 | * A more advanced bgscan could process scan results internally, select | |
538 | * the BSS and request roam if needed. This sample uses the existing | |
539 | * BSS/ESS selection routine. Change this to return 1 if selection is | |
540 | * done inside the bgscan module. | |
541 | */ | |
542 | ||
543 | return 0; | |
544 | } | |
545 | ||
546 | ||
547 | static void bgscan_learn_notify_beacon_loss(void *priv) | |
548 | { | |
549 | wpa_printf(MSG_DEBUG, "bgscan learn: beacon loss"); | |
550 | /* TODO: speed up background scanning */ | |
551 | } | |
552 | ||
553 | ||
60a972a6 | 554 | static void bgscan_learn_notify_signal_change(void *priv, int above, |
174fa789 PS |
555 | int current_signal, |
556 | int current_noise, | |
557 | int current_txrate) | |
c4d71c25 JM |
558 | { |
559 | struct bgscan_learn_data *data = priv; | |
266c828e | 560 | int scan = 0; |
9ff80a10 | 561 | struct os_time now; |
c4d71c25 JM |
562 | |
563 | if (data->short_interval == data->long_interval || | |
564 | data->signal_threshold == 0) | |
565 | return; | |
566 | ||
567 | wpa_printf(MSG_DEBUG, "bgscan learn: signal level changed " | |
174fa789 PS |
568 | "(above=%d current_signal=%d current_noise=%d " |
569 | "current_txrate=%d)", above, current_signal, | |
570 | current_noise, current_txrate); | |
c4d71c25 | 571 | if (data->scan_interval == data->long_interval && !above) { |
9ff80a10 JM |
572 | wpa_printf(MSG_DEBUG, "bgscan learn: Start using short bgscan " |
573 | "interval"); | |
c4d71c25 | 574 | data->scan_interval = data->short_interval; |
9ff80a10 JM |
575 | os_get_time(&now); |
576 | if (now.sec > data->last_bgscan.sec + 1) | |
577 | scan = 1; | |
c4d71c25 JM |
578 | } else if (data->scan_interval == data->short_interval && above) { |
579 | wpa_printf(MSG_DEBUG, "bgscan learn: Start using long bgscan " | |
580 | "interval"); | |
581 | data->scan_interval = data->long_interval; | |
582 | eloop_cancel_timeout(bgscan_learn_timeout, data, NULL); | |
583 | eloop_register_timeout(data->scan_interval, 0, | |
584 | bgscan_learn_timeout, data, NULL); | |
585 | } else if (!above) { | |
c4d71c25 JM |
586 | /* |
587 | * Signal dropped further 4 dB. Request a new scan if we have | |
588 | * not yet scanned in a while. | |
589 | */ | |
590 | os_get_time(&now); | |
9ff80a10 JM |
591 | if (now.sec > data->last_bgscan.sec + 10) |
592 | scan = 1; | |
593 | } | |
594 | ||
595 | if (scan) { | |
596 | wpa_printf(MSG_DEBUG, "bgscan learn: Trigger immediate scan"); | |
597 | eloop_cancel_timeout(bgscan_learn_timeout, data, NULL); | |
598 | eloop_register_timeout(0, 0, bgscan_learn_timeout, data, NULL); | |
c4d71c25 JM |
599 | } |
600 | } | |
601 | ||
602 | ||
603 | const struct bgscan_ops bgscan_learn_ops = { | |
604 | .name = "learn", | |
605 | .init = bgscan_learn_init, | |
606 | .deinit = bgscan_learn_deinit, | |
607 | .notify_scan = bgscan_learn_notify_scan, | |
608 | .notify_beacon_loss = bgscan_learn_notify_beacon_loss, | |
609 | .notify_signal_change = bgscan_learn_notify_signal_change, | |
610 | }; |