2016-05-29 01:53:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/linuxlist.h>
|
|
|
|
#include <osmocom/core/timer.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
|
|
|
|
|
|
|
/*! \defgroup fsm Finite State Machine abstraction
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file fsm.h
|
|
|
|
* \brief Finite State Machine
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct osmo_fsm_inst;
|
|
|
|
|
|
|
|
enum osmo_fsm_term_cause {
|
|
|
|
/*! \brief terminate because parent terminated */
|
|
|
|
OSMO_FSM_TERM_PARENT,
|
|
|
|
/*! \brief terminate on explicit user request */
|
|
|
|
OSMO_FSM_TERM_REQUEST,
|
|
|
|
/*! \brief regular termination of process */
|
|
|
|
OSMO_FSM_TERM_REGULAR,
|
|
|
|
/*! \brief erroneous termination of process */
|
|
|
|
OSMO_FSM_TERM_ERROR,
|
2016-06-18 08:36:25 +00:00
|
|
|
/*! \brief termination due to time-out */
|
|
|
|
OSMO_FSM_TERM_TIMEOUT,
|
2016-05-29 01:53:17 +00:00
|
|
|
};
|
|
|
|
|
2016-12-14 17:35:47 +00:00
|
|
|
extern const struct value_string osmo_fsm_term_cause_names[];
|
|
|
|
static inline const char *osmo_fsm_term_cause_name(enum osmo_fsm_term_cause cause)
|
|
|
|
{
|
|
|
|
return get_value_string(osmo_fsm_term_cause_names, cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-29 01:53:17 +00:00
|
|
|
/*! \brief description of a rule in the FSM */
|
|
|
|
struct osmo_fsm_state {
|
|
|
|
/*! \brief bit-mask of permitted input events for this state */
|
|
|
|
uint32_t in_event_mask;
|
|
|
|
/*! \brief bit-mask to which other states this state may transiton */
|
|
|
|
uint32_t out_state_mask;
|
|
|
|
/*! \brief human-readable name of this state */
|
|
|
|
const char *name;
|
|
|
|
/*! \brief function to be called for events arriving in this state */
|
|
|
|
void (*action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
|
|
|
|
/*! \brief function to be called just after entering the state */
|
|
|
|
void (*onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state);
|
|
|
|
/*! \brief function to be called just before leaving the state */
|
|
|
|
void (*onleave)(struct osmo_fsm_inst *fi, uint32_t next_state);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief a description of an osmocom finite state machine */
|
|
|
|
struct osmo_fsm {
|
|
|
|
/*! \brief global list */
|
|
|
|
struct llist_head list;
|
|
|
|
/*! \brief list of instances of this FSM */
|
|
|
|
struct llist_head instances;
|
|
|
|
/*! \brief human readable name */
|
|
|
|
const char *name;
|
|
|
|
/*! \brief table of state transition rules */
|
|
|
|
const struct osmo_fsm_state *states;
|
|
|
|
/*! \brief number of entries in \ref states */
|
|
|
|
unsigned int num_states;
|
|
|
|
/*! \brief bit-mask of events permitted in all states */
|
|
|
|
uint32_t allstate_event_mask;
|
|
|
|
/*! \brief function pointer to be called for allstate events */
|
|
|
|
void (*allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
|
2016-12-20 11:06:05 +00:00
|
|
|
/*! \brief clean-up function, called during termination */
|
2016-05-29 01:53:17 +00:00
|
|
|
void (*cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
|
2016-12-09 15:10:34 +00:00
|
|
|
/*! \brief timer call-back for states with time-out.
|
|
|
|
* \returns 1 to request termination, 0 to keep running. */
|
2016-06-18 08:36:25 +00:00
|
|
|
int (*timer_cb)(struct osmo_fsm_inst *fi);
|
2016-05-29 01:53:17 +00:00
|
|
|
/*! \brief logging sub-system for this FSM */
|
|
|
|
int log_subsys;
|
|
|
|
/*! \brief human-readable names of events */
|
|
|
|
const struct value_string *event_names;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*! \brief a single instanceof an osmocom finite state machine */
|
|
|
|
struct osmo_fsm_inst {
|
|
|
|
/*! \brief member in the fsm->instances list */
|
|
|
|
struct llist_head list;
|
|
|
|
/*! \brief back-pointer to the FSM of which we are an instance */
|
|
|
|
struct osmo_fsm *fsm;
|
|
|
|
/*! \brief human readable identifier */
|
|
|
|
const char *id;
|
|
|
|
/*! \brief human readable fully-qualified name */
|
|
|
|
const char *name;
|
|
|
|
/*! \brief some private data of this instance */
|
|
|
|
void *priv;
|
|
|
|
/*! \brief logging level for this FSM */
|
|
|
|
int log_level;
|
|
|
|
/*! \brief current state of the FSM */
|
|
|
|
uint32_t state;
|
|
|
|
|
|
|
|
/*! \brief timer number for states with time-out */
|
|
|
|
int T;
|
|
|
|
/*! \brief timer back-end for states with time-out */
|
|
|
|
struct osmo_timer_list timer;
|
|
|
|
|
|
|
|
/*! \brief support for fsm-based procedures */
|
|
|
|
struct {
|
|
|
|
/*! \brief the parent FSM that has created us */
|
|
|
|
struct osmo_fsm_inst *parent;
|
|
|
|
/*! \brief the event we should send upon termination */
|
|
|
|
uint32_t parent_term_event;
|
|
|
|
/*! \brief a list of children processes */
|
|
|
|
struct llist_head children;
|
|
|
|
/*! \brief \ref llist_head linked to parent->proc.children */
|
|
|
|
struct llist_head child;
|
|
|
|
} proc;
|
|
|
|
};
|
|
|
|
|
|
|
|
void osmo_fsm_log_addr(bool log_addr);
|
|
|
|
|
2016-12-14 16:37:34 +00:00
|
|
|
#define LOGPFSML(fi, level, fmt, args...) \
|
|
|
|
LOGP((fi)->fsm->log_subsys, level, "%s{%s}: " fmt, \
|
2016-05-29 01:53:17 +00:00
|
|
|
osmo_fsm_inst_name(fi), \
|
|
|
|
osmo_fsm_state_name((fi)->fsm, (fi)->state), ## args)
|
|
|
|
|
2016-12-14 16:37:34 +00:00
|
|
|
#define LOGPFSM(fi, fmt, args...) \
|
|
|
|
LOGPFSML(fi, (fi)->log_level, fmt, ## args)
|
|
|
|
|
2016-12-23 00:00:13 +00:00
|
|
|
#define LOGPFSMLSRC(fi, level, caller_file, caller_line, fmt, args...) \
|
|
|
|
LOGPSRC((fi)->fsm->log_subsys, level, \
|
|
|
|
caller_file, caller_line, \
|
|
|
|
"%s{%s}: " fmt, \
|
|
|
|
osmo_fsm_inst_name(fi), \
|
|
|
|
osmo_fsm_state_name((fi)->fsm, (fi)->state), \
|
|
|
|
## args)
|
|
|
|
|
|
|
|
#define LOGPFSMSRC(fi, caller_file, caller_line, fmt, args...) \
|
|
|
|
LOGPFSMLSRC(fi, (fi)->log_level, \
|
|
|
|
caller_file, caller_line, \
|
|
|
|
fmt, ## args)
|
|
|
|
|
2016-05-29 01:53:17 +00:00
|
|
|
int osmo_fsm_register(struct osmo_fsm *fsm);
|
2016-11-01 10:02:17 +00:00
|
|
|
void osmo_fsm_unregister(struct osmo_fsm *fsm);
|
2016-05-29 01:53:17 +00:00
|
|
|
struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
|
|
|
|
int log_level, const char *id);
|
|
|
|
struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm,
|
|
|
|
struct osmo_fsm_inst *parent,
|
|
|
|
uint32_t parent_term_event);
|
|
|
|
void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
|
|
|
|
|
|
|
|
const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event);
|
|
|
|
const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi);
|
|
|
|
const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state);
|
|
|
|
|
fsm: log caller's source for events and state changes, not fsm.c lines
When looking at log output, it is not interesting to see that a state
transition's petty details are implemented in fsm.c. Rather log the *caller's*
source file and line that caused an event, state change and cascading events.
To that end, introduce LOGPSRC() absorbing the guts of LOGP(), to be able to
explicitly pass the source file and line information.
Prepend an underscore to the function names of osmo_fsm_inst_state_chg(),
osmo_fsm_inst_dispatch() and osmo_fsm_inst_term(), and add file and line
arguments to them. Provide the previous names as macros that insert the
caller's __BASE_FILE__ and __LINE__ constants for the new arguments. Hence no
calling code needs to be changed.
In fsm.c, add LOGPFSMSRC to call LOGPSRC, and add LOGPFSMLSRC, and use them in
above _osmo_fsm_inst_* functions.
In addition, in _osmo_fsm_inst_term(), pass the caller's source file and line
on to nested event dispatches, so showing where a cascade originated from.
Change-Id: Iae72aba7bbf99e19dd584ccabea5867210650dcd
2016-12-14 16:24:54 +00:00
|
|
|
/*! \brief perform a state change of the given FSM instance
|
|
|
|
*
|
|
|
|
* This is a macro that calls _osmo_fsm_inst_state_chg() with the given
|
|
|
|
* parameters as well as the caller's source file and line number for logging
|
|
|
|
* purposes. See there for documentation.
|
|
|
|
*/
|
|
|
|
#define osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T) \
|
|
|
|
_osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T, \
|
|
|
|
__BASE_FILE__, __LINE__)
|
|
|
|
int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
|
|
|
|
unsigned long timeout_secs, int T,
|
|
|
|
const char *file, int line);
|
|
|
|
|
|
|
|
/*! \brief dispatch an event to an osmocom finite state machine instance
|
|
|
|
*
|
|
|
|
* This is a macro that calls _osmo_fsm_inst_dispatch() with the given
|
|
|
|
* parameters as well as the caller's source file and line number for logging
|
|
|
|
* purposes. See there for documentation.
|
|
|
|
*/
|
|
|
|
#define osmo_fsm_inst_dispatch(fi, event, data) \
|
|
|
|
_osmo_fsm_inst_dispatch(fi, event, data, __BASE_FILE__, __LINE__)
|
|
|
|
int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
|
|
|
|
const char *file, int line);
|
|
|
|
|
|
|
|
/*! \brief Terminate FSM instance with given cause
|
|
|
|
*
|
|
|
|
* This is a macro that calls _osmo_fsm_inst_term() with the given parameters
|
|
|
|
* as well as the caller's source file and line number for logging purposes.
|
|
|
|
* See there for documentation.
|
|
|
|
*/
|
|
|
|
#define osmo_fsm_inst_term(fi, cause, data) \
|
|
|
|
_osmo_fsm_inst_term(fi, cause, data, __BASE_FILE__, __LINE__)
|
|
|
|
void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
|
|
|
|
enum osmo_fsm_term_cause cause, void *data,
|
|
|
|
const char *file, int line);
|
2016-05-29 01:53:17 +00:00
|
|
|
|
fsm: factor out osmo_fsm_inst_term_children() from osmo_fsm_inst_term()
osmo_fsm_inst_term() has code for safe child removal, publish that part as
osmo_fsm_inst_term_children(); also use from osmo_fsm_inst_term().
As with osmo_fsm_inst_term(), add osmo_fsm_inst_term_children() macro to pass
the caller's source file and line to new _osmo_fsm_inst_term_children().
Rationale: in openbsc's VLR, I want to discard child FSMs when certain events
are handled. I could keep a pointer to each one, or simply iterate all
children, making the code a lot simpler in some places.
(Unfortunately, the patch may be displayed subobtimally. This really only moves
the children-loop to a new function, replaces it with a call to
_osmo_fsm_inst_term_children(fi, OSMO_FSM_TERM_PARENT, NULL, file, line) and
drops two local iterator variables. No other code changes are made, even though
the diff may show large removal + addition chunks)
Change-Id: I8dac1206259cbd251660f793ad023aaa1dc705a2
2016-12-23 03:26:39 +00:00
|
|
|
/*! \brief Terminate all child FSM instances of an FSM instance.
|
|
|
|
*
|
|
|
|
* This is a macro that calls _osmo_fsm_inst_term_children() with the given
|
|
|
|
* parameters as well as the caller's source file and line number for logging
|
|
|
|
* purposes. See there for documentation.
|
|
|
|
*/
|
|
|
|
#define osmo_fsm_inst_term_children(fi, cause, data) \
|
|
|
|
_osmo_fsm_inst_term_children(fi, cause, data, __BASE_FILE__, __LINE__)
|
|
|
|
void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi,
|
|
|
|
enum osmo_fsm_term_cause cause,
|
|
|
|
void *data,
|
|
|
|
const char *file, int line);
|
|
|
|
|
2016-05-29 01:53:17 +00:00
|
|
|
/*! @} */
|