blob: beaac5dc984176111fb38987195de7b7ba150c24 [file] [log] [blame]
Harald Welte50dfc192012-01-17 15:11:37 +01001% Conversion between SUA messages and #sccp_msg{}
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/>.
19
20% FIXME: this currently only supports connection-less SCCP
21
22-module(sua_sccp_conv).
23-author('Harald Welte <laforge@gnumonks.org>').
24
25-include("sua.hrl").
26-include("sccp.hrl").
27
28-export([sua_to_sccp/1, sccp_to_sua/1]).
29
30sua_to_sccp(M=#sua_msg{msg_class = Class, msg_type = Type}) ->
31 sua_to_sccp(Class, Type, M).
32sua_to_sccp(?SUA_MSGC_CL, ?SUA_CL_CLDT, Sua) ->
33 Params = sua_to_sccp_params(Sua),
34 #sccp_msg{msg_type = ?SCCP_MSGT_UDT,
35 parameters = Params};
36sua_to_sccp(?SUA_MSGC_CL, ?SUA_CL_CLDR, Sua) ->
37 Params = sua_to_sccp_params(Sua),
38 #sccp_msg{msg_type = ?SCCP_MSGT_UDTS,
39 parameters = Params}.
40
Harald Welte99efaf92012-01-18 14:11:23 +010041sccp_to_sua(#sccp_msg{msg_type = Type, parameters = Params}) ->
Harald Welte50dfc192012-01-17 15:11:37 +010042 sccp_to_sua(Type, Params).
43sccp_to_sua(Type, Params) when Type == ?SCCP_MSGT_UDT;
44 Type == ?SCCP_MSGT_XUDT;
45 Type == ?SCCP_MSGT_LUDT ->
Harald Welte8e92c9a2012-01-18 00:25:26 +010046 Opts = sccp_to_sua_params(Type, Params),
47 #sua_msg{version = 1, msg_class = ?SUA_MSGC_CL,
48 msg_type = ?SUA_CL_CLDT, payload = Opts};
Harald Welte50dfc192012-01-17 15:11:37 +010049sccp_to_sua(Type, Params) when Type == ?SCCP_MSGT_UDTS;
50 Type == ?SCCP_MSGT_XUDTS;
51 Type == ?SCCP_MSGT_LUDTS ->
52 Opts = sccp_to_sua_params(Params),
Harald Welte8e92c9a2012-01-18 00:25:26 +010053 #sua_msg{version=1, msg_class = ?SUA_MSGC_CL,
54 msg_type = ?SUA_CL_CLDR, payload = Opts}.
Harald Welte50dfc192012-01-17 15:11:37 +010055
56
57% CLDT parameters:
58% ?SUA_IEI_ROUTE_CTX, ?SUA_IEI_PROTO_CLASS, ?SUA_IEI_SRC_ADDR,
59% ?SUA_IEI_DEST_ADDR, ?SUA_IEI_SEQ_CTRL, ?SUA_IEI_S7_HOP_CTR,
60% ?SUA_IEI_IMPORTANCE, ?SUA_IEI_MSG_PRIO, ?SUA_IEI_CORR_ID,
61% ?SUA_IEI_SEGMENTATION, ?SUA_IEI_DATA
62
63sua_to_sccp_params(#sua_msg{msg_class=Class, msg_type=Type, payload=Payload}) ->
64 sua_to_sccp_params(Class, Type, Payload).
65sua_to_sccp_params(Class, Type, Payload) ->
66 sua_to_sccp_params(Class, Type, Payload, []).
Harald Welte99efaf92012-01-18 14:11:23 +010067sua_to_sccp_params(_Class, _Type, [], List) ->
Harald Welte50dfc192012-01-17 15:11:37 +010068 List;
Harald Welte042a5792012-01-18 00:09:51 +010069sua_to_sccp_params(Class, Type, [{ParTag, {_Len, ParVal}}|Remain], List) ->
Harald Welte50dfc192012-01-17 15:11:37 +010070 NewPars = sua_to_sccp_param(Class, Type, ParTag, ParVal),
71 sua_to_sccp_params(Class, Type, Remain, List ++ NewPars).
72
73% convert an individual SUA parameter to a SCCP option
74sua_to_sccp_param(_, _, ?SUA_IEI_PROTO_CLASS, Remain) ->
Harald Welte99efaf92012-01-18 14:11:23 +010075 <<_:24, PCOpt:4, _:2, Class:2>> = Remain,
76 [{protocol_class, {Class, PCOpt}}];
Harald Welte50dfc192012-01-17 15:11:37 +010077sua_to_sccp_param(_, _, ?SUA_IEI_SRC_ADDR, Remain) ->
78 Addr = sua_to_sccp_addr(Remain),
Harald Welte7e1c2612012-01-18 07:44:31 +010079 [{calling_party_addr, Addr}];
Harald Welte50dfc192012-01-17 15:11:37 +010080sua_to_sccp_param(_, _, ?SUA_IEI_DEST_ADDR, Remain) ->
81 Addr = sua_to_sccp_addr(Remain),
Harald Welte7e1c2612012-01-18 07:44:31 +010082 [{called_party_addr, Addr}];
Harald Welte99efaf92012-01-18 14:11:23 +010083sua_to_sccp_param(_, _, ?SUA_IEI_SEQ_CTRL, _Remain) ->
84 % If we were to translate to a N-UNITDATA.req, we could map
85 % this, but there is no mapping to a SCCP message...
86 [];
Harald Welte50dfc192012-01-17 15:11:37 +010087sua_to_sccp_param(_, _, ?SUA_IEI_S7_HOP_CTR, Remain) ->
88 <<_:24, HopCtr:8>> = Remain,
89 [{?SCCP_PNC_HOP_COUNTER, HopCtr}];
90sua_to_sccp_param(_, _, ?SUA_IEI_IMPORTANCE, Remain) ->
91 <<_:24, Imp:8>> = Remain,
92 [{?SCCP_PNC_IMPORTANCE, Imp}];
93sua_to_sccp_param(_, _, ?SUA_IEI_DATA, Remain) ->
Harald Welte7e1c2612012-01-18 07:44:31 +010094 [{user_data, Remain}];
Harald Welte99efaf92012-01-18 14:11:23 +010095sua_to_sccp_param(_, _, ?SUA_IEI_ROUTE_CTX, _Remain) ->
Harald Welte042a5792012-01-18 00:09:51 +010096 %FIXME: what to do with routing context?
97 [].
Harald Welte50dfc192012-01-17 15:11:37 +010098
99sccp_to_sua_params(#sccp_msg{msg_type=Type, parameters=Params}) ->
100 sccp_to_sua_params(Type, Params).
101sccp_to_sua_params(Type, Params) when is_list(Params) ->
102 sccp_to_sua_params(Type, Params, []).
Harald Welte99efaf92012-01-18 14:11:23 +0100103sccp_to_sua_params(_Type, [], List) ->
Harald Welte50dfc192012-01-17 15:11:37 +0100104 List;
105sccp_to_sua_params(Type, [{ParTag, ParVal}|Tail], List) ->
106 NewPars = sccp_to_sua_param(Type, ParTag, ParVal),
107 sccp_to_sua_params(Type, Tail, List ++ NewPars).
108
Harald Welte99efaf92012-01-18 14:11:23 +0100109sccp_to_sua_param(_, protocol_class, {Opt, Class}) ->
110 [{?SUA_IEI_PROTO_CLASS, <<0:24, Opt:4, 0:2, Class:2>>}];
Harald Welte7e1c2612012-01-18 07:44:31 +0100111sccp_to_sua_param(_, calling_party_addr, Addr) ->
Harald Welte50dfc192012-01-17 15:11:37 +0100112 AddrSua = sccp_to_sua_addr(Addr),
113 [{?SUA_IEI_SRC_ADDR, AddrSua}];
Harald Welte7e1c2612012-01-18 07:44:31 +0100114sccp_to_sua_param(_, called_party_addr, Addr) ->
Harald Welte50dfc192012-01-17 15:11:37 +0100115 AddrSua = sccp_to_sua_addr(Addr),
116 [{?SUA_IEI_DEST_ADDR, AddrSua}];
Harald Welte50dfc192012-01-17 15:11:37 +0100117sccp_to_sua_param(_, ?SCCP_PNC_HOP_COUNTER, Hop) ->
118 [{?SUA_IEI_S7_HOP_CTR, <<0:24, Hop:8>>}];
119sccp_to_sua_param(_, ?SCCP_PNC_IMPORTANCE, Imp) ->
120 [{?SUA_IEI_IMPORTANCE, <<0:24, Imp:8>>}];
Harald Welte7e1c2612012-01-18 07:44:31 +0100121sccp_to_sua_param(_, user_data, Data) ->
Harald Welte50dfc192012-01-17 15:11:37 +0100122 [{?SUA_IEI_DATA, Data}].
123
124sua_to_sccp_addr(SuaBin) ->
125 <<RoutInd:16, _:13, GTinc:1, PCinc:1, SSNinc:1, Remain/binary>> = SuaBin,
126 ParList = addr_pars_to_list(Remain),
127 case GTinc of
128 1 ->
Harald Welte042a5792012-01-18 00:09:51 +0100129 {_, GTopt} = proplists:get_value(?SUA_IEI_GT, ParList),
Harald Welte50dfc192012-01-17 15:11:37 +0100130 GT = parse_sua_gt(GTopt);
131 0 ->
132 GT = undefined
133 end,
134 case PCinc of
135 1 ->
Harald Welte042a5792012-01-18 00:09:51 +0100136 {_, PCopt} = proplists:get_value(?SUA_IEI_PC, ParList),
Harald Welte50dfc192012-01-17 15:11:37 +0100137 PC = parse_sua_pc(PCopt);
138 0 ->
139 PC = undefined
140 end,
141 case SSNinc of
142 1 ->
Harald Welte042a5792012-01-18 00:09:51 +0100143 {_, SSNopt} = proplists:get_value(?SUA_IEI_SSN, ParList),
Harald Welte50dfc192012-01-17 15:11:37 +0100144 SSN = parse_sua_ssn(SSNopt);
145 0 ->
146 SSN = undefined
147 end,
148 case RoutInd of
149 ?SUA_RI_GT ->
150 RoutSSN = 0;
151 ?SUA_RI_SSN_PC ->
152 RoutSSN = 1
153 end,
154 #sccp_addr{route_on_ssn = RoutSSN, point_code = PC, ssn = SSN, global_title = GT}.
155
156addr_pars_to_list(Bin) ->
157 sua_codec:parse_xua_opts(Bin).
158
159sccp_to_sua_addr(Addr) when is_record(Addr, sccp_addr) ->
160 #sccp_addr{route_on_ssn = RoutOnSsn, point_code = PC, ssn = SSN,
161 global_title = GT} = Addr,
162 case GT of
163 #global_title{} ->
Harald Welte8e92c9a2012-01-18 00:25:26 +0100164 GTopt = [{?SUA_IEI_GT, encode_sua_gt(GT)}],
Harald Welte50dfc192012-01-17 15:11:37 +0100165 GTinc = 1;
166 _ ->
167 GTopt = [],
168 GTinc = 0
169 end,
170 case PC of
171 Int when is_integer(Int) ->
Harald Welte8e92c9a2012-01-18 00:25:26 +0100172 PCopt = [{?SUA_IEI_PC, encode_sua_pc(PC)}],
Harald Welte50dfc192012-01-17 15:11:37 +0100173 PCinc = 1;
174 _ ->
175 PCopt = [],
176 PCinc = 0
177 end,
178 case SSN of
179 Int2 when is_integer(Int2) ->
Harald Welte8e92c9a2012-01-18 00:25:26 +0100180 SSNopt = [{?SUA_IEI_SSN, encode_sua_ssn(SSN)}],
Harald Welte50dfc192012-01-17 15:11:37 +0100181 SSNinc = 1;
182 _ ->
183 SSNopt = [],
184 SSNinc = 0
185 end,
186 case RoutOnSsn of
187 0 ->
188 RoutInd = ?SUA_RI_GT;
189 1 ->
190 RoutInd = ?SUA_RI_SSN_PC
191 end,
192 Tail = sua_codec:encode_xua_opts(GTopt ++ PCopt ++ SSNopt),
193 <<RoutInd:16, 0:13, GTinc:1, PCinc:1, SSNinc:1, Tail/binary>>.
194
195parse_sua_gt(Bin) ->
196 <<_:24, GTI:8, NoDigits:8, TransType:8, NumPlan:8, NAI:8, Remain/binary>> = Bin,
197 Number = parse_sua_gt_digits(NoDigits, Remain),
198 #global_title{gti = GTI, nature_of_addr_ind = NAI,
Harald Welte7e1c2612012-01-18 07:44:31 +0100199 trans_type = TransType,
Harald Welte50dfc192012-01-17 15:11:37 +0100200 numbering_plan = NumPlan,
201 phone_number = Number}.
202encode_sua_gt(Gt) when is_record(Gt, global_title) ->
203 #global_title{gti = GTI, nature_of_addr_ind = NAI,
Harald Welte7e1c2612012-01-18 07:44:31 +0100204 trans_type = TransType,
Harald Welte50dfc192012-01-17 15:11:37 +0100205 numbering_plan = NumPlan,
206 phone_number = Number} = Gt,
207 NoDigits = count_digits(Number),
208 DigitBin = encode_sua_gt_digits(Number),
209 <<0:24, GTI:8, NoDigits:8, TransType:8, NumPlan:8, NAI:8, DigitBin/binary>>.
210
211count_digits(Number) when is_integer(Number) ->
212 BcdList = osmo_util:int2digit_list(Number),
213 count_digits(BcdList);
214count_digits(Number) when is_list(Number) ->
215 length(Number).
216
217
218parse_sua_gt_digits(NoDigits, Remain) ->
219 % as opposed to ISUP/SCCP, we can have more than one nibble padding,
220 OddEven = NoDigits rem 1,
221 case OddEven of
222 0 ->
Harald Welte042a5792012-01-18 00:09:51 +0100223 ByteLen = NoDigits div 2;
Harald Welte50dfc192012-01-17 15:11:37 +0100224 1 ->
Harald Welte042a5792012-01-18 00:09:51 +0100225 ByteLen = NoDigits div 2 + 1
Harald Welte50dfc192012-01-17 15:11:37 +0100226 end,
227 <<Bin:ByteLen/binary, _/binary>> = Remain,
228 isup_codec:parse_isup_party(Bin, OddEven).
229encode_sua_gt_digits(Digits) when is_list(Digits); is_integer(Digits) ->
230 % Assume that overall option encoder will do the padding...
Harald Welte8e92c9a2012-01-18 00:25:26 +0100231 {Enc, _OddEven} = isup_codec:encode_isup_party(Digits),
232 Enc.
Harald Welte50dfc192012-01-17 15:11:37 +0100233
234parse_sua_pc(<<PC:32/big>>) ->
235 PC.
236encode_sua_pc(Pc) when is_integer(Pc) ->
237 <<Pc:32/big>>.
238
239parse_sua_ssn(<<_:24, SSN:8>>) ->
240 SSN.
241encode_sua_ssn(Ssn) when is_integer(Ssn) ->
242 <<0:24, Ssn:8>>.