blob: 072a1d5414aea9ef3bc9b890ca82525ac15aa65a [file] [log] [blame]
Dmitry Shmidt04949592012-07-19 12:16:46 -07001/*
2 * WPA Supplicant - auto scan
3 * Copyright (c) 2012, Intel Corporation. All rights reserved.
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004 * Copyright 2015 Intel Deutschland GmbH
Dmitry Shmidt04949592012-07-19 12:16:46 -07005 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "config.h"
14#include "wpa_supplicant_i.h"
15#include "bss.h"
16#include "scan.h"
17#include "autoscan.h"
18
Dmitry Shmidt04949592012-07-19 12:16:46 -070019
20static const struct autoscan_ops * autoscan_modules[] = {
21#ifdef CONFIG_AUTOSCAN_EXPONENTIAL
22 &autoscan_exponential_ops,
23#endif /* CONFIG_AUTOSCAN_EXPONENTIAL */
24#ifdef CONFIG_AUTOSCAN_PERIODIC
25 &autoscan_periodic_ops,
26#endif /* CONFIG_AUTOSCAN_PERIODIC */
27 NULL
28};
29
30
31static void request_scan(struct wpa_supplicant *wpa_s)
32{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080033 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt04949592012-07-19 12:16:46 -070034
35 if (wpa_supplicant_req_sched_scan(wpa_s))
36 wpa_supplicant_req_scan(wpa_s, wpa_s->scan_interval, 0);
37}
38
39
40int autoscan_init(struct wpa_supplicant *wpa_s, int req_scan)
41{
42 const char *name = wpa_s->conf->autoscan;
43 const char *params;
44 size_t nlen;
45 int i;
46 const struct autoscan_ops *ops = NULL;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080047 struct sched_scan_plan *scan_plans;
48
49 /* Give preference to scheduled scan plans if supported/configured */
Dmitry Shmidtfe31a9a2016-11-09 13:43:31 -080050 if (wpa_s->sched_scan_plans)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080051 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -070052
Dmitry Shmidtfe31a9a2016-11-09 13:43:31 -080053 if (wpa_s->autoscan && wpa_s->autoscan_priv)
Dmitry Shmidt04949592012-07-19 12:16:46 -070054 return 0;
55
56 if (name == NULL)
57 return 0;
58
59 params = os_strchr(name, ':');
60 if (params == NULL) {
61 params = "";
62 nlen = os_strlen(name);
63 } else {
64 nlen = params - name;
65 params++;
66 }
67
68 for (i = 0; autoscan_modules[i]; i++) {
69 if (os_strncmp(name, autoscan_modules[i]->name, nlen) == 0) {
70 ops = autoscan_modules[i];
71 break;
72 }
73 }
74
75 if (ops == NULL) {
76 wpa_printf(MSG_ERROR, "autoscan: Could not find module "
77 "matching the parameter '%s'", name);
78 return -1;
79 }
80
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080081 scan_plans = os_malloc(sizeof(*wpa_s->sched_scan_plans));
82 if (!scan_plans)
83 return -1;
84
Dmitry Shmidt04949592012-07-19 12:16:46 -070085 wpa_s->autoscan_params = NULL;
86
87 wpa_s->autoscan_priv = ops->init(wpa_s, params);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080088 if (!wpa_s->autoscan_priv) {
89 os_free(scan_plans);
Dmitry Shmidt04949592012-07-19 12:16:46 -070090 return -1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080091 }
92
93 scan_plans[0].interval = 5;
94 scan_plans[0].iterations = 0;
95 os_free(wpa_s->sched_scan_plans);
96 wpa_s->sched_scan_plans = scan_plans;
97 wpa_s->sched_scan_plans_num = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -070098 wpa_s->autoscan = ops;
99
100 wpa_printf(MSG_DEBUG, "autoscan: Initialized module '%s' with "
101 "parameters '%s'", ops->name, params);
102 if (!req_scan)
103 return 0;
104
105 /*
106 * Cancelling existing scan requests, if any.
107 */
108 wpa_supplicant_cancel_sched_scan(wpa_s);
109 wpa_supplicant_cancel_scan(wpa_s);
110
111 /*
112 * Firing first scan, which will lead to call autoscan_notify_scan.
113 */
114 request_scan(wpa_s);
115
116 return 0;
117}
118
119
120void autoscan_deinit(struct wpa_supplicant *wpa_s)
121{
122 if (wpa_s->autoscan && wpa_s->autoscan_priv) {
123 wpa_printf(MSG_DEBUG, "autoscan: Deinitializing module '%s'",
124 wpa_s->autoscan->name);
125 wpa_s->autoscan->deinit(wpa_s->autoscan_priv);
126 wpa_s->autoscan = NULL;
127 wpa_s->autoscan_priv = NULL;
128
129 wpa_s->scan_interval = 5;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800130
131 os_free(wpa_s->sched_scan_plans);
132 wpa_s->sched_scan_plans = NULL;
133 wpa_s->sched_scan_plans_num = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700134 }
135}
136
137
138int autoscan_notify_scan(struct wpa_supplicant *wpa_s,
139 struct wpa_scan_results *scan_res)
140{
141 int interval;
142
143 if (wpa_s->autoscan && wpa_s->autoscan_priv) {
144 interval = wpa_s->autoscan->notify_scan(wpa_s->autoscan_priv,
145 scan_res);
146
147 if (interval <= 0)
148 return -1;
149
150 wpa_s->scan_interval = interval;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800151 wpa_s->sched_scan_plans[0].interval = interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700152
153 request_scan(wpa_s);
154 }
155
156 return 0;
157}