1 /*
2 * BSS Load Element / Channel Utilization
3 * Copyright (c) 2014, Qualcomm Atheros, Inc.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "hostapd.h"
14 #include "bss_load.h"
15 #include "ap_drv_ops.h"
16 #include "beacon.h"
17
18 #ifndef EXT_CODE_CROP
get_bss_load_update_timeout(struct hostapd_data * hapd,unsigned int * sec,unsigned int * usec)19 static int get_bss_load_update_timeout(struct hostapd_data *hapd,
20 unsigned int *sec, unsigned int *usec)
21 {
22 unsigned int update_period = hapd->conf->bss_load_update_period;
23 unsigned int beacon_int = hapd->iconf->beacon_int;
24 unsigned int update_timeout;
25
26 if (!update_period || !beacon_int) {
27 wpa_printf(MSG_ERROR,
28 "BSS Load: Invalid BSS load update configuration (period=%u beacon_int=%u)",
29 update_period, beacon_int);
30 return -1;
31 }
32
33 update_timeout = update_period * beacon_int;
34
35 *sec = ((update_timeout / 1000) * 1024) / 1000;
36 *usec = (update_timeout % 1000) * 1024;
37
38 return 0;
39 }
40
41
update_channel_utilization(void * eloop_data,void * user_data)42 static void update_channel_utilization(void *eloop_data, void *user_data)
43 {
44 struct hostapd_data *hapd = eloop_data;
45 unsigned int sec, usec;
46 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
47 int err;
48 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
49 struct hostapd_iface *iface = hapd->iface;
50
51 if (!(hapd->beacon_set_done && hapd->started))
52 return;
53
54 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
55 err = hostapd_drv_get_survey(hapd, hapd->iface->freq);
56 if (err) {
57 wpa_printf(MSG_ERROR, "BSS Load: Failed to get survey data");
58 return;
59 }
60 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
61
62 ieee802_11_set_beacon(hapd);
63
64 if (get_bss_load_update_timeout(hapd, &sec, &usec) < 0)
65 return;
66
67 if (hapd->conf->chan_util_avg_period) {
68 iface->chan_util_samples_sum += iface->channel_utilization;
69 iface->chan_util_num_sample_periods +=
70 hapd->conf->bss_load_update_period;
71 if (iface->chan_util_num_sample_periods >=
72 hapd->conf->chan_util_avg_period) {
73 iface->chan_util_average =
74 iface->chan_util_samples_sum /
75 (iface->chan_util_num_sample_periods /
76 hapd->conf->bss_load_update_period);
77 iface->chan_util_samples_sum = 0;
78 iface->chan_util_num_sample_periods = 0;
79 }
80 }
81
82 eloop_register_timeout(sec, usec, update_channel_utilization, hapd,
83 NULL);
84 }
85
86
bss_load_update_init(struct hostapd_data * hapd)87 int bss_load_update_init(struct hostapd_data *hapd)
88 {
89 unsigned int sec, usec;
90
91 if (get_bss_load_update_timeout(hapd, &sec, &usec) < 0)
92 return -1;
93
94 eloop_register_timeout(sec, usec, update_channel_utilization, hapd,
95 NULL);
96 return 0;
97 }
98
99
bss_load_update_deinit(struct hostapd_data * hapd)100 void bss_load_update_deinit(struct hostapd_data *hapd)
101 {
102 eloop_cancel_timeout(update_channel_utilization, hapd, NULL);
103 }
104 #endif /* EXT_CODE_CROP */
105