blob: fb182f359d7d28603857ff8dbd507e160dd81006 [file] [log] [blame]
Harald Welte136e7372016-05-29 10:53:17 +09001#pragma once
2
3#include <stdint.h>
4#include <stdbool.h>
5
6#include <osmocom/core/linuxlist.h>
7#include <osmocom/core/timer.h>
8#include <osmocom/core/utils.h>
9
10/*! \defgroup fsm Finite State Machine abstraction
11 * @{
12 */
13
14/*! \file fsm.h
Neels Hofmeyr87e45502017-06-20 00:17:59 +020015 * Finite State Machine
Harald Welte136e7372016-05-29 10:53:17 +090016 */
17
18struct osmo_fsm_inst;
19
20enum osmo_fsm_term_cause {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020021 /*! terminate because parent terminated */
Harald Welte136e7372016-05-29 10:53:17 +090022 OSMO_FSM_TERM_PARENT,
Neels Hofmeyr87e45502017-06-20 00:17:59 +020023 /*! terminate on explicit user request */
Harald Welte136e7372016-05-29 10:53:17 +090024 OSMO_FSM_TERM_REQUEST,
Neels Hofmeyr87e45502017-06-20 00:17:59 +020025 /*! regular termination of process */
Harald Welte136e7372016-05-29 10:53:17 +090026 OSMO_FSM_TERM_REGULAR,
Neels Hofmeyr87e45502017-06-20 00:17:59 +020027 /*! erroneous termination of process */
Harald Welte136e7372016-05-29 10:53:17 +090028 OSMO_FSM_TERM_ERROR,
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029 /*! termination due to time-out */
Harald Weltef627c0f2016-06-18 10:36:25 +020030 OSMO_FSM_TERM_TIMEOUT,
Harald Welte136e7372016-05-29 10:53:17 +090031};
32
Neels Hofmeyr5c5c78a2016-12-14 18:35:47 +010033extern const struct value_string osmo_fsm_term_cause_names[];
34static 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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040/*! description of a rule in the FSM */
Harald Welte136e7372016-05-29 10:53:17 +090041struct osmo_fsm_state {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042 /*! bit-mask of permitted input events for this state */
Harald Welte136e7372016-05-29 10:53:17 +090043 uint32_t in_event_mask;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020044 /*! bit-mask to which other states this state may transiton */
Harald Welte136e7372016-05-29 10:53:17 +090045 uint32_t out_state_mask;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 /*! human-readable name of this state */
Harald Welte136e7372016-05-29 10:53:17 +090047 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 /*! function to be called for events arriving in this state */
Harald Welte136e7372016-05-29 10:53:17 +090049 void (*action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 /*! function to be called just after entering the state */
Harald Welte136e7372016-05-29 10:53:17 +090051 void (*onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state);
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052 /*! function to be called just before leaving the state */
Harald Welte136e7372016-05-29 10:53:17 +090053 void (*onleave)(struct osmo_fsm_inst *fi, uint32_t next_state);
54};
55
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056/*! a description of an osmocom finite state machine */
Harald Welte136e7372016-05-29 10:53:17 +090057struct osmo_fsm {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058 /*! global list */
Harald Welte136e7372016-05-29 10:53:17 +090059 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020060 /*! list of instances of this FSM */
Harald Welte136e7372016-05-29 10:53:17 +090061 struct llist_head instances;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 /*! human readable name */
Harald Welte136e7372016-05-29 10:53:17 +090063 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064 /*! table of state transition rules */
Harald Welte136e7372016-05-29 10:53:17 +090065 const struct osmo_fsm_state *states;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066 /*! number of entries in \ref states */
Harald Welte136e7372016-05-29 10:53:17 +090067 unsigned int num_states;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068 /*! bit-mask of events permitted in all states */
Harald Welte136e7372016-05-29 10:53:17 +090069 uint32_t allstate_event_mask;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070 /*! function pointer to be called for allstate events */
Harald Welte136e7372016-05-29 10:53:17 +090071 void (*allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
Neels Hofmeyr87e45502017-06-20 00:17:59 +020072 /*! clean-up function, called during termination */
Harald Welte136e7372016-05-29 10:53:17 +090073 void (*cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074 /*! timer call-back for states with time-out.
Neels Hofmeyrdda5e792016-12-09 16:10:34 +010075 * \returns 1 to request termination, 0 to keep running. */
Harald Weltef627c0f2016-06-18 10:36:25 +020076 int (*timer_cb)(struct osmo_fsm_inst *fi);
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077 /*! logging sub-system for this FSM */
Harald Welte136e7372016-05-29 10:53:17 +090078 int log_subsys;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079 /*! human-readable names of events */
Harald Welte136e7372016-05-29 10:53:17 +090080 const struct value_string *event_names;
81};
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083/*! a single instanceof an osmocom finite state machine */
Harald Welte136e7372016-05-29 10:53:17 +090084struct osmo_fsm_inst {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085 /*! member in the fsm->instances list */
Harald Welte136e7372016-05-29 10:53:17 +090086 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020087 /*! back-pointer to the FSM of which we are an instance */
Harald Welte136e7372016-05-29 10:53:17 +090088 struct osmo_fsm *fsm;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020089 /*! human readable identifier */
Harald Welte136e7372016-05-29 10:53:17 +090090 const char *id;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020091 /*! human readable fully-qualified name */
Harald Welte136e7372016-05-29 10:53:17 +090092 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093 /*! some private data of this instance */
Harald Welte136e7372016-05-29 10:53:17 +090094 void *priv;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095 /*! logging level for this FSM */
Harald Welte136e7372016-05-29 10:53:17 +090096 int log_level;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097 /*! current state of the FSM */
Harald Welte136e7372016-05-29 10:53:17 +090098 uint32_t state;
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100 /*! timer number for states with time-out */
Harald Welte136e7372016-05-29 10:53:17 +0900101 int T;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200102 /*! timer back-end for states with time-out */
Harald Welte136e7372016-05-29 10:53:17 +0900103 struct osmo_timer_list timer;
104
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200105 /*! support for fsm-based procedures */
Harald Welte136e7372016-05-29 10:53:17 +0900106 struct {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107 /*! the parent FSM that has created us */
Harald Welte136e7372016-05-29 10:53:17 +0900108 struct osmo_fsm_inst *parent;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200109 /*! the event we should send upon termination */
Harald Welte136e7372016-05-29 10:53:17 +0900110 uint32_t parent_term_event;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200111 /*! a list of children processes */
Harald Welte136e7372016-05-29 10:53:17 +0900112 struct llist_head children;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113 /*! \ref llist_head linked to parent->proc.children */
Harald Welte136e7372016-05-29 10:53:17 +0900114 struct llist_head child;
115 } proc;
116};
117
118void osmo_fsm_log_addr(bool log_addr);
119
Neels Hofmeyr6a13e7f2016-12-14 17:37:34 +0100120#define LOGPFSML(fi, level, fmt, args...) \
121 LOGP((fi)->fsm->log_subsys, level, "%s{%s}: " fmt, \
Harald Welte136e7372016-05-29 10:53:17 +0900122 osmo_fsm_inst_name(fi), \
123 osmo_fsm_state_name((fi)->fsm, (fi)->state), ## args)
124
Neels Hofmeyr6a13e7f2016-12-14 17:37:34 +0100125#define LOGPFSM(fi, fmt, args...) \
126 LOGPFSML(fi, (fi)->log_level, fmt, ## args)
127
Neels Hofmeyreeacf902016-12-23 01:00:13 +0100128#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
Harald Welte136e7372016-05-29 10:53:17 +0900141int osmo_fsm_register(struct osmo_fsm *fsm);
Max8b25a3f2016-11-01 11:02:17 +0100142void osmo_fsm_unregister(struct osmo_fsm *fsm);
Harald Welte8808bb42017-01-07 11:11:03 +0100143struct osmo_fsm *osmo_fsm_find_by_name(const char *name);
Harald Welte4585e672017-04-16 17:23:56 +0200144struct osmo_fsm_inst *osmo_fsm_inst_find_by_name(const struct osmo_fsm *fsm,
145 const char *name);
146struct osmo_fsm_inst *osmo_fsm_inst_find_by_id(const struct osmo_fsm *fsm,
147 const char *id);
Harald Welte136e7372016-05-29 10:53:17 +0900148struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
149 int log_level, const char *id);
150struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm,
151 struct osmo_fsm_inst *parent,
152 uint32_t parent_term_event);
153void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
154
155const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event);
156const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi);
157const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state);
158
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159/*! return the name of the state the FSM instance is currently in. */
Neels Hofmeyrfca04bb2017-02-28 03:38:38 +0100160static inline const char *osmo_fsm_inst_state_name(struct osmo_fsm_inst *fi)
161{ return osmo_fsm_state_name(fi->fsm, fi->state); }
162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/*! perform a state change of the given FSM instance
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100164 *
165 * This is a macro that calls _osmo_fsm_inst_state_chg() with the given
166 * parameters as well as the caller's source file and line number for logging
167 * purposes. See there for documentation.
168 */
169#define osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T) \
170 _osmo_fsm_inst_state_chg(fi, new_state, timeout_secs, T, \
171 __BASE_FILE__, __LINE__)
172int _osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
173 unsigned long timeout_secs, int T,
174 const char *file, int line);
Harald Welte136e7372016-05-29 10:53:17 +0900175
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176/*! dispatch an event to an osmocom finite state machine instance
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100177 *
178 * This is a macro that calls _osmo_fsm_inst_dispatch() with the given
179 * parameters as well as the caller's source file and line number for logging
180 * purposes. See there for documentation.
181 */
182#define osmo_fsm_inst_dispatch(fi, event, data) \
183 _osmo_fsm_inst_dispatch(fi, event, data, __BASE_FILE__, __LINE__)
184int _osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data,
185 const char *file, int line);
186
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200187/*! Terminate FSM instance with given cause
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100188 *
189 * This is a macro that calls _osmo_fsm_inst_term() with the given parameters
190 * as well as the caller's source file and line number for logging purposes.
191 * See there for documentation.
192 */
193#define osmo_fsm_inst_term(fi, cause, data) \
194 _osmo_fsm_inst_term(fi, cause, data, __BASE_FILE__, __LINE__)
195void _osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
196 enum osmo_fsm_term_cause cause, void *data,
197 const char *file, int line);
Harald Welte136e7372016-05-29 10:53:17 +0900198
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200199/*! Terminate all child FSM instances of an FSM instance.
Neels Hofmeyrc014f602016-12-23 04:26:39 +0100200 *
201 * This is a macro that calls _osmo_fsm_inst_term_children() with the given
202 * parameters as well as the caller's source file and line number for logging
203 * purposes. See there for documentation.
204 */
205#define osmo_fsm_inst_term_children(fi, cause, data) \
206 _osmo_fsm_inst_term_children(fi, cause, data, __BASE_FILE__, __LINE__)
207void _osmo_fsm_inst_term_children(struct osmo_fsm_inst *fi,
208 enum osmo_fsm_term_cause cause,
209 void *data,
210 const char *file, int line);
211
Harald Welte136e7372016-05-29 10:53:17 +0900212/*! @} */