usteer: drop usteer_ubus_notify_client_disassoc
[project/usteer.git] / measurement.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
14 *
15 * Copyright (C) 2021 David Bauer <mail@david-bauer.net>
16 */
17
18 #include "usteer.h"
19
20 LIST_HEAD(measurements);
21 static struct usteer_timeout_queue tq;
22
23 void
24 usteer_measurement_report_node_cleanup(struct usteer_node *node)
25 {
26 struct usteer_measurement_report *mr, *tmp;
27
28 list_for_each_entry_safe(mr, tmp, &node->measurements, node_list)
29 usteer_measurement_report_del(mr);
30 }
31
32 void
33 usteer_measurement_report_sta_cleanup(struct sta *sta)
34 {
35 struct usteer_measurement_report *mr, *tmp;
36
37 list_for_each_entry_safe(mr, tmp, &sta->measurements, sta_list)
38 usteer_measurement_report_del(mr);
39 }
40
41 struct usteer_measurement_report *
42 usteer_measurement_report_get(struct sta *sta, struct usteer_node *node, bool create)
43 {
44 struct usteer_measurement_report *mr;
45
46 list_for_each_entry(mr, &sta->measurements, sta_list) {
47 if (mr->node == node)
48 return mr;
49 }
50
51 if (!create)
52 return NULL;
53
54 mr = calloc(1, sizeof(*mr));
55 if (!mr)
56 return NULL;
57
58 /* Set node & add to nodes list */
59 mr->node = node;
60 list_add(&mr->node_list, &node->measurements);
61
62 /* Set sta & add to STAs list */
63 mr->sta = sta;
64 list_add(&mr->sta_list, &sta->measurements);
65
66 /* Add to Measurement list */
67 list_add(&mr->list, &measurements);
68
69 /* Set measurement expiration */
70 usteer_timeout_set(&tq, &mr->timeout, config.measurement_report_timeout);
71
72 return mr;
73 }
74
75 struct usteer_measurement_report *
76 usteer_measurement_report_add(struct sta *sta, struct usteer_node *node,
77 uint8_t rcpi, uint8_t rsni, uint64_t timestamp)
78 {
79 struct usteer_measurement_report *mr = usteer_measurement_report_get(sta, node, true);
80
81 if (!mr)
82 return NULL;
83
84 mr->timestamp = timestamp;
85 mr->rsni = rsni;
86 mr->rcpi = rcpi;
87
88 /* Reset timeout */
89 usteer_timeout_set(&tq, &mr->timeout, config.measurement_report_timeout);
90
91 return mr;
92 }
93
94 void
95 usteer_measurement_report_del(struct usteer_measurement_report *mr)
96 {
97 usteer_timeout_cancel(&tq, &mr->timeout);
98 list_del(&mr->node_list);
99 list_del(&mr->sta_list);
100 list_del(&mr->list);
101 free(mr);
102 }
103
104 static void
105 usteer_measurement_timeout(struct usteer_timeout_queue *q, struct usteer_timeout *t)
106 {
107 struct usteer_measurement_report *mr = container_of(t, struct usteer_measurement_report, timeout);
108
109 usteer_measurement_report_del(mr);
110 }
111
112 static void __usteer_init usteer_measurement_init(void)
113 {
114 usteer_timeout_init(&tq);
115 tq.cb = usteer_measurement_timeout;
116 }