blob: aee8f1f0a26f73009c7045acb62ab80b02502aa4 [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
41sccp_to_sua(M=#sccp_msg{msg_type = Type, parameters = Params}) ->
42 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, []).
67sua_to_sccp_params(Class, Type, [], List) ->
68 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) ->
75 <<_:24, RetErr:1, _:5, Class:2>> = Remain,
76 [{?SCCP_PNC_PROTOCOL_CLASS, Class}];
77sua_to_sccp_param(_, _, ?SUA_IEI_SRC_ADDR, Remain) ->
78 Addr = sua_to_sccp_addr(Remain),
79 [{?SCCP_PNC_CALLING_PARTY_ADDRESS, Addr}];
80sua_to_sccp_param(_, _, ?SUA_IEI_DEST_ADDR, Remain) ->
81 Addr = sua_to_sccp_addr(Remain),
82 [{?SCCP_PNC_CALLED_PARTY_ADDRESS, Addr}];
83sua_to_sccp_param(_, _, ?SUA_IEI_SEQ_CTRL, Remain) ->
84 [{?SCCP_PNC_SEQUENCING, Remain}];
85sua_to_sccp_param(_, _, ?SUA_IEI_S7_HOP_CTR, Remain) ->
86 <<_:24, HopCtr:8>> = Remain,
87 [{?SCCP_PNC_HOP_COUNTER, HopCtr}];
88sua_to_sccp_param(_, _, ?SUA_IEI_IMPORTANCE, Remain) ->
89 <<_:24, Imp:8>> = Remain,
90 [{?SCCP_PNC_IMPORTANCE, Imp}];
91sua_to_sccp_param(_, _, ?SUA_IEI_DATA, Remain) ->
Harald Welte042a5792012-01-18 00:09:51 +010092 [{?SCCP_PNC_DATA, Remain}];
93sua_to_sccp_param(_, _, ?SUA_IEI_ROUTE_CTX, Remain) ->
94 %FIXME: what to do with routing context?
95 [].
Harald Welte50dfc192012-01-17 15:11:37 +010096
97sccp_to_sua_params(#sccp_msg{msg_type=Type, parameters=Params}) ->
98 sccp_to_sua_params(Type, Params).
99sccp_to_sua_params(Type, Params) when is_list(Params) ->
100 sccp_to_sua_params(Type, Params, []).
101sccp_to_sua_params(Type, [], List) ->
102 List;
103sccp_to_sua_params(Type, [{ParTag, ParVal}|Tail], List) ->
104 NewPars = sccp_to_sua_param(Type, ParTag, ParVal),
105 sccp_to_sua_params(Type, Tail, List ++ NewPars).
106
107sccp_to_sua_param(_, ?SCCP_PNC_PROTOCOL_CLASS, Class) ->
108 [{?SUA_IEI_PROTO_CLASS, <<0:24, 0:1, 0:5, Class:2>>}];
109sccp_to_sua_param(_, ?SCCP_PNC_CALLING_PARTY_ADDRESS, Addr) ->
110 AddrSua = sccp_to_sua_addr(Addr),
111 [{?SUA_IEI_SRC_ADDR, AddrSua}];
112sccp_to_sua_param(_, ?SCCP_PNC_CALLED_PARTY_ADDRESS, Addr) ->
113 AddrSua = sccp_to_sua_addr(Addr),
114 [{?SUA_IEI_DEST_ADDR, AddrSua}];
115sccp_to_sua_param(_, ?SCCP_PNC_SEQUENCING, Par) ->
116 [{?SUA_IEI_SEQ_CTRL, Par}];
117sccp_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>>}];
121sccp_to_sua_param(_, ?SCCP_PNC_DATA, Data) ->
122 [{?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,
199 trans_type = TransType, encoding = fixme,
200 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,
204 trans_type = TransType, encoding = Encoding,
205 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,
Harald Welte042a5792012-01-18 00:09:51 +0100220 io:format("NoDigits=~p (~p)~n", [NoDigits, Remain]),
Harald Welte50dfc192012-01-17 15:11:37 +0100221 OddEven = NoDigits rem 1,
222 case OddEven of
223 0 ->
Harald Welte042a5792012-01-18 00:09:51 +0100224 ByteLen = NoDigits div 2;
Harald Welte50dfc192012-01-17 15:11:37 +0100225 1 ->
Harald Welte042a5792012-01-18 00:09:51 +0100226 ByteLen = NoDigits div 2 + 1
Harald Welte50dfc192012-01-17 15:11:37 +0100227 end,
228 <<Bin:ByteLen/binary, _/binary>> = Remain,
229 isup_codec:parse_isup_party(Bin, OddEven).
230encode_sua_gt_digits(Digits) when is_list(Digits); is_integer(Digits) ->
231 % Assume that overall option encoder will do the padding...
Harald Welte8e92c9a2012-01-18 00:25:26 +0100232 {Enc, _OddEven} = isup_codec:encode_isup_party(Digits),
233 Enc.
Harald Welte50dfc192012-01-17 15:11:37 +0100234
235parse_sua_pc(<<PC:32/big>>) ->
236 PC.
237encode_sua_pc(Pc) when is_integer(Pc) ->
238 <<Pc:32/big>>.
239
240parse_sua_ssn(<<_:24, SSN:8>>) ->
241 SSN.
242encode_sua_ssn(Ssn) when is_integer(Ssn) ->
243 <<0:24, Ssn:8>>.