blob: 897a92fde239139a9bb51fc641658099f3065542 [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) {
Pau Espin Pedrol1b75e4b2020-11-04 20:00:38 +010096 case OSMO_TDEF_US:
97 switch (a) {
98 case OSMO_TDEF_MS:
99 return 1000;
100 case OSMO_TDEF_S:
101 return 1000*1000;
102 case OSMO_TDEF_M:
103 return 60*1000*1000;
104 default:
105 return 0;
106 }
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100107 case OSMO_TDEF_MS:
108 switch (a) {
109 case OSMO_TDEF_S:
110 return 1000;
111 case OSMO_TDEF_M:
112 return 60*1000;
113 default:
114 return 0;
115 }
116 case OSMO_TDEF_S:
117 switch (a) {
118 case OSMO_TDEF_M:
119 return 60;
120 default:
121 return 0;
122 }
123 default:
124 return 0;
125 }
126}
127
128/*! \return val in unit to_unit, rounded up to the next integer value and clamped to ULONG_MAX, or 0 if val == 0. */
129static unsigned long osmo_tdef_round(unsigned long val, enum osmo_tdef_unit from_unit, enum osmo_tdef_unit to_unit)
130{
131 unsigned long f;
132 if (!val)
133 return 0;
134
135 f = osmo_tdef_factor(from_unit, to_unit);
136 if (f == 1)
137 return val;
138 if (f < 1) {
139 f = osmo_tdef_factor(to_unit, from_unit);
140 return (val / f) + (val % f? 1 : 0);
141 }
142 /* range checking */
143 if (f > (ULONG_MAX / val))
144 return ULONG_MAX;
145 return val * f;
146}
147
148/*! Set all osmo_tdef values to the default_val.
149 * 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 +0200150 * program startup. (See also osmo_tdef_vty_init()).
151 * 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 +0100152 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
153 */
154void osmo_tdefs_reset(struct osmo_tdef *tdefs)
155{
156 struct osmo_tdef *t;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200157 osmo_tdef_for_each(t, tdefs) {
158 if (!osmo_tdef_val_in_range(t, t->default_val)) {
159 char range_str[64];
160 osmo_tdef_range_str_buf(range_str, sizeof(range_str), t);
161 osmo_panic("%s:%d Timer " OSMO_T_FMT " contains default value %lu not in range %s\n",
162 __FILE__, __LINE__, OSMO_T_FMT_ARGS(t->T), t->default_val, range_str);
163 }
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100164 t->val = t->default_val;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200165 }
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100166}
167
168/*! Return the value of a T timer from a list of osmo_tdef, in the given unit.
169 * If no such timer is defined, return the default value passed, or abort the program if default < 0.
170 *
171 * Round up any value match as_unit: 1100 ms as OSMO_TDEF_S becomes 2 seconds, as OSMO_TDEF_M becomes one minute.
172 * However, always return a value of zero as zero (0 ms as OSMO_TDEF_M still is 0 m).
173 *
174 * Range: even though the value range is unsigned long here, in practice, using ULONG_MAX as value for a timeout in
175 * seconds may actually wrap to negative or low timeout values (e.g. in struct timeval). It is recommended to stay below
176 * INT_MAX seconds. See also osmo_fsm_inst_state_chg().
177 *
178 * Usage example:
179 *
180 * struct osmo_tdef global_T_defs[] = {
181 * { .T=7, .default_val=50, .desc="Water Boiling Timeout" }, // default is .unit=OSMO_TDEF_S == 0
182 * { .T=8, .default_val=300, .desc="Tea brewing" },
183 * { .T=9, .default_val=5, .unit=OSMO_TDEF_M, .desc="Let tea cool down before drinking" },
184 * { .T=10, .default_val=20, .unit=OSMO_TDEF_M, .desc="Forgot to drink tea while it's warm" },
185 * {} // <-- important! last entry shall be zero
186 * };
187 * osmo_tdefs_reset(global_T_defs); // make all values the default
188 * osmo_tdef_vty_init(global_T_defs, CONFIG_NODE);
189 *
190 * val = osmo_tdef_get(global_T_defs, 7, OSMO_TDEF_S, -1); // -> 50
191 * sleep(val);
192 *
193 * val = osmo_tdef_get(global_T_defs, 7, OSMO_TDEF_M, -1); // 50 seconds becomes 1 minute -> 1
194 * sleep_minutes(val);
195 *
196 * val = osmo_tdef_get(global_T_defs, 99, OSMO_TDEF_S, 3); // not defined, returns 3
197 *
198 * val = osmo_tdef_get(global_T_defs, 99, OSMO_TDEF_S, -1); // not defined, program aborts!
199 *
200 * \param[in] tdefs Array of timer definitions, last entry must be fully zero initialized.
201 * \param[in] T Timer number to get the value for.
202 * \param[in] as_unit Return timeout value in this unit.
203 * \param[in] val_if_not_present Fallback value to return if no timeout is defined.
204 * \return Timeout value in the unit given by as_unit, rounded up if necessary, or val_if_not_present.
205 */
Neels Hofmeyr989f01c2019-08-15 02:52:55 +0200206unsigned 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 +0100207{
208 const struct osmo_tdef *t = osmo_tdef_get_entry((struct osmo_tdef*)tdefs, T);
209 if (!t) {
Neels Hofmeyr989f01c2019-08-15 02:52:55 +0200210 OSMO_ASSERT(val_if_not_present >= 0);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100211 return val_if_not_present;
212 }
213 return osmo_tdef_round(t->val, t->unit, as_unit);
214}
215
216/*! Find tdef entry matching T.
217 * This is useful for manipulation, which is usually limited to the VTY configuration. To retrieve a timeout value,
218 * most callers probably should use osmo_tdef_get() instead.
219 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
220 * \param[in] T Timer number to get the entry for.
221 * \return osmo_tdef entry matching T in given array, or NULL if no match is found.
222 */
223struct osmo_tdef *osmo_tdef_get_entry(struct osmo_tdef *tdefs, int T)
224{
225 struct osmo_tdef *t;
226 osmo_tdef_for_each(t, tdefs) {
227 if (t->T == T)
228 return t;
229 }
230 return NULL;
231}
232
Pau Espin Pedrol77cd10f2019-09-05 13:30:48 +0200233/*! Set value in entry matching T, converting val from val_unit to unit of T.
Neels Hofmeyr9655ed52019-09-11 01:48:11 +0200234 * 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 +0200235 * \param[in] tdefs Array of timer definitions, last entry being fully zero.
236 * \param[in] T Timer number to set the value for.
237 * \param[in] val The new timer value to set.
238 * \param[in] val_unit Units of value in parameter val.
239 * \return 0 on success, negative on error.
240 */
241int osmo_tdef_set(struct osmo_tdef *tdefs, int T, unsigned long val, enum osmo_tdef_unit val_unit)
242{
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200243 unsigned long new_val;
Neels Hofmeyr9655ed52019-09-11 01:48:11 +0200244 struct osmo_tdef *t = osmo_tdef_get_entry(tdefs, T);
245 if (!t)
246 return -EEXIST;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200247
248 new_val = osmo_tdef_round(val, val_unit, t->unit);
249 if (!osmo_tdef_val_in_range(t, new_val))
250 return -ERANGE;
251
252 t->val = new_val;
Neels Hofmeyr9655ed52019-09-11 01:48:11 +0200253 return 0;
Pau Espin Pedrol77cd10f2019-09-05 13:30:48 +0200254}
255
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200256/*! Check if value new_val is in range of valid possible values for timer entry tdef.
257 * \param[in] tdef Timer entry from a timer definition table.
258 * \param[in] new_val The value whose validity to check, in units as per this timer entry.
259 * \return true if inside range, false otherwise.
260 */
261bool osmo_tdef_val_in_range(struct osmo_tdef *tdef, unsigned long new_val)
262{
263 return new_val >= tdef->min_val && (!tdef->max_val || new_val <= tdef->max_val);
264}
265
266/*! Write string representation of osmo_tdef range into buf.
267 * \param[in] buf The buffer where the string representation is stored.
268 * \param[in] buf_len Length of buffer in bytes.
269 * \param[in] tdef Timer entry from a timer definition table.
Pau Espin Pedrolc85f7732019-10-11 17:04:15 +0200270 * \return The number of characters printed on success (or number of characters
271 * which would have been written to the final string if enough space
272 * had been available), negative on error. See snprintf().
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200273 */
274int osmo_tdef_range_str_buf(char *buf, size_t buf_len, struct osmo_tdef *t)
275{
276 int ret, len = 0, offset = 0, rem = buf_len;
277
278 buf[0] = '\0';
279 ret = snprintf(buf + offset, rem, "[%lu .. ", t->min_val);
280 if (ret < 0)
281 return ret;
282 OSMO_SNPRINTF_RET(ret, rem, offset, len);
283
284 if (t->max_val)
285 ret = snprintf(buf + offset, rem, "%lu]", t->max_val);
286 else
287 ret = snprintf(buf + offset, rem, "inf]");
288 if (ret < 0)
289 return ret;
290 OSMO_SNPRINTF_RET(ret, rem, offset, len);
Pau Espin Pedrolc85f7732019-10-11 17:04:15 +0200291 return len;
Pau Espin Pedrol0cbe8f02019-09-17 13:13:52 +0200292}
293
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100294/*! Using osmo_tdef for osmo_fsm_inst: find a given state's osmo_tdef_state_timeout entry.
295 *
296 * The timeouts_array shall contain exactly 32 elements, regardless whether only some of them are actually populated
297 * with nonzero values. 32 corresponds to the number of states allowed by the osmo_fsm_* API. Lookup is by array index.
298 * Not populated entries imply a state change invocation without timeout.
299 *
300 * For example:
301 *
302 * struct osmo_tdef_state_timeout my_fsm_timeouts[32] = {
303 * [MY_FSM_STATE_3] = { .T = 423 }, // look up timeout configured for T423
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100304 * [MY_FSM_STATE_7] = { .keep_timer = true, .T = 235 }, // keep previous timer if running, or start T235
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100305 * [MY_FSM_STATE_8] = { .keep_timer = true }, // keep previous state's T number, continue timeout.
306 * // any state that is omitted will remain zero == no timeout
307 * };
308 * osmo_tdef_get_state_timeout(MY_FSM_STATE_0, &my_fsm_timeouts) -> NULL,
309 * osmo_tdef_get_state_timeout(MY_FSM_STATE_7, &my_fsm_timeouts) -> { .T = 235 }
310 *
311 * The intention is then to obtain the timer like osmo_tdef_get(global_T_defs, T=235); see also
312 * fsm_inst_state_chg_T() below.
313 *
314 * \param[in] state State constant to look up.
315 * \param[in] timeouts_array Array[32] of struct osmo_tdef_state_timeout defining which timer number to use per state.
316 * \return A struct osmo_tdef_state_timeout entry, or NULL if that entry is zero initialized.
317 */
318const struct osmo_tdef_state_timeout *osmo_tdef_get_state_timeout(uint32_t state, const struct osmo_tdef_state_timeout *timeouts_array)
319{
320 const struct osmo_tdef_state_timeout *t;
321 OSMO_ASSERT(state < 32);
322 t = &timeouts_array[state];
323 if (!t->keep_timer && !t->T)
324 return NULL;
325 return t;
326}
327
328/*! See invocation macro osmo_tdef_fsm_inst_state_chg() instead.
329 * \param[in] file Source file name, like __FILE__.
330 * \param[in] line Source file line number, like __LINE__.
331 */
332int _osmo_tdef_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t state,
333 const struct osmo_tdef_state_timeout *timeouts_array,
334 const struct osmo_tdef *tdefs, unsigned long default_timeout,
335 const char *file, int line)
336{
337 const struct osmo_tdef_state_timeout *t = osmo_tdef_get_state_timeout(state, timeouts_array);
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100338 unsigned long val = 0;
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100339
340 /* No timeout defined for this state? */
341 if (!t)
342 return _osmo_fsm_inst_state_chg(fi, state, 0, 0, file, line);
343
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100344 if (t->T)
345 val = osmo_tdef_get(tdefs, t->T, OSMO_TDEF_S, default_timeout);
346
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100347 if (t->keep_timer) {
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100348 if (t->T)
349 return _osmo_fsm_inst_state_chg_keep_or_start_timer(fi, state, val, t->T, file, line);
350 else
351 return _osmo_fsm_inst_state_chg_keep_timer(fi, state, file, line);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100352 }
353
Neels Hofmeyrd4b79c82019-03-06 05:43:23 +0100354 /* val is always initialized here, because if t->keep_timer is false, t->T must be != 0.
355 * Otherwise osmo_tdef_get_state_timeout() would have returned NULL. */
356 OSMO_ASSERT(t->T);
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100357 return _osmo_fsm_inst_state_chg(fi, state, val, t->T, file, line);
358}
359
360const struct value_string osmo_tdef_unit_names[] = {
361 { OSMO_TDEF_S, "s" },
362 { OSMO_TDEF_MS, "ms" },
363 { OSMO_TDEF_M, "m" },
364 { OSMO_TDEF_CUSTOM, "custom-unit" },
Pau Espin Pedrol1b75e4b2020-11-04 20:00:38 +0100365 { OSMO_TDEF_US, "us" },
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +0100366 {}
367};
368
369/*! @} */