aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/new_fsm.py
blob: e584150cf3913789031fab21fee90f118beb6bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
import jinja2
import sys

def as_tuple(str_or_tuple):
	if isinstance(str_or_tuple, str):
		return (str_or_tuple,)
	return tuple(str_or_tuple)

class State:
	def __init__(s, name, events, out_states, onenter=True):
		s.name = name
		s.const = name.upper()
		s.events = as_tuple(events)
		s.out_states = as_tuple(out_states)
		s.onenter = onenter
	def __eq__(s, name):
		return s.name == name

class Event:
	def __init__(s, name):
		s.name = name
		s.const = name.upper()

	def __eq__(s, name):
		return s.name == name

class FSM:
	def NAME(s, name):
		return '_'.join((s.prefix, name)).upper()

	def name(s, name):
		return '_'.join((s.prefix, name)).lower()

	def state_const(s, name):
		return s.NAME('ST_' + name)

	def event_const(s, name):
		return s.NAME('EV_' + name)

	def __init__(s, prefix, priv, states, head=''):
		s.head = head
		s.prefix = prefix
		s.priv = priv
		s.states = states
		for state in s.states:
			state.const = s.state_const(state.name)

			out_state_class_insts = []
			for out_state in state.out_states:
				out_state_class_insts.append(s.states[s.states.index(out_state)])
			state.out_states = out_state_class_insts

		s.events = []
		for state in s.states:
			state_event_class_insts = []
			for event in state.events:
				if event not in s.events:
					ev = Event(event)
					ev.const = s.event_const(event)
					s.events.append(ev)
				else:
					ev = s.events[s.events.index(event)]
				state_event_class_insts.append(ev)
			state.events = state_event_class_insts

	def to_c(s):
		template = jinja2.Template(
'''
{{head}}
#include <osmocom/core/utils.h>
#include <osmocom/core/fsm.h>

enum {{prefix}}_fsm_state {
{% for state in states %}	{{state.const}},
{% endfor -%}
};

enum {{prefix}}_fsm_event {
{% for event in events %}	{{event.const}},
{% endfor -%}
};

static const struct value_string {{prefix}}_fsm_event_names[] = {
{% for event in events %}	OSMO_VALUE_STRING({{event.const}}),
{% endfor %}	{}
};

static struct osmo_fsm {{prefix}}_fsm;

struct {{priv}} *{{prefix}}_alloc(struct osmo_fsm_inst *parent_fi, uint32_t parent_event_term)
{
	struct {{priv}} *{{priv}};

	struct osmo_fsm_inst *fi = osmo_fsm_inst_alloc_child(&{{prefix}}_fsm, parent_fi, parent_event_term);
	OSMO_ASSERT(fi);

	{{priv}} = talloc(fi, struct {{priv}});
	OSMO_ASSERT({{priv}});
	fi->priv = {{priv}};
	*{{priv}} = (struct {{priv}}){
		.fi = fi,
	};

	return {{priv}};
}

static int {{prefix}}_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	//struct {{priv}} *{{priv}} = fi->priv;
	/* Return 1 to terminate FSM instance, 0 to keep running */
	return 1;
}
{% for state in states %}
{%- if state.onenter %}
void {{prefix}}_{{state.name}}_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	//struct {{priv}} *{{priv}} = fi->priv;
	// FIXME
}
{%  endif %}
static void {{prefix}}_{{state.name}}_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	//struct {{priv}} *{{priv}} = fi->priv;

	switch (event) {
	{% for event in state.events %}
	case {{event.const}}:
		// FIXME
		break;
	{% endfor %}
	default:
		OSMO_ASSERT(false);
	}
}
{% endfor %}
#define S(x)    (1 << (x))

static const struct osmo_fsm_state {{prefix}}_fsm_states[] = {
{% for state in states %}	[{{state.const}}] = {
		.name = "{{state.name}}",
		.in_event_mask = 0
{% for event in state.events %}			| S({{event.const}})
{% endfor %}			,
		.out_state_mask = 0
{% for out_state in state.out_states %}			| S({{out_state.const}})
{% endfor %}			,{% if state.onenter %}
		.onenter = {{prefix}}_{{state.name}}_onenter,{% endif %}
		.action = {{prefix}}_{{state.name}}_action,
	},
{% endfor -%}
};

static struct osmo_fsm {{prefix}}_fsm = {
	.name = "{{prefix}}",
	.states = {{prefix}}_fsm_states,
	.num_states = ARRAY_SIZE({{prefix}}_fsm_states),
	.log_subsys = DLGLOBAL, // FIXME
	.event_names = {{prefix}}_fsm_event_names,
	.timer_cb = {{prefix}}_fsm_timer_cb,
};

static __attribute__((constructor)) void {{prefix}}_fsm_register(void)
{
	OSMO_ASSERT(osmo_fsm_register(&{{prefix}}_fsm) == 0);
}
''')

		return template.render(**vars(s))

fsm = FSM(head='#include <osmocom/hlr/proxy.h>',
	  prefix = 'proxy_mm',
	  priv = 'proxy_subscr',
	  states = (
		    State('ready',
			  ('subscr_invalid', 'rx_gsup_lu', 'rx_gsup_sai', ),
			  ('wait_subscr_data', 'wait_gsup_isd_result', 'wait_auth_tuples',),
			  onenter=False,
			 ),
		    State('wait_subscr_data',
			  ('rx_subscr_data',),
			  ('wait_gsup_isd_result', 'ready'),
			 ),
		    State('wait_gsup_isd_result',
			  ('rx_gsup_isd_result',),
			  ('ready',),
			 ),
		    State('wait_auth_tuples',
			  ('rx_auth_tuples',),
			  ('ready',),
			 ),
		   )
	 )
with open('proxy_mm.c', 'w') as f:
	f.write(fsm.to_c())

all_home_events = ('home_hlr_resolved',
		   'rx_insert_subscriber_data_req', 'rx_update_location_result',
		   'rx_send_auth_info_result',
		   'check_tuples','confirm_lu',)
all_home_states = ('wait_home_hlr_resolved', 'wait_update_location_result', 'wait_send_auth_info_result', 'idle', 'clear',)
fsm = FSM(head='#include <osmocom/hlr/proxy.h>',
	  prefix = 'proxy_to_home',
	  priv = 'proxy_subscr',
	  states = [State(state, all_home_events, all_home_states) for state in all_home_states],
	 )
with open('proxy_to_home.c', 'w') as f:
	f.write(fsm.to_c())