blob: 9d5d7363170e06919067367a33e264f9bd467a2b [file] [log] [blame]
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +01001/*! \file tdef.c
2 * Implementation to define Tnnn timers globally and use for FSM state changes.
3 */
4/*
5 * (C) 2018-2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
6 *
7 * All Rights Reserved
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 *
11 * Author: Neels Hofmeyr <neels@hofmeyr.de>
12 *
13 * This program is free software; you can redistribute it and/or modify
Harald Welte581a34d2019-05-27 23:15:28 +020014 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010016 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte581a34d2019-05-27 23:15:28 +020021 * GNU General Public License for more details.
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010022 *
Harald Welte581a34d2019-05-27 23:15:28 +020023 * You should have received a copy of the GNU General Public License
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +010024 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27#include <limits.h>
28
29#include <osmocom/core/fsm.h>
30#include <osmocom/core/tdef.h>
31
32/*! \addtogroup Tdef
33 *
34 * Implementation to define Tnnn timers globally and use for FSM state changes.
35 *
36 * See also \ref Tdef_VTY
37 *
38 * osmo_tdef provides:
39 *
40 * - a list of Tnnnn (GSM) timers with description, unit and default value.
41 * - vty UI to allow users to configure non-default timeouts.
42 * - API to tie T timers to osmo_fsm states and set them on state transitions.
43 *
44 * - a few standard units (minute, second, millisecond) as well as a custom unit
45 * (which relies on the timer's human readable description to indicate the
46 * meaning of the value).
47 * - conversion for standard units: for example, some GSM timers are defined in
48 * minutes, while our FSM definitions need timeouts in seconds. Conversion is
49 * for convenience only and can be easily avoided via the custom unit.
50 *
51 * By keeping separate osmo_tdef arrays, several groups of timers can be kept
52 * separately. The VTY tests in tests/tdef/ showcase different schemes:
53 *
54 * - \ref tests/vty/tdef_vty_test_config_root.c:
55 * Keep several timer definitions in separately named groups: showcase the
56 * osmo_tdef_vty_groups*() API. Each timer group exists exactly once.
57 *
58 * - \ref tests/vty/tdef_vty_test_config_subnode.c:
59 * Keep a single list of timers without separate grouping.
60 * Put this list on a specific subnode below the CONFIG_NODE.
61 * There could be several separate subnodes with timers like this, i.e.
62 * continuing from this example, sets of timers could be separated by placing
63 * timers in specific config subnodes instead of using the global group name.
64 *
65 * - \ref tests/vty/tdef_vty_test_dynamic.c:
66 * Dynamically allocate timer definitions per each new created object.
67 * Thus there can be an arbitrary number of independent timer definitions, one
68 * per allocated object.
69 *
70 * osmo_tdef was introduced because:
71 *
72 * - without osmo_tdef, each invocation of osmo_fsm_inst_state_chg() needs to be
73 * programmed with the right timeout value, for all code paths that invoke this
74 * state change. It is a likely source of errors to get one of them wrong. By
75 * defining a T timer exactly for an FSM state, the caller can merely invoke the
76 * state change and trust on the original state definition to apply the correct
77 * timeout.
78 *
79 * - it is helpful to have a standardized config file UI to provide user
80 * configurable timeouts, instead of inventing new VTY commands for each
81 * separate application of T timer numbers. See \ref tdef_vty.h.
82 *
83 * @{
84 * \file tdef.c
85 */
86
87/*! a = return_val * b. \return 0 if factor is below 1. */
88static unsigned long osmo_tdef_factor(enum osmo_tdef_unit a, enum osmo_tdef_unit b)
89{
90 if (b == a
91 || b == OSMO_TDEF_CUSTOM || a == OSMO_TDEF_CUSTOM)
92 return 1;
93
94 switch (b) {
95 case OSMO_TDEF_MS:
96 switch (a) {
97 case OSMO_TDEF_S:
98 return 1000;
99 case OSMO_TDEF_M:
100 return 60*1000;
101 default:
102 return 0;
103 }
104 case OSMO_TDEF_S:
105 switch (a) {
106 case OSMO_TDEF_M:
107 return 60;
108 default:
109 return 0;
110 }
111 default:
112 return 0;
113 }
114}
115
116/*! \return val in unit to_unit, rounded up to the next integer value and clamped to ULONG_MAX, or 0 if val == 0. */
117static unsigned long osmo_tdef_round(unsigned long val, enum osmo_tdef_unit from_unit, enum osmo_tdef_unit to_unit)
118{
119 unsigned long f;
120 if (!val)
121 return 0;
122
123 f = osmo_tdef_factor(from_unit, to_unit);
124 if (f == 1)
125 return val;
126 if (f < 1) {
127 f = osmo_tdef_factor(to_unit, from_unit);
128 return (val / f) + (val % f? 1 : 0);
129 }
130 /* range checking */
131 if (f > (ULONG_MAX / val))
132 return ULONG_MAX;
133 return val * f;
134}
135
136/*! Set all osmo_tdef values to the default_val.
137 * It is convenient to define a tdefs array by setting only the default_val, and calling osmo_tdefs_reset() once for
138 * program startup. (See also osmo_tdef_vty_init())
139 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
140 */
141void osmo_tdefs_reset(struct osmo_tdef *tdefs)
142{
143 struct osmo_tdef *t;
144 osmo_tdef_for_each(t, tdefs)
145 t->val = t->default_val;
146}
147
148/*! Return the value of a T timer from a list of osmo_tdef, in the given unit.
149 * If no such timer is defined, return the default value passed, or abort the program if default < 0.
150 *
151 * Round up any value match as_unit: 1100 ms as OSMO_TDEF_S becomes 2 seconds, as OSMO_TDEF_M becomes one minute.
152 * However, always return a value of zero as zero (0 ms as OSMO_TDEF_M still is 0 m).
153 *
154 * Range: even though the value range is unsigned long here, in practice, using ULONG_MAX as value for a timeout in
155 * seconds may actually wrap to negative or low timeout values (e.g. in struct timeval). It is recommended to stay below
156 * INT_MAX seconds. See also osmo_fsm_inst_state_chg().
157 *
158 * Usage example:
159 *
160 * struct osmo_tdef global_T_defs[] = {
161 * { .T=7, .default_val=50, .desc="Water Boiling Timeout" }, // default is .unit=OSMO_TDEF_S == 0
162 * { .T=8, .default_val=300, .desc="Tea brewing" },
163 * { .T=9, .default_val=5, .unit=OSMO_TDEF_M, .desc="Let tea cool down before drinking" },
164 * { .T=10, .default_val=20, .unit=OSMO_TDEF_M, .desc="Forgot to drink tea while it's warm" },
165 * {} // <-- important! last entry shall be zero
166 * };
167 * osmo_tdefs_reset(global_T_defs); // make all values the default
168 * osmo_tdef_vty_init(global_T_defs, CONFIG_NODE);
169 *
170 * val = osmo_tdef_get(global_T_defs, 7, OSMO_TDEF_S, -1); // -> 50
171 * sleep(val);
172 *
173 * val = osmo_tdef_get(global_T_defs, 7, OSMO_TDEF_M, -1); // 50 seconds becomes 1 minute -> 1
174 * sleep_minutes(val);
175 *
176 * val = osmo_tdef_get(global_T_defs, 99, OSMO_TDEF_S, 3); // not defined, returns 3
177 *
178 * val = osmo_tdef_get(global_T_defs, 99, OSMO_TDEF_S, -1); // not defined, program aborts!
179 *
180 * \param[in] tdefs Array of timer definitions, last entry must be fully zero initialized.
181 * \param[in] T Timer number to get the value for.
182 * \param[in] as_unit Return timeout value in this unit.
183 * \param[in] val_if_not_present Fallback value to return if no timeout is defined.
184 * \return Timeout value in the unit given by as_unit, rounded up if necessary, or val_if_not_present.
185 */
186unsigned long osmo_tdef_get(const struct osmo_tdef *tdefs, int T, enum osmo_tdef_unit as_unit, unsigned long val_if_not_present)
187{
188 const struct osmo_tdef *t = osmo_tdef_get_entry((struct osmo_tdef*)tdefs, T);
189 if (!t) {
190 OSMO_ASSERT(val_if_not_present >= 0);
191 return val_if_not_present;
192 }
193 return osmo_tdef_round(t->val, t->unit, as_unit);
194}
195
196/*! Find tdef entry matching T.
197 * This is useful for manipulation, which is usually limited to the VTY configuration. To retrieve a timeout value,
198 * most callers probably should use osmo_tdef_get() instead.
199 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
200 * \param[in] T Timer number to get the entry for.
201 * \return osmo_tdef entry matching T in given array, or NULL if no match is found.
202 */
203struct osmo_tdef *osmo_tdef_get_entry(struct osmo_tdef *tdefs, int T)
204{
205 struct osmo_tdef *t;
206 osmo_tdef_for_each(t, tdefs) {
207 if (t->T == T)
208 return t;
209 }
210 return NULL;
211}
212
213/*! Using osmo_tdef for osmo_fsm_inst: find a given state's osmo_tdef_state_timeout entry.
214 *
215 * The timeouts_array shall contain exactly 32 elements, regardless whether only some of them are actually populated
216 * with nonzero values. 32 corresponds to the number of states allowed by the osmo_fsm_* API. Lookup is by array index.
217 * Not populated entries imply a state change invocation without timeout.
218 *
219 * For example:
220 *
221 * struct osmo_tdef_state_timeout my_fsm_timeouts[32] = {
222 * [MY_FSM_STATE_3] = { .T = 423 }, // look up timeout configured for T423
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100223 * [MY_FSM_STATE_7] = { .keep_timer = true, .T = 235 }, // keep previous timer if running, or start T235
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100224 * [MY_FSM_STATE_8] = { .keep_timer = true }, // keep previous state's T number, continue timeout.
225 * // any state that is omitted will remain zero == no timeout
226 * };
227 * osmo_tdef_get_state_timeout(MY_FSM_STATE_0, &my_fsm_timeouts) -> NULL,
228 * osmo_tdef_get_state_timeout(MY_FSM_STATE_7, &my_fsm_timeouts) -> { .T = 235 }
229 *
230 * The intention is then to obtain the timer like osmo_tdef_get(global_T_defs, T=235); see also
231 * fsm_inst_state_chg_T() below.
232 *
233 * \param[in] state State constant to look up.
234 * \param[in] timeouts_array Array[32] of struct osmo_tdef_state_timeout defining which timer number to use per state.
235 * \return A struct osmo_tdef_state_timeout entry, or NULL if that entry is zero initialized.
236 */
237const struct osmo_tdef_state_timeout *osmo_tdef_get_state_timeout(uint32_t state, const struct osmo_tdef_state_timeout *timeouts_array)
238{
239 const struct osmo_tdef_state_timeout *t;
240 OSMO_ASSERT(state < 32);
241 t = &timeouts_array[state];
242 if (!t->keep_timer && !t->T)
243 return NULL;
244 return t;
245}
246
247/*! See invocation macro osmo_tdef_fsm_inst_state_chg() instead.
248 * \param[in] file Source file name, like __FILE__.
249 * \param[in] line Source file line number, like __LINE__.
250 */
251int _osmo_tdef_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t state,
252 const struct osmo_tdef_state_timeout *timeouts_array,
253 const struct osmo_tdef *tdefs, unsigned long default_timeout,
254 const char *file, int line)
255{
256 const struct osmo_tdef_state_timeout *t = osmo_tdef_get_state_timeout(state, timeouts_array);
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100257 unsigned long val = 0;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100258
259 /* No timeout defined for this state? */
260 if (!t)
261 return _osmo_fsm_inst_state_chg(fi, state, 0, 0, file, line);
262
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100263 if (t->T)
264 val = osmo_tdef_get(tdefs, t->T, OSMO_TDEF_S, default_timeout);
265
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100266 if (t->keep_timer) {
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100267 if (t->T)
268 return _osmo_fsm_inst_state_chg_keep_or_start_timer(fi, state, val, t->T, file, line);
269 else
270 return _osmo_fsm_inst_state_chg_keep_timer(fi, state, file, line);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100271 }
272
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100273 /* val is always initialized here, because if t->keep_timer is false, t->T must be != 0.
274 * Otherwise osmo_tdef_get_state_timeout() would have returned NULL. */
275 OSMO_ASSERT(t->T);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100276 return _osmo_fsm_inst_state_chg(fi, state, val, t->T, file, line);
277}
278
279const struct value_string osmo_tdef_unit_names[] = {
280 { OSMO_TDEF_S, "s" },
281 { OSMO_TDEF_MS, "ms" },
282 { OSMO_TDEF_M, "m" },
283 { OSMO_TDEF_CUSTOM, "custom-unit" },
284 {}
285};
286
287/*! @} */