blob: 0b04f7698c5f964a990ecd8c4c77ef4caf39fa75 [file] [log] [blame]
Harald Weltefbd4f732012-05-06 23:29:42 +02001% M2UA / M3UA / SUA AS gsn_fsm according to RFC3868 4.3.1
2
3% (C) 2011-2012 by Harald Welte <laforge@gnumonks.org>
4%
5% All Rights Reserved
6%
7% This program is free software; you can redistribute it and/or modify
8% it under the terms of the GNU Affero General Public License as
9% published by the Free Software Foundation; either version 3 of the
10% License, or (at your option) any later version.
11%
12% This program is distributed in the hope that it will be useful,
13% but WITHOUT ANY WARRANTY; without even the implied warranty of
14% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15% GNU General Public License for more details.
16%
17% You should have received a copy of the GNU Affero General Public License
18% along with this program. If not, see <http://www.gnu.org/licenses/>.
19%
20% Additional Permission under GNU AGPL version 3 section 7:
21%
22% If you modify this Program, or any covered work, by linking or
23% combining it with runtime libraries of Erlang/OTP as released by
24% Ericsson on http://www.erlang.org (or a modified version of these
25% libraries), containing parts covered by the terms of the Erlang Public
26% License (http://www.erlang.org/EPLICENSE), the licensors of this
27% Program grant you additional permission to convey the resulting work
28% without the need to license the runtime libraries of Erlang/OTP under
29% the GNU Affero General Public License. Corresponding Source for a
30% non-source form of such a combination shall include the source code
31% for the parts of the runtime libraries of Erlang/OTP used as well as
32% that of the covered work.
33
34-module(xua_as_fsm).
35-author('Harald Welte <laforge@gnumonks.org>').
36-behaviour(gen_fsm).
37
38-include("osmo_util.hrl").
39-include("m3ua.hrl").
40
41% gen_fsm exports
Harald Welteee7964c2012-05-07 23:55:02 +020042-export([init/1, terminate/3, code_change/4, handle_event/3, handle_sync_event/4, handle_info/3]).
Harald Weltefbd4f732012-05-06 23:29:42 +020043
44% states in this FSM
45-export([as_down/2, as_inactive/2, as_active/2, as_pending/2]).
46
47% Timeouts in milliseconds
48-define(T_R_TIMEOUT, 2*60*100).
49
50-record(as_state, {
Harald Welteee7964c2012-05-07 23:55:02 +020051 as_sup_pid,
Harald Weltefbd4f732012-05-06 23:29:42 +020052 role,
53 t_r,
54 asp_list
55 }).
56
57%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58% gen_fsm callbacks
59%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60
Harald Welteee7964c2012-05-07 23:55:02 +020061init([AsSupPid]) when is_pid(AsSupPid) ->
Harald Weltefbd4f732012-05-06 23:29:42 +020062 AsState = #as_state{asp_list = [],
Harald Welteee7964c2012-05-07 23:55:02 +020063 as_sup_pid = AsSupPid,
Harald Weltefbd4f732012-05-06 23:29:42 +020064 role = sg},
65 {ok, as_down, AsState}.
66
67terminate(Reason, State, _LoopDat) ->
68 io:format("Terminating ~p in State ~p (Reason: ~p)~n",
69 [?MODULE, State, Reason]),
70 ok.
71
72code_change(_OldVsn, StateName, LoopDat, _Extra) ->
73 {ok, StateName, LoopDat}.
74
Harald Welteee7964c2012-05-07 23:55:02 +020075handle_sync_event({create_asp, Args}, From, State, LoopDat) ->
76 % resolve the ASP supervisor PID
77 AsSupPid = LoopDat#as_state.as_sup_pid,
78 AsChildList = supervisor:which_children(AsSupPid),
79 io:format("AsSupPid ~p, ChildList ~p~n", [AsSupPid, AsChildList]),
80 {asp_sup, AspSupPid, _, _} = lists:keyfind(asp_sup, 1, AsChildList),
81 % actually tell it to start a new ASP, prepend our own Pid
82 Ret = supervisor:start_child(AspSupPid, [self()|Args]),
83 LoopDatOut = case Ret of
84 {ok, AspPid} ->
85 link(AspPid),
86 LoopDat#as_state{asp_list = [AspPid|LoopDat#as_state.asp_list]};
87 {ok, AspPid, _} ->
88 link(AspPid),
89 LoopDat#as_state{asp_list = [AspPid|LoopDat#as_state.asp_list]};
90 _ ->
91 LoopDat
92 end,
93 {reply, Ret, State, LoopDatOut}.
94
Harald Weltefbd4f732012-05-06 23:29:42 +020095handle_event(Event, State, LoopDat) ->
96 io:format("Unknown Event ~p in state ~p~n", [Event, State]),
97 {next_state, State, LoopDat}.
98
99handle_info({'EXIT', Pid, Reason}, State, LoopDat) ->
100 io:format("EXIT from Process ~p (~p), cleaning up ASP list~n",
101 [Pid, Reason]),
102 % FIXME: send fake ASP-DOWN event about ASP to self
Harald Welteee7964c2012-05-07 23:55:02 +0200103 NewAspList = lists:delete(Pid, LoopDat#as_state.asp_list),
104 {next_state, State, LoopDat#as_state{asp_list = NewAspList}};
Harald Weltefbd4f732012-05-06 23:29:42 +0200105
106handle_info(Info, State, LoopDat) ->
107 io:format("Unknown Info ~p in state ~p~n", [Info, State]),
108 {next_state, State, LoopDat}.
109
110
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112% STATE "as_down"
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114
115as_down(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_INACTIVE',
116 spec_name = indication, parameters = _Params}, LoopDat) ->
117 % One ASP transitions into ASP-INACTIVE
118 next_state(as_inactive, LoopDat);
119
120as_down(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_DOWN',
121 spec_name = indication, parameters = _Params}, LoopDat) ->
122 % ignore
123 next_state(as_down, LoopDat).
124
125
126%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127% STATE "as_inactive"
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129
130as_inactive(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_DOWN',
131 spec_name = indication, parameters = AsPid}, LoopDat) ->
132 % One ASP transitions into ASP-DOWN
133 % FIXME: check if there are any other ASP != DOWN, if yes -> as_inactive
134 case check_any_other_asp_not_down(LoopDat, AsPid) of
135 true ->
136 next_state(as_inactive, LoopDat);
137 false ->
138 next_state(as_down, LoopDat)
139 end;
140
141as_inactive(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_ACTIVE',
142 spec_name = indication, parameters = Params}, LoopDat) ->
143 % One ASP transitions to ASP-ACTIVE
144 next_state(as_active, LoopDat);
145
146as_inactive(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_INACTIVE',
147 spec_name = indication, parameters = _Params}, LoopDat) ->
148 % ignore
149 next_state(as_inactive, LoopDat).
150
151
152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153% STATE "as_active"
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156as_active(#primitive{subsystem = 'ASPAS', gen_name = InactDown,
157 spec_name = indication, parameters = AspPid}, LoopDat) when
158 InactDown == 'ASP_DOWN'; InactDown == 'ASP_INACTIVE' ->
159 % One ASP transitions to ASP-INACTIVE
160 % check if there are other ASP in active, if yes -> as_active
161 case check_any_other_asp_in_active(LoopDat, AspPid) of
162 true ->
163 next_state(as_active, LoopDat);
164 false ->
165 {ok, Tr} = timer:apply_after(?T_R_TIMEOUT, gen_fsm, send_event,
166 [self(), {timer_expired, t_r}]),
167 next_state(as_pending, LoopDat#as_state{t_r = Tr})
168 end;
169
170as_active(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_ACTIVE',
171 spec_name = indication, parameters = _Params}, LoopDat) ->
172 % ignore
173 next_state(as_active, LoopDat).
174
175
176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177% STATE "as_pending"
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179
180as_pending(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_ACTIVE',
181 spec_name = indication}, LoopDat) ->
182 % One ASP transitions into ASP-ACTIVE
183 timer:cancel(LoopDat#as_state.t_r),
184 next_state(as_active, LoopDat);
185
186as_pending(#primitive{subsystem = 'ASPAS', gen_name = 'ASP_INACTIVE',
187 spec_name = indication, parameters = _Params}, LoopDat) ->
188 % ignore
189 next_state(as_pending, LoopDat);
190
191% FIXME: do we need to re-check as_pending state if we get ASP_DOWN of the last
192% inactive ASP ?
193
194as_pending({timer_expired, t_r}, LoopDat) ->
195 % check if there is at least one ASP in ASP-INACTIVE -> AS-INACTIVE
196 case check_any_other_asp_in_inactive(LoopDat, undefined) of
197 true ->
198 next_state(as_inactive, LoopDat);
199 false ->
200 next_state(as_down, LoopDat)
201 end.
202
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204% helper functions
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206
207next_state(NewState, LoopDat) ->
208 %FIXME Module:as_state_change(NewState, LoopDat#as_state.ext_state),
209 {next_state, NewState, LoopDat}.
210
211%create_asp(LoopDatIn = #as_state{asp_module = {AspModule, AspModuleArgs},
212% asp_list = AspListIn}) ->
213% Args = [AspModule, AspModuleArgs, UserFun, UserFunArgs, SctpPid],
214% {ok, AspPid} = gen_fsm:start_link(xua_asp_fsm, Args, [{debug, [trace]}]),
215% {AspPid, LoopDatIn#{asp_list = [AspPid|AspListIn]}}.
216
217
218
219check_any_other_asp_in_inactive(LoopDat, AspPid) ->
220 check_any_other_asp_in_state('ASP_INACTIVE', LoopDat, AspPid).
221
222check_any_other_asp_in_active(LoopDat, AspPid) ->
223 check_any_other_asp_in_state('ASP_ACTIVE', LoopDat, AspPid).
224
225check_any_other_asp_not_down(LoopDat, AspPid) ->
226 ListWithoutMe = lists:delete(AspPid, LoopDat#as_state.asp_list),
227 StateList = build_asp_state_list(ListWithoutMe),
228 not lists:all('ASP_DOWN', StateList).
229
230check_any_other_asp_in_state(State, LoopDat, AspPid) ->
231 ListWithoutMe = lists:delete(AspPid, LoopDat#as_state.asp_list),
232 StateList = build_asp_state_list(ListWithoutMe),
233 lists:member(State, StateList).
234
235build_asp_state_list(ListOfPids) ->
236 % FIXME
237 [].