blob: fba5497b7ce6ca82b4088521f5a7d63594f433ed [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
15 * \brief Finite State Machine
16 */
17
18struct osmo_fsm_inst;
19
20enum osmo_fsm_term_cause {
21 /*! \brief terminate because parent terminated */
22 OSMO_FSM_TERM_PARENT,
23 /*! \brief terminate on explicit user request */
24 OSMO_FSM_TERM_REQUEST,
25 /*! \brief regular termination of process */
26 OSMO_FSM_TERM_REGULAR,
27 /*! \brief erroneous termination of process */
28 OSMO_FSM_TERM_ERROR,
Harald Weltef627c0f2016-06-18 10:36:25 +020029 /*! \brief termination due to time-out */
30 OSMO_FSM_TERM_TIMEOUT,
Harald Welte136e7372016-05-29 10:53:17 +090031};
32
33/*! \brief description of a rule in the FSM */
34struct osmo_fsm_state {
35 /*! \brief bit-mask of permitted input events for this state */
36 uint32_t in_event_mask;
37 /*! \brief bit-mask to which other states this state may transiton */
38 uint32_t out_state_mask;
39 /*! \brief human-readable name of this state */
40 const char *name;
41 /*! \brief function to be called for events arriving in this state */
42 void (*action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
43 /*! \brief function to be called just after entering the state */
44 void (*onenter)(struct osmo_fsm_inst *fi, uint32_t prev_state);
45 /*! \brief function to be called just before leaving the state */
46 void (*onleave)(struct osmo_fsm_inst *fi, uint32_t next_state);
47};
48
49/*! \brief a description of an osmocom finite state machine */
50struct osmo_fsm {
51 /*! \brief global list */
52 struct llist_head list;
53 /*! \brief list of instances of this FSM */
54 struct llist_head instances;
55 /*! \brief human readable name */
56 const char *name;
57 /*! \brief table of state transition rules */
58 const struct osmo_fsm_state *states;
59 /*! \brief number of entries in \ref states */
60 unsigned int num_states;
61 /*! \brief bit-mask of events permitted in all states */
62 uint32_t allstate_event_mask;
63 /*! \brief function pointer to be called for allstate events */
64 void (*allstate_action)(struct osmo_fsm_inst *fi, uint32_t event, void *data);
65 /*! \breif clean-up function, called during termination */
66 void (*cleanup)(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause);
Neels Hofmeyrdda5e792016-12-09 16:10:34 +010067 /*! \brief timer call-back for states with time-out.
68 * \returns 1 to request termination, 0 to keep running. */
Harald Weltef627c0f2016-06-18 10:36:25 +020069 int (*timer_cb)(struct osmo_fsm_inst *fi);
Harald Welte136e7372016-05-29 10:53:17 +090070 /*! \brief logging sub-system for this FSM */
71 int log_subsys;
72 /*! \brief human-readable names of events */
73 const struct value_string *event_names;
74};
75
76/*! \brief a single instanceof an osmocom finite state machine */
77struct osmo_fsm_inst {
78 /*! \brief member in the fsm->instances list */
79 struct llist_head list;
80 /*! \brief back-pointer to the FSM of which we are an instance */
81 struct osmo_fsm *fsm;
82 /*! \brief human readable identifier */
83 const char *id;
84 /*! \brief human readable fully-qualified name */
85 const char *name;
86 /*! \brief some private data of this instance */
87 void *priv;
88 /*! \brief logging level for this FSM */
89 int log_level;
90 /*! \brief current state of the FSM */
91 uint32_t state;
92
93 /*! \brief timer number for states with time-out */
94 int T;
95 /*! \brief timer back-end for states with time-out */
96 struct osmo_timer_list timer;
97
98 /*! \brief support for fsm-based procedures */
99 struct {
100 /*! \brief the parent FSM that has created us */
101 struct osmo_fsm_inst *parent;
102 /*! \brief the event we should send upon termination */
103 uint32_t parent_term_event;
104 /*! \brief a list of children processes */
105 struct llist_head children;
106 /*! \brief \ref llist_head linked to parent->proc.children */
107 struct llist_head child;
108 } proc;
109};
110
111void osmo_fsm_log_addr(bool log_addr);
112
113#define LOGPFSM(fi, fmt, args...) \
114 LOGP((fi)->fsm->log_subsys, (fi)->log_level, "%s{%s}: " fmt, \
115 osmo_fsm_inst_name(fi), \
116 osmo_fsm_state_name((fi)->fsm, (fi)->state), ## args)
117
118int osmo_fsm_register(struct osmo_fsm *fsm);
Max8b25a3f2016-11-01 11:02:17 +0100119void osmo_fsm_unregister(struct osmo_fsm *fsm);
Harald Welte136e7372016-05-29 10:53:17 +0900120struct osmo_fsm_inst *osmo_fsm_inst_alloc(struct osmo_fsm *fsm, void *ctx, void *priv,
121 int log_level, const char *id);
122struct osmo_fsm_inst *osmo_fsm_inst_alloc_child(struct osmo_fsm *fsm,
123 struct osmo_fsm_inst *parent,
124 uint32_t parent_term_event);
125void osmo_fsm_inst_free(struct osmo_fsm_inst *fi);
126
127const char *osmo_fsm_event_name(struct osmo_fsm *fsm, uint32_t event);
128const char *osmo_fsm_inst_name(struct osmo_fsm_inst *fi);
129const char *osmo_fsm_state_name(struct osmo_fsm *fsm, uint32_t state);
130
131int osmo_fsm_inst_state_chg(struct osmo_fsm_inst *fi, uint32_t new_state,
132 unsigned long timeout_secs, int T);
133int osmo_fsm_inst_dispatch(struct osmo_fsm_inst *fi, uint32_t event, void *data);
134
135void osmo_fsm_inst_term(struct osmo_fsm_inst *fi,
136 enum osmo_fsm_term_cause cause, void *data);
137
138/*! @} */