Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file fsm.c |
| 2 | * Osmocom generic Finite State Machine implementation. */ |
| 3 | /* |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 4 | * (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdbool.h> |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 24 | #include <string.h> |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 25 | #include <inttypes.h> |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/core/fsm.h> |
| 28 | #include <osmocom/core/talloc.h> |
| 29 | #include <osmocom/core/logging.h> |
| 30 | #include <osmocom/core/utils.h> |
| 31 | |
| 32 | /*! \addtogroup fsm |
| 33 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 34 | * Finite State Machine abstraction |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 35 | * |
| 36 | * This is a generic C-language abstraction for implementing finite |
| 37 | * state machines within the Osmocom framework. It is intended to |
| 38 | * replace existing hand-coded or even only implicitly existing FSMs |
| 39 | * all over the existing code base. |
| 40 | * |
| 41 | * An libosmocore FSM is described by its \ref osmo_fsm description, |
| 42 | * which in turn refers to an array of \ref osmo_fsm_state descriptor, |
| 43 | * each describing a single state in the FSM. |
| 44 | * |
| 45 | * The general idea is that all actions performed within one state are |
| 46 | * located at one position in the code (the state's action function), |
| 47 | * as opposed to the 'message-centric' view of e.g. the existing |
| 48 | * state machines of the LAPD(m) coe, where there is one message for |
| 49 | * eahc possible event (primitive), and the function then needs to |
| 50 | * concern itself on how to handle that event over all possible states. |
| 51 | * |
| 52 | * For each state, there is a bit-mask of permitted input events for |
| 53 | * this state, as well as a bit-mask of permitted new output states to |
| 54 | * which the state can change. Furthermore, there is a function |
| 55 | * pointer implementing the actual handling of the input events |
| 56 | * occurring whilst in thta state. |
| 57 | * |
| 58 | * Furthermore, each state offers a function pointer that can be |
| 59 | * executed just before leaving a state, and another one just after |
| 60 | * entering a state. |
| 61 | * |
| 62 | * When transitioning into a new state, an optional timer number and |
| 63 | * time-out can be passed along. The timer is started just after |
| 64 | * entering the new state, and will call the \ref osmo_fsm timer_cb |
| 65 | * function once it expires. This is intended to be used in telecom |
| 66 | * state machines where a given timer (identified by a certain number) |
| 67 | * is started to terminate the fsm or terminate the fsm once expected |
| 68 | * events are not happening before timeout expiration. |
| 69 | * |
| 70 | * As there can often be many concurrent FSMs of one given class, we |
| 71 | * introduce the concept of \ref osmo_fsm_inst, i.e. an FSM instance. |
| 72 | * The instance keeps the actual state, while the \ref osmo_fsm |
| 73 | * descriptor contains the static/const descriptor of the FSM's states |
| 74 | * and possible transitions. |
| 75 | * |
| 76 | * osmo_fsm are integrated with the libosmocore logging system. The |
| 77 | * logging sub-system is determined by the FSM descriptor, as we assume |
| 78 | * one FSM (let's say one related to a location update procedure) is |
| 79 | * inevitably always tied to a sub-system. The logging level however |
| 80 | * is configurable for each FSM instance, to ensure that e.g. DEBUG |
| 81 | * logging can be used for the LU procedure of one subscriber, while |
| 82 | * NOTICE level is used for all other subscribers. |
| 83 | * |
| 84 | * In order to attach private state to the \ref osmo_fsm_inst, it |
| 85 | * offers an opaque priv pointer. |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 86 | * |
| 87 | * \file fsm.c */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 88 | |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 89 | LLIST_HEAD(osmo_g_fsms); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 90 | static bool fsm_log_addr = true; |
| 91 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 92 | /*! specify if FSM instance addresses should be logged or not |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 93 | * |
| 94 | * By default, the FSM name includes the pointer address of the \ref |
Neels Hofmeyr | a3953e0 | 2016-12-14 18:34:30 +0100 | [diff] [blame] | 95 | * osmo_fsm_inst. This behavior can be disabled (and re-enabled) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 96 | * using this function. |
| 97 | * |
| 98 | * \param[in] log_addr Indicate if FSM instance address shall be logged |
| 99 | */ |
| 100 | void osmo_fsm_log_addr(bool log_addr) |
| 101 | { |
Max | 61281f4 | 2016-11-01 10:49:31 +0100 | [diff] [blame] | 102 | fsm_log_addr = log_addr; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 105 | struct osmo_fsm *osmo_fsm_find_by_name(const char *name) |
| 106 | { |
| 107 | struct osmo_fsm *fsm; |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 108 | llist_for_each_entry(fsm, &osmo_g_fsms, list) { |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 109 | if (!strcmp(name, fsm->name)) |
| 110 | return fsm; |
| 111 | } |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Harald Welte | 4585e67 | 2017-04-16 17:23:56 +0200 | [diff] [blame] | 115 | struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm, |
| 116 | const char *name) |
| 117 | { |
| 118 | struct osmo_fsm_inst *fi; |
| 119 | |
| 120 | llist_for_each_entry(fi, &fsm->instances, list) { |
| 121 | if (!strcmp(name, fi->name)) |
| 122 | return fi; |
| 123 | } |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm, |
| 128 | const char *id) |
| 129 | { |
| 130 | struct osmo_fsm_inst *fi; |
| 131 | |
| 132 | llist_for_each_entry(fi, &fsm->instances, list) { |
| 133 | if (!strcmp(id, fi->id)) |
| 134 | return fi; |
| 135 | } |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 139 | /*! register a FSM with the core |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 140 | * |
| 141 | * A FSM descriptor needs to be registered with the core before any |
| 142 | * instances can be created for it. |
| 143 | * |
| 144 | * \param[in] fsm Descriptor of Finite State Machine to be registered |
| 145 | * \returns 0 on success; negative on error |
| 146 | */ |
| 147 | int osmo_fsm_register(struct osmo_fsm *fsm) |
| 148 | { |
Harald Welte | 8c4f545 | 2017-10-03 17:44:03 +0800 | [diff] [blame] | 149 | if (!osmo_identifier_valid(fsm->name)) { |
| 150 | LOGP(DLGLOBAL, LOGL_ERROR, "Attempting to register FSM with illegal identifier '%s'\n", fsm->name); |
| 151 | return -EINVAL; |
| 152 | } |
Harald Welte | 8808bb4 | 2017-01-07 11:11:03 +0100 | [diff] [blame] | 153 | if (osmo_fsm_find_by_name(fsm->name)) |
| 154 | return -EEXIST; |
Harald Welte | 3419391 | 2017-01-07 11:49:55 +0100 | [diff] [blame] | 155 | llist_add_tail(&fsm->list, &osmo_g_fsms); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 156 | INIT_LLIST_HEAD(&fsm->instances); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 161 | /*! unregister a FSM from the core |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 162 | * |
| 163 | * Once the FSM descriptor is unregistered, active instances can still |
| 164 | * use it, but no new instances may be created for it. |
| 165 | * |
| 166 | * \param[in] fsm Descriptor of Finite State Machine to be removed |
| 167 | */ |
| 168 | void osmo_fsm_unregister(struct osmo_fsm *fsm) |
| 169 | { |
| 170 | llist_del(&fsm->list); |
| 171 | } |
| 172 | |
| 173 | /* small wrapper function around timer expiration (for logging) */ |
| 174 | static void fsm_tmr_cb(void *data) |
| 175 | { |
| 176 | struct osmo_fsm_inst *fi = data; |
| 177 | struct osmo_fsm *fsm = fi->fsm; |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 178 | uint32_t T = fi->T; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 179 | |
| 180 | LOGPFSM(fi, "Timeout of T%u\n", fi->T); |
| 181 | |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 182 | if (fsm->timer_cb) { |
| 183 | int rc = fsm->timer_cb(fi); |
Harald Welte | 9e83c3d | 2017-04-16 17:24:46 +0200 | [diff] [blame] | 184 | if (rc != 1) { |
| 185 | fi->T = 0; |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 186 | return; |
Harald Welte | 9e83c3d | 2017-04-16 17:24:46 +0200 | [diff] [blame] | 187 | } |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 188 | LOGPFSM(fi, "timer_cb requested termination\n"); |
| 189 | } else |
| 190 | LOGPFSM(fi, "No timer_cb, automatic termination\n"); |
| 191 | |
| 192 | /* if timer_cb returns 1 or there is no timer_cb */ |
Harald Welte | 9e83c3d | 2017-04-16 17:24:46 +0200 | [diff] [blame] | 193 | fi->T = 0; |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 194 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, &T); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 195 | } |
| 196 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 197 | /*! allocate a new instance of a specified FSM |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 198 | * \param[in] fsm Descriptor of the FSM |
| 199 | * \param[in] ctx talloc context from which to allocate memory |
| 200 | * \param[in] priv private data reference store in fsm instance |
| 201 | * \param[in] log_level The log level for events of this FSM |
| 202 | * \returns newly-allocated, initialized and registered FSM instance |
| 203 | */ |
| 204 | struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv, |
| 205 | int log_level, const char *id) |
| 206 | { |
| 207 | struct osmo_fsm_inst *fi = talloc_zero(ctx, struct osmo_fsm_inst); |
| 208 | |
| 209 | fi->fsm = fsm; |
| 210 | fi->priv = priv; |
| 211 | fi->log_level = log_level; |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 212 | osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi); |
Harald Welte | 8c4f545 | 2017-10-03 17:44:03 +0800 | [diff] [blame] | 213 | if (id) { |
| 214 | if (!osmo_identifier_valid(id)) { |
| 215 | LOGP(DLGLOBAL, LOGL_ERROR, "Attempting to allocate FSM instance of type '%s'" |
| 216 | " with illegal identifier '%s'\n", fsm->name, id); |
| 217 | talloc_free(fi); |
| 218 | return NULL; |
| 219 | } |
Harald Welte | f323911 | 2016-07-10 15:11:45 +0200 | [diff] [blame] | 220 | fi->id = talloc_strdup(fi, id); |
Harald Welte | 8c4f545 | 2017-10-03 17:44:03 +0800 | [diff] [blame] | 221 | } |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 222 | |
| 223 | if (!fsm_log_addr) { |
| 224 | if (id) |
| 225 | fi->name = talloc_asprintf(fi, "%s(%s)", fsm->name, id); |
| 226 | else |
| 227 | fi->name = talloc_asprintf(fi, "%s", fsm->name); |
| 228 | } else { |
| 229 | if (id) |
| 230 | fi->name = talloc_asprintf(fi, "%s(%s)[%p]", fsm->name, |
| 231 | id, fi); |
| 232 | else |
| 233 | fi->name = talloc_asprintf(fi, "%s[%p]", fsm->name, fi); |
| 234 | } |
| 235 | |
| 236 | INIT_LLIST_HEAD(&fi->proc.children); |
| 237 | INIT_LLIST_HEAD(&fi->proc.child); |
| 238 | llist_add(&fi->list, &fsm->instances); |
| 239 | |
| 240 | LOGPFSM(fi, "Allocated\n"); |
| 241 | |
| 242 | return fi; |
| 243 | } |
| 244 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 245 | /*! allocate a new instance of a specified FSM as child of |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 246 | * other FSM instance |
| 247 | * |
| 248 | * This is like \ref osmo_fsm_inst_alloc but using the parent FSM as |
| 249 | * talloc context, and inheriting the log level of the parent. |
| 250 | * |
| 251 | * \param[in] fsm Descriptor of the to-be-allocated FSM |
| 252 | * \param[in] parent Parent FSM instance |
| 253 | * \param[in] parent_term_event Event to be sent to parent when terminating |
| 254 | * \returns newly-allocated, initialized and registered FSM instance |
| 255 | */ |
| 256 | struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm, |
| 257 | struct osmo_fsm_inst *parent, |
| 258 | uint32_t parent_term_event) |
| 259 | { |
| 260 | struct osmo_fsm_inst *fi; |
| 261 | |
| 262 | fi = osmo_fsm_inst_alloc(fsm, parent, NULL, parent->log_level, |
| 263 | parent->id); |
| 264 | if (!fi) { |
| 265 | /* indicate immediate termination to caller */ |
| 266 | osmo_fsm_inst_dispatch(parent, parent_term_event, NULL); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | LOGPFSM(fi, "is child of %s\n", osmo_fsm_inst_name(parent)); |
| 271 | |
| 272 | fi->proc.parent = parent; |
| 273 | fi->proc.parent_term_event = parent_term_event; |
| 274 | llist_add(&fi->proc.child, &parent->proc.children); |
| 275 | |
| 276 | return fi; |
| 277 | } |
| 278 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 279 | /*! delete a given instance of a FSM |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 280 | * \param[in] fsm The FSM to be un-registered and deleted |
| 281 | */ |
| 282 | void osmo_fsm_inst_free(struct osmo_fsm_inst *fi) |
| 283 | { |
Max | 3de97e1 | 2016-11-02 10:37:58 +0100 | [diff] [blame] | 284 | LOGPFSM(fi, "Deallocated\n"); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 285 | osmo_timer_del(&fi->timer); |
| 286 | llist_del(&fi->list); |
| 287 | talloc_free(fi); |
| 288 | } |
| 289 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 290 | /*! get human-readable name of FSM event |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 291 | * \param[in] fsm FSM descriptor of event |
| 292 | * \param[in] event Event integer value |
| 293 | * \returns string rendering of the event |
| 294 | */ |
| 295 | const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event) |
| 296 | { |
| 297 | static char buf[32]; |
| 298 | if (!fsm->event_names) { |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 299 | snprintf(buf, sizeof(buf), "%"PRIu32, event); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 300 | return buf; |
| 301 | } else |
| 302 | return get_value_string(fsm->event_names, event); |
| 303 | } |
| 304 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 305 | /*! get human-readable name of FSM instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 306 | * \param[in] fi FSM instance |
| 307 | * \returns string rendering of the FSM identity |
| 308 | */ |
| 309 | const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi) |
| 310 | { |
| 311 | if (!fi) |
| 312 | return "NULL"; |
| 313 | |
| 314 | if (fi->name) |
| 315 | return fi->name; |
| 316 | else |
| 317 | return fi->fsm->name; |
| 318 | } |
| 319 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 320 | /*! get human-readable name of FSM instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 321 | * \param[in] fsm FSM descriptor |
| 322 | * \param[in] state FSM state number |
| 323 | * \returns string rendering of the FSM state |
| 324 | */ |
| 325 | const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state) |
| 326 | { |
| 327 | static char buf[32]; |
| 328 | if (state >= fsm->num_states) { |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 329 | snprintf(buf, sizeof(buf), "unknown %"PRIu32, state); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 330 | return buf; |
| 331 | } else |
| 332 | return fsm->states[state].name; |
| 333 | } |
| 334 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 335 | /*! perform a state change of the given FSM instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 336 | * |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 337 | * Best invoke via the osmo_fsm_inst_state_chg() macro which logs the source |
| 338 | * file where the state change was effected. Alternatively, you may pass \a |
| 339 | * file as NULL to use the normal file/line indication instead. |
| 340 | * |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 341 | * All changes to the FSM instance state must be made via this |
| 342 | * function. It verifies that the existing state actually permits a |
| 343 | * transiiton to new_state. |
| 344 | * |
| 345 | * timeout_secs and T are optional parameters, and only have any effect |
| 346 | * if timeout_secs is not 0. If the timeout function is used, then the |
| 347 | * new_state is entered, and the FSM instances timer is set to expire |
| 348 | * in timeout_secs functions. At that time, the FSM's timer_cb |
| 349 | * function will be called for handling of the timeout by the user. |
| 350 | * |
| 351 | * \param[in] fi FSM instance whose state is to change |
| 352 | * \param[in] new_state The new state into which we should change |
| 353 | * \param[in] timeout_secs Timeout in seconds (if !=0) |
| 354 | * \param[in] T Timer number (if \ref timeout_secs != 0) |
Neels Hofmeyr | b805cc1 | 2016-12-23 04:23:18 +0100 | [diff] [blame] | 355 | * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro) |
| 356 | * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 357 | * \returns 0 on success; negative on error |
| 358 | */ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 359 | int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state, |
| 360 | unsigned long timeout_secs, int T, |
| 361 | const char *file, int line) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 362 | { |
| 363 | struct osmo_fsm *fsm = fi->fsm; |
| 364 | uint32_t old_state = fi->state; |
| 365 | const struct osmo_fsm_state *st = &fsm->states[fi->state]; |
| 366 | |
| 367 | /* validate if new_state is a valid state */ |
| 368 | if (!(st->out_state_mask & (1 << new_state))) { |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 369 | LOGPFSMLSRC(fi, LOGL_ERROR, file, line, |
| 370 | "transition to state %s not permitted!\n", |
| 371 | osmo_fsm_state_name(fsm, new_state)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 372 | return -EPERM; |
| 373 | } |
| 374 | |
Harald Welte | 02a6672 | 2016-07-10 15:13:51 +0200 | [diff] [blame] | 375 | /* delete the old timer */ |
| 376 | osmo_timer_del(&fi->timer); |
| 377 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 378 | if (st->onleave) |
| 379 | st->onleave(fi, new_state); |
| 380 | |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 381 | LOGPFSMSRC(fi, file, line, "state_chg to %s\n", |
| 382 | osmo_fsm_state_name(fsm, new_state)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 383 | fi->state = new_state; |
Harald Welte | 460f9ef | 2016-08-01 00:38:36 +0200 | [diff] [blame] | 384 | st = &fsm->states[new_state]; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 385 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 386 | if (timeout_secs) { |
Harald Welte | f627c0f | 2016-06-18 10:36:25 +0200 | [diff] [blame] | 387 | fi->T = T; |
| 388 | osmo_timer_schedule(&fi->timer, timeout_secs, 0); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 389 | } |
| 390 | |
Harald Welte | 673018f | 2016-07-10 15:09:43 +0200 | [diff] [blame] | 391 | /* Call 'onenter' last, user might terminate FSM from there */ |
| 392 | if (st->onenter) |
| 393 | st->onenter(fi, old_state); |
| 394 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 398 | /*! dispatch an event to an osmocom finite state machine instance |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 399 | * |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 400 | * Best invoke via the osmo_fsm_inst_dispatch() macro which logs the source |
| 401 | * file where the event was effected. Alternatively, you may pass \a file as |
| 402 | * NULL to use the normal file/line indication instead. |
| 403 | * |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 404 | * Any incoming events to \ref osmo_fsm instances must be dispatched to |
| 405 | * them via this function. It verifies, whether the event is permitted |
| 406 | * based on the current state of the FSM. If not, -1 is returned. |
| 407 | * |
| 408 | * \param[in] fi FSM instance |
| 409 | * \param[in] event Event to send to FSM instance |
| 410 | * \param[in] data Data to pass along with the event |
Neels Hofmeyr | b805cc1 | 2016-12-23 04:23:18 +0100 | [diff] [blame] | 411 | * \param[in] file Calling source file (from osmo_fsm_inst_dispatch macro) |
| 412 | * \param[in] line Calling source line (from osmo_fsm_inst_dispatch macro) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 413 | * \returns 0 in case of success; negative on error |
| 414 | */ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 415 | int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data, |
| 416 | const char *file, int line) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 417 | { |
| 418 | struct osmo_fsm *fsm; |
| 419 | const struct osmo_fsm_state *fs; |
| 420 | |
| 421 | if (!fi) { |
Neels Hofmeyr | c7155df | 2016-12-23 04:24:51 +0100 | [diff] [blame] | 422 | LOGPSRC(DLGLOBAL, LOGL_ERROR, file, line, |
Pau Espin Pedrol | 4f8857e | 2017-06-18 12:23:00 +0200 | [diff] [blame] | 423 | "Trying to dispatch event %"PRIu32" to non-existent" |
Neels Hofmeyr | c7155df | 2016-12-23 04:24:51 +0100 | [diff] [blame] | 424 | " FSM instance!\n", event); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 425 | osmo_log_backtrace(DLGLOBAL, LOGL_ERROR); |
| 426 | return -ENODEV; |
| 427 | } |
| 428 | |
| 429 | fsm = fi->fsm; |
| 430 | OSMO_ASSERT(fi->state < fsm->num_states); |
| 431 | fs = &fi->fsm->states[fi->state]; |
| 432 | |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 433 | LOGPFSMSRC(fi, file, line, |
| 434 | "Received Event %s\n", osmo_fsm_event_name(fsm, event)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 435 | |
| 436 | if (((1 << event) & fsm->allstate_event_mask) && fsm->allstate_action) { |
| 437 | fsm->allstate_action(fi, event, data); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | if (!((1 << event) & fs->in_event_mask)) { |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 442 | LOGPFSMLSRC(fi, LOGL_ERROR, file, line, |
| 443 | "Event %s not permitted\n", |
| 444 | osmo_fsm_event_name(fsm, event)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 445 | return -1; |
| 446 | } |
| 447 | fs->action(fi, event, data); |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 452 | /*! Terminate FSM instance with given cause |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 453 | * |
| 454 | * This safely terminates the given FSM instance by first iterating |
| 455 | * over all children and sending them a termination event. Next, it |
| 456 | * calls the FSM descriptors cleanup function (if any), followed by |
| 457 | * releasing any memory associated with the FSM instance. |
| 458 | * |
| 459 | * Finally, the parent FSM instance (if any) is notified using the |
| 460 | * parent termination event configured at time of FSM instance start. |
| 461 | * |
| 462 | * \param[in] fi FSM instance to be terminated |
| 463 | * \param[in] cause Cause / reason for termination |
Neels Hofmeyr | b805cc1 | 2016-12-23 04:23:18 +0100 | [diff] [blame] | 464 | * \param[in] data Opaque event data to be passed with the parent term event |
| 465 | * \param[in] file Calling source file (from osmo_fsm_inst_term macro) |
| 466 | * \param[in] line Calling source line (from osmo_fsm_inst_term macro) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 467 | */ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 468 | void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi, |
| 469 | enum osmo_fsm_term_cause cause, void *data, |
| 470 | const char *file, int line) |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 471 | { |
Neels Hofmeyr | 3faa014 | 2016-12-18 23:41:41 +0100 | [diff] [blame] | 472 | struct osmo_fsm_inst *parent; |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 473 | uint32_t parent_term_event = fi->proc.parent_term_event; |
| 474 | |
Neels Hofmeyr | 5c5c78a | 2016-12-14 18:35:47 +0100 | [diff] [blame] | 475 | LOGPFSMSRC(fi, file, line, "Terminating (cause = %s)\n", |
| 476 | osmo_fsm_term_cause_name(cause)); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 477 | |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 478 | _osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL, |
| 479 | file, line); |
| 480 | |
| 481 | /* delete ourselves from the parent */ |
Neels Hofmeyr | 3faa014 | 2016-12-18 23:41:41 +0100 | [diff] [blame] | 482 | parent = fi->proc.parent; |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 483 | if (parent) |
| 484 | LOGPFSMSRC(fi, file, line, "Removing from parent %s\n", |
| 485 | osmo_fsm_inst_name(parent)); |
| 486 | llist_del(&fi->proc.child); |
| 487 | |
| 488 | /* call destructor / clean-up function */ |
| 489 | if (fi->fsm->cleanup) |
| 490 | fi->fsm->cleanup(fi, cause); |
| 491 | |
| 492 | LOGPFSMSRC(fi, file, line, "Freeing instance\n"); |
Neels Hofmeyr | 3faa014 | 2016-12-18 23:41:41 +0100 | [diff] [blame] | 493 | /* Fetch parent again in case it has changed. */ |
| 494 | parent = fi->proc.parent; |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 495 | osmo_fsm_inst_free(fi); |
| 496 | |
| 497 | /* indicate our termination to the parent */ |
| 498 | if (parent && cause != OSMO_FSM_TERM_PARENT) |
| 499 | _osmo_fsm_inst_dispatch(parent, parent_term_event, data, |
| 500 | file, line); |
| 501 | } |
| 502 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 503 | /*! Terminate all child FSM instances of an FSM instance. |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 504 | * |
| 505 | * Iterate over all children and send them a termination event, with the given |
| 506 | * cause. Pass OSMO_FSM_TERM_PARENT to avoid dispatching events from the |
| 507 | * terminated child FSMs. |
| 508 | * |
| 509 | * \param[in] fi FSM instance that should be cleared of child FSMs |
| 510 | * \param[in] cause Cause / reason for termination (OSMO_FSM_TERM_PARENT) |
| 511 | * \param[in] data Opaque event data to be passed with the parent term events |
| 512 | * \param[in] file Calling source file (from osmo_fsm_inst_term_children macro) |
| 513 | * \param[in] line Calling source line (from osmo_fsm_inst_term_children macro) |
| 514 | */ |
| 515 | void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi, |
| 516 | enum osmo_fsm_term_cause cause, |
| 517 | void *data, |
| 518 | const char *file, int line) |
| 519 | { |
| 520 | struct osmo_fsm_inst *first_child, *last_seen_first_child; |
| 521 | |
Neels Hofmeyr | 06ac9b4 | 2016-12-20 12:05:19 +0100 | [diff] [blame] | 522 | /* iterate over all children, starting from the beginning every time: |
| 523 | * terminating an FSM may emit events that cause other FSMs to also |
| 524 | * terminate and remove themselves from this list. */ |
| 525 | last_seen_first_child = NULL; |
| 526 | while (!llist_empty(&fi->proc.children)) { |
| 527 | first_child = llist_entry(fi->proc.children.next, |
| 528 | typeof(*first_child), |
| 529 | proc.child); |
| 530 | |
| 531 | /* paranoia: do not loop forever */ |
| 532 | if (first_child == last_seen_first_child) { |
| 533 | LOGPFSMLSRC(fi, LOGL_ERROR, file, line, |
| 534 | "Internal error while terminating child" |
| 535 | " FSMs: a child FSM is stuck\n"); |
| 536 | break; |
| 537 | } |
| 538 | last_seen_first_child = first_child; |
| 539 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 540 | /* terminate child */ |
Neels Hofmeyr | c014f60 | 2016-12-23 04:26:39 +0100 | [diff] [blame] | 541 | _osmo_fsm_inst_term(first_child, cause, data, |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 542 | file, line); |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 543 | } |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 544 | } |
| 545 | |
Neels Hofmeyr | 5c5c78a | 2016-12-14 18:35:47 +0100 | [diff] [blame] | 546 | const struct value_string osmo_fsm_term_cause_names[] = { |
Neels Hofmeyr | 1808096 | 2016-12-16 13:43:54 +0100 | [diff] [blame] | 547 | OSMO_VALUE_STRING(OSMO_FSM_TERM_PARENT), |
| 548 | OSMO_VALUE_STRING(OSMO_FSM_TERM_REQUEST), |
| 549 | OSMO_VALUE_STRING(OSMO_FSM_TERM_REGULAR), |
| 550 | OSMO_VALUE_STRING(OSMO_FSM_TERM_ERROR), |
| 551 | OSMO_VALUE_STRING(OSMO_FSM_TERM_TIMEOUT), |
Neels Hofmeyr | 5c5c78a | 2016-12-14 18:35:47 +0100 | [diff] [blame] | 552 | { 0, NULL } |
| 553 | }; |
| 554 | |
Harald Welte | 136e737 | 2016-05-29 10:53:17 +0900 | [diff] [blame] | 555 | /*! @} */ |