libosmocore 1.9.0.192-1c24
Osmocom core library
fsm.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <stdint.h>
8#include <stdbool.h>
9
11#include <osmocom/core/timer.h>
12#include <osmocom/core/utils.h>
14
19struct osmo_fsm_inst;
20
32};
33
34extern const struct value_string osmo_fsm_term_cause_names[];
35static inline const char *osmo_fsm_term_cause_name(enum osmo_fsm_term_cause cause)
36{
38}
39
40
44 uint32_t in_event_mask;
48 const char *name;
50 void (*action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
52 void (*onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state);
54 void (*onleave)(struct osmo_fsm_inst *fi, uint32_t next_state);
55};
56
58struct osmo_fsm {
64 const char *name;
66 const struct osmo_fsm_state *states;
68 unsigned int num_states;
72 void (*allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
77 int (*timer_cb)(struct osmo_fsm_inst *fi);
84};
85
91 struct osmo_fsm *fsm;
93 const char *id;
95 const char *name;
97 void *priv;
101 uint32_t state;
102
104 int T;
107
109 struct {
121};
122
123void osmo_fsm_log_addr(bool log_addr);
124void osmo_fsm_log_timeouts(bool log_timeouts);
125void osmo_fsm_term_safely(bool term_safely);
126void osmo_fsm_set_dealloc_ctx(void *ctx);
127
135#define LOGPFSMSL(fi, subsys, level, fmt, args...) \
136 LOGPFSMSLSRC(fi, subsys, level, __FILE__, __LINE__, fmt, ## args)
137
148#define LOGPFSMSLSRC(fi, subsys, level, caller_file, caller_line, fmt, args...) \
149 LOGPSRC(subsys, level, \
150 caller_file, caller_line, \
151 "%s{%s}: " fmt, \
152 osmo_fsm_inst_name(fi), \
153 (fi) ? osmo_fsm_state_name((fi)->fsm, (fi)->state) : "fi=NULL", ## args)
154
155
162#define LOGPFSML(fi, level, fmt, args...) \
163 LOGPFSMLSRC(fi, level, __FILE__, __LINE__, fmt, ## args)
164
174#define LOGPFSMLSRC(fi, level, caller_file, caller_line, fmt, args...) \
175 LOGPFSMSLSRC(fi, (fi) ? (fi)->fsm->log_subsys : DLGLOBAL, level, \
176 caller_file, caller_line, fmt, ## args)
177
185#define LOGPFSM(fi, fmt, args...) \
186 LOGPFSML(fi, (fi) ? (fi)->log_level : LOGL_ERROR, fmt, ## args)
187
197#define LOGPFSMSRC(fi, caller_file, caller_line, fmt, args...) \
198 LOGPFSMLSRC(fi, (fi) ? (fi)->log_level : LOGL_ERROR, \
199 caller_file, caller_line, \
200 fmt, ## args)
201
202#define OSMO_T_FMT "%c%u"
203#define OSMO_T_FMT_ARGS(T) ((T) >= 0 ? 'T' : 'X'), ((T) >= 0 ? (T) : -(T))
204
205int osmo_fsm_register(struct osmo_fsm *fsm);
206void osmo_fsm_unregister(struct osmo_fsm *fsm);
207struct osmo_fsm *osmo_fsm_find_by_name(const char *name);
209 const char *name);
211 const char *id);
212struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
213 int log_level, const char *id);
215 struct osmo_fsm_inst *parent,
216 uint32_t parent_term_event);
217void osmo_fsm_inst_unlink_parent(struct osmo_fsm_inst *fi, void *ctx);
219 struct osmo_fsm_inst *new_parent,
220 uint32_t new_parent_term_event);
221void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
222
223int osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id);
224int osmo_fsm_inst_update_id_f(struct osmo_fsm_inst *fi, const char *fmt, ...);
225int osmo_fsm_inst_update_id_f_sanitize(struct osmo_fsm_inst *fi, char replace_with, const char *fmt, ...);
226
227const char *osmo_fsm_event_name(const struct osmo_fsm *fsm, uint32_t event);
228const char *osmo_fsm_inst_name(const struct osmo_fsm_inst *fi);
229const char *osmo_fsm_state_name(const struct osmo_fsm *fsm, uint32_t state);
230
232static inline const char *osmo_fsm_inst_state_name(struct osmo_fsm_inst *fi)
233{ return fi ? osmo_fsm_state_name(fi->fsm, fi->state) : "NULL"; }
234
241#define osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T) \
242 _osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T, \
243 __FILE__, __LINE__)
244int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
245 unsigned long timeout_secs, int T,
246 const char *file, int line);
247
248#define osmo_fsm_inst_state_chg_ms(fi, new_state, timeout_ms, T) \
249 _osmo_fsm_inst_state_chg_ms(fi, new_state, timeout_ms, T, \
250 __FILE__, __LINE__)
251int _osmo_fsm_inst_state_chg_ms(struct osmo_fsm_inst *fi, uint32_t new_state,
252 unsigned long timeout_ms, int T,
253 const char *file, int line);
254
264#define osmo_fsm_inst_state_chg_keep_timer(fi, new_state) \
265 _osmo_fsm_inst_state_chg_keep_timer(fi, new_state, \
266 __FILE__, __LINE__)
267int _osmo_fsm_inst_state_chg_keep_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
268 const char *file, int line);
269
278#define osmo_fsm_inst_state_chg_keep_or_start_timer(fi, new_state, timeout_secs, T) \
279 _osmo_fsm_inst_state_chg_keep_or_start_timer(fi, new_state, timeout_secs, T, \
280 __FILE__, __LINE__)
281int _osmo_fsm_inst_state_chg_keep_or_start_timer(struct osmo_fsm_inst *fi, uint32_t new_state,
282 unsigned long timeout_secs, int T,
283 const char *file, int line);
284
285#define osmo_fsm_inst_state_chg_keep_or_start_timer_ms(fi, new_state, timeout_ms, T) \
286 _osmo_fsm_inst_state_chg_keep_or_start_timer_ms(fi, new_state, timeout_ms, T, \
287 __FILE__, __LINE__)
288int _osmo_fsm_inst_state_chg_keep_or_start_timer_ms(struct osmo_fsm_inst *fi, uint32_t new_state,
289 unsigned long timeout_ms, int T,
290 const char *file, int line);
291
292
299#define osmo_fsm_inst_dispatch(fi, event, data) \
300 _osmo_fsm_inst_dispatch(fi, event, data, __FILE__, __LINE__)
301int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
302 const char *file, int line);
303
310#define osmo_fsm_inst_term(fi, cause, data) \
311 _osmo_fsm_inst_term(fi, cause, data, __FILE__, __LINE__)
312void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
313 enum osmo_fsm_term_cause cause, void *data,
314 const char *file, int line);
315
322#define osmo_fsm_inst_term_children(fi, cause, data) \
323 _osmo_fsm_inst_term_children(fi, cause, data, __FILE__, __LINE__)
325 enum osmo_fsm_term_cause cause,
326 void *data,
327 const char *file, int line);
328
335#define osmo_fsm_inst_broadcast_children(fi, cause, data) \
336 _osmo_fsm_inst_broadcast_children(fi, cause, data, __FILE__, __LINE__)
338 void *data, const char *file, int line);
339
const char * name
write Write running configuration to or terminal n Write configuration to the file(same as write file)\n") ALIAS(config_write_file
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:748
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:409
const char * osmo_fsm_inst_name(const struct osmo_fsm_inst *fi)
get human-readable name of FSM instance
Definition: fsm.c:593
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:802
osmo_fsm_term_cause
Definition: fsm.h:21
struct osmo_fsm * osmo_fsm_find_by_name(const char *name)
Definition: fsm.c:235
const char * osmo_fsm_event_name(const struct osmo_fsm *fsm, uint32_t event)
get human-readable name of FSM event
Definition: fsm.c:579
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:986
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:808
void osmo_fsm_unregister(struct osmo_fsm *fsm)
unregister a FSM from the core
Definition: fsm.c:305
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:372
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:517
int osmo_fsm_register(struct osmo_fsm *fsm)
register a FSM with the core
Definition: fsm.c:282
const struct value_string osmo_fsm_term_cause_names[]
Definition: fsm.c:1037
void osmo_fsm_inst_free(struct osmo_fsm_inst *fi)
delete a given instance of a FSM
Definition: fsm.c:536
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:1027
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:833
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:742
void osmo_fsm_term_safely(bool term_safely)
Enable safer way to deallocate cascades of terminating FSM instances.
Definition: fsm.c:168
void osmo_fsm_log_addr(bool log_addr)
specify if FSM instance addresses should be logged or not
Definition: fsm.c:122
void osmo_fsm_inst_unlink_parent(struct osmo_fsm_inst *fi, void *ctx)
unlink child FSM from its parent FSM.
Definition: fsm.c:500
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:472
static const char * osmo_fsm_term_cause_name(enum osmo_fsm_term_cause cause)
Definition: fsm.h:35
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:896
void osmo_fsm_log_timeouts(bool log_timeouts)
Enable or disable logging of timeout values for FSM instance state changes.
Definition: fsm.c:142
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:774
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:437
int osmo_fsm_inst_update_id(struct osmo_fsm_inst *fi, const char *id)
Change id of the FSM instance.
Definition: fsm.c:340
const char * osmo_fsm_state_name(const struct osmo_fsm *fsm, uint32_t state)
get human-readable name of FSM state
Definition: fsm.c:609
void osmo_fsm_set_dealloc_ctx(void *ctx)
Instead of deallocating FSM instances, move them to the given talloc context.
Definition: fsm.c:193
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
struct osmo_fsm_inst * osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm, const char *id)
Definition: fsm.c:262
struct osmo_fsm_inst * osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm, const char *name)
Definition: fsm.c:245
@ OSMO_FSM_TERM_REQUEST
terminate on explicit user request
Definition: fsm.h:25
@ OSMO_FSM_TERM_ERROR
erroneous termination of process
Definition: fsm.h:29
@ OSMO_FSM_TERM_TIMEOUT
termination due to time-out
Definition: fsm.h:31
@ OSMO_FSM_TERM_REGULAR
regular termination of process
Definition: fsm.h:27
@ OSMO_FSM_TERM_PARENT
terminate because parent terminated
Definition: fsm.h:23
uint8_t data[0]
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:54
event
uint8_t cause
Simple doubly linked list implementation.
(double) linked list header structure
Definition: linuxlist.h:46
a single instanceof an osmocom finite state machine
Definition: fsm.h:87
struct osmo_fsm_inst * parent
the parent FSM that has created us
Definition: fsm.h:111
struct osmo_fsm * fsm
back-pointer to the FSM of which we are an instance
Definition: fsm.h:91
const char * name
human readable fully-qualified name
Definition: fsm.h:95
int log_level
logging level for this FSM
Definition: fsm.h:99
void * priv
some private data of this instance
Definition: fsm.h:97
const char * id
human readable identifier
Definition: fsm.h:93
uint32_t state
current state of the FSM
Definition: fsm.h:101
struct llist_head child
llist_head linked to parent->proc.children
Definition: fsm.h:117
struct osmo_fsm_inst::@0 proc
support for fsm-based procedures
struct llist_head list
member in the fsm->instances list
Definition: fsm.h:89
int T
timer number for states with time-out
Definition: fsm.h:104
bool terminating
Indicator whether osmo_fsm_inst_term() was already invoked on this instance.
Definition: fsm.h:119
uint32_t parent_term_event
the event we should send upon termination
Definition: fsm.h:113
struct llist_head children
a list of children processes
Definition: fsm.h:115
struct osmo_timer_list timer
timer back-end for states with time-out
Definition: fsm.h:106
description of a rule in the FSM
Definition: fsm.h:42
void(* onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state)
function to be called just after entering the state
Definition: fsm.h:52
uint32_t out_state_mask
bit-mask to which other states this state may transiton
Definition: fsm.h:46
const char * name
human-readable name of this state
Definition: fsm.h:48
uint32_t in_event_mask
bit-mask of permitted input events for this state
Definition: fsm.h:44
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
void(* onleave)(struct osmo_fsm_inst *fi, uint32_t next_state)
function to be called just before leaving the state
Definition: fsm.h:54
a description of an osmocom finite state machine
Definition: fsm.h:58
uint32_t allstate_event_mask
bit-mask of events permitted in all states
Definition: fsm.h:70
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
int(* timer_cb)(struct osmo_fsm_inst *fi)
timer call-back for states with time-out.
Definition: fsm.h:77
unsigned int num_states
number of entries in states
Definition: fsm.h:68
int log_subsys
logging sub-system for this FSM
Definition: fsm.h:79
void(* cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
clean-up function, called during termination
Definition: fsm.h:74
const char * name
human readable name
Definition: fsm.h:64
const struct value_string * event_names
human-readable names of events
Definition: fsm.h:81
struct llist_head list
global list
Definition: fsm.h:60
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
struct llist_head instances
list of instances of this FSM
Definition: fsm.h:62
const struct osmo_fsm_state * states
table of state transition rules
Definition: fsm.h:66
A structure representing a single instance of a timer.
Definition: timer.h:52
A mapping between human-readable string and numeric value.
Definition: utils.h:51
Osmocom timer handling routines.