blob: 18415172c675323a40a0b655523e57aaba18242c [file] [log] [blame]
Harald Welte3bf7cb62011-04-03 00:25:34 +02001% M3UA in accordance with RFC4666 (http://tools.ietf.org/html/rfc4666)
2
3% (C) 2011 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 Welte3bf7cb62011-04-03 00:25:34 +020033
34-module(m3ua_core).
35-author('Harald Welte <laforge@gnumonks.org>').
Harald Welteead1ba12011-04-15 10:20:04 +020036-behaviour(gen_fsm).
Harald Welte3bf7cb62011-04-03 00:25:34 +020037
38-include_lib("kernel/include/inet_sctp.hrl").
Harald Weltee393ea82011-04-04 16:00:06 +020039-include("osmo_util.hrl").
Harald Welte3bf7cb62011-04-03 00:25:34 +020040-include("sccp.hrl").
41-include("m3ua.hrl").
42
43-export([start_link/1]).
44
Harald Welteb2d3abf2011-04-04 11:26:11 +020045-export([init/1, terminate/3, code_change/4, handle_event/3, handle_info/3]).
Harald Welte3bf7cb62011-04-03 00:25:34 +020046
47% FSM states:
48-export([asp_down/2, asp_inactive/2, asp_active/2]).
49
50-define(T_ACK_TIMEOUT, 2*60*100).
51
52% Loop Data
53-record(m3ua_state, {
54 role, % asp | sgp
55 asp_state, % down, inactive, active
56 t_ack,
Harald Weltecb1c0682011-04-14 21:56:26 +020057 user_fun,
58 user_args,
Harald Welte3bf7cb62011-04-03 00:25:34 +020059 sctp_remote_ip,
60 sctp_remote_port,
Harald Welte8a0ab002011-04-03 22:16:12 +020061 sctp_local_port,
Harald Welte3bf7cb62011-04-03 00:25:34 +020062 sctp_sock,
63 sctp_assoc_id
64 }).
65
66start_link(InitOpts) ->
67 gen_fsm:start_link(?MODULE, InitOpts, [{debug, [trace]}]).
68
69reconnect_sctp(L = #m3ua_state{sctp_remote_ip = Ip, sctp_remote_port = Port, sctp_sock = Sock}) ->
Harald Welte231ae0b2012-04-01 20:13:23 +020070 timer:sleep(1*1000),
Harald Welte3bf7cb62011-04-03 00:25:34 +020071 io:format("SCTP Reconnect ~p:~p~n", [Ip, Port]),
Harald Welte8a0ab002011-04-03 22:16:12 +020072 InitMsg = #sctp_initmsg{num_ostreams = 2, max_instreams = 2},
Harald Welte3bf7cb62011-04-03 00:25:34 +020073 case gen_sctp:connect(Sock, Ip, Port, [{active, once}, {reuseaddr, true},
74 {sctp_initmsg, InitMsg}]) of
75 {ok, Assoc} ->
Harald Welte9544cab2011-04-04 17:03:23 +020076 send_prim_to_user(L, osmo_util:make_prim('M','SCTP_ESTABLISH',confirm)),
Harald Welte3bf7cb62011-04-03 00:25:34 +020077 L#m3ua_state{sctp_assoc_id = Assoc#sctp_assoc_change.assoc_id};
78 {error, Error } ->
Harald Welte6fc5b292011-04-04 10:38:30 +020079 io:format("SCTP Error ~p, reconnecting~n", [Error]),
Harald Welte3bf7cb62011-04-03 00:25:34 +020080 reconnect_sctp(L)
81 end.
82
83init(InitOpts) ->
Harald Welte8a0ab002011-04-03 22:16:12 +020084 OpenOptsBase = [{active, once}, {reuseaddr, true}],
85 LocalPort = proplists:get_value(sctp_local_port, InitOpts),
86 case LocalPort of
87 undefined ->
88 OpenOpts = OpenOptsBase;
89 _ ->
90 OpenOpts = OpenOptsBase ++ [{port, LocalPort}]
91 end,
92 {ok, SctpSock} = gen_sctp:open(OpenOpts),
Harald Welte3bf7cb62011-04-03 00:25:34 +020093 LoopDat = #m3ua_state{role = asp, sctp_sock = SctpSock,
Harald Weltecb1c0682011-04-14 21:56:26 +020094 user_fun = proplists:get_value(user_fun, InitOpts),
95 user_args = proplists:get_value(user_args, InitOpts),
Harald Welte3bf7cb62011-04-03 00:25:34 +020096 sctp_remote_ip = proplists:get_value(sctp_remote_ip, InitOpts),
Harald Welte8a0ab002011-04-03 22:16:12 +020097 sctp_remote_port = proplists:get_value(sctp_remote_port, InitOpts),
98 sctp_local_port = LocalPort},
Harald Welte3bf7cb62011-04-03 00:25:34 +020099 LoopDat2 = reconnect_sctp(LoopDat),
100 {ok, asp_down, LoopDat2}.
101
Harald Welteb2d3abf2011-04-04 11:26:11 +0200102terminate(Reason, _State, LoopDat) ->
103 io:format("Terminating ~p (Reason: ~p)~n", [?MODULE, Reason]),
104 gen_sctp:close(LoopDat#m3ua_state.sctp_sock).
105
106code_change(_OldVsn, StateName, StateData, _Extra) ->
107 {ok, StateName, StateData}.
108
Harald Welte3bf7cb62011-04-03 00:25:34 +0200109% Helper function to send data to the SCTP peer
Harald Weltecb1c0682011-04-14 21:56:26 +0200110send_sctp_to_peer(LoopDat, PktData, StreamId) when is_binary(PktData) ->
Harald Welte3bf7cb62011-04-03 00:25:34 +0200111 #m3ua_state{sctp_sock = Sock, sctp_assoc_id = Assoc} = LoopDat,
Harald Weltecb1c0682011-04-14 21:56:26 +0200112 SndRcvInfo = #sctp_sndrcvinfo{assoc_id = Assoc, ppid = 3, stream = StreamId},
113 gen_sctp:send(Sock, SndRcvInfo, PktData).
Harald Welte3bf7cb62011-04-03 00:25:34 +0200114
115% same as above, but for un-encoded #m3ua_msg{}
116send_sctp_to_peer(LoopDat, M3uaMsg) when is_record(M3uaMsg, m3ua_msg) ->
117 MsgBin = m3ua_codec:encode_m3ua_msg(M3uaMsg),
Harald Weltecb1c0682011-04-14 21:56:26 +0200118 StreamId = sctp_stream_for_m3ua(M3uaMsg),
119 send_sctp_to_peer(LoopDat, MsgBin, StreamId).
120
121% resolve the Stream ID depending on the m3ua_msg: 0 == management, 1 == trafic
122sctp_stream_for_m3ua(#m3ua_msg{msg_class = Class}) when
123 Class == ?M3UA_MSGC_TRANSFER ->
124 1;
125sctp_stream_for_m3ua(#m3ua_msg{}) ->
126 0.
Harald Welte3bf7cb62011-04-03 00:25:34 +0200127
Harald Weltee393ea82011-04-04 16:00:06 +0200128
129send_prim_to_user(LoopDat, Prim) when is_record(LoopDat, m3ua_state), is_record(Prim, primitive) ->
Harald Weltecb1c0682011-04-14 21:56:26 +0200130 #m3ua_state{user_fun = Fun, user_args = Args} = LoopDat,
131 Fun(Prim, Args).
Harald Weltee393ea82011-04-04 16:00:06 +0200132
Harald Welte3bf7cb62011-04-03 00:25:34 +0200133% helper to send one of the up/down/act/inact management messages + start timer
134send_msg_start_tack(LoopDat, State, MsgClass, MsgType, Params) ->
135 % generate and send the respective message
136 Msg = #m3ua_msg{version = 1, msg_class = MsgClass, msg_type = MsgType, payload = Params},
137 send_sctp_to_peer(LoopDat, Msg),
138 % start T(ack) timer and wait for ASP_UP_ACK
Harald Weltee393ea82011-04-04 16:00:06 +0200139 timer:cancel(LoopDat#m3ua_state.t_ack),
Harald Welte6fc5b292011-04-04 10:38:30 +0200140 {ok, Tack} = timer:apply_after(?T_ACK_TIMEOUT, gen_fsm, send_event,
Harald Welte3bf7cb62011-04-03 00:25:34 +0200141 [self(), {timer_expired, t_ack, {MsgClass, MsgType, Params}}]),
142 {next_state, State, LoopDat#m3ua_state{t_ack = Tack}}.
143
144
Harald Welte3bf7cb62011-04-03 00:25:34 +0200145
Harald Welte6fc5b292011-04-04 10:38:30 +0200146handle_event(Event, State, LoopDat) ->
147 io:format("Unknown Event ~p in state ~p~n", [Event, State]),
148 {next_state, State, LoopDat}.
149
150
151
152handle_info({sctp, Socket, _RemoteIp, _RemotePort, {ANC, SAC}},
Harald Welte3bf7cb62011-04-03 00:25:34 +0200153 _State, LoopDat) when is_record(SAC, sctp_assoc_change) ->
154 io:format("SCTP Assoc Change ~p ~p~n", [ANC, SAC]),
Harald Welte6fc5b292011-04-04 10:38:30 +0200155 #sctp_assoc_change{state = SacState, outbound_streams = _OutStreams,
Harald Welteb2d3abf2011-04-04 11:26:11 +0200156 inbound_streams = _InStreams, assoc_id = _AssocId} = SAC,
Harald Welte3bf7cb62011-04-03 00:25:34 +0200157 case SacState of
158 comm_up ->
Harald Weltee393ea82011-04-04 16:00:06 +0200159 % primmitive to the user
160 send_prim_to_user(LoopDat, osmo_util:make_prim('M','SCTP_ESTABLISH',confirm)),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200161 LoopDat2 = LoopDat;
162 comm_lost ->
Harald Weltee393ea82011-04-04 16:00:06 +0200163 send_prim_to_user(LoopDat, osmo_util:make_prim('M','SCTP_RELEASE',indication)),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200164 LoopDat2 = reconnect_sctp(LoopDat);
Harald Weltea0d2d962013-06-18 18:04:18 +0200165 shutdown_comp ->
166 send_prim_to_user(LoopDat, osmo_util:make_prim('M','SCTP_RELEASE',indication)),
167 LoopDat2 = reconnect_sctp(LoopDat);
Harald Welte3bf7cb62011-04-03 00:25:34 +0200168 addr_unreachable ->
169 LoopDat2 = reconnect_sctp(LoopDat)
170 end,
171 inet:setopts(Socket, [{active, once}]),
172 {next_state, asp_down, LoopDat2};
173
Harald Weltea5dfd5b2013-07-15 11:27:10 +0200174handle_info({sctp, Socket, _RemoteIp, _RemotePort, {ANC, SPC}},
175 State, LoopDat) when is_record(SPC, sctp_paddr_change) ->
176 io:format("SCTP Peer address change ~p ~p~n", [ANC, SPC]),
177 {NewState, LoopDat2} = case SPC#sctp_paddr_change.state of
178 addr_available ->
179 % we don't care
180 {State, LoopDat};
181 addr_unreachable ->
182 send_prim_to_user(LoopDat, osmo_util:make_prim('M','SCTP_RELEASE',indication)),
183 {asp_down, reconnect_sctp(LoopDat)};
184 addr_removed ->
185 % FIXME: what if the last one is removed
186 {State, LoopDat};
187 addr_added ->
188 % we don't care
189 {State, LoopDat};
190 addr_made_prim ->
191 % FIXME: do we need to change remote_ip in our LoopDat?
192 {State, LoopDat}
193 end,
194 inet:setopts(Socket, [{active, once}]),
195 {next_state, NewState, LoopDat2};
196
Harald Welte6fc5b292011-04-04 10:38:30 +0200197handle_info({sctp, Socket, RemoteIp, RemotePort, {[Anc], Data}}, State, LoopDat) ->
Harald Welte3bf7cb62011-04-03 00:25:34 +0200198 io:format("SCTP rx data: ~p ~p~n", [Anc, Data]),
Harald Weltee393ea82011-04-04 16:00:06 +0200199 % process incoming SCTP data
Harald Welte6fc5b292011-04-04 10:38:30 +0200200 if Socket == LoopDat#m3ua_state.sctp_sock,
201 RemoteIp == LoopDat#m3ua_state.sctp_remote_ip,
202 RemotePort == LoopDat#m3ua_state.sctp_remote_port,
203 3 == Anc#sctp_sndrcvinfo.ppid ->
204 Ret = rx_sctp(Anc, Data, State, LoopDat);
205 true ->
206 io:format("unknown SCTP: ~p ~p~n", [Anc, Data]),
207 Ret = {next_state, State, LoopDat}
208 end,
Harald Welte3bf7cb62011-04-03 00:25:34 +0200209 inet:setopts(Socket, [{active, once}]),
Harald Welte6fc5b292011-04-04 10:38:30 +0200210 Ret;
Harald Welte3bf7cb62011-04-03 00:25:34 +0200211
Harald Welte6fc5b292011-04-04 10:38:30 +0200212handle_info({sctp, Socket, RemoteIp, RemotePort, {_Anc, Data}}, _State, LoopDat)
Harald Welte3bf7cb62011-04-03 00:25:34 +0200213 when is_record(Data, sctp_shutdown_event) ->
214 io:format("SCTP remote ~p:~p shutdown~n", [RemoteIp, RemotePort]),
215 inet:setopts(Socket, [{active, once}]),
216 {next_state, asp_down, LoopDat}.
217
218
219
220asp_down(#primitive{subsystem = 'M', gen_name = 'ASP_UP',
Harald Welte6fc5b292011-04-04 10:38:30 +0200221 spec_name = request, parameters = _Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200222 % M-ASP_UP.req from user, generate message and send to remote peer
Harald Welte6fc5b292011-04-04 10:38:30 +0200223 send_msg_start_tack(LoopDat, asp_down, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPUP, []);
Harald Welte3bf7cb62011-04-03 00:25:34 +0200224asp_down({timer_expired, t_ack, {?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPUP, Params}}, LoopDat) ->
225 send_msg_start_tack(LoopDat, asp_down, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPUP, Params);
226
227asp_down(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM,
228 msg_type = ?M3UA_MSGT_ASPSM_ASPUP_ACK}, LoopDat) ->
Harald Welte6fc5b292011-04-04 10:38:30 +0200229 timer:cancel(LoopDat#m3ua_state.t_ack),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200230 % transition into ASP_INACTIVE
Harald Weltee393ea82011-04-04 16:00:06 +0200231 send_prim_to_user(LoopDat, osmo_util:make_prim('M','ASP_UP',confirm)),
Harald Welte6fc5b292011-04-04 10:38:30 +0200232 {next_state, asp_inactive, LoopDat};
233
234asp_down(M3uaMsg, LoopDat) when is_record(M3uaMsg, m3ua_msg) ->
235 rx_m3ua(M3uaMsg, asp_down, LoopDat).
Harald Welte3bf7cb62011-04-03 00:25:34 +0200236
237
Harald Welte6fc5b292011-04-04 10:38:30 +0200238asp_inactive(#primitive{subsystem = 'M', gen_name = 'ASP_ACTIVE',
239 spec_name = request, parameters = _Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200240 % M-ASP_ACTIVE.req from user, generate message and send to remote peer
Harald Welte6fc5b292011-04-04 10:38:30 +0200241 send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPAC,
242 [{?M3UA_IEI_TRAF_MODE_TYPE, <<0,0,0,1>>}]);
Harald Welte3bf7cb62011-04-03 00:25:34 +0200243
244asp_inactive({timer_expired, t_ack, {?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPAC, Params}}, LoopDat) ->
245 send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPAC, Params);
246
247asp_inactive(#primitive{subsystem = 'M', gen_name = 'ASP_DOWN',
Harald Welte6fc5b292011-04-04 10:38:30 +0200248 spec_name = request, parameters = _Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200249 % M-ASP_DOWN.req from user, generate message and send to remote peer
Harald Welte6fc5b292011-04-04 10:38:30 +0200250 send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, []);
Harald Welte3bf7cb62011-04-03 00:25:34 +0200251
252asp_inactive({timer_expired, t_ack, {?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params}}, LoopDat) ->
253 send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params);
254
255asp_inactive(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPTM,
256 msg_type = ?M3UA_MSGT_ASPTM_ASPAC_ACK}, LoopDat) ->
Harald Welte6fc5b292011-04-04 10:38:30 +0200257 timer:cancel(LoopDat#m3ua_state.t_ack),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200258 % transition into ASP_ACTIVE
Harald Weltee393ea82011-04-04 16:00:06 +0200259 % signal this to the user
260 send_prim_to_user(LoopDat, osmo_util:make_prim('M','ASP_ACTIVE',confirm)),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200261 {next_state, asp_active, LoopDat};
262
263asp_inactive(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM,
264 msg_type = ?M3UA_MSGT_ASPSM_ASPDN_ACK}, LoopDat) ->
Harald Welte6fc5b292011-04-04 10:38:30 +0200265 timer:cancel(LoopDat#m3ua_state.t_ack),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200266 % transition into ASP_DOWN
Harald Weltee393ea82011-04-04 16:00:06 +0200267 % signal this to the user
268 send_prim_to_user(LoopDat, osmo_util:make_prim('M','ASP_DOWN',confirm)),
Harald Welte6fc5b292011-04-04 10:38:30 +0200269 {next_state, asp_down, LoopDat};
270
271asp_inactive(M3uaMsg, LoopDat) when is_record(M3uaMsg, m3ua_msg) ->
272 rx_m3ua(M3uaMsg, asp_inactive, LoopDat).
Harald Welte3bf7cb62011-04-03 00:25:34 +0200273
274
275
276asp_active(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM,
277 msg_type = ?M3UA_MSGT_ASPSM_ASPDN_ACK}, LoopDat) ->
Harald Welte6fc5b292011-04-04 10:38:30 +0200278 timer:cancel(LoopDat#m3ua_state.t_ack),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200279 % transition into ASP_DOWN
Harald Weltee393ea82011-04-04 16:00:06 +0200280 % signal this to the user
281 send_prim_to_user(LoopDat, osmo_util:make_prim('M','ASP_DOWN',confirm)),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200282 {next_state, asp_down, LoopDat};
283
284asp_active(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPTM,
285 msg_type = ?M3UA_MSGT_ASPTM_ASPIA_ACK}, LoopDat) ->
Harald Welte6fc5b292011-04-04 10:38:30 +0200286 timer:cancel(LoopDat#m3ua_state.t_ack),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200287 % transition into ASP_INACTIVE
Harald Weltee393ea82011-04-04 16:00:06 +0200288 % signal this to the user
289 send_prim_to_user(LoopDat, osmo_util:make_prim('M','ASP_INACTIVE',confirm)),
Harald Welte3bf7cb62011-04-03 00:25:34 +0200290 {next_state, asp_inactive, LoopDat};
291
292asp_active(#primitive{subsystem = 'M', gen_name = 'ASP_DOWN',
Harald Welte6fc5b292011-04-04 10:38:30 +0200293 spec_name = request, parameters = _Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200294 % M-ASP_DOWN.req from user, generate message and send to remote peer
Harald Welte6fc5b292011-04-04 10:38:30 +0200295 send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, []);
Harald Welte3bf7cb62011-04-03 00:25:34 +0200296
297asp_active({timer_expired, t_ack, {?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params}}, LoopDat) ->
298 send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params);
299
300asp_active(#primitive{subsystem = 'M', gen_name = 'ASP_INACTIVE',
Harald Welte6fc5b292011-04-04 10:38:30 +0200301 spec_name = request, parameters = _Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200302 % M-ASP_INACTIVE.req from user, generate message and send to remote peer
Harald Welte6fc5b292011-04-04 10:38:30 +0200303 send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPIA, []);
Harald Welte3bf7cb62011-04-03 00:25:34 +0200304
305asp_active({timer_expired, t_ack, {?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPIA, Params}}, LoopDat) ->
306 send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPIA, Params);
307
308asp_active(#primitive{subsystem = 'MTP', gen_name = 'TRANSFER',
309 spec_name = request, parameters = Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200310 % MTP-TRANSFER.req from user app: Send message to remote peer
Harald Welte3bf7cb62011-04-03 00:25:34 +0200311 OptList = [{?M3UA_IEI_PROTOCOL_DATA, Params}],
312 Msg = #m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_TRANSFER,
313 msg_type = ?M3UA_MSGT_XFR_DATA,
314 payload = OptList},
315 send_sctp_to_peer(LoopDat, Msg),
316 {next_state, asp_active, LoopDat};
317asp_active(#m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_TRANSFER,
318 msg_type = ?M3UA_MSGT_XFR_DATA, payload = Params}, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200319 % Data transfer from remote entity: Send MTP-TRANSFER.ind primitive to the user
Harald Weltee393ea82011-04-04 16:00:06 +0200320 Mtp3 = proplists:get_value(?M3UA_IEI_PROTOCOL_DATA, Params),
Harald Welte7dadde82011-10-19 13:40:39 +0200321 send_prim_to_user(LoopDat, osmo_util:make_prim('MTP','TRANSFER',indication,Mtp3)),
Harald Welte6fc5b292011-04-04 10:38:30 +0200322 {next_state, asp_active, LoopDat};
323asp_active(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPTM,
324 msg_type = ?M3UA_MSGT_ASPTM_ASPIA_ACK}, LoopDat) ->
325 timer:cancel(LoopDat#m3ua_state.t_ack),
326 % transition to ASP_INACTIVE
327 {next_state, asp_inactive, LoopDat};
328
Harald Welte6fc5b292011-04-04 10:38:30 +0200329asp_active(M3uaMsg, LoopDat) when is_record(M3uaMsg, m3ua_msg) ->
330 rx_m3ua(M3uaMsg, asp_active, LoopDat).
331
332
Harald Weltee393ea82011-04-04 16:00:06 +0200333
Harald Welteb2d3abf2011-04-04 11:26:11 +0200334rx_sctp(_Anc, Data, State, LoopDat) ->
Harald Welte6fc5b292011-04-04 10:38:30 +0200335 M3uaMsg = m3ua_codec:parse_m3ua_msg(Data),
336 gen_fsm:send_event(self(), M3uaMsg),
337 {next_state, State, LoopDat}.
338
339
Harald Weltee393ea82011-04-04 16:00:06 +0200340
Harald Welte6fc5b292011-04-04 10:38:30 +0200341rx_m3ua(Msg = #m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_MGMT,
342 msg_type = ?M3UA_MSGT_MGMT_NTFY}, State, LoopDat) ->
Harald Weltee393ea82011-04-04 16:00:06 +0200343 send_prim_to_user(LoopDat, osmo_util:make_prim('M','NOTIFY',indication,[Msg])),
Harald Welte6fc5b292011-04-04 10:38:30 +0200344 {next_state, State, LoopDat};
345
346rx_m3ua(Msg = #m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_ASPSM,
347 msg_type = ?M3UA_MSGT_ASPSM_BEAT}, State, LoopDat) ->
348 % Send BEAT_ACK using the same payload as the BEAT msg
Harald Welte6fc5b292011-04-04 10:38:30 +0200349 send_sctp_to_peer(LoopDat, Msg#m3ua_msg{msg_type = ?M3UA_MSGT_ASPSM_BEAT_ACK}),
350 {next_state, State, LoopDat};
351
Harald Weltee393ea82011-04-04 16:00:06 +0200352rx_m3ua(Msg = #m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_MGMT,
353 msg_type = ?M3UA_MSGT_MGMT_ERR}, State, LoopDat) ->
354 send_prim_to_user(LoopDat, osmo_util:make_prim('M','ERROR',indication,[Msg])),
355 {next_state, State, LoopDat};
356
357rx_m3ua(Msg = #m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_SSNM,
358 msg_type = MsgType, payload = Params}, State, LoopDat) ->
Harald Welteb6473702011-04-14 22:06:42 +0200359 % transform to classic MTP primitive and send up to the user
Harald Weltee393ea82011-04-04 16:00:06 +0200360 Mtp = map_ssnm_to_mtp_prim(MsgType),
361 send_prim_to_user(LoopDat, Mtp),
362 {next_state, State, LoopDat};
363
Harald Welte6fc5b292011-04-04 10:38:30 +0200364rx_m3ua(Msg = #m3ua_msg{}, State, LoopDat) ->
365 io:format("M3UA Unknown messge ~p in state ~p~n", [Msg, State]),
366 {next_state, State, LoopDat}.
Harald Weltee393ea82011-04-04 16:00:06 +0200367
Harald Welteb6473702011-04-14 22:06:42 +0200368% Transform the M3UA SSNM messages into classic MTP primitives
Harald Weltee393ea82011-04-04 16:00:06 +0200369map_ssnm_to_mtp_prim(MsgType) ->
Harald Welte10d77cd2011-11-04 22:28:21 +0100370 Mtp = #primitive{subsystem = 'MTP', spec_name = indication},
Harald Weltee393ea82011-04-04 16:00:06 +0200371 case MsgType of
372 ?M3UA_MSGT_SSNM_DUNA -> Mtp#primitive{gen_name = 'PAUSE'};
373 ?M3UA_MSGT_SSNM_DAVA -> Mtp#primitive{gen_name = 'RESUME'};
374 ?M3UA_MSGT_SSNM_SCON -> Mtp#primitive{gen_name = 'STATUS'};
375 ?M3UA_MSGT_SSNM_DUPU -> Mtp#primitive{gen_name = 'STATUS'}
376 end.