libosmocore  0.9.6-9.20170220git32ee5af8.fc28
Osmocom core library
fsm.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <stdbool.h>
5 
7 #include <osmocom/core/timer.h>
8 #include <osmocom/core/utils.h>
9 
18 struct osmo_fsm_inst;
19 
31 };
32 
33 extern const struct value_string osmo_fsm_term_cause_names[];
34 static inline const char *osmo_fsm_term_cause_name(enum osmo_fsm_term_cause cause)
35 {
36  return get_value_string(osmo_fsm_term_cause_names, cause);
37 }
38 
39 
43  uint32_t in_event_mask;
45  uint32_t out_state_mask;
47  const char *name;
49  void (*action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
51  void (*onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state);
53  void (*onleave)(struct osmo_fsm_inst *fi, uint32_t next_state);
54 };
55 
57 struct osmo_fsm {
59  struct llist_head list;
63  const char *name;
65  const struct osmo_fsm_state *states;
67  unsigned int num_states;
71  void (*allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
73  void (*cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
76  int (*timer_cb)(struct osmo_fsm_inst *fi);
80  const struct value_string *event_names;
81 };
82 
84 struct osmo_fsm_inst {
86  struct llist_head list;
88  struct osmo_fsm *fsm;
90  const char *id;
92  const char *name;
94  void *priv;
96  int log_level;
98  uint32_t state;
99 
101  int T;
104 
106  struct {
115  } proc;
116 };
117 
118 void osmo_fsm_log_addr(bool log_addr);
119 
120 #define LOGPFSML(fi, level, fmt, args...) \
121  LOGP((fi)->fsm->log_subsys, level, "%s{%s}: " fmt, \
122  osmo_fsm_inst_name(fi), \
123  osmo_fsm_state_name((fi)->fsm, (fi)->state), ## args)
124 
125 #define LOGPFSM(fi, fmt, args...) \
126  LOGPFSML(fi, (fi)->log_level, fmt, ## args)
127 
128 #define LOGPFSMLSRC(fi, level, caller_file, caller_line, fmt, args...) \
129  LOGPSRC((fi)->fsm->log_subsys, level, \
130  caller_file, caller_line, \
131  "%s{%s}: " fmt, \
132  osmo_fsm_inst_name(fi), \
133  osmo_fsm_state_name((fi)->fsm, (fi)->state), \
134  ## args)
135 
136 #define LOGPFSMSRC(fi, caller_file, caller_line, fmt, args...) \
137  LOGPFSMLSRC(fi, (fi)->log_level, \
138  caller_file, caller_line, \
139  fmt, ## args)
140 
141 int osmo_fsm_register(struct osmo_fsm *fsm);
142 void osmo_fsm_unregister(struct osmo_fsm *fsm);
143 struct osmo_fsm *osmo_fsm_find_by_name(const char *name);
144 struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
145  int log_level, const char *id);
147  struct osmo_fsm_inst *parent,
148  uint32_t parent_term_event);
149 void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
150 
151 const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event);
152 const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi);
153 const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state);
154 
161 #define osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T) \
162  _osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T, \
163  __BASE_FILE__, __LINE__)
164 int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
165  unsigned long timeout_secs, int T,
166  const char *file, int line);
167 
174 #define osmo_fsm_inst_dispatch(fi, event, data) \
175  _osmo_fsm_inst_dispatch(fi, event, data, __BASE_FILE__, __LINE__)
176 int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
177  const char *file, int line);
178 
185 #define osmo_fsm_inst_term(fi, cause, data) \
186  _osmo_fsm_inst_term(fi, cause, data, __BASE_FILE__, __LINE__)
187 void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
188  enum osmo_fsm_term_cause cause, void *data,
189  const char *file, int line);
190 
197 #define osmo_fsm_inst_term_children(fi, cause, data) \
198  _osmo_fsm_inst_term_children(fi, cause, data, __BASE_FILE__, __LINE__)
200  enum osmo_fsm_term_cause cause,
201  void *data,
202  const char *file, int line);
203 
uint32_t state
current state of the FSM
Definition: fsm.h:98
struct osmo_fsm_inst * parent
the parent FSM that has created us
Definition: fsm.h:108
int log_subsys
logging sub-system for this FSM
Definition: fsm.h:78
uint32_t out_state_mask
bit-mask to which other states this state may transiton
Definition: fsm.h:45
const char * name
human-readable name of this state
Definition: fsm.h:47
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:379
const char * name
human readable fully-qualified name
Definition: fsm.h:92
int(* timer_cb)(struct osmo_fsm_inst *fi)
timer call-back for states with time-out.
Definition: fsm.h:76
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:323
uint32_t allstate_event_mask
bit-mask of events permitted in all states
Definition: fsm.h:69
const char * get_value_string(const struct value_string *vs, uint32_t val)
get human-readable string for given value
Definition: utils.c:51
void osmo_fsm_inst_free(struct osmo_fsm_inst *fi)
delete a given instance of a FSM
Definition: fsm.c:246
void osmo_fsm_log_addr(bool log_addr)
specify if FSM instance addresses should be logged or not
Definition: fsm.c:101
osmo_fsm_term_cause
Definition: fsm.h:20
int T
timer number for states with time-out
Definition: fsm.h:101
void osmo_fsm_unregister(struct osmo_fsm *fsm)
unregister a FSM from the core
Definition: fsm.c:141
struct osmo_fsm * fsm
back-pointer to the FSM of which we are an instance
Definition: fsm.h:88
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:432
const char * osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event)
get human-readable name of FSM event
Definition: fsm.c:259
struct llist_head list
global list
Definition: fsm.h:59
const char * id
human readable identifier
Definition: fsm.h:90
A mapping between human-readable string and numeric value.
Definition: utils.h:27
uint32_t parent_term_event
the event we should send upon termination
Definition: fsm.h:110
void(* onleave)(struct osmo_fsm_inst *fi, uint32_t next_state)
function to be called just before leaving the state
Definition: fsm.h:53
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:479
erroneous termination of process
Definition: fsm.h:28
unsigned int num_states
number of entries in states
Definition: fsm.h:67
struct llist_head instances
list of instances of this FSM
Definition: fsm.h:61
void(* cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
clean-up function, called during termination
Definition: fsm.h:73
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:174
Osmocom timer handling routines.
const char * name
human readable name
Definition: fsm.h:63
struct osmo_timer_list timer
timer back-end for states with time-out
Definition: fsm.h:103
void * priv
some private data of this instance
Definition: fsm.h:94
a single instanceof an osmocom finite state machine
Definition: fsm.h:84
void(* allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data)
function pointer to be called for allstate events
Definition: fsm.h:71
int log_level
logging level for this FSM
Definition: fsm.h:96
Simple doubly linked list implementation.
struct llist_head list
member in the fsm->instances list
Definition: fsm.h:86
struct osmo_fsm_inst::@0 proc
support for fsm-based procedures
(double) linked list header structure
Definition: linuxlist.h:47
const char * osmo_fsm_inst_name(struct osmo_fsm_inst *fi)
get human-readable name of FSM instance
Definition: fsm.c:273
a description of an osmocom finite state machine
Definition: fsm.h:57
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:49
struct llist_head children
a list of children processes
Definition: fsm.h:112
description of a rule in the FSM
Definition: fsm.h:41
const struct osmo_fsm_state * states
table of state transition rules
Definition: fsm.h:65
void(* onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state)
function to be called just after entering the state
Definition: fsm.h:51
const struct value_string * event_names
human-readable names of events
Definition: fsm.h:80
struct llist_head child
llist_head linked to parent->proc.children
Definition: fsm.h:114
terminate because parent terminated
Definition: fsm.h:22
const char * osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state)
get human-readable name of FSM instance
Definition: fsm.c:289
A structure representing a single instance of a timer.
Definition: timer.h:54
int osmo_fsm_register(struct osmo_fsm *fsm)
register a FSM with the core
Definition: fsm.c:124
uint32_t in_event_mask
bit-mask of permitted input events for this state
Definition: fsm.h:43
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:220
regular termination of process
Definition: fsm.h:26
termination due to time-out
Definition: fsm.h:30
terminate on explicit user request
Definition: fsm.h:24