blob: 7184c6db37f05a7fa8f8ea064210ad43bafd4c45 [file] [log] [blame]
Harald Welte26bdef22012-01-16 22:22:17 +01001% MTP3 Message handling; message distribution (HMDT) according to Q.704
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/>.
Harald Weltef8bf0322012-04-16 13:10:47 +020019%
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.
Harald Welte26bdef22012-01-16 22:22:17 +010033
34-module(mtp3_hmdt).
35-author('Harald Welte <laforge@gnumonks.org>').
36-behaviour(gen_fsm).
37
38-include("mtp3.hrl").
39
40% gen_fsm exports
41-export([init/1, terminate/3, code_change/4, handle_event/3, handle_info/3]).
42
43% individual FSM states
44-export([idle/2, own_sp_restart/2]).
45
46-record(hmdt_state, {
47 sltc_pid
48 }).
49
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51% gen_fsm callbacks
52%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53
54init([Sltc]) when is_pid(Sltc) ->
55 HmdtState = #hmdt_state{sltc_pid = Sltc},
56 {ok, idle, HmdtState}.
57
58terminate(Reason, State, _LoopDat) ->
59 io:format("Terminating ~p in State ~p (Reason: ~p)~n",
60 [?MODULE, State, Reason]),
61 ok.
62
63code_change(_OldVsn, StateName, LoopDat, _Extra) ->
64 {ok, StateName, LoopDat}.
65
66handle_event(Event, State, LoopDat) ->
67 io:format("Unknown Event ~p in state ~p~n", [Event, State]),
68 {next_state, State, LoopDat}.
69
70handle_info(Info, State, LoopDat) ->
71 io:format("Unknown Info ~p in state ~p~n", [Info, State]),
72 {next_state, State, LoopDat}.
73
74% See Figure 2 of Q.707
75
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77% STATE: idle
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79
80idle(M=#mtp3_msg{service_ind=Sio}, LoopDat) ->
81 handle_mtp3(Sio, M, LoopDat),
82 {next_state, idle, LoopDat};
83
84idle(restart_begins, LoopDat) ->
85 {next_state, own_sp_restart, LoopDat}.
86
87%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88% STATE: own_sp_restart
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90
91own_sp_restart(M=#mtp3_msg{service_ind=Sio}, LoopDat) when
92 Sio == ?MTP3_SERV_MGMT; Sio == ?MTP3_SERV_MTN ->
93 handle_mtp3(Sio, M, LoopDat),
94 {next_state, own_sp_restart, LoopDat};
95
96own_sp_restart(restart_ends, LoopDat) ->
97 {next_state, idle, LoopDat}.
98
99
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101% helper functions
102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104handle_mtp3(?MTP3_SERV_MTN, Mtp3, LoopDat) ->
105 io:format("SIO ~p HMDT -> SLTC~n", [?MTP3_SERV_MTN]),
106 gen_fsm:send_event(LoopDat#hmdt_state.sltc_pid, Mtp3);
107handle_mtp3(?MTP3_SERV_MGMT, Mtp3, LoopDat) ->
108 io:format("SIO ~p HMDT -> NULL~n", [?MTP3_SERV_MGMT]),
109 % FIXME: distinguish between SRM, SLM and STM
110 ok;
111handle_mtp3(Sio, Mtp3, LoopDat) ->
112 io:format("SIO ~p HMDT -> ss7_links~n", [Sio]),
113 % deliver to subsystem
114 ss7_links:mtp3_rx(Mtp3),
115 % FIXME: Send UPU! ?
116 ok.