blob: 1f6141fac425eb924b16a130971fbffd866baafa [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file fsm.c
2 * Osmocom generic Finite State Machine implementation. */
3/*
Harald Welte136e7372016-05-29 10:53:17 +09004 * (C) 2016 by Harald Welte <laforge@gnumonks.org>
5 *
Harald Weltee08da972017-11-13 01:00:26 +09006 * SPDX-License-Identifier: GPL-2.0+
7 *
Harald Welte136e7372016-05-29 10:53:17 +09008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23
24#include <errno.h>
25#include <stdbool.h>
Harald Welte8808bb42017-01-07 11:11:03 +010026#include <string.h>
Pau Espin Pedrol4f8857e2017-06-18 12:23:00 +020027#include <inttypes.h>
Harald Welte136e7372016-05-29 10:53:17 +090028
29#include <osmocom/core/fsm.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/core/logging.h>
32#include <osmocom/core/utils.h>
33
34/*! \addtogroup fsm
35 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020036 * Finite State Machine abstraction
Harald Welte136e7372016-05-29 10:53:17 +090037 *
38 * This is a generic C-language abstraction for implementing finite
39 * state machines within the Osmocom framework. It is intended to
40 * replace existing hand-coded or even only implicitly existing FSMs
41 * all over the existing code base.
42 *
43 * An libosmocore FSM is described by its \ref osmo_fsm description,
44 * which in turn refers to an array of \ref osmo_fsm_state descriptor,
45 * each describing a single state in the FSM.
46 *
47 * The general idea is that all actions performed within one state are
48 * located at one position in the code (the state's action function),
49 * as opposed to the 'message-centric' view of e.g. the existing
50 * state machines of the LAPD(m) coe, where there is one message for
51 * eahc possible event (primitive), and the function then needs to
52 * concern itself on how to handle that event over all possible states.
53 *
54 * For each state, there is a bit-mask of permitted input events for
55 * this state, as well as a bit-mask of permitted new output states to
56 * which the state can change. Furthermore, there is a function
57 * pointer implementing the actual handling of the input events
58 * occurring whilst in thta state.
59 *
60 * Furthermore, each state offers a function pointer that can be
61 * executed just before leaving a state, and another one just after
62 * entering a state.
63 *
64 * When transitioning into a new state, an optional timer number and
65 * time-out can be passed along. The timer is started just after
66 * entering the new state, and will call the \ref osmo_fsm timer_cb
67 * function once it expires. This is intended to be used in telecom
68 * state machines where a given timer (identified by a certain number)
69 * is started to terminate the fsm or terminate the fsm once expected
70 * events are not happening before timeout expiration.
71 *
72 * As there can often be many concurrent FSMs of one given class, we
73 * introduce the concept of \ref osmo_fsm_inst, i.e. an FSM instance.
74 * The instance keeps the actual state, while the \ref osmo_fsm
75 * descriptor contains the static/const descriptor of the FSM's states
76 * and possible transitions.
77 *
78 * osmo_fsm are integrated with the libosmocore logging system. The
79 * logging sub-system is determined by the FSM descriptor, as we assume
80 * one FSM (let's say one related to a location update procedure) is
81 * inevitably always tied to a sub-system. The logging level however
82 * is configurable for each FSM instance, to ensure that e.g. DEBUG
83 * logging can be used for the LU procedure of one subscriber, while
84 * NOTICE level is used for all other subscribers.
85 *
86 * In order to attach private state to the \ref osmo_fsm_inst, it
87 * offers an opaque priv pointer.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020088 *
89 * \file fsm.c */
Harald Welte96e2a002017-06-12 21:44:18 +020090
Harald Welte34193912017-01-07 11:49:55 +010091LLIST_HEAD(osmo_g_fsms);
Harald Welte136e7372016-05-29 10:53:17 +090092static bool fsm_log_addr = true;
93
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094/*! specify if FSM instance addresses should be logged or not
Harald Welte136e7372016-05-29 10:53:17 +090095 *
96 * By default, the FSM name includes the pointer address of the \ref
Neels Hofmeyra3953e02016-12-14 18:34:30 +010097 * osmo_fsm_inst. This behavior can be disabled (and re-enabled)
Harald Welte136e7372016-05-29 10:53:17 +090098 * using this function.
99 *
100 * \param[in] log_addr Indicate if FSM instance address shall be logged
101 */
102void osmo_fsm_log_addr(bool log_addr)
103{
Max61281f42016-11-01 10:49:31 +0100104 fsm_log_addr = log_addr;
Harald Welte136e7372016-05-29 10:53:17 +0900105}
106
Harald Welte8808bb42017-01-07 11:11:03 +0100107struct osmo_fsm *osmo_fsm_find_by_name(const char *name)
108{
109 struct osmo_fsm *fsm;
Harald Welte34193912017-01-07 11:49:55 +0100110 llist_for_each_entry(fsm, &osmo_g_fsms, list) {
Harald Welte8808bb42017-01-07 11:11:03 +0100111 if (!strcmp(name, fsm->name))
112 return fsm;
113 }
114 return NULL;
115}
116
Harald Welte4585e672017-04-16 17:23:56 +0200117struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm,
118 const char *name)
119{
120 struct osmo_fsm_inst *fi;
121
Neels Hofmeyr2bcc8732018-04-09 01:35:02 +0200122 if (!name)
123 return NULL;
124
Harald Welte4585e672017-04-16 17:23:56 +0200125 llist_for_each_entry(fi, &fsm->instances, list) {
Neels Hofmeyr2bcc8732018-04-09 01:35:02 +0200126 if (!fi->name)
127 continue;
Harald Welte4585e672017-04-16 17:23:56 +0200128 if (!strcmp(name, fi->name))
129 return fi;
130 }
131 return NULL;
132}
133
134struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm,
135 const char *id)
136{
137 struct osmo_fsm_inst *fi;
138
139 llist_for_each_entry(fi, &fsm->instances, list) {
140 if (!strcmp(id, fi->id))
141 return fi;
142 }
143 return NULL;
144}
145
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200146/*! register a FSM with the core
Harald Welte136e7372016-05-29 10:53:17 +0900147 *
148 * A FSM descriptor needs to be registered with the core before any
149 * instances can be created for it.
150 *
151 * \param[in] fsm Descriptor of Finite State Machine to be registered
152 * \returns 0 on success; negative on error
153 */
154int osmo_fsm_register(struct osmo_fsm *fsm)
155{
Harald Welte8c4f5452017-10-03 17:44:03 +0800156 if (!osmo_identifier_valid(fsm->name)) {
157 LOGP(DLGLOBAL, LOGL_ERROR, "Attempting to register FSM with illegal identifier '%s'\n", fsm->name);
158 return -EINVAL;
159 }
Harald Welte8808bb42017-01-07 11:11:03 +0100160 if (osmo_fsm_find_by_name(fsm->name))
161 return -EEXIST;
Stefan Sperling888dc7d2018-02-26 19:17:02 +0100162 if (fsm->event_names == NULL)
163 LOGP(DLGLOBAL, LOGL_ERROR, "FSM '%s' has no event names! Please fix!\n", fsm->name);
Harald Welte34193912017-01-07 11:49:55 +0100164 llist_add_tail(&fsm->list, &osmo_g_fsms);
Harald Welte136e7372016-05-29 10:53:17 +0900165 INIT_LLIST_HEAD(&fsm->instances);
166
167 return 0;
168}
169
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200170/*! unregister a FSM from the core
Harald Welte136e7372016-05-29 10:53:17 +0900171 *
172 * Once the FSM descriptor is unregistered, active instances can still
173 * use it, but no new instances may be created for it.
174 *
175 * \param[in] fsm Descriptor of Finite State Machine to be removed
176 */
177void osmo_fsm_unregister(struct osmo_fsm *fsm)
178{
179 llist_del(&fsm->list);
180}
181
182/* small wrapper function around timer expiration (for logging) */
183static void fsm_tmr_cb(void *data)
184{
185 struct osmo_fsm_inst *fi = data;
186 struct osmo_fsm *fsm = fi->fsm;
Harald Weltef627c0f2016-06-18 10:36:25 +0200187 uint32_t T = fi->T;
Harald Welte136e7372016-05-29 10:53:17 +0900188
189 LOGPFSM(fi, "Timeout of T%u\n", fi->T);
190
Harald Weltef627c0f2016-06-18 10:36:25 +0200191 if (fsm->timer_cb) {
192 int rc = fsm->timer_cb(fi);
Neels Hofmeyr19ec7b92017-11-18 23:10:24 +0100193 if (rc != 1)
194 /* We don't actually know whether fi exists anymore.
195 * Make sure to not access it and return right away. */
Harald Weltef627c0f2016-06-18 10:36:25 +0200196 return;
Neels Hofmeyr19ec7b92017-11-18 23:10:24 +0100197 /* The timer_cb told us to terminate, so we can safely assume
198 * that fi still exists. */
Harald Weltef627c0f2016-06-18 10:36:25 +0200199 LOGPFSM(fi, "timer_cb requested termination\n");
200 } else
201 LOGPFSM(fi, "No timer_cb, automatic termination\n");
202
203 /* if timer_cb returns 1 or there is no timer_cb */
204 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_TIMEOUT, &T);
Harald Welte136e7372016-05-29 10:53:17 +0900205}
206
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100207/*! Change id of the FSM instance
208 * \param[in] fi FSM instance
209 * \param[in] id new ID
210 * \returns 0 if the ID was updated, otherwise -EINVAL
211 */
212int osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id)
213{
Neels Hofmeyra64c45a2018-03-31 16:34:49 +0200214 if (!id)
215 return osmo_fsm_inst_update_id_f(fi, NULL);
216 else
217 return osmo_fsm_inst_update_id_f(fi, "%s", id);
218}
219
220static void update_name(struct osmo_fsm_inst *fi)
221{
222 if (fi->name)
223 talloc_free((char*)fi->name);
224
225 if (!fsm_log_addr) {
226 if (fi->id)
227 fi->name = talloc_asprintf(fi, "%s(%s)", fi->fsm->name, fi->id);
228 else
229 fi->name = talloc_asprintf(fi, "%s", fi->fsm->name);
230 } else {
231 if (fi->id)
232 fi->name = talloc_asprintf(fi, "%s(%s)[%p]", fi->fsm->name, fi->id, fi);
233 else
234 fi->name = talloc_asprintf(fi, "%s[%p]", fi->fsm->name, fi);
235 }
236}
237
238/*! Change id of the FSM instance using a string format.
239 * \param[in] fi FSM instance.
240 * \param[in] fmt format string to compose new ID.
241 * \param[in] ... variable argument list for format string.
242 * \returns 0 if the ID was updated, otherwise -EINVAL.
243 */
244int osmo_fsm_inst_update_id_f(struct osmo_fsm_inst *fi, const char *fmt, ...)
245{
246 char *id = NULL;
247
248 if (fmt) {
249 va_list ap;
250
251 va_start(ap, fmt);
252 id = talloc_vasprintf(fi, fmt, ap);
253 va_end(ap);
254
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100255 if (!osmo_identifier_valid(id)) {
Neels Hofmeyr6e8c0882018-04-09 02:28:34 +0200256 LOGP(DLGLOBAL, LOGL_ERROR,
257 "Attempting to set illegal id for FSM instance of type '%s': %s\n",
258 fi->fsm->name, osmo_quote_str(id, -1));
Neels Hofmeyra64c45a2018-03-31 16:34:49 +0200259 talloc_free(id);
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100260 return -EINVAL;
261 }
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100262 }
Daniel Willmann04a2a322018-03-14 18:31:33 +0100263
264 if (fi->id)
Neels Hofmeyra64c45a2018-03-31 16:34:49 +0200265 talloc_free((char*)fi->id);
266 fi->id = id;
Daniel Willmann04a2a322018-03-14 18:31:33 +0100267
Neels Hofmeyra64c45a2018-03-31 16:34:49 +0200268 update_name(fi);
Daniel Willmann04a2a322018-03-14 18:31:33 +0100269 return 0;
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100270}
271
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200272/*! allocate a new instance of a specified FSM
Harald Welte136e7372016-05-29 10:53:17 +0900273 * \param[in] fsm Descriptor of the FSM
274 * \param[in] ctx talloc context from which to allocate memory
275 * \param[in] priv private data reference store in fsm instance
276 * \param[in] log_level The log level for events of this FSM
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100277 * \param[in] id The name/ID of the FSM instance
Harald Welte136e7372016-05-29 10:53:17 +0900278 * \returns newly-allocated, initialized and registered FSM instance
279 */
280struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
281 int log_level, const char *id)
282{
283 struct osmo_fsm_inst *fi = talloc_zero(ctx, struct osmo_fsm_inst);
284
285 fi->fsm = fsm;
286 fi->priv = priv;
287 fi->log_level = log_level;
Pablo Neira Ayuso44f423f2017-05-08 18:00:28 +0200288 osmo_timer_setup(&fi->timer, fsm_tmr_cb, fi);
Daniel Willmannb0c43a62018-02-08 18:00:37 +0100289
Neels Hofmeyr71f76a12018-03-31 16:30:25 +0200290 if (osmo_fsm_inst_update_id(fi, id) < 0) {
291 talloc_free(fi);
292 return NULL;
Harald Welte8c4f5452017-10-03 17:44:03 +0800293 }
Harald Welte136e7372016-05-29 10:53:17 +0900294
Harald Welte136e7372016-05-29 10:53:17 +0900295 INIT_LLIST_HEAD(&fi->proc.children);
296 INIT_LLIST_HEAD(&fi->proc.child);
297 llist_add(&fi->list, &fsm->instances);
298
299 LOGPFSM(fi, "Allocated\n");
300
301 return fi;
302}
303
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200304/*! allocate a new instance of a specified FSM as child of
Harald Welte136e7372016-05-29 10:53:17 +0900305 * other FSM instance
306 *
307 * This is like \ref osmo_fsm_inst_alloc but using the parent FSM as
308 * talloc context, and inheriting the log level of the parent.
309 *
310 * \param[in] fsm Descriptor of the to-be-allocated FSM
311 * \param[in] parent Parent FSM instance
312 * \param[in] parent_term_event Event to be sent to parent when terminating
313 * \returns newly-allocated, initialized and registered FSM instance
314 */
315struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm,
316 struct osmo_fsm_inst *parent,
317 uint32_t parent_term_event)
318{
319 struct osmo_fsm_inst *fi;
320
321 fi = osmo_fsm_inst_alloc(fsm, parent, NULL, parent->log_level,
322 parent->id);
323 if (!fi) {
324 /* indicate immediate termination to caller */
325 osmo_fsm_inst_dispatch(parent, parent_term_event, NULL);
326 return NULL;
327 }
328
329 LOGPFSM(fi, "is child of %s\n", osmo_fsm_inst_name(parent));
330
Philipp Maier2a06a492018-01-16 18:45:56 +0100331 osmo_fsm_inst_change_parent(fi, parent, parent_term_event);
Harald Welte136e7372016-05-29 10:53:17 +0900332
333 return fi;
334}
335
Philipp Maier2a06a492018-01-16 18:45:56 +0100336/*! unlink child FSM from its parent FSM.
337 * \param[in] fi Descriptor of the child FSM to unlink.
Philipp Maierd1f57932018-02-14 18:20:07 +0100338 * \param[in] ctx New talloc context
339 *
340 * Never call this function from the cleanup callback, because at that time
341 * the child FSMs will already be terminated. If unlinking should be performed
342 * on FSM termination, use the grace callback instead. */
Philipp Maier2a06a492018-01-16 18:45:56 +0100343void osmo_fsm_inst_unlink_parent(struct osmo_fsm_inst *fi, void *ctx)
344{
345 if (fi->proc.parent) {
346 talloc_steal(ctx, fi);
347 fi->proc.parent = NULL;
348 fi->proc.parent_term_event = 0;
349 llist_del(&fi->proc.child);
350 }
351}
352
353/*! change parent instance of an FSM.
354 * \param[in] fi Descriptor of the to-be-allocated FSM.
355 * \param[in] new_parent New parent FSM instance.
Philipp Maierd1f57932018-02-14 18:20:07 +0100356 * \param[in] new_parent_term_event Event to be sent to parent when terminating.
357 *
358 * Never call this function from the cleanup callback!
359 * (see also osmo_fsm_inst_unlink_parent()).*/
Philipp Maier2a06a492018-01-16 18:45:56 +0100360void osmo_fsm_inst_change_parent(struct osmo_fsm_inst *fi,
361 struct osmo_fsm_inst *new_parent,
362 uint32_t new_parent_term_event)
363{
364 /* Make sure a possibly existing old parent is unlinked first
365 * (new_parent can be NULL) */
366 osmo_fsm_inst_unlink_parent(fi, new_parent);
367
368 /* Add new parent */
369 if (new_parent) {
370 fi->proc.parent = new_parent;
371 fi->proc.parent_term_event = new_parent_term_event;
372 llist_add(&fi->proc.child, &new_parent->proc.children);
373 }
374}
375
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200376/*! delete a given instance of a FSM
Harald Welte136e7372016-05-29 10:53:17 +0900377 * \param[in] fsm The FSM to be un-registered and deleted
378 */
379void osmo_fsm_inst_free(struct osmo_fsm_inst *fi)
380{
Max3de97e12016-11-02 10:37:58 +0100381 LOGPFSM(fi, "Deallocated\n");
Harald Welte136e7372016-05-29 10:53:17 +0900382 osmo_timer_del(&fi->timer);
383 llist_del(&fi->list);
384 talloc_free(fi);
385}
386
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200387/*! get human-readable name of FSM event
Harald Welte136e7372016-05-29 10:53:17 +0900388 * \param[in] fsm FSM descriptor of event
389 * \param[in] event Event integer value
390 * \returns string rendering of the event
391 */
392const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event)
393{
394 static char buf[32];
395 if (!fsm->event_names) {
Pau Espin Pedrol4f8857e2017-06-18 12:23:00 +0200396 snprintf(buf, sizeof(buf), "%"PRIu32, event);
Harald Welte136e7372016-05-29 10:53:17 +0900397 return buf;
398 } else
399 return get_value_string(fsm->event_names, event);
400}
401
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200402/*! get human-readable name of FSM instance
Harald Welte136e7372016-05-29 10:53:17 +0900403 * \param[in] fi FSM instance
404 * \returns string rendering of the FSM identity
405 */
406const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi)
407{
408 if (!fi)
409 return "NULL";
410
411 if (fi->name)
412 return fi->name;
413 else
414 return fi->fsm->name;
415}
416
Philipp Maieraf6710f2018-11-16 17:45:40 +0100417/*! get human-readable name of FSM state
Harald Welte136e7372016-05-29 10:53:17 +0900418 * \param[in] fsm FSM descriptor
419 * \param[in] state FSM state number
420 * \returns string rendering of the FSM state
421 */
422const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
423{
424 static char buf[32];
425 if (state >= fsm->num_states) {
Pau Espin Pedrol4f8857e2017-06-18 12:23:00 +0200426 snprintf(buf, sizeof(buf), "unknown %"PRIu32, state);
Harald Welte136e7372016-05-29 10:53:17 +0900427 return buf;
428 } else
429 return fsm->states[state].name;
430}
431
Neels Hofmeyr407df022018-05-25 18:20:06 +0200432static int state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
433 bool keep_timer, unsigned long timeout_secs, int T,
434 const char *file, int line)
435{
436 struct osmo_fsm *fsm = fi->fsm;
437 uint32_t old_state = fi->state;
438 const struct osmo_fsm_state *st = &fsm->states[fi->state];
439
440 /* validate if new_state is a valid state */
441 if (!(st->out_state_mask & (1 << new_state))) {
442 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
443 "transition to state %s not permitted!\n",
444 osmo_fsm_state_name(fsm, new_state));
445 return -EPERM;
446 }
447
448 if (!keep_timer) {
449 /* delete the old timer */
450 osmo_timer_del(&fi->timer);
451 }
452
453 if (st->onleave)
454 st->onleave(fi, new_state);
455
456 LOGPFSMSRC(fi, file, line, "state_chg to %s\n",
457 osmo_fsm_state_name(fsm, new_state));
458 fi->state = new_state;
459 st = &fsm->states[new_state];
460
461 if (!keep_timer && timeout_secs) {
462 fi->T = T;
463 osmo_timer_schedule(&fi->timer, timeout_secs, 0);
464 }
465
466 /* Call 'onenter' last, user might terminate FSM from there */
467 if (st->onenter)
468 st->onenter(fi, old_state);
469
470 return 0;
471}
472
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200473/*! perform a state change of the given FSM instance
Harald Welte136e7372016-05-29 10:53:17 +0900474 *
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100475 * Best invoke via the osmo_fsm_inst_state_chg() macro which logs the source
476 * file where the state change was effected. Alternatively, you may pass \a
477 * file as NULL to use the normal file/line indication instead.
478 *
Neels Hofmeyr407df022018-05-25 18:20:06 +0200479 * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_*
Harald Welte136e7372016-05-29 10:53:17 +0900480 * function. It verifies that the existing state actually permits a
Neels Hofmeyr407df022018-05-25 18:20:06 +0200481 * transition to new_state.
Harald Welte136e7372016-05-29 10:53:17 +0900482 *
483 * timeout_secs and T are optional parameters, and only have any effect
484 * if timeout_secs is not 0. If the timeout function is used, then the
485 * new_state is entered, and the FSM instances timer is set to expire
486 * in timeout_secs functions. At that time, the FSM's timer_cb
487 * function will be called for handling of the timeout by the user.
488 *
489 * \param[in] fi FSM instance whose state is to change
490 * \param[in] new_state The new state into which we should change
491 * \param[in] timeout_secs Timeout in seconds (if !=0)
492 * \param[in] T Timer number (if \ref timeout_secs != 0)
Neels Hofmeyrb805cc12016-12-23 04:23:18 +0100493 * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)
494 * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)
Harald Welte136e7372016-05-29 10:53:17 +0900495 * \returns 0 on success; negative on error
496 */
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100497int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
498 unsigned long timeout_secs, int T,
499 const char *file, int line)
Harald Welte136e7372016-05-29 10:53:17 +0900500{
Neels Hofmeyr407df022018-05-25 18:20:06 +0200501 return state_chg(fi, new_state, false, timeout_secs, T, file, line);
502}
Harald Welte136e7372016-05-29 10:53:17 +0900503
Neels Hofmeyr407df022018-05-25 18:20:06 +0200504/*! perform a state change while keeping the current timer running.
505 *
506 * This is useful to keep a timeout across several states (without having to round the
507 * remaining time to seconds).
508 *
509 * Best invoke via the osmo_fsm_inst_state_chg_keep_timer() macro which logs the source
510 * file where the state change was effected. Alternatively, you may pass \a
511 * file as NULL to use the normal file/line indication instead.
512 *
513 * All changes to the FSM instance state must be made via an osmo_fsm_inst_state_chg_*
514 * function. It verifies that the existing state actually permits a
515 * transition to new_state.
516 *
517 * \param[in] fi FSM instance whose state is to change
518 * \param[in] new_state The new state into which we should change
519 * \param[in] file Calling source file (from osmo_fsm_inst_state_chg macro)
520 * \param[in] line Calling source line (from osmo_fsm_inst_state_chg macro)
521 * \returns 0 on success; negative on error
522 */
523int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
524 const char *file, int line)
525{
526 return state_chg(fi, new_state, true, 0, 0, file, line);
Harald Welte136e7372016-05-29 10:53:17 +0900527}
528
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200529/*! dispatch an event to an osmocom finite state machine instance
Harald Welte136e7372016-05-29 10:53:17 +0900530 *
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100531 * Best invoke via the osmo_fsm_inst_dispatch() macro which logs the source
532 * file where the event was effected. Alternatively, you may pass \a file as
533 * NULL to use the normal file/line indication instead.
534 *
Harald Welte136e7372016-05-29 10:53:17 +0900535 * Any incoming events to \ref osmo_fsm instances must be dispatched to
536 * them via this function. It verifies, whether the event is permitted
537 * based on the current state of the FSM. If not, -1 is returned.
538 *
539 * \param[in] fi FSM instance
540 * \param[in] event Event to send to FSM instance
541 * \param[in] data Data to pass along with the event
Neels Hofmeyrb805cc12016-12-23 04:23:18 +0100542 * \param[in] file Calling source file (from osmo_fsm_inst_dispatch macro)
543 * \param[in] line Calling source line (from osmo_fsm_inst_dispatch macro)
Harald Welte136e7372016-05-29 10:53:17 +0900544 * \returns 0 in case of success; negative on error
545 */
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100546int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
547 const char *file, int line)
Harald Welte136e7372016-05-29 10:53:17 +0900548{
549 struct osmo_fsm *fsm;
550 const struct osmo_fsm_state *fs;
551
552 if (!fi) {
Neels Hofmeyrc7155df2016-12-23 04:24:51 +0100553 LOGPSRC(DLGLOBAL, LOGL_ERROR, file, line,
Pau Espin Pedrol4f8857e2017-06-18 12:23:00 +0200554 "Trying to dispatch event %"PRIu32" to non-existent"
Neels Hofmeyrc7155df2016-12-23 04:24:51 +0100555 " FSM instance!\n", event);
Harald Welte136e7372016-05-29 10:53:17 +0900556 osmo_log_backtrace(DLGLOBAL, LOGL_ERROR);
557 return -ENODEV;
558 }
559
560 fsm = fi->fsm;
561 OSMO_ASSERT(fi->state < fsm->num_states);
562 fs = &fi->fsm->states[fi->state];
563
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100564 LOGPFSMSRC(fi, file, line,
565 "Received Event %s\n", osmo_fsm_event_name(fsm, event));
Harald Welte136e7372016-05-29 10:53:17 +0900566
567 if (((1 << event) & fsm->allstate_event_mask) && fsm->allstate_action) {
568 fsm->allstate_action(fi, event, data);
569 return 0;
570 }
571
572 if (!((1 << event) & fs->in_event_mask)) {
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100573 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
574 "Event %s not permitted\n",
575 osmo_fsm_event_name(fsm, event));
Harald Welte136e7372016-05-29 10:53:17 +0900576 return -1;
577 }
Philipp Maier3d4fb592018-05-15 10:06:22 +0200578
579 if (fs->action)
580 fs->action(fi, event, data);
Harald Welte136e7372016-05-29 10:53:17 +0900581
582 return 0;
583}
584
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200585/*! Terminate FSM instance with given cause
Harald Welte136e7372016-05-29 10:53:17 +0900586 *
587 * This safely terminates the given FSM instance by first iterating
588 * over all children and sending them a termination event. Next, it
589 * calls the FSM descriptors cleanup function (if any), followed by
590 * releasing any memory associated with the FSM instance.
591 *
592 * Finally, the parent FSM instance (if any) is notified using the
593 * parent termination event configured at time of FSM instance start.
594 *
595 * \param[in] fi FSM instance to be terminated
596 * \param[in] cause Cause / reason for termination
Neels Hofmeyrb805cc12016-12-23 04:23:18 +0100597 * \param[in] data Opaque event data to be passed with the parent term event
598 * \param[in] file Calling source file (from osmo_fsm_inst_term macro)
599 * \param[in] line Calling source line (from osmo_fsm_inst_term macro)
Harald Welte136e7372016-05-29 10:53:17 +0900600 */
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100601void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
602 enum osmo_fsm_term_cause cause, void *data,
603 const char *file, int line)
Harald Welte136e7372016-05-29 10:53:17 +0900604{
Neels Hofmeyr3faa0142016-12-18 23:41:41 +0100605 struct osmo_fsm_inst *parent;
Harald Welte136e7372016-05-29 10:53:17 +0900606 uint32_t parent_term_event = fi->proc.parent_term_event;
607
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +0100608 LOGPFSMSRC(fi, file, line, "Terminating (cause = %s)\n",
609 osmo_fsm_term_cause_name(cause));
Harald Welte136e7372016-05-29 10:53:17 +0900610
Philipp Maierd1f57932018-02-14 18:20:07 +0100611 /* graceful exit (optional) */
612 if (fi->fsm->pre_term)
613 fi->fsm->pre_term(fi, cause);
614
Harald Welte65900442018-02-09 09:58:57 +0000615 _osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL,
616 file, line);
617
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100618 /* delete ourselves from the parent */
Neels Hofmeyr3faa0142016-12-18 23:41:41 +0100619 parent = fi->proc.parent;
Philipp Maier23d31612018-01-16 18:50:23 +0100620 if (parent) {
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100621 LOGPFSMSRC(fi, file, line, "Removing from parent %s\n",
622 osmo_fsm_inst_name(parent));
Philipp Maier23d31612018-01-16 18:50:23 +0100623 llist_del(&fi->proc.child);
624 }
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100625
626 /* call destructor / clean-up function */
627 if (fi->fsm->cleanup)
628 fi->fsm->cleanup(fi, cause);
629
630 LOGPFSMSRC(fi, file, line, "Freeing instance\n");
Neels Hofmeyr3faa0142016-12-18 23:41:41 +0100631 /* Fetch parent again in case it has changed. */
632 parent = fi->proc.parent;
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100633 osmo_fsm_inst_free(fi);
634
635 /* indicate our termination to the parent */
636 if (parent && cause != OSMO_FSM_TERM_PARENT)
637 _osmo_fsm_inst_dispatch(parent, parent_term_event, data,
638 file, line);
639}
640
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200641/*! Terminate all child FSM instances of an FSM instance.
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100642 *
643 * Iterate over all children and send them a termination event, with the given
644 * cause. Pass OSMO_FSM_TERM_PARENT to avoid dispatching events from the
645 * terminated child FSMs.
646 *
647 * \param[in] fi FSM instance that should be cleared of child FSMs
648 * \param[in] cause Cause / reason for termination (OSMO_FSM_TERM_PARENT)
649 * \param[in] data Opaque event data to be passed with the parent term events
650 * \param[in] file Calling source file (from osmo_fsm_inst_term_children macro)
651 * \param[in] line Calling source line (from osmo_fsm_inst_term_children macro)
652 */
653void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi,
654 enum osmo_fsm_term_cause cause,
655 void *data,
656 const char *file, int line)
657{
658 struct osmo_fsm_inst *first_child, *last_seen_first_child;
659
Neels Hofmeyr06ac9b42016-12-20 12:05:19 +0100660 /* iterate over all children, starting from the beginning every time:
661 * terminating an FSM may emit events that cause other FSMs to also
662 * terminate and remove themselves from this list. */
663 last_seen_first_child = NULL;
664 while (!llist_empty(&fi->proc.children)) {
665 first_child = llist_entry(fi->proc.children.next,
666 typeof(*first_child),
667 proc.child);
668
669 /* paranoia: do not loop forever */
670 if (first_child == last_seen_first_child) {
671 LOGPFSMLSRC(fi, LOGL_ERROR, file, line,
672 "Internal error while terminating child"
673 " FSMs: a child FSM is stuck\n");
674 break;
675 }
676 last_seen_first_child = first_child;
677
Harald Welte136e7372016-05-29 10:53:17 +0900678 /* terminate child */
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100679 _osmo_fsm_inst_term(first_child, cause, data,
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100680 file, line);
Harald Welte136e7372016-05-29 10:53:17 +0900681 }
Harald Welte136e7372016-05-29 10:53:17 +0900682}
683
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +0100684const struct value_string osmo_fsm_term_cause_names[] = {
Neels Hofmeyr18080962016-12-16 13:43:54 +0100685 OSMO_VALUE_STRING(OSMO_FSM_TERM_PARENT),
686 OSMO_VALUE_STRING(OSMO_FSM_TERM_REQUEST),
687 OSMO_VALUE_STRING(OSMO_FSM_TERM_REGULAR),
688 OSMO_VALUE_STRING(OSMO_FSM_TERM_ERROR),
689 OSMO_VALUE_STRING(OSMO_FSM_TERM_TIMEOUT),
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +0100690 { 0, NULL }
691};
692
Harald Welte136e7372016-05-29 10:53:17 +0900693/*! @} */