Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 1 | /*! \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 | |
| 31 | struct osmo_fsm_inst; |
| 32 | |
| 33 | /*! \defgroup Tdef Tnnn timer configuration |
| 34 | * @{ |
| 35 | * \file tdef.h |
| 36 | */ |
| 37 | |
| 38 | enum 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. */ |
Pau Espin Pedrol | 1b75e4b | 2020-11-04 20:00:38 +0100 | [diff] [blame] | 43 | OSMO_TDEF_US, /*!< microseconds */ |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | extern const struct value_string osmo_tdef_unit_names[]; |
| 47 | /*! \return enum osmo_tdef_unit value as human readable unit letter, or "custom-unit". */ |
| 48 | static inline const char *osmo_tdef_unit_name(enum osmo_tdef_unit val) |
| 49 | { return get_value_string(osmo_tdef_unit_names, val); } |
| 50 | |
| 51 | /*! Define a GSM timer of the form Tnnn, with unit, default value and doc string. |
| 52 | * Typically used as an array with the last entry being left zero-initialized, e.g.: |
| 53 | * |
| 54 | * struct osmo_tdef tdefs[] = { |
| 55 | * { .T=10, .default_val=6, .desc="RR Assignment" }, |
| 56 | * { .T=101, .default_val=10, .desc="inter-BSC Handover MT, HO Request to HO Accept" }, |
| 57 | * { .T=3101, .default_val=3, .desc="RR Immediate Assignment" }, |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 58 | * { .T=-23, .default_val=42, .desc="internal X23 timeout (contrived example)" }, |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 59 | * {} |
| 60 | * }; |
| 61 | * |
| 62 | * Program initialization should call osmo_tdefs_reset() so that all timers return the default_val, until e.g. the VTY |
| 63 | * configuration sets user-defined values (see osmo_tdef_vty_init()). |
| 64 | */ |
| 65 | struct osmo_tdef { |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 66 | /*! T1234 or X1234 number, corresponding to struct osmo_fsm_inst::T. |
| 67 | * Positive values for T are considered to be 3GPP spec compliant and appear in logging and VTY as "T1234", |
| 68 | * while negative values are considered to be Osmocom specific timers, represented in logging and VTY as |
| 69 | * "X1234". Be aware that osmo_tdef_fsm_inst_state_chg() interprets T == 0 as "state without timeout". */ |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 70 | const int T; |
| 71 | /*! Timeout duration (according to unit), default value; type corresponds to osmo_fsm_inst_state_chg()'s |
| 72 | * timeout_secs argument. Note that osmo_fsm_inst_state_chg() clamps the range. */ |
| 73 | const unsigned long default_val; |
| 74 | const enum osmo_tdef_unit unit; |
| 75 | /*! Human readable description. For unit == OSMO_TDEF_CUSTOM, this should include an explanation of the value's |
| 76 | * unit. Best keep this a short one-liner (e.g. for VTY output). */ |
| 77 | const char *desc; |
| 78 | /*! Currently active timeout value, e.g. set by user config. This is the only mutable member: a user may |
| 79 | * configure the timeout value, but neither unit nor any other field. */ |
| 80 | unsigned long val; |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 81 | /*! Minimum timer value (in this tdef unit), checked if set (not zero). */ |
| 82 | unsigned long min_val; |
| 83 | /*! Maximum timer value (in this tdef unit), checked if set (not zero). */ |
| 84 | unsigned long max_val; |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /*! Iterate an array of struct osmo_tdef, the last item should be fully zero, i.e. "{}". |
| 88 | * Example: |
| 89 | * |
| 90 | * struct osmo_tdef *t; |
| 91 | * osmo_tdef_for_each(t, tdefs) { |
| 92 | * printf("%lu %s %s\n", t->val, osmo_tdef_unit_name(t->unit), t->desc); |
| 93 | * } |
| 94 | * |
| 95 | * \param[inout] t A struct osmo_tdef *t used for iteration, will point at the current entry inside the loop scope. |
| 96 | * \param[in] tdefs Array of struct osmo_tdef to iterate, zero-terminated. |
| 97 | */ |
| 98 | #define osmo_tdef_for_each(t, tdefs) \ |
| 99 | for (t = tdefs; t && (t->T || t->default_val || t->desc); t++) |
| 100 | |
| 101 | void osmo_tdefs_reset(struct osmo_tdef *tdefs); |
| 102 | unsigned long osmo_tdef_get(const struct osmo_tdef *tdefs, int T, enum osmo_tdef_unit as_unit, |
Neels Hofmeyr | 989f01c | 2019-08-15 02:52:55 +0200 | [diff] [blame] | 103 | long val_if_not_present); |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 104 | struct osmo_tdef *osmo_tdef_get_entry(struct osmo_tdef *tdefs, int T); |
Neels Hofmeyr | 9655ed5 | 2019-09-11 01:48:11 +0200 | [diff] [blame] | 105 | int osmo_tdef_set(struct osmo_tdef *tdefs, int T, unsigned long val, enum osmo_tdef_unit val_unit); |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 106 | bool osmo_tdef_val_in_range(struct osmo_tdef *tdef, unsigned long new_val); |
| 107 | int osmo_tdef_range_str_buf(char *buf, size_t buf_len, struct osmo_tdef *t); |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 108 | |
| 109 | /*! Using osmo_tdef for osmo_fsm_inst: array entry for a mapping of state numbers to timeout definitions. |
| 110 | * For a usage example, see osmo_tdef_get_state_timeout() and test_tdef_state_timeout() in tdef_test.c. */ |
| 111 | struct osmo_tdef_state_timeout { |
Neels Hofmeyr | 5734bff | 2019-02-21 02:27:48 +0100 | [diff] [blame] | 112 | /*! Timer number to match struct osmo_tdef.T, and to pass to osmo_fsm_inst_state_chg(). Positive values for T |
| 113 | * are considered to be 3GPP spec compliant and appear in logging and VTY as "T1234", while negative values are |
| 114 | * considered to be Osmocom specific timers, represented in logging and VTY as "X1234". */ |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 115 | int T; |
| 116 | /*! If true, call osmo_fsm_inst_state_chg_keep_timer(). |
| 117 | * If T == 0, keep previous T number, otherwise also set fi->T. */ |
| 118 | bool keep_timer; |
| 119 | }; |
| 120 | |
| 121 | const struct osmo_tdef_state_timeout *osmo_tdef_get_state_timeout(uint32_t state, |
| 122 | const struct osmo_tdef_state_timeout *timeouts_array); |
| 123 | |
| 124 | /*! Call osmo_fsm_inst_state_chg() or osmo_fsm_inst_state_chg_keep_timer(), depending on the timeouts_array, tdefs and |
| 125 | * default_timeout. |
| 126 | * |
| 127 | * A T timer configured in sub-second precision is rounded up to the next full second. A timer in unit = |
| 128 | * OSMO_TDEF_CUSTOM is applied as if the unit is in seconds (i.e. this macro does not make sense for custom units!). |
| 129 | * |
| 130 | * See osmo_tdef_get_state_timeout() and osmo_tdef_get(). |
| 131 | * |
| 132 | * If no T timer is defined for the given state (T == 0), invoke the state change without a timeout. |
| 133 | * |
| 134 | * Should a T number be defined in timeouts_array that is not defined in tdefs, use default_timeout (in seconds). If |
| 135 | * default_timeout is negative, a missing T definition in tdefs instead causes a program abort. |
| 136 | * |
| 137 | * This is best used by wrapping this function call in a macro suitable for a specific FSM implementation, which can |
| 138 | * become as short as: my_fsm_state_chg(fi, NEXT_STATE): |
| 139 | * |
| 140 | * #define my_fsm_state_chg(fi, NEXT_STATE) \ |
| 141 | * osmo_tdef_fsm_inst_state_chg(fi, NEXT_STATE, my_fsm_timeouts, global_T_defs, 5) |
| 142 | * |
| 143 | * my_fsm_state_chg(fi, MY_FSM_STATE_1); |
| 144 | * // -> No timeout configured, will enter state without timeout. |
| 145 | * |
| 146 | * my_fsm_state_chg(fi, MY_FSM_STATE_3); |
| 147 | * // T423 configured for this state, will look up T423 in tdefs, or use 5 seconds if unset. |
| 148 | * |
| 149 | * my_fsm_state_chg(fi, MY_FSM_STATE_8); |
| 150 | * // keep_timer == true for this state, will invoke osmo_fsm_inst_state_chg_keep_timer(). |
| 151 | * |
| 152 | * \param[inout] fi osmo_fsm_inst to transition to another state. |
| 153 | * \param[in] state State number to transition to. |
| 154 | * \param[in] timeouts_array Array of struct osmo_tdef_state_timeout[32] to look up state in. |
| 155 | * \param[in] tdefs Array of struct osmo_tdef (last entry zero initialized) to look up T in. |
| 156 | * \param[in] default_timeout If a T is set in timeouts_array, but no timeout value is configured for T, then use this |
| 157 | * default timeout value as fallback, or pass -1 to abort the program. |
| 158 | * \return Return value from osmo_fsm_inst_state_chg() or osmo_fsm_inst_state_chg_keep_timer(). |
| 159 | */ |
| 160 | #define osmo_tdef_fsm_inst_state_chg(fi, state, timeouts_array, tdefs, default_timeout) \ |
| 161 | _osmo_tdef_fsm_inst_state_chg(fi, state, timeouts_array, tdefs, default_timeout, \ |
| 162 | __FILE__, __LINE__) |
| 163 | int _osmo_tdef_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t state, |
| 164 | const struct osmo_tdef_state_timeout *timeouts_array, |
| 165 | const struct osmo_tdef *tdefs, unsigned long default_timeout, |
| 166 | const char *file, int line); |
| 167 | |
| 168 | /*! Manage timer definitions in named groups. |
| 169 | * This should be defined as an array with the final element kept fully zero-initialized, |
| 170 | * to be compatible with osmo_tdef_vty* API. There must not be any tdefs == NULL entries except on the final |
| 171 | * zero-initialized entry. */ |
| 172 | struct osmo_tdef_group { |
| 173 | const char *name; |
| 174 | const char *desc; |
| 175 | struct osmo_tdef *tdefs; |
| 176 | }; |
| 177 | |
| 178 | /*! Iterate an array of struct osmo_tdef_group, the last item should be fully zero, i.e. "{}". |
| 179 | * \param[inout] g A struct osmo_tdef_group *g used for iteration, will point at the current entry inside the loop scope. |
| 180 | * \param[in] tdefs Array of struct osmo_tdef_group to iterate, zero-terminated. |
| 181 | */ |
| 182 | #define osmo_tdef_groups_for_each(g, tdef_groups) \ |
| 183 | for (g = tdef_groups; g && g->tdefs; g++) |
| 184 | |
| 185 | /*! @} */ |