blob: 94d987fafded951dacba931731a684c0066e1164 [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
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200139 * program startup. (See also osmo_tdef_vty_init()).
140 * During call to this function, default values are verified to be inside valid range; process is aborted otherwise.
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100141 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
142 */
143void osmo_tdefs_reset(struct osmo_tdef *tdefs)
144{
145 struct osmo_tdef *t;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200146 osmo_tdef_for_each(t, tdefs) {
147 if (!osmo_tdef_val_in_range(t, t->default_val)) {
148 char range_str[64];
149 osmo_tdef_range_str_buf(range_str, sizeof(range_str), t);
150 osmo_panic("%s:%d Timer " OSMO_T_FMT " contains default value %lu not in range %s\n",
151 __FILE__, __LINE__, OSMO_T_FMT_ARGS(t->T), t->default_val, range_str);
152 }
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100153 t->val = t->default_val;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200154 }
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100155}
156
157/*! Return the value of a T timer from a list of osmo_tdef, in the given unit.
158 * If no such timer is defined, return the default value passed, or abort the program if default < 0.
159 *
160 * Round up any value match as_unit: 1100 ms as OSMO_TDEF_S becomes 2 seconds, as OSMO_TDEF_M becomes one minute.
161 * However, always return a value of zero as zero (0 ms as OSMO_TDEF_M still is 0 m).
162 *
163 * Range: even though the value range is unsigned long here, in practice, using ULONG_MAX as value for a timeout in
164 * seconds may actually wrap to negative or low timeout values (e.g. in struct timeval). It is recommended to stay below
165 * INT_MAX seconds. See also osmo_fsm_inst_state_chg().
166 *
167 * Usage example:
168 *
169 * struct osmo_tdef global_T_defs[] = {
170 * { .T=7, .default_val=50, .desc="Water Boiling Timeout" }, // default is .unit=OSMO_TDEF_S == 0
171 * { .T=8, .default_val=300, .desc="Tea brewing" },
172 * { .T=9, .default_val=5, .unit=OSMO_TDEF_M, .desc="Let tea cool down before drinking" },
173 * { .T=10, .default_val=20, .unit=OSMO_TDEF_M, .desc="Forgot to drink tea while it's warm" },
174 * {} // <-- important! last entry shall be zero
175 * };
176 * osmo_tdefs_reset(global_T_defs); // make all values the default
177 * osmo_tdef_vty_init(global_T_defs, CONFIG_NODE);
178 *
179 * val = osmo_tdef_get(global_T_defs, 7, OSMO_TDEF_S, -1); // -> 50
180 * sleep(val);
181 *
182 * val = osmo_tdef_get(global_T_defs, 7, OSMO_TDEF_M, -1); // 50 seconds becomes 1 minute -> 1
183 * sleep_minutes(val);
184 *
185 * val = osmo_tdef_get(global_T_defs, 99, OSMO_TDEF_S, 3); // not defined, returns 3
186 *
187 * val = osmo_tdef_get(global_T_defs, 99, OSMO_TDEF_S, -1); // not defined, program aborts!
188 *
189 * \param[in] tdefs Array of timer definitions, last entry must be fully zero initialized.
190 * \param[in] T Timer number to get the value for.
191 * \param[in] as_unit Return timeout value in this unit.
192 * \param[in] val_if_not_present Fallback value to return if no timeout is defined.
193 * \return Timeout value in the unit given by as_unit, rounded up if necessary, or val_if_not_present.
194 */
Neels Hofmeyr989f01c2019-08-15 02:52:55 +0200195unsigned 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 +0100196{
197 const struct osmo_tdef *t = osmo_tdef_get_entry((struct osmo_tdef*)tdefs, T);
198 if (!t) {
Neels Hofmeyr989f01c2019-08-15 02:52:55 +0200199 OSMO_ASSERT(val_if_not_present >= 0);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100200 return val_if_not_present;
201 }
202 return osmo_tdef_round(t->val, t->unit, as_unit);
203}
204
205/*! Find tdef entry matching T.
206 * This is useful for manipulation, which is usually limited to the VTY configuration. To retrieve a timeout value,
207 * most callers probably should use osmo_tdef_get() instead.
208 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
209 * \param[in] T Timer number to get the entry for.
210 * \return osmo_tdef entry matching T in given array, or NULL if no match is found.
211 */
212struct osmo_tdef *osmo_tdef_get_entry(struct osmo_tdef *tdefs, int T)
213{
214 struct osmo_tdef *t;
215 osmo_tdef_for_each(t, tdefs) {
216 if (t->T == T)
217 return t;
218 }
219 return NULL;
220}
221
Pau Espin Pedrol77cd10f2019-09-05 13:30:48 +0200222/*! Set value in entry matching T, converting val from val_unit to unit of T.
Neels Hofmeyr9655ed52019-09-11 01:48:11 +0200223 * The converted value is rounded up to the next integer value of T's unit and clamped to ULONG_MAX, or 0 if val == 0.
Pau Espin Pedrol77cd10f2019-09-05 13:30:48 +0200224 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
225 * \param[in] T Timer number to set the value for.
226 * \param[in] val The new timer value to set.
227 * \param[in] val_unit Units of value in parameter val.
228 * \return 0 on success, negative on error.
229 */
230int osmo_tdef_set(struct osmo_tdef *tdefs, int T, unsigned long val, enum osmo_tdef_unit val_unit)
231{
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200232 unsigned long new_val;
Neels Hofmeyr9655ed52019-09-11 01:48:11 +0200233 struct osmo_tdef *t = osmo_tdef_get_entry(tdefs, T);
234 if (!t)
235 return -EEXIST;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200236
237 new_val = osmo_tdef_round(val, val_unit, t->unit);
238 if (!osmo_tdef_val_in_range(t, new_val))
239 return -ERANGE;
240
241 t->val = new_val;
Neels Hofmeyr9655ed52019-09-11 01:48:11 +0200242 return 0;
Pau Espin Pedrol77cd10f2019-09-05 13:30:48 +0200243}
244
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200245/*! Check if value new_val is in range of valid possible values for timer entry tdef.
246 * \param[in] tdef Timer entry from a timer definition table.
247 * \param[in] new_val The value whose validity to check, in units as per this timer entry.
248 * \return true if inside range, false otherwise.
249 */
250bool osmo_tdef_val_in_range(struct osmo_tdef *tdef, unsigned long new_val)
251{
252 return new_val >= tdef->min_val && (!tdef->max_val || new_val <= tdef->max_val);
253}
254
255/*! Write string representation of osmo_tdef range into buf.
256 * \param[in] buf The buffer where the string representation is stored.
257 * \param[in] buf_len Length of buffer in bytes.
258 * \param[in] tdef Timer entry from a timer definition table.
259 * \return The number of characters printed on success, negative on error. See snprintf().
260 */
261int osmo_tdef_range_str_buf(char *buf, size_t buf_len, struct osmo_tdef *t)
262{
263 int ret, len = 0, offset = 0, rem = buf_len;
264
265 buf[0] = '\0';
266 ret = snprintf(buf + offset, rem, "[%lu .. ", t->min_val);
267 if (ret < 0)
268 return ret;
269 OSMO_SNPRINTF_RET(ret, rem, offset, len);
270
271 if (t->max_val)
272 ret = snprintf(buf + offset, rem, "%lu]", t->max_val);
273 else
274 ret = snprintf(buf + offset, rem, "inf]");
275 if (ret < 0)
276 return ret;
277 OSMO_SNPRINTF_RET(ret, rem, offset, len);
278 return ret;
279}
280
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100281/*! Using osmo_tdef for osmo_fsm_inst: find a given state's osmo_tdef_state_timeout entry.
282 *
283 * The timeouts_array shall contain exactly 32 elements, regardless whether only some of them are actually populated
284 * with nonzero values. 32 corresponds to the number of states allowed by the osmo_fsm_* API. Lookup is by array index.
285 * Not populated entries imply a state change invocation without timeout.
286 *
287 * For example:
288 *
289 * struct osmo_tdef_state_timeout my_fsm_timeouts[32] = {
290 * [MY_FSM_STATE_3] = { .T = 423 }, // look up timeout configured for T423
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100291 * [MY_FSM_STATE_7] = { .keep_timer = true, .T = 235 }, // keep previous timer if running, or start T235
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100292 * [MY_FSM_STATE_8] = { .keep_timer = true }, // keep previous state's T number, continue timeout.
293 * // any state that is omitted will remain zero == no timeout
294 * };
295 * osmo_tdef_get_state_timeout(MY_FSM_STATE_0, &my_fsm_timeouts) -> NULL,
296 * osmo_tdef_get_state_timeout(MY_FSM_STATE_7, &my_fsm_timeouts) -> { .T = 235 }
297 *
298 * The intention is then to obtain the timer like osmo_tdef_get(global_T_defs, T=235); see also
299 * fsm_inst_state_chg_T() below.
300 *
301 * \param[in] state State constant to look up.
302 * \param[in] timeouts_array Array[32] of struct osmo_tdef_state_timeout defining which timer number to use per state.
303 * \return A struct osmo_tdef_state_timeout entry, or NULL if that entry is zero initialized.
304 */
305const struct osmo_tdef_state_timeout *osmo_tdef_get_state_timeout(uint32_t state, const struct osmo_tdef_state_timeout *timeouts_array)
306{
307 const struct osmo_tdef_state_timeout *t;
308 OSMO_ASSERT(state < 32);
309 t = &timeouts_array[state];
310 if (!t->keep_timer && !t->T)
311 return NULL;
312 return t;
313}
314
315/*! See invocation macro osmo_tdef_fsm_inst_state_chg() instead.
316 * \param[in] file Source file name, like __FILE__.
317 * \param[in] line Source file line number, like __LINE__.
318 */
319int _osmo_tdef_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t state,
320 const struct osmo_tdef_state_timeout *timeouts_array,
321 const struct osmo_tdef *tdefs, unsigned long default_timeout,
322 const char *file, int line)
323{
324 const struct osmo_tdef_state_timeout *t = osmo_tdef_get_state_timeout(state, timeouts_array);
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100325 unsigned long val = 0;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100326
327 /* No timeout defined for this state? */
328 if (!t)
329 return _osmo_fsm_inst_state_chg(fi, state, 0, 0, file, line);
330
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100331 if (t->T)
332 val = osmo_tdef_get(tdefs, t->T, OSMO_TDEF_S, default_timeout);
333
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100334 if (t->keep_timer) {
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100335 if (t->T)
336 return _osmo_fsm_inst_state_chg_keep_or_start_timer(fi, state, val, t->T, file, line);
337 else
338 return _osmo_fsm_inst_state_chg_keep_timer(fi, state, file, line);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100339 }
340
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100341 /* val is always initialized here, because if t->keep_timer is false, t->T must be != 0.
342 * Otherwise osmo_tdef_get_state_timeout() would have returned NULL. */
343 OSMO_ASSERT(t->T);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100344 return _osmo_fsm_inst_state_chg(fi, state, val, t->T, file, line);
345}
346
347const struct value_string osmo_tdef_unit_names[] = {
348 { OSMO_TDEF_S, "s" },
349 { OSMO_TDEF_MS, "ms" },
350 { OSMO_TDEF_M, "m" },
351 { OSMO_TDEF_CUSTOM, "custom-unit" },
352 {}
353};
354
355/*! @} */