Add a simple periodic autoscan module
[mech_eap.git] / wpa_supplicant / autoscan.c
1 /*
2  * WPA Supplicant - auto scan
3  * Copyright (c) 2012, Intel Corporation. All rights reserved.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "config.h"
13 #include "wpa_supplicant_i.h"
14 #include "bss.h"
15 #include "scan.h"
16 #include "autoscan.h"
17
18 #ifdef CONFIG_AUTOSCAN_EXPONENTIAL
19 extern const struct autoscan_ops autoscan_exponential_ops;
20 #endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
21
22 #ifdef CONFIG_AUTOSCAN_PERIODIC
23 extern const struct autoscan_ops autoscan_periodic_ops;
24 #endif /* CONFIG_AUTOSCAN_PERIODIC */
25
26 static const struct autoscan_ops * autoscan_modules[] = {
27 #ifdef CONFIG_AUTOSCAN_EXPONENTIAL
28         &autoscan_exponential_ops,
29 #endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
30 #ifdef CONFIG_AUTOSCAN_PERIODIC
31         &autoscan_periodic_ops,
32 #endif /* CONFIG_AUTOSCAN_PERIODIC */
33         NULL
34 };
35
36
37 static void request_scan(struct wpa_supplicant *wpa_s)
38 {
39         wpa_s->scan_req = 2;
40
41         if (wpa_supplicant_req_sched_scan(wpa_s))
42                 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 0);
43 }
44
45
46 int autoscan_init(struct wpa_supplicant *wpa_s)
47 {
48         const char *name = wpa_s->conf->autoscan;
49         const char *params;
50         size_t nlen;
51         int i;
52         const struct autoscan_ops *ops = NULL;
53
54         if (wpa_s->autoscan && wpa_s->autoscan_priv)
55                 return 0;
56
57         if (name == NULL)
58                 return 0;
59
60         params = os_strchr(name, ':');
61         if (params == NULL) {
62                 params = "";
63                 nlen = os_strlen(name);
64         } else {
65                 nlen = params - name;
66                 params++;
67         }
68
69         for (i = 0; autoscan_modules[i]; i++) {
70                 if (os_strncmp(name, autoscan_modules[i]->name, nlen) == 0) {
71                         ops = autoscan_modules[i];
72                         break;
73                 }
74         }
75
76         if (ops == NULL) {
77                 wpa_printf(MSG_ERROR, "autoscan: Could not find module "
78                            "matching the parameter '%s'", name);
79                 return -1;
80         }
81
82         wpa_s->autoscan_params = NULL;
83
84         wpa_s->autoscan_priv = ops->init(wpa_s, params);
85         if (wpa_s->autoscan_priv == NULL)
86                 return -1;
87         wpa_s->autoscan = ops;
88
89         wpa_printf(MSG_DEBUG, "autoscan: Initialized module '%s' with "
90                    "parameters '%s'", ops->name, params);
91
92         /*
93          * Cancelling existing scan requests, if any.
94          */
95         wpa_supplicant_cancel_sched_scan(wpa_s);
96         wpa_supplicant_cancel_scan(wpa_s);
97
98         /*
99          * Firing first scan, which will lead to call autoscan_notify_scan.
100          */
101         request_scan(wpa_s);
102
103         return 0;
104 }
105
106
107 void autoscan_deinit(struct wpa_supplicant *wpa_s)
108 {
109         if (wpa_s->autoscan && wpa_s->autoscan_priv) {
110                 wpa_printf(MSG_DEBUG, "autoscan: Deinitializing module '%s'",
111                            wpa_s->autoscan->name);
112                 wpa_s->autoscan->deinit(wpa_s->autoscan_priv);
113                 wpa_s->autoscan = NULL;
114                 wpa_s->autoscan_priv = NULL;
115
116                 wpa_s->scan_interval = 5;
117                 wpa_s->sched_scan_interval = 0;
118         }
119 }
120
121
122 int autoscan_notify_scan(struct wpa_supplicant *wpa_s,
123                          struct wpa_scan_results *scan_res)
124 {
125         int interval;
126
127         if (wpa_s->autoscan && wpa_s->autoscan_priv) {
128                 interval = wpa_s->autoscan->notify_scan(wpa_s->autoscan_priv,
129                                                         scan_res);
130
131                 if (interval <= 0)
132                         return -1;
133
134                 wpa_s->scan_interval = interval;
135                 wpa_s->sched_scan_interval = interval;
136
137                 request_scan(wpa_s);
138         }
139
140         return 0;
141 }