blob: dbb68ed92da5c4a4fa77cda0fc78cef6916673d7 [file] [log] [blame]
Harald Welte50a44c22011-01-15 21:39:20 +01001% ITU-T Q.76x ISUPcoding / decoding
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-module(isup_codec).
21-author('Harald Welte <laforge@gnumonks.org>').
22-include("isup.hrl").
23
24-export([parse_isup_msg/1, encode_isup_msg/1]).
25
Harald Welte01f8ea32011-01-17 21:30:42 +010026-compile(export_all).
27
Harald Weltede30a872011-01-16 17:12:56 +010028parse_isup_party(<<>>, OddEven, DigitList) ->
29 % in case of odd number of digits, we need to cut the last
30 case OddEven of
31 1 ->
Harald Welte01f8ea32011-01-17 21:30:42 +010032 lists:sublist(DigitList, length(DigitList)-1);
Harald Weltede30a872011-01-16 17:12:56 +010033 0 ->
34 DigitList
35 end;
36parse_isup_party(BcdBin, OddEven, DigitList) ->
37 <<Second:4, First:4, Remain/binary>> = BcdBin,
38 NewDigits = [First, Second],
39 parse_isup_party(Remain, OddEven, DigitList ++ NewDigits).
40
41parse_isup_party(BinBcd, OddEven) when is_binary(BinBcd) ->
42 parse_isup_party(BinBcd, OddEven, []).
43
44
45% parse a single option
Harald Welte01f8ea32011-01-17 21:30:42 +010046parse_isup_opt(OptType = ?ISUP_PAR_CALLED_P_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010047 % C.3.7 Called Party Number
48 <<OddEven:1, Nature:7, Inn:1, NumPlan:3, 0:4, Remain/binary>> = Content,
49 PhoneNum = parse_isup_party(Remain, OddEven),
50 {OptType, #party_number{nature_of_addr_ind = Nature,
51 internal_net_num = Inn,
52 numbering_plan = NumPlan,
53 phone_number = PhoneNum}};
Harald Welte01f8ea32011-01-17 21:30:42 +010054parse_isup_opt(OptType = ?ISUP_PAR_CALLING_P_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010055 % C.3.8 Calling Party Number
56 <<OddEven:1, Nature:7, Ni:1, NumPlan:3, PresRestr:2, Screen:2, Remain/binary>> = Content,
57 PhoneNum = parse_isup_party(Remain, OddEven),
58 {OptType, #party_number{nature_of_addr_ind = Nature,
59 number_incompl_ind = Ni,
60 numbering_plan = NumPlan,
61 present_restrict = PresRestr,
62 screening_ind = Screen,
63 phone_number = PhoneNum}};
Harald Welte01f8ea32011-01-17 21:30:42 +010064parse_isup_opt(OptType = ?ISUP_PAR_CONNECTED_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010065 % C.3.14 Connected Number
66 <<OddEven:1, Nature:7, 0:1, NumPlan:3, PresRestr:2, Screen:2, Remain/binary>> = Content,
67 PhoneNum = parse_isup_party(Remain, OddEven),
68 {OptType, #party_number{nature_of_addr_ind = Nature,
69 numbering_plan = NumPlan,
70 present_restrict = PresRestr,
71 screening_ind = Screen,
72 phone_number = PhoneNum}};
Harald Welte01f8ea32011-01-17 21:30:42 +010073parse_isup_opt(OptType = ?ISUP_PAR_SUBSEQ_NUM, _OptLen, Content) ->
Harald Weltede30a872011-01-16 17:12:56 +010074 % C.3.32 Subsequent Number
Harald Welte01f8ea32011-01-17 21:30:42 +010075 <<OddEven:1, 0:7, Remain/binary>> = Content,
Harald Weltede30a872011-01-16 17:12:56 +010076 PhoneNum = parse_isup_party(Remain, OddEven),
77 {OptType, #party_number{phone_number = PhoneNum}};
78parse_isup_opt(OptType, OptLen, Content) ->
79 {OptType, {OptLen, Content}}.
80
81% parse a Binary into a list of options
82parse_isup_opts(<<>>, OptList) ->
83 % empty list
84 OptList;
85parse_isup_opts(<<0>>, OptList) ->
86 % end of options
87 OptList;
88parse_isup_opts(OptBin, OptList) when is_binary(OptBin) ->
89 <<OptType:8, OptLen:8, Content:OptLen/binary, Remain/binary>> = OptBin,
90 NewOpt = parse_isup_opt(OptType, OptLen, Content),
Harald Welte01f8ea32011-01-17 21:30:42 +010091 parse_isup_opts(Remain, OptList ++ [NewOpt]).
92parse_isup_opts(OptBin) ->
93 parse_isup_opts(OptBin, []).
Harald Weltede30a872011-01-16 17:12:56 +010094
Harald Weltef48736b2011-01-21 14:34:32 +010095% Parse options preceeded by 1 byte OptPtr
96parse_isup_opts_ptr(OptBinPtr) ->
97 OptPtr = binary:at(OptBinPtr, 0),
98 case OptPtr of
99 0 ->
100 [];
101 _ ->
102 OptBin = binary:part(OptBinPtr, OptPtr, byte_size(OptBinPtr)-OptPtr),
103 parse_isup_opts(OptBin, [])
104 end.
105
Harald Welte50a44c22011-01-15 21:39:20 +0100106% References to 'Tabe C-xxx' are to Annex C of Q.767
107
108% Default case: no fixed and no variable parts, only options
109% ANM, RLC, FOT
110parse_isup_msgt(M, Bin) when
111 M == ?ISUP_MSGT_ANM;
112 M == ?ISUP_MSGT_RLC;
Harald Weltede30a872011-01-16 17:12:56 +0100113 M == ?ISUP_MSGT_FOT ->
Harald Weltef48736b2011-01-21 14:34:32 +0100114 parse_isup_opts_ptr(Bin);
Harald Welte50a44c22011-01-15 21:39:20 +0100115% Table C-5 Address complete
116parse_isup_msgt(?ISUP_MSGT_ACM, Bin) ->
117 <<BackCallInd:16, Remain/binary>> = Bin,
118 BciOpt = {backward_call_ind, BackCallInd},
Harald Weltef48736b2011-01-21 14:34:32 +0100119 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100120 [BciOpt|Opts];
121% Table C-7 Call progress
122parse_isup_msgt(?ISUP_MSGT_CPG, Bin) ->
123 <<EventInf:8, Remain/binary>> = Bin,
124 BciOpt = {event_info, EventInf},
Harald Weltef48736b2011-01-21 14:34:32 +0100125 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100126 [BciOpt|Opts];
127% Table C-9 Circuit group reset acknowledgement
128parse_isup_msgt(?ISUP_MSGT_GRA, Bin) ->
129 % V: Range and status
Harald Welte01f8ea32011-01-17 21:30:42 +0100130 <<PtrVar:8, Remain/binary>> = Bin,
131 RangStsLen = binary:at(Remain, PtrVar),
132 RangeStatus = binary:part(Remain, PtrVar+1, RangStsLen),
133 RangeStsTuple = {?ISUP_PAR_RANGE_AND_STATUS, {RangStsLen, RangeStatus}},
134 [RangeStsTuple];
Harald Welte50a44c22011-01-15 21:39:20 +0100135% Table C-11 Connect
136parse_isup_msgt(?ISUP_MSGT_CON, Bin) ->
137 <<BackCallInd:16, Remain/binary>> = Bin,
138 BciOpt = {backward_call_ind, BackCallInd},
Harald Weltef48736b2011-01-21 14:34:32 +0100139 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100140 [BciOpt|Opts];
141% Table C-12 Continuity
142parse_isup_msgt(?ISUP_MSGT_COT, Bin) ->
143 <<ContInd:8>> = Bin,
144 [{continuity_ind, ContInd}];
145% Table C-16 Initial address
146parse_isup_msgt(?ISUP_MSGT_IAM, Bin) ->
147 <<CINat:8, FwCallInd:16/big, CallingCat:8, TransmReq:8, VarAndOpt/binary>> = Bin,
148 FixedOpts = [{conn_ind_nature, CINat}, {fw_call_ind, FwCallInd}, {calling_cat, CallingCat},
149 {transm_medium_req, TransmReq}],
Harald Welte01f8ea32011-01-17 21:30:42 +0100150 <<PtrVar:8, PtrOpt:8, _/binary>> = VarAndOpt,
Harald Welte50a44c22011-01-15 21:39:20 +0100151 % V: Called Party Number
Harald Welte01f8ea32011-01-17 21:30:42 +0100152 CalledPartyLen = binary:at(VarAndOpt, PtrVar),
153 CalledParty = binary:part(VarAndOpt, PtrVar+1, CalledPartyLen),
154 VarOpts = [parse_isup_opt(?ISUP_PAR_CALLED_P_NUM, CalledPartyLen, CalledParty)],
155 % Optional part
Harald Welte86494e82011-01-21 12:13:35 +0000156 case PtrOpt of
157 0 ->
158 Opts = [];
159 _ ->
160 Remain = binary:part(VarAndOpt, 1 + PtrOpt, byte_size(VarAndOpt)-(1+PtrOpt)),
161 Opts = parse_isup_opts(Remain)
162 end,
Harald Welte01f8ea32011-01-17 21:30:42 +0100163 FixedOpts ++ VarOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100164% Table C-17 Release
165parse_isup_msgt(?ISUP_MSGT_REL, Bin) ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100166 <<PtrVar:8, PtrOpt:8, VarAndOpt/binary>> = Bin,
Harald Welte50a44c22011-01-15 21:39:20 +0100167 % V: Cause indicators
Harald Welte86494e82011-01-21 12:13:35 +0000168 CauseIndLen = binary:at(VarAndOpt, PtrVar-2),
169 CauseInd = binary:part(VarAndOpt, PtrVar-1, CauseIndLen),
170 VarOpts = [{?ISUP_PAR_CAUSE_IND, {CauseIndLen, CauseInd}}],
171 case PtrOpt of
172 0 ->
173 Opts = [];
174 _ ->
175 Remain = binary:part(VarAndOpt, 1 + PtrOpt, byte_size(VarAndOpt)-(1+PtrOpt)),
176 Opts = parse_isup_opts(Remain)
177 end,
Harald Welte01f8ea32011-01-17 21:30:42 +0100178 VarOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100179% Table C-19 Subsequent address
180parse_isup_msgt(?ISUP_MSGT_SAM, Bin) ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100181 <<PtrVar:8, PtrOpt:8, VarAndOpt/binary>> = Bin,
Harald Welte50a44c22011-01-15 21:39:20 +0100182 % V: Subsequent number
Harald Welte01f8ea32011-01-17 21:30:42 +0100183 SubseqNumLen = binary:at(VarAndOpt, PtrVar),
184 SubsetNum = binary:part(VarAndOpt, PtrVar+1, SubseqNumLen),
185 VarOpts = [{?ISUP_PAR_SUBSEQ_NUM, {SubseqNumLen, SubsetNum}}],
186 Remain = binary:part(VarAndOpt, 1 + PtrOpt, byte_size(VarAndOpt)-(1+PtrOpt)),
Harald Welte50a44c22011-01-15 21:39:20 +0100187 Opts = parse_isup_opts(Remain),
Harald Welte01f8ea32011-01-17 21:30:42 +0100188 VarOpts ++ Opts;
Harald Welte50a44c22011-01-15 21:39:20 +0100189% Table C-21 Suspend, Resume
Harald Weltede30a872011-01-16 17:12:56 +0100190parse_isup_msgt(Msgt, Bin) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS ->
Harald Welte50a44c22011-01-15 21:39:20 +0100191 <<SuspResInd:8, Remain/binary>> = Bin,
192 FixedOpts = [{susp_res_ind, SuspResInd}],
Harald Weltef48736b2011-01-21 14:34:32 +0100193 Opts = parse_isup_opts_ptr(Remain),
Harald Welte50a44c22011-01-15 21:39:20 +0100194 [FixedOpts|Opts];
195% Table C-23
196parse_isup_msgt(M, <<>>) when
197 M == ?ISUP_MSGT_BLO;
198 M == ?ISUP_MSGT_BLA;
199 M == ?ISUP_MSGT_CCR;
200 M == ?ISUP_MSGT_RSC;
201 M == ?ISUP_MSGT_UBL;
202 M == ?ISUP_MSGT_UBA ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100203 [];
Harald Welte50a44c22011-01-15 21:39:20 +0100204% Table C-25
205parse_isup_msgt(M, Bin) when
206 M == ?ISUP_MSGT_CGB;
207 M == ?ISUP_MSGT_CGBA;
Harald Welte01f8ea32011-01-17 21:30:42 +0100208 M == ?ISUP_MSGT_CGU;
209 M == ?ISUP_MSGT_CGUA ->
210 <<CGMsgt:8, PtrVar:8, VarBin/binary>> = Bin,
Harald Welte50a44c22011-01-15 21:39:20 +0100211 FixedOpts = [{cg_supv_msgt, CGMsgt}],
212 % V: Range and status
Harald Welte01f8ea32011-01-17 21:30:42 +0100213 RangStsLen = binary:at(VarBin, PtrVar),
214 RangeStatus = binary:part(VarBin, PtrVar+1, RangStsLen),
215 VarOpts = [{?ISUP_PAR_RANGE_AND_STATUS, {RangStsLen, RangeStatus}}],
216 FixedOpts ++ VarOpts;
Harald Welte50a44c22011-01-15 21:39:20 +0100217% Table C-26 Circuit group reset
218parse_isup_msgt(?ISUP_MSGT_GRS, Bin) ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100219 <<PtrVar:8, VarBin/binary>> = Bin,
Harald Welte50a44c22011-01-15 21:39:20 +0100220 % V: Range without status
Harald Welte01f8ea32011-01-17 21:30:42 +0100221 RangeLen = binary:at(VarBin, PtrVar),
222 Range = binary:part(VarBin, PtrVar+1, RangeLen),
223 [{?ISUP_PAR_RANGE_AND_STATUS, {RangeLen, Range}}].
Harald Welte50a44c22011-01-15 21:39:20 +0100224
225
Harald Welte01f8ea32011-01-17 21:30:42 +0100226parse_isup_msg(DataBin) when is_binary(DataBin) ->
227 <<Cic:12/little, 0:4, MsgType:8, Remain/binary>> = DataBin,
Harald Welte50a44c22011-01-15 21:39:20 +0100228 Opts = parse_isup_msgt(MsgType, Remain),
229 #isup_msg{cic = Cic, msg_type = MsgType, parameters = Opts}.
Harald Weltede30a872011-01-16 17:12:56 +0100230
231
Harald Welte01f8ea32011-01-17 21:30:42 +0100232% encode a phone number from a list of digits into the BCD binary sequence
Harald Weltede30a872011-01-16 17:12:56 +0100233encode_isup_party(BcdList) ->
Harald Welte01f8ea32011-01-17 21:30:42 +0100234 encode_isup_party(BcdList, <<>>, length(BcdList)).
Harald Weltede30a872011-01-16 17:12:56 +0100235encode_isup_party([], Bin, NumDigits) ->
236 case NumDigits rem 2 of
237 1 ->
238 {Bin, 1};
239 0 ->
240 {Bin, 0}
241 end;
242encode_isup_party([First,Second|BcdList], Bin, NumDigits) ->
Harald Welte84cc60d2011-01-18 18:25:53 +0100243 encode_isup_party(BcdList, <<Bin/binary, Second:4, First:4>>, NumDigits);
244encode_isup_party([Last], Bin, NumDigits) ->
245 encode_isup_party([], <<Bin/binary, 0:4, Last:4>>, NumDigits).
Harald Welte01f8ea32011-01-17 21:30:42 +0100246
Harald Weltede30a872011-01-16 17:12:56 +0100247% encode a single option
Harald Welte84cc60d2011-01-18 18:25:53 +0100248encode_isup_par(?ISUP_PAR_CALLED_P_NUM,
Harald Weltede30a872011-01-16 17:12:56 +0100249 #party_number{nature_of_addr_ind = Nature,
250 internal_net_num = Inn,
251 numbering_plan = NumPlan,
252 phone_number= PhoneNum}) ->
253 % C.3.7 Called Party Number
254 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
Harald Welte01f8ea32011-01-17 21:30:42 +0100255 <<OddEven:1, Nature:7, Inn:1, NumPlan:3, 0:4, PhoneBin/binary>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100256encode_isup_par(?ISUP_PAR_CALLING_P_NUM,
Harald Weltede30a872011-01-16 17:12:56 +0100257 #party_number{nature_of_addr_ind = Nature,
258 number_incompl_ind = Ni,
259 numbering_plan = NumPlan,
260 present_restrict = PresRestr,
261 screening_ind = Screen,
262 phone_number= PhoneNum}) ->
263 % C.3.8 Calling Party Number
264 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
265 <<OddEven:1, Nature:7, Ni:1, NumPlan:3, PresRestr:2, Screen:2, PhoneBin/binary>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100266encode_isup_par(?ISUP_PAR_CONNECTED_NUM,
Harald Weltede30a872011-01-16 17:12:56 +0100267 #party_number{nature_of_addr_ind = Nature,
268 numbering_plan = NumPlan,
269 present_restrict = PresRestr,
270 screening_ind = Screen,
271 phone_number = PhoneNum}) ->
272 % C.3.14 Connected Number
273 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
274 <<OddEven:1, Nature:7, 0:1, NumPlan:3, PresRestr:2, Screen:2, PhoneBin/binary>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100275encode_isup_par(?ISUP_PAR_SUBSEQ_NUM,
276 #party_number{phone_number = PhoneNum}) ->
277 % C.3.32 Subsequent Number
278 {PhoneBin, OddEven} = encode_isup_party(PhoneNum),
279 <<OddEven:1, 0:7, PhoneBin/binary>>;
Harald Weltef48736b2011-01-21 14:34:32 +0100280encode_isup_par(Atom, _More) when is_atom(Atom) ->
Harald Welte84cc60d2011-01-18 18:25:53 +0100281 <<>>;
282encode_isup_par(OptNum, {OptLen, Binary}) when is_binary(Binary), is_integer(OptNum), is_integer(OptLen) ->
Harald Weltede30a872011-01-16 17:12:56 +0100283 Binary.
284
Harald Welte84cc60d2011-01-18 18:25:53 +0100285% encode a single OPTIONAL parameter (TLV type), skip all others
Harald Weltef48736b2011-01-21 14:34:32 +0100286encode_isup_optpar(ParNum, _ParBody) when is_atom(ParNum) ->
Harald Welte84cc60d2011-01-18 18:25:53 +0100287 <<>>;
288encode_isup_optpar(ParNum, ParBody) ->
289 ParBin = encode_isup_par(ParNum, ParBody),
290 ParLen = byte_size(ParBin),
291 <<ParNum:8, ParLen:8, ParBin/binary>>.
292
Harald Weltef48736b2011-01-21 14:34:32 +0100293% recursive function to encode all optional parameters
Harald Welte84cc60d2011-01-18 18:25:53 +0100294encode_isup_opts([], OutBin) ->
295 OutBin;
296encode_isup_opts([Opt|OptPropList], OutBin) ->
297 {OptType, OptBody} = Opt,
298 OptBin = encode_isup_optpar(OptType, OptBody),
299 encode_isup_opts(OptPropList, <<OutBin/binary, OptBin/binary>>).
300encode_isup_opts(OptPropList) ->
301 encode_isup_opts(OptPropList, <<>>).
302
303encode_isup_hdr(#isup_msg{msg_type = MsgType, cic = Cic}) ->
304 <<Cic:12/little, 0:4, MsgType:8>>.
305
Harald Weltef48736b2011-01-21 14:34:32 +0100306% Table C-5 Address complete
307encode_isup_msgt(?ISUP_MSGT_ACM, #isup_msg{parameters = Params}) ->
308 BackCallInd = proplists:get_value(backward_call_ind, Params),
309 OptBin = encode_isup_opts(Params),
310 case OptBin of
311 <<>> -> PtrOpt = 0;
312 _ -> PtrOpt = 1
313 end,
314 <<BackCallInd:16, PtrOpt:8, OptBin/binary>>;
315% Table C-7 Call progress
316encode_isup_msgt(?ISUP_MSGT_CPG, #isup_msg{parameters = Params}) ->
317 EventInf = proplists:get_value(event_info, Params),
318 OptBin = encode_isup_opts(Params),
319 case OptBin of
320 <<>> -> PtrOpt = 0;
321 _ -> PtrOpt = 1
322 end,
323 <<EventInf:8, PtrOpt:8, OptBin/binary>>;
324% Table C-9 Circuit group reset acknowledgement
325encode_isup_msgt(?ISUP_MSGT_GRA, #isup_msg{parameters = Params}) ->
326 % V: Range and status
327 {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
328 <<1:8, RangStsLen:8, RangeStatus/binary>>;
329% Table C-11 Connect
330encode_isup_msgt(?ISUP_MSGT_CON, #isup_msg{parameters = Params}) ->
331 BackCallInd = proplists:get_value(backward_call_ind, Params),
332 OptBin = encode_isup_opts(Params),
333 case OptBin of
334 <<>> -> PtrOpt = 0;
335 _ -> PtrOpt = 1
336 end,
337 <<BackCallInd:16, PtrOpt:8, OptBin/binary>>;
338% Table C-12 Continuity
339encode_isup_msgt(?ISUP_MSGT_COT, #isup_msg{parameters = Params}) ->
340 ContInd = proplists:get_value(continuity_ind, Params),
341 <<ContInd:8>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100342% Table C-16 Initial address
343encode_isup_msgt(?ISUP_MSGT_IAM, #isup_msg{parameters = Params}) ->
344 % Fixed part
345 CINat = proplists:get_value(conn_ind_nature, Params),
346 FwCallInd = proplists:get_value(fw_call_ind, Params),
347 CallingCat = proplists:get_value(calling_cat, Params),
348 TransmReq = proplists:get_value(transm_medium_req, Params),
349 PtrVar = 2, % one byte behind the PtrOpt
350 FixedBin = <<CINat:8, FwCallInd:16/big, CallingCat:8, TransmReq:8>>,
351 % V: Called Party Number
352 CalledParty = encode_isup_par(?ISUP_PAR_CALLED_P_NUM,
353 proplists:get_value(?ISUP_PAR_CALLED_P_NUM, Params)),
354 CalledPartyLen = byte_size(CalledParty),
355 % Optional part
Harald Welted1cb16f2011-01-21 15:48:34 +0000356 Params2 = proplists:delete(?ISUP_PAR_CALLED_P_NUM, Params),
357 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100358 case OptBin of
359 <<>> -> PtrOpt = 0;
360 _ -> PtrOpt = CalledPartyLen + 1 + 1 % 1 byte length, 1 byte start offset
361 end,
362 <<FixedBin/binary, PtrVar:8, PtrOpt:8, CalledPartyLen:8, CalledParty/binary, OptBin/binary>>;
363% Table C-17 Release
364encode_isup_msgt(?ISUP_MSGT_REL, #isup_msg{parameters = Params}) ->
365 PtrVar = 2, % one byte behind the PtrOpt
366 % V: Cause indicators
367 CauseInd = encode_isup_par(?ISUP_PAR_CAUSE_IND,
368 proplists:get_value(?ISUP_PAR_CAUSE_IND, Params)),
369 CauseIndLen = byte_size(CauseInd),
370 % Optional Part
Harald Welted1cb16f2011-01-21 15:48:34 +0000371 Params2 = proplists:delete(?ISUP_PAR_CAUSE_IND, Params),
372 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100373 case OptBin of
374 <<>> -> PtrOpt = 0;
375 _ -> PtrOpt = CauseIndLen + 1 + 1 % 1 byte length, 1 byte start offset
376 end,
377 <<PtrVar:8, PtrOpt:8, CauseIndLen:8, CauseInd/binary, OptBin/binary>>;
378% Table C-19 Subsequent address
379encode_isup_msgt(?ISUP_MSGT_SAM, #isup_msg{parameters = Params}) ->
380 PtrVar = 2, % one byte behind the PtrOpt
381 % V: Subsequent number
382 SubseqNum = encode_isup_par(?ISUP_PAR_SUBSEQ_NUM,
383 proplists:get_value(?ISUP_PAR_SUBSEQ_NUM, Params)),
384 SubseqNumLen = byte_size(SubseqNum),
385 % Optional Part
Harald Welted1cb16f2011-01-21 15:48:34 +0000386 Params2 = proplists:delete(?ISUP_PAR_SUBSEQ_NUM, Params),
387 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100388 case OptBin of
389 <<>> -> PtrOpt = 0;
390 _ -> PtrOpt = SubseqNumLen + 1 + 1 % 1 byte length, 1 byte start offset
391 end,
392 <<PtrVar:8, PtrOpt:8, SubseqNumLen:8, SubseqNum/binary, OptBin/binary>>;
393% Table C-21 Suspend, Resume
394encode_isup_msgt(Msgt, #isup_msg{parameters = Params}) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS ->
395 SuspResInd = proplists:get_value(susp_res_ind, Params),
396 OptBin = encode_isup_opts(Params),
397 case OptBin of
398 <<>> -> PtrOpt = 0;
399 _ -> PtrOpt = 1
400 end,
401 <<SuspResInd:8, PtrOpt:8, OptBin/binary>>;
402% Table C-23
403encode_isup_msgt(M, #isup_msg{}) when
404 M == ?ISUP_MSGT_BLO;
405 M == ?ISUP_MSGT_BLA;
406 M == ?ISUP_MSGT_CCR;
407 M == ?ISUP_MSGT_RSC;
408 M == ?ISUP_MSGT_UBL;
409 M == ?ISUP_MSGT_UBA ->
410 <<>>;
411% Table C-25
412encode_isup_msgt(M, #isup_msg{parameters = Params}) when
413 M == ?ISUP_MSGT_CGB;
414 M == ?ISUP_MSGT_CGBA;
415 M == ?ISUP_MSGT_CGU;
416 M == ?ISUP_MSGT_CGUA ->
417 PtrVar = 1, % one byte behind the PtrVar
418 CGMsgt = proplists:get_value(cg_supv_msgt, Params),
419 % V: Range and status
420 {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
421 <<CGMsgt:8, PtrVar:8, RangStsLen:8, RangeStatus/binary>>;
422% Table C-26 Circuit group reset
423encode_isup_msgt(?ISUP_MSGT_GRS, #isup_msg{parameters = Params}) ->
424 PtrVar = 1, % one byte behind the PtrVar
425 {RangeLen, Range} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
426 % V: Range without status
427 <<PtrVar:8, RangeLen:8, Range/binary>>.
Harald Welte84cc60d2011-01-18 18:25:53 +0100428
429encode_isup_msg(Msg = #isup_msg{msg_type = MsgType}) ->
430 HdrBin = encode_isup_hdr(Msg),
431 Remain = encode_isup_msgt(MsgType, Msg),
432 <<HdrBin/binary, Remain/binary>>.