blob: 0871b986b5668a048c1ddfc4c2ceaf40030b1b15 [file] [log] [blame]
Harald Welte033cef02010-12-19 22:47:14 +01001
2% (C) 2010 by Harald Welte <laforge@gnumonks.org>
3%
4% All Rights Reserved
5%
6% This program is free software; you can redistribute it and/or modify
7% it under the terms of the GNU Affero General Public License as
8% published by the Free Software Foundation; either version 3 of the
9% License, or (at your option) any later version.
10%
11% This program is distributed in the hope that it will be useful,
12% but WITHOUT ANY WARRANTY; without even the implied warranty of
13% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14% GNU General Public License for more details.
15%
16% You should have received a copy of the GNU Affero General Public License
17% along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19-module(sccp_user).
20-author('Harald Welte <laforge@gnumonks.org>').
21-export([init/3]).
22
23-include("sccp.hrl").
24
25-define(IPA_STREAM_ID_SCCP, 253).
26
27-record(loop_data, {
28 ipa_stream_id
29 }).
30
31init(TcpServerPort, IpaStreamId, Opts) ->
32 ipa_proto:init(),
33 % Create listening IPA socket
34 ipa_proto:start_listen(TcpServerPort, 1, Opts),
35 loop(#loop_data{ipa_stream_id = IpaStreamId}).
36
37% callback function to be called by IPA socket handler if it receives some data
38sccp_ipa_adapter_cb(S, IpaStreamID, DataBin, [ScrcPid]) ->
39 io:format("sccp_ipa_adapter_cb (Socket ~p, Stream ~p), passing data to SCRP~n", [S, IpaStreamID]),
40 % hand any incoming IPA message off into the SCCP stacks SCRC
41 gen_fsm:send_event(ScrcPid, sccp_scoc:make_prim('MTP', 'TRANSFER', indication, DataBin)).
42
43% callback function to be called by SCCP if it wants to transmit some data
44sccp_to_ipa_cb(#primitive{subsystem = 'MTP', gen_name = 'TRANSFER',
45 spec_name = request, parameters = DataBin}, [IpaPid, S, IpaStreamID]) ->
46 %ipa_proto:send(S, IpaStreamID, DataBin).
47 io:format("sccp_to_ipa_cb: Sending to ~p ~p/~p: ~p~n", [IpaPid, S,IpaStreamID, DataBin]),
48 IpaPid ! {ipa_send, S, IpaStreamID, DataBin}.
49
50loop(LoopData) ->
51 receive
52 {ipa_tcp_accept, S} ->
53 io:format("sccp_ipa_adapter: ipa_tcp_accept from ~p~n", [inet:peername(S)]),
Harald Welte13688ae2010-12-20 13:27:00 +010054 IpaStreamId = LoopData#loop_data.ipa_stream_id,
Harald Welte033cef02010-12-19 22:47:14 +010055 % hand over the socket into the IPA stack
56 {ok, IpaPid} = ipa_proto:register_socket(S),
57 % Start the SCRC FSM for this virtual MTP link
Harald Welte13688ae2010-12-20 13:27:00 +010058 ScrcMtpCb = {callback_fn, fun sccp_to_ipa_cb/2, [IpaPid, S, IpaStreamId]},
Harald Welte033cef02010-12-19 22:47:14 +010059 {ok, ScrcPid} = sccp_scrc:start_link([{mtp_tx_action, ScrcMtpCb}]),
60 % Register an IPA stream for SCCP
Harald Welte13688ae2010-12-20 13:27:00 +010061 ipa_proto:register_stream(S, IpaStreamId,
Harald Welte033cef02010-12-19 22:47:14 +010062 {callback_fn, fun sccp_ipa_adapter_cb/4, [ScrcPid]}),
63 ipa_proto:unblock(S),
64 loop(LoopData);
65 % this code should later be moved into the actual MSC
66 {sccp, Prim} ->
67 io:format("sccp_user has received primitive ~p~n", [Prim]),
68 handle_sccp_prim(Prim),
69 loop(LoopData)
70 end.
71
72
73handle_sccp_prim(#primitive{subsystem = 'N', gen_name = 'CONNECT',
74 spec_name = indication, parameters = Params}) ->
75 %RespPrim = Prim#primitive{spec_name = response},
76 RespPrim = sccp_scoc:make_prim('N', 'CONNECT', response, []),
77 ScocPid = proplists:get_value(scoc_pid, Params),
78 gen_fsm:send_event(ScocPid, RespPrim);
79handle_sccp_prim(#primitive{}) ->
80 ok.