bgscan_simple.c revision 1.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 christos int short_interval; /* use if signal < threshold */
32 1.1 christos int long_interval; /* use if signal > threshold */
33 1.1 christos struct os_time last_bgscan;
34 1.1 christos };
35 1.1 christos
36 1.1 christos
37 1.1 christos static void bgscan_simple_timeout(void *eloop_ctx, void *timeout_ctx)
38 1.1 christos {
39 1.1 christos struct bgscan_simple_data *data = eloop_ctx;
40 1.1 christos struct wpa_supplicant *wpa_s = data->wpa_s;
41 1.1 christos struct wpa_driver_scan_params params;
42 1.1 christos
43 1.1 christos os_memset(¶ms, 0, sizeof(params));
44 1.1 christos params.num_ssids = 1;
45 1.1 christos params.ssids[0].ssid = data->ssid->ssid;
46 1.1 christos params.ssids[0].ssid_len = data->ssid->ssid_len;
47 1.1 christos params.freqs = data->ssid->scan_freq;
48 1.1 christos
49 1.1 christos /*
50 1.1 christos * A more advanced bgscan module would learn about most like channels
51 1.1 christos * over time and request scans only for some channels (probing others
52 1.1 christos * every now and then) to reduce effect on the data connection.
53 1.1 christos */
54 1.1 christos
55 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: Request a background scan");
56 1.1 christos if (wpa_supplicant_trigger_scan(wpa_s, ¶ms)) {
57 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: Failed to trigger scan");
58 1.1 christos eloop_register_timeout(data->scan_interval, 0,
59 1.1 christos bgscan_simple_timeout, data, NULL);
60 1.1 christos } else
61 1.1 christos os_get_time(&data->last_bgscan);
62 1.1 christos }
63 1.1 christos
64 1.1 christos
65 1.1 christos static int bgscan_simple_get_params(struct bgscan_simple_data *data,
66 1.1 christos const char *params)
67 1.1 christos {
68 1.1 christos const char *pos;
69 1.1 christos
70 1.1 christos if (params == NULL)
71 1.1 christos return 0;
72 1.1 christos
73 1.1 christos data->short_interval = atoi(params);
74 1.1 christos
75 1.1 christos pos = os_strchr(params, ':');
76 1.1 christos if (pos == NULL)
77 1.1 christos return 0;
78 1.1 christos pos++;
79 1.1 christos data->signal_threshold = atoi(pos);
80 1.1 christos pos = os_strchr(pos, ':');
81 1.1 christos if (pos == NULL) {
82 1.1 christos wpa_printf(MSG_ERROR, "bgscan simple: Missing scan interval "
83 1.1 christos "for high signal");
84 1.1 christos return -1;
85 1.1 christos }
86 1.1 christos pos++;
87 1.1 christos data->long_interval = atoi(pos);
88 1.1 christos
89 1.1 christos return 0;
90 1.1 christos }
91 1.1 christos
92 1.1 christos
93 1.1 christos static void * bgscan_simple_init(struct wpa_supplicant *wpa_s,
94 1.1 christos const char *params,
95 1.1 christos const struct wpa_ssid *ssid)
96 1.1 christos {
97 1.1 christos struct bgscan_simple_data *data;
98 1.1 christos
99 1.1 christos data = os_zalloc(sizeof(*data));
100 1.1 christos if (data == NULL)
101 1.1 christos return NULL;
102 1.1 christos data->wpa_s = wpa_s;
103 1.1 christos data->ssid = ssid;
104 1.1 christos if (bgscan_simple_get_params(data, params) < 0) {
105 1.1 christos os_free(data);
106 1.1 christos return NULL;
107 1.1 christos }
108 1.1 christos if (data->short_interval <= 0)
109 1.1 christos data->short_interval = 30;
110 1.1 christos if (data->long_interval <= 0)
111 1.1 christos data->long_interval = 30;
112 1.1 christos
113 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: Signal strength threshold %d "
114 1.1 christos "Short bgscan interval %d Long bgscan interval %d",
115 1.1 christos data->signal_threshold, data->short_interval,
116 1.1 christos data->long_interval);
117 1.1 christos
118 1.1 christos if (data->signal_threshold &&
119 1.1 christos wpa_drv_signal_monitor(wpa_s, data->signal_threshold, 4) < 0) {
120 1.1 christos wpa_printf(MSG_ERROR, "bgscan simple: Failed to enable "
121 1.1 christos "signal strength monitoring");
122 1.1 christos }
123 1.1 christos
124 1.1 christos data->scan_interval = data->short_interval;
125 1.1 christos eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
126 1.1 christos data, NULL);
127 1.1 christos return data;
128 1.1 christos }
129 1.1 christos
130 1.1 christos
131 1.1 christos static void bgscan_simple_deinit(void *priv)
132 1.1 christos {
133 1.1 christos struct bgscan_simple_data *data = priv;
134 1.1 christos eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
135 1.1 christos if (data->signal_threshold)
136 1.1 christos wpa_drv_signal_monitor(data->wpa_s, 0, 0);
137 1.1 christos os_free(data);
138 1.1 christos }
139 1.1 christos
140 1.1 christos
141 1.1 christos static int bgscan_simple_notify_scan(void *priv)
142 1.1 christos {
143 1.1 christos struct bgscan_simple_data *data = priv;
144 1.1 christos
145 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: scan result notification");
146 1.1 christos
147 1.1 christos eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
148 1.1 christos eloop_register_timeout(data->scan_interval, 0, bgscan_simple_timeout,
149 1.1 christos data, NULL);
150 1.1 christos
151 1.1 christos /*
152 1.1 christos * A more advanced bgscan could process scan results internally, select
153 1.1 christos * the BSS and request roam if needed. This sample uses the existing
154 1.1 christos * BSS/ESS selection routine. Change this to return 1 if selection is
155 1.1 christos * done inside the bgscan module.
156 1.1 christos */
157 1.1 christos
158 1.1 christos return 0;
159 1.1 christos }
160 1.1 christos
161 1.1 christos
162 1.1 christos static void bgscan_simple_notify_beacon_loss(void *priv)
163 1.1 christos {
164 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: beacon loss");
165 1.1 christos /* TODO: speed up background scanning */
166 1.1 christos }
167 1.1 christos
168 1.1 christos
169 1.1 christos static void bgscan_simple_notify_signal_change(void *priv, int above)
170 1.1 christos {
171 1.1 christos struct bgscan_simple_data *data = priv;
172 1.1 christos
173 1.1 christos if (data->short_interval == data->long_interval ||
174 1.1 christos data->signal_threshold == 0)
175 1.1 christos return;
176 1.1 christos
177 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: signal level changed "
178 1.1 christos "(above=%d)", above);
179 1.1 christos if (data->scan_interval == data->long_interval && !above) {
180 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: Trigger immediate scan "
181 1.1 christos "and start using short bgscan interval");
182 1.1 christos data->scan_interval = data->short_interval;
183 1.1 christos eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
184 1.1 christos eloop_register_timeout(0, 0, bgscan_simple_timeout, data,
185 1.1 christos NULL);
186 1.1 christos } else if (data->scan_interval == data->short_interval && above) {
187 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: Start using long bgscan "
188 1.1 christos "interval");
189 1.1 christos data->scan_interval = data->long_interval;
190 1.1 christos eloop_cancel_timeout(bgscan_simple_timeout, data, NULL);
191 1.1 christos eloop_register_timeout(data->scan_interval, 0,
192 1.1 christos bgscan_simple_timeout, data, NULL);
193 1.1 christos } else if (!above) {
194 1.1 christos struct os_time now;
195 1.1 christos /*
196 1.1 christos * Signal dropped further 4 dB. Request a new scan if we have
197 1.1 christos * not yet scanned in a while.
198 1.1 christos */
199 1.1 christos os_get_time(&now);
200 1.1 christos if (now.sec > data->last_bgscan.sec + 10) {
201 1.1 christos wpa_printf(MSG_DEBUG, "bgscan simple: Trigger "
202 1.1 christos "immediate scan");
203 1.1 christos eloop_cancel_timeout(bgscan_simple_timeout, data,
204 1.1 christos NULL);
205 1.1 christos eloop_register_timeout(0, 0, bgscan_simple_timeout,
206 1.1 christos data, NULL);
207 1.1 christos }
208 1.1 christos }
209 1.1 christos }
210 1.1 christos
211 1.1 christos
212 1.1 christos const struct bgscan_ops bgscan_simple_ops = {
213 1.1 christos .name = "simple",
214 1.1 christos .init = bgscan_simple_init,
215 1.1 christos .deinit = bgscan_simple_deinit,
216 1.1 christos .notify_scan = bgscan_simple_notify_scan,
217 1.1 christos .notify_beacon_loss = bgscan_simple_notify_beacon_loss,
218 1.1 christos .notify_signal_change = bgscan_simple_notify_signal_change,
219 1.1 christos };
220