summaryrefslogtreecommitdiffstats
path: root/TCAP/src/ITU/tcap_cco_server.erl
blob: 610ee7671b8336da04ae9c86dfb4778790cbe08b (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
%%% $Id: tcap_cco_server.erl,v 1.3 2005/08/04 09:33:17 vances Exp $
%%%---------------------------------------------------------------------
%%% @copyright 2010-2011 Harald Welte
%%% @author Harald Welte <laforge@gnumonks.org>
%%% @end
%%%
%%% Copyright (c) 2010-2011, Harald Welte
%%% 
%%% 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 TCAP Component Coordinator (CCO) functional block within the
%%% 		component sub-layer of ITU TCAP.
%%%
%%% @reference ITU-T Q.774 (06/97) Annex A Transaction capabilities SDLs
%%%
%%% @private
%%%

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

-behaviour(gen_server).

%% call backs needed for gen_server behaviour
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
	terminate/2, code_change/3]).

-include("tcap.hrl").
-include("TR.hrl").
-include("TC.hrl").

-record(state, {supervisor, usap, dialogueID, components, dha, ism}).

-record(component, {asn_ber, user_prim}).

%%----------------------------------------------------------------------
%%  The gen_server call backs
%%----------------------------------------------------------------------

%% initialize the server
init([Supervisor, USAP, DialogueID]) ->
	process_flag(trap_exit, true),
	DHA = list_to_atom("tcap_dha_" ++ integer_to_list(DialogueID)),
	{ok, #state{supervisor = Supervisor, usap = USAP, dha = DHA,
		    dialogueID = DialogueID, components = [], ism = []}}.

%% set the DHA pid
handle_call(set_dha, From, State) ->
	{noreply, State#state{dha = From}};

%% shutdown the server
handle_call(stop, _From, State) ->
	{stop, shutdown, ok, State};

%% unrecognized calls
handle_call(Other, From, State) ->
	error_logger:error_report([{unknown_call, Other}, {from, From}]),
	{noreply, State}.

% from TCU: TC-INVOKE.req
handle_cast({'TC','INVOKE',request, InvParam}, State)
			when is_record(InvParam, 'TC-INVOKE') ->
	% assemble INVOKE component
	AsnRosRec = uprim_to_asn_rec(InvParam),
	Component = #component{user_prim = InvParam, asn_ber = AsnRosRec},
	% mark it as available
	NewState = add_components_to_state(State, Component),
	% what to do with class and timeout?
	{noreply, NewState};

% from TCU: TC-U-CANCEL.req
handle_cast({'TC','U-CANCEL',request, Param}, State)
			when is_record(Param, 'TC-U-CANCEL') ->
	InvokeId = Param#'TC-U-CANCEL'.invokeID,
	OldComps = State#state.components,
	% if there are any INV componnents waiting, discard them
	NewComps = discard_inv_component(OldComps, InvokeId),
	case NewComps of
	    OldComps ->
		% if not, check any active ISM, if yes, terminate ISM
		NewISMs = terminate_active_ISM(State#state.ism,
						InvokeId),
		NewState = State#state{ism = NewISMs};
	    _ ->
		NewState = State#state{components = NewComps}
	end,
	{noreply, NewState};

% from DHA -> CCO: dialogue-terminated
handle_cast('dialogue-terminated', State) ->
	% discard components awaiting transmission
	%	* automatically released
	% if any ISM active, terminate ISM
	%	* ISMs are linked, they should terminate
	terminate_ISMs(State#state.ism),
	% terminate
	{stop, normal, State};

% from TCL -> CHA (CCO): TC-RESULT-{L,NL}, U-ERROR
handle_cast({'TC', Req, request, Param}, State) when 
				Req == 'RESULT-L';
				Req == 'RESULT-NL';
				Req == 'U-ERROR' ->
	% Figure A.6/Q.774 (1 of 4)
	% assemble requested component
	AsnRosRec = uprim_to_asn_rec(Param),
	Component = #component{user_prim = Param, asn_ber = AsnRosRec}, %FIXME
	% mark component available for this dialogue
	NewState = add_components_to_state(State, Component),
	{noreply, NewState};

% TCL->CHA: TC-U-REJECT.req
handle_cast({'TC','U-REJECT',request, Param}, State)
			when is_record(Param, 'TC-U-REJECT') ->
	% assemble reject component
	AsnRosRec = uprim_to_asn_rec(Param),
	Component = #component{user_prim = Param, asn_ber = AsnRosRec}, %FIXME
	% FIXME: if probelm type Result/Error, terminate ISM
	% mark component available for this dialogue
	NewState = add_components_to_state(State, Component),
	{noreply, NewState};

% DHA -> CHA (CCO): Components received
handle_cast({components, Components}, State) ->
	% Figure A.6/Q.774 (2 of 4)
	process_rx_components(State#state.ism, State#state.usap,
			      State#state.dialogueID, Components),
	{noreply, State};

% ISM -> CCO: Generate REJ component
handle_cast({reject_component, Reject}, State) ->
	Component = #component{user_prim = Reject, asn_ber = 0}, %FIXME
	NewState = add_components_to_state(State, Component),
	{noreply, NewState};

% DHA -> CHA (CCO)
handle_cast('request-components', State = #state{components=CompIn}) ->
	% Figure A.6/Q.774 (4 of 4)
	case CompIn of
	    [] ->
		% if no components, signal 'no-components' to DHA
		gen_fsm:send_event(State#state.dha, 'no-component'),
		NewState = State;
	    _ ->
		% for each component
		{CompOut, ISMs} = process_request_components(CompIn, State),
		% signal 'requested-components' to DHA
		gen_fsm:send_event(State#state.dha,
				   {'requested-components', CompOut}),
		NewState = State#state{ism = State#state.ism ++ ISMs,
					components = []}
	end,
	{noreply, NewState};

%% unrecognized casts
handle_cast(Other, State) ->
	error_logger:error_report([{unknown_cast, Other}]),
	{noreply, State}.


%% trapped exit signals
handle_info({'EXIT', _Pid, Reason}, State) ->
	{stop, Reason, State};

%% unknown messages
handle_info(Unknown, State) ->
	error_logger:error_msg("Received unknown message: ~p~n", [Unknown]),
	{noreply, State}.

%% someone wants us to shutdown and cleanup
terminate(_Reason, _State) -> ok.

%% upgrading the running code
code_change(_, _, _) -> ok.

%%%
%%% internal functions
%%%

asn_val(Foo) ->
	case Foo of
		undefined ->
			asn1_NOVALIE;
		[] ->
			asn1_NOVALUE;
		Foo ->
			Foo
	end.

% convert invoke ID from user-input format to what ASN1RT expects
inv_id_to_asn_rec(undefined) ->
	asn1_NOVALUE;
inv_id_to_asn_rec(Int) when is_integer(Int) ->
	{present, Int}.

inv_id_to_uprim({present, Inv}) ->
	Inv;
inv_id_to_uprim(undefined) ->
	undefined;
inv_id_to_uprim(asn1_NOVALUE) ->
	undefined.

% Figure A.6/Q.774 (4 of 4)
process_request_components(Components, State) when 
			is_list(Components), is_record(State, state) ->
	process_request_components(Components, State, [], []).
process_request_components([], _State, AsnComps, ISMs) ->
	{lists:reverse(AsnComps), ISMs};
process_request_components([Head|Tail], State, AsnComps, ISMs) when
				is_record(Head, component),
			        is_record(State, state)	->
	#component{asn_ber = Asn, user_prim = Uprim} = Head,
	#state{usap = Usap, dialogueID = DialogueId} = State,
	case Uprim of
	    #'TC-INVOKE'{class = Class, timeout = Tout,
	    		 invokeID = InvId} ->
		% if INVOKE component
		% start ISM and store ISM
		{ok, ISM} = tcap_invocation_sup:start_ism(Usap, DialogueId,
							  InvId, self(), Class, Tout),
		% signal 'operation-sent' to ISM
		gen_fsm:send_event(ISM, 'operation-sent'),
		NewISMs = [{InvId, ISM}|ISMs];
	    _ ->
		NewISMs = ISMs
	end,
	process_request_components(Tail, State, [Asn|AsnComps], NewISMs).

% discard components of type INVOKE for matching InvokeID
discard_inv_component(Components, InvId) when is_list(Components) ->
	discard_inv_component(Components, InvId, []).
discard_inv_component([], _InvId, CompOut) ->
	lists:reverse(CompOut);
discard_inv_component([Head|Tail], InvId, CompAcc) ->
	#component{user_prim = Uprim} = Head,
	case Uprim of
	    #'TC-INVOKE'{invokeID = InvId} ->
		CompOut = CompAcc;
	    _ ->
		CompOut = [Head|CompAcc]
	end,
	discard_inv_component(Tail, InvId, CompOut).

% iterate over list of ISMs, terminate the one with matching InvId
terminate_active_ISM(ISMs, InvId) ->
	case lists:keyfind(InvId, 1, ISMs) of
	    {InvId, ISM} ->
		gen_fsm:send_event(ISM, terminate),
		lists:keydelete(InvId, 1, ISMs);
	    false ->
		ISMs
	end.

terminate_ISMs([]) ->
	ok;
terminate_ISMs([{_Id, ISM}|Tail]) ->
	gen_fsm:send_event(ISM, terminate),
	terminate_ISMs(Tail).

% Convert from user-visible primitive records to asn1ct-generated record
uprim_to_asn_rec(Uprim) when is_record(Uprim, 'TC-INVOKE') ->
	{invoke, #'Invoke'{invokeId = inv_id_to_asn_rec(Uprim#'TC-INVOKE'.invokeID),
		  linkedId = inv_id_to_asn_rec(Uprim#'TC-INVOKE'.linkedID),
		  opcode = asn_val(Uprim#'TC-INVOKE'.operation),
		  argument = asn_val(Uprim#'TC-INVOKE'.parameters)}};
uprim_to_asn_rec(#'TC-RESULT-NL'{invokeID = InvId, operation = Op,
				 parameters = Params}) ->
	ResRes = #'ReturnResult_result'{opcode = asn_val(Op),
					result = asn_val(Params)},
	{returnResultNotLast, #'ReturnResult'{invokeId = inv_id_to_asn_rec(InvId), result = ResRes}};
uprim_to_asn_rec(#'TC-RESULT-L'{invokeID = InvId, operation = Op,
				parameters = Params}) ->
	ResRes = #'ReturnResult_result'{opcode = asn_val(Op),
					result = asn_val(Params)},
	{returnResult, #'ReturnResult'{invokeId = inv_id_to_asn_rec(InvId), result = ResRes}};
uprim_to_asn_rec(#'TC-U-ERROR'{invokeID = InvId, error = Error,
			       parameters = Params}) ->
	{returnError, #'ReturnError'{invokeId = inv_id_to_asn_rec(InvId),
			errcode = asn_val(Error),
			parameter = asn_val(Params)}};
uprim_to_asn_rec(#'TC-R-REJECT'{invokeID = InvId, problemCode = Pcode}) ->
	{reject, #'Reject'{invokeId = InvId, problem = Pcode}};
uprim_to_asn_rec(#'TC-U-REJECT'{invokeID = InvId, problemCode = Pcode}) ->
	{reject, #'Reject'{invokeId = InvId, problem = Pcode}}.

% Convert from asn1ct-generated record to the primitive records
asn_rec_to_uprim({invoke, AsnRec}, DlgId) when is_record(AsnRec, 'Invoke') ->
	#'TC-INVOKE'{dialogueID = DlgId,
		     invokeID = inv_id_to_uprim(AsnRec#'Invoke'.invokeId),
		     linkedID = inv_id_to_uprim(AsnRec#'Invoke'.linkedId),
		     operation = AsnRec#'Invoke'.opcode,
		     parameters = AsnRec#'Invoke'.argument};
asn_rec_to_uprim({returnResultNotLast, AsnRec}, DlgId) when is_record(AsnRec, 'ReturnResult') ->
	#'ReturnResult_result'{opcode = Op, result = Result} = AsnRec#'ReturnResult'.result,
	#'TC-RESULT-NL'{dialogueID = DlgId,
			invokeID = inv_id_to_uprim(AsnRec#'ReturnResult'.invokeId),
			operation = Op,
			parameters = Result};
asn_rec_to_uprim({returnResult, AsnRec}, DlgId) when is_record(AsnRec, 'ReturnResult') ->
	#'ReturnResult_result'{opcode = Op, result = Result} = AsnRec#'ReturnResult'.result,
	#'TC-RESULT-L'{dialogueID = DlgId,
			invokeID = inv_id_to_uprim(AsnRec#'ReturnResult'.invokeId),
			operation = Op,
			parameters = Result};
asn_rec_to_uprim({returnError, AsnRec}, DlgId) when is_record(AsnRec, 'ReturnError') ->
	#'TC-U-ERROR'{dialogueID = DlgId,
		      invokeID = inv_id_to_uprim(AsnRec#'ReturnError'.invokeId),
		      error = AsnRec#'ReturnError'.errcode,
		      parameters = AsnRec#'ReturnError'.parameter};
asn_rec_to_uprim({reject, AsnRec}, DlgId) when is_record(AsnRec, 'Reject') ->
	#'TC-U-REJECT'{dialogueID = DlgId,
			invokeID = inv_id_to_uprim(AsnRec#'Reject'.invokeId),
			problemCode = AsnRec#'Reject'.problem}.


process_rx_components(_ISMs, _Usap, _DlgId, []) ->
	ok;
process_rx_components(ISMs, Usap, DlgId, [Head|Tail]) ->
	process_rx_component(ISMs, Usap, DlgId, Head),
	process_rx_components(ISMs, Usap, DlgId, Tail).

process_rx_component(ISMs, Usap, DlgId, C={invoke, #'Invoke'{invokeId=InvId}}) ->
	{invoke, I} = C,
	case I#'Invoke'.linkedId of
	    asn1_NOVALUE ->
		ok;
	    Linked ->
		% check if Linked ISM is in operation sent state
		% FIXME
		ok
	end,
	Prim = asn_rec_to_uprim(C, DlgId),
	gen_fsm:send_event(Usap, {'TC','INVOKE',indication,Prim});
process_rx_component(ISMs, _Usap, DlgId, C={reject, #'Reject'{invokeId=InvId,
							problem=Problem}}) ->
	ISM = lists:keyfind(InvId, 1, ISMs),
	case Problem of
	    {invoke, _} ->
		% FIXME: ISM active (No -> Inform TC-User)
		gen_fsm:send_event(ISM, terminate);
	    _ ->
		ok
	end,
	% FIXME: decide on TC-U-REJECT or TC-R-REJECT
	Prim = asn_rec_to_uprim(C, DlgId),
	{InvId, ISM} = lists:keyfind(InvId, 1, ISMs),
	gen_fsm:send_event(ISM, Prim);
process_rx_component(ISMs, _Usap, DlgId, Comp) ->
	% syntax error?
	InvId = get_invoke_id_from_comp(Comp),
	{InvId, ISM} = lists:keyfind(InvId, 1, ISMs),
	% FIXME: ISM active (No -> 6)
	Prim = asn_rec_to_uprim(Comp, DlgId),
	gen_fsm:send_event(ISM, Prim).

add_components_to_state(State = #state{components=CompOld}, CompNew) when is_list(CompNew) ->
	State#state{components = CompOld ++ CompNew};
add_components_to_state(State, CompNew) when is_record(CompNew, component) ->
	add_components_to_state(State, [CompNew]).


% get the invokeId from the given asn-record component tuple.
get_invoke_id_from_comp({invoke,
			 #'Invoke'{invokeId = InvId}}) ->
	InvId;
get_invoke_id_from_comp({returnResult,
			 #'ReturnResult'{invokeId = InvId}}) ->
	InvId;
get_invoke_id_from_comp({returnResultNotLast,
			 #'ReturnResult'{invokeId = InvId}}) ->
	InvId;
get_invoke_id_from_comp({returnError,
			 #'ReturnError'{invokeId = InvId}}) ->
	InvId;
get_invoke_id_from_comp({reject,
			 #'Reject'{invokeId = InvId}}) ->
	InvId.