summaryrefslogtreecommitdiffstats
path: root/TCAP/src/ITU/tcap_ism_fsm.erl
blob: 2793f6c051664c61d211e7c43febfae65874d48a (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
%%% $Id: tcap_ism_fsm.erl,v 1.3 2005/08/04 09:33:17 vances Exp $
%%%---------------------------------------------------------------------
%%% @copyright 2010-2013 Harald Welte
%%% @author Harald Welte <laforge@gnumonks.org>
%%% @end
%%%
%%% Copyright (c) 2010-2013, Harald Welte <laforge@gnumonks.org>
%%% 
%%% All rights reserved.
%%% 
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions
%%% are met:
%%% 
%%%    - Redistributions of source code must retain the above copyright
%%%      notice, this list of conditions and the following disclaimer.
%%%    - Redistributions in binary form must reproduce the above copyright
%%%      notice, this list of conditions and the following disclaimer in
%%%      the documentation and/or other materials provided with the 
%%%      distribution.
%%%    - Neither the name of Motivity Telecom nor the names of its
%%%      contributors may be used to endorse or promote products derived
%%%      from this software without specific prior written permission.
%%%
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
%%% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
%%% OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
%%% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
%%% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
%%% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
%%% THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
%%% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
%%% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%%%
%%%---------------------------------------------------------------------
%%%
%%% @doc Invocation State Machine (ISM) functional block within the
%%% 		component sub-layer of ITU TCAP.
%%%
%%% @reference ANSI T1.114.4 Transaction Capabilities Procedures 
%%% @reference ITU-T Q.774 (06/97) Annex A Transaction capabilities SDLs
%%%
%%% @private
%%%

-module(tcap_ism_fsm).
-copyright('Copyright (c) 2010-2013 Harald Welte').
-author('laforge@gnumonks.org').
-vsn('$Revision: 1.3 $').

-behaviour(gen_fsm).

%% call backs needed for gen_fsm behaviour
-export([init/1, handle_event/3, handle_info/3, terminate/3, code_change/4]).

%% invocation_fsm state callbacks 
-export([start_link/5]).

-export([idle/2, op_sent_cl1/2, op_sent_cl2/2, op_sent_cl3/2,
	 op_sent_cl4/2, wait_for_reject/2]).

%% record definitions for TC-User primitives
-include("tcap.hrl").
%% record definitions for TCAP messages
%-include("TCAPMessages.hrl").

%% the invocation_fsm state data
-record(state, {
	usap,		% Pid of the TC-User
	dialogueId,
	invokeId,
	cco,		% Pid of the CCO
	op_class,	% operation class (1..4)
	inv_timeout,	% milliseconds
	inv_timer,	% timer()
	rej_timer	% timer()
}).

% value in milliseconds, spec doesn't say how long...
-define(REJECT_TIMER, 1 * 1000).

start_link(Usap, DialogueId, InvokeId, OpClass, InvTimeout) ->
	ProcName = list_to_atom("ism_fsm_" ++ integer_to_list(DialogueId)
				++ "_" ++ integer_to_list(InvokeId)),
	ArgL = [Usap, DialogueId, InvokeId, self(), OpClass, InvTimeout],
	gen_fsm:start_link({local, ProcName}, ?MODULE, ArgL, [{debug, [trace]}]).

% DHA needs to tell us: USAP, DialogueID, InvokeID, CCO-PID, OpClass, InvTimer
init([Usap, DialogueId, InvokeId, CcoPid, OpClass, InvTimeout]) ->
	% we need to trap EXIT signals, as otherwise we might be terminated by
	% the dialogue END before we have delivered the components to the TC-User.
	process_flag(trap_exit, true),
	State = #state{usap = Usap, dialogueId = DialogueId,
			invokeId = InvokeId, cco = CcoPid,
			op_class = OpClass, inv_timeout = InvTimeout},
	{ok, idle, State}.

%%----------------------------------------------------------------------
%%  The gen_fsm call backs
%%----------------------------------------------------------------------

%% Start the Invocation State Machine (ISM) process
%% reference: Figure A.7/Q.774 (sheet 1 of 6)

% CCO -> ISM: Operation sent
idle('operation-sent', State) ->
	% start invocation timer
	Tinv = timer:apply_after(State#state.inv_timeout, gen_fsm,
				 send_all_state_event,
				 [self(), {timer_expired, invoke}]),
	case State#state.op_class of
		1 ->
			StateName = op_sent_cl1;
		2 ->
			StateName = op_sent_cl2;
		3 ->
			StateName = op_sent_cl3;
		4 ->
			StateName = op_sent_cl4
	end,
	{next_state, StateName, State#state{inv_timer = Tinv}}.

op_sent_cl1(P=#'TC-RESULT-L'{}, State) ->
	% Figure A.7/Q.774 (2 of 6)
	% TC-RESULT-L.ind to user
	gen_fsm:send_event(State#state.usap, {'TC', 'RESULT-L', indication, P}),
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	% start reject timer
	Trej = start_reject_timer(),
	{next_state, wait_for_reject, State#state{rej_timer = Trej}};
op_sent_cl1(P=#'TC-U-REJECT'{}, State) ->
	% Figure A.7/Q.774 (2 of 6)
	% TC-U-ERROR.ind to user
	gen_fsm:send_event(State#state.usap, {'TC', 'U-ERROR', indication, P}),
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	% start reject timer
	Trej = start_reject_timer(),
	{next_state, wait_for_reject, State#state{rej_timer = Trej}};
op_sent_cl1(P=#'TC-RESULT-NL'{}, State) ->
	% Figure A.7/Q.774 (2 of 6)
	% TC-RESULT-NL.ind to user
	gen_fsm:send_event(State#state.usap, {'TC', 'RESULT-NL', indication, P}),
	{next_state, op_sent_cl1, State};
op_sent_cl1('terminate', State) ->
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	{stop, normal, State}.

wait_for_reject('terminate', State) ->
	% stop reject timer
	timer:cancel(State#state.rej_timer),
	{stop, normal, State};
wait_for_reject({timer_expired, reject}, State) ->
	% reject timer expiry
	% terminate
	{stop, normal, State}.

op_sent_cl2(P=#'TC-U-REJECT'{}, State) ->
	% TC-U-ERROR.ind to user
	gen_fsm:send_event(State#state.usap, {'TC', 'U-REJECT', indication, P}),
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	% start reject timer
	Trej = start_reject_timer(),
	{next_state, wait_for_reject, State#state{rej_timer = Trej}};
op_sent_cl2(Op, State) when
			is_record(Op, 'TC-RESULT-L');
			is_record(Op, 'TC-RESULT-NL') ->
	% Generate REJ component to CCO
	Problem = {'ReturnResultProblem', resultResponseUnexpected},
	Reject = #'TC-R-REJECT'{dialogueID = State#state.dialogueId,
				invokeID = State#state.invokeId,
				problemCode = Problem},
	gen_server:cast(State#state.cco, {reject_component, Reject}),
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	% terminate
	{stop, normal, State};
op_sent_cl2('terminate', State) ->
	% terminate
	{stop, normal, State}.

op_sent_cl3(P=#'TC-RESULT-L'{}, State) ->
	% Figure A.7/Q.774 (5 of 6)
	% TC-RESULT-L.ind to user
	gen_fsm:send_event(State#state.usap, {'TC', 'RESULT-L', indication, P}),
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	% start reject timer
	Trej = start_reject_timer(),
	{next_state, wait_for_reject, State#state{rej_timer = Trej}};
op_sent_cl3(P=#'TC-RESULT-NL'{}, State) ->
	% TC-RESULT-NL.ind to user
	gen_fsm:send_event(State#state.usap, {'TC', 'RESULT-NL', indication, P}),
	{next_state, op_sent_cl3, State};
op_sent_cl3('terminate', State) ->
	% stop invocation timter
	timer:cancel(State#state.inv_timer),
	% terminate
	{stop, normal, State}.

op_sent_cl4('terminate', State) ->
	% terminate
	{stop, normal, State};
op_sent_cl4(Op, State) ->
	% Figure A.7/Q.774 (6 of 6)
	% generate REJ component to CCO
	Problem = {'ReturnResultProblem', resultResponseUnexpected},
	Reject = #'TC-R-REJECT'{dialogueID = State#state.dialogueId,
				invokeID = State#state.invokeId,
				problemCode = Problem},
	gen_server:cast(State#state.cco, {reject_component, Reject}),
	% stop invocation timer
	timer:cancel(State#state.inv_timer),
	% terminate
	{stop, normal, State}.

handle_event({timer_expired, invoke}, _StateName, State) ->
	% invocation timer expiry
	#state{dialogueId = DlgId, invokeId = InvId} = State,
	% TC-L-CANCEL.ind to user
	P = #'TC-L-CANCEL'{dialogueID = DlgId, invokeID = InvId},
	gen_fsm:send_event(State#state.usap, {'TC', 'L-CANCEL', indication, P}),
	{stop, normal, State}.

%% handle any other message
handle_info(Info, StateName, State) ->
	error_logger:format("~w (~w) received unexpected message: ~w~n", [?MODULE, self(), Info]),
	{next_state, StateName, State}.

%% handle a shutdown request
terminate(Reason, StateName, State) ->
	timer:cancel(State#state.inv_timer),
	timer:cancel(State#state.rej_timer),
	error_logger:format("~w (~w) terminating Reason: ~w in state ~w~n",
			    [?MODULE, self(), Reason, StateName]).

%% handle updating state data due to a code replacement
code_change(_OldVsn, StateName, State, _Extra) ->
	{ok, StateName, State}.


start_reject_timer() ->
	timer:apply_after(?REJECT_TIMER, gen_fsm,
			  send_event,
			  [self(), {timer_expired, reject}]).