Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 1 | /*! \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 Welte | 581a34d | 2019-05-27 23:15:28 +0200 | [diff] [blame] | 14 | * 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 Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 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 |
Harald Welte | 581a34d | 2019-05-27 23:15:28 +0200 | [diff] [blame] | 21 | * GNU General Public License for more details. |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 22 | * |
Harald Welte | 581a34d | 2019-05-27 23:15:28 +0200 | [diff] [blame] | 23 | * You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | */ |
| 26 | |
| 27 | #include <limits.h> |
Pau Espin Pedrol | 77cd10f | 2019-09-05 13:30:48 +0200 | [diff] [blame] | 28 | #include <errno.h> |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 29 | |
| 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. */ |
| 89 | static 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. */ |
| 118 | static 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 Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 139 | * 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 Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 141 | * \param[in] tdefs Array of timer definitions, last entry being fully zero. |
| 142 | */ |
| 143 | void osmo_tdefs_reset(struct osmo_tdef *tdefs) |
| 144 | { |
| 145 | struct osmo_tdef *t; |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 146 | 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 Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 153 | t->val = t->default_val; |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 154 | } |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 155 | } |
| 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 Hofmeyr | 989f01c | 2019-08-15 02:52:55 +0200 | [diff] [blame] | 195 | unsigned long osmo_tdef_get(const struct osmo_tdef *tdefs, int T, enum osmo_tdef_unit as_unit, long val_if_not_present) |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 196 | { |
| 197 | const struct osmo_tdef *t = osmo_tdef_get_entry((struct osmo_tdef*)tdefs, T); |
| 198 | if (!t) { |
Neels Hofmeyr | 989f01c | 2019-08-15 02:52:55 +0200 | [diff] [blame] | 199 | OSMO_ASSERT(val_if_not_present >= 0); |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 200 | 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 | */ |
| 212 | struct 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 Pedrol | 77cd10f | 2019-09-05 13:30:48 +0200 | [diff] [blame] | 222 | /*! Set value in entry matching T, converting val from val_unit to unit of T. |
Neels Hofmeyr | 9655ed5 | 2019-09-11 01:48:11 +0200 | [diff] [blame] | 223 | * 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 Pedrol | 77cd10f | 2019-09-05 13:30:48 +0200 | [diff] [blame] | 224 | * \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 | */ |
| 230 | int osmo_tdef_set(struct osmo_tdef *tdefs, int T, unsigned long val, enum osmo_tdef_unit val_unit) |
| 231 | { |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 232 | unsigned long new_val; |
Neels Hofmeyr | 9655ed5 | 2019-09-11 01:48:11 +0200 | [diff] [blame] | 233 | struct osmo_tdef *t = osmo_tdef_get_entry(tdefs, T); |
| 234 | if (!t) |
| 235 | return -EEXIST; |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 236 | |
| 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 Hofmeyr | 9655ed5 | 2019-09-11 01:48:11 +0200 | [diff] [blame] | 242 | return 0; |
Pau Espin Pedrol | 77cd10f | 2019-09-05 13:30:48 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 245 | /*! 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 | */ |
| 250 | bool 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. |
Pau Espin Pedrol | c85f773 | 2019-10-11 17:04:15 +0200 | [diff] [blame] | 259 | * \return The number of characters printed on success (or number of characters |
| 260 | * which would have been written to the final string if enough space |
| 261 | * had been available), negative on error. See snprintf(). |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 262 | */ |
| 263 | int osmo_tdef_range_str_buf(char *buf, size_t buf_len, struct osmo_tdef *t) |
| 264 | { |
| 265 | int ret, len = 0, offset = 0, rem = buf_len; |
| 266 | |
| 267 | buf[0] = '\0'; |
| 268 | ret = snprintf(buf + offset, rem, "[%lu .. ", t->min_val); |
| 269 | if (ret < 0) |
| 270 | return ret; |
| 271 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 272 | |
| 273 | if (t->max_val) |
| 274 | ret = snprintf(buf + offset, rem, "%lu]", t->max_val); |
| 275 | else |
| 276 | ret = snprintf(buf + offset, rem, "inf]"); |
| 277 | if (ret < 0) |
| 278 | return ret; |
| 279 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
Pau Espin Pedrol | c85f773 | 2019-10-11 17:04:15 +0200 | [diff] [blame] | 280 | return len; |
Pau Espin Pedrol | 0cbe8f0 | 2019-09-17 13:13:52 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 283 | /*! Using osmo_tdef for osmo_fsm_inst: find a given state's osmo_tdef_state_timeout entry. |
| 284 | * |
| 285 | * The timeouts_array shall contain exactly 32 elements, regardless whether only some of them are actually populated |
| 286 | * with nonzero values. 32 corresponds to the number of states allowed by the osmo_fsm_* API. Lookup is by array index. |
| 287 | * Not populated entries imply a state change invocation without timeout. |
| 288 | * |
| 289 | * For example: |
| 290 | * |
| 291 | * struct osmo_tdef_state_timeout my_fsm_timeouts[32] = { |
| 292 | * [MY_FSM_STATE_3] = { .T = 423 }, // look up timeout configured for T423 |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 293 | * [MY_FSM_STATE_7] = { .keep_timer = true, .T = 235 }, // keep previous timer if running, or start T235 |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 294 | * [MY_FSM_STATE_8] = { .keep_timer = true }, // keep previous state's T number, continue timeout. |
| 295 | * // any state that is omitted will remain zero == no timeout |
| 296 | * }; |
| 297 | * osmo_tdef_get_state_timeout(MY_FSM_STATE_0, &my_fsm_timeouts) -> NULL, |
| 298 | * osmo_tdef_get_state_timeout(MY_FSM_STATE_7, &my_fsm_timeouts) -> { .T = 235 } |
| 299 | * |
| 300 | * The intention is then to obtain the timer like osmo_tdef_get(global_T_defs, T=235); see also |
| 301 | * fsm_inst_state_chg_T() below. |
| 302 | * |
| 303 | * \param[in] state State constant to look up. |
| 304 | * \param[in] timeouts_array Array[32] of struct osmo_tdef_state_timeout defining which timer number to use per state. |
| 305 | * \return A struct osmo_tdef_state_timeout entry, or NULL if that entry is zero initialized. |
| 306 | */ |
| 307 | const struct osmo_tdef_state_timeout *osmo_tdef_get_state_timeout(uint32_t state, const struct osmo_tdef_state_timeout *timeouts_array) |
| 308 | { |
| 309 | const struct osmo_tdef_state_timeout *t; |
| 310 | OSMO_ASSERT(state < 32); |
| 311 | t = &timeouts_array[state]; |
| 312 | if (!t->keep_timer && !t->T) |
| 313 | return NULL; |
| 314 | return t; |
| 315 | } |
| 316 | |
| 317 | /*! See invocation macro osmo_tdef_fsm_inst_state_chg() instead. |
| 318 | * \param[in] file Source file name, like __FILE__. |
| 319 | * \param[in] line Source file line number, like __LINE__. |
| 320 | */ |
| 321 | int _osmo_tdef_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t state, |
| 322 | const struct osmo_tdef_state_timeout *timeouts_array, |
| 323 | const struct osmo_tdef *tdefs, unsigned long default_timeout, |
| 324 | const char *file, int line) |
| 325 | { |
| 326 | const struct osmo_tdef_state_timeout *t = osmo_tdef_get_state_timeout(state, timeouts_array); |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 327 | unsigned long val = 0; |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 328 | |
| 329 | /* No timeout defined for this state? */ |
| 330 | if (!t) |
| 331 | return _osmo_fsm_inst_state_chg(fi, state, 0, 0, file, line); |
| 332 | |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 333 | if (t->T) |
| 334 | val = osmo_tdef_get(tdefs, t->T, OSMO_TDEF_S, default_timeout); |
| 335 | |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 336 | if (t->keep_timer) { |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 337 | if (t->T) |
| 338 | return _osmo_fsm_inst_state_chg_keep_or_start_timer(fi, state, val, t->T, file, line); |
| 339 | else |
| 340 | return _osmo_fsm_inst_state_chg_keep_timer(fi, state, file, line); |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Neels Hofmeyr | d4b79c8 | 2019-03-06 05:43:23 +0100 | [diff] [blame] | 343 | /* val is always initialized here, because if t->keep_timer is false, t->T must be != 0. |
| 344 | * Otherwise osmo_tdef_get_state_timeout() would have returned NULL. */ |
| 345 | OSMO_ASSERT(t->T); |
Neels Hofmeyr | 0fd615f | 2019-01-26 20:36:12 +0100 | [diff] [blame] | 346 | return _osmo_fsm_inst_state_chg(fi, state, val, t->T, file, line); |
| 347 | } |
| 348 | |
| 349 | const struct value_string osmo_tdef_unit_names[] = { |
| 350 | { OSMO_TDEF_S, "s" }, |
| 351 | { OSMO_TDEF_MS, "ms" }, |
| 352 | { OSMO_TDEF_M, "m" }, |
| 353 | { OSMO_TDEF_CUSTOM, "custom-unit" }, |
| 354 | {} |
| 355 | }; |
| 356 | |
| 357 | /*! @} */ |