aboutsummaryrefslogtreecommitdiffstats
path: root/wpa_supplicant/autoscan_exponential.c
blob: 424477be8d4332528a16f1c80c22de3f2a518867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * WPA Supplicant - auto scan exponential module
 * Copyright (c) 2012, Intel Corporation. All rights reserved.
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */

#include "includes.h"

#include "common.h"
#include "wpa_supplicant_i.h"
#include "autoscan.h"

struct autoscan_exponential_data {
	struct wpa_supplicant *wpa_s;
	int base;
	int limit;
	int interval;
};


static int
autoscan_exponential_get_params(struct autoscan_exponential_data *data,
				const char *params)
{
	const char *pos;

	if (params == NULL)
		return -1;

	data->base = atoi(params);

	pos = os_strchr(params, ':');
	if (pos == NULL)
		return -1;

	pos++;
	data->limit = atoi(pos);

	return 0;
}


static void * autoscan_exponential_init(struct wpa_supplicant *wpa_s,
					const char *params)
{
	struct autoscan_exponential_data *data;

	data = os_zalloc(sizeof(struct autoscan_exponential_data));
	if (data == NULL)
		return NULL;

	if (autoscan_exponential_get_params(data, params) < 0) {
		os_free(data);
		return NULL;
	}

	wpa_printf(MSG_DEBUG, "autoscan exponential: base exponential is %d "
		   "and limit is %d", data->base, data->limit);

	data->wpa_s = wpa_s;

	return data;
}


static void autoscan_exponential_deinit(void *priv)
{
	struct autoscan_exponential_data *data = priv;

	os_free(data);
}


static int autoscan_exponential_notify_scan(void *priv,
					    struct wpa_scan_results *scan_res)
{
	struct autoscan_exponential_data *data = priv;

	wpa_printf(MSG_DEBUG, "autoscan exponential: scan result "
		   "notification");

	if (data->interval >= data->limit)
		return data->limit;

	if (data->interval <= 0)
		data->interval = data->base;
	else {
		data->interval = data->interval * data->base;
		if (data->interval > data->limit)
			return data->limit;
	}

	return data->interval;
}


const struct autoscan_ops autoscan_exponential_ops = {
	.name = "exponential",
	.init = autoscan_exponential_init,
	.deinit = autoscan_exponential_deinit,
	.notify_scan = autoscan_exponential_notify_scan,
};