blob: fa2119404aa1f0b7fd52ce4eb52e90557a80dd6a [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
Harald Welteba6fdbb2011-01-23 22:04:39 +010024-export([parse_isup_msg/1, encode_isup_msg/1, parse_isup_party/2]).
Harald Welte50a44c22011-01-15 21:39:20 +010025
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) ->
Harald Welte548f93b2011-01-21 16:06:49 +0000295 <<OutBin/binary, 0:8>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100296encode_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 Welteed4c9ea2011-01-21 16:58:19 +0000306% Default case: no fixed and no variable parts, only options
307% ANM, RLC, FOT
308encode_isup_msgt(M, #isup_msg{parameters = Params}) when
309 M == ?ISUP_MSGT_ANM;
310 M == ?ISUP_MSGT_RLC;
311 M == ?ISUP_MSGT_FOT ->
312 encode_isup_opts(Params);
Harald Weltef48736b2011-01-21 14:34:32 +0100313% Table C-5 Address complete
314encode_isup_msgt(?ISUP_MSGT_ACM, #isup_msg{parameters = Params}) ->
315 BackCallInd = proplists:get_value(backward_call_ind, Params),
316 OptBin = encode_isup_opts(Params),
317 case OptBin of
318 <<>> -> PtrOpt = 0;
319 _ -> PtrOpt = 1
320 end,
321 <<BackCallInd:16, PtrOpt:8, OptBin/binary>>;
322% Table C-7 Call progress
323encode_isup_msgt(?ISUP_MSGT_CPG, #isup_msg{parameters = Params}) ->
324 EventInf = proplists:get_value(event_info, Params),
325 OptBin = encode_isup_opts(Params),
326 case OptBin of
327 <<>> -> PtrOpt = 0;
328 _ -> PtrOpt = 1
329 end,
330 <<EventInf:8, PtrOpt:8, OptBin/binary>>;
331% Table C-9 Circuit group reset acknowledgement
332encode_isup_msgt(?ISUP_MSGT_GRA, #isup_msg{parameters = Params}) ->
333 % V: Range and status
334 {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
335 <<1:8, RangStsLen:8, RangeStatus/binary>>;
336% Table C-11 Connect
337encode_isup_msgt(?ISUP_MSGT_CON, #isup_msg{parameters = Params}) ->
338 BackCallInd = proplists:get_value(backward_call_ind, Params),
339 OptBin = encode_isup_opts(Params),
340 case OptBin of
341 <<>> -> PtrOpt = 0;
342 _ -> PtrOpt = 1
343 end,
344 <<BackCallInd:16, PtrOpt:8, OptBin/binary>>;
345% Table C-12 Continuity
346encode_isup_msgt(?ISUP_MSGT_COT, #isup_msg{parameters = Params}) ->
347 ContInd = proplists:get_value(continuity_ind, Params),
348 <<ContInd:8>>;
Harald Welte84cc60d2011-01-18 18:25:53 +0100349% Table C-16 Initial address
350encode_isup_msgt(?ISUP_MSGT_IAM, #isup_msg{parameters = Params}) ->
351 % Fixed part
352 CINat = proplists:get_value(conn_ind_nature, Params),
353 FwCallInd = proplists:get_value(fw_call_ind, Params),
354 CallingCat = proplists:get_value(calling_cat, Params),
355 TransmReq = proplists:get_value(transm_medium_req, Params),
356 PtrVar = 2, % one byte behind the PtrOpt
357 FixedBin = <<CINat:8, FwCallInd:16/big, CallingCat:8, TransmReq:8>>,
358 % V: Called Party Number
359 CalledParty = encode_isup_par(?ISUP_PAR_CALLED_P_NUM,
360 proplists:get_value(?ISUP_PAR_CALLED_P_NUM, Params)),
361 CalledPartyLen = byte_size(CalledParty),
362 % Optional part
Harald Welted1cb16f2011-01-21 15:48:34 +0000363 Params2 = proplists:delete(?ISUP_PAR_CALLED_P_NUM, Params),
364 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100365 case OptBin of
366 <<>> -> PtrOpt = 0;
367 _ -> PtrOpt = CalledPartyLen + 1 + 1 % 1 byte length, 1 byte start offset
368 end,
369 <<FixedBin/binary, PtrVar:8, PtrOpt:8, CalledPartyLen:8, CalledParty/binary, OptBin/binary>>;
370% Table C-17 Release
371encode_isup_msgt(?ISUP_MSGT_REL, #isup_msg{parameters = Params}) ->
372 PtrVar = 2, % one byte behind the PtrOpt
373 % V: Cause indicators
374 CauseInd = encode_isup_par(?ISUP_PAR_CAUSE_IND,
375 proplists:get_value(?ISUP_PAR_CAUSE_IND, Params)),
376 CauseIndLen = byte_size(CauseInd),
377 % Optional Part
Harald Welted1cb16f2011-01-21 15:48:34 +0000378 Params2 = proplists:delete(?ISUP_PAR_CAUSE_IND, Params),
379 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100380 case OptBin of
381 <<>> -> PtrOpt = 0;
382 _ -> PtrOpt = CauseIndLen + 1 + 1 % 1 byte length, 1 byte start offset
383 end,
384 <<PtrVar:8, PtrOpt:8, CauseIndLen:8, CauseInd/binary, OptBin/binary>>;
385% Table C-19 Subsequent address
386encode_isup_msgt(?ISUP_MSGT_SAM, #isup_msg{parameters = Params}) ->
387 PtrVar = 2, % one byte behind the PtrOpt
388 % V: Subsequent number
389 SubseqNum = encode_isup_par(?ISUP_PAR_SUBSEQ_NUM,
390 proplists:get_value(?ISUP_PAR_SUBSEQ_NUM, Params)),
391 SubseqNumLen = byte_size(SubseqNum),
392 % Optional Part
Harald Welted1cb16f2011-01-21 15:48:34 +0000393 Params2 = proplists:delete(?ISUP_PAR_SUBSEQ_NUM, Params),
394 OptBin = encode_isup_opts(Params2),
Harald Weltef48736b2011-01-21 14:34:32 +0100395 case OptBin of
396 <<>> -> PtrOpt = 0;
397 _ -> PtrOpt = SubseqNumLen + 1 + 1 % 1 byte length, 1 byte start offset
398 end,
399 <<PtrVar:8, PtrOpt:8, SubseqNumLen:8, SubseqNum/binary, OptBin/binary>>;
400% Table C-21 Suspend, Resume
401encode_isup_msgt(Msgt, #isup_msg{parameters = Params}) when Msgt == ?ISUP_MSGT_RES; Msgt == ?ISUP_MSGT_SUS ->
402 SuspResInd = proplists:get_value(susp_res_ind, Params),
403 OptBin = encode_isup_opts(Params),
404 case OptBin of
405 <<>> -> PtrOpt = 0;
406 _ -> PtrOpt = 1
407 end,
408 <<SuspResInd:8, PtrOpt:8, OptBin/binary>>;
409% Table C-23
410encode_isup_msgt(M, #isup_msg{}) when
411 M == ?ISUP_MSGT_BLO;
412 M == ?ISUP_MSGT_BLA;
413 M == ?ISUP_MSGT_CCR;
414 M == ?ISUP_MSGT_RSC;
415 M == ?ISUP_MSGT_UBL;
416 M == ?ISUP_MSGT_UBA ->
417 <<>>;
418% Table C-25
419encode_isup_msgt(M, #isup_msg{parameters = Params}) when
420 M == ?ISUP_MSGT_CGB;
421 M == ?ISUP_MSGT_CGBA;
422 M == ?ISUP_MSGT_CGU;
423 M == ?ISUP_MSGT_CGUA ->
424 PtrVar = 1, % one byte behind the PtrVar
425 CGMsgt = proplists:get_value(cg_supv_msgt, Params),
426 % V: Range and status
427 {RangStsLen, RangeStatus} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
428 <<CGMsgt:8, PtrVar:8, RangStsLen:8, RangeStatus/binary>>;
429% Table C-26 Circuit group reset
430encode_isup_msgt(?ISUP_MSGT_GRS, #isup_msg{parameters = Params}) ->
431 PtrVar = 1, % one byte behind the PtrVar
432 {RangeLen, Range} = proplists:get_value(?ISUP_PAR_RANGE_AND_STATUS, Params),
433 % V: Range without status
434 <<PtrVar:8, RangeLen:8, Range/binary>>.
Harald Welte84cc60d2011-01-18 18:25:53 +0100435
436encode_isup_msg(Msg = #isup_msg{msg_type = MsgType}) ->
437 HdrBin = encode_isup_hdr(Msg),
438 Remain = encode_isup_msgt(MsgType, Msg),
439 <<HdrBin/binary, Remain/binary>>.