Add automatic scanning support
[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 static const struct autoscan_ops * autoscan_modules[] = {
19         NULL
20 };
21
22
23 static void request_scan(struct wpa_supplicant *wpa_s)
24 {
25         wpa_s->scan_req = 2;
26
27         if (wpa_supplicant_req_sched_scan(wpa_s))
28                 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 0);
29 }
30
31
32 int autoscan_init(struct wpa_supplicant *wpa_s)
33 {
34         const char *name = wpa_s->conf->autoscan;
35         const char *params;
36         size_t nlen;
37         int i;
38         const struct autoscan_ops *ops = NULL;
39
40         if (wpa_s->autoscan && wpa_s->autoscan_priv)
41                 return 0;
42
43         if (name == NULL)
44                 return 0;
45
46         params = os_strchr(name, ':');
47         if (params == NULL) {
48                 params = "";
49                 nlen = os_strlen(name);
50         } else {
51                 nlen = params - name;
52                 params++;
53         }
54
55         for (i = 0; autoscan_modules[i]; i++) {
56                 if (os_strncmp(name, autoscan_modules[i]->name, nlen) == 0) {
57                         ops = autoscan_modules[i];
58                         break;
59                 }
60         }
61
62         if (ops == NULL) {
63                 wpa_printf(MSG_ERROR, "autoscan: Could not find module "
64                            "matching the parameter '%s'", name);
65                 return -1;
66         }
67
68         wpa_s->autoscan_params = NULL;
69
70         wpa_s->autoscan_priv = ops->init(wpa_s, params);
71         if (wpa_s->autoscan_priv == NULL)
72                 return -1;
73         wpa_s->autoscan = ops;
74
75         wpa_printf(MSG_DEBUG, "autoscan: Initialized module '%s' with "
76                    "parameters '%s'", ops->name, params);
77
78         /*
79          * Cancelling existing scan requests, if any.
80          */
81         wpa_supplicant_cancel_sched_scan(wpa_s);
82         wpa_supplicant_cancel_scan(wpa_s);
83
84         /*
85          * Firing first scan, which will lead to call autoscan_notify_scan.
86          */
87         request_scan(wpa_s);
88
89         return 0;
90 }
91
92
93 void autoscan_deinit(struct wpa_supplicant *wpa_s)
94 {
95         if (wpa_s->autoscan && wpa_s->autoscan_priv) {
96                 wpa_printf(MSG_DEBUG, "autoscan: Deinitializing module '%s'",
97                            wpa_s->autoscan->name);
98                 wpa_s->autoscan->deinit(wpa_s->autoscan_priv);
99                 wpa_s->autoscan = NULL;
100                 wpa_s->autoscan_priv = NULL;
101
102                 wpa_s->scan_interval = 5;
103                 wpa_s->sched_scan_interval = 0;
104         }
105 }
106
107
108 int autoscan_notify_scan(struct wpa_supplicant *wpa_s,
109                          struct wpa_scan_results *scan_res)
110 {
111         int interval;
112
113         if (wpa_s->autoscan && wpa_s->autoscan_priv) {
114                 interval = wpa_s->autoscan->notify_scan(wpa_s->autoscan_priv,
115                                                         scan_res);
116
117                 if (interval <= 0)
118                         return -1;
119
120                 wpa_s->scan_interval = interval;
121                 wpa_s->sched_scan_interval = interval;
122
123                 request_scan(wpa_s);
124         }
125
126         return 0;
127 }