1/*
2 * WPA Supplicant - auto scan exponential module
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 "wpa_supplicant_i.h"
13#include "autoscan.h"
14
15struct autoscan_exponential_data {
16	struct wpa_supplicant *wpa_s;
17	int base;
18	int limit;
19	int interval;
20};
21
22
23static int
24autoscan_exponential_get_params(struct autoscan_exponential_data *data,
25				const char *params)
26{
27	const char *pos;
28
29	if (params == NULL)
30		return -1;
31
32	data->base = atoi(params);
33
34	pos = os_strchr(params, ':');
35	if (pos == NULL)
36		return -1;
37
38	pos++;
39	data->limit = atoi(pos);
40
41	return 0;
42}
43
44
45static void * autoscan_exponential_init(struct wpa_supplicant *wpa_s,
46					const char *params)
47{
48	struct autoscan_exponential_data *data;
49
50	data = os_zalloc(sizeof(struct autoscan_exponential_data));
51	if (data == NULL)
52		return NULL;
53
54	if (autoscan_exponential_get_params(data, params) < 0) {
55		os_free(data);
56		return NULL;
57	}
58
59	wpa_printf(MSG_DEBUG, "autoscan exponential: base exponential is %d "
60		   "and limit is %d", data->base, data->limit);
61
62	data->wpa_s = wpa_s;
63
64	return data;
65}
66
67
68static void autoscan_exponential_deinit(void *priv)
69{
70	struct autoscan_exponential_data *data = priv;
71
72	os_free(data);
73}
74
75
76static int autoscan_exponential_notify_scan(void *priv,
77					    struct wpa_scan_results *scan_res)
78{
79	struct autoscan_exponential_data *data = priv;
80
81	wpa_printf(MSG_DEBUG, "autoscan exponential: scan result "
82		   "notification");
83
84	if (data->interval >= data->limit)
85		return data->limit;
86
87	if (data->interval <= 0)
88		data->interval = data->base;
89	else {
90		data->interval = data->interval * data->base;
91		if (data->interval > data->limit)
92			return data->limit;
93	}
94
95	return data->interval;
96}
97
98
99const struct autoscan_ops autoscan_exponential_ops = {
100	.name = "exponential",
101	.init = autoscan_exponential_init,
102	.deinit = autoscan_exponential_deinit,
103	.notify_scan = autoscan_exponential_notify_scan,
104};
105