fsm.h
Go to the documentation of this file.
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>
struct osmo_fsm_inst;
enum osmo_fsm_term_cause {
OSMO_FSM_TERM_PARENT,
OSMO_FSM_TERM_REQUEST,
OSMO_FSM_TERM_REGULAR,
OSMO_FSM_TERM_ERROR,
OSMO_FSM_TERM_TIMEOUT,
};
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);
}
struct osmo_fsm_state {
uint32_t in_event_mask;
uint32_t out_state_mask;
const char *name;
void (*action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
void (*onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state);
void (*onleave)(struct osmo_fsm_inst *fi, uint32_t next_state);
};
struct osmo_fsm {
struct llist_head list;
struct llist_head instances;
const char *name;
const struct osmo_fsm_state *states;
unsigned int num_states;
uint32_t allstate_event_mask;
void (*allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
void (*cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
int (*timer_cb)(struct osmo_fsm_inst *fi);
int log_subsys;
const struct value_string *event_names;
void (*pre_term)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
};
struct osmo_fsm_inst {
struct llist_head list;
struct osmo_fsm *fsm;
const char *id;
const char *name;
void *priv;
int log_level;
uint32_t state;
int T;
struct osmo_timer_list timer;
struct {
struct osmo_fsm_inst *parent;
uint32_t parent_term_event;
struct llist_head children;
struct llist_head child;
bool terminating;
} proc;
};
void osmo_fsm_log_addr(bool log_addr);
void osmo_fsm_log_timeouts(bool log_timeouts);
void osmo_fsm_term_safely(bool term_safely);
void osmo_fsm_set_dealloc_ctx(void *ctx);
#define LOGPFSMSL(fi, subsys, level, fmt, args...) \
LOGPFSMSLSRC(fi, subsys, level, __FILE__, __LINE__, fmt, ## args)
#define LOGPFSMSLSRC(fi, subsys, level, caller_file, caller_line, fmt, args...) \
LOGPSRC(subsys, level, \
caller_file, caller_line, \
"%s{%s}: " fmt, \
osmo_fsm_inst_name(fi), \
(fi) ? osmo_fsm_state_name((fi)->fsm, (fi)->state) : "fi=NULL", ## args)
#define LOGPFSML(fi, level, fmt, args...) \
LOGPFSMLSRC(fi, level, __FILE__, __LINE__, fmt, ## args)
#define LOGPFSMLSRC(fi, level, caller_file, caller_line, fmt, args...) \
LOGPFSMSLSRC(fi, (fi) ? (fi)->fsm->log_subsys : DLGLOBAL, level, \
caller_file, caller_line, fmt, ## args)
#define LOGPFSM(fi, fmt, args...) \
LOGPFSML(fi, (fi) ? (fi)->log_level : LOGL_ERROR, fmt, ## args)
#define LOGPFSMSRC(fi, caller_file, caller_line, fmt, args...) \
LOGPFSMLSRC(fi, (fi) ? (fi)->log_level : LOGL_ERROR, \
caller_file, caller_line, \
fmt, ## args)
#define OSMO_T_FMT "%c%u"
#define OSMO_T_FMT_ARGS(T) ((T) >= 0 ? 'T' : 'X'), ((T) >= 0 ? T : -T)
int osmo_fsm_register(struct osmo_fsm *fsm);
void osmo_fsm_unregister(struct osmo_fsm *fsm);
struct osmo_fsm *osmo_fsm_find_by_name(const char *name);
struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm,
const char *name);
struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm,
const char *id);
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_unlink_parent(struct osmo_fsm_inst *fi, void *ctx);
void osmo_fsm_inst_change_parent(struct osmo_fsm_inst *fi,
struct osmo_fsm_inst *new_parent,
uint32_t new_parent_term_event);
void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
int osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id);
int osmo_fsm_inst_update_id_f(struct osmo_fsm_inst *fi, const char *fmt, ...);
int osmo_fsm_inst_update_id_f_sanitize(struct osmo_fsm_inst *fi, char replace_with, const char *fmt, ...);
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);
static inline const char *osmo_fsm_inst_state_name(struct osmo_fsm_inst *fi)
{ return fi ? osmo_fsm_state_name(fi->fsm, fi->state) : "NULL"; }
#define osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T) \
_osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T, \
__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);
#define osmo_fsm_inst_state_chg_ms(fi, new_state, timeout_ms, T) \
_osmo_fsm_inst_state_chg_ms(fi, new_state, timeout_ms, T, \
__FILE__, __LINE__)
int _osmo_fsm_inst_state_chg_ms(struct osmo_fsm_inst *fi, uint32_t new_state,
unsigned long timeout_ms, int T,
const char *file, int line);
#define osmo_fsm_inst_state_chg_keep_timer(fi, new_state) \
_osmo_fsm_inst_state_chg_keep_timer(fi, new_state, \
__FILE__, __LINE__)
int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
const char *file, int line);
#define osmo_fsm_inst_state_chg_keep_or_start_timer(fi, new_state, timeout_secs, T) \
_osmo_fsm_inst_state_chg_keep_or_start_timer(fi, new_state, timeout_secs, T, \
__FILE__, __LINE__)
int _osmo_fsm_inst_state_chg_keep_or_start_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
unsigned long timeout_secs, int T,
const char *file, int line);
#define osmo_fsm_inst_state_chg_keep_or_start_timer_ms(fi, new_state, timeout_ms, T) \
_osmo_fsm_inst_state_chg_keep_or_start_timer_ms(fi, new_state, timeout_ms, T, \
__FILE__, __LINE__)
int _osmo_fsm_inst_state_chg_keep_or_start_timer_ms(struct osmo_fsm_inst *fi, uint32_t new_state,
unsigned long timeout_ms, int T,
const char *file, int line);
#define osmo_fsm_inst_dispatch(fi, event, data) \
_osmo_fsm_inst_dispatch(fi, event, data, __FILE__, __LINE__)
int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
const char *file, int line);
#define osmo_fsm_inst_term(fi, cause, data) \
_osmo_fsm_inst_term(fi, cause, data, __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);
#define osmo_fsm_inst_term_children(fi, cause, data) \
_osmo_fsm_inst_term_children(fi, cause, data, __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);
#define osmo_fsm_inst_broadcast_children(fi, cause, data) \
_osmo_fsm_inst_broadcast_children(fi, cause, data, __FILE__, __LINE__)
void _osmo_fsm_inst_broadcast_children(struct osmo_fsm_inst *fi, uint32_t event,
void *data, const char *file, int line);
name
const char * name
file
write Write running configuration to or terminal n Write configuration to the file(same as write file)\n") ALIAS(config_write_file
_osmo_fsm_inst_state_chg_ms
int _osmo_fsm_inst_state_chg_ms(struct osmo_fsm_inst *fi, uint32_t new_state, unsigned long timeout_ms, int T, const char *file, int line)
Definition: fsm.c:750
osmo_fsm_inst_update_id_f_sanitize
int osmo_fsm_inst_update_id_f_sanitize(struct osmo_fsm_inst *fi, char replace_with, const char *fmt,...)
Change id of the FSM instance using a string format, and ensuring a valid id.
Definition: fsm.c:414
_osmo_fsm_inst_state_chg_keep_or_start_timer
int _osmo_fsm_inst_state_chg_keep_or_start_timer(struct osmo_fsm_inst *fi, uint32_t new_state, unsigned long timeout_secs, int T, const char *file, int line)
perform a state change while keeping the current timer if running, or starting a timer otherwise.
Definition: fsm.c:804
osmo_fsm_term_cause
osmo_fsm_term_cause
Definition: fsm.h:21
osmo_fsm_find_by_name
struct osmo_fsm * osmo_fsm_find_by_name(const char *name)
Definition: fsm.c:240
_osmo_fsm_inst_term_children
void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause, void *data, const char *file, int line)
Terminate all child FSM instances of an FSM instance.
Definition: fsm.c:988
osmo_fsm_event_name
const char * osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event)
get human-readable name of FSM event
Definition: fsm.c:584
osmo_fsm_inst_state_name
static const char * osmo_fsm_inst_state_name(struct osmo_fsm_inst *fi)
return the name of the state the FSM instance is currently in.
Definition: fsm.h:232
_osmo_fsm_inst_state_chg_keep_or_start_timer_ms
int _osmo_fsm_inst_state_chg_keep_or_start_timer_ms(struct osmo_fsm_inst *fi, uint32_t new_state, unsigned long timeout_ms, int T, const char *file, int line)
Definition: fsm.c:810
osmo_fsm_unregister
void osmo_fsm_unregister(struct osmo_fsm *fsm)
unregister a FSM from the core
Definition: fsm.c:310
osmo_fsm_inst_update_id_f
int osmo_fsm_inst_update_id_f(struct osmo_fsm_inst *fi, const char *fmt,...)
Change id of the FSM instance using a string format.
Definition: fsm.c:377
osmo_fsm_inst_change_parent
void osmo_fsm_inst_change_parent(struct osmo_fsm_inst *fi, struct osmo_fsm_inst *new_parent, uint32_t new_parent_term_event)
change parent instance of an FSM.
Definition: fsm.c:522
osmo_fsm_register
int osmo_fsm_register(struct osmo_fsm *fsm)
register a FSM with the core
Definition: fsm.c:287
osmo_fsm_term_cause_names
const struct value_string osmo_fsm_term_cause_names[]
Definition: fsm.c:1039
osmo_fsm_inst_free
void osmo_fsm_inst_free(struct osmo_fsm_inst *fi)
delete a given instance of a FSM
Definition: fsm.c:541
_osmo_fsm_inst_broadcast_children
void _osmo_fsm_inst_broadcast_children(struct osmo_fsm_inst *fi, uint32_t event, void *data, const char *file, int line)
Broadcast an event to all the FSMs children.
Definition: fsm.c:1029
_osmo_fsm_inst_dispatch
int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data, const char *file, int line)
dispatch an event to an osmocom finite state machine instance
Definition: fsm.c:835
_osmo_fsm_inst_state_chg
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)
perform a state change of the given FSM instance
Definition: fsm.c:744
osmo_fsm_term_safely
void osmo_fsm_term_safely(bool term_safely)
Enable safer way to deallocate cascades of terminating FSM instances.
Definition: fsm.c:173
osmo_fsm_log_addr
void osmo_fsm_log_addr(bool log_addr)
specify if FSM instance addresses should be logged or not
Definition: fsm.c:127
osmo_fsm_term_cause_name
static const char * osmo_fsm_term_cause_name(enum osmo_fsm_term_cause cause)
Definition: fsm.h:35
osmo_fsm_inst_unlink_parent
void osmo_fsm_inst_unlink_parent(struct osmo_fsm_inst *fi, void *ctx)
unlink child FSM from its parent FSM.
Definition: fsm.c:505
osmo_fsm_inst_alloc_child
struct osmo_fsm_inst * osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm, struct osmo_fsm_inst *parent, uint32_t parent_term_event)
allocate a new instance of a specified FSM as child of other FSM instance
Definition: fsm.c:477
osmo_fsm_inst_name
const char * osmo_fsm_inst_name(struct osmo_fsm_inst *fi)
get human-readable name of FSM instance
Definition: fsm.c:598
_osmo_fsm_inst_term
void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause, void *data, const char *file, int line)
Terminate FSM instance with given cause.
Definition: fsm.c:898
osmo_fsm_log_timeouts
void osmo_fsm_log_timeouts(bool log_timeouts)
Enable or disable logging of timeout values for FSM instance state changes.
Definition: fsm.c:147
_osmo_fsm_inst_state_chg_keep_timer
int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state, const char *file, int line)
perform a state change while keeping the current timer running.
Definition: fsm.c:776
osmo_fsm_inst_alloc
struct osmo_fsm_inst * osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv, int log_level, const char *id)
allocate a new instance of a specified FSM
Definition: fsm.c:442
osmo_fsm_state_name
const char * osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
get human-readable name of FSM state
Definition: fsm.c:614
osmo_fsm_inst_update_id
int osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id)
Change id of the FSM instance.
Definition: fsm.c:345
osmo_fsm_set_dealloc_ctx
void osmo_fsm_set_dealloc_ctx(void *ctx)
Instead of deallocating FSM instances, move them to the given talloc context.
Definition: fsm.c:198
osmo_fsm_inst_find_by_id
struct osmo_fsm_inst * osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm, const char *id)
Definition: fsm.c:267
osmo_fsm_inst_find_by_name
struct osmo_fsm_inst * osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm, const char *name)
Definition: fsm.c:250
OSMO_FSM_TERM_REQUEST
@ OSMO_FSM_TERM_REQUEST
terminate on explicit user request
Definition: fsm.h:25
OSMO_FSM_TERM_ERROR
@ OSMO_FSM_TERM_ERROR
erroneous termination of process
Definition: fsm.h:29
OSMO_FSM_TERM_TIMEOUT
@ OSMO_FSM_TERM_TIMEOUT
termination due to time-out
Definition: fsm.h:31
OSMO_FSM_TERM_REGULAR
@ OSMO_FSM_TERM_REGULAR
regular termination of process
Definition: fsm.h:27
OSMO_FSM_TERM_PARENT
@ OSMO_FSM_TERM_PARENT
terminate because parent terminated
Definition: fsm.h:23
data
uint8_t data[0]
get_value_string
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:58
event
event
cause
uint8_t cause
linuxlist.h
Simple doubly linked list implementation.
logging.h
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
osmo_fsm_inst
a single instanceof an osmocom finite state machine
Definition: fsm.h:87
osmo_fsm_inst::parent
struct osmo_fsm_inst * parent
the parent FSM that has created us
Definition: fsm.h:111
osmo_fsm_inst::fsm
struct osmo_fsm * fsm
back-pointer to the FSM of which we are an instance
Definition: fsm.h:91
osmo_fsm_inst::name
const char * name
human readable fully-qualified name
Definition: fsm.h:95
osmo_fsm_inst::log_level
int log_level
logging level for this FSM
Definition: fsm.h:99
osmo_fsm_inst::priv
void * priv
some private data of this instance
Definition: fsm.h:97
osmo_fsm_inst::id
const char * id
human readable identifier
Definition: fsm.h:93
osmo_fsm_inst::state
uint32_t state
current state of the FSM
Definition: fsm.h:101
osmo_fsm_inst::child
struct llist_head child
llist_head linked to parent->proc.children
Definition: fsm.h:117
osmo_fsm_inst::proc
struct osmo_fsm_inst::@0 proc
support for fsm-based procedures
osmo_fsm_inst::list
struct llist_head list
member in the fsm->instances list
Definition: fsm.h:89
osmo_fsm_inst::T
int T
timer number for states with time-out
Definition: fsm.h:104
osmo_fsm_inst::terminating
bool terminating
Indicator whether osmo_fsm_inst_term() was already invoked on this instance.
Definition: fsm.h:119
osmo_fsm_inst::parent_term_event
uint32_t parent_term_event
the event we should send upon termination
Definition: fsm.h:113
osmo_fsm_inst::children
struct llist_head children
a list of children processes
Definition: fsm.h:115
osmo_fsm_inst::timer
struct osmo_timer_list timer
timer back-end for states with time-out
Definition: fsm.h:106
osmo_fsm_state
description of a rule in the FSM
Definition: fsm.h:42
osmo_fsm_state::onenter
void(* onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state)
function to be called just after entering the state
Definition: fsm.h:52
osmo_fsm_state::out_state_mask
uint32_t out_state_mask
bit-mask to which other states this state may transiton
Definition: fsm.h:46
osmo_fsm_state::name
const char * name
human-readable name of this state
Definition: fsm.h:48
osmo_fsm_state::in_event_mask
uint32_t in_event_mask
bit-mask of permitted input events for this state
Definition: fsm.h:44
osmo_fsm_state::action
void(* action)(struct osmo_fsm_inst *fi, uint32_t event, void *data)
function to be called for events arriving in this state
Definition: fsm.h:50
osmo_fsm_state::onleave
void(* onleave)(struct osmo_fsm_inst *fi, uint32_t next_state)
function to be called just before leaving the state
Definition: fsm.h:54
osmo_fsm
a description of an osmocom finite state machine
Definition: fsm.h:58
osmo_fsm::allstate_event_mask
uint32_t allstate_event_mask
bit-mask of events permitted in all states
Definition: fsm.h:70
osmo_fsm::allstate_action
void(* allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data)
function pointer to be called for allstate events
Definition: fsm.h:72
osmo_fsm::timer_cb
int(* timer_cb)(struct osmo_fsm_inst *fi)
timer call-back for states with time-out.
Definition: fsm.h:77
osmo_fsm::num_states
unsigned int num_states
number of entries in states
Definition: fsm.h:68
osmo_fsm::log_subsys
int log_subsys
logging sub-system for this FSM
Definition: fsm.h:79
osmo_fsm::cleanup
void(* cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
clean-up function, called during termination
Definition: fsm.h:74
osmo_fsm::name
const char * name
human readable name
Definition: fsm.h:64
osmo_fsm::event_names
const struct value_string * event_names
human-readable names of events
Definition: fsm.h:81
osmo_fsm::list
struct llist_head list
global list
Definition: fsm.h:60
osmo_fsm::pre_term
void(* pre_term)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
graceful exit function, called at the beginning of termination
Definition: fsm.h:83
osmo_fsm::instances
struct llist_head instances
list of instances of this FSM
Definition: fsm.h:62
osmo_fsm::states
const struct osmo_fsm_state * states
table of state transition rules
Definition: fsm.h:66
osmo_timer_list
A structure representing a single instance of a timer.
Definition: timer.h:56
value_string
A mapping between human-readable string and numeric value.
Definition: utils.h:42
timer.h
Osmocom timer handling routines.
utils.h