blob: 92b71597bfb6d9a3c1970ea38252c71fc93e5bfc [file] [log] [blame]
Neels Hofmeyr0fd615f2019-01-26 20:36:12 +01001/*! \file tdef.h
2 * API 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
14 * it under the terms of the GNU Affero General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (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
21 * GNU Affero General Public License for more details.
22 *
23 * You should have received a copy of the GNU Affero General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26#pragma once
27
28#include <stdint.h>
29#include <osmocom/core/utils.h>
30
31struct osmo_fsm_inst;
32
33/*! \defgroup Tdef Tnnn timer configuration
34 * @{
35 * \file tdef.h
36 */
37
38enum osmo_tdef_unit {
39 OSMO_TDEF_S = 0, /*!< most T are in seconds, keep 0 as default. */
40 OSMO_TDEF_MS, /*!< milliseconds */
41 OSMO_TDEF_M, /*!< minutes */
42 OSMO_TDEF_CUSTOM, /*!< unspecified unit, explained in osmo_tdef.desc. */
43};
44
45extern const struct value_string osmo_tdef_unit_names[];
46/*! \return enum osmo_tdef_unit value as human readable unit letter, or "custom-unit". */
47static inline const char *osmo_tdef_unit_name(enum osmo_tdef_unit val)
48{ return get_value_string(osmo_tdef_unit_names, val); }
49
50/*! Define a GSM timer of the form Tnnn, with unit, default value and doc string.
51 * Typically used as an array with the last entry being left zero-initialized, e.g.:
52 *
53 * struct osmo_tdef tdefs[] = {
54 * { .T=10, .default_val=6, .desc="RR Assignment" },
55 * { .T=101, .default_val=10, .desc="inter-BSC Handover MT, HO Request to HO Accept" },
56 * { .T=3101, .default_val=3, .desc="RR Immediate Assignment" },
57 * {}
58 * };
59 *
60 * Program initialization should call osmo_tdefs_reset() so that all timers return the default_val, until e.g. the VTY
61 * configuration sets user-defined values (see osmo_tdef_vty_init()).
62 */
63struct osmo_tdef {
64 /*! T1234 number; type corresponds to struct osmo_fsm_inst.T. Negative and zero T numbers are actually possible,
65 * but be aware that osmo_tdef_fsm_inst_state_chg() interprets T == 0 as "no timer". */
66 const int T;
67 /*! Timeout duration (according to unit), default value; type corresponds to osmo_fsm_inst_state_chg()'s
68 * timeout_secs argument. Note that osmo_fsm_inst_state_chg() clamps the range. */
69 const unsigned long default_val;
70 const enum osmo_tdef_unit unit;
71 /*! Human readable description. For unit == OSMO_TDEF_CUSTOM, this should include an explanation of the value's
72 * unit. Best keep this a short one-liner (e.g. for VTY output). */
73 const char *desc;
74 /*! Currently active timeout value, e.g. set by user config. This is the only mutable member: a user may
75 * configure the timeout value, but neither unit nor any other field. */
76 unsigned long val;
77};
78
79/*! Iterate an array of struct osmo_tdef, the last item should be fully zero, i.e. "{}".
80 * Example:
81 *
82 * struct osmo_tdef *t;
83 * osmo_tdef_for_each(t, tdefs) {
84 * printf("%lu %s %s\n", t->val, osmo_tdef_unit_name(t->unit), t->desc);
85 * }
86 *
87 * \param[inout] t A struct osmo_tdef *t used for iteration, will point at the current entry inside the loop scope.
88 * \param[in] tdefs Array of struct osmo_tdef to iterate, zero-terminated.
89 */
90#define osmo_tdef_for_each(t, tdefs) \
91 for (t = tdefs; t && (t->T || t->default_val || t->desc); t++)
92
93void osmo_tdefs_reset(struct osmo_tdef *tdefs);
94unsigned long osmo_tdef_get(const struct osmo_tdef *tdefs, int T, enum osmo_tdef_unit as_unit,
95 unsigned long val_if_not_present);
96struct osmo_tdef *osmo_tdef_get_entry(struct osmo_tdef *tdefs, int T);
97
98/*! Using osmo_tdef for osmo_fsm_inst: array entry for a mapping of state numbers to timeout definitions.
99 * For a usage example, see osmo_tdef_get_state_timeout() and test_tdef_state_timeout() in tdef_test.c. */
100struct osmo_tdef_state_timeout {
101 /*! Timer number to match struct osmo_tdef.T, and to pass to osmo_fsm_inst_state_chg(). */
102 int T;
103 /*! If true, call osmo_fsm_inst_state_chg_keep_timer().
104 * If T == 0, keep previous T number, otherwise also set fi->T. */
105 bool keep_timer;
106};
107
108const struct osmo_tdef_state_timeout *osmo_tdef_get_state_timeout(uint32_t state,
109 const struct osmo_tdef_state_timeout *timeouts_array);
110
111/*! Call osmo_fsm_inst_state_chg() or osmo_fsm_inst_state_chg_keep_timer(), depending on the timeouts_array, tdefs and
112 * default_timeout.
113 *
114 * A T timer configured in sub-second precision is rounded up to the next full second. A timer in unit =
115 * OSMO_TDEF_CUSTOM is applied as if the unit is in seconds (i.e. this macro does not make sense for custom units!).
116 *
117 * See osmo_tdef_get_state_timeout() and osmo_tdef_get().
118 *
119 * If no T timer is defined for the given state (T == 0), invoke the state change without a timeout.
120 *
121 * Should a T number be defined in timeouts_array that is not defined in tdefs, use default_timeout (in seconds). If
122 * default_timeout is negative, a missing T definition in tdefs instead causes a program abort.
123 *
124 * This is best used by wrapping this function call in a macro suitable for a specific FSM implementation, which can
125 * become as short as: my_fsm_state_chg(fi, NEXT_STATE):
126 *
127 * #define my_fsm_state_chg(fi, NEXT_STATE) \
128 * osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, my_fsm_timeouts, global_T_defs, 5)
129 *
130 * my_fsm_state_chg(fi, MY_FSM_STATE_1);
131 * // -> No timeout configured, will enter state without timeout.
132 *
133 * my_fsm_state_chg(fi, MY_FSM_STATE_3);
134 * // T423 configured for this state, will look up T423 in tdefs, or use 5 seconds if unset.
135 *
136 * my_fsm_state_chg(fi, MY_FSM_STATE_8);
137 * // keep_timer == true for this state, will invoke osmo_fsm_inst_state_chg_keep_timer().
138 *
139 * \param[inout] fi osmo_fsm_inst to transition to another state.
140 * \param[in] state State number to transition to.
141 * \param[in] timeouts_array Array of struct osmo_tdef_state_timeout[32] to look up state in.
142 * \param[in] tdefs Array of struct osmo_tdef (last entry zero initialized) to look up T in.
143 * \param[in] default_timeout If a T is set in timeouts_array, but no timeout value is configured for T, then use this
144 * default timeout value as fallback, or pass -1 to abort the program.
145 * \return Return value from osmo_fsm_inst_state_chg() or osmo_fsm_inst_state_chg_keep_timer().
146 */
147#define osmo_tdef_fsm_inst_state_chg(fi, state, timeouts_array, tdefs, default_timeout) \
148 _osmo_tdef_fsm_inst_state_chg(fi, state, timeouts_array, tdefs, default_timeout, \
149 __FILE__, __LINE__)
150int _osmo_tdef_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t state,
151 const struct osmo_tdef_state_timeout *timeouts_array,
152 const struct osmo_tdef *tdefs, unsigned long default_timeout,
153 const char *file, int line);
154
155/*! Manage timer definitions in named groups.
156 * This should be defined as an array with the final element kept fully zero-initialized,
157 * to be compatible with osmo_tdef_vty* API. There must not be any tdefs == NULL entries except on the final
158 * zero-initialized entry. */
159struct osmo_tdef_group {
160 const char *name;
161 const char *desc;
162 struct osmo_tdef *tdefs;
163};
164
165/*! Iterate an array of struct osmo_tdef_group, the last item should be fully zero, i.e. "{}".
166 * \param[inout] g A struct osmo_tdef_group *g used for iteration, will point at the current entry inside the loop scope.
167 * \param[in] tdefs Array of struct osmo_tdef_group to iterate, zero-terminated.
168 */
169#define osmo_tdef_groups_for_each(g, tdef_groups) \
170 for (g = tdef_groups; g && g->tdefs; g++)
171
172/*! @} */