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